Skip to content

Commit

Permalink
fix .links in PagerDutyTransformer
Browse files Browse the repository at this point in the history
added test cases based on IQE input
  • Loading branch information
jessicarod7 committed Jan 23, 2025
1 parent 8c83f12 commit fd6fde8
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.redhat.cloud.notifications.connector.pagerduty;

import io.quarkus.logging.Log;
import io.vertx.core.json.JsonArray;
import io.vertx.core.json.JsonObject;
import jakarta.enterprise.context.ApplicationScoped;
import org.apache.camel.Exchange;
Expand Down Expand Up @@ -181,9 +182,11 @@ static JsonObject getClientLinks(final JsonObject cloudEventPayload) {

String inventoryUrl = cloudEventPayload.getString(INVENTORY_URL, "");
if (!inventoryUrl.isEmpty()) {
clientLinks.put(LINKS, JsonObject.of(
HREF, inventoryUrl,
TEXT, "Host"
clientLinks.put(LINKS, JsonArray.of(
JsonObject.of(
HREF, inventoryUrl,
TEXT, "Host"
)
));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,39 @@ void testSuccessfulTestMessage() {
validatePayloadTransform(cloudEventData, expectedPayload);
}

@Test
void testSuccessfulIqeTestMessage() {
JsonObject cloudEventData = createIncomingPayload(TEST_URL);
JsonObject cloudEventPayload = JsonObject.of(
"account_id", "default-account-id",
"application", "inventory",
"bundle", "rhel",
"context", JsonObject.of(
"inventory_id", "85094ed1-1c52-4bc5-8e3e-4ea3869a17ce",
"hostname", "rhiqe.2349fj.notif-test",
"display_name", "rhiqe.2349fj.notif-test",
"rhel_version", "8.0"
),
"event_type", "new-system-registered",
"events", JsonArray.of(),
"org_id", "default-org-id",
"timestamp", "2020-10-03T15:22:13.000000025",
"source", JsonObject.of(
"application", JsonObject.of("display_name", "Inventory"),
"bundle", JsonObject.of("display_name", "Red Hat Enterprise Linux"),
"event_type", JsonObject.of("display_name", "New system registered")
),
"environment_url", "https://localhost"
);
cloudEventPayload.put("inventory_url", "https://localhost/insights/inventory/85094ed1-1c52-4bc5-8e3e-4ea3869a17ce");
cloudEventPayload.put("application_url", "https://localhost/insights/inventory");
cloudEventPayload.put("severity", "error");
cloudEventData.put("payload", cloudEventPayload);

JsonObject expectedPayload = buildExpectedOutgoingPayload(cloudEventData);
validatePayloadTransform(cloudEventData, expectedPayload);
}

@Test
void testInvalidTimestampDropped() {
JsonObject cloudEventData = createIncomingPayload(TEST_URL);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import java.time.LocalDateTime;
import java.util.List;

import static com.redhat.cloud.notifications.TestConstants.DEFAULT_ACCOUNT_ID;
import static com.redhat.cloud.notifications.TestConstants.DEFAULT_ORG_ID;
import static com.redhat.cloud.notifications.processors.ConnectorSender.TOCAMEL_CHANNEL;
import static com.redhat.cloud.notifications.processors.pagerduty.PagerDutyProcessor.PROCESSED_PAGERDUTY_COUNTER;
Expand Down Expand Up @@ -137,6 +138,69 @@ void testPagerDutyUsingConnector() {
micrometerAssertionHelper.assertCounterIncrement(PROCESSED_PAGERDUTY_COUNTER, 1);
}

@Test
void testPagerdutyUsingIqeMessage() {
// Make sure that the payload does not get stored in the database.
when(engineConfig.getKafkaToCamelMaximumRequestSize()).thenReturn(Integer.MAX_VALUE);

Action pagerDutyActionMessage = new Action();
pagerDutyActionMessage.setBundle("rhel");
pagerDutyActionMessage.setApplication("inventory");
pagerDutyActionMessage.setTimestamp(LocalDateTime.of(2020, 10, 3, 15, 22, 13, 25));
pagerDutyActionMessage.setEventType("new-system-registered");
pagerDutyActionMessage.setAccountId(DEFAULT_ACCOUNT_ID);
pagerDutyActionMessage.setOrgId(DEFAULT_ORG_ID);

Context context = new Context.ContextBuilder()
.withAdditionalProperty("inventory_id", "85094ed1-1c52-4bc5-8e3e-4ea3869a17ce")
.withAdditionalProperty("hostname", "rhiqe.2349fj.notif-test")
.withAdditionalProperty("display_name", "rhiqe.2349fj.notif-test")
.withAdditionalProperty("rhel_version", "8.0")
.build();
pagerDutyActionMessage.setContext(context);

Event event = new Event();
event.setEventWrapper(new EventWrapperAction(pagerDutyActionMessage));
event.setBundleDisplayName("Red Hat Enterprise Linux");
event.setApplicationDisplayName("Inventory");
event.setEventTypeDisplayName("New system registered");

Application application = new Application();
application.setName("inventory");
application.setDisplayName("Inventory");
EventType eventType = new EventType();
eventType.setApplication(application);
eventType.setName("new-system-registered");
eventType.setDisplayName("New system registered");
event.setEventType(eventType);

Endpoint ep = buildPagerDutyEndpoint();

pagerDutyProcessor.process(event, List.of(ep));
ArgumentCaptor<NotificationHistory> historyArgumentCaptor = ArgumentCaptor.forClass(NotificationHistory.class);
verify(notificationHistoryRepository, times(1)).createNotificationHistory(historyArgumentCaptor.capture());
NotificationHistory history = historyArgumentCaptor.getAllValues().getFirst();
assertFalse(history.isInvocationResult());
assertEquals(NotificationStatus.PROCESSING, history.getStatus());
// Now let's check the Kafka messages sent to the outgoing channel.
// The channel should have received two messages.
assertEquals(1, inMemorySink.received().size());

// We'll only check the payload and metadata of the first Kafka message.
Message<JsonObject> message = inMemorySink.received().getFirst();
JsonObject payload = message.getPayload();

final JsonObject payloadToSend = transformer.toJsonObject(event);
payloadToSend.put("environment_url", environment.url());
insightsUrlsBuilder.buildInventoryUrl(payloadToSend)
.ifPresent(url -> payloadToSend.put("inventory_url", url));
payloadToSend.put("application_url", insightsUrlsBuilder.buildApplicationUrl(payloadToSend));
payloadToSend.put("severity", PagerDutySeverity.ERROR);
assertEquals(payloadToSend, payload.getJsonObject("payload"));

micrometerAssertionHelper.assertCounterIncrement(PROCESSED_PAGERDUTY_COUNTER, 1);
}

@Test
void testEmailsOnlyMode() {
when(engineConfig.isEmailsOnlyModeEnabled()).thenReturn(true);
Expand Down

0 comments on commit fd6fde8

Please sign in to comment.