use of com.redhat.cloud.notifications.models.Endpoint in project notifications-backend by RedHatInsights.
the class LifecycleITest method addBehaviorGroupAction.
@Transactional
void addBehaviorGroupAction(UUID behaviorGroupId, UUID endpointId) {
BehaviorGroup behaviorGroup = entityManager.find(BehaviorGroup.class, behaviorGroupId);
Endpoint endpoint = entityManager.find(Endpoint.class, endpointId);
BehaviorGroupAction action = new BehaviorGroupAction();
action.setId(new BehaviorGroupActionId());
action.setBehaviorGroup(behaviorGroup);
action.setEndpoint(endpoint);
entityManager.persist(action);
}
use of com.redhat.cloud.notifications.models.Endpoint in project notifications-backend by RedHatInsights.
the class LifecycleITest method createEndpoint.
@Transactional
Endpoint createEndpoint(String accountId, EndpointType type, String name, String description, EndpointProperties properties) {
Endpoint endpoint = new Endpoint();
endpoint.setType(type);
endpoint.setAccountId(accountId);
endpoint.setEnabled(true);
endpoint.setName(name);
endpoint.setDescription(description);
endpoint.setProperties(properties);
properties.setEndpoint(endpoint);
entityManager.persist(endpoint);
entityManager.persist(endpoint.getProperties());
return endpoint;
}
use of com.redhat.cloud.notifications.models.Endpoint in project notifications-backend by RedHatInsights.
the class CamelTypeProcessorTest method testOBEndpointProcessing.
@Test
void testOBEndpointProcessing() {
// We need input data for the test.
Event event = buildEvent();
event.setAccountId("rhid123");
Endpoint endpoint = buildCamelEndpoint(event.getAction().getAccountId());
endpoint.setSubType("slack");
processor.obEnabled = true;
bridgeHelper.setObEnabled(true);
// Let's trigger the processing.
// First with 'random OB endpoints', so we expect this to fail
List<NotificationHistory> result = processor.process(event, List.of(endpoint));
// One endpoint should have been processed.
assertEquals(1, result.size());
// Metrics should report the same thing.
micrometerAssertionHelper.assertCounterIncrement(PROCESSED_COUNTER_NAME, 0, SUB_TYPE_KEY, SUB_TYPE);
micrometerAssertionHelper.assertCounterIncrement(PROCESSED_COUNTER_NAME, 1, SUB_TYPE_KEY, "slack");
micrometerAssertionHelper.assertCounterIncrement(PROCESSED_COUNTER_NAME, 0, SUB_TYPE_KEY, "other-type");
micrometerAssertionHelper.assertCounterIncrement(PROCESSED_COUNTER_NAME, 0);
// Let's have a look at the first result entry fields.
NotificationHistory historyItem = result.get(0);
assertEquals(event, historyItem.getEvent());
assertEquals(endpoint, historyItem.getEndpoint());
assertEquals(CAMEL, historyItem.getEndpointType());
assertEquals("slack", historyItem.getEndpointSubType());
assertEquals(1, historyItem.getDetails().size());
Map<String, Object> details = historyItem.getDetails();
assertTrue(details.containsKey("failure"));
// Now set up some mock OB endpoints (simulate valid bridge)
String eventsEndpoint = getMockServerUrl() + "/events";
System.out.println("==> Setting events endpoint to " + eventsEndpoint);
Bridge bridge = new Bridge("321", eventsEndpoint, "my bridge");
Map<String, String> auth = new HashMap<>();
auth.put("access_token", "li-la-lu-token");
Map<String, String> obProcessor = new HashMap<>();
obProcessor.put("id", "p-my-id");
MockServerConfig.addOpenBridgeEndpoints(auth, bridge);
bridgeHelper.setOurBridge("321");
System.out.println("==> Auth token " + bridgeHelper.getAuthToken());
System.out.println("==> The bridge " + bridgeHelper.getBridgeIfNeeded());
// Process again
result = processor.process(event, List.of(endpoint));
// One endpoint should have been processed.
assertEquals(1, result.size());
// Metrics should report the same thing.
micrometerAssertionHelper.assertCounterIncrement(PROCESSED_COUNTER_NAME, 2, SUB_TYPE_KEY, "slack");
// Let's have a look at the first result entry fields.
historyItem = result.get(0);
assertEquals(event, historyItem.getEvent());
assertEquals(endpoint, historyItem.getEndpoint());
assertEquals(CAMEL, historyItem.getEndpointType());
assertEquals("slack", historyItem.getEndpointSubType());
assertNull(historyItem.getDetails());
// Now try again, but the remote throws an error
event.getAction().setAccountId("something-random");
result = processor.process(event, List.of(endpoint));
assertEquals(1, result.size());
// Metrics should report the same thing.
micrometerAssertionHelper.assertCounterIncrement(PROCESSED_COUNTER_NAME, 3, SUB_TYPE_KEY, "slack");
// Let's have a look at the first result entry fields.
historyItem = result.get(0);
assertEquals(event, historyItem.getEvent());
assertEquals(1, historyItem.getDetails().size());
details = historyItem.getDetails();
assertTrue(details.containsKey("failure"));
assertNotNull(historyItem.getInvocationTime());
// The invocation will be complete when the response from Camel has been received.
assertFalse(historyItem.isInvocationResult());
MockServerConfig.clearOpenBridgeEndpoints(bridge);
processor.obEnabled = false;
bridgeHelper.setObEnabled(false);
}
use of com.redhat.cloud.notifications.models.Endpoint in project notifications-backend by RedHatInsights.
the class CamelTypeProcessorTest method buildCamelEndpoint.
private static Endpoint buildCamelEndpoint(String accountId) {
BasicAuthentication basicAuth = new BasicAuthentication("john", "doe");
CamelProperties properties = new CamelProperties();
properties.setUrl("https://redhat.com");
properties.setDisableSslVerification(TRUE);
properties.setSecretToken("top-secret");
properties.setBasicAuthentication(basicAuth);
properties.setExtras(Map.of("foo", "bar"));
// Todo: NOTIF-429 backward compatibility change - Remove soon.
properties.setSubType(SUB_TYPE);
Endpoint endpoint = new Endpoint();
endpoint.setAccountId(accountId);
endpoint.setType(CAMEL);
endpoint.setSubType(SUB_TYPE);
endpoint.setProperties(properties);
return endpoint;
}
use of com.redhat.cloud.notifications.models.Endpoint in project notifications-backend by RedHatInsights.
the class EmailTest method testEmailSubscriptionInstantWrongPayload.
@Test
@Disabled
void testEmailSubscriptionInstantWrongPayload() {
mockGetUsers(8);
final String tenant = "instant-email-tenant-wrong-payload";
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 = new Action();
emailActionMessage.setBundle(bundle);
emailActionMessage.setApplication(application);
emailActionMessage.setTimestamp(LocalDateTime.of(2020, 10, 3, 15, 22, 13, 25));
emailActionMessage.setEventType(TestHelpers.eventType);
emailActionMessage.setRecipients(List.of());
emailActionMessage.setContext(new Context.ContextBuilder().withAdditionalProperty("inventory_id-wrong", "host-01").withAdditionalProperty("system_check_in-wrong", "2020-08-03T15:22:42.199046").withAdditionalProperty("display_name-wrong", "My test machine").withAdditionalProperty("tags-what?", List.of()).build());
emailActionMessage.setEvents(List.of(new com.redhat.cloud.notifications.ingress.Event.EventBuilder().withMetadata(new Metadata.MetadataBuilder().build()).withPayload(new Payload.PayloadBuilder().withAdditionalProperty("foo", "bar").build()).build()));
emailActionMessage.setAccountId(tenant);
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));
});
// The processor returns a null history value but Multi does not support null values so the resulting Multi is empty.
assertTrue(historyEntries.isEmpty());
// No email, invalid payload
assertEquals(0, bodyRequests.size());
} catch (Exception e) {
e.printStackTrace();
fail(e);
} finally {
// Remove expectations
MockServerLifecycleManager.getClient().clear(postReq);
}
clearSubscriptions();
}
Aggregations