Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MARP-700 Refactor code #67

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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";
}
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -82,8 +83,8 @@ public ResponseEntity<FeedbackModel> findFeedbackByUserIdAndProductId(@RequestPa
public ResponseEntity<Void> 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
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -57,8 +58,8 @@ public ResponseEntity<PagedModel<ProductModel>> findProducts(@RequestParam(name
public ResponseEntity<Message> 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)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -14,6 +15,7 @@
@Setter
@Getter
public class MavenArtifactModel implements Serializable {
@Serial
private static final long serialVersionUID = 1L;
private String name;
private String downloadUrl;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -21,6 +22,7 @@
@NoArgsConstructor
@Document(MAVEN_ARTIFACT_VERSION)
public class MavenArtifactVersion implements Serializable {
@Serial
private static final long serialVersionUID = -6492612804634492078L;

@Id
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -26,6 +27,7 @@
@Builder
@Document(PRODUCT)
public class Product implements Serializable {
@Serial
private static final long serialVersionUID = -8770801877877277258L;
@Id
private String id;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import lombok.NoArgsConstructor;
import lombok.Setter;

import java.io.Serial;
import java.io.Serializable;
import java.util.Map;

Expand All @@ -13,6 +14,7 @@
@NoArgsConstructor
@AllArgsConstructor
public class ProductModuleContent implements Serializable {
@Serial
private static final long serialVersionUID = 1L;
private String tag;
private Map<String, String> description;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import lombok.NoArgsConstructor;
import lombok.Setter;

import java.io.Serial;
import java.io.Serializable;

@Getter
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
public class GitHubUtils {

private static String pathToProductFolderFromTagContent;
private static String pathToImageFolder;

public static long getGHCommitDate(GHCommit commit) {
long commitTime = 0L;
Expand Down Expand Up @@ -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";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ public interface ProductRepository extends MongoRepository<Product, String> {

Product findByLogoUrl(String logoUrl);

Product findByIdAndType(String id, String type);

Optional<Product> findById(String productId);

@Query("{'marketDirectory': {$regex : ?0, $options: 'i'}}")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand All @@ -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());
}

Expand All @@ -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
Expand All @@ -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
Expand All @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -54,9 +55,6 @@ class ProductControllerTest {
@Mock
private PagedResourcesAssembler<Product> pagedResourcesAssembler;

@Mock
private PagedModel<?> pagedProductModel;

@Mock
private GitHubService gitHubService;

Expand All @@ -71,21 +69,21 @@ void setup() {
@Test
void testFindProductsAsEmpty() {
PageRequest pageable = PageRequest.of(0, 20);
Page<Product> mockProducts = new PageImpl<Product>(List.of(), pageable, 0);
Page<Product> 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
void testFindProducts() {
PageRequest pageable = PageRequest.of(0, 20, Sort.by(Order.by(SortOption.ALPHABETICALLY.getOption())));
Product mockProduct = createProductMock();

Page<Product> mockProducts = new PageImpl<Product>(List.of(mockProduct), pageable, 1);
Page<Product> 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);
Expand All @@ -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,
Expand All @@ -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());
}

Expand All @@ -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"));
}

Expand All @@ -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());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -87,7 +85,7 @@ void testFindProductVersionsById() {
Mockito.when(
versionService.getArtifactsAndVersionToDisplay(Mockito.anyString(), Mockito.anyBoolean(), Mockito.anyString()))
.thenReturn(models);
ResponseEntity<List<MavenArtifactVersionModel>> result = productDetailsController.findProductVersionsById("protal",
ResponseEntity<List<MavenArtifactVersionModel>> result = productDetailsController.findProductVersionsById("portal",
true, "10.0.1");
Assertions.assertEquals(HttpStatus.OK, result.getStatusCode());
Assertions.assertEquals(1, Objects.requireNonNull(result.getBody()).size());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Loading
Loading