Implements Tests for StorageService

This commit is contained in:
2023-10-24 02:04:27 -03:00
parent 686c800a95
commit 2ad112e8c3
5 changed files with 206 additions and 8 deletions

View File

@@ -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

View File

@@ -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);
}
}

View File

@@ -13,6 +13,9 @@ com:
username: "YoshiUnfriendly"
password: "passwd"
microservice:
storageServicePath: undertest
spring:
liquibase: