Search in sources :

Example 1 with CamelProperties

use of com.redhat.cloud.notifications.models.CamelProperties in project notifications-backend by RedHatInsights.

the class CamelTypeProcessor method callCamel.

public NotificationHistory callCamel(Notification item, UUID historyId, JsonObject payload, String originalEventId) {
    final long startTime = System.currentTimeMillis();
    Endpoint endpoint = item.getEndpoint();
    String accountId = endpoint.getAccountId();
    // the next could give a CCE, but we only come here when it is a camel endpoint anyway
    String subType = endpoint.getSubType();
    CamelProperties camelProperties = endpoint.getProperties(CamelProperties.class);
    String integrationName = endpoint.getName();
    if (subType.equals("slack")) {
        // OpenBridge
        long endTime;
        NotificationHistory history = getHistoryStub(endpoint, item.getEvent(), 0L, historyId);
        try {
            callOpenBridge(payload, historyId, accountId, camelProperties, integrationName, originalEventId);
            history.setInvocationResult(true);
        } catch (Exception e) {
            history.setInvocationResult(false);
            Map<String, Object> details = new HashMap<>();
            details.put("failure", e.getMessage());
            history.setDetails(details);
            LOGGER.infof("SE: Sending event with historyId=%s and originalId=%s failed: %s ", historyId, originalEventId, e.getMessage());
        } finally {
            endTime = System.currentTimeMillis();
        }
        history.setInvocationTime(endTime - startTime);
        return history;
    } else {
        reallyCallCamel(payload, historyId, accountId, subType, integrationName, originalEventId);
        final long endTime = System.currentTimeMillis();
        // We only create a basic stub. The FromCamel filler will update it later
        NotificationHistory history = getHistoryStub(endpoint, item.getEvent(), endTime - startTime, historyId);
        return history;
    }
}
Also used : Endpoint(com.redhat.cloud.notifications.models.Endpoint) NotificationHistory(com.redhat.cloud.notifications.models.NotificationHistory) CamelProperties(com.redhat.cloud.notifications.models.CamelProperties) HashMap(java.util.HashMap) Map(java.util.Map)

Example 2 with CamelProperties

use of com.redhat.cloud.notifications.models.CamelProperties 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 3 with CamelProperties

use of com.redhat.cloud.notifications.models.CamelProperties in project notifications-backend by RedHatInsights.

the class EndpointResourceTest method addCamelEndpoint.

@Test
void addCamelEndpoint() {
    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());
    cAttr.setBasicAuthentication(new BasicAuthentication("testuser", "secret"));
    Map<String, String> extras = new HashMap<>();
    extras.put("template", "11");
    cAttr.setExtras(extras);
    Endpoint ep = new Endpoint();
    ep.setType(EndpointType.CAMEL);
    ep.setSubType("ansible");
    ep.setName("Push the camel through the needle's ear");
    ep.setDescription("How many humps has a camel?");
    ep.setEnabled(true);
    ep.setProperties(cAttr);
    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("ansible", endpoint.getString("sub_type"));
        // Todo: NOTIF-429 backward compatibility change - Remove soon.
        assertEquals("ansible", properties.getString("sub_type"));
        JsonObject extrasObject = properties.getJsonObject("extras");
        assertNotNull(extrasObject);
        String template = extrasObject.getString("template");
        assertEquals("11", template);
        JsonObject basicAuth = properties.getJsonObject("basic_authentication");
        assertNotNull(basicAuth);
        String user = basicAuth.getString("username");
        String pass = basicAuth.getString("password");
        assertEquals("testuser", user);
        assertEquals("secret", pass);
    } finally {
        given().header(identityHeader).when().delete("/endpoints/" + id).then().statusCode(204).extract().body().asString();
    }
}
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) BasicAuthentication(com.redhat.cloud.notifications.models.BasicAuthentication) JsonObject(io.vertx.core.json.JsonObject) 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)

Example 4 with CamelProperties

use of com.redhat.cloud.notifications.models.CamelProperties in project notifications-backend by RedHatInsights.

the class EndpointResourceTest method addBogusCamelEndpoint.

@Test
void addBogusCamelEndpoint() {
    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());
    cAttr.setBasicAuthentication(new BasicAuthentication("testuser", "secret"));
    Map<String, String> extras = new HashMap<>();
    extras.put("template", "11");
    cAttr.setExtras(extras);
    // This is bogus because it has no sub_type
    Endpoint ep = new Endpoint();
    ep.setType(EndpointType.CAMEL);
    ep.setName("Push the camel through the needle's ear");
    ep.setDescription("How many humps has a camel?");
    ep.setEnabled(true);
    ep.setProperties(cAttr);
    given().header(identityHeader).when().contentType(JSON).body(Json.encode(ep)).post("/endpoints").then().statusCode(400);
    endpointResource.obEnabled = true;
    given().header(identityHeader).when().contentType(JSON).body(Json.encode(ep)).post("/endpoints").then().statusCode(400);
    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) BasicAuthentication(com.redhat.cloud.notifications.models.BasicAuthentication) 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)

Example 5 with CamelProperties

use of com.redhat.cloud.notifications.models.CamelProperties 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

CamelProperties (com.redhat.cloud.notifications.models.CamelProperties)11 Endpoint (com.redhat.cloud.notifications.models.Endpoint)10 JsonObject (io.vertx.core.json.JsonObject)5 HashMap (java.util.HashMap)5 BasicAuthentication (com.redhat.cloud.notifications.models.BasicAuthentication)4 QuarkusTest (io.quarkus.test.junit.QuarkusTest)4 Header (io.restassured.http.Header)4 Test (org.junit.jupiter.api.Test)4 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)4 DbIsolatedTest (com.redhat.cloud.notifications.db.DbIsolatedTest)3 Transactional (javax.transaction.Transactional)3 RhIdPrincipal (com.redhat.cloud.notifications.auth.principal.rhid.RhIdPrincipal)2 MapConverter (com.redhat.cloud.notifications.db.converters.MapConverter)2 NotificationHistory (com.redhat.cloud.notifications.models.NotificationHistory)2 WebhookProperties (com.redhat.cloud.notifications.models.WebhookProperties)2 UUID (java.util.UUID)2 RolesAllowed (javax.annotation.security.RolesAllowed)2 BadRequestException (javax.ws.rs.BadRequestException)2 InternalServerErrorException (javax.ws.rs.InternalServerErrorException)2 NotFoundException (javax.ws.rs.NotFoundException)2