Untitled
package com.convatec.filemgmt.service; import com.amazonaws.regions.Regions; import com.amazonaws.services.neptunedata.model.S3Exception; import com.amazonaws.services.s3.AmazonS3; import com.amazonaws.services.s3.model.ListObjectsRequest; import com.amazonaws.services.s3.model.ListObjectsV2Result; import com.amazonaws.services.s3.model.ObjectListing; import com.amazonaws.services.s3.model.PutObjectRequest; import com.amazonaws.services.s3.model.S3ObjectSummary; import com.convatec.filemgmt.FileMgmtApplication; import com.convatec.filemgmt.exceptions.FileDownloadException; import com.convatec.filemgmt.util.AppTestConstants; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.*; import org.mockito.junit.jupiter.MockitoExtension; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.ActiveProfiles; import org.springframework.test.util.ReflectionTestUtils; import org.springframework.web.multipart.MultipartFile; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.Mockito.doNothing; import static org.mockito.Mockito.doThrow; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.never; import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; import java.lang.reflect.Method; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; @SpringBootTest @ActiveProfiles("local") @ExtendWith(MockitoExtension.class) public class FileMgmtServiceTest { @Mock AwsS3ClientBuilderService awsS3ClientBuilderService; @Mock private AmazonS3 s3Client; @InjectMocks FileMgmtService fileMgmtService; @BeforeEach void setUp() throws Exception { MockitoAnnotations.initMocks(this); ObjectMapper mapper = new ObjectMapper(); JsonNode element =mapper.readTree(AppTestConstants.JSON_CONTENT); Map<String, JsonNode> mockMap = new HashMap<>(); mockMap.put(element.get(AppTestConstants.BUCKET_KEY).asText(), element); ReflectionTestUtils.setField(FileMgmtApplication.class, AppTestConstants.VAULT_VARIABLES_MAP, mockMap); } @Test void testListFiles_Error() throws Exception { try { List<String> result = fileMgmtService.listFiles(AppTestConstants.BUCKET); } catch (Exception e) { Assertions.assertEquals(AppTestConstants.LIST_FILES_ERROR_STR, e.getMessage()); } } @Test void testDownloadFile_Error() throws Exception { try { byte[] result = fileMgmtService.downloadFile(AppTestConstants.BUCKET, AppTestConstants.FILENAME); } catch (Exception e) { Assertions.assertEquals(AppTestConstants.DOWNLOAD_FILE_ERROR_STR, e.getMessage()); } } @Test void testDeleteFile_Error() throws Exception { try { boolean result = fileMgmtService.delete(AppTestConstants.BUCKET, AppTestConstants.FILENAME); } catch (Exception e) { Assertions.assertEquals(AppTestConstants.DELETE_FILE_ERROR_STR, e.getMessage()); } } @Test void testUploadFile_Error() throws Exception { final MultipartFile mockFile = Mockito.mock(MultipartFile.class); byte[] buffer = new byte[10]; Mockito.when(mockFile.getOriginalFilename()).thenReturn(AppTestConstants.COOLNAME); Mockito.when(mockFile.getBytes()).thenReturn(buffer); try { String response = fileMgmtService.uploadFile(AppTestConstants.BUCKET, AppTestConstants.FILENAME, mockFile); } catch (Exception e) { Assertions.assertEquals(AppTestConstants.UPLOAD_FILE_ERROR_STR, e.getMessage()); } } }
Leave a Comment