Skip to content

Commit

Permalink
Add tests for CsWebexPublisher and remove MockServer
Browse files Browse the repository at this point in the history
Contributes to #993

Signed-off-by: nscuro <[email protected]>
  • Loading branch information
nscuro committed Feb 13, 2024
1 parent 20c9e9b commit 5e4b29c
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 68 deletions.
14 changes: 5 additions & 9 deletions notification-publisher/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@
<groupId>io.quarkus</groupId>
<artifactId>quarkus-logging-json</artifactId>
</dependency>

<dependency>
<groupId>io.pebbletemplates</groupId>
<artifactId>pebble</artifactId>
</dependency>
<dependency>
<groupId>io.github.resilience4j</groupId>
<artifactId>resilience4j-all</artifactId>
Expand All @@ -78,11 +83,6 @@
<artifactId>quarkus-jacoco</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mock-server</groupId>
<artifactId>mockserver-netty</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.github.tomakehurst</groupId>
<artifactId>wiremock-jre8-standalone</artifactId>
Expand All @@ -93,10 +93,6 @@
<artifactId>quarkus-test-kafka-companion</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.pebbletemplates</groupId>
<artifactId>pebble</artifactId>
</dependency>
</dependencies>

<profiles>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,74 +20,97 @@

import io.quarkus.test.TestTransaction;
import io.quarkus.test.junit.QuarkusTest;
import jakarta.inject.Inject;
import jakarta.json.JsonObject;
import jakarta.persistence.EntityManager;
import org.apache.http.HttpHeaders;
import org.dependencytrack.proto.notification.v1.Group;
import org.dependencytrack.proto.notification.v1.Level;
import org.dependencytrack.proto.notification.v1.Notification;
import org.dependencytrack.proto.notification.v1.Scope;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.mockserver.client.MockServerClient;
import org.mockserver.integration.ClientAndServer;

import static org.dependencytrack.notification.publisher.PublisherTestUtil.createPublisherContext;
import static org.dependencytrack.notification.publisher.PublisherTestUtil.getConfig;
import static org.mockserver.integration.ClientAndServer.startClientAndServer;
import static org.mockserver.model.HttpRequest.request;
import static org.mockserver.model.HttpResponse.response;

import static com.github.tomakehurst.wiremock.client.WireMock.anyUrl;
import static com.github.tomakehurst.wiremock.client.WireMock.equalTo;
import static com.github.tomakehurst.wiremock.client.WireMock.equalToJson;
import static com.github.tomakehurst.wiremock.client.WireMock.postRequestedFor;

