-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e875aeb
commit 7491093
Showing
4 changed files
with
216 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
marketplace-service/src/main/java/com/axonivy/market/constants/PreviewConstants.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package com.axonivy.market.constants; | ||
|
||
import lombok.AccessLevel; | ||
import lombok.NoArgsConstructor; | ||
|
||
@NoArgsConstructor(access = AccessLevel.PRIVATE) | ||
public class PreviewConstants { | ||
|
||
public static final String PREVIEW_DIR = "marketplace-service/release-preview"; | ||
|
||
public static final String IMAGE_DOWNLOAD_URL = "%s/api/image/preview/%s"; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
195 changes: 195 additions & 0 deletions
195
...-service/src/test/java/com/axonivy/market/service/impl/ReleasePreviewServiceImplTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,195 @@ | ||
package com.axonivy.market.service.impl; | ||
|
||
import com.axonivy.market.model.ReleasePreview; | ||
import com.axonivy.market.util.FileUtils; | ||
import org.junit.jupiter.api.AfterEach; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.mockito.MockedStatic; | ||
import org.mockito.junit.jupiter.MockitoExtension; | ||
import org.springframework.mock.web.MockMultipartFile; | ||
|
||
import java.io.ByteArrayOutputStream; | ||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.nio.file.Paths; | ||
import java.util.Comparator; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.stream.Stream; | ||
import java.util.zip.ZipEntry; | ||
import java.util.zip.ZipOutputStream; | ||
|
||
import static com.axonivy.market.constants.PreviewConstants.IMAGE_DOWNLOAD_URL; | ||
import static com.axonivy.market.constants.PreviewConstants.PREVIEW_DIR; | ||
import static org.junit.jupiter.api.Assertions.*; | ||
import static org.mockito.Mockito.*; | ||
|
||
; | ||
|
||
@ExtendWith(MockitoExtension.class) | ||
class ReleasePreviewServiceImplTest { | ||
|
||
private ReleasePreviewServiceImpl releasePreviewService; | ||
private Path tempDirectory; | ||
|
||
private final String baseUrl = "http://example.com"; | ||
|
||
private final String readmeContent = "# Sample README Content\n![image](image1.png)"; | ||
|
||
private final String updatedReadme = "# Sample README Content\n![image](http://example" + | ||
".com/api/image/preview/image1.png)"; | ||
|
||
@BeforeEach | ||
void setUp() throws IOException { | ||
releasePreviewService = spy(new ReleasePreviewServiceImpl()); | ||
tempDirectory = Files.createTempDirectory("test-dir"); | ||
} | ||
|
||
@AfterEach | ||
void tearDown() throws IOException { | ||
clear(tempDirectory); | ||
clear(Path.of(PREVIEW_DIR).getParent()); | ||
} | ||
|
||
private void clear(Path path) throws IOException { | ||
if (Files.exists(path)) { | ||
Files.walk(path) | ||
.sorted(Comparator.reverseOrder()) | ||
.map(Path::toFile) | ||
.forEach(java.io.File::delete); | ||
} | ||
} | ||
|
||
@Test | ||
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(PREVIEW_DIR, readmeContent, baseUrl); | ||
releasePreviewService.processReadme(tempReadmeFile, moduleContents, baseUrl); | ||
assertEquals(3, moduleContents.size()); | ||
Files.deleteIfExists(tempReadmeFile); | ||
} | ||
|
||
@Test | ||
void testPrepareUnZipDirectory() { | ||
releasePreviewService.prepareUnZipDirectory(tempDirectory); | ||
assertTrue(Files.exists(tempDirectory)); | ||
} | ||
|
||
@Test | ||
void testPrepareUnZipDirectory_IOException() { | ||
try (MockedStatic<Files> mockedFiles = mockStatic(Files.class)) { | ||
mockedFiles.when(() -> Files.exists(tempDirectory)).thenReturn(true); | ||
} | ||
assertDoesNotThrow(() -> releasePreviewService.prepareUnZipDirectory(tempDirectory)); | ||
} | ||
|
||
@Test | ||
void testUpdateImagesWithDownloadUrl_Success() throws IOException { | ||
Path tempReadmeFile = Files.createTempFile("README", ".md"); | ||
Files.writeString(tempReadmeFile, readmeContent); | ||
String parentPath = tempReadmeFile.getParent().toString(); | ||
|
||
Path imagePath1 = Paths.get(parentPath + "/image1.png"); | ||
try (MockedStatic<Files> mockedFiles = mockStatic(Files.class)) { | ||
mockedFiles.when(() -> Files.walk(Paths.get(parentPath))) | ||
.thenReturn(Stream.of(imagePath1)); | ||
mockedFiles.when(() -> Files.isRegularFile(any())) | ||
.thenReturn(true); | ||
String result = releasePreviewService.updateImagesWithDownloadUrl(parentPath, | ||
readmeContent | ||
, baseUrl); | ||
|
||
assertNotNull(result); | ||
assertTrue(result.contains(String.format(IMAGE_DOWNLOAD_URL, baseUrl, "image1.png"))); | ||
} | ||
} | ||
|
||
@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)); | ||
} | ||
} | ||
|
||
@Test | ||
void testExtractREADME_Success() throws IOException { | ||
String parentPath = tempDirectory.getParent().toString(); | ||
Path readmeFile1 = FileUtils.createFile(parentPath + "/README.md").toPath(); | ||
Files.writeString(readmeFile1, readmeContent); | ||
|
||
try (MockedStatic<Files> mockedFiles = mockStatic(Files.class)) { | ||
mockedFiles.when(() -> Files.walk(Paths.get(PREVIEW_DIR))) | ||
.thenReturn(Stream.of(readmeFile1)); | ||
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); | ||
assertNotNull(result); | ||
} | ||
} | ||
|
||
@Test | ||
void testExtractREADME_NoReadmeFiles() { | ||
try (MockedStatic<Files> mockedFiles = mockStatic(Files.class)) { | ||
mockedFiles.when(() -> Files.walk(Paths.get(PREVIEW_DIR))) | ||
.thenReturn(Stream.empty()); | ||
ReleasePreview result = releasePreviewService.extractREADME(baseUrl); | ||
assertNull(result); | ||
mockedFiles.verify(() -> Files.walk(Paths.get(PREVIEW_DIR)), times(1)); | ||
} | ||
} | ||
|
||
@Test | ||
void testExtractREADME_IOException() { | ||
try (MockedStatic<Files> mockedFiles = mockStatic(Files.class)) { | ||
mockedFiles.when(() -> Files.walk(Paths.get(PREVIEW_DIR))) | ||
.thenThrow(new IOException("Simulated IOException")); | ||
ReleasePreview result = releasePreviewService.extractREADME(baseUrl); | ||
assertNull(result); | ||
assertDoesNotThrow( | ||
() -> releasePreviewService.extractREADME(baseUrl)); | ||
} | ||
} | ||
|
||
@Test | ||
void testUnzip_Success() throws Exception { | ||
String mockFileName = "test.zip"; | ||
byte[] zipContent = createMockZipContent(); | ||
MockMultipartFile mockMultipartFile = new MockMultipartFile("file", mockFileName, "application/zip", zipContent); | ||
Path path = Paths.get(PREVIEW_DIR); | ||
try (MockedStatic<Files> mockedFiles = mockStatic(Files.class)) { | ||
mockedFiles.when(() -> Files.exists(path)).thenReturn(false); | ||
mockedFiles.when(() -> Files.createDirectories(path)).thenReturn(null); | ||
releasePreviewService.unzip(mockMultipartFile); | ||
mockedFiles.verify(() -> Files.exists(path), times(1)); | ||
mockedFiles.verify(() -> Files.createDirectories(path), times(1)); | ||
} | ||
} | ||
|
||
private byte[] createMockZipContent() throws IOException { | ||
ByteArrayOutputStream baos = new ByteArrayOutputStream(); | ||
try (ZipOutputStream zos = new ZipOutputStream(baos)) { | ||
ZipEntry entry = new ZipEntry("mockFile.txt"); | ||
zos.putNextEntry(entry); | ||
zos.write("Mock file content".getBytes()); | ||
zos.closeEntry(); | ||
} | ||
return baos.toByteArray(); | ||
} | ||
|
||
} |