Implements Tests for StorageService
This commit is contained in:
@@ -2,12 +2,10 @@ package com.hideyoshi.backendportfolio.microservice.storageService.model;
|
||||
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.Getter;
|
||||
import lombok.NonNull;
|
||||
import lombok.*;
|
||||
|
||||
@Getter
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public class StorageServiceUploadResponse {
|
||||
|
||||
|
||||
@@ -121,7 +121,7 @@ public class StorageService {
|
||||
}
|
||||
}
|
||||
|
||||
private String getRequest(URI requestURI) throws IOException {
|
||||
protected String getRequest(URI requestURI) throws IOException {
|
||||
HttpGet request = new HttpGet(requestURI);
|
||||
request.setHeader("Content-Type", "application/json");
|
||||
|
||||
@@ -134,7 +134,7 @@ public class StorageService {
|
||||
);
|
||||
}
|
||||
|
||||
private String postRequest(URI requestURI, String requestBody) throws IOException {
|
||||
protected String postRequest(URI requestURI, String requestBody) throws IOException {
|
||||
HttpPost request = new HttpPost(requestURI);
|
||||
request.setHeader("Content-Type", "application/json");
|
||||
|
||||
@@ -153,7 +153,7 @@ public class StorageService {
|
||||
);
|
||||
}
|
||||
|
||||
private void deleteRequest(URI requestURI) throws IOException {
|
||||
protected void deleteRequest(URI requestURI) throws IOException {
|
||||
HttpDelete request = new HttpDelete(requestURI);
|
||||
request.setHeader("Content-Type", "application/json");
|
||||
|
||||
|
||||
@@ -32,7 +32,6 @@ import static org.mockito.Mockito.verify;
|
||||
@DirtiesContext(classMode= DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD)
|
||||
class UserServiceImplTest {
|
||||
|
||||
@InjectMocks
|
||||
private UserServiceImpl underTest;
|
||||
|
||||
@Mock
|
||||
|
||||
@@ -0,0 +1,198 @@
|
||||
package com.hideyoshi.backendportfolio.microservice.storageService.service;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.hideyoshi.backendportfolio.microservice.storageService.config.StorageServiceConfig;
|
||||
import com.hideyoshi.backendportfolio.microservice.storageService.enums.FileTypeEnum;
|
||||
import com.hideyoshi.backendportfolio.microservice.storageService.model.StorageServiceDownloadResponse;
|
||||
import com.hideyoshi.backendportfolio.microservice.storageService.model.StorageServiceUploadResponse;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.mockito.Mockito;
|
||||
import org.mockito.junit.jupiter.MockitoExtension;
|
||||
import org.springframework.test.annotation.DirtiesContext;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
@DirtiesContext(classMode= DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD)
|
||||
public class StorageServiceTest {
|
||||
|
||||
private StorageService storageService;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
StorageServiceConfig config = new StorageServiceConfig();
|
||||
|
||||
this.storageService = new StorageService(new ObjectMapper(), config);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testGetNewFileUrlIfFileExists() {
|
||||
StorageService storageService = Mockito.spy(this.storageService);
|
||||
|
||||
// Given
|
||||
String username = "test";
|
||||
String filePostfix = "test";
|
||||
FileTypeEnum fileTypeEnum = FileTypeEnum.JPEG;
|
||||
|
||||
// When
|
||||
try {
|
||||
String responseString = "{\"presigned_url\":\"https://test.com\", \"file_key\":\"test\"}";
|
||||
Mockito.doReturn(responseString).when(storageService).postRequest(Mockito.any(), Mockito.any());
|
||||
} catch (IOException e) {
|
||||
assert false;
|
||||
}
|
||||
|
||||
// Then
|
||||
var response = storageService.getNewFileUrl(username, filePostfix, fileTypeEnum);
|
||||
|
||||
assertThat(response).isPresent();
|
||||
assertThat(response.get()).isInstanceOf(StorageServiceUploadResponse.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testGetNewFileUrlIfFileDoesNotExist() {
|
||||
StorageService storageService = Mockito.spy(this.storageService);
|
||||
|
||||
// Given
|
||||
String username = "test";
|
||||
String filePostfix = "test";
|
||||
FileTypeEnum fileTypeEnum = FileTypeEnum.JPEG;
|
||||
|
||||
// When
|
||||
try {
|
||||
Mockito.doThrow(new IOException()).when(storageService).postRequest(Mockito.any(), Mockito.any());
|
||||
} catch (IOException e) {
|
||||
assert false;
|
||||
}
|
||||
|
||||
// Then
|
||||
var response = storageService.getNewFileUrl(username, filePostfix, fileTypeEnum);
|
||||
|
||||
assertThat(response).isNotPresent();
|
||||
}
|
||||
|
||||
@Test
|
||||
void getFileUrlIfExists() {
|
||||
StorageService storageService = Mockito.spy(this.storageService);
|
||||
|
||||
// Given
|
||||
String username = "test";
|
||||
String filePostfix = "test";
|
||||
|
||||
// When
|
||||
try {
|
||||
String responseString = "{\"presigned_url\":\"http://test.com\"}";
|
||||
Mockito.doReturn(responseString).when(storageService).getRequest(Mockito.any());
|
||||
} catch (IOException e) {
|
||||
assert false;
|
||||
}
|
||||
|
||||
// Then
|
||||
var response = storageService.getFileUrl(username, filePostfix);
|
||||
|
||||
assertThat(response).isPresent();
|
||||
assertThat(response.get()).isInstanceOf(StorageServiceDownloadResponse.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
void getFileUrlIfNotExists() {
|
||||
StorageService storageService = Mockito.spy(this.storageService);
|
||||
|
||||
// Given
|
||||
String username = "test";
|
||||
String filePostfix = "test";
|
||||
|
||||
// When
|
||||
try {
|
||||
Mockito.doThrow(new IOException()).when(storageService).getRequest(Mockito.any());
|
||||
} catch (IOException e) {
|
||||
assert false;
|
||||
}
|
||||
|
||||
// Then
|
||||
var response = storageService.getFileUrl(username, filePostfix);
|
||||
|
||||
assertThat(response).isNotPresent();
|
||||
}
|
||||
|
||||
@Test
|
||||
void deleteFileIfExists() {
|
||||
StorageService storageService = Mockito.spy(this.storageService);
|
||||
|
||||
// Given
|
||||
String username = "test";
|
||||
String filePostfix = "test";
|
||||
|
||||
// When
|
||||
try {
|
||||
Mockito.doNothing().when(storageService).deleteRequest(Mockito.any());
|
||||
} catch (IOException e) {
|
||||
assert false;
|
||||
}
|
||||
|
||||
// Then
|
||||
storageService.deleteFile(username, filePostfix);
|
||||
}
|
||||
|
||||
@Test
|
||||
void deleteFileIfNotExists() {
|
||||
StorageService storageService = Mockito.spy(this.storageService);
|
||||
|
||||
// Given
|
||||
String username = "test";
|
||||
String filePostfix = "test";
|
||||
|
||||
// When
|
||||
try {
|
||||
Mockito.doThrow(new IOException()).when(storageService).deleteRequest(Mockito.any());
|
||||
} catch (IOException e) {
|
||||
assert false;
|
||||
}
|
||||
|
||||
// Then
|
||||
storageService.deleteFile(username, filePostfix);
|
||||
}
|
||||
|
||||
@Test
|
||||
void processFileIfExists() {
|
||||
StorageService storageService = Mockito.spy(this.storageService);
|
||||
|
||||
// Given
|
||||
String username = "test";
|
||||
String filePostfix = "test";
|
||||
|
||||
// When
|
||||
try {
|
||||
Mockito.doNothing().when(storageService).deleteRequest(Mockito.any());
|
||||
} catch (IOException e) {
|
||||
assert false;
|
||||
}
|
||||
|
||||
// Then
|
||||
storageService.deleteFile(username, filePostfix);
|
||||
}
|
||||
|
||||
@Test
|
||||
void processFileIfNotExists() {
|
||||
StorageService storageService = Mockito.spy(this.storageService);
|
||||
|
||||
// Given
|
||||
String username = "test";
|
||||
String filePostfix = "test";
|
||||
|
||||
// When
|
||||
try {
|
||||
Mockito.doThrow(new IOException()).when(storageService).deleteRequest(Mockito.any());
|
||||
} catch (IOException e) {
|
||||
assert false;
|
||||
}
|
||||
|
||||
// Then
|
||||
storageService.deleteFile(username, filePostfix);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -13,6 +13,9 @@ com:
|
||||
username: "YoshiUnfriendly"
|
||||
password: "passwd"
|
||||
|
||||
microservice:
|
||||
storageServicePath: undertest
|
||||
|
||||
spring:
|
||||
|
||||
liquibase:
|
||||
|
||||
Reference in New Issue
Block a user