Search in sources :

Example 36 with Endpoint

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

Example 37 with Endpoint

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

the class EndpointResourceTest method testSortingOrder.

@Test
void testSortingOrder() {
    String tenant = "testSortingOrder";
    String orgId = "testSortingOrder2";
    String userName = "user";
    String identityHeaderValue = TestHelpers.encodeRHIdentityInfo(tenant, orgId, userName);
    Header identityHeader = TestHelpers.createRHIdentityHeader(identityHeaderValue);
    MockServerConfig.addMockRbacAccess(identityHeaderValue, MockServerConfig.RbacAccess.FULL_ACCESS);
    // Create 50 test-ones with sanely sortable name & enabled & disabled & type
    int[] stats = helpers.createTestEndpoints(tenant, 50);
    int disableCount = stats[1];
    int webhookCount = stats[2];
    Response response = given().header(identityHeader).when().get("/endpoints?limit=100").then().statusCode(200).contentType(JSON).extract().response();
    EndpointPage endpointPage = Json.decodeValue(response.getBody().asString(), EndpointPage.class);
    Endpoint[] endpoints = endpointPage.getData().toArray(new Endpoint[0]);
    assertEquals(stats[0], endpoints.length);
    response = given().header(identityHeader).queryParam("sort_by", "enabled").when().get("/endpoints?limit=100").then().statusCode(200).contentType(JSON).extract().response();
    endpointPage = Json.decodeValue(response.getBody().asString(), EndpointPage.class);
    endpoints = endpointPage.getData().toArray(new Endpoint[0]);
    assertFalse(endpoints[0].isEnabled());
    assertFalse(endpoints[disableCount - 1].isEnabled());
    assertTrue(endpoints[disableCount].isEnabled());
    assertTrue(endpoints[stats[0] - 1].isEnabled());
    response = given().header(identityHeader).queryParam("sort_by", "name:desc").queryParam("limit", "50").queryParam("offset", stats[0] - 20).when().get("/endpoints?limit=100").then().statusCode(200).contentType(JSON).extract().response();
    endpointPage = Json.decodeValue(response.getBody().asString(), EndpointPage.class);
    endpoints = endpointPage.getData().toArray(new Endpoint[0]);
    assertEquals(20, endpoints.length);
    assertEquals("Endpoint 1", endpoints[endpoints.length - 1].getName());
    assertEquals("Endpoint 10", endpoints[endpoints.length - 2].getName());
    assertEquals("Endpoint 27", endpoints[0].getName());
}
Also used : Response(io.restassured.response.Response) EndpointPage(com.redhat.cloud.notifications.routers.models.EndpointPage) Header(io.restassured.http.Header) Endpoint(com.redhat.cloud.notifications.models.Endpoint) Endpoint(com.redhat.cloud.notifications.models.Endpoint) 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 38 with Endpoint

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

the class EndpointResourceTest method testEndpointValidation.

@Test
void testEndpointValidation() {
    String tenant = "validation";
    String orgId = "validation2";
    String userName = "testEndpointValidation";
    String identityHeaderValue = TestHelpers.encodeRHIdentityInfo(tenant, orgId, userName);
    Header identityHeader = TestHelpers.createRHIdentityHeader(identityHeaderValue);
    MockServerConfig.addMockRbacAccess(identityHeaderValue, MockServerConfig.RbacAccess.FULL_ACCESS);
    // Add new endpoint without properties
    Endpoint ep = new Endpoint();
    ep.setType(EndpointType.WEBHOOK);
    ep.setName("endpoint with missing properties");
    ep.setDescription("Destined to fail");
    ep.setEnabled(true);
    expectReturn400(identityHeader, ep);
    WebhookProperties properties = new WebhookProperties();
    properties.setMethod(HttpType.POST);
    properties.setDisableSslVerification(false);
    properties.setSecretToken("my-super-secret-token");
    properties.setUrl(getMockServerUrl());
    // Test with properties, but without endpoint type
    ep.setProperties(properties);
    ep.setType(null);
    expectReturn400(identityHeader, ep);
    // Test with incorrect webhook properties
    ep.setType(EndpointType.WEBHOOK);
    ep.setName("endpoint with incorrect webhook properties");
    properties.setMethod(null);
    expectReturn400(identityHeader, ep);
    // Type and attributes don't match
    properties.setMethod(HttpType.POST);
    ep.setType(EndpointType.EMAIL_SUBSCRIPTION);
    expectReturn400(identityHeader, ep);
    ep.setName("endpoint with subtype too long");
    ep.setType(EndpointType.CAMEL);
    ep.setSubType("something-longer-than-20-chars");
    expectReturn400(identityHeader, ep);
}
Also used : Header(io.restassured.http.Header) Endpoint(com.redhat.cloud.notifications.models.Endpoint) WebhookProperties(com.redhat.cloud.notifications.models.WebhookProperties) 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 39 with Endpoint

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

