Skip to content

Commit

Permalink
Merge pull request #143 from smtadmin/apmsmt-422
Browse files Browse the repository at this point in the history
Sonarqube Cleanup and slight version bump
  • Loading branch information
YetAnotherMask authored Aug 25, 2023
2 parents 3008645 + 228310f commit a83ae9f
Show file tree
Hide file tree
Showing 15 changed files with 112 additions and 221 deletions.
6 changes: 3 additions & 3 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ plugins {
// Apply the java-library plugin to add support for Java Library
id 'java'
id 'jacoco'
id 'org.springframework.boot' version '3.1.2'
id 'org.springframework.boot' version '3.1.3'
id 'io.spring.dependency-management' version '1.1.3'
id 'io.freefair.lombok' version '8.2.2'
id 'signing'
Expand All @@ -25,8 +25,8 @@ group = 'com.siliconmtn'
* For 'release' publishing, use: version = n.n.n
*
*/
version = '2.0.0-SNAPSHOT'
//version = '2.0.0'
//version = '2.0.1-SNAPSHOT'
version = '2.0.1'

sourceCompatibility = '17'
archivesBaseName = "spacelibs-java"
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/siliconmtn/core/HashCodeUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public static int hash(boolean aBoolean) {
* @return hash for a char as an int
*/
public static int hash(char aChar) {
return firstTerm() + (int) aChar;
return firstTerm() + aChar;
}

/**
Expand Down Expand Up @@ -131,7 +131,7 @@ private static int hashString(String val) {
char[] item = val.toCharArray();
int total = 0;
for (char c : item) {
total += (int) c;
total += c;
}

return total;
Expand Down
10 changes: 5 additions & 5 deletions src/main/java/com/siliconmtn/data/bean/GenericVO.java
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,8 @@ public void setValue(Serializable value) {
public int compareTo(GenericVO val) {
if (val == null || val.getKey() == null) return 0;

if (key instanceof java.lang.String) {
return (((String)key).compareToIgnoreCase((String)val.getKey()));
if (key instanceof String s) {
return (s.compareToIgnoreCase((String)val.getKey()));
} else {
return 0;
}
Expand All @@ -106,10 +106,10 @@ public int compareTo(GenericVO val) {
@Override
public boolean equals(Object o) {
GenericVO val = null;
if (o instanceof GenericVO) val = (GenericVO)o;
if (o instanceof GenericVO g) val = g;

if (val != null && key instanceof java.lang.String) {
return (((String)key).equalsIgnoreCase((String)val.getKey()));
if (val != null && key instanceof String s) {
return (s.equalsIgnoreCase((String)val.getKey()));
}

return false;
Expand Down
14 changes: 5 additions & 9 deletions src/main/java/com/siliconmtn/data/format/BooleanUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,16 +73,16 @@ public static boolean toBoolean(Object obj) {
if (obj == null) return false;

// If the object is a boolean, return it
if (obj instanceof Boolean) return ((Boolean) obj);
if (obj instanceof Boolean b) return b;

//check for boxed ints
if (obj instanceof Integer) return toBoolean(((Integer) obj).intValue());
if (obj instanceof Integer i) return toBoolean(i.intValue());

//check for boxed long
if (obj instanceof Long) return toBoolean(((Long) obj).longValue());
if (obj instanceof Long l) return toBoolean(l.longValue());

//check for boxed chars
if (obj instanceof Character) return toBoolean(((Character) obj).charValue());
if (obj instanceof Character c) return toBoolean(c.charValue());

// Make sure the passed data is a String
if (!(obj instanceof String)) return false;
Expand All @@ -98,11 +98,7 @@ public static boolean toBoolean(Object obj) {
*/
private static boolean checkStringVals(String val) {
switch(val.toUpperCase()) {
case "Y" :
case "YES" :
case "TRUE" :
case "ON" :
case "1" :
case "Y", "YES", "TRUE", "ON","1" :
return true;
default:
return false;
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/com/siliconmtn/data/report/ExcelReport.java
Original file line number Diff line number Diff line change
Expand Up @@ -369,11 +369,11 @@ public CellValueType setCellValue(Object value, Cell c) {
} else if (value instanceof Boolean) {
c.setCellValue(BooleanUtil.toBoolean(value));
cvt = CellValueType.BOOLEAN;
} else if (value instanceof Timestamp) {
c.setCellValue((Timestamp)value);
} else if (value instanceof Timestamp t) {
c.setCellValue(t);
cvt = CellValueType.TIMESTAMP;
} else if (value instanceof Date) {
c.setCellValue((Date)value);
} else if (value instanceof Date d) {
c.setCellValue(d);
cvt = CellValueType.DATE;
} else {
if (Pattern.matches("^(\\d{4}-\\d{2}-\\d{2}).*", value + "")) {
Expand Down
3 changes: 1 addition & 2 deletions src/main/java/com/siliconmtn/data/text/StringUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import java.util.Collection;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
Expand Down Expand Up @@ -473,7 +472,7 @@ public static String join(Collection<Object> list, String delimiter, String symb
list
.stream()
.map(item -> (StringUtil.defaultString(symbol,"") + item + StringUtil.defaultString(symbol,"")))
.collect(Collectors.toList()));
.toList());
}

/**
Expand Down
3 changes: 1 addition & 2 deletions src/main/java/com/siliconmtn/data/util/EnumUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/********************************************************************
Expand Down Expand Up @@ -115,7 +114,7 @@ public static <E extends Enum<E>> List<String> toStringList(Enum<E>[] enumArray)
if (enumArray == null) return new ArrayList<>();
return Stream.of(enumArray)
.map(Enum::name)
.collect(Collectors.toList());
.toList();
}

}
24 changes: 3 additions & 21 deletions src/main/java/com/siliconmtn/io/api/RestExceptionHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,6 @@

import java.util.ArrayList;

// Spring JPA
import jakarta.persistence.EntityNotFoundException;
import jakarta.validation.ConstraintViolationException;

// Spring 5.x
import org.springframework.beans.ConversionNotSupportedException;
import org.springframework.beans.TypeMismatchException;
Expand All @@ -21,7 +17,6 @@
import org.springframework.http.ResponseEntity;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.http.converter.HttpMessageNotWritableException;
import org.springframework.validation.BindException;
import org.springframework.web.HttpMediaTypeNotAcceptableException;
import org.springframework.web.HttpMediaTypeNotSupportedException;
import org.springframework.web.HttpRequestMethodNotSupportedException;
Expand All @@ -43,6 +38,9 @@
import com.siliconmtn.io.api.validation.ValidationErrorDTO;
import com.siliconmtn.io.api.validation.ValidationErrorDTO.ValidationError;

// Spring JPA
import jakarta.persistence.EntityNotFoundException;
import jakarta.validation.ConstraintViolationException;
// Log4J 2.x
import lombok.extern.log4j.Log4j2;

Expand Down Expand Up @@ -178,22 +176,6 @@ protected ResponseEntity<Object> handleMissingServletRequestPart(
return buildResponseEntity(new EndpointResponse(status, "multipart/form-data error", ex));
}

/**
* Customize the response for BindException.
* <p>This method delegates to {@link #buildResponseEntity}.
* @param ex the exception
* @param headers the headers to be written to the response
* @param status the selected response status
* @param request the current request
* @return a {@code ResponseEntity} instance
*/
@Override
protected ResponseEntity<Object> handleBindException(
BindException ex, HttpHeaders headers, HttpStatusCode status, WebRequest request) {
log.error(ex);
return buildResponseEntity(new EndpointResponse(status, "Unable to Support a Binding Result", ex));
}

/**
* Customize the response for AsyncRequestTimeoutException.
* <p>This method delegates to {@link #buildResponseEntity}.
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/siliconmtn/io/api/base/BaseService.java
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ public T save(V dto) {
public List<T> saveAll(List<?> entities) {
if (entities == null || entities.isEmpty()) return new ArrayList<>();
if (entities.get(0) instanceof BaseDTO)
return repository.saveAll(toEntityList((List<BaseDTO>)entities));
return repository.saveAll(toEntityList(entities));
return repository.saveAll((List<T>)entities);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import org.junit.jupiter.api.Test;

public class InvalidDataExceptionTest {
class InvalidDataExceptionTest {

@Test
void baseConstructorTest() {
Expand Down
27 changes: 7 additions & 20 deletions src/test/java/com/siliconmtn/io/api/RestExceptionHandlerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,6 @@
import java.util.HashSet;
import java.util.List;

// JEE 7
import jakarta.persistence.EntityNotFoundException;
import jakarta.validation.ConstraintViolation;
import jakarta.validation.ConstraintViolationException;
import jakarta.validation.Path;

import org.junit.jupiter.api.Test;
import org.springframework.beans.ConversionNotSupportedException;
import org.springframework.beans.TypeMismatchException;
Expand All @@ -29,7 +23,6 @@
import org.springframework.http.converter.HttpMessageConversionException;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.http.converter.HttpMessageNotWritableException;
import org.springframework.validation.BindException;
// Spring 5.5.x
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
Expand All @@ -48,6 +41,12 @@

import com.siliconmtn.io.api.security.SecurityAuthorizationException;

// JEE 7
import jakarta.persistence.EntityNotFoundException;
import jakarta.validation.ConstraintViolation;
import jakarta.validation.ConstraintViolationException;
import jakarta.validation.Path;

class RestExceptionHandlerTest {

/**
Expand Down Expand Up @@ -198,7 +197,7 @@ void testHandleMethodArgumentTypeMismatchNoType() throws Exception {
@Test
void testHandleHttpRequestMethodNotSupported() throws Exception {
RestExceptionHandler rest = new RestExceptionHandler();
ResponseEntity<Object> resp = rest.handleHttpRequestMethodNotSupported(new HttpRequestMethodNotSupportedException("emailAddress", "String"), null, HttpStatus.BAD_REQUEST, null);
ResponseEntity<Object> resp = rest.handleHttpRequestMethodNotSupported(new HttpRequestMethodNotSupportedException("emailAddress", List.of("test")), null, HttpStatus.BAD_REQUEST, null);
assertEquals(HttpStatus.BAD_REQUEST, resp.getStatusCode());
assertEquals("Method is Not Supported", ((EndpointResponse)resp.getBody()).getMessage());
}
Expand Down Expand Up @@ -273,18 +272,6 @@ void testHandleMissingServletRequestPart() throws Exception {
assertEquals("multipart/form-data error", ((EndpointResponse)resp.getBody()).getMessage());
}

/**
* Test method for {@link com.siliconmtn.io.api.RestExceptionHandler#handleBindException(org.springframework.validation.BindException, org.springframework.http.HttpHeaders, org.springframework.http.HttpStatus, org.springframework.web.context.request.WebRequest)}.
*/
@Test
void testHandleBindException() throws Exception {
RestExceptionHandler rest = new RestExceptionHandler();
ResponseEntity<Object> resp = rest.handleBindException(new BindException("this", "that"), null, HttpStatus.BAD_REQUEST, null);
assertEquals(HttpStatus.BAD_REQUEST, resp.getStatusCode());
assertEquals("Unable to Support a Binding Result", ((EndpointResponse)resp.getBody()).getMessage());

}

/**
* Test method for {@link com.siliconmtn.io.api.RestExceptionHandler#handleAsyncRequestTimeoutException(org.springframework.web.context.request.async.AsyncRequestTimeoutException, org.springframework.http.HttpHeaders, org.springframework.http.HttpStatus, org.springframework.web.context.request.WebRequest)}.
*/
Expand Down
Loading

0 comments on commit a83ae9f

Please sign in to comment.