Search in sources :

Example 31 with Endpoint

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

the class EndpointResourceTest method testAddEndpointEmailSubscription.

@Test
void testAddEndpointEmailSubscription() {
    String tenant = "adding-email-subscription";
    String orgId = "adding-email-subscription2";
    String userName = "user";
    String identityHeaderValue = TestHelpers.encodeRHIdentityInfo(tenant, orgId, userName);
    Header identityHeader = TestHelpers.createRHIdentityHeader(identityHeaderValue);
    MockServerConfig.addMockRbacAccess(identityHeaderValue, MockServerConfig.RbacAccess.FULL_ACCESS);
    // EmailSubscription can't be created
    EmailSubscriptionProperties properties = new EmailSubscriptionProperties();
    Endpoint ep = new Endpoint();
    ep.setType(EndpointType.EMAIL_SUBSCRIPTION);
    ep.setName("Endpoint: EmailSubscription");
    ep.setDescription("Subscribe!");
    ep.setEnabled(true);
    ep.setProperties(properties);
    String stringResponse = given().header(identityHeader).when().contentType(JSON).body(Json.encode(ep)).post("/endpoints").then().statusCode(400).extract().asString();
    assertSystemEndpointTypeError(stringResponse, EndpointType.EMAIL_SUBSCRIPTION);
    RequestEmailSubscriptionProperties requestProps = new RequestEmailSubscriptionProperties();
    // EmailSubscription can be fetch from the properties
    Response response = given().header(identityHeader).when().contentType(JSON).body(Json.encode(requestProps)).post("/endpoints/system/email_subscription").then().statusCode(200).contentType(JSON).extract().response();
    JsonObject responsePoint = new JsonObject(response.getBody().asString());
    responsePoint.mapTo(Endpoint.class);
    assertNotNull(responsePoint.getString("id"));
    // It is always enabled
    assertEquals(true, responsePoint.getBoolean("enabled"));
    // Calling again yields the same endpoint id
    String defaultEndpointId = responsePoint.getString("id");
    response = given().header(identityHeader).when().contentType(JSON).body(Json.encode(requestProps)).post("/endpoints/system/email_subscription").then().statusCode(200).contentType(JSON).extract().response();
    responsePoint = new JsonObject(response.getBody().asString());
    responsePoint.mapTo(Endpoint.class);
    assertEquals(defaultEndpointId, responsePoint.getString("id"));
    // Different properties are different endpoints
    Set<String> endpointIds = new HashSet<>();
    endpointIds.add(defaultEndpointId);
    requestProps.setOnlyAdmins(true);
    response = given().header(identityHeader).when().contentType(JSON).body(Json.encode(requestProps)).post("/endpoints/system/email_subscription").then().statusCode(200).contentType(JSON).extract().response();
    responsePoint = new JsonObject(response.getBody().asString());
    responsePoint.mapTo(Endpoint.class);
    assertFalse(endpointIds.contains(responsePoint.getString("id")));
    endpointIds.add(responsePoint.getString("id"));
    response = given().header(identityHeader).when().contentType(JSON).body(Json.encode(requestProps)).post("/endpoints/system/email_subscription").then().statusCode(200).contentType(JSON).extract().response();
    responsePoint = new JsonObject(response.getBody().asString());
    responsePoint.mapTo(Endpoint.class);
    assertTrue(endpointIds.contains(responsePoint.getString("id")));
    // It is not possible to delete it
    stringResponse = given().header(identityHeader).when().delete("/endpoints/" + defaultEndpointId).then().statusCode(400).extract().asString();
    assertSystemEndpointTypeError(stringResponse, EndpointType.EMAIL_SUBSCRIPTION);
    // It is not possible to disable or enable it
    stringResponse = given().header(identityHeader).when().delete("/endpoints/" + defaultEndpointId + "/enable").then().statusCode(400).extract().asString();
    assertSystemEndpointTypeError(stringResponse, EndpointType.EMAIL_SUBSCRIPTION);
    stringResponse = given().header(identityHeader).when().put("/endpoints/" + defaultEndpointId + "/enable").then().statusCode(400).extract().asString();
    assertSystemEndpointTypeError(stringResponse, EndpointType.EMAIL_SUBSCRIPTION);
    // It is not possible to update it
    stringResponse = given().header(identityHeader).contentType(JSON).body(Json.encode(ep)).when().put("/endpoints/" + defaultEndpointId).then().statusCode(400).extract().asString();
    assertSystemEndpointTypeError(stringResponse, EndpointType.EMAIL_SUBSCRIPTION);
    // It is not possible to update it to other type
    ep.setType(EndpointType.WEBHOOK);
    WebhookProperties webhookProperties = new WebhookProperties();
    webhookProperties.setMethod(HttpType.POST);
    webhookProperties.setDisableSslVerification(false);
    webhookProperties.setSecretToken("my-super-secret-token");
    webhookProperties.setUrl(getMockServerUrl());
    ep.setProperties(webhookProperties);
    stringResponse = given().header(identityHeader).contentType(JSON).body(Json.encode(ep)).when().put("/endpoints/" + defaultEndpointId).then().statusCode(400).extract().asString();
    assertSystemEndpointTypeError(stringResponse, EndpointType.EMAIL_SUBSCRIPTION);
}
Also used : Response(io.restassured.response.Response) RequestEmailSubscriptionProperties(com.redhat.cloud.notifications.routers.models.RequestEmailSubscriptionProperties) Header(io.restassured.http.Header) Endpoint(com.redhat.cloud.notifications.models.Endpoint) RequestEmailSubscriptionProperties(com.redhat.cloud.notifications.routers.models.RequestEmailSubscriptionProperties) EmailSubscriptionProperties(com.redhat.cloud.notifications.models.EmailSubscriptionProperties) WebhookProperties(com.redhat.cloud.notifications.models.WebhookProperties) JsonObject(io.vertx.core.json.JsonObject) HashSet(java.util.HashSet) 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 32 with Endpoint

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

