Skip to content

Commit

Permalink
handle feedback v2
Browse files Browse the repository at this point in the history
  • Loading branch information
quanpham-axonivy committed Dec 31, 2024
1 parent c907a8e commit 66b4d8a
Show file tree
Hide file tree
Showing 9 changed files with 163 additions and 138 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class PreviewConstants {

public static final String PREVIEW_DIR = " marketplace-service/data/work";
public static final String PREVIEW_DIR = "data/work/preview";

public static final String IMAGE_DOWNLOAD_URL = "%s/api/image/preview/%s";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,7 @@ public ResponseEntity<byte[]> findImageById(
}

@GetMapping(BY_FILE_NAME)
@Operation(summary = "Get the preview image content by file name",
description = "Collect the byte[] of image with contentType in header is PNG")
@ApiResponse(responseCode = "200", description = "Image found and returned",
content = @Content(mediaType = MediaType.IMAGE_PNG_VALUE, schema = @Schema(implementation = Image.class)))
@ApiResponse(responseCode = "404", description = "Image not found")
@ApiResponse(responseCode = "204", description = "No content (image empty)")
@Operation(hidden = true)
public ResponseEntity<byte[]> findPreviewImageByName(
@PathVariable("imageName") String imageName) {
HttpHeaders headers = new HttpHeaders();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public static void writeToFile(File file, String content) throws IOException {
}
}

// Common method to extract .zip file
public static void unzip(MultipartFile file, String location) throws IOException {
File extractDir = new File(location);
prepareUnZipDirectory(extractDir.toPath());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.MockedStatic;
import org.mockito.Mockito;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.mock.web.MockMultipartFile;

import java.io.IOException;
import java.nio.file.Files;
Expand All @@ -24,118 +26,144 @@
@ExtendWith(MockitoExtension.class)
class ReleasePreviewServiceImplTest {

private ReleasePreviewServiceImpl releasePreviewService;
private ReleasePreviewServiceImpl releasePreviewService;

private Path tempDirectory;
private Path tempDirectory;

private final String baseUrl = "http://example.com";
private static final String BASE_URL = "http://example.com";

private final String readmeContent = "# Sample README Content\n![image](image1.png)";
private static final String README_CONTENT = "# Sample README Content\n![image](image1.png)";

private final String updatedReadme = "# Sample README Content\n![image](http://example" +
".com/api/image/preview/image1.png)";
private static final String UPDATED_README_CONTENT = "# 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 {
FileUtils.clearDirectory(tempDirectory);
}
@BeforeEach
void setUp() throws IOException {
releasePreviewService = spy(new ReleasePreviewServiceImpl());
tempDirectory = Files.createTempDirectory("test-dir");
}

@Test
void testProcessReadme() throws IOException {
Path tempReadmeFile = Files.createTempFile("README", ".md");
Files.writeString(tempReadmeFile, readmeContent);
Map<String, Map<String, String>> moduleContents = new HashMap<>();
@AfterEach
void tearDown() throws IOException {
FileUtils.clearDirectory(tempDirectory);
}

doReturn(updatedReadme).when(releasePreviewService)
.updateImagesWithDownloadUrl(tempDirectory.toString(), readmeContent, baseUrl);
releasePreviewService.processReadme(tempReadmeFile, moduleContents, baseUrl, tempDirectory.toString());
@Test
void testProcessReadme() throws IOException {
Path tempReadmeFile = Files.createTempFile("README", ".md");
Files.writeString(tempReadmeFile, README_CONTENT);
Map<String, Map<String, String>> moduleContents = new HashMap<>();

assertEquals(3, moduleContents.size());
Files.deleteIfExists(tempReadmeFile);
}
doReturn(UPDATED_README_CONTENT).when(releasePreviewService)
.updateImagesWithDownloadUrl(tempDirectory.toString(), README_CONTENT, BASE_URL);
releasePreviewService.processReadme(tempReadmeFile, moduleContents, BASE_URL, tempDirectory.toString());

@Test
void testUpdateImagesWithDownloadUrl_Success() throws IOException {
Path tempReadmeFile = Files.createTempFile("README", ".md");
Files.writeString(tempReadmeFile, readmeContent);
String parentPath = tempReadmeFile.getParent().toString();
assertEquals(3, moduleContents.size());
Files.deleteIfExists(tempReadmeFile);
}

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);
@Test
void testUpdateImagesWithDownloadUrl_Success() throws IOException {
Path tempReadmeFile = Files.createTempFile("README", ".md");
Files.writeString(tempReadmeFile, README_CONTENT);
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,
README_CONTENT
, BASE_URL);

assertNotNull(result);
assertTrue(result.contains(String.format(IMAGE_DOWNLOAD_URL, BASE_URL, "image1.png")));
}
Files.deleteIfExists(tempReadmeFile);
}

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"));
assertDoesNotThrow(
() -> releasePreviewService.updateImagesWithDownloadUrl(tempDirectory.toString()
, README_CONTENT, BASE_URL));
}
}
Files.deleteIfExists(tempReadmeFile);
}

