Skip to content

Commit

Permalink
add unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
Robert Kiss committed Jan 18, 2024
1 parent 5435656 commit c2c52a3
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@ public PageableList parse(final JSONObject object) {
for (int i = 0; i < securityAdvisoriesNodes.length(); i++) {
final JSONObject securityAdvisory = securityAdvisoriesNodes.getJSONObject(i);
final GitHubSecurityAdvisory advisory = parseSecurityAdvisory(securityAdvisory);
advisories.add(advisory);
if (advisory != null) {
advisories.add(advisory);
}
}
}
pageableList.setTotalCount(securityAdvisories.optInt("totalCount"));
Expand All @@ -65,10 +67,10 @@ private GitHubSecurityAdvisory parseSecurityAdvisory(final JSONObject object) {

// initial check if advisory is valid or withdrawn
String withdrawnAt = object.optString("withdrawnAt", null);
if(object == null || withdrawnAt != null) {
if (object == null || withdrawnAt != null) {
return null;
}

advisory.setDatabaseId(object.getInt("databaseId"));
advisory.setDescription(object.optString("description", null));
advisory.setGhsaId(object.optString("ghsaId", null));
Expand All @@ -84,7 +86,7 @@ private GitHubSecurityAdvisory parseSecurityAdvisory(final JSONObject object) {

final JSONArray identifiers = object.optJSONArray("identifiers");
if (identifiers != null) {
for (int i=0; i<identifiers.length(); i++) {
for (int i = 0; i < identifiers.length(); i++) {
final JSONObject identifier = identifiers.getJSONObject(i);
final String type = identifier.optString("type", null);
final String value = identifier.optString("value", null);
Expand All @@ -97,7 +99,7 @@ private GitHubSecurityAdvisory parseSecurityAdvisory(final JSONObject object) {

final JSONArray references = object.optJSONArray("references");
if (references != null) {
for (int i=0; i<references.length(); i++) {
for (int i = 0; i < references.length(); i++) {
final String url = references.optJSONObject(i).optString("url", null);
if (url != null) {
advisory.addReference(url);
Expand Down Expand Up @@ -140,7 +142,7 @@ private List<GitHubVulnerability> parseVulnerabilities(final JSONObject object)
if (vs != null) {
final JSONArray edges = vs.optJSONArray("edges");
if (edges != null) {
for (int i=0; i<edges.length(); i++) {
for (int i = 0; i < edges.length(); i++) {
final JSONObject node = edges.getJSONObject(i).optJSONObject("node");
if (node != null) {
GitHubVulnerability vulnerability = parseVulnerability(node);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package org.dependencytrack.parser.github;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;

import org.dependencytrack.parser.github.graphql.GitHubSecurityAdvisoryParser;
import org.dependencytrack.parser.github.graphql.model.GitHubSecurityAdvisory;
import org.json.JSONObject;
import org.junit.Assert;
import org.junit.Test;

public class GitHubSecurityAdvisoryParserTest {

GitHubSecurityAdvisoryParser parser = new GitHubSecurityAdvisoryParser();

@Test
public void testWithdrawnAdvisory() throws IOException {

String jsonFile = "src/test/resources/unit/github.jsons/GHSA-8v27-2fg9-7h62.json";
String jsonString = new String(Files.readAllBytes(Paths.get(jsonFile)));
JSONObject jsonObject = new JSONObject(jsonString);
List<GitHubSecurityAdvisory> advisories = parser.parse(jsonObject).getAdvisories();
Assert.assertEquals(0, advisories.size());
}

}
13 changes: 13 additions & 0 deletions src/test/resources/unit/github.jsons/GHSA-8v27-2fg9-7h62.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"data": {
"securityAdvisories": {
"pageInfo": {},
"nodes": [
{
"id": "GHSA-8v27-2fg9-7h62",
"withdrawnAt": "2021-05-04T20:26:20Z"
}
]
}
}
}

0 comments on commit c2c52a3

Please sign in to comment.