use of com.redhat.cloud.notifications.models.NotificationHistory in project notifications-backend by RedHatInsights.
the class EmailTest method testEmailSubscriptionInstant.
@Test
@Disabled
void testEmailSubscriptionInstant() {
mockGetUsers(8);
final String tenant = "instant-email-tenant";
final String[] usernames = { "username-1", "username-2", "username-4" };
String bundle = "rhel";
String application = "policies";
for (String username : usernames) {
subscribe(tenant, username, bundle, application);
}
final List<String> bodyRequests = new ArrayList<>();
ExpectationResponseCallback verifyEmptyRequest = req -> {
assertEquals(BOP_TOKEN, req.getHeader(EmailSender.BOP_APITOKEN_HEADER).get(0));
assertEquals(BOP_CLIENT_ID, req.getHeader(EmailSender.BOP_CLIENT_ID_HEADER).get(0));
assertEquals(BOP_ENV, req.getHeader(EmailSender.BOP_ENV_HEADER).get(0));
bodyRequests.add(req.getBodyAsString());
return response().withStatusCode(200);
};
HttpRequest postReq = getMockHttpRequest(verifyEmptyRequest);
Action emailActionMessage = TestHelpers.createPoliciesAction(tenant, bundle, application, "My test machine");
Event event = new Event();
event.setAction(emailActionMessage);
EmailSubscriptionProperties properties = new EmailSubscriptionProperties();
Endpoint ep = new Endpoint();
ep.setType(EndpointType.EMAIL_SUBSCRIPTION);
ep.setName("positive feeling");
ep.setDescription("needle in the haystack");
ep.setEnabled(true);
ep.setProperties(properties);
try {
List<NotificationHistory> historyEntries = statelessSessionFactory.withSession(statelessSession -> {
return emailProcessor.process(event, List.of(ep));
});
NotificationHistory history = historyEntries.get(0);
assertTrue(history.isInvocationResult());
assertEquals(3, bodyRequests.size());
List<JsonObject> emailRequests = emailRequestIsOK(bodyRequests, usernames);
for (int i = 0; i < usernames.length; ++i) {
JsonObject body = emailRequests.get(i);
JsonArray emails = body.getJsonArray("emails");
assertNotNull(emails);
assertEquals(1, emails.size());
JsonObject firstEmail = emails.getJsonObject(0);
JsonArray recipients = firstEmail.getJsonArray("recipients");
assertEquals(1, recipients.size());
assertEquals(usernames[i], recipients.getString(0));
JsonArray bccList = firstEmail.getJsonArray("bccList");
assertEquals(0, bccList.size());
String bodyRequest = body.toString();
assertTrue(bodyRequest.contains(TestHelpers.policyId1), "Body should contain policy id" + TestHelpers.policyId1);
assertTrue(bodyRequest.contains(TestHelpers.policyName1), "Body should contain policy name" + TestHelpers.policyName1);
assertTrue(bodyRequest.contains(TestHelpers.policyId2), "Body should contain policy id" + TestHelpers.policyId2);
assertTrue(bodyRequest.contains(TestHelpers.policyName2), "Body should contain policy name" + TestHelpers.policyName2);
// Display name
assertTrue(bodyRequest.contains("My test machine"), "Body should contain the display_name");
// Formatted date
assertTrue(bodyRequest.contains("03 Aug 2020 15:22 UTC"));
}
} catch (Exception e) {
e.printStackTrace();
fail(e);
} finally {
// Remove expectations
MockServerLifecycleManager.getClient().clear(postReq);
}
clearSubscriptions();
}
use of com.redhat.cloud.notifications.models.NotificationHistory in project notifications-backend by RedHatInsights.
the class WebhookTest method testRetry.
private void testRetry(boolean shouldSucceedEventually) {
String url = getMockServerUrl() + "/foobar";
AtomicInteger callsCounter = new AtomicInteger();
ExpectationResponseCallback expectationResponseCallback = request -> {
if (callsCounter.incrementAndGet() == MAX_RETRY_ATTEMPTS && shouldSucceedEventually) {
return response().withStatusCode(200);
} else {
return response().withStatusCode(500);
}
};
HttpRequest mockServerRequest = getMockHttpRequest(expectationResponseCallback);
try {
Action action = buildWebhookAction();
Event event = new Event();
event.setAction(action);
Endpoint ep = buildWebhookEndpoint(url);
List<NotificationHistory> process = webhookTypeProcessor.process(event, List.of(ep));
NotificationHistory history = process.get(0);
assertEquals(shouldSucceedEventually, history.isInvocationResult());
assertEquals(MAX_RETRY_ATTEMPTS, callsCounter.get());
} finally {
// Remove expectations
MockServerLifecycleManager.getClient().clear(mockServerRequest);
}
}
use of com.redhat.cloud.notifications.models.NotificationHistory in project notifications-backend by RedHatInsights.
the class EventResourceTest method assertSameEvent.
private static void assertSameEvent(EventLogEntry eventLogEntry, Event event, NotificationHistory... historyEntries) {
assertEquals(event.getId(), eventLogEntry.getId());
// Jackson's serialization gets rid of nanoseconds so an equals between the LocalDateTime objects won't work.
assertEquals(event.getCreated().toEpochSecond(UTC), eventLogEntry.getCreated().toEpochSecond(UTC));
assertEquals(event.getBundleDisplayName(), eventLogEntry.getBundle());
assertEquals(event.getApplicationDisplayName(), eventLogEntry.getApplication());
assertEquals(event.getEventTypeDisplayName(), eventLogEntry.getEventType());
if (historyEntries == null) {
assertTrue(eventLogEntry.getActions().isEmpty());
} else {
assertEquals(historyEntries.length, eventLogEntry.getActions().size());
for (EventLogEntryAction eventLogEntryAction : eventLogEntry.getActions()) {
Optional<NotificationHistory> historyEntry = Arrays.stream(historyEntries).filter(entry -> entry.getId().equals(eventLogEntryAction.getId())).findAny();
assertTrue(historyEntry.isPresent());
assertEquals(historyEntry.get().getEndpointType(), eventLogEntryAction.getEndpointType());
assertEquals(historyEntry.get().getEndpointSubType(), eventLogEntryAction.getEndpointSubType());
assertEquals(historyEntry.get().isInvocationResult(), eventLogEntryAction.getInvocationResult());
}
}
}
use of com.redhat.cloud.notifications.models.NotificationHistory in project notifications-backend by RedHatInsights.
the class LifecycleITest method createNotificationHistory.
@Transactional
void createNotificationHistory(Event event, String endpointId, boolean invocationResult) {
Endpoint endpoint = entityManager.createQuery("FROM Endpoint WHERE id = :id", Endpoint.class).setParameter("id", UUID.fromString(endpointId)).getSingleResult();
NotificationHistory history = new NotificationHistory();
history.setId(UUID.randomUUID());
history.setEvent(event);
history.setEndpoint(endpoint);
history.setEndpointType(endpoint.getType());
history.setInvocationTime(123L);
history.setInvocationResult(invocationResult);
if (!invocationResult) {
history.setDetails(Map.of("code", 400, "url", "https://www.foo.com", "method", "GET"));
}
history.prePersist();
entityManager.persist(history);
}
use of com.redhat.cloud.notifications.models.NotificationHistory in project notifications-backend by RedHatInsights.
the class ResourceHelpers method createNotificationHistory.
public NotificationHistory createNotificationHistory(Event event, Endpoint endpoint, Boolean invocationResult) {
NotificationHistory history = new NotificationHistory();
history.setId(UUID.randomUUID());
history.setInvocationTime(1L);
history.setInvocationResult(invocationResult);
history.setEvent(event);
history.setEndpoint(endpoint);
history.setEndpointType(endpoint.getType());
history.setEndpointSubType(endpoint.getSubType());
history.prePersist();
entityManager.persist(history);
return history;
}
Aggregations