Skip to content

Commit

Permalink
🚨 fix pmd warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
simonhir committed Nov 12, 2024
1 parent 8226efe commit a76140b
Show file tree
Hide file tree
Showing 14 changed files with 33 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public class CosysAdapter implements GenerateDocumentOutPort {

public static final String DATA_FILE_NAME = "data";
public static final String MERGE_FILE_NAME = "merge";
public static final String DOC_GEN_EXCEPTION_MESSAGE = "Document could not be created.";

private final CosysConfiguration configuration;
private final GenerationApi generationApi;
Expand Down Expand Up @@ -47,15 +48,15 @@ public Mono<byte[]> generateCosysDocument(final GenerateDocument generateDocumen
null)
.onStatus(HttpStatusCode::is5xxServerError,
response -> response.bodyToMono(byte[].class)
.flatMap(body -> Mono.error(new CosysException("Document could not be created."))))
.flatMap(body -> Mono.error(new CosysException(DOC_GEN_EXCEPTION_MESSAGE))))
.onStatus(HttpStatusCode::is4xxClientError,
response -> response.bodyToMono(byte[].class)
.flatMap(body -> Mono.error(new CosysException("Document could not be created."))))
.flatMap(body -> Mono.error(new CosysException(DOC_GEN_EXCEPTION_MESSAGE))))
.bodyToMono(byte[].class);

} catch (final IOException ex) {
log.error("Document could not be created.", ex);
throw new CosysException("Document could not be created.");
log.error(DOC_GEN_EXCEPTION_MESSAGE, ex);
throw new CosysException(DOC_GEN_EXCEPTION_MESSAGE, ex);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
import java.nio.file.Files;
import java.nio.file.Path;

public class FileUtils {
public final class FileUtils {
private FileUtils() {
}

public static File createFile(final String name, final byte[] content) throws IOException {
final Path tempFile = Files.createTempFile(name, ".json");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,10 @@ public void saveDocumentInStorage(
}

private void validateFileSize(final byte[] data) {
if (!fileValidationService.isValidFileSize(data))
if (!fileValidationService.isValidFileSize(data)) {
throw new FileSizeValidationException(
String.format("Invalid file size %d MB. Allowed are %d MB.", DataSize.ofBytes(data.length).toMegabytes(),
fileValidationService.getMaxFileSize().toMegabytes()));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@
public interface CreateDocumentInPort {

void createDocument(
@Valid final GenerateDocument generateDocument,
@NotBlank final String filePath) throws CosysException, DocumentStorageException;
@Valid GenerateDocument generateDocument,
@NotBlank String filePath) throws CosysException, DocumentStorageException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@

public interface GenerateDocumentOutPort {

Mono<byte[]> generateCosysDocument(final GenerateDocument generateDocument) throws CosysException;
Mono<byte[]> generateCosysDocument(GenerateDocument generateDocument) throws CosysException;

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@

public interface SaveFileToStorageOutPort {
void saveDocumentInStorage(
final String filePath,
final byte[] data) throws DocumentStorageException;
String filePath,
byte[] data) throws DocumentStorageException;
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
package de.muenchen.refarch.integration.cosys.domain.exception;

@SuppressWarnings("PMD.MissingSerialVersionUID")
public class CosysException extends Exception {
public CosysException(final String message) {
super(message);
}

public CosysException(final String message, final Throwable cause) {
super(message, cause);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import de.muenchen.refarch.integration.cosys.domain.model.GenerateDocument;
import java.io.File;
import java.io.IOException;
import lombok.val;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.ArgumentCaptor;
Expand Down Expand Up @@ -40,11 +39,11 @@ void setUp() {
}

@Test
void testGenerateCosysDocument_Success() throws IOException, CosysException {
void testGenerateCosysDocumentSuccess() throws IOException, CosysException {
// given
val generateDocument = generateDocument();
val response = "Response".getBytes();
val responseSpecMock = Mockito.mock(WebClient.ResponseSpec.class);
final GenerateDocument generateDocument = generateDocument();
final byte[] response = "Response".getBytes();
final WebClient.ResponseSpec responseSpecMock = Mockito.mock(WebClient.ResponseSpec.class);
when(responseSpecMock.onStatus(any(), any())).thenReturn(responseSpecMock);
when(responseSpecMock.bodyToMono(byte[].class)).thenReturn(Mono.just(response));
final ArgumentCaptor<File> dataFileCaptor = ArgumentCaptor.forClass(File.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,18 +52,18 @@ void saveDocumentInStorageWithThrowsDocumentStorageException()
throws DocumentStorageException, DocumentStorageClientErrorException, DocumentStorageServerErrorException {
doThrow(new DocumentStorageException("DocumentStorageClientErrorException", new Exception())).when(documentStorageFileRepository)
.saveFile(anyString(), any(), eq(1));
DocumentStorageException documentStorageException = assertThrows(DocumentStorageException.class,
final DocumentStorageException documentStorageException = assertThrows(DocumentStorageException.class,
() -> s3Adapter.saveDocumentInStorage("filePath.txt", DATA_AS_BYTE_ARRAY));

String expectedMessage = "Document could not be saved.";
String actualMessage = documentStorageException.getMessage();
final String expectedMessage = "Document could not be saved.";
final String actualMessage = documentStorageException.getMessage();

assertEquals(expectedMessage, actualMessage);
}

@Test
void testSaveDocumentInStorageThrowsBpmnErrorForInvalidFileSize() {
String expectedMessage = String.format("Invalid file size %d MB. Allowed are %d MB.", DataSize.ofBytes(TOO_LARGE_FILE.length).toMegabytes(),
final String expectedMessage = String.format("Invalid file size %d MB. Allowed are %d MB.", DataSize.ofBytes(TOO_LARGE_FILE.length).toMegabytes(),
ALLOWED_FILE_SIZE.toMegabytes());

assertThatThrownBy(() -> s3Adapter.saveDocumentInStorage("filePath.txt", TOO_LARGE_FILE))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class CreateDocumentUseCaseTest {

private final GenerateDocument generateDocument = new GenerateDocument("Client", "Role", "guid", new ObjectMapper().readTree("{\"key1\":\"value\"}"));

CreateDocumentUseCaseTest() throws JsonProcessingException {
protected CreateDocumentUseCaseTest() throws JsonProcessingException {
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication()
@SuppressWarnings("PMD.UseUtilityClass")
public class CosysExampleApplication {

public static void main(final String[] args) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
public class NoSecurityConfiguration {

@Bean
public SecurityFilterChain mainSecurityFilterChain(HttpSecurity http) throws Exception {
public SecurityFilterChain mainSecurityFilterChain(final HttpSecurity http) throws Exception {
// @formatter:off
http
.headers(httpSecurityHeadersConfigurer -> httpSecurityHeadersConfigurer.frameOptions(HeadersConfigurer.FrameOptionsConfig::disable))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public class ExampleController {

private final GenerateDocumentOutPort generateDocumentOutPort;

@PostMapping(value = "/test/document")
@PostMapping("/test/document")
public ResponseEntity<byte[]> testCreateCosysDocument() throws CosysException {
final byte[] file = this.generateDocumentOutPort.generateCosysDocument(this.generateDocument()).block();
return ResponseEntity.ok(file);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public ApiClient cosysApiClient(final ClientRegistrationRepository clientRegistr
private WebClient webClient(
final ClientRegistrationRepository clientRegistrationRepository,
final OAuth2AuthorizedClientService authorizedClientService) {
final var oauth = new ServletOAuth2AuthorizedClientExchangeFilterFunction(
final ServletOAuth2AuthorizedClientExchangeFilterFunction oauth = new ServletOAuth2AuthorizedClientExchangeFilterFunction(
new AuthorizedClientServiceOAuth2AuthorizedClientManager(
clientRegistrationRepository, authorizedClientService));
oauth.setDefaultClientRegistrationId("cosys");
Expand Down

0 comments on commit a76140b

Please sign in to comment.