Skip to content

Commit

Permalink
Write unit test for ImageServiceImpl
Browse files Browse the repository at this point in the history
  • Loading branch information
quanpham-axonivy committed Dec 26, 2024
1 parent 39802d6 commit 3f5f472
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,18 @@
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Collections;
import java.util.List;
import java.util.stream.Stream;

import static com.axonivy.market.constants.CommonConstants.SLASH;
import static com.axonivy.market.constants.MetaConstants.META_FILE;
import static org.bson.assertions.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.any;
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 static org.mockito.Mockito.*;

@ExtendWith(MockitoExtension.class)
class ImageServiceImplTest extends BaseSetup {
Expand Down Expand Up @@ -166,4 +162,67 @@ void testMappingImageFromGHContent_noGhContent() {
var result = imageService.mappingImageFromGHContent(GOOGLE_MAPS_CONNECTOR, null);
assertNull(result);
}

@Test
void testReadPreviewImageByName_ImageExists() {
Path imagePath = Path.of("test.png");

try (MockedStatic<Files> mockedFiles = mockStatic(Files.class);
MockedStatic<MavenUtils> mockedMavenUtils = mockStatic(MavenUtils.class)) {
mockedFiles.when(() -> Files.exists(any())).thenReturn(true);
mockedFiles.when(() -> Files.isDirectory(any())).thenReturn(true);
mockedFiles.when(() -> Files.isRegularFile(any())).thenReturn(true);
mockedFiles.when(() -> Files.walk(any())).thenReturn(Stream.of(imagePath));

InputStream mockedInputStream = new ByteArrayInputStream("mocked image content".getBytes());
mockedMavenUtils.when(() -> MavenUtils.extractedContentStream(imagePath)).thenReturn(mockedInputStream);

byte[] result = imageService.readPreviewImageByName("test.png");

assertNotNull(result);
assertArrayEquals("mocked image content".getBytes(), result);
}
}

@Test
void testReadPreviewImageByName_NotFoundDirectory() {
try (MockedStatic<Files> mockedFiles = mockStatic(Files.class)) {
mockedFiles.when(() -> Files.exists(any())).thenReturn(false);
byte[] result = imageService.readPreviewImageByName("test.png");

assertNull(result);
}
}

@Test
void testReadPreviewImageByName_NotFoundImage() {
Path imagePath = Path.of("test.png");

try (MockedStatic<Files> mockedFiles = mockStatic(Files.class);
MockedStatic<MavenUtils> mockedMavenUtils = mockStatic(MavenUtils.class)) {

mockedFiles.when(() -> Files.exists(any())).thenReturn(true);
mockedFiles.when(() -> Files.isDirectory(any())).thenReturn(true);
mockedFiles.when(() -> Files.isRegularFile(any())).thenReturn(true);
mockedFiles.when(() -> Files.walk(any())).thenReturn(Stream.of(imagePath));

byte[] result = imageService.readPreviewImageByName("wrong.png");

assertNull(result);
}
}

@Test
void testReadPreviewImageByName_IOException() {
try (MockedStatic<Files> mockedFiles = mockStatic(Files.class)) {

mockedFiles.when(() -> Files.exists(any())).thenReturn(true);
mockedFiles.when(() -> Files.isDirectory(any())).thenReturn(true);
mockedFiles.when(() -> Files.isRegularFile(any())).thenReturn(true);
mockedFiles.when(() -> Files.walk(any())).thenThrow(new IOException("Exception!!"));

assertDoesNotThrow(() -> imageService.readPreviewImageByName("test.png"));
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,11 @@ void testProcessReadme() throws IOException {
Path tempReadmeFile = Files.createTempFile("README", ".md");
Files.writeString(tempReadmeFile, readmeContent);
Map<String, Map<String, String>> moduleContents = new HashMap<>();

doReturn(updatedReadme).when(releasePreviewService)
.updateImagesWithDownloadUrl(tempDirectory.toString(), readmeContent, baseUrl);
releasePreviewService.processReadme(tempReadmeFile, moduleContents, baseUrl, tempDirectory.toString());

assertEquals(3, moduleContents.size());
Files.deleteIfExists(tempReadmeFile);
}
Expand All @@ -77,16 +79,14 @@ void testUpdateImagesWithDownloadUrl_Success() throws IOException {
assertNotNull(result);
assertTrue(result.contains(String.format(IMAGE_DOWNLOAD_URL, baseUrl, "image1.png")));
}
Files.deleteIfExists(tempReadmeFile);
}

@Test
void testUpdateImagesWithDownloadUrl_IOException() {
try (MockedStatic<Files> mockedFiles = mockStatic(Files.class)) {
mockedFiles.when(() -> Files.walk(tempDirectory))
.thenThrow(new IOException("Simulated IOException"));
String result = releasePreviewService.updateImagesWithDownloadUrl(tempDirectory.toString(), readmeContent,
baseUrl);
assertNull(result);
assertDoesNotThrow(
() -> releasePreviewService.updateImagesWithDownloadUrl(tempDirectory.toString(), readmeContent, baseUrl));
}
Expand All @@ -95,31 +95,33 @@ void testUpdateImagesWithDownloadUrl_IOException() {
@Test
void testExtractREADME_Success() throws IOException {
String parentPath = tempDirectory.getParent().toString();
Path readmeFile1 = FileUtils.createFile(parentPath + "/README.md").toPath();
Files.writeString(readmeFile1, readmeContent);
Path readmeFile = FileUtils.createFile(parentPath + "/README.md").toPath();
Files.writeString(readmeFile, readmeContent);

try (MockedStatic<Files> mockedFiles = mockStatic(Files.class)) {
mockedFiles.when(() -> Files.walk(tempDirectory))
.thenReturn(Stream.of(readmeFile1));
.thenReturn(Stream.of(readmeFile));
mockedFiles.when(() -> Files.isRegularFile(any()))
.thenReturn(true);
mockedFiles.when(() -> Files.readString(any()))
.thenReturn(readmeContent);
when(releasePreviewService.updateImagesWithDownloadUrl(any(), anyString(), anyString())).thenReturn(
updatedReadme);

ReleasePreview result = releasePreviewService.extractREADME(baseUrl, tempDirectory.toString());
assertNotNull(result);
}
Files.deleteIfExists(readmeFile);
}

@Test
void testExtractREADME_NoReadmeFiles() {
try (MockedStatic<Files> mockedFiles = mockStatic(Files.class)) {
mockedFiles.when(() -> Files.walk(tempDirectory))
.thenReturn(Stream.empty());

ReleasePreview result = releasePreviewService.extractREADME(baseUrl, tempDirectory.toString());
assertNull(result);
mockedFiles.verify(() -> Files.walk(tempDirectory), times(1));
}
}

Expand All @@ -128,6 +130,7 @@ void testExtractREADME_IOException() {
try (MockedStatic<Files> mockedFiles = mockStatic(Files.class)) {
mockedFiles.when(() -> Files.walk(tempDirectory))
.thenThrow(new IOException("Simulated IOException"));

ReleasePreview result = releasePreviewService.extractREADME(baseUrl, tempDirectory.toString());
assertNull(result);
assertDoesNotThrow(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,8 @@ void testUnzip_Success() throws Exception {
Path tempDirectory = Files.createTempDirectory("test-dir");
String mockFileName = "test.zip";
byte[] zipContent = createMockZipContent();
MockMultipartFile mockMultipartFile = new MockMultipartFile("file", mockFileName, "application/zip", zipContent);
MockMultipartFile mockMultipartFile = new MockMultipartFile("file", mockFileName,
"application/zip", zipContent);
try (MockedStatic<Files> mockedFiles = mockStatic(Files.class)) {
mockedFiles.when(() -> Files.exists(tempDirectory)).thenReturn(false);
mockedFiles.when(() -> Files.createDirectories(tempDirectory)).thenReturn(null);
Expand Down

0 comments on commit 3f5f472

Please sign in to comment.