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