Search in sources :

Example 16 with Endpoint

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);
}
Also used : BehaviorGroupActionId(com.redhat.cloud.notifications.models.BehaviorGroupActionId) Endpoint(com.redhat.cloud.notifications.models.Endpoint) BehaviorGroup(com.redhat.cloud.notifications.models.BehaviorGroup) BehaviorGroupAction(com.redhat.cloud.notifications.models.BehaviorGroupAction) Transactional(javax.transaction.Transactional)

Example 17 with Endpoint

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;
}
Also used : Endpoint(com.redhat.cloud.notifications.models.Endpoint) Transactional(javax.transaction.Transactional)

Example 18 with 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);
}
Also used : Endpoint(com.redhat.cloud.notifications.models.Endpoint) NotificationHistory(com.redhat.cloud.notifications.models.NotificationHistory) HashMap(java.util.HashMap) Event(com.redhat.cloud.notifications.models.Event) JsonObject(io.vertx.core.json.JsonObject) Bridge(com.redhat.cloud.notifications.openbridge.Bridge) Test(org.junit.jupiter.api.Test) QuarkusTest(io.quarkus.test.junit.QuarkusTest)

Example 19 with Endpoint

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;
}
Also used : Endpoint(com.redhat.cloud.notifications.models.Endpoint) CamelProperties(com.redhat.cloud.notifications.models.CamelProperties) BasicAuthentication(com.redhat.cloud.notifications.models.BasicAuthentication)

Example 20 with 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();
}
Also used : INSTANT(com.redhat.cloud.notifications.models.EmailSubscriptionType.INSTANT) Assertions.fail(org.junit.jupiter.api.Assertions.fail) Assertions.assertNotNull(org.junit.jupiter.api.Assertions.assertNotNull) Arrays(java.util.Arrays) EmailSubscriptionProperties(com.redhat.cloud.notifications.models.EmailSubscriptionProperties) HttpRequest(org.mockserver.model.HttpRequest) Endpoint(com.redhat.cloud.notifications.models.Endpoint) ITUserResponse(com.redhat.cloud.notifications.recipients.itservice.pojo.response.ITUserResponse) LocalDateTime(java.time.LocalDateTime) Disabled(org.junit.jupiter.api.Disabled) NotificationHistory(com.redhat.cloud.notifications.models.NotificationHistory) MockServerLifecycleManager(com.redhat.cloud.notifications.MockServerLifecycleManager) ArrayList(java.util.ArrayList) Inject(javax.inject.Inject) InjectSpy(io.quarkus.test.junit.mockito.InjectSpy) ITUserRequest(com.redhat.cloud.notifications.recipients.itservice.pojo.request.ITUserRequest) Event(com.redhat.cloud.notifications.models.Event) JsonObject(io.vertx.core.json.JsonObject) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) Payload(com.redhat.cloud.notifications.ingress.Payload) Context(com.redhat.cloud.notifications.ingress.Context) InjectMock(io.quarkus.test.junit.mockito.InjectMock) RestClient(org.eclipse.microprofile.rest.client.inject.RestClient) Transactional(javax.transaction.Transactional) ExpectationResponseCallback(org.mockserver.mock.action.ExpectationResponseCallback) EntityManager(javax.persistence.EntityManager) Collectors(java.util.stream.Collectors) ITUserService(com.redhat.cloud.notifications.recipients.itservice.ITUserService) Test(org.junit.jupiter.api.Test) JsonArray(io.vertx.core.json.JsonArray) Mockito(org.mockito.Mockito) List(java.util.List) Authentication(com.redhat.cloud.notifications.recipients.itservice.pojo.response.Authentication) EndpointType(com.redhat.cloud.notifications.models.EndpointType) StatelessSessionFactory(com.redhat.cloud.notifications.db.StatelessSessionFactory) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) AccountRelationship(com.redhat.cloud.notifications.recipients.itservice.pojo.response.AccountRelationship) TestHelpers(com.redhat.cloud.notifications.TestHelpers) Metadata(com.redhat.cloud.notifications.ingress.Metadata) Action(com.redhat.cloud.notifications.ingress.Action) Comparator(java.util.Comparator) HttpResponse.response(org.mockserver.model.HttpResponse.response) PersonalInformation(com.redhat.cloud.notifications.recipients.itservice.pojo.response.PersonalInformation) HttpRequest(org.mockserver.model.HttpRequest) Action(com.redhat.cloud.notifications.ingress.Action) ExpectationResponseCallback(org.mockserver.mock.action.ExpectationResponseCallback) EmailSubscriptionProperties(com.redhat.cloud.notifications.models.EmailSubscriptionProperties) ArrayList(java.util.ArrayList) Endpoint(com.redhat.cloud.notifications.models.Endpoint) NotificationHistory(com.redhat.cloud.notifications.models.NotificationHistory) Event(com.redhat.cloud.notifications.models.Event) Test(org.junit.jupiter.api.Test) Disabled(org.junit.jupiter.api.Disabled)

Aggregations

Endpoint (com.redhat.cloud.notifications.models.Endpoint)57 Test (org.junit.jupiter.api.Test)23 JsonObject (io.vertx.core.json.JsonObject)22 QuarkusTest (io.quarkus.test.junit.QuarkusTest)20 WebhookProperties (com.redhat.cloud.notifications.models.WebhookProperties)18 DbIsolatedTest (com.redhat.cloud.notifications.db.DbIsolatedTest)15 CamelProperties (com.redhat.cloud.notifications.models.CamelProperties)14 NotificationHistory (com.redhat.cloud.notifications.models.NotificationHistory)14 Header (io.restassured.http.Header)13 List (java.util.List)13 Transactional (javax.transaction.Transactional)13 Inject (javax.inject.Inject)12 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)12 EndpointType (com.redhat.cloud.notifications.models.EndpointType)10 Collectors (java.util.stream.Collectors)10 EmailSubscriptionProperties (com.redhat.cloud.notifications.models.EmailSubscriptionProperties)9 Event (com.redhat.cloud.notifications.models.Event)9 Response (io.restassured.response.Response)9 Map (java.util.Map)9 UUID (java.util.UUID)9