diff --git a/source/file-server-client/pom.xml b/source/file-server-client/pom.xml index f79d402..6d0d358 100644 --- a/source/file-server-client/pom.xml +++ b/source/file-server-client/pom.xml @@ -1,4 +1,3 @@ - - + 4.0.0 nl.aerius diff --git a/source/file-server-client/src/test/java/nl/aerius/fileserver/client/FileServerClientTest.java b/source/file-server-client/src/test/java/nl/aerius/fileserver/client/FileServerClientTest.java index d432ffc..d38e42b 100644 --- a/source/file-server-client/src/test/java/nl/aerius/fileserver/client/FileServerClientTest.java +++ b/source/file-server-client/src/test/java/nl/aerius/fileserver/client/FileServerClientTest.java @@ -55,8 +55,7 @@ class FileServerClientTest { private static final String FILE_CONTENTS = "test"; private MockWebServer mockWebServer; - @Mock - private FileServerProperties properties; + @Mock private FileServerProperties properties; private FileServerClient fileServerClient; @@ -74,7 +73,7 @@ void destroy() throws IOException { } @Test - void testRetrieveFile() throws IOException, InterruptedException { + void testRetrieveFile() throws InterruptedException { final String expectedFileName = "fileServiceResponse.json"; mockFileServiceResponse(expectedFileName, FILE_CONTENTS.getBytes(StandardCharsets.UTF_8), HttpStatus.OK.value()); @@ -90,7 +89,7 @@ void testRetrieveFile() throws IOException, InterruptedException { } @Test - void testRetrieveFileServiceNotFoundError() throws IOException, InterruptedException { + void testRetrieveFileServiceNotFoundError() throws InterruptedException { mockFileServiceResponse("", new byte[0], HttpStatus.NOT_FOUND.value()); final ResponseStatusException exception = assertThrows(ResponseStatusException.class, @@ -103,7 +102,7 @@ void testRetrieveFileServiceNotFoundError() throws IOException, InterruptedExcep } @Test - void testRetrieveFileServiceClientError() throws IOException, InterruptedException { + void testRetrieveFileServiceClientError() throws InterruptedException { mockFileServiceResponse("", new byte[0], HttpStatus.FORBIDDEN.value()); final ResponseStatusException exception = assertThrows(ResponseStatusException.class, @@ -118,7 +117,7 @@ void testRetrieveFileServiceClientError() throws IOException, InterruptedExcepti } @Test - void testRetrieveFileServiceServerError() throws IOException, InterruptedException { + void testRetrieveFileServiceServerError() throws InterruptedException { mockFileServiceResponse("", new byte[0], HttpStatus.BAD_GATEWAY.value()); final ResponseStatusException exception = assertThrows(ResponseStatusException.class, @@ -131,7 +130,7 @@ void testRetrieveFileServiceServerError() throws IOException, InterruptedExcepti } @Test - void testWrite() throws IOException, InterruptedException { + void testWrite() throws InterruptedException { mockFileServiceResponse(HttpStatus.OK.value()); fileServerClient.writeJson(UUID_CODE, ExampleFileServerFile.VALIDATION, FileServerExpireTag.NEVER, "test"); @@ -139,7 +138,7 @@ void testWrite() throws IOException, InterruptedException { } @Test - void testCopy() throws IOException, InterruptedException { + void testCopy() throws InterruptedException { mockFileServiceResponse(HttpStatus.OK.value()); final String destinationCode = "456"; final String filename = "SomeFile"; @@ -149,7 +148,7 @@ void testCopy() throws IOException, InterruptedException { } @Test - void testDelete() throws IOException, InterruptedException { + void testDelete() throws InterruptedException { mockFileServiceResponse("", new byte[0], HttpStatus.OK.value()); fileServerClient.deleteFilesForId(UUID_CODE); @@ -157,19 +156,19 @@ void testDelete() throws IOException, InterruptedException { } @Test - void testDeleteFileServerError() throws IOException, InterruptedException { + void testDeleteFileServerError() throws InterruptedException { mockFileServiceResponse("", new byte[0], HttpStatus.INTERNAL_SERVER_ERROR.value()); assertDoesNotThrow(() -> fileServerClient.deleteFilesForId(UUID_CODE), "A file service error should not cause an exception."); assertRecordedRequest(HttpMethod.DELETE, UUID_CODE); } - private void mockFileServiceResponse(final int status) throws IOException { + private void mockFileServiceResponse(final int status) { mockWebServer.enqueue(new MockResponse() .setResponseCode(status)); } - private void mockFileServiceResponse(final String fileName, final byte[] fileContents, final int status) throws IOException { + private void mockFileServiceResponse(final String fileName, final byte[] fileContents, final int status) { try (final Buffer buffer = new Buffer()) { mockWebServer.enqueue(new MockResponse() .setResponseCode(status) diff --git a/source/file-server/pom.xml b/source/file-server/pom.xml index 7b79437..cbcbfe6 100644 --- a/source/file-server/pom.xml +++ b/source/file-server/pom.xml @@ -1,4 +1,3 @@ - - + 4.0.0 nl.aerius diff --git a/source/file-server/src/main/java/nl/aerius/fileserver/Application.java b/source/file-server/src/main/java/nl/aerius/fileserver/Application.java index 062bf98..3983568 100644 --- a/source/file-server/src/main/java/nl/aerius/fileserver/Application.java +++ b/source/file-server/src/main/java/nl/aerius/fileserver/Application.java @@ -16,6 +16,8 @@ */ package nl.aerius.fileserver; +import jakarta.servlet.Filter; + import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.web.servlet.FilterRegistrationBean; @@ -43,7 +45,7 @@ public static void main(final String[] args) { * CORS filter to allow client calls to be made on a different port. */ @Bean - public FilterRegistrationBean corsFilter() { + public FilterRegistrationBean corsFilter() { final CorsConfiguration config = new CorsConfiguration(); config.addAllowedOriginPattern("*"); @@ -52,7 +54,7 @@ public FilterRegistrationBean corsFilter() { final UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource(); source.registerCorsConfiguration("/**", config); - final FilterRegistrationBean bean = new FilterRegistrationBean<>(new CorsFilter(source)); + final FilterRegistrationBean bean = new FilterRegistrationBean<>(new CorsFilter(source)); bean.setOrder(0); return bean; diff --git a/source/file-server/src/main/java/nl/aerius/fileserver/local/LocalFileController.java b/source/file-server/src/main/java/nl/aerius/fileserver/local/LocalFileController.java index 77014c4..85a8bdd 100644 --- a/source/file-server/src/main/java/nl/aerius/fileserver/local/LocalFileController.java +++ b/source/file-server/src/main/java/nl/aerius/fileserver/local/LocalFileController.java @@ -27,10 +27,9 @@ import org.springframework.http.ContentDisposition; import org.springframework.http.HttpHeaders; import org.springframework.http.ResponseEntity; -import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; -import org.springframework.web.bind.annotation.ResponseBody; +import org.springframework.web.bind.annotation.RestController; import nl.aerius.fileserver.storage.FileController; import nl.aerius.fileserver.storage.StorageService; @@ -39,7 +38,7 @@ /** * Controller to handle the HTTP requests to the file server. */ -@Controller +@RestController @Profile("local") class LocalFileController extends FileController { @@ -59,7 +58,6 @@ public LocalFileController(final StorageService storageService) { * @return returns the file or not found status if not present */ @GetMapping(FILE_PATH) - @ResponseBody public ResponseEntity getFile(final @PathVariable String uuid, final @PathVariable String filename) { try { FilenameUtil.validateParameters(uuid, filename); diff --git a/source/file-server/src/main/java/nl/aerius/fileserver/s3/AmazonS3StorageService.java b/source/file-server/src/main/java/nl/aerius/fileserver/s3/AmazonS3StorageService.java index 902871a..c73dfd6 100644 --- a/source/file-server/src/main/java/nl/aerius/fileserver/s3/AmazonS3StorageService.java +++ b/source/file-server/src/main/java/nl/aerius/fileserver/s3/AmazonS3StorageService.java @@ -32,10 +32,8 @@ import software.amazon.awssdk.core.sync.RequestBody; import software.amazon.awssdk.services.s3.S3Client; import software.amazon.awssdk.services.s3.model.CopyObjectRequest; -import software.amazon.awssdk.services.s3.model.Delete; import software.amazon.awssdk.services.s3.model.DeleteObjectsRequest; import software.amazon.awssdk.services.s3.model.GetObjectAttributesRequest; -import software.amazon.awssdk.services.s3.model.GetObjectRequest; import software.amazon.awssdk.services.s3.model.ListObjectsRequest; import software.amazon.awssdk.services.s3.model.ObjectAttributes; import software.amazon.awssdk.services.s3.model.ObjectIdentifier; @@ -88,14 +86,12 @@ public String getFile(final String uuid, final String filename) throws FileNotFo final String key = key(uuid, filename); checkFileExists(key); - final GetObjectRequest getObjectRequest = GetObjectRequest.builder() - .bucket(bucketName) - .responseContentDisposition("attachment; filename=\"" + filename + "\"") - .key(key) - .build(); final GetObjectPresignRequest objectPresignRequest = GetObjectPresignRequest.builder() .signatureDuration(SIGNATURE_DURATION) - .getObjectRequest(getObjectRequest) + .getObjectRequest(d -> d + .bucket(bucketName) + .responseContentDisposition("attachment; filename=\"" + filename + "\"") + .key(key)) .build(); // Generate the presigned request return presigner.presignGetObject(objectPresignRequest).url().toExternalForm(); @@ -151,8 +147,7 @@ private void deleteObjects(final List toDelete) throws IOExcep try { final DeleteObjectsRequest dor = DeleteObjectsRequest.builder() .bucket(bucketName) - .delete(Delete.builder() - .objects(toDelete).build()) + .delete(d -> d.objects(toDelete)) .build(); s3Client.deleteObjects(dor); diff --git a/source/file-server/src/main/java/nl/aerius/fileserver/s3/S3ClientFactory.java b/source/file-server/src/main/java/nl/aerius/fileserver/s3/S3ClientFactory.java index a179371..ad7ce67 100644 --- a/source/file-server/src/main/java/nl/aerius/fileserver/s3/S3ClientFactory.java +++ b/source/file-server/src/main/java/nl/aerius/fileserver/s3/S3ClientFactory.java @@ -21,6 +21,8 @@ import org.springframework.context.annotation.Lazy; import software.amazon.awssdk.auth.credentials.EnvironmentVariableCredentialsProvider; +import software.amazon.awssdk.core.SdkSystemSetting; +import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.s3.S3Client; import software.amazon.awssdk.services.s3.presigner.S3Presigner; @@ -34,6 +36,7 @@ class S3ClientFactory { @Lazy public S3Client s3Client() { return S3Client.builder() + .region(Region.of(System.getenv(SdkSystemSetting.AWS_REGION.environmentVariable()))) .credentialsProvider(EnvironmentVariableCredentialsProvider.create()) .build(); } diff --git a/source/file-server/src/test/java/nl/aerius/fileserver/local/LocalFileControllerTest.java b/source/file-server/src/test/java/nl/aerius/fileserver/local/LocalFileControllerTest.java index 21ba777..c63d9d3 100644 --- a/source/file-server/src/test/java/nl/aerius/fileserver/local/LocalFileControllerTest.java +++ b/source/file-server/src/test/java/nl/aerius/fileserver/local/LocalFileControllerTest.java @@ -39,11 +39,11 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.boot.test.mock.mockito.MockBean; import org.springframework.http.HttpHeaders; import org.springframework.mock.web.MockHttpServletResponse; import org.springframework.mock.web.MockMultipartFile; import org.springframework.test.context.ActiveProfiles; +import org.springframework.test.context.bean.override.mockito.MockitoBean; import org.springframework.test.web.servlet.MockMvc; import nl.aerius.fileserver.storage.StorageService; @@ -69,14 +69,11 @@ class LocalFileControllerTest { private static final String URL_BAD_FILENAME = HTTP_LOCALHOST + UUID_CODE + "/" + "1".repeat(1000); private static final String EXPIRE_TAG_VALUE = "never"; - @Autowired - private MockMvc mvc; + @Autowired private MockMvc mvc; - @MockBean - private StorageService storageService; + @MockitoBean private StorageService storageService; - @TempDir - File tempDir; + @TempDir File tempDir; @Test void testPutFile() throws Exception { diff --git a/source/file-server/src/test/java/nl/aerius/fileserver/local/LocalFileStorageSeviceTest.java b/source/file-server/src/test/java/nl/aerius/fileserver/local/LocalFileStorageSeviceTest.java index 295d6c6..8185e21 100644 --- a/source/file-server/src/test/java/nl/aerius/fileserver/local/LocalFileStorageSeviceTest.java +++ b/source/file-server/src/test/java/nl/aerius/fileserver/local/LocalFileStorageSeviceTest.java @@ -41,8 +41,7 @@ class LocalFileStorageSeviceTest { private static final String CONTENT = "AERIUS"; private static final String UUID_CODE = "123"; private static final String FILENAME = "test.gml"; - @TempDir - File tempDir; + @TempDir File tempDir; private LocalFileStorageSevice service; private File expectedFile; @@ -90,7 +89,7 @@ void testGetFile() throws IOException { } @Test - void testGetFileNotFound() throws FileNotFoundException { + void testGetFileNotFound() { assertThrows(FileNotFoundException.class, () -> service.getFile(UUID_CODE, FILENAME), "Expects the file to not be found."); } @@ -105,7 +104,7 @@ void testCopyFile() throws IOException { } @Test - void testCopyFileNotFound() throws IOException { + void testCopyFileNotFound() { final String destinationUuid = UUID.randomUUID().toString(); assertFalse(expectedFile.exists(), "Check if file does not exist before trying to copy non existing file."); assertThrows(FileNotFoundException.class, () -> service.copyFile(UUID_CODE, destinationUuid, FILENAME, null), @@ -120,7 +119,7 @@ void testDeleteFile() throws IOException { } @Test - void testDeleteNotFound() throws IOException { + void testDeleteNotFound() { assertFalse(expectedFile.exists(), "Check if file does not exist before trying to delete non existing file."); assertThrows(NoSuchFileException.class, () -> service.deleteFile(UUID_CODE, FILENAME), "Should throw exception when file not found."); } diff --git a/source/file-server/src/test/java/nl/aerius/fileserver/s3/AmazonS3ControllerTest.java b/source/file-server/src/test/java/nl/aerius/fileserver/s3/AmazonS3ControllerTest.java index b1ce44c..60065a5 100644 --- a/source/file-server/src/test/java/nl/aerius/fileserver/s3/AmazonS3ControllerTest.java +++ b/source/file-server/src/test/java/nl/aerius/fileserver/s3/AmazonS3ControllerTest.java @@ -25,9 +25,9 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.boot.test.mock.mockito.MockBean; import org.springframework.mock.web.MockHttpServletResponse; import org.springframework.test.context.ActiveProfiles; +import org.springframework.test.context.bean.override.mockito.MockitoBean; import org.springframework.test.web.servlet.MockMvc; import nl.aerius.fileserver.storage.StorageService; @@ -44,11 +44,9 @@ class AmazonS3ControllerTest { private static final String UUID_CODE = "00000000-0000-0000-0000-000000000001"; private static final Object AMAZON_URL = "https://s3/uuid/filename"; - @Autowired - private MockMvc mvc; + @Autowired private MockMvc mvc; - @MockBean - private StorageService storageService; + @MockitoBean private StorageService storageService; @Test void testGetFile() throws Exception { diff --git a/source/file-server/src/test/java/nl/aerius/fileserver/s3/AmazonS3StorageServiceTest.java b/source/file-server/src/test/java/nl/aerius/fileserver/s3/AmazonS3StorageServiceTest.java index eff85ea..f82b443 100644 --- a/source/file-server/src/test/java/nl/aerius/fileserver/s3/AmazonS3StorageServiceTest.java +++ b/source/file-server/src/test/java/nl/aerius/fileserver/s3/AmazonS3StorageServiceTest.java @@ -28,7 +28,7 @@ import java.io.ByteArrayInputStream; import java.io.FileNotFoundException; import java.io.IOException; -import java.net.URL; +import java.net.URI; import java.util.Collection; import java.util.List; import java.util.UUID; @@ -94,7 +94,7 @@ class AmazonS3StorageServiceTest { private AmazonS3StorageService service; @BeforeEach - void beforeEach() throws IOException { + void beforeEach() { final AmazonS3StorageProperties properties = new AmazonS3StorageProperties(); properties.setBucketName(BUCKET_NAME); @@ -134,12 +134,12 @@ void testGetFile(final String uuid, final String expectedFullPath) throws IOExce final Builder builder = S3Utilities.builder(); builder.region(REGION); doReturn(presignedGetObjectRequest).when(presigner).presignGetObject(any(GetObjectPresignRequest.class)); - doAnswer(a -> new URL(expectedFullPath)).when(presignedGetObjectRequest).url(); + doAnswer(a -> new URI(expectedFullPath).toURL()).when(presignedGetObjectRequest).url(); assertEquals(expectedFullPath, service.getFile(uuid, FILENAME), "Expects the complete path to file."); } @Test - void testGetFileNotFound() throws FileNotFoundException { + void testGetFileNotFound() { doThrow(S3Exception.builder().build()).when(s3Client).getObjectAttributes(any(GetObjectAttributesRequest.class)); assertThrows(FileNotFoundException.class, () -> service.getFile(UUID_CODE, FILENAME), "Expects the file to not be found."); } @@ -159,7 +159,7 @@ void testCopyFile() throws IOException { } @Test - void testCopyFileNotFound() throws IOException { + void testCopyFileNotFound() { final String destinationUuid = UUID.randomUUID().toString(); doThrow(S3Exception.builder().build()).when(s3Client).copyObject(any(CopyObjectRequest.class)); assertThrows(IOException.class, () -> service.copyFile(UUID_CODE, destinationUuid, FILENAME, null), @@ -176,7 +176,7 @@ void testDeleteFile() throws IOException { } @Test - void testDeleteNotFound() throws IOException { + void testDeleteNotFound() { doThrow(S3Exception.builder().build()).when(s3Client).deleteObjects(any(DeleteObjectsRequest.class)); assertThrows(IOException.class, () -> service.deleteFile(UUID_CODE, FILENAME), "Should throw exception when file not found."); } diff --git a/source/pom.xml b/source/pom.xml index 4fa839c..d15db80 100644 --- a/source/pom.xml +++ b/source/pom.xml @@ -1,4 +1,3 @@ - - + 4.0.0 nl.aerius @@ -75,7 +73,7 @@ file-server file-server-client - + dependency-check