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