Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Applying spotless and fixing some warnings #19

Merged
merged 2 commits into from
Jan 31, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions source/file-server-client/pom.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright the State of the Netherlands
Expand All @@ -17,8 +16,7 @@
along with this program. If not, see http://www.gnu.org/licenses/.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>nl.aerius</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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());
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -131,15 +130,15 @@ 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");
assertRecordedRequest(HttpMethod.PUT, UUID_CODE);
}

@Test
void testCopy() throws IOException, InterruptedException {
void testCopy() throws InterruptedException {
mockFileServiceResponse(HttpStatus.OK.value());
final String destinationCode = "456";
final String filename = "SomeFile";
Expand All @@ -149,27 +148,27 @@ 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);
assertRecordedRequest(HttpMethod.DELETE, UUID_CODE);
}

@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)
Expand Down
4 changes: 1 addition & 3 deletions source/file-server/pom.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright the State of the Netherlands
Expand All @@ -17,8 +16,7 @@
along with this program. If not, see http://www.gnu.org/licenses/.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>nl.aerius</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<Filter> corsFilter() {
final CorsConfiguration config = new CorsConfiguration();

config.addAllowedOriginPattern("*");
Expand All @@ -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<Filter> bean = new FilterRegistrationBean<>(new CorsFilter(source));

bean.setOrder(0);
return bean;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -39,7 +38,7 @@
/**
* Controller to handle the HTTP requests to the file server.
*/
@Controller
@RestController
@Profile("local")
class LocalFileController extends FileController {

Expand All @@ -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<Resource> getFile(final @PathVariable String uuid, final @PathVariable String filename) {
try {
FilenameUtil.validateParameters(uuid, filename);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -151,8 +147,7 @@ private void deleteObjects(final List<ObjectIdentifier> 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.");
}

Expand All @@ -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),
Expand All @@ -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.");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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.");
}
Expand All @@ -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),
Expand All @@ -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.");
}
Expand Down
Loading