the class WebhookTest method testRetry.

private void testRetry(boolean shouldSucceedEventually) {
    String url = getMockServerUrl() + "/foobar";
    AtomicInteger callsCounter = new AtomicInteger();
    ExpectationResponseCallback expectationResponseCallback = request -> {
        if (callsCounter.incrementAndGet() == MAX_RETRY_ATTEMPTS && shouldSucceedEventually) {
            return response().withStatusCode(200);
        } else {
            return response().withStatusCode(500);
        }
    };
    HttpRequest mockServerRequest = getMockHttpRequest(expectationResponseCallback);
    try {
        Action action = buildWebhookAction();
        Event event = new Event();
        event.setAction(action);
        Endpoint ep = buildWebhookEndpoint(url);
        List<NotificationHistory> process = webhookTypeProcessor.process(event, List.of(ep));
        NotificationHistory history = process.get(0);
        assertEquals(shouldSucceedEventually, history.isInvocationResult());
        assertEquals(MAX_RETRY_ATTEMPTS, callsCounter.get());
    } finally {
        // Remove expectations
        MockServerLifecycleManager.getClient().clear(mockServerRequest);
    }
}
Also used : Assertions.fail(org.junit.jupiter.api.Assertions.fail) HttpType(com.redhat.cloud.notifications.models.HttpType) HttpRequest(org.mockserver.model.HttpRequest) Endpoint(com.redhat.cloud.notifications.models.Endpoint) WebhookTypeProcessor(com.redhat.cloud.notifications.processors.webhooks.WebhookTypeProcessor) TestLifecycleManager(com.redhat.cloud.notifications.TestLifecycleManager) LocalDateTime(java.time.LocalDateTime) NotificationHistory(com.redhat.cloud.notifications.models.NotificationHistory) MockServerLifecycleManager(com.redhat.cloud.notifications.MockServerLifecycleManager) QuarkusTest(io.quarkus.test.junit.QuarkusTest) ArrayList(java.util.ArrayList) MockServerLifecycleManager.getMockServerUrl(com.redhat.cloud.notifications.MockServerLifecycleManager.getMockServerUrl) Inject(javax.inject.Inject) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Event(com.redhat.cloud.notifications.models.Event) WebhookProperties(com.redhat.cloud.notifications.models.WebhookProperties) 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) QuarkusTestResource(io.quarkus.test.common.QuarkusTestResource) ExpectationResponseCallback(org.mockserver.mock.action.ExpectationResponseCallback) Test(org.junit.jupiter.api.Test) JsonArray(io.vertx.core.json.JsonArray) List(java.util.List) EndpointType(com.redhat.cloud.notifications.models.EndpointType) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) Metadata(com.redhat.cloud.notifications.ingress.Metadata) Action(com.redhat.cloud.notifications.ingress.Action) HttpResponse.response(org.mockserver.model.HttpResponse.response) HttpRequest(org.mockserver.model.HttpRequest) Action(com.redhat.cloud.notifications.ingress.Action) Endpoint(com.redhat.cloud.notifications.models.Endpoint) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ExpectationResponseCallback(org.mockserver.mock.action.ExpectationResponseCallback) NotificationHistory(com.redhat.cloud.notifications.models.NotificationHistory) Event(com.redhat.cloud.notifications.models.Event)

