Search in sources :

Example 1 with Bridge

use of com.redhat.cloud.notifications.openbridge.Bridge 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 2 with Bridge

use of com.redhat.cloud.notifications.openbridge.Bridge in project notifications-backend by RedHatInsights.

the class EndpointResourceTest method addOpenBridgeEndpoint.

@Test
void addOpenBridgeEndpoint() {
    String tenant = "empty";
    String orgId = "empty";
    String userName = "user";
    String identityHeaderValue = TestHelpers.encodeRHIdentityInfo(tenant, orgId, userName);
    Header identityHeader = TestHelpers.createRHIdentityHeader(identityHeaderValue);
    MockServerConfig.addMockRbacAccess(identityHeaderValue, MockServerConfig.RbacAccess.FULL_ACCESS);
    CamelProperties cAttr = new CamelProperties();
    cAttr.setDisableSslVerification(false);
    cAttr.setUrl(getMockServerUrl());
    Map<String, String> extras = new HashMap<>();
    extras.put("channel", "#notifications");
    cAttr.setExtras(extras);
    Endpoint ep = new Endpoint();
    ep.setType(EndpointType.CAMEL);
    ep.setSubType("slack");
    ep.setName("Push the camel through the needle's ear");
    ep.setDescription("I guess the camel is slacking");
    ep.setEnabled(true);
    ep.setProperties(cAttr);
    endpointResource.obEnabled = true;
    bridgeHelper.setObEnabled(true);
    // First we try with bogus values for the OB endpoint itself (no valid bridge)
    given().header(identityHeader).when().contentType(JSON).body(Json.encode(ep)).post("/endpoints").then().statusCode(500);
    // Now set up some mock OB endpoints (simulate valid bridge)
    Bridge bridge = new Bridge("321", "http://some.events/", "my bridge");
    Map<String, String> auth = new HashMap<>();
    auth.put("access_token", "li-la-lu-token");
    Map<String, String> processor = new HashMap<>();
    processor.put("id", "p-my-id");
    MockServerConfig.addOpenBridgeEndpoints(auth, bridge, processor);
    bridgeHelper.setOurBridge("321");
    String responseBody = given().header(identityHeader).when().contentType(JSON).body(Json.encode(ep)).post("/endpoints").then().statusCode(200).contentType(JSON).extract().asString();
    JsonObject responsePoint = new JsonObject(responseBody);
    responsePoint.mapTo(Endpoint.class);
    String id = responsePoint.getString("id");
    assertNotNull(id);
    try {
        JsonObject endpoint = fetchSingle(id, identityHeader);
        JsonObject properties = responsePoint.getJsonObject("properties");
        assertNotNull(properties);
        assertTrue(endpoint.getBoolean("enabled"));
        assertEquals("slack", endpoint.getString("sub_type"));
        JsonObject extrasObject = properties.getJsonObject("extras");
        assertNotNull(extrasObject);
        String channel = extrasObject.getString("channel");
        assertEquals("#notifications", channel);
    } finally {
        given().header(identityHeader).when().delete("/endpoints/" + id).then().statusCode(204);
    }
    MockServerConfig.clearOpenBridgeEndpoints(bridge);
    bridgeHelper.setObEnabled(false);
    endpointResource.obEnabled = false;
}
Also used : Header(io.restassured.http.Header) Endpoint(com.redhat.cloud.notifications.models.Endpoint) HashMap(java.util.HashMap) CamelProperties(com.redhat.cloud.notifications.models.CamelProperties) 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) DbIsolatedTest(com.redhat.cloud.notifications.db.DbIsolatedTest) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Aggregations

Endpoint (com.redhat.cloud.notifications.models.Endpoint)2 Bridge (com.redhat.cloud.notifications.openbridge.Bridge)2 QuarkusTest (io.quarkus.test.junit.QuarkusTest)2 JsonObject (io.vertx.core.json.JsonObject)2 HashMap (java.util.HashMap)2 Test (org.junit.jupiter.api.Test)2 DbIsolatedTest (com.redhat.cloud.notifications.db.DbIsolatedTest)1 CamelProperties (com.redhat.cloud.notifications.models.CamelProperties)1 Event (com.redhat.cloud.notifications.models.Event)1 NotificationHistory (com.redhat.cloud.notifications.models.NotificationHistory)1 Header (io.restassured.http.Header)1 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)1