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);
}
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);
}
}
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;
}
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());
}
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);
}
Aggregations