Search in sources :

Example 26 with Endpoint

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

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

the class EndpointResourceTest method addEndpoints.

private void addEndpoints(int count, Header identityHeader) {
    for (int i = 0; i < count; i++) {
        // Add new endpoints
        WebhookProperties properties = new WebhookProperties();
        properties.setMethod(HttpType.POST);
        properties.setDisableSslVerification(false);
        properties.setSecretToken("my-super-secret-token");
        properties.setUrl(getMockServerUrl() + "/" + i);
        Endpoint ep = new Endpoint();
        ep.setType(EndpointType.WEBHOOK);
        ep.setName(String.format("Endpoint %d", i));
        ep.setDescription("Try to find me!");
        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"));
    }
}
Also used : Response(io.restassured.response.Response) Endpoint(com.redhat.cloud.notifications.models.Endpoint) WebhookProperties(com.redhat.cloud.notifications.models.WebhookProperties) JsonObject(io.vertx.core.json.JsonObject) Endpoint(com.redhat.cloud.notifications.models.Endpoint)

Example 28 with Endpoint

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

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

the class EndpointResourceTest method testEndpointUpdates.

@Test
void testEndpointUpdates() {
    String tenant = "updates";
    String orgId = "updates2";
    String userName = "testEndpointUpdates";
    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).contentType(JSON).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"));
    // Update the endpoint
    responsePointSingle.put("name", "endpoint found");
    JsonObject attrSingle = responsePointSingle.getJsonObject("properties");
    attrSingle.mapTo(WebhookProperties.class);
    attrSingle.put("secret_token", "not-so-secret-anymore");
    // Update without payload
    given().header(identityHeader).contentType(JSON).when().put(String.format("/endpoints/%s", responsePointSingle.getString("id"))).then().statusCode(400);
    // With payload
    given().header(identityHeader).contentType(JSON).when().body(responsePointSingle.encode()).put(String.format("/endpoints/%s", responsePointSingle.getString("id"))).then().statusCode(200).contentType(TEXT);
    // Fetch single one again to see that the updates were done
    JsonObject updatedEndpoint = fetchSingle(responsePointSingle.getString("id"), identityHeader);
    JsonObject attrSingleUpdated = updatedEndpoint.getJsonObject("properties");
    attrSingleUpdated.mapTo(WebhookProperties.class);
    assertEquals("endpoint found", updatedEndpoint.getString("name"));
    assertEquals("not-so-secret-anymore", attrSingleUpdated.getString("secret_token"));
}
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)

Example 30 with Endpoint

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

the class EndpointResourceTest method testConnectionCount.

@Test
void testConnectionCount() {
    String tenant = "count";
    String orgId = "count2";
    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}}"));
    for (int i = 0; i < 200; i++) {
        // 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" + i);
        ep.setDescription("needle in the haystack" + i);
        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
        given().header(identityHeader).when().get("/endpoints").then().statusCode(200).contentType(JSON).extract().response();
    }
}
Also used : Response(io.restassured.response.Response) 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) 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)

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