@Test
void testUpdateImagesWithDownloadUrl_IOException() {
try (MockedStatic<Files> mockedFiles = mockStatic(Files.class)) {
mockedFiles.when(() -> Files.walk(tempDirectory))
.thenThrow(new IOException("Simulated IOException"));
assertDoesNotThrow(
() -> releasePreviewService.updateImagesWithDownloadUrl(tempDirectory.toString(), readmeContent, baseUrl));

@Test
void testExtractReadme_Success() throws IOException {
String parentPath = tempDirectory.getParent().toString();
Path readmeFile = FileUtils.createFile(parentPath + "/README.md").toPath();
Files.writeString(readmeFile, README_CONTENT);

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

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

@Test
void testExtractReadme_Success() throws IOException {
String parentPath = tempDirectory.getParent().toString();
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(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);

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

ReleasePreview result = releasePreviewService.extractReadme(BASE_URL, tempDirectory.toString());
assertNull(result);
}
}
Files.deleteIfExists(readmeFile);
}

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

ReleasePreview result = releasePreviewService.extractReadme(BASE_URL, tempDirectory.toString());
assertNull(result);
assertDoesNotThrow(
() -> releasePreviewService.extractReadme(BASE_URL, tempDirectory.toString()));
}
}

ReleasePreview result = releasePreviewService.extractReadme(baseUrl, tempDirectory.toString());
assertNull(result);
@Test
void testExtract_Success() {
MockMultipartFile mockMultipartFile = new MockMultipartFile("file", "mockFileName",
"application/zip", "test".getBytes());
try (MockedStatic<FileUtils> fileUtils = Mockito.mockStatic(FileUtils.class)) {
fileUtils.when(() -> FileUtils.unzip(any(), anyString())).thenAnswer(invocation -> null);
}
when(releasePreviewService.extractReadme(anyString(), anyString())).thenReturn(new ReleasePreview());
ReleasePreview result = releasePreviewService.extract(mockMultipartFile, tempDirectory.toString());
assertNotNull(result);
}
}

@Test
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(
() -> releasePreviewService.extractReadme(baseUrl, tempDirectory.toString()));

@Test
void testExtract_IOException() {
MockMultipartFile mockMultipartFile = new MockMultipartFile("file", "mockFileName",
"application/zip", "test".getBytes());
try (MockedStatic<FileUtils> fileUtils = Mockito.mockStatic(FileUtils.class)) {
fileUtils.when(() -> FileUtils.unzip(any(), anyString())).thenThrow(new IOException());
}
ReleasePreview result = releasePreviewService.extract(mockMultipartFile, tempDirectory.toString());
assertNull(result);
assertDoesNotThrow(
() -> releasePreviewService.extract(mockMultipartFile, tempDirectory.toString()));
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ <h3 [lang]="languageService.selectedLanguage()" class="text-secondary">
<div class="form-group">
<input
type="file"
id="fileInput"
id="file-input"
(change)="onFileSelected($event)"
accept=".zip"
class="form-control" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,25 +20,25 @@
}

.hint-text {
font-size: 10px;
font-size: 1rem;
}

/* Form Styles */
.form-group {
font-size: 18px;
font-size: 1.8rem;
margin-bottom: 15px;
}

/* File Input Styles */
#fileInput {
#file-input {
border: 1px solid #ced4da;
padding: 10px;
width: 100%;
border-radius: 5px;
margin-top: 10px;
}

#fileInput:focus {
#file-input:focus {
outline: none;
border-color: #5b9bd5;
}
Expand All @@ -61,13 +61,12 @@ button:disabled {
.no-tabs {
text-align: center;
padding: 50px;
font-size: 1.2rem;
color: #6c757d;
}

.no-tabs p {
margin: 0;
color: #6c757d;
font-size: 1.2rem;
color: var(--text-no-rating-color);
}

/* Hint content styling */
Expand Down Expand Up @@ -111,21 +110,21 @@ button:disabled {

.readme-content ::ng-deep {
h1 {
font-size: 24px;
font-size: 2.4rem;
}

h2 {
font-size: 22px;
font-size: 2.2rem;
}

h3 {
font-size: 20px;
font-size: 2rem;
}

p,
ul,
li {
font-size: 18px;
font-size: 1.8rem;
font-weight: 400;
color: var(--ivy-text-primary-color);
}
Expand Down Expand Up @@ -156,7 +155,7 @@ button:disabled {
padding: 8px;
word-wrap: break-word;
white-space: normal;
font-size: 12px;
font-size: 1.2rem;
}

tr {
Expand All @@ -182,7 +181,7 @@ button:disabled {

a {
font-weight: 400;
font-size: 22px;
font-size: 2.2rem;
}

a.active {
Expand Down Expand Up @@ -231,15 +230,4 @@ button:disabled {

.dropdown-tab {
margin-top: 10px;
}

.tab-group {
gap: 40px;

.tab-content {
.tab-pane {
opacity: 1;
transition: none;
}
}
}
Loading

0 comments on commit 66b4d8a

Please sign in to comment.