Example 33 with Endpoint

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

the class WebhookTest method buildWebhookEndpoint.

private static Endpoint buildWebhookEndpoint(String url) {
    WebhookProperties properties = new WebhookProperties();
    properties.setMethod(HttpType.POST);
    properties.setUrl(url);
    Endpoint ep = new Endpoint();
    ep.setType(EndpointType.WEBHOOK);
    ep.setName("positive feeling");
    ep.setDescription("needle in the haystack");
    ep.setEnabled(true);
    ep.setProperties(properties);
    return ep;
}
Also used : Endpoint(com.redhat.cloud.notifications.models.Endpoint) WebhookProperties(com.redhat.cloud.notifications.models.WebhookProperties)

Example 34 with Endpoint

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

the class BehaviorGroupRepositoryTest method testUpdateBehaviorGroupActionsWithWrongAccountId.

@Test
void testUpdateBehaviorGroupActionsWithWrongAccountId() {
    Bundle bundle = resourceHelpers.createBundle();
    BehaviorGroup behaviorGroup = resourceHelpers.createBehaviorGroup(DEFAULT_ACCOUNT_ID, "displayName", bundle.getId());
    Endpoint endpoint = resourceHelpers.createEndpoint(DEFAULT_ACCOUNT_ID, WEBHOOK);
    updateAndCheckBehaviorGroupActions("unknownAccountId", bundle.getId(), behaviorGroup.getId(), NOT_FOUND, endpoint.getId());
}
Also used : Endpoint(com.redhat.cloud.notifications.models.Endpoint) Bundle(com.redhat.cloud.notifications.models.Bundle) BehaviorGroup(com.redhat.cloud.notifications.models.BehaviorGroup) QuarkusTest(io.quarkus.test.junit.QuarkusTest) DbIsolatedTest(com.redhat.cloud.notifications.db.DbIsolatedTest) Test(org.junit.jupiter.api.Test)

Example 35 with Endpoint

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

the class EndpointRepositoryTest method queryBuilderTest.

@Test
public void queryBuilderTest() {
    TypedQuery<Endpoint> query = mock(TypedQuery.class);
    // types with subtype and without it
    EndpointRepository.queryBuilderEndpointsPerType(null, null, Set.of(new CompositeEndpointType(EndpointType.WEBHOOK), new CompositeEndpointType(EndpointType.CAMEL, "splunk")), null).build((hql, endpointClass) -> {
        assertEquals("SELECT e FROM Endpoint e WHERE e.accountId IS NULL AND (e.compositeType.type IN (:endpointType) OR e.compositeType IN (:compositeTypes))", hql);
        return query;
    });
    verify(query, times(2)).setParameter((String) any(), any());
    verifyNoMoreInteractions(query);
    clearInvocations(query);
    // without sub-types
    EndpointRepository.queryBuilderEndpointsPerType(null, null, Set.of(new CompositeEndpointType(EndpointType.WEBHOOK)), null).build((hql, endpointClass) -> {
        assertEquals("SELECT e FROM Endpoint e WHERE e.accountId IS NULL AND (e.compositeType.type IN (:endpointType))", hql);
        return query;
    });
    verify(query, times(1)).setParameter((String) any(), any());
    verifyNoMoreInteractions(query);
    clearInvocations(query);
    // with sub-types
    EndpointRepository.queryBuilderEndpointsPerType(null, null, Set.of(new CompositeEndpointType(EndpointType.CAMEL, "splunk")), null).build((hql, endpointClass) -> {
        assertEquals("SELECT e FROM Endpoint e WHERE e.accountId IS NULL AND (e.compositeType IN (:compositeTypes))", hql);
        return query;
    });
    verify(query, times(1)).setParameter((String) any(), any());
    verifyNoMoreInteractions(query);
    clearInvocations(query);
}
Also used : CompositeEndpointType(com.redhat.cloud.notifications.models.CompositeEndpointType) Endpoint(com.redhat.cloud.notifications.models.Endpoint) Test(org.junit.jupiter.api.Test)

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