Skip to content

Commit

Permalink
handle feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
quanpham-axonivy committed Dec 27, 2024
1 parent 07e2e4c commit 04eba4b
Show file tree
Hide file tree
Showing 18 changed files with 1,646 additions and 375 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/release-preview";
public static final String PREVIEW_DIR = " marketplace-service/data/work";

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 @@ -18,7 +18,9 @@
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import static com.axonivy.market.constants.RequestMappingConstants.*;
import static com.axonivy.market.constants.RequestMappingConstants.BY_ID;
import static com.axonivy.market.constants.RequestMappingConstants.IMAGE;
import static com.axonivy.market.constants.RequestMappingConstants.BY_FILE_NAME;
import static com.axonivy.market.constants.RequestParamConstants.ID;

@RestController
Expand Down Expand Up @@ -55,6 +57,12 @@ 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)")
public ResponseEntity<byte[]> findPreviewImageByName(
@PathVariable("imageName") String imageName) {
HttpHeaders headers = new HttpHeaders();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
package com.axonivy.market.model;

import io.swagger.v3.oas.annotations.media.Schema;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.*;

import java.util.Map;

import static com.axonivy.market.util.ProductContentUtils.*;

import static com.axonivy.market.util.ProductContentUtils.DESCRIPTION;
import static com.axonivy.market.util.ProductContentUtils.DEMO;
import static com.axonivy.market.util.ProductContentUtils.SETUP;
import static com.axonivy.market.util.ProductContentUtils.replaceEmptyContentsWithEnContent;

@Getter
@Setter
Expand All @@ -19,19 +17,19 @@
@AllArgsConstructor
public class ReleasePreview {

@Schema(description = "Product detail description content ",
example = "{ \"de\": \"E-Sign-Konnektor\", \"en\": \"E-sign connector\" }")
private Map<String, String> description;
@Schema(description = "Setup tab content", example = "{ \"de\": \"Setup\", \"en\": \"Setup\" ")
private Map<String, String> setup;
@Schema(description = "Demo tab content", example = "{ \"de\": \"Demo\", \"en\": \"Demo\" ")
private Map<String, String> demo;

public static ReleasePreview from(Map<String, Map<String, String>> moduleContents) {
return ReleasePreview.builder().description(replaceEmptyContentsWithEnContent(moduleContents.get(DESCRIPTION)))
.demo(replaceEmptyContentsWithEnContent(moduleContents.get(DEMO)))
.setup(replaceEmptyContentsWithEnContent(moduleContents.get(SETUP)))
.build();
}
@Schema(description = "Product detail description content ",
example = "{ \"de\": \"E-Sign-Konnektor\", \"en\": \"E-sign connector\" }")
private Map<String, String> description;
@Schema(description = "Setup tab content", example = "{ \"de\": \"Setup\", \"en\": \"Setup\" ")
private Map<String, String> setup;
@Schema(description = "Demo tab content", example = "{ \"de\": \"Demo\", \"en\": \"Demo\" ")
private Map<String, String> demo;

public static ReleasePreview from(Map<String, Map<String, String>> moduleContents) {
return ReleasePreview.builder().description(replaceEmptyContentsWithEnContent(moduleContents.get(DESCRIPTION)))
.demo(replaceEmptyContentsWithEnContent(moduleContents.get(DEMO)))
.setup(replaceEmptyContentsWithEnContent(moduleContents.get(SETUP)))
.build();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,18 @@
@AllArgsConstructor
public class ReleasePreviewServiceImpl implements ReleasePreviewService {


@Override
public ReleasePreview extract(MultipartFile file, String baseUrl) {
try {
FileUtils.unzip(file, PREVIEW_DIR);
} catch (IOException e){
log.info("#extract Error extracting zip file, message: {}", e.getMessage());
return null;
}
return extractREADME(baseUrl, PREVIEW_DIR);
return extractReadme(baseUrl, PREVIEW_DIR);
}

public ReleasePreview extractREADME(String baseUrl, String location) {
public ReleasePreview extractReadme(String baseUrl, String location) {
Map<String, Map<String, String>> moduleContents = new HashMap<>();
try (Stream<Path> readmePathStream = Files.walk(Paths.get(location))) {
List<Path> readmeFiles = readmePathStream.filter(Files::isRegularFile)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public static void unzip(MultipartFile file, String location) throws IOException
File parentDir = outFile.getParentFile();
createDirectoryFromFile(parentDir);
try (FileOutputStream fos = new FileOutputStream(outFile)) {
byte[] buffer = new byte[1024];
byte[] buffer = new byte[4096];
int length;
while ((length = zipInputStream.read(buffer)) > 0) {
fos.write(buffer, 0, length);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,6 @@ jwt.secret=${MARKET_JWT_SECRET_KEY}
jwt.expiration=365
logging.level.org.springframework.data.mongodb.core.MongoTemplate=${MARKET_MONGO_LOG_LEVEL}
spring.jackson.serialization.indent_output=true
loggable.log-path=${MARKET_LOG_PATH}
loggable.log-path=${MARKET_LOG_PATH}
spring.servlet.multipart.max-file-size=20MB
spring.servlet.multipart.max-request-size=20MB
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ public class BaseSetup {
protected static final String MOCK_IMAGE_URL = "https://raw.githubusercontent" +
".com/amazon-comprehend-connector-product/images/comprehend-demo-sentiment.png";
protected static final String INSTALLATION_FILE_PATH = "src/test/resources/installationCount.json";
protected static final String IMAGE_NAME = "test.png";

protected Page<Product> createPageProductsMock() {
var mockProducts = new ArrayList<Product>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ void testMappingImageFromGHContent_noGhContent() {

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

try (MockedStatic<Files> mockedFiles = mockStatic(Files.class);
MockedStatic<MavenUtils> mockedMavenUtils = mockStatic(MavenUtils.class)) {
Expand All @@ -177,7 +177,7 @@ void testReadPreviewImageByName_ImageExists() {
InputStream mockedInputStream = new ByteArrayInputStream("mocked image content".getBytes());
mockedMavenUtils.when(() -> MavenUtils.extractedContentStream(imagePath)).thenReturn(mockedInputStream);

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

assertNotNull(result);
assertArrayEquals("mocked image content".getBytes(), result);
Expand All @@ -188,15 +188,15 @@ void testReadPreviewImageByName_ImageExists() {
void testReadPreviewImageByName_NotFoundDirectory() {
try (MockedStatic<Files> mockedFiles = mockStatic(Files.class)) {
mockedFiles.when(() -> Files.exists(any())).thenReturn(false);
byte[] result = imageService.readPreviewImageByName("test.png");
byte[] result = imageService.readPreviewImageByName(IMAGE_NAME);

assertNull(result);
}
}

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

try (MockedStatic<Files> mockedFiles = mockStatic(Files.class);
MockedStatic<MavenUtils> mockedMavenUtils = mockStatic(MavenUtils.class)) {
Expand All @@ -221,7 +221,7 @@ void testReadPreviewImageByName_IOException() {
mockedFiles.when(() -> Files.isRegularFile(any())).thenReturn(true);
mockedFiles.when(() -> Files.walk(any())).thenThrow(new IOException("Exception!!"));

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ void testUpdateImagesWithDownloadUrl_IOException() {
}

@Test
void testExtractREADME_Success() throws IOException {
void testExtractReadme_Success() throws IOException {
String parentPath = tempDirectory.getParent().toString();
Path readmeFile = FileUtils.createFile(parentPath + "/README.md").toPath();
Files.writeString(readmeFile, readmeContent);
Expand All @@ -108,7 +108,7 @@ void testExtractREADME_Success() throws IOException {
when(releasePreviewService.updateImagesWithDownloadUrl(any(), anyString(), anyString())).thenReturn(
updatedReadme);

ReleasePreview result = releasePreviewService.extractREADME(baseUrl, tempDirectory.toString());
ReleasePreview result = releasePreviewService.extractReadme(baseUrl, tempDirectory.toString());
assertNotNull(result);
}
Files.deleteIfExists(readmeFile);
Expand All @@ -120,21 +120,21 @@ void testExtractREADME_NoReadmeFiles() {
mockedFiles.when(() -> Files.walk(tempDirectory))
.thenReturn(Stream.empty());

ReleasePreview result = releasePreviewService.extractREADME(baseUrl, tempDirectory.toString());
ReleasePreview result = releasePreviewService.extractReadme(baseUrl, tempDirectory.toString());
assertNull(result);
}
}

@Test
void testExtractREADME_IOException() {
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());
ReleasePreview result = releasePreviewService.extractReadme(baseUrl, tempDirectory.toString());
assertNull(result);
assertDoesNotThrow(
() -> releasePreviewService.extractREADME(baseUrl, tempDirectory.toString()));
() -> releasePreviewService.extractReadme(baseUrl, tempDirectory.toString()));
}
}

Expand Down
Loading

0 comments on commit 04eba4b

Please sign in to comment.