@QuarkusTest
public class CsWebexPublisherTest {
public class CsWebexPublisherTest extends AbstractWebhookPublisherTest<CsWebexPublisher> {

@Override
@TestTransaction
void testInformWithBomConsumedNotification() throws Exception {
super.testInformWithBomConsumedNotification();

wireMockServer.verify(postRequestedFor(anyUrl())
.withHeader("Content-Type", equalTo("application/json"))
.withRequestBody(equalToJson("""
{
"markdown": "**Bill of Materials Consumed**\\n[View Component](https://example.com/component/?uuid=)\\n**Description:** A CycloneDX BOM was consumed and will be processed"
}
""")));
}

@Override
@TestTransaction
void testInformWithBomProcessingFailedNotification() throws Exception {
super.testInformWithBomProcessingFailedNotification();

wireMockServer.verify(postRequestedFor(anyUrl())
.withHeader("Content-Type", equalTo("application/json"))
.withRequestBody(equalToJson("""
{
"markdown": "**Bill of Materials Processing Failed**\\n[View Component](https://example.com/component/?uuid=)\\n**Description:** An error occurred while processing a BOM"
}
""")));
}

@Inject
CsWebexPublisher publisher;
@Override
@TestTransaction
void testInformWithBomProcessingFailedNotificationAndNoSpecVersionInSubject() throws Exception {
super.testInformWithBomProcessingFailedNotificationAndNoSpecVersionInSubject();

@Inject
EntityManager entityManager;
wireMockServer.verify(postRequestedFor(anyUrl())
.withHeader("Content-Type", equalTo("application/json"))
.withRequestBody(equalToJson("""
{
"markdown": "**Bill of Materials Processing Failed**\\n[View Component](https://example.com/component/?uuid=)\\n**Description:** An error occurred while processing a BOM"
}
""")));
}

private static ClientAndServer mockServer;
@Override
@TestTransaction
void testInformWithDataSourceMirroringNotification() throws Exception {
super.testInformWithDataSourceMirroringNotification();

@BeforeAll
public static void beforeClass() {
mockServer = startClientAndServer(1040);
wireMockServer.verify(postRequestedFor(anyUrl())
.withHeader("Content-Type", equalTo("application/json"))
.withRequestBody(equalToJson("""
{
"markdown": "**GitHub Advisory Mirroring**\\n[View Component](https://example.com/component/?uuid=)\\n**Description:** An error occurred mirroring the contents of GitHub Advisories. Check log for details."
}
""")));
}

@AfterAll
public static void afterClass() {
mockServer.stop();
@Override
@TestTransaction
void testInformWithNewVulnerabilityNotification() throws Exception {
super.testInformWithNewVulnerabilityNotification();

wireMockServer.verify(postRequestedFor(anyUrl())
.withHeader("Content-Type", equalTo("application/json"))
.withRequestBody(equalToJson("""
{
"markdown": "**New Vulnerability Identified**\\n**VulnID:** INT-001\\n**Severity:** MEDIUM\\n**Source:** INTERNAL\\n**Component:** componentName : componentVersion\\n**Actions:**\\n[View Vulnerability](https://example.com/vulnerability/?source=INTERNAL&vulnId=INT-001)\\n[View Component](https://example.com/component/?uuid=94f87321-a5d1-4c2f-b2fe-95165debebc6)\\n**Description:** "
}
""")));
}

@Test
@Override
@TestTransaction
public void testPublish() throws Exception {
new MockServerClient("localhost", 1040)
.when(
request()
.withMethod("POST")
.withPath("/mychannel")
)
.respond(
response()
.withStatusCode(200)
.withHeader(HttpHeaders.CONTENT_TYPE, "application/json")
);
entityManager.createNativeQuery("""
INSERT INTO "CONFIGPROPERTY" ("DESCRIPTION", "GROUPNAME", "PROPERTYTYPE", "PROPERTYNAME", "PROPERTYVALUE") VALUES
('cswebex', 'general', 'STRING', 'base.url', 'http://localhost:1040/mychannel');
""").executeUpdate();

JsonObject config = getConfig("WEBEX","http://localhost:1040/mychannel");
var notification = Notification.newBuilder()
.setScope(Scope.SCOPE_PORTFOLIO)
.setLevel(Level.LEVEL_INFORMATIONAL)
.setGroup(Group.GROUP_NEW_VULNERABILITY)
.setTitle("Test Notification")
.setContent("This is only a test")
.build();
publisher.inform(createPublisherContext(notification), notification, config);
void testInformWithProjectAuditChangeNotification() throws Exception {
super.testInformWithProjectAuditChangeNotification();

wireMockServer.verify(postRequestedFor(anyUrl())
.withHeader("Content-Type", equalTo("application/json"))
.withRequestBody(equalToJson("""
{
"markdown": "**Analysis Decision: Finding Suppressed**\\n[View Component](https://example.com/component/?uuid=94f87321-a5d1-4c2f-b2fe-95165debebc6)\\n**Description:** "
}
""")));
}

}
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"markdown":"**{{ notification.title | escape(strategy="json") }}**{% if notification.group == "GROUP_NEW_VULNERABILITY" %}\n**VulnID:** {{ subject.vulnerability.vulnId | escape(strategy="json") }}\n**Severity:** {{ subject.vulnerability.severity | escape(strategy="json") }}\n**Source:** {{ subject.vulnerability.source | escape(strategy="json") }}\n**Component:** {{ subject.component | summarize | escape(strategy="json") }}\n**Actions:**\n[View Vulnerability]({{ baseUrl }}/vulnerability/?source={{ subject.vulnerability.source | escape(strategy="json") }}&vulnId={{ subject.vulnerability.vulnId | escape(strategy="json") }}){% elseif notification.group == "GROUP_NEW_VULNERABLE_DEPENDENCY" %}\n**Project:** {{ subject.dependency.project | summarize | escape(strategy="json") }}\n**Component:** {{ subject.dependency.component | summarize | escape(strategy="json") }}\n**Actions:**\n[View Project]({{ baseUrl }}/projects/?uuid={{ subject.dependency.project.uuid | escape(strategy="json") }}){% endif %}\n[View Component]({{ baseUrl }}/component/?uuid={{ subject.dependency.component.uuid | escape(strategy="json") }})\n**Description:** {{ notification.content | escape(strategy="json") }}"}
{"markdown":"**{{ notification.title | escape(strategy="json") }}**{% if notification.group == "GROUP_NEW_VULNERABILITY" %}\n**VulnID:** {{ subject.vulnerability.vulnId | escape(strategy="json") }}\n**Severity:** {{ subject.vulnerability.severity | escape(strategy="json") }}\n**Source:** {{ subject.vulnerability.source | escape(strategy="json") }}\n**Component:** {{ subject.component | summarize | escape(strategy="json") }}\n**Actions:**\n[View Vulnerability]({{ baseUrl }}/vulnerability/?source={{ subject.vulnerability.source | escape(strategy="json") }}&vulnId={{ subject.vulnerability.vulnId | escape(strategy="json") }}){% elseif notification.group == "GROUP_NEW_VULNERABLE_DEPENDENCY" %}\n**Project:** {{ subject.project | summarize | escape(strategy="json") }}\n**Component:** {{ subject.component | summarize | escape(strategy="json") }}\n**Actions:**\n[View Project]({{ baseUrl }}/projects/?uuid={{ subject.project.uuid | escape(strategy="json") }}){% endif %}\n[View Component]({{ baseUrl }}/component/?uuid={{ subject.component.uuid | escape(strategy="json") }})\n**Description:** {{ notification.content | escape(strategy="json") }}"}

0 comments on commit 5e4b29c

Please sign in to comment.