diff --git a/marketplace-service/src/main/java/com/axonivy/market/constants/CommonConstants.java b/marketplace-service/src/main/java/com/axonivy/market/constants/CommonConstants.java index 5f6eea10d..9785cc676 100644 --- a/marketplace-service/src/main/java/com/axonivy/market/constants/CommonConstants.java +++ b/marketplace-service/src/main/java/com/axonivy/market/constants/CommonConstants.java @@ -14,4 +14,5 @@ public class CommonConstants { public static final String PLUS = "+"; public static final String DASH_SEPARATOR = "-"; public static final String SPACE_SEPARATOR = " "; + public static final String BEARER = "Bearer"; } 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 8d3aa03a2..4a9bef8ed 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 @@ -12,7 +12,6 @@ public class ProductJsonConstants { public static final String TYPE = "type"; public static final String DEPENDENCIES = "dependencies"; public static final String INSTALLERS = "installers"; - public static final String DEPENDENCY_SUFFIX = "-dependency"; public static final String MAVEN_IMPORT_INSTALLER_ID = "maven-import"; public static final String MAVEN_DROPIN_INSTALLER_ID = "maven-dropins"; public static final String MAVEN_DEPENDENCY_INSTALLER_ID = "maven-dependency"; diff --git a/marketplace-service/src/main/java/com/axonivy/market/controller/FeedbackController.java b/marketplace-service/src/main/java/com/axonivy/market/controller/FeedbackController.java index 22b61b65d..ce7a8e10a 100644 --- a/marketplace-service/src/main/java/com/axonivy/market/controller/FeedbackController.java +++ b/marketplace-service/src/main/java/com/axonivy/market/controller/FeedbackController.java @@ -1,6 +1,7 @@ package com.axonivy.market.controller; import com.axonivy.market.assembler.FeedbackModelAssembler; +import com.axonivy.market.constants.CommonConstants; import com.axonivy.market.entity.Feedback; import com.axonivy.market.model.FeedbackModel; import com.axonivy.market.model.ProductRating; @@ -82,8 +83,8 @@ public ResponseEntity findFeedbackByUserIdAndProductId(@RequestPa public ResponseEntity createFeedback(@RequestBody @Valid FeedbackModel feedback, @RequestHeader(value = "Authorization") String authorizationHeader) { String token = null; - if (authorizationHeader != null && authorizationHeader.startsWith("Bearer ")) { - token = authorizationHeader.substring(7); // Remove "Bearer " prefix + if (authorizationHeader != null && authorizationHeader.startsWith(CommonConstants.BEARER)) { + token = authorizationHeader.substring(CommonConstants.BEARER.length()).trim(); // Remove "Bearer " prefix } // Validate the token diff --git a/marketplace-service/src/main/java/com/axonivy/market/controller/ProductController.java b/marketplace-service/src/main/java/com/axonivy/market/controller/ProductController.java index 7f8b7d5cd..2d4980d08 100644 --- a/marketplace-service/src/main/java/com/axonivy/market/controller/ProductController.java +++ b/marketplace-service/src/main/java/com/axonivy/market/controller/ProductController.java @@ -1,6 +1,7 @@ package com.axonivy.market.controller; import com.axonivy.market.assembler.ProductModelAssembler; +import com.axonivy.market.constants.CommonConstants; import com.axonivy.market.constants.GitHubConstants; import com.axonivy.market.entity.Product; import com.axonivy.market.enums.ErrorCode; @@ -57,8 +58,8 @@ public ResponseEntity> findProducts(@RequestParam(name public ResponseEntity syncProducts(@RequestHeader(value = "Authorization") String authorizationHeader, @RequestParam(value = "resetSync", required = false) Boolean resetSync) { String token = null; - if (authorizationHeader.startsWith("Bearer ")) { - token = authorizationHeader.substring(7); // Remove "Bearer " prefix + if (authorizationHeader.startsWith(CommonConstants.BEARER)) { + token = authorizationHeader.substring(CommonConstants.BEARER.length()).trim(); // Remove "Bearer " prefix } gitHubService.validateUserOrganization(token, GitHubConstants.AXONIVY_MARKET_ORGANIZATION_NAME); if (Boolean.TRUE.equals(resetSync)) { diff --git a/marketplace-service/src/main/java/com/axonivy/market/entity/MavenArtifactModel.java b/marketplace-service/src/main/java/com/axonivy/market/entity/MavenArtifactModel.java index 1c9ad87ec..81a27e864 100644 --- a/marketplace-service/src/main/java/com/axonivy/market/entity/MavenArtifactModel.java +++ b/marketplace-service/src/main/java/com/axonivy/market/entity/MavenArtifactModel.java @@ -6,6 +6,7 @@ import lombok.Setter; import org.springframework.data.annotation.Transient; +import java.io.Serial; import java.io.Serializable; import java.util.Objects; @@ -14,6 +15,7 @@ @Setter @Getter public class MavenArtifactModel implements Serializable { + @Serial private static final long serialVersionUID = 1L; private String name; private String downloadUrl; diff --git a/marketplace-service/src/main/java/com/axonivy/market/entity/MavenArtifactVersion.java b/marketplace-service/src/main/java/com/axonivy/market/entity/MavenArtifactVersion.java index 8f2e87291..e7a6a2e92 100644 --- a/marketplace-service/src/main/java/com/axonivy/market/entity/MavenArtifactVersion.java +++ b/marketplace-service/src/main/java/com/axonivy/market/entity/MavenArtifactVersion.java @@ -7,6 +7,7 @@ import org.springframework.data.annotation.Id; import org.springframework.data.mongodb.core.mapping.Document; +import java.io.Serial; import java.io.Serializable; import java.util.ArrayList; import java.util.HashMap; @@ -21,6 +22,7 @@ @NoArgsConstructor @Document(MAVEN_ARTIFACT_VERSION) public class MavenArtifactVersion implements Serializable { + @Serial private static final long serialVersionUID = -6492612804634492078L; @Id diff --git a/marketplace-service/src/main/java/com/axonivy/market/entity/Product.java b/marketplace-service/src/main/java/com/axonivy/market/entity/Product.java index a760e0e85..c1ec15ded 100644 --- a/marketplace-service/src/main/java/com/axonivy/market/entity/Product.java +++ b/marketplace-service/src/main/java/com/axonivy/market/entity/Product.java @@ -12,6 +12,7 @@ import org.springframework.data.annotation.Id; import org.springframework.data.mongodb.core.mapping.Document; +import java.io.Serial; import java.io.Serializable; import java.util.Date; import java.util.List; @@ -26,6 +27,7 @@ @Builder @Document(PRODUCT) public class Product implements Serializable { + @Serial private static final long serialVersionUID = -8770801877877277258L; @Id private String id; diff --git a/marketplace-service/src/main/java/com/axonivy/market/entity/ProductModuleContent.java b/marketplace-service/src/main/java/com/axonivy/market/entity/ProductModuleContent.java index f8df6160c..bc48a39ad 100644 --- a/marketplace-service/src/main/java/com/axonivy/market/entity/ProductModuleContent.java +++ b/marketplace-service/src/main/java/com/axonivy/market/entity/ProductModuleContent.java @@ -5,6 +5,7 @@ import lombok.NoArgsConstructor; import lombok.Setter; +import java.io.Serial; import java.io.Serializable; import java.util.Map; @@ -13,6 +14,7 @@ @NoArgsConstructor @AllArgsConstructor public class ProductModuleContent implements Serializable { + @Serial private static final long serialVersionUID = 1L; private String tag; private Map description; diff --git a/marketplace-service/src/main/java/com/axonivy/market/exceptions/model/InvalidParamException.java b/marketplace-service/src/main/java/com/axonivy/market/exceptions/model/InvalidParamException.java index 3cecdf03e..42f066651 100644 --- a/marketplace-service/src/main/java/com/axonivy/market/exceptions/model/InvalidParamException.java +++ b/marketplace-service/src/main/java/com/axonivy/market/exceptions/model/InvalidParamException.java @@ -4,9 +4,12 @@ import lombok.Getter; import lombok.Setter; +import java.io.Serial; + @Getter @Setter public class InvalidParamException extends NotFoundException { + @Serial private static final long serialVersionUID = 1L; public InvalidParamException(String code, String message) { diff --git a/marketplace-service/src/main/java/com/axonivy/market/github/model/ArchivedArtifact.java b/marketplace-service/src/main/java/com/axonivy/market/github/model/ArchivedArtifact.java index 0bd450349..557a9e4ee 100644 --- a/marketplace-service/src/main/java/com/axonivy/market/github/model/ArchivedArtifact.java +++ b/marketplace-service/src/main/java/com/axonivy/market/github/model/ArchivedArtifact.java @@ -7,6 +7,7 @@ import lombok.NoArgsConstructor; import lombok.Setter; +import java.io.Serial; import java.io.Serializable; @Getter @@ -16,6 +17,7 @@ @JsonInclude(JsonInclude.Include.NON_EMPTY) @JsonIgnoreProperties(ignoreUnknown = true) public class ArchivedArtifact implements Serializable { + @Serial private static final long serialVersionUID = 1L; private String lastVersion; private String groupId; diff --git a/marketplace-service/src/main/java/com/axonivy/market/github/model/MavenArtifact.java b/marketplace-service/src/main/java/com/axonivy/market/github/model/MavenArtifact.java index 5c7cf3a37..61e1891e1 100644 --- a/marketplace-service/src/main/java/com/axonivy/market/github/model/MavenArtifact.java +++ b/marketplace-service/src/main/java/com/axonivy/market/github/model/MavenArtifact.java @@ -8,6 +8,7 @@ import lombok.Setter; import org.springframework.data.annotation.Transient; +import java.io.Serial; import java.io.Serializable; import java.util.List; @@ -18,6 +19,7 @@ @JsonInclude(JsonInclude.Include.NON_EMPTY) @JsonIgnoreProperties(ignoreUnknown = true) public class MavenArtifact implements Serializable { + @Serial private static final long serialVersionUID = 1L; private String repoUrl; private String name; diff --git a/marketplace-service/src/main/java/com/axonivy/market/github/service/impl/GHAxonIvyProductRepoServiceImpl.java b/marketplace-service/src/main/java/com/axonivy/market/github/service/impl/GHAxonIvyProductRepoServiceImpl.java index 7a2916da3..308d97c80 100644 --- a/marketplace-service/src/main/java/com/axonivy/market/github/service/impl/GHAxonIvyProductRepoServiceImpl.java +++ b/marketplace-service/src/main/java/com/axonivy/market/github/service/impl/GHAxonIvyProductRepoServiceImpl.java @@ -164,7 +164,7 @@ public ProductModuleContent getReadmeAndProductContentsFromTag(Product product, } } } catch (Exception e) { - log.error("Cannot get product.json and README file's content {}", e); + log.error("Cannot get product.json and README file's content {}", e.getMessage()); return null; } return productModuleContent; diff --git a/marketplace-service/src/main/java/com/axonivy/market/github/util/GitHubUtils.java b/marketplace-service/src/main/java/com/axonivy/market/github/util/GitHubUtils.java index 26ffb7260..2e7af413c 100644 --- a/marketplace-service/src/main/java/com/axonivy/market/github/util/GitHubUtils.java +++ b/marketplace-service/src/main/java/com/axonivy/market/github/util/GitHubUtils.java @@ -20,7 +20,6 @@ public class GitHubUtils { private static String pathToProductFolderFromTagContent; - private static String pathToImageFolder; public static long getGHCommitDate(GHCommit commit) { long commitTime = 0L; @@ -111,6 +110,7 @@ public static String getNonStandardProductFilePath(String productId) { } public static String getNonStandardImageFolder(String productId) { + String pathToImageFolder; switch (productId) { case NonStandardProductPackageConstants.EXCEL_IMPORTER: pathToImageFolder = "doc"; diff --git a/marketplace-service/src/main/java/com/axonivy/market/repository/ProductRepository.java b/marketplace-service/src/main/java/com/axonivy/market/repository/ProductRepository.java index b638e30ea..7d72b8257 100644 --- a/marketplace-service/src/main/java/com/axonivy/market/repository/ProductRepository.java +++ b/marketplace-service/src/main/java/com/axonivy/market/repository/ProductRepository.java @@ -16,8 +16,6 @@ public interface ProductRepository extends MongoRepository { Product findByLogoUrl(String logoUrl); - Product findByIdAndType(String id, String type); - Optional findById(String productId); @Query("{'marketDirectory': {$regex : ?0, $options: 'i'}}") diff --git a/marketplace-service/src/test/java/com/axonivy/market/controller/AppControllerTest.java b/marketplace-service/src/test/java/com/axonivy/market/controller/AppControllerTest.java index 055b81d2c..27a62c6b8 100644 --- a/marketplace-service/src/test/java/com/axonivy/market/controller/AppControllerTest.java +++ b/marketplace-service/src/test/java/com/axonivy/market/controller/AppControllerTest.java @@ -6,6 +6,8 @@ import org.springframework.http.HttpStatus; import org.springframework.test.context.junit.jupiter.SpringExtension; +import java.util.Objects; + import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertTrue; @@ -16,10 +18,10 @@ class AppControllerTest { private AppController appController; @Test - void testRoot() throws Exception { + void testRoot() { var response = appController.root(); assertEquals(HttpStatus.OK, response.getStatusCode()); assertTrue(response.hasBody()); - assertTrue(response.getBody().getMessageDetails().contains("/swagger-ui/index.html")); + assertTrue(Objects.requireNonNull(response.getBody()).getMessageDetails().contains("/swagger-ui/index.html")); } } \ No newline at end of file diff --git a/marketplace-service/src/test/java/com/axonivy/market/controller/FeedbackControllerTest.java b/marketplace-service/src/test/java/com/axonivy/market/controller/FeedbackControllerTest.java index ea02160f9..1af06b839 100644 --- a/marketplace-service/src/test/java/com/axonivy/market/controller/FeedbackControllerTest.java +++ b/marketplace-service/src/test/java/com/axonivy/market/controller/FeedbackControllerTest.java @@ -25,6 +25,7 @@ import org.springframework.web.context.request.ServletRequestAttributes; import java.util.List; +import java.util.Objects; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertTrue; @@ -73,7 +74,7 @@ void testFindFeedbacksAsEmpty() { var result = feedbackController.findFeedbacks(PRODUCT_ID_SAMPLE, pageable); assertEquals(HttpStatus.OK, result.getStatusCode()); assertTrue(result.hasBody()); - assertEquals(0, result.getBody().getContent().size()); + assertEquals(0, Objects.requireNonNull(result.getBody()).getContent().size()); } @Test @@ -91,7 +92,7 @@ void testFindFeedbacks() { var result = feedbackController.findFeedbacks(PRODUCT_ID_SAMPLE, pageable); assertEquals(HttpStatus.OK, result.getStatusCode()); assertTrue(result.hasBody()); - assertEquals(1, result.getBody().getContent().size()); + assertEquals(1, Objects.requireNonNull(result.getBody()).getContent().size()); assertEquals(USER_NAME_SAMPLE, result.getBody().getContent().iterator().next().getUsername()); } @@ -104,7 +105,7 @@ void testFindFeedback() { var result = feedbackController.findFeedback(FEEDBACK_ID_SAMPLE); assertEquals(HttpStatus.OK, result.getStatusCode()); assertTrue(result.hasBody()); - assertEquals(USER_NAME_SAMPLE, result.getBody().getUsername()); + assertEquals(USER_NAME_SAMPLE, Objects.requireNonNull(result.getBody()).getUsername()); } @Test @@ -116,7 +117,7 @@ void testFindFeedbackByUserIdAndProductId() { var result = feedbackController.findFeedbackByUserIdAndProductId(USER_ID_SAMPLE, PRODUCT_ID_SAMPLE); assertEquals(HttpStatus.OK, result.getStatusCode()); assertTrue(result.hasBody()); - assertEquals(USER_NAME_SAMPLE, result.getBody().getUsername()); + assertEquals(USER_NAME_SAMPLE, Objects.requireNonNull(result.getBody()).getUsername()); } @Test @@ -132,7 +133,7 @@ void testCreateFeedback() { var result = feedbackController.createFeedback(mockFeedbackModel, "Bearer " + TOKEN_SAMPLE); assertEquals(HttpStatus.CREATED, result.getStatusCode()); - assertTrue(result.getHeaders().getLocation().toString().contains(mockFeedback.getId())); + assertTrue(Objects.requireNonNull(result.getHeaders().getLocation()).toString().contains(mockFeedback.getId())); } private Feedback createFeedbackMock() { diff --git a/marketplace-service/src/test/java/com/axonivy/market/controller/OAuth2ControllerTest.java b/marketplace-service/src/test/java/com/axonivy/market/controller/OAuth2ControllerTest.java index f4a67a939..e53c4fd6d 100644 --- a/marketplace-service/src/test/java/com/axonivy/market/controller/OAuth2ControllerTest.java +++ b/marketplace-service/src/test/java/com/axonivy/market/controller/OAuth2ControllerTest.java @@ -51,7 +51,7 @@ void testGitHubLogin() { ResponseEntity response = oAuth2Controller.gitHubLogin(oauth2AuthorizationCode); - assertEquals(200, response.getStatusCodeValue()); + assertEquals(200, response.getStatusCode().value()); assertEquals(Map.of("token", jwtToken), response.getBody()); } 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 9152a506f..c28c96b6a 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 @@ -28,6 +28,7 @@ import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.Objects; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertThrows; @@ -54,9 +55,6 @@ class ProductControllerTest { @Mock private PagedResourcesAssembler pagedResourcesAssembler; - @Mock - private PagedModel pagedProductModel; - @Mock private GitHubService gitHubService; @@ -71,13 +69,13 @@ void setup() { @Test void testFindProductsAsEmpty() { PageRequest pageable = PageRequest.of(0, 20); - Page mockProducts = new PageImpl(List.of(), pageable, 0); + Page mockProducts = new PageImpl<>(List.of(), pageable, 0); when(service.findProducts(any(), any(), any(), any())).thenReturn(mockProducts); when(pagedResourcesAssembler.toEmptyModel(any(), any())).thenReturn(PagedModel.empty()); var result = productController.findProducts(TypeOption.ALL.getOption(), null, "en", pageable); assertEquals(HttpStatus.OK, result.getStatusCode()); assertTrue(result.hasBody()); - assertEquals(0, result.getBody().getContent().size()); + assertEquals(0, Objects.requireNonNull(result.getBody()).getContent().size()); } @Test @@ -85,7 +83,7 @@ void testFindProducts() { PageRequest pageable = PageRequest.of(0, 20, Sort.by(Order.by(SortOption.ALPHABETICALLY.getOption()))); Product mockProduct = createProductMock(); - Page mockProducts = new PageImpl(List.of(mockProduct), pageable, 1); + Page mockProducts = new PageImpl<>(List.of(mockProduct), pageable, 1); when(service.findProducts(any(), any(), any(), any())).thenReturn(mockProducts); assembler = new ProductModelAssembler(); var mockProductModel = assembler.toModel(mockProduct); @@ -94,7 +92,7 @@ void testFindProducts() { var result = productController.findProducts(TypeOption.ALL.getOption(), "", "en", pageable); assertEquals(HttpStatus.OK, result.getStatusCode()); assertTrue(result.hasBody()); - assertEquals(1, result.getBody().getContent().size()); + assertEquals(1, Objects.requireNonNull(result.getBody()).getContent().size()); assertEquals(PRODUCT_NAME_SAMPLE, result.getBody().getContent().iterator().next().getNames().get(Language.EN.getValue())); assertEquals(PRODUCT_NAME_DE_SAMPLE, @@ -109,7 +107,7 @@ void testSyncProductsSuccess() { assertEquals(HttpStatus.OK, response.getStatusCode()); assertTrue(response.hasBody()); - assertEquals(ErrorCode.SUCCESSFUL.getCode(), response.getBody().getHelpCode()); + assertEquals(ErrorCode.SUCCESSFUL.getCode(), Objects.requireNonNull(response.getBody()).getHelpCode()); assertEquals("Data is already up to date, nothing to sync", response.getBody().getMessageDetails()); } @@ -122,7 +120,7 @@ void testSyncProductsWithResetSuccess() { assertEquals(HttpStatus.OK, response.getStatusCode()); assertTrue(response.hasBody()); - assertEquals(ErrorCode.SUCCESSFUL.getCode(), response.getBody().getHelpCode()); + assertEquals(ErrorCode.SUCCESSFUL.getCode(), Objects.requireNonNull(response.getBody()).getHelpCode()); assertTrue(response.getBody().getMessageDetails().contains("Finished sync data")); } @@ -132,9 +130,8 @@ void testSyncProductsInvalidToken() { ErrorCode.GITHUB_USER_UNAUTHORIZED.getHelpText())).when(gitHubService) .validateUserOrganization(any(String.class), any(String.class)); - UnauthorizedException exception = assertThrows(UnauthorizedException.class, () -> { - productController.syncProducts(INVALID_AUTHORIZATION_HEADER, false); - }); + UnauthorizedException exception = assertThrows(UnauthorizedException.class, + () -> productController.syncProducts(INVALID_AUTHORIZATION_HEADER, false)); assertEquals(ErrorCode.GITHUB_USER_UNAUTHORIZED.getHelpText(), exception.getMessage()); } 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 e1163587e..3d3a2af8a 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 @@ -9,9 +9,7 @@ import java.util.Objects; import java.util.HashMap; -import java.util.List; import java.util.Map; -import java.util.Objects; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; @@ -87,7 +85,7 @@ void testFindProductVersionsById() { Mockito.when( versionService.getArtifactsAndVersionToDisplay(Mockito.anyString(), Mockito.anyBoolean(), Mockito.anyString())) .thenReturn(models); - ResponseEntity> result = productDetailsController.findProductVersionsById("protal", + ResponseEntity> result = productDetailsController.findProductVersionsById("portal", true, "10.0.1"); Assertions.assertEquals(HttpStatus.OK, result.getStatusCode()); Assertions.assertEquals(1, Objects.requireNonNull(result.getBody()).size()); 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 5b9f3727e..fe7ff227e 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 @@ -19,18 +19,6 @@ import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; -import java.io.IOException; -import java.io.InputStream; - -import com.axonivy.market.github.model.Meta; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.ExtendWith; -import org.kohsuke.github.GHContent; -import org.mockito.junit.jupiter.MockitoExtension; - -import com.axonivy.market.constants.CommonConstants; -import com.axonivy.market.entity.Product; import com.axonivy.market.enums.Language; @ExtendWith(MockitoExtension.class) 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 1f70286ae..75f97b562 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 @@ -26,8 +26,14 @@ import java.util.List; import java.util.Optional; -import static org.junit.jupiter.api.Assertions.*; -import static org.mockito.Mockito.*; +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.mockito.Mockito.any; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; @ExtendWith(MockitoExtension.class) class FeedbackServiceImplTest { @@ -86,9 +92,8 @@ void testFindFeedbacks_ProductNotFound() { when(productRepository.findById(productId)).thenReturn(Optional.empty()); - NotFoundException exception = assertThrows(NotFoundException.class, () -> { - feedbackService.findFeedbacks(productId, pageable); - }); + NotFoundException exception = assertThrows(NotFoundException.class, + () -> feedbackService.findFeedbacks(productId, pageable)); assertEquals(ErrorCode.PRODUCT_NOT_FOUND.getCode(), exception.getCode()); verify(productRepository, times(1)).findById(productId); verify(feedbackRepository, times(0)).searchByProductId(productId, pageable); @@ -112,9 +117,7 @@ void testFindFeedback_NotFound() { when(feedbackRepository.findById(feedbackId)).thenReturn(Optional.empty()); - NotFoundException exception = assertThrows(NotFoundException.class, () -> { - feedbackService.findFeedback(feedbackId); - }); + NotFoundException exception = assertThrows(NotFoundException.class, () -> feedbackService.findFeedback(feedbackId)); assertEquals(ErrorCode.FEEDBACK_NOT_FOUND.getCode(), exception.getCode()); verify(feedbackRepository, times(1)).findById(feedbackId); } @@ -146,9 +149,8 @@ void testFindFeedbackByUserIdAndProductId_NotFound() { when(productRepository.findById(productId)).thenReturn(Optional.of(new Product())); when(feedbackRepository.findByUserIdAndProductId(userId, productId)).thenReturn(null); - NotFoundException exception = assertThrows(NotFoundException.class, () -> { - feedbackService.findFeedbackByUserIdAndProductId(userId, productId); - }); + NotFoundException exception = assertThrows(NotFoundException.class, + () -> feedbackService.findFeedbackByUserIdAndProductId(userId, productId)); assertEquals(ErrorCode.FEEDBACK_NOT_FOUND.getCode(), exception.getCode()); verify(userRepository, times(1)).findById(userId); verify(productRepository, times(1)).findById(productId); @@ -210,6 +212,7 @@ void testGetProductRatingById() { verify(feedbackRepository, times(1)).findByProductId(productId); } + @Test void testGetProductRatingById_NoFeedbacks() { String productId = "product1"; @@ -241,9 +244,8 @@ void testValidateProductExists_NotFound() { when(productRepository.findById(productId)).thenReturn(Optional.empty()); - NotFoundException exception = assertThrows(NotFoundException.class, () -> { - feedbackService.validateProductExists(productId); - }); + NotFoundException exception = assertThrows(NotFoundException.class, + () -> feedbackService.validateProductExists(productId)); assertEquals(ErrorCode.PRODUCT_NOT_FOUND.getCode(), exception.getCode()); verify(productRepository, times(1)).findById(productId); } @@ -264,9 +266,8 @@ void testValidateUserExists_NotFound() { when(userRepository.findById(userId)).thenReturn(Optional.empty()); - NotFoundException exception = assertThrows(NotFoundException.class, () -> { - feedbackService.validateUserExists(userId); - }); + NotFoundException exception = assertThrows(NotFoundException.class, + () -> feedbackService.validateUserExists(userId)); assertEquals(ErrorCode.USER_NOT_FOUND.getCode(), exception.getCode()); verify(userRepository, times(1)).findById(userId); } diff --git a/marketplace-service/src/test/java/com/axonivy/market/service/GHAxonIvyMarketRepoServiceImplTest.java b/marketplace-service/src/test/java/com/axonivy/market/service/GHAxonIvyMarketRepoServiceImplTest.java index 5eda06723..c26a567a2 100644 --- a/marketplace-service/src/test/java/com/axonivy/market/service/GHAxonIvyMarketRepoServiceImplTest.java +++ b/marketplace-service/src/test/java/com/axonivy/market/service/GHAxonIvyMarketRepoServiceImplTest.java @@ -108,7 +108,7 @@ void testFetchMarketItemsBySHA1Range() throws IOException { @Test void testGetLastCommit() throws IOException { - var lastCommit = axonIvyMarketRepoServiceImpl.getLastCommit(0l); + var lastCommit = axonIvyMarketRepoServiceImpl.getLastCommit(0L); assertEquals(null, lastCommit); } } 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 52c01e218..762172f55 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 @@ -80,7 +80,7 @@ void setup() throws IOException { } @AfterEach - void after() throws IOException { + void after() { reset(mockGHOrganization); reset(gitHubService); } @@ -138,7 +138,7 @@ void testExtractMavenArtifactFromJsonNode() { private void createListNodeForDataNoteByName(String nodeName) { JsonNode sectionNode = Mockito.mock(JsonNode.class); - Iterator iterator = Mockito.mock(Iterator.class); + Iterator iterator = Mockito.mock(String.valueOf(Iterator.class)); Mockito.when(dataNode.path(nodeName)).thenReturn(sectionNode); Mockito.when(sectionNode.iterator()).thenReturn(iterator); Mockito.when(iterator.hasNext()).thenReturn(true, false); @@ -230,7 +230,7 @@ void testGetReadmeAndProductContentFromTag_ImageFromFolder() throws IOException when(mockImageFile.isDirectory()).thenReturn(true); when(mockImageFile.getDownloadUrl()).thenReturn(IMAGE_DOWNLOAD_URL); - PagedIterable pagedIterable = mock(PagedIterable.class); + PagedIterable pagedIterable = Mockito.mock(String.valueOf(GHContent.class)); when(mockImageFile.listDirectoryContent()).thenReturn(pagedIterable); when(pagedIterable.toList()).thenReturn(List.of(mockImageFile)); @@ -340,7 +340,7 @@ private static InputStream getMockInputStreamWithOutProjectAndDependency() { return new ByteArrayInputStream(jsonContent.getBytes(StandardCharsets.UTF_8)); } - private Product createMockProduct() throws IOException { + private Product createMockProduct() { Product product = new Product(); product.setId("docuware-connector"); product.setLanguage("en"); diff --git a/marketplace-service/src/test/java/com/axonivy/market/service/GitHubServiceImplTest.java b/marketplace-service/src/test/java/com/axonivy/market/service/GitHubServiceImplTest.java index afe25e7ce..b6703cdc7 100644 --- a/marketplace-service/src/test/java/com/axonivy/market/service/GitHubServiceImplTest.java +++ b/marketplace-service/src/test/java/com/axonivy/market/service/GitHubServiceImplTest.java @@ -1,33 +1,21 @@ package com.axonivy.market.service; -import com.axonivy.market.constants.GitHubConstants; -import com.axonivy.market.github.model.GitHubAccessTokenResponse; import com.axonivy.market.github.service.impl.GitHubServiceImpl; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.kohsuke.github.GHContent; import org.kohsuke.github.GHRepository; -import org.kohsuke.github.GitHub; import org.mockito.InjectMocks; import org.mockito.Mock; import org.mockito.MockitoAnnotations; import org.mockito.junit.jupiter.MockitoExtension; import org.springframework.boot.web.client.RestTemplateBuilder; -import org.springframework.http.HttpEntity; -import org.springframework.http.HttpHeaders; -import org.springframework.http.HttpStatus; -import org.springframework.http.MediaType; -import org.springframework.http.ResponseEntity; -import org.springframework.util.LinkedMultiValueMap; -import org.springframework.util.MultiValueMap; import org.springframework.web.client.RestTemplate; import java.io.IOException; -import java.util.Collections; import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.lenient; import static org.mockito.Mockito.mock; 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 1242e442f..4b244e2c2 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 @@ -72,7 +72,7 @@ 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 long LAST_CHANGE_TIME = 1718096290000L; private static final Pageable PAGEABLE = PageRequest.of(0, 20, Sort.by(SortOption.ALPHABETICALLY.getOption()).descending()); private static final String SHA1_SAMPLE = "35baa89091b2452b77705da227f1a964ecabc6c8"; @@ -280,7 +280,7 @@ void testSyncProductsFirstTime() throws IOException { when(ghAxonIvyProductRepoService.getReadmeAndProductContentsFromTag(any(), any(), anyString())).thenReturn( mockReadmeProductContent()); when(gitHubService.getRepository(any())).thenReturn(ghRepository); - PagedIterable pagedIterable = mock(PagedIterable.class); + PagedIterable pagedIterable = Mockito.mock(String.valueOf(GHTag.class)); when(ghRepository.listTags()).thenReturn(pagedIterable); GHTag mockTag = mock(GHTag.class); @@ -341,7 +341,7 @@ void testFetchProductDetail() { String id = "amazon-comprehend"; Product mockProduct = mockResultReturn.getContent().get(0); mockProduct.setSynchronizedInstallationCount(true); - when(productRepository.findById(id)).thenReturn(Optional.ofNullable(mockProduct)); + when(productRepository.findById(id)).thenReturn(Optional.of(mockProduct)); Product result = productService.fetchProductDetail(id); assertEquals(mockProduct, result); verify(productRepository, times(1)).findById(id); diff --git a/marketplace-service/src/test/java/com/axonivy/market/service/SchedulingTasksTest.java b/marketplace-service/src/test/java/com/axonivy/market/service/SchedulingTasksTest.java index aa7b8af7c..5d7571a98 100644 --- a/marketplace-service/src/test/java/com/axonivy/market/service/SchedulingTasksTest.java +++ b/marketplace-service/src/test/java/com/axonivy/market/service/SchedulingTasksTest.java @@ -10,10 +10,9 @@ import static org.mockito.Mockito.atLeast; import static org.mockito.Mockito.verify; -@SpringBootTest(properties = { "MONGODB_USERNAME=user", - "MONGODB_PASSWORD=password", "MONGODB_HOST=mongoHost", "MONGODB_DATABASE=product", - "MARKET_GITHUB_OAUTH_APP_CLIENT_ID=clientId", "MARKET_GITHUB_OAUTH_APP_CLIENT_SECRET=clientSecret", - "MARKET_JWT_SECRET_KEY=jwtSecret" }) +@SpringBootTest(properties = { "MONGODB_USERNAME=user", "MONGODB_PASSWORD=password", "MONGODB_HOST=mongoHost", + "MONGODB_DATABASE=product", "MARKET_GITHUB_OAUTH_APP_CLIENT_ID=clientId", + "MARKET_GITHUB_OAUTH_APP_CLIENT_SECRET=clientSecret", "MARKET_JWT_SECRET_KEY=jwtSecret" }) class SchedulingTasksTest { @SpyBean @@ -21,8 +20,7 @@ class SchedulingTasksTest { @Test void testShouldNotTriggerAfterApplicationStarted() { - Awaitility.await().atMost(Durations.TEN_SECONDS).untilAsserted(() -> { - verify(tasks, atLeast(0)).syncDataForProductFromGitHubRepo(); - }); + Awaitility.await().atMost(Durations.TEN_SECONDS) + .untilAsserted(() -> verify(tasks, atLeast(0)).syncDataForProductFromGitHubRepo()); } } diff --git a/marketplace-service/src/test/java/com/axonivy/market/service/UserServiceImplTest.java b/marketplace-service/src/test/java/com/axonivy/market/service/UserServiceImplTest.java index 457378568..475c4ef6e 100644 --- a/marketplace-service/src/test/java/com/axonivy/market/service/UserServiceImplTest.java +++ b/marketplace-service/src/test/java/com/axonivy/market/service/UserServiceImplTest.java @@ -1,9 +1,10 @@ -package com.axonivy.market.service.impl; +package com.axonivy.market.service; import com.axonivy.market.entity.User; import com.axonivy.market.enums.ErrorCode; import com.axonivy.market.exceptions.model.NotFoundException; import com.axonivy.market.repository.UserRepository; +import com.axonivy.market.service.impl.UserServiceImpl; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; @@ -15,8 +16,13 @@ import java.util.List; import java.util.Optional; -import static org.junit.jupiter.api.Assertions.*; -import static org.mockito.Mockito.*; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.mockito.Mockito.any; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; @ExtendWith(MockitoExtension.class) class UserServiceImplTest { @@ -66,9 +72,7 @@ void testFindUser_NotFound() { when(userRepository.findById(userId)).thenReturn(Optional.empty()); - NotFoundException exception = assertThrows(NotFoundException.class, () -> { - userService.findUser(userId); - }); + NotFoundException exception = assertThrows(NotFoundException.class, () -> userService.findUser(userId)); assertEquals(ErrorCode.USER_NOT_FOUND.getCode(), exception.getCode()); verify(userRepository, times(1)).findById(userId); }