Skip to content

Commit

Permalink
MARP-1248 Images in demo tab of Graphql-Demo are missing (#202)
Browse files Browse the repository at this point in the history
  • Loading branch information
tvtphuc-axonivy authored Oct 18, 2024
1 parent 2ae7146 commit 36919be
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
import com.axonivy.market.entity.Image;
import com.axonivy.market.github.util.GitHubUtils;
import com.axonivy.market.repository.ImageRepository;
import com.axonivy.market.service.FileDownloadService;
import com.axonivy.market.service.ImageService;
import com.axonivy.market.util.MavenUtils;
import lombok.AllArgsConstructor;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.ObjectUtils;
Expand All @@ -21,13 +23,11 @@

@Service
@Log4j2
@AllArgsConstructor
public class ImageServiceImpl implements ImageService {

private final ImageRepository imageRepository;

public ImageServiceImpl(ImageRepository imageRepository) {
this.imageRepository = imageRepository;
}
private final FileDownloadService fileDownloadService;

@Override
public Binary getImageBinary(GHContent ghContent) {
Expand All @@ -36,7 +36,17 @@ public Binary getImageBinary(GHContent ghContent) {
byte[] sourceBytes = IOUtils.toByteArray(contentStream);
return new Binary(sourceBytes);
} catch (Exception exception) {
log.error("Cannot get content of product logo {} ", ghContent.getName());
log.error("Cannot get content of product image {} ", ghContent.getName());
return null;
}
}

private Binary getImageByDownloadUrl(String downloadUrl) {
try {
byte[] downloadedImage = fileDownloadService.downloadFile(downloadUrl);
return new Binary(downloadedImage);
} catch (Exception exception) {
log.error("Cannot download the image from the url: {} with error {}", downloadUrl, exception.getMessage());
return null;
}
}
Expand All @@ -54,11 +64,15 @@ public Image mappingImageFromGHContent(String productId, GHContent ghContent, bo
return existsImage;
}
}
Image image = new Image();

String currentImageUrl = GitHubUtils.getDownloadUrl(ghContent);
Binary imageContent = Optional.ofNullable(getImageBinary(ghContent))
.orElseGet(() -> getImageByDownloadUrl(currentImageUrl));

Image image = new Image();
image.setProductId(productId);
image.setImageUrl(currentImageUrl);
image.setImageData(getImageBinary(ghContent));
image.setImageData(imageContent);
image.setSha(ghContent.getSha());
return imageRepository.save(image);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.axonivy.market.service.impl;

import com.axonivy.market.BaseSetup;
import com.axonivy.market.entity.Image;
import com.axonivy.market.repository.ImageRepository;
import com.axonivy.market.service.FileDownloadService;
import com.axonivy.market.util.MavenUtils;
import org.bson.types.Binary;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -36,13 +38,15 @@
import static org.mockito.Mockito.never;

@ExtendWith(MockitoExtension.class)
class ImageServiceImplTest {
class ImageServiceImplTest extends BaseSetup {
@Captor
ArgumentCaptor<Image> argumentCaptor = ArgumentCaptor.forClass(Image.class);
@InjectMocks
private ImageServiceImpl imageService;
@Mock
private ImageRepository imageRepository;
@Mock
private FileDownloadService fileDownloadService;
public static final String GOOGLE_MAPS_CONNECTOR = "google-maps-connector";

@Test
Expand Down Expand Up @@ -73,6 +77,23 @@ void testMappingImageFromGHContent() throws IOException {

}

@Test
void testMappingImageFromGHContent_getImageFromDownloadUrl() throws IOException {
GHContent content = mock(GHContent.class);
when(content.getSha()).thenReturn("914d9b6956db7a1404622f14265e435f36db81fa");
when(content.getDownloadUrl()).thenReturn(MOCK_MAVEN_URL);

when(content.read()).thenThrow(new UnsupportedOperationException("Unrecognized encoding"));
when(fileDownloadService.downloadFile(MOCK_MAVEN_URL)).thenReturn("content".getBytes());

imageService.mappingImageFromGHContent(GOOGLE_MAPS_CONNECTOR, content, false);

verify(imageRepository).save(argumentCaptor.capture());
verify(fileDownloadService, times(1)).downloadFile(MOCK_MAVEN_URL);
assertEquals(new Binary("content".getBytes()), argumentCaptor.getValue().getImageData());

}

@Test
void testMappingImageFromDownloadedFolder() {
try (MockedStatic<MavenUtils> mockedMavenUtils = Mockito.mockStatic(MavenUtils.class)) {
Expand Down

0 comments on commit 36919be

Please sign in to comment.