Skip to content

Commit

Permalink
CR comment fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
checkmarx-kobi-hagmi committed Jun 9, 2024
1 parent 101967d commit 9a21b8c
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 30 deletions.
4 changes: 2 additions & 2 deletions src/main/java/com/checkmarx/ast/ScanResult/ScanResult.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.checkmarx.ast.ScanResult;

import com.checkmarx.ast.utils.JsonParser;
import com.checkmarx.ast.wrapper.CxBaseObject;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
Expand All @@ -12,7 +13,6 @@
import lombok.Value;

import java.util.List;
import java.util.Map;

@Value
@EqualsAndHashCode(callSuper = true)
Expand Down Expand Up @@ -45,6 +45,6 @@ public ScanResult(
}

public static <T> T fromLine(String line) {
return parse(line, TypeFactory.defaultInstance().constructType(ScanResult.class));
return JsonParser.parse(line, TypeFactory.defaultInstance().constructType(ScanResult.class));
}
}
5 changes: 3 additions & 2 deletions src/main/java/com/checkmarx/ast/project/Project.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.checkmarx.ast.project;

import com.checkmarx.ast.utils.JsonParser;
import com.checkmarx.ast.wrapper.CxBaseObject;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
Expand Down Expand Up @@ -36,10 +37,10 @@ public Project(@JsonProperty("ID") String id,
}

public static <T> T fromLine(String line) {
return parse(line, TypeFactory.defaultInstance().constructType(Project.class));
return JsonParser.parse(line, TypeFactory.defaultInstance().constructType(Project.class));
}

public static <T> List<T> listFromLine(String line) {
return parse(line, TypeFactory.defaultInstance().constructCollectionType(List.class, Project.class));
return JsonParser.parse(line, TypeFactory.defaultInstance().constructCollectionType(List.class, Project.class));
}
}
5 changes: 3 additions & 2 deletions src/main/java/com/checkmarx/ast/scan/Scan.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.checkmarx.ast.scan;

import com.checkmarx.ast.utils.JsonParser;
import com.checkmarx.ast.wrapper.CxBaseObject;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
Expand Down Expand Up @@ -43,10 +44,10 @@ public Scan(@JsonProperty("ID") String id, @JsonProperty("ProjectID") String pro
}

public static <T> T fromLine(String line) {
return parse(line, TypeFactory.defaultInstance().constructType(Scan.class));
return JsonParser.parse(line, TypeFactory.defaultInstance().constructType(Scan.class));
}

public static <T> List<T> listFromLine(String line) {
return parse(line, TypeFactory.defaultInstance().constructCollectionType(List.class, Scan.class));
return JsonParser.parse(line, TypeFactory.defaultInstance().constructCollectionType(List.class, Scan.class));
}
}
3 changes: 2 additions & 1 deletion src/main/java/com/checkmarx/ast/tenant/TenantSetting.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.checkmarx.ast.tenant;

import com.checkmarx.ast.utils.JsonParser;
import com.checkmarx.ast.wrapper.CxBaseObject;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
Expand Down Expand Up @@ -31,7 +32,7 @@ public TenantSetting(@JsonProperty("key") String key, @JsonProperty("value") Str
}

public static <T> List<T> listFromLine(String line) {
return CxBaseObject.parse(line,
return JsonParser.parse(line,
TypeFactory.defaultInstance()
.constructCollectionType(List.class, TenantSetting.class));
}
Expand Down
32 changes: 32 additions & 0 deletions src/main/java/com/checkmarx/ast/utils/JsonParser.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.checkmarx.ast.utils;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.lang3.StringUtils;

import java.io.IOException;

public class JsonParser {
public static <T> T parse(String line, JavaType type) {
T result = null;
if (!StringUtils.isBlank(line) && isValidJSON(line)) {
try {
result = new ObjectMapper().readValue(line, type);
} catch (JsonProcessingException e) {
e.printStackTrace();
}
}
return result;
}

private static boolean isValidJSON(final String json) {
try {
final ObjectMapper mapper = new ObjectMapper();
mapper.readTree(json);
return true;
} catch (IOException e) {
return false;
}
}
}
22 changes: 0 additions & 22 deletions src/main/java/com/checkmarx/ast/wrapper/CxBaseObject.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,26 +34,4 @@ protected CxBaseObject(@JsonProperty("ID") String id,
this.updatedAt = updatedAt;
this.tags = tags;
}

public static <T> T parse(String line, JavaType type) {
T result = null;
if (!StringUtils.isBlank(line) && isValidJSON(line)) {
try {
result = new ObjectMapper().readValue(line, type);
} catch (JsonProcessingException e) {
e.printStackTrace();
}
}
return result;
}

public static boolean isValidJSON(final String json) {
try {
final ObjectMapper mapper = new ObjectMapper();
mapper.readTree(json);
return true;
} catch (IOException e) {
return false;
}
}
}
3 changes: 2 additions & 1 deletion src/main/java/com/checkmarx/ast/wrapper/CxWrapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import com.checkmarx.ast.results.result.Node;
import com.checkmarx.ast.scan.Scan;
import com.checkmarx.ast.tenant.TenantSetting;
import com.checkmarx.ast.utils.JsonParser;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.type.CollectionType;
import com.fasterxml.jackson.databind.type.TypeFactory;
Expand Down Expand Up @@ -245,7 +246,7 @@ public List<String> projectBranches(@NonNull UUID projectId, String filter)

return Execution.executeCommand(withConfigArguments(arguments),
logger,
line -> CxBaseObject.parse(line, BRANCHES_TYPE));
line -> JsonParser.parse(line, BRANCHES_TYPE));
}

public List<CodeBashing> codeBashingList(@NonNull String cweId, @NonNull String language, @NonNull String queryName) throws IOException, InterruptedException, CxException {
Expand Down
9 changes: 9 additions & 0 deletions src/test/java/com/checkmarx/ast/ScanTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.checkmarx.ast.ScanResult.ScanResult;
import com.checkmarx.ast.kicsRealtimeResults.KicsRealtimeResults;
import com.checkmarx.ast.scan.Scan;
import com.checkmarx.ast.wrapper.CxException;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

Expand Down Expand Up @@ -64,6 +65,14 @@ void testScanVorpal_WhenInvalidRequestIsSent_ReturnInternalErrorFailure() throws
Assertions.assertEquals(2, error.code);
}

@Test
void testScanVorpal_WhenMissingFileExtension_ReturnFileExtensionIsRequiredFailure() throws Exception {
// ScanResult scanResult = wrapper.ScanVorpal("src/test/resources/python-vul-file", false);
Assertions.assertThrowsExactly(CxException.class, () -> {
wrapper.ScanVorpal("src/test/resources/python-vul-file", false);
}, "file must have an extension");
}

@Test
void testScanList() throws Exception {
List<Scan> cxOutput = wrapper.scanList("limit=10");
Expand Down

0 comments on commit 9a21b8c

Please sign in to comment.