Skip to content

Commit

Permalink
MARP-1573 fix issues of SonarQube (#249)
Browse files Browse the repository at this point in the history
  • Loading branch information
phhung-axonivy authored Dec 4, 2024
1 parent b25113d commit 8d7a4a5
Show file tree
Hide file tree
Showing 15 changed files with 284 additions and 144 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -112,34 +112,6 @@ public ResponseEntity<Message> syncProducts(@RequestHeader(value = AUTHORIZATION
return new ResponseEntity<>(message, HttpStatus.OK);
}

/**
* @deprecated
*/
@Deprecated(forRemoval = true , since = "1.6.0")
@PutMapping(SYNC_PRODUCT_VERSION)
@Operation(hidden = true)
public ResponseEntity<Message> syncProductVersions(@RequestHeader(value = AUTHORIZATION) String authorizationHeader
,@RequestParam(value = RESET_SYNC, required = false) Boolean resetSync) {
String token = AuthorizationUtils.getBearerToken(authorizationHeader);
gitHubService.validateUserInOrganizationAndTeam(token, GitHubConstants.AXONIVY_MARKET_ORGANIZATION_NAME,
GitHubConstants.AXONIVY_MARKET_TEAM_NAME);
if (Boolean.TRUE.equals(resetSync)) {
productService.clearAllProductVersion();
}
int nonSyncResult = metadataService.syncAllProductsMetadata();
var message = new Message();
HttpStatus statusCode = HttpStatus.OK;
if(nonSyncResult == 1) {
message.setHelpCode(ErrorCode.SUCCESSFUL.getCode());
message.setHelpText(ErrorCode.SUCCESSFUL.getHelpText());
} else {
statusCode = HttpStatus.INTERNAL_SERVER_ERROR;
message.setHelpCode(ErrorCode.MAVEN_VERSION_SYNC_FAILED.getCode());
message.setMessageDetails(ErrorCode.MAVEN_VERSION_SYNC_FAILED.getHelpText());
}
return new ResponseEntity<>(message, statusCode);
}

@PutMapping(SYNC_ONE_PRODUCT_BY_ID)
@Operation(hidden = true)
public ResponseEntity<Message> syncOneProduct(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,16 +68,7 @@ public class Product implements Serializable {
@Transient
private ProductModuleContent productModuleContent;
private List<Artifact> artifacts;
/**
* @deprecated
*/
@Deprecated(forRemoval = true, since = "1.6.0")
private Boolean synchronizedInstallationCount;
/**
* @deprecated
*/
@Deprecated(forRemoval = true, since = "1.6.0")
private Integer customOrder;
private List<String> releasedVersions;
@Transient
private String metaProductJsonUrl;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,6 @@ public class ProductJsonContent {
@JsonIgnore
private String id;
private String version;
/**
* @deprecated
*/
@Deprecated(forRemoval = true, since = "1.5.0")
private String relatedTag;
private String productId;
private String name;
private String content;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import java.io.Serializable;
import java.util.Date;
import java.util.Map;
import java.util.Set;

import static com.axonivy.market.constants.EntityConstants.PRODUCT_MODULE_CONTENT;

Expand All @@ -31,18 +30,6 @@ public class ProductModuleContent implements Serializable {
private static final long serialVersionUID = 1L;
@Schema(description = "product Id (from meta.json)", example = "portal")
private String productId;
/**
* @deprecated
*/
@Deprecated(forRemoval = true, since = "1.5.0")
@Schema(description = "Target release tag", example = "v10.0.25")
private String tag;
/**
* @deprecated
*/
@Deprecated(forRemoval = true, since = "1.5.0")
@Schema(description = "Versions in maven", example = "10.0.25-SNAPSHOT")
private Set<String> mavenVersions;
@Schema(description = "Maven version", example = "10.0.25")
private String version;
@Schema(description = "Product detail description content ",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public static String getReadmeFileLocale(String readmeFile) {

// Cover some cases including when demo and setup parts switch positions or
// missing one of them
public static ReadmeContentsModel getExtractedPartsOfReadme( String readmeContents) {
public static ReadmeContentsModel getExtractedPartsOfReadme(String readmeContents) {
String[] parts = readmeContents.split(DEMO_SETUP_TITLE);
int demoIndex = readmeContents.indexOf(ReadmeConstants.DEMO_PART);
int setupIndex = readmeContents.indexOf(ReadmeConstants.SETUP_PART);
Expand All @@ -71,18 +71,20 @@ public static ReadmeContentsModel getExtractedPartsOfReadme( String readmeConten
description = removeFirstLine(parts[0]);
}

if (demoIndex != -1 && setupIndex != -1) {
if (parts.length == 2) {
if (demoIndex != -1) {
demo = parts[1];
} else {
setup = parts[1];
}
} else if (demoIndex != -1 && setupIndex != -1 && parts.length > 2) {
if (demoIndex < setupIndex) {
demo = parts[1];
setup = parts[2];
} else {
setup = parts[1];
demo = parts[2];
}
} else if (demoIndex != -1) {
demo = parts[1];
} else if (setupIndex != -1) {
setup = parts[1];
}

ReadmeContentsModel readmeContentsModel = new ReadmeContentsModel();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ public class BaseSetup {
protected static final String MOCK_METADATA_FILE_PATH = "src/test/resources/metadata.xml";
protected static final String MOCK_SNAPSHOT_METADATA_FILE_PATH = "src/test/resources/snapshotMetadata.xml";
protected static final String MOCK_README_FILE = "src/test/resources/README.md";
protected static final String MOCK_README_DE_FILE = "src/test/resources/README_DE.md";
protected static final String MOCK_README_FILE_NO_DEMO_PART = "src/test/resources/README_NO_DEMO_PART.md";
protected static final String MOCK_README_FILE_NO_SETUP_PART = "src/test/resources/README_NO_SETUP_PART.md";
protected static final String MOCK_README_FILE_SWAP_DEMO_SETUP_PARTS = "src/test/resources/README_SWAP_DEMO_SETUP.md";
protected static final String INVALID_FILE_PATH = "test/file/path";
protected static final String MOCK_MAVEN_URL = "https://maven.axonivy.com/com/axonivy/util/bpmn-statistic/maven" +
"-metadata.xml";
Expand Down Expand Up @@ -136,6 +140,14 @@ protected static String getMockReadmeContent() {
return getContentFromTestResourcePath(MOCK_README_FILE);
}

protected static String getMockReadmeContent(String filePath) {
if (StringUtils.isBlank(filePath)) {
return getMockReadmeContent();
}

return getContentFromTestResourcePath(filePath);
}

protected Artifact getMockArtifact() {
Artifact mockArtifact = new Artifact();
mockArtifact.setIsDependency(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,32 +146,6 @@ void testSyncProductsInvalidToken() {
assertEquals(ErrorCode.GITHUB_USER_UNAUTHORIZED.getHelpText(), exception.getMessage());
}

@Test
void testSyncMavenVersionSuccess() {
var response = productController.syncProductVersions(AUTHORIZATION_HEADER, false);
assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, response.getStatusCode());
assertTrue(response.hasBody());
assertEquals(ErrorCode.MAVEN_VERSION_SYNC_FAILED.getCode(), Objects.requireNonNull(response.getBody()).getHelpCode());
when(metadataService.syncAllProductsMetadata()).thenReturn(1);
response = productController.syncProductVersions(AUTHORIZATION_HEADER, false);
assertEquals(HttpStatus.OK, response.getStatusCode());
assertTrue(response.hasBody());
assertEquals(ErrorCode.SUCCESSFUL.getCode(), Objects.requireNonNull(response.getBody()).getHelpCode());
}


@Test
void testSyncMavenVersionWithInvalidToken() {
doThrow(new UnauthorizedException(ErrorCode.GITHUB_USER_UNAUTHORIZED.getCode(),
ErrorCode.GITHUB_USER_UNAUTHORIZED.getHelpText())).when(gitHubService)
.validateUserInOrganizationAndTeam(any(String.class), any(String.class), any(String.class));

UnauthorizedException exception = assertThrows(UnauthorizedException.class,
() -> productController.syncProductVersions(INVALID_AUTHORIZATION_HEADER, false));

assertEquals(ErrorCode.GITHUB_USER_UNAUTHORIZED.getHelpText(), exception.getMessage());
}

@Test
void testSyncOneProductInvalidProductPath() {
Product product = new Product();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@

import com.axonivy.market.BaseSetup;
import com.axonivy.market.bo.Artifact;
import com.axonivy.market.constants.CommonConstants;
import com.axonivy.market.entity.ProductModuleContent;
import com.axonivy.market.enums.Language;
import com.axonivy.market.model.ReadmeContentsModel;
import org.apache.commons.lang3.StringUtils;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
Expand All @@ -12,7 +15,7 @@
import java.util.List;
import java.util.Map;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.*;

@ExtendWith(MockitoExtension.class)
class ProductContentUtilsTest extends BaseSetup {
Expand Down Expand Up @@ -55,4 +58,111 @@ void testReplaceImageDirWithImageCustomId() {

assertEquals(expectedResult, updatedContents);
}

@Test
void testGetExtractedPartsOfReadme() {
String readmeContents = getMockReadmeContent();
ReadmeContentsModel readmeContentsModel = ProductContentUtils.getExtractedPartsOfReadme(readmeContents);
assertTrue(StringUtils.isNotBlank(readmeContentsModel.getDescription()));
assertTrue(StringUtils.isNotBlank(readmeContentsModel.getDemo()));
assertTrue(StringUtils.isNotBlank(readmeContentsModel.getSetup()));
}

@Test
void testGetExtractedPartsOfReadmeSwapDemoAndSetupParts() {
String readmeContents = getMockReadmeContent(MOCK_README_FILE_SWAP_DEMO_SETUP_PARTS);
ReadmeContentsModel readmeContentsModel = ProductContentUtils.getExtractedPartsOfReadme(readmeContents);
assertTrue(StringUtils.isNotBlank(readmeContentsModel.getDescription()));
assertTrue(StringUtils.isNotBlank(readmeContentsModel.getDemo()));
assertTrue(StringUtils.isNotBlank(readmeContentsModel.getSetup()));
if (StringUtils.isNotBlank(readmeContentsModel.getSetup())) {
assertTrue(readmeContentsModel.getSetup().startsWith("Mattermost Instance"));
}
}

@Test
void testGetExtractedPartsOfReadmeNoDemoPart() {
String readmeContents = getMockReadmeContent(MOCK_README_FILE_NO_DEMO_PART);
ReadmeContentsModel readmeContentsModel = ProductContentUtils.getExtractedPartsOfReadme(readmeContents);
assertTrue(StringUtils.isNotBlank(readmeContentsModel.getDescription()));
assertTrue(StringUtils.isBlank(readmeContentsModel.getDemo()));
assertTrue(StringUtils.isNotBlank(readmeContentsModel.getSetup()));
}

@Test
void testGetExtractedPartsOfReadmeNoSetupPart() {
String readmeContents = getMockReadmeContent(MOCK_README_FILE_NO_SETUP_PART);
ReadmeContentsModel readmeContentsModel = ProductContentUtils.getExtractedPartsOfReadme(readmeContents);
assertTrue(StringUtils.isNotBlank(readmeContentsModel.getDescription()));
assertTrue(StringUtils.isNotBlank(readmeContentsModel.getDemo()));
assertTrue(StringUtils.isBlank(readmeContentsModel.getSetup()));
}

@Test
void testGetExtractedPartsOfReadmeWithOnlyOneDescription() {
ReadmeContentsModel readmeContentsModel = ProductContentUtils.getExtractedPartsOfReadme(SAMPLE_PRODUCT_NAME);
assertEquals(SAMPLE_PRODUCT_NAME, readmeContentsModel.getDescription());
assertTrue(StringUtils.isBlank(readmeContentsModel.getDemo()));
assertTrue(StringUtils.isBlank(readmeContentsModel.getSetup()));
}

@Test
void testGetExtractedPartsOfEmptyReadme() {
ReadmeContentsModel readmeContentsModel = ProductContentUtils.getExtractedPartsOfReadme(StringUtils.EMPTY);
assertTrue(StringUtils.isBlank(readmeContentsModel.getDescription()));
assertTrue(StringUtils.isBlank(readmeContentsModel.getDemo()));
assertTrue(StringUtils.isBlank(readmeContentsModel.getSetup()));
}

@Test
void testHasImageDirectives() {
String readmeContents = getMockReadmeContent();
assertTrue(ProductContentUtils.hasImageDirectives(readmeContents));
assertFalse(ProductContentUtils.hasImageDirectives(StringUtils.EMPTY));
}

@Test
void testInitProductModuleContent() {
ProductModuleContent productModuleContent = ProductContentUtils.initProductModuleContent(SAMPLE_PRODUCT_ID,
MOCK_RELEASED_VERSION);
assertEquals(SAMPLE_PRODUCT_ID, productModuleContent.getProductId());
assertEquals(MOCK_RELEASED_VERSION, productModuleContent.getVersion());
assertEquals(String.format(CommonConstants.ID_WITH_NUMBER_PATTERN, SAMPLE_PRODUCT_ID, MOCK_RELEASED_VERSION),
productModuleContent.getId());
}

@Test
void testInitProductModuleContentWithoutVersion() {
ProductModuleContent productModuleContent = ProductContentUtils.initProductModuleContent(SAMPLE_PRODUCT_ID,
StringUtils.EMPTY);
assertEquals(SAMPLE_PRODUCT_ID, productModuleContent.getProductId());
assertTrue(StringUtils.isBlank(productModuleContent.getVersion()));
assertTrue(StringUtils.isBlank(productModuleContent.getId()));
}

@Test
void testMappingDescriptionSetupDemoAndUpdateProductModuleTabContents() {
String readmeContents = getMockReadmeContent();
ReadmeContentsModel readmeContentsModel = ProductContentUtils.getExtractedPartsOfReadme(readmeContents);
Map<String, Map<String, String>> moduleContents = new HashMap<>();
ProductContentUtils.mappingDescriptionSetupAndDemo(moduleContents, MOCK_README_FILE, readmeContentsModel);
String readmeDEContents = getMockReadmeContent(MOCK_README_DE_FILE);
ReadmeContentsModel readmeDEContentsModel = ProductContentUtils.getExtractedPartsOfReadme(readmeDEContents);
ProductContentUtils.mappingDescriptionSetupAndDemo(moduleContents, MOCK_README_DE_FILE, readmeDEContentsModel);
ProductModuleContent productModuleContent = new ProductModuleContent();
ProductContentUtils.updateProductModuleTabContents(productModuleContent, moduleContents);
assertEquals(3, moduleContents.size());
assertTrue(productModuleContent.getDescription().get(Language.EN.getValue()).startsWith("Axon Ivy"));
assertTrue(productModuleContent.getDescription().get(Language.DE.getValue()).startsWith("Der"));
assertTrue(StringUtils.isNotBlank(productModuleContent.getSetup().get(Language.DE.getValue())));
assertTrue(StringUtils.equals(productModuleContent.getSetup().get(Language.DE.getValue()),
productModuleContent.getSetup().get(Language.EN.getValue())));
assertTrue(StringUtils.equals(productModuleContent.getSetup().get(Language.DE.getValue()),
productModuleContent.getSetup().get(Language.EN.getValue())));
assertTrue(StringUtils.isNotBlank(productModuleContent.getDemo().get(Language.DE.getValue())));
assertTrue(StringUtils.equals(productModuleContent.getDemo().get(Language.DE.getValue()),
productModuleContent.getDemo().get(Language.EN.getValue())));
assertTrue(StringUtils.equals(productModuleContent.getDemo().get(Language.DE.getValue()),
productModuleContent.getDemo().get(Language.EN.getValue())));
}
}
4 changes: 2 additions & 2 deletions marketplace-service/src/test/resources/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ This connector:

![call-slash-command](images/slash-command.png)

### Setup
## Setup

Mattermost Instance

Expand Down Expand Up @@ -60,4 +60,4 @@ Variables:
# This variable is used for getting incoming webhook list per page
incomingWebhookPerPage: 200
```
```
11 changes: 11 additions & 0 deletions marketplace-service/src/test/resources/README_DE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Mattermost Konnektor

Der Mattermost-Konnektor von Axon Ivy integriert Mattermost-Funktionen in deine Axon Ivy Prozessanwendung.

Dieser Konnektor:

- unterstützt dich mit einer Demo-Implementierung, um den Integrationsaufwand zu reduzieren.
- gewährleistet vollen Zugriff auf die APIs von [Mattermost](https://api.mattermost.com/).
- ermöglicht es dir, den Axon Ivy-Prozess durch Eingabe des Slash-Kommandos im Mattermost-Kanal zu starten.
- ermöglicht das Senden von Nachrichten an den Mattermost-Kanal aus der Axon Ivy-Oberfläche.
- benachrichtigt Benutzer im Kanal über neue Axon Ivy-Workflow-Aufgaben.
Loading

0 comments on commit 8d7a4a5

Please sign in to comment.