Skip to content

Commit

Permalink
feat: added endpoint for multipart media type requests
Browse files Browse the repository at this point in the history
Signed-off-by: Tomer Figenblat <tfigenbl@redhat.com>
  • Loading branch information
TomerFi committed Jul 3, 2023
1 parent 4b1276c commit 88a90f6
Show file tree
Hide file tree
Showing 13 changed files with 2,284 additions and 701 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ module x { // module-info.java
Code example

```java
import com.redhat.crda.Api.MixedReport;
import com.redhat.crda.impl.CrdaApi;
import com.redhat.crda.backend.AnalysisReport;
import java.nio.file.Files;
Expand All @@ -150,6 +151,11 @@ public class CrdaExample {
// get a AnalysisReport future holding a deserialized Stack Analysis report
CompletableFuture<AnalysisReport> stackReport = crdaApi.stackAnalysis("/path/to/pom.xml");

// get a AnalysisReport future holding a mixed report object aggregating:
// - (json) deserialized Stack Analysis report
// - (html) html Stack Analysis report
CompletableFuture<MixedReport> mixedStackReport = crdaApi.stackAnalysisMixed("/path/to/pom.xml");

// get a AnalysisReport future holding a deserialized Component Analysis report
var manifestContent = Files.readAllBytes(Paths.get("/path/to/pom.xml"));
CompletableFuture<AnalysisReport> componentReport = crdaApi.componentAnalysis("pom.xml", manifestContent);
Expand Down
10 changes: 10 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
<!-- Dependencies -->
<jackson.version>2.15.0</jackson.version>
<jakarta.annotation-api.version>2.1.1</jakarta.annotation-api.version>
<jakarta.mail.version>2.0.2</jakarta.mail.version>
<!-- Testing Dependencies -->
<assertj.version>3.24.2</assertj.version>
<junit-jupiter.version>5.9.1</junit-jupiter.version>
Expand Down Expand Up @@ -109,6 +110,11 @@
<artifactId>jakarta.annotation-api</artifactId>
<version>${jakarta.annotation-api.version}</version>
</dependency>
<dependency>
<groupId>org.eclipse.angus</groupId>
<artifactId>jakarta.mail</artifactId>
<version>${jakarta.mail.version}</version>
</dependency>
<!-- Testing Dependencies -->
<dependency>
<groupId>org.assertj</groupId>
Expand Down Expand Up @@ -166,6 +172,10 @@
<groupId>jakarta.annotation</groupId>
<artifactId>jakarta.annotation-api</artifactId>
</dependency>
<dependency>
<groupId>org.eclipse.angus</groupId>
<artifactId>jakarta.mail</artifactId>
</dependency>
<!-- Testing Dependencies -->
<dependency>
<groupId>org.assertj</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,14 @@
*/
package simple.modular;

import static org.assertj.core.api.Assertions.as;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.argThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.redhat.crda.backend.AnalysisReport;
import com.redhat.crda.impl.CrdaApi;
Expand All @@ -39,13 +41,16 @@ class Simple_Integration_Test {
CrdaApi crdaApi;
HttpClient mockHttpClient;

ObjectMapper mapper;

@BeforeAll
static void prepare() {
var useRealApi = System.getenv("CRDA_ITS_USE_REAL_API");
Simple_Integration_Test.useRealAPI = Boolean.parseBoolean(useRealApi);
}
@BeforeEach
void initialize() throws Exception {
mapper = new ObjectMapper().disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
if (Simple_Integration_Test.useRealAPI) {
crdaApi = new CrdaApi();
} else {
Expand Down Expand Up @@ -77,12 +82,37 @@ void test_stack_analysis_html_report() throws Exception {
assertThat(htmlAnalysis).isEqualTo(expectedHtmlAnalysis);
}

@Test
void test_stack_analysis_mixed_report() throws Exception {
// load the pre-configured expected html and json responses
var expectedHtmlAnalysis = Files.readAllBytes(Paths.get("src/test/resources/it_poms/analysis-report.html"));
var expectedAnalysisJson = Files.readString(Paths.get("src/test/resources/it_poms/analysis-report.json"));
// deserialize the json expected response
var expectedAnalysis = mapper.readValue(expectedAnalysisJson, AnalysisReport.class);

var expectedMixedAnalysis = Files.readAllBytes(Paths.get("src/test/resources/it_poms/analysis-report.mixed"));
if (!Simple_Integration_Test.useRealAPI) {
// mock a http response object and stub it to return the expected html report as a body
var mockMixedResponse = mock(HttpResponse.class);
when(mockMixedResponse.body()).thenReturn(expectedMixedAnalysis);
// stub the mocked http client to return the mocked http response for requests accepting text/html
when(mockHttpClient.sendAsync(
argThat(r -> r.headers().firstValue("Accept").get().equals("multipart/mixed")), any())
).thenReturn(CompletableFuture.completedFuture(mockMixedResponse));
}

// get the html report from the api
var mixedAnalysis = crdaApi.stackAnalysisMixed("src/test/resources/it_poms/pom.xml").get();
assertThat(new String(mixedAnalysis.html).trim()).isEqualTo(new String(expectedHtmlAnalysis).trim());
assertThat(mixedAnalysis.json).isEqualTo(expectedAnalysis);
}

@Test
void test_stack_analysis_report() throws Exception {
// load the pre-configured expected json response
var expectedAnalysisJson = Files.readString(Paths.get("src/test/resources/it_poms/analysis-report.json"));
// deserialize the expected response
var expectedAnalysis = new ObjectMapper().readValue(expectedAnalysisJson, AnalysisReport.class);
var expectedAnalysis = mapper.readValue(expectedAnalysisJson, AnalysisReport.class);
if (!Simple_Integration_Test.useRealAPI) {
// mock a http response object and stub it to return the expected json report as a body
var mockJsonResponse = mock(HttpResponse.class);
Expand All @@ -103,7 +133,7 @@ void test_component_analysis_report() throws Exception {
// load the pre-configured expected json response
var expectedAnalysisJson = Files.readString(Paths.get("src/test/resources/it_poms/analysis-report.json"));
// deserialize the expected response
var expectedAnalysis = new ObjectMapper().readValue(expectedAnalysisJson, AnalysisReport.class);
var expectedAnalysis = mapper.readValue(expectedAnalysisJson, AnalysisReport.class);
if (!Simple_Integration_Test.useRealAPI) {
// mock a http response object and stub it to return the expected json report as a body
var mockJsonResponse = mock(HttpResponse.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,20 @@
font-family: Arial;
padding: 20px;
}
.accordion-toggle[aria-expanded="true"] i.fas.fa-angle-down {
display: none;
}
.accordion-toggle[aria-expanded="false"] i.fas.fa-angle-up {
display: none;
}
.pf-c-table thead,
.pf-c-table .pf-m-truncate {
--pf-c-table--cell--MaxWidth: none !important;
}

.pf-c-table thead, .pf-c-table .pf-m-truncate {
--pf-c-table--cell--MinWidth: none !important;
}
.hiddenRow {
padding: 0 !important;
}
Expand Down Expand Up @@ -124,7 +134,7 @@
integrity="sha384-TX8t27EcRE3e/ihU7zmQxVncDAy5uIKz4rEkgIXeMed4M0jlfIDPvg6uqKI2xXr2" crossorigin="anonymous">
<title>CRDA Dependency Analysis</title>
</head>
<body class="p-4 container-fluid">
<body class="p-2 container-fluid">

<div class="card">
<div class="card-header">
Expand Down Expand Up @@ -176,21 +186,22 @@
<table class="pf-c-table pf-m-expandable pf-m-compact pf-m-grid-md">
<thead class="">
<tr>
<th role="columnheader" scope="col"></th>
<th role="columnheader" scope="col">#</th>
<th role="columnheader" scope="col">Dependencies</th>
<th role="columnheader" scope="col"># Direct</th>
<th role="columnheader" scope="col"># Transitive</th>
<th role="columnheader" scope="col">Highest CVSS</th>
<th role="columnheader" scope="col">Highest Severity</th>
<th role="columnheader" scope="col">Red Hat remediation available</th>
<th scope="col"></th>
<th scope="col">#</th>
<th scope="col">Dependencies</th>
<th scope="col"># Direct</th>
<th scope="col"># Transitive</th>
<th scope="col">Highest CVSS</th>
<th scope="col">Highest Severity</th>
<th scope="col">Red Hat remediation available</th>
</tr>
</thead>
<tbody>
<tr data-toggle="collapse" data-target="#log4jlog4j" class="accordion-toggle">
<tr data-toggle="collapse" data-target="#log4jlog4j" class="accordion-toggle" aria-expanded="false">
<td role="cell">
<div class="pf-c-table__toggle-icon">
<i class="fas fa-angle-down" aria-hidden="true"></i>
<i class="fas fa-angle-up" aria-hidden="true"></i>
</div>
</td>
<td>#1</td>
Expand Down Expand Up @@ -226,7 +237,7 @@
target="_blank">
SNYK-JAVA-LOG4J-1300176
</a>

</td>
<td>
</td>
Expand Down Expand Up @@ -517,17 +528,17 @@
aria-controls="log4jlog4jtransTable">
Transitive Dependencies with vulnerabilites <i class="fa fa-angle-down"></i>
</button>
<div class="p-3 collapse" id="log4jlog4jtransTable">
<div class="collapse" id="log4jlog4jtransTable">
<table class="pf-c-table pf-m-expandable pf-m-compact pf-m-grid-md">
<thead>
<tr>
<th scope="col" style="width: 21%">Dependencies</th>
<th scope="col" style="width: 19%">Dependencies</th>
<th scope="col">Severity</th>
<th scope="col" style="width: 18%">Exploit Maturity</th>
<th scope="col" >Exploit Maturity</th>
<th scope="col">Description</th>
<th scope="col" style="width: 13%">CVSS</th>
<th scope="col">CVE</th>
<th scope="col">Remediation</th>
<th scope="col" style="width: 13%">CVE</th>
<th scope="col" style="width: 26%">Remediation</th>
</tr>
</thead>
<tbody>
Expand Down Expand Up @@ -588,4 +599,4 @@ <h5 class="modal-title" id="modalLabel">
})
</script>
</body>
</html>
</html>
Loading

0 comments on commit 88a90f6

Please sign in to comment.