diff --git a/marketplace-service/README.md b/marketplace-service/README.md index 4d546f33a..d85b7f861 100644 --- a/marketplace-service/README.md +++ b/marketplace-service/README.md @@ -1,6 +1,7 @@ # Getting Started ### Reference Documentation + For further reference, please consider the following sections: * [Official Apache Maven documentation](https://maven.apache.org/guides/index.html) @@ -9,9 +10,11 @@ For further reference, please consider the following sections: * [Spring Web](https://docs.spring.io/spring-boot/docs/3.2.5/reference/htmlsingle/index.html#web) ### Guides + The following guides illustrate how to use some features concretely: -* Installing mongodb, and access it as Url mongodb://localhost:27017/, and you can create and name whatever you want ,then you should put them to application.properties +* Installing mongodb, and access it as Url mongodb://localhost:27017/, and you can create and name whatever you want + ,then you should put them to application.properties * You can change the MongoDB configuration in file `application.properties` ``` spring.data.mongodb.host= @@ -21,14 +24,18 @@ The following guides illustrate how to use some features concretely: * Run mvn clean install to build project * Run mvn test to test all tests - ### Access Swagger URL: http://{your-host}/swagger-ui/index.html ### Install Lombok for Eclipse IDE + * Download lombok here https://projectlombok.org/download -* run command "java -jar lombok.jar" then you can access file “eclipse.ini“ in eclipse folder where you install → there is a text like this: -javaagent:C:\Users\tvtphuc\eclipse\jee-2024-032\eclipse\lombok.jar → it means you are successful +* run command "java -jar lombok.jar" then you can access file “eclipse.ini“ in eclipse folder where you install → there + is a text like this: -javaagent:C:\Users\tvtphuc\eclipse\jee-2024-032\eclipse\lombok.jar → it means you are + successful * Start eclipse * Import the project then in the eclipse , you should run the command “mvn clean install“ -* After that you go to class MarketplaceServiceApplication → right click to main method → click run as → choose Java Application +* After that you go to class MarketplaceServiceApplication → right click to main method → click run as → choose Java + Application * Then you can send a request in postman -* If you want to run single test in class UserServiceImplTest. You can right-click to method testFindAllUser and right click → select Run as → choose JUnit Test \ No newline at end of file +* If you want to run single test in class UserServiceImplTest. You can right-click to method testFindAllUser and right + click → select Run as → choose JUnit Test \ No newline at end of file diff --git a/marketplace-service/pom.xml b/marketplace-service/pom.xml index d6a0d9a67..44d55027e 100644 --- a/marketplace-service/pom.xml +++ b/marketplace-service/pom.xml @@ -1,13 +1,13 @@ + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 org.springframework.boot spring-boot-starter-parent 3.2.5 - + com.axonivy.market marketplace-service diff --git a/marketplace-service/src/main/java/com/axonivy/market/constants/MavenConstants.java b/marketplace-service/src/main/java/com/axonivy/market/constants/MavenConstants.java index 4ba05471d..71cdb72ee 100644 --- a/marketplace-service/src/main/java/com/axonivy/market/constants/MavenConstants.java +++ b/marketplace-service/src/main/java/com/axonivy/market/constants/MavenConstants.java @@ -1,7 +1,8 @@ package com.axonivy.market.constants; public class MavenConstants { - private MavenConstants() {} + private MavenConstants() { + } public static final String SNAPSHOT_RELEASE_POSTFIX = "-SNAPSHOT"; public static final String SPRINT_RELEASE_POSTFIX = "-m"; diff --git a/marketplace-service/src/main/java/com/axonivy/market/constants/NonStandardProductPackageConstants.java b/marketplace-service/src/main/java/com/axonivy/market/constants/NonStandardProductPackageConstants.java index 133ff55ff..dec20303d 100644 --- a/marketplace-service/src/main/java/com/axonivy/market/constants/NonStandardProductPackageConstants.java +++ b/marketplace-service/src/main/java/com/axonivy/market/constants/NonStandardProductPackageConstants.java @@ -1,7 +1,8 @@ package com.axonivy.market.constants; public class NonStandardProductPackageConstants { - private NonStandardProductPackageConstants() {} + private NonStandardProductPackageConstants() { + } public static final String PORTAL = "portal"; public static final String MICROSOFT_REPO_NAME = "msgraph-connector"; diff --git a/marketplace-service/src/main/java/com/axonivy/market/constants/ProductJsonConstants.java b/marketplace-service/src/main/java/com/axonivy/market/constants/ProductJsonConstants.java index 96c6eb5e1..8d3aa03a2 100644 --- a/marketplace-service/src/main/java/com/axonivy/market/constants/ProductJsonConstants.java +++ b/marketplace-service/src/main/java/com/axonivy/market/constants/ProductJsonConstants.java @@ -17,5 +17,6 @@ public class ProductJsonConstants { public static final String MAVEN_DROPIN_INSTALLER_ID = "maven-dropins"; public static final String MAVEN_DEPENDENCY_INSTALLER_ID = "maven-dependency"; - private ProductJsonConstants() {} + private ProductJsonConstants() { + } } diff --git a/marketplace-service/src/main/java/com/axonivy/market/controller/OAuth2Controller.java b/marketplace-service/src/main/java/com/axonivy/market/controller/OAuth2Controller.java index ce74af732..611b6a860 100644 --- a/marketplace-service/src/main/java/com/axonivy/market/controller/OAuth2Controller.java +++ b/marketplace-service/src/main/java/com/axonivy/market/controller/OAuth2Controller.java @@ -14,7 +14,6 @@ import org.springframework.web.bind.annotation.RestController; import java.util.Collections; -import java.util.Map; @RestController @RequestMapping("/auth") diff --git a/marketplace-service/src/main/java/com/axonivy/market/enums/TypeOption.java b/marketplace-service/src/main/java/com/axonivy/market/enums/TypeOption.java index 1c30aca92..3176a7ba9 100644 --- a/marketplace-service/src/main/java/com/axonivy/market/enums/TypeOption.java +++ b/marketplace-service/src/main/java/com/axonivy/market/enums/TypeOption.java @@ -6,7 +6,8 @@ @Getter public enum TypeOption { - ALL("all", ""), CONNECTORS("connectors", "connector"), UTILITIES("utilities", "util"), SOLUTIONS("solutions", "solution"), DEMOS("demos", "demo"); + ALL("all", ""), CONNECTORS("connectors", "connector"), UTILITIES("utilities", "util"), + SOLUTIONS("solutions", "solution"), DEMOS("demos", "demo"); private String option; private String code; diff --git a/marketplace-service/src/main/java/com/axonivy/market/util/XmlReaderUtils.java b/marketplace-service/src/main/java/com/axonivy/market/util/XmlReaderUtils.java index d49802145..33fe8e20e 100644 --- a/marketplace-service/src/main/java/com/axonivy/market/util/XmlReaderUtils.java +++ b/marketplace-service/src/main/java/com/axonivy/market/util/XmlReaderUtils.java @@ -24,7 +24,8 @@ public class XmlReaderUtils { private static final RestTemplate restTemplate = new RestTemplate(); - private XmlReaderUtils() {} + private XmlReaderUtils() { + } public static List readXMLFromUrl(String url) { List versions = new ArrayList<>(); diff --git a/marketplace-service/src/test/java/com/axonivy/market/controller/ProductControllerTest.java b/marketplace-service/src/test/java/com/axonivy/market/controller/ProductControllerTest.java index b0150c159..079526960 100644 --- a/marketplace-service/src/test/java/com/axonivy/market/controller/ProductControllerTest.java +++ b/marketplace-service/src/test/java/com/axonivy/market/controller/ProductControllerTest.java @@ -6,7 +6,6 @@ import com.axonivy.market.enums.SortOption; import com.axonivy.market.enums.TypeOption; import com.axonivy.market.model.MultilingualismValue; -import com.axonivy.market.model.ProductRating; import com.axonivy.market.service.ProductService; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; diff --git a/marketplace-service/src/test/java/com/axonivy/market/controller/ProductDetailsControllerTest.java b/marketplace-service/src/test/java/com/axonivy/market/controller/ProductDetailsControllerTest.java index f188b3f4c..af5c1941c 100644 --- a/marketplace-service/src/test/java/com/axonivy/market/controller/ProductDetailsControllerTest.java +++ b/marketplace-service/src/test/java/com/axonivy/market/controller/ProductDetailsControllerTest.java @@ -1,17 +1,16 @@ package com.axonivy.market.controller; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.mockito.Mockito.times; -import static org.mockito.Mockito.verify; - +import com.axonivy.market.assembler.ProductDetailModelAssembler; +import com.axonivy.market.entity.Product; import com.axonivy.market.model.MavenArtifactVersionModel; import com.axonivy.market.model.MultilingualismValue; +import com.axonivy.market.model.ProductDetailModel; +import com.axonivy.market.service.ProductService; import com.axonivy.market.service.VersionService; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.InjectMocks; - import org.mockito.Mock; import org.mockito.Mockito; import org.mockito.junit.jupiter.MockitoExtension; @@ -21,10 +20,9 @@ import java.util.List; import java.util.Objects; -import com.axonivy.market.assembler.ProductDetailModelAssembler; -import com.axonivy.market.entity.Product; -import com.axonivy.market.model.ProductDetailModel; -import com.axonivy.market.service.ProductService; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; @ExtendWith(MockitoExtension.class) class ProductDetailsControllerTest { @@ -48,8 +46,8 @@ class ProductDetailsControllerTest { void testProductDetails() { Mockito.when(productService.fetchProductDetail(Mockito.anyString())).thenReturn(mockProduct()); Mockito.when(detailModelAssembler.toModel(mockProduct(), null)).thenReturn(createProductMockWithDetails()); - ResponseEntity mockExpectedResult = - new ResponseEntity<>(createProductMockWithDetails(), HttpStatus.OK); + ResponseEntity mockExpectedResult = new ResponseEntity<>(createProductMockWithDetails(), + HttpStatus.OK); ResponseEntity result = productDetailsController.findProductDetails(DOCKER_CONNECTOR_ID); @@ -64,11 +62,11 @@ void testProductDetails() { void testProductDetailsWithVersion() { Mockito.when(productService.fetchProductDetail(Mockito.anyString())).thenReturn(mockProduct()); Mockito.when(detailModelAssembler.toModel(mockProduct(), TAG)).thenReturn(createProductMockWithDetails()); - ResponseEntity mockExpectedResult = - new ResponseEntity<>(createProductMockWithDetails(), HttpStatus.OK); + ResponseEntity mockExpectedResult = new ResponseEntity<>(createProductMockWithDetails(), + HttpStatus.OK); - ResponseEntity result = - productDetailsController.findProductDetailsByVersion(DOCKER_CONNECTOR_ID, TAG); + ResponseEntity result = productDetailsController.findProductDetailsByVersion( + DOCKER_CONNECTOR_ID, TAG); assertEquals(HttpStatus.OK, result.getStatusCode()); assertEquals(result, mockExpectedResult); @@ -80,10 +78,10 @@ void testProductDetailsWithVersion() { void testFindProductVersionsById() { List models = List.of(new MavenArtifactVersionModel()); Mockito.when( - versionService.getArtifactsAndVersionToDisplay(Mockito.anyString(), Mockito.anyBoolean(), Mockito.anyString())) + versionService.getArtifactsAndVersionToDisplay(Mockito.anyString(), Mockito.anyBoolean(), Mockito.anyString())) .thenReturn(models); - ResponseEntity> result = - productDetailsController.findProductVersionsById("protal", true, "10.0.1"); + ResponseEntity> result = productDetailsController.findProductVersionsById("protal", + true, "10.0.1"); Assertions.assertEquals(HttpStatus.OK, result.getStatusCode()); Assertions.assertEquals(1, Objects.requireNonNull(result.getBody()).size()); Assertions.assertEquals(models, result.getBody()); diff --git a/marketplace-service/src/test/java/com/axonivy/market/factory/ProductFactoryTest.java b/marketplace-service/src/test/java/com/axonivy/market/factory/ProductFactoryTest.java index dd770b752..2446c18c1 100644 --- a/marketplace-service/src/test/java/com/axonivy/market/factory/ProductFactoryTest.java +++ b/marketplace-service/src/test/java/com/axonivy/market/factory/ProductFactoryTest.java @@ -1,15 +1,7 @@ package com.axonivy.market.factory; -import static com.axonivy.market.constants.CommonConstants.SLASH; -import static com.axonivy.market.constants.MetaConstants.META_FILE; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertNotEquals; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; - -import java.io.IOException; -import java.io.InputStream; - +import com.axonivy.market.constants.CommonConstants; +import com.axonivy.market.entity.Product; import com.axonivy.market.github.model.Meta; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; @@ -17,13 +9,19 @@ import org.kohsuke.github.GHContent; import org.mockito.junit.jupiter.MockitoExtension; -import com.axonivy.market.constants.CommonConstants; -import com.axonivy.market.entity.Product; +import java.io.IOException; +import java.io.InputStream; + +import static com.axonivy.market.constants.CommonConstants.SLASH; +import static com.axonivy.market.constants.MetaConstants.META_FILE; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotEquals; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; @ExtendWith(MockitoExtension.class) class ProductFactoryTest { - private static final String DUMMY_LOGO_URL = - "https://raw.githubusercontent.com/axonivy-market/market/master/market/connector/amazon-comprehend-connector/logo.png"; + private static final String DUMMY_LOGO_URL = "https://raw.githubusercontent.com/axonivy-market/market/master/market/connector/amazon-comprehend-connector/logo.png"; @Test void testMappingByGHContent() throws IOException { diff --git a/marketplace-service/src/test/java/com/axonivy/market/service/FeedbackServiceImplTest.java b/marketplace-service/src/test/java/com/axonivy/market/service/FeedbackServiceImplTest.java index 77b7f14e3..ec24a206a 100644 --- a/marketplace-service/src/test/java/com/axonivy/market/service/FeedbackServiceImplTest.java +++ b/marketplace-service/src/test/java/com/axonivy/market/service/FeedbackServiceImplTest.java @@ -21,7 +21,8 @@ import java.util.List; import java.util.Optional; -import static org.junit.jupiter.api.Assertions.*; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.*; @@ -102,7 +103,8 @@ void testFindFeedbackByUserIdAndProductId_UserNotFound() { when(userRepository.findById(nonExistingUserId)).thenReturn(Optional.empty()); // Test and verify exception - assertThrows(NotFoundException.class, () -> feedbackService.findFeedbackByUserIdAndProductId(nonExistingUserId, productId)); + assertThrows(NotFoundException.class, + () -> feedbackService.findFeedbackByUserIdAndProductId(nonExistingUserId, productId)); // Verify interactions verify(userRepository, times(1)).findById(nonExistingUserId); @@ -127,7 +129,8 @@ void testUpsertFeedback_NewFeedback() throws NotFoundException { User u = new User(); u.setId(newFeedbackModel.getUserId()); when(userRepository.findById(newFeedbackModel.getUserId())).thenReturn(Optional.of(u)); - when(feedbackRepository.findByUserIdAndProductId(newFeedbackModel.getUserId(), newFeedbackModel.getProductId())).thenReturn(null); + when(feedbackRepository.findByUserIdAndProductId(newFeedbackModel.getUserId(), + newFeedbackModel.getProductId())).thenReturn(null); when(feedbackRepository.save(any(Feedback.class))).thenReturn(newFeedback); // Test method @@ -157,7 +160,8 @@ void testUpsertFeedback_UpdateFeedback() throws NotFoundException { User u = new User(); u.setId(existingFeedback.getUserId()); when(userRepository.findById(existingFeedback.getUserId())).thenReturn(Optional.of(u)); - when(feedbackRepository.findByUserIdAndProductId(existingFeedback.getUserId(), existingFeedback.getProductId())).thenReturn(existingFeedback); + when(feedbackRepository.findByUserIdAndProductId(existingFeedback.getUserId(), + existingFeedback.getProductId())).thenReturn(existingFeedback); when(feedbackRepository.save(existingFeedback)).thenReturn(existingFeedback); // Test method @@ -175,7 +179,8 @@ void testUpsertFeedback_UpdateFeedback() throws NotFoundException { assertEquals(updatedFeedback.getContent(), result.getContent()); assertEquals(updatedFeedback.getRating(), result.getRating()); verify(userRepository, times(1)).findById(existingFeedback.getUserId()); - verify(feedbackRepository, times(1)).findByUserIdAndProductId(existingFeedback.getUserId(), existingFeedback.getProductId()); + verify(feedbackRepository, times(1)).findByUserIdAndProductId(existingFeedback.getUserId(), + existingFeedback.getProductId()); verify(feedbackRepository, times(1)).save(existingFeedback); } diff --git a/marketplace-service/src/test/java/com/axonivy/market/service/GHAxonIvyProductRepoServiceImplTest.java b/marketplace-service/src/test/java/com/axonivy/market/service/GHAxonIvyProductRepoServiceImplTest.java index fa0104d4a..95ffda78c 100644 --- a/marketplace-service/src/test/java/com/axonivy/market/service/GHAxonIvyProductRepoServiceImplTest.java +++ b/marketplace-service/src/test/java/com/axonivy/market/service/GHAxonIvyProductRepoServiceImplTest.java @@ -104,8 +104,8 @@ void testExtractMavenArtifactFromJsonNode() { createListNodeForDataNoteByName(nodeName); MavenArtifact mockArtifact = Mockito.mock(MavenArtifact.class); - Mockito.doReturn(mockArtifact).when(axonivyProductRepoServiceImpl).createArtifactFromJsonNode(childNode, null, - isDependency); + Mockito.doReturn(mockArtifact).when(axonivyProductRepoServiceImpl) + .createArtifactFromJsonNode(childNode, null, isDependency); axonivyProductRepoServiceImpl.extractMavenArtifactFromJsonNode(dataNode, isDependency, artifacts); @@ -116,8 +116,8 @@ void testExtractMavenArtifactFromJsonNode() { nodeName = ProductJsonConstants.PROJECTS; createListNodeForDataNoteByName(nodeName); - Mockito.doReturn(mockArtifact).when(axonivyProductRepoServiceImpl).createArtifactFromJsonNode(childNode, null, - isDependency); + Mockito.doReturn(mockArtifact).when(axonivyProductRepoServiceImpl) + .createArtifactFromJsonNode(childNode, null, isDependency); axonivyProductRepoServiceImpl.extractMavenArtifactFromJsonNode(dataNode, isDependency, artifacts); @@ -190,8 +190,7 @@ void testGetOrganization() throws IOException { @Test void testGetReadmeAndProductContentsFromTag() throws IOException { - String readmeContentWithImage = - "#Product-name\n Test README\n## Demo\nDemo content\n## Setup\nSetup content (image.png)"; + String readmeContentWithImage = "#Product-name\n Test README\n## Demo\nDemo content\n## Setup\nSetup content (image.png)"; GHContent mockContent = createMockProductFolderWithProductJson(); @@ -213,8 +212,7 @@ void testGetReadmeAndProductContentsFromTag() throws IOException { @Test void testGetReadmeAndProductContentFromTag_ImageFromFolder() throws IOException { - String readmeContentWithImageFolder = - "#Product-name\n Test README\n## Demo\nDemo content\n## Setup\nSetup content (./images/image.png)"; + String readmeContentWithImageFolder = "#Product-name\n Test README\n## Demo\nDemo content\n## Setup\nSetup content (./images/image.png)"; GHContent mockImageFile = mock(GHContent.class); when(mockImageFile.getName()).thenReturn(ReadmeConstants.IMAGES, IMAGE_NAME); @@ -324,9 +322,10 @@ private static InputStream getMockInputStream() { } private static InputStream getMockInputStreamWithOutProjectAndDependency() { - String jsonContent = "{\n" + " \"installers\": [\n" + " {\n" + " \"data\": {\n" - + " \"repositories\": [\n" + " {\n" + " \"url\": \"http://example.com/repo\"\n" - + " }\n" + " ]\n" + " }\n" + " }\n" + " ]\n" + "}"; + String jsonContent = + "{\n" + " \"installers\": [\n" + " {\n" + " \"data\": {\n" + " \"repositories\": [\n" + + " {\n" + " \"url\": \"http://example.com/repo\"\n" + " }\n" + " ]\n" + + " }\n" + " }\n" + " ]\n" + "}"; return new ByteArrayInputStream(jsonContent.getBytes(StandardCharsets.UTF_8)); } @@ -357,10 +356,10 @@ private GHContent createMockProductFolderWithProductJson() throws IOException { GHContent mockContent2 = createMockProductJson(); - when(ghRepository.getDirectoryContent(CommonConstants.SLASH, RELEASE_TAG)) - .thenReturn(List.of(mockContent, mockContent2)); - when(ghRepository.getDirectoryContent(DOCUWARE_CONNECTOR_PRODUCT, RELEASE_TAG)) - .thenReturn(List.of(mockContent, mockContent2)); + when(ghRepository.getDirectoryContent(CommonConstants.SLASH, RELEASE_TAG)).thenReturn( + List.of(mockContent, mockContent2)); + when(ghRepository.getDirectoryContent(DOCUWARE_CONNECTOR_PRODUCT, RELEASE_TAG)).thenReturn( + List.of(mockContent, mockContent2)); return mockContent; } diff --git a/marketplace-service/src/test/java/com/axonivy/market/service/JwtServiceImplTest.java b/marketplace-service/src/test/java/com/axonivy/market/service/JwtServiceImplTest.java index 4eb8ffcec..405dd2df8 100644 --- a/marketplace-service/src/test/java/com/axonivy/market/service/JwtServiceImplTest.java +++ b/marketplace-service/src/test/java/com/axonivy/market/service/JwtServiceImplTest.java @@ -16,79 +16,79 @@ @ExtendWith(MockitoExtension.class) class JwtServiceImplTest { - private static final String SECRET = "mySecret"; - private static final long EXPIRATION = 7L; // 7 days - - @InjectMocks - private JwtServiceImpl jwtService; - - @BeforeEach - void setUp() { - ReflectionTestUtils.setField(jwtService, "secret", SECRET); - ReflectionTestUtils.setField(jwtService, "expiration", EXPIRATION); - } - - @Test - void testGenerateToken() { - User user = new User(); - user.setId("123"); - user.setName("John Doe"); - user.setUsername("johndoe"); - - String token = jwtService.generateToken(user); - - assertNotNull(token); - assertFalse(token.isEmpty()); - - Claims claims = jwtService.getClaimsFromToken(token); - assertEquals("123", claims.getSubject()); - assertEquals("John Doe", claims.get("name")); - assertEquals("johndoe", claims.get("username")); - } - - @Test - void testValidateToken() { - User user = new User(); - user.setId("123"); - user.setName("John Doe"); - user.setUsername("johndoe"); - - String validToken = jwtService.generateToken(user); - assertTrue(jwtService.validateToken(validToken)); - - String invalidToken = "invalid.token.here"; - assertFalse(jwtService.validateToken(invalidToken)); - } - - @Test - void testGetClaimsFromToken() { - User user = new User(); - user.setId("123"); - user.setName("John Doe"); - user.setUsername("johndoe"); - - String token = jwtService.generateToken(user); - - Claims claims = jwtService.getClaimsFromToken(token); - assertNotNull(claims); - assertEquals("123", claims.getSubject()); - assertEquals("John Doe", claims.get("name")); - assertEquals("johndoe", claims.get("username")); - } - - @Test - void testGetClaimsJws() { - User user = new User(); - user.setId("123"); - user.setName("John Doe"); - user.setUsername("johndoe"); - - String token = jwtService.generateToken(user); - - Jws claimsJws = jwtService.getClaimsJws(token); - assertNotNull(claimsJws); - assertNotNull(claimsJws.getBody()); - assertEquals("123", claimsJws.getBody().getSubject()); - } + private static final String SECRET = "mySecret"; + private static final long EXPIRATION = 7L; // 7 days + + @InjectMocks + private JwtServiceImpl jwtService; + + @BeforeEach + void setUp() { + ReflectionTestUtils.setField(jwtService, "secret", SECRET); + ReflectionTestUtils.setField(jwtService, "expiration", EXPIRATION); + } + + @Test + void testGenerateToken() { + User user = new User(); + user.setId("123"); + user.setName("John Doe"); + user.setUsername("johndoe"); + + String token = jwtService.generateToken(user); + + assertNotNull(token); + assertFalse(token.isEmpty()); + + Claims claims = jwtService.getClaimsFromToken(token); + assertEquals("123", claims.getSubject()); + assertEquals("John Doe", claims.get("name")); + assertEquals("johndoe", claims.get("username")); + } + + @Test + void testValidateToken() { + User user = new User(); + user.setId("123"); + user.setName("John Doe"); + user.setUsername("johndoe"); + + String validToken = jwtService.generateToken(user); + assertTrue(jwtService.validateToken(validToken)); + + String invalidToken = "invalid.token.here"; + assertFalse(jwtService.validateToken(invalidToken)); + } + + @Test + void testGetClaimsFromToken() { + User user = new User(); + user.setId("123"); + user.setName("John Doe"); + user.setUsername("johndoe"); + + String token = jwtService.generateToken(user); + + Claims claims = jwtService.getClaimsFromToken(token); + assertNotNull(claims); + assertEquals("123", claims.getSubject()); + assertEquals("John Doe", claims.get("name")); + assertEquals("johndoe", claims.get("username")); + } + + @Test + void testGetClaimsJws() { + User user = new User(); + user.setId("123"); + user.setName("John Doe"); + user.setUsername("johndoe"); + + String token = jwtService.generateToken(user); + + Jws claimsJws = jwtService.getClaimsJws(token); + assertNotNull(claimsJws); + assertNotNull(claimsJws.getBody()); + assertEquals("123", claimsJws.getBody().getSubject()); + } } diff --git a/marketplace-service/src/test/java/com/axonivy/market/service/ProductServiceImplTest.java b/marketplace-service/src/test/java/com/axonivy/market/service/ProductServiceImplTest.java index 9fd472991..8a8ffffbc 100644 --- a/marketplace-service/src/test/java/com/axonivy/market/service/ProductServiceImplTest.java +++ b/marketplace-service/src/test/java/com/axonivy/market/service/ProductServiceImplTest.java @@ -1,52 +1,45 @@ package com.axonivy.market.service; -import static com.axonivy.market.constants.CommonConstants.LOGO_FILE; -import static com.axonivy.market.constants.MetaConstants.META_FILE; -import static com.axonivy.market.constants.CommonConstants.SLASH; -import static org.assertj.core.api.Assertions.assertThat; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertTrue; -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.ArgumentMatchers.anyLong; -import static org.mockito.ArgumentMatchers.anyString; -import static org.mockito.Mockito.*; - -import java.io.IOException; -import java.io.InputStream; -import java.util.*; -import java.util.stream.Collectors; - -import com.axonivy.market.entity.ProductModuleContent; -import com.axonivy.market.github.service.GHAxonIvyProductRepoService; -import com.axonivy.market.model.MultilingualismValue; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.ExtendWith; -import org.kohsuke.github.*; -import org.mockito.ArgumentCaptor; -import org.mockito.Captor; -import org.mockito.InjectMocks; -import org.mockito.Mock; -import org.mockito.junit.jupiter.MockitoExtension; -import org.springframework.data.domain.Page; -import org.springframework.data.domain.PageImpl; -import org.springframework.data.domain.PageRequest; -import org.springframework.data.domain.Pageable; -import org.springframework.data.domain.Sort; - import com.axonivy.market.constants.GitHubConstants; import com.axonivy.market.entity.GitHubRepoMeta; import com.axonivy.market.entity.Product; +import com.axonivy.market.entity.ProductModuleContent; import com.axonivy.market.enums.FileStatus; import com.axonivy.market.enums.FileType; import com.axonivy.market.enums.SortOption; import com.axonivy.market.enums.TypeOption; import com.axonivy.market.github.model.GitHubFile; import com.axonivy.market.github.service.GHAxonIvyMarketRepoService; +import com.axonivy.market.github.service.GHAxonIvyProductRepoService; import com.axonivy.market.github.service.GitHubService; +import com.axonivy.market.model.MultilingualismValue; import com.axonivy.market.repository.GitHubRepoMetaRepository; import com.axonivy.market.repository.ProductRepository; import com.axonivy.market.service.impl.ProductServiceImpl; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.kohsuke.github.*; +import org.mockito.ArgumentCaptor; +import org.mockito.Captor; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; +import org.springframework.data.domain.*; + +import java.io.IOException; +import java.io.InputStream; +import java.util.*; +import java.util.stream.Collectors; + +import static com.axonivy.market.constants.CommonConstants.LOGO_FILE; +import static com.axonivy.market.constants.CommonConstants.SLASH; +import static com.axonivy.market.constants.MetaConstants.META_FILE; +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.ArgumentMatchers.*; +import static org.mockito.Mockito.*; @ExtendWith(MockitoExtension.class) class ProductServiceImplTest { @@ -54,8 +47,8 @@ class ProductServiceImplTest { private static final String SAMPLE_PRODUCT_ID = "amazon-comprehend"; private static final String SAMPLE_PRODUCT_NAME = "Amazon Comprehend"; private static final long LAST_CHANGE_TIME = 1718096290000l; - private static final Pageable PAGEABLE = - PageRequest.of(0, 20, Sort.by(SortOption.ALPHABETICALLY.getOption()).descending()); + private static final Pageable PAGEABLE = PageRequest.of(0, 20, + Sort.by(SortOption.ALPHABETICALLY.getOption()).descending()); private static final String SHA1_SAMPLE = "35baa89091b2452b77705da227f1a964ecabc6c8"; public static final String RELEASE_TAG = "v10.0.2"; private String keyword; @@ -185,9 +178,10 @@ void testFindAllProductsWithKeyword() throws IOException { verify(productRepository).findAll(any(Pageable.class)); // Test has keyword - when(productRepository.searchByNameOrShortDescriptionRegex(any(), any(), any(Pageable.class))) - .thenReturn(new PageImpl<>(mockResultReturn.stream() - .filter(product -> product.getNames().getEn().equals(SAMPLE_PRODUCT_NAME)).collect(Collectors.toList()))); + when(productRepository.searchByNameOrShortDescriptionRegex(any(), any(), any(Pageable.class))).thenReturn( + new PageImpl<>( + mockResultReturn.stream().filter(product -> product.getNames().getEn().equals(SAMPLE_PRODUCT_NAME)) + .collect(Collectors.toList()))); // Executes result = productService.findProducts(TypeOption.ALL.getOption(), SAMPLE_PRODUCT_NAME, langague, PAGEABLE); verify(productRepository).findAll(any(Pageable.class)); @@ -195,14 +189,10 @@ void testFindAllProductsWithKeyword() throws IOException { assertEquals(SAMPLE_PRODUCT_NAME, result.getContent().get(0).getNames().getEn()); // Test has keyword and type is connector - when( - productRepository.searchByKeywordAndType(any(), any(), any(), any(Pageable.class))) - .thenReturn( - new PageImpl<>( - mockResultReturn.stream() - .filter(product -> product.getNames().getEn().equals(SAMPLE_PRODUCT_NAME) - && product.getType().equals(TypeOption.CONNECTORS.getCode())) - .collect(Collectors.toList()))); + when(productRepository.searchByKeywordAndType(any(), any(), any(), any(Pageable.class))).thenReturn(new PageImpl<>( + mockResultReturn.stream().filter( + product -> product.getNames().getEn().equals(SAMPLE_PRODUCT_NAME) && product.getType() + .equals(TypeOption.CONNECTORS.getCode())).collect(Collectors.toList()))); // Executes result = productService.findProducts(TypeOption.CONNECTORS.getOption(), SAMPLE_PRODUCT_NAME, langague, PAGEABLE); assertTrue(result.hasContent()); @@ -214,8 +204,8 @@ void testSyncProductsFirstTime() throws IOException { var mockCommit = mockGHCommitHasSHA1(SHA1_SAMPLE); when(marketRepoService.getLastCommit(anyLong())).thenReturn(mockCommit); when(repoMetaRepository.findByRepoName(anyString())).thenReturn(null); - when(ghAxonIvyProductRepoService.getReadmeAndProductContentsFromTag(any(), any(), anyString())) - .thenReturn(mockReadmeProductContent()); + when(ghAxonIvyProductRepoService.getReadmeAndProductContentsFromTag(any(), any(), anyString())).thenReturn( + mockReadmeProductContent()); when(gitHubService.getRepository(any())).thenReturn(ghRepository); PagedIterable pagedIterable = mock(PagedIterable.class); when(ghRepository.listTags()).thenReturn(pagedIterable); @@ -265,8 +255,8 @@ void testSearchProducts() { String type = TypeOption.ALL.getOption(); keyword = "on"; langague = "en"; - when(productRepository.searchByNameOrShortDescriptionRegex(keyword, langague, simplePageable)) - .thenReturn(mockResultReturn); + when(productRepository.searchByNameOrShortDescriptionRegex(keyword, langague, simplePageable)).thenReturn( + mockResultReturn); var result = productService.findProducts(type, keyword, langague, simplePageable); assertEquals(result, mockResultReturn); diff --git a/marketplace-service/src/test/resources/meta.json b/marketplace-service/src/test/resources/meta.json index d46c28424..6f23b2898 100644 --- a/marketplace-service/src/test/resources/meta.json +++ b/marketplace-service/src/test/resources/meta.json @@ -3,21 +3,21 @@ "id": "amazon-comprehend", "names": [ { - "locale":"en", + "locale": "en", "value": "Amazon Comprehend" }, { - "locale":"de", + "locale": "de", "value": "Amazon Comprehend DE" } ], "descriptions": [ { - "locale":"en", + "locale": "en", "value": "Amazon Comprehend is a AI service that uses machine learning to uncover information in unstructured data." }, { - "locale":"de", + "locale": "de", "value": "Amazon Comprehend is a AI service that uses machine learning to uncover information in unstructured data. DE" } ],