Example 40 with Endpoint

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

the class EndpointResourceTest method testEndpointAdding.

@Test
void testEndpointAdding() {
    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);
    // Test empty tenant
    given().header(identityHeader).when().get("/endpoints").then().statusCode(// TODO Maybe 204 here instead?
    200).contentType(JSON).body(is("{\"data\":[],\"links\":{},\"meta\":{\"count\":0}}"));
    // Add new endpoints
    WebhookProperties properties = new WebhookProperties();
    properties.setMethod(HttpType.POST);
    properties.setDisableSslVerification(false);
    properties.setSecretToken("my-super-secret-token");
    properties.setUrl(getMockServerUrl());
    Endpoint ep = new Endpoint();
    ep.setType(EndpointType.WEBHOOK);
    ep.setName("endpoint to find");
    ep.setDescription("needle in the haystack");
    ep.setEnabled(true);
    ep.setProperties(properties);
    Response response = given().header(identityHeader).when().contentType(JSON).body(Json.encode(ep)).post("/endpoints").then().statusCode(200).contentType(JSON).extract().response();
    JsonObject responsePoint = new JsonObject(response.getBody().asString());
    responsePoint.mapTo(Endpoint.class);
    assertNotNull(responsePoint.getString("id"));
    // Fetch the list
    response = given().header(identityHeader).when().get("/endpoints").then().statusCode(200).contentType(JSON).extract().response();
    EndpointPage endpointPage = Json.decodeValue(response.getBody().asString(), EndpointPage.class);
    List<Endpoint> endpoints = endpointPage.getData();
    assertEquals(1, endpoints.size());
    // Fetch single endpoint also and verify
    JsonObject responsePointSingle = fetchSingle(responsePoint.getString("id"), identityHeader);
    assertNotNull(responsePoint.getJsonObject("properties"));
    assertTrue(responsePointSingle.getBoolean("enabled"));
    // Disable and fetch
    String body = given().header(identityHeader).when().delete("/endpoints/" + responsePoint.getString("id") + "/enable").then().statusCode(204).extract().body().asString();
    assertEquals(0, body.length());
    responsePointSingle = fetchSingle(responsePoint.getString("id"), identityHeader);
    assertNotNull(responsePoint.getJsonObject("properties"));
    assertFalse(responsePointSingle.getBoolean("enabled"));
    // Enable and fetch
    given().header(identityHeader).when().put("/endpoints/" + responsePoint.getString("id") + "/enable").then().statusCode(200).contentType(TEXT).contentType(ContentType.TEXT);
    responsePointSingle = fetchSingle(responsePoint.getString("id"), identityHeader);
    assertNotNull(responsePoint.getJsonObject("properties"));
    assertTrue(responsePointSingle.getBoolean("enabled"));
    // Delete
    body = given().header(identityHeader).when().delete("/endpoints/" + responsePoint.getString("id")).then().statusCode(204).extract().body().asString();
    assertEquals(0, body.length());
    // Fetch single
    given().header(identityHeader).when().get("/endpoints/" + responsePoint.getString("id")).then().statusCode(404).contentType(JSON);
    // Fetch all, nothing should be left
    given().header(identityHeader).when().get("/endpoints").then().statusCode(200).contentType(JSON).body(is("{\"data\":[],\"links\":{},\"meta\":{\"count\":0}}"));
}
Also used : Response(io.restassured.response.Response) EndpointPage(com.redhat.cloud.notifications.routers.models.EndpointPage) Header(io.restassured.http.Header) Endpoint(com.redhat.cloud.notifications.models.Endpoint) WebhookProperties(com.redhat.cloud.notifications.models.WebhookProperties) 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)

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