@Test @RunAsClient @InSequence(3) public void testBadSubTreeWillReturn404() { when().get("/metrics/bad-tree").then().statusCode(404); }
@Test public void testEvictTenantFromBuilding() throws InterruptedException { // given final String buildingId = "testEvictTenantFromBuilding-id1"; final String tenantId1 = "testEvictTenantFromBuilding-tid1"; final String tenant2Name = "testEvictTenantFromBuilding-tenant2"; buildingEntityGateway.save(BuildingFactory.create().createBuilding(buildingId, "testEvictTenantFromBuilding-building1", Arrays.asList( TenantFactory.create().createTenant(tenantId1, "testEvictTenantFromBuilding-tenant1"), TenantFactory.create().createTenant("testEvictTenantFromBuilding-tid2", tenant2Name)))); // expect when() .get("/building/{0}", buildingId) .then() .statusCode(200) .body("tenants.size()", is(2)); when() .delete("/building/{0}/tenant/{1}", buildingId, tenantId1) .then() .statusCode(200); // and when() .get("/building/{0}", buildingId) .then() .statusCode(200) .body("tenants.size()", is(1)) .body("tenants[0].name", is(tenant2Name)); } }
@Test public void testGetBuildingsWithNoBuildings() throws InterruptedException { // expect when() .get("/building?nameStartsWith=nehnehneh") .then() .statusCode(200) .body("size()", is(0)); }
@Test public void testGetBuilding() throws InterruptedException { // given final String building1 = UUID.randomUUID().toString(); final String id1 = UUID.randomUUID().toString(); buildingEntityGateway.save(BuildingFactory.create().createBuilding(id1, building1, Arrays.asList(TenantFactory.create().createTenant("tid1", "tenant1"), TenantFactory.create().createTenant("tid2", "tenant2")))); // expect // expect when() .get("/building/" + id1) .then() .statusCode(200) .body("id", is(id1)) .body("name", is(building1)) .body("tenants.size()", is(2)); }
public void shouldListDefinitions(List<String> names, // Class clazz, // Function<List<String>, Map<String, ? extends Definition>> provider, // DefinitionType wantedType, // String expectedType) throws IOException { // given BDDMockito.given(delegate.getDefinitionsMapByType(clazz)) // .willReturn(provider.apply(names)); // when final Response response = when().get(getVersionPrefix() + "/definitions/" + wantedType).andReturn(); // then assertEquals(OK.value(), response.getStatusCode()); List<DefinitionDTO> actual = objectMapper.readValue(response.asInputStream(), new TypeReference<List<DefinitionDTO>>() { }); assertEquals(names.size(), actual.size()); actual.forEach(d -> { assertEquals(expectedType, d.getType()); assertTrue(names.contains(d.getName().substring("mock ".length()))); // it's expected }); }
public void shouldFilterComponentsByExecutionEngine(ExecutionEngine executionEngine, int expectedResults) throws IOException { // given Map<String, ComponentDefinition> definitions = getComponentsDefinitions(); BDDMockito.given(delegate.getDefinitionsMapByType(ComponentDefinition.class)) // .willReturn(definitions); // when final Response response = when() .get(getVersionPrefix() + "/definitions/components?executionEngine=" + executionEngine.name()).andReturn(); // then assertEquals(OK.value(), response.getStatusCode()); List<DefinitionDTO> actual = objectMapper.readValue(response.asInputStream(), new TypeReference<List<DefinitionDTO>>() { }); assertEquals(expectedResults, actual.size()); assertEquals(expectedResults, actual.stream().filter(dto -> dto.getExecutionEngines().contains(executionEngine.name())).count()); }
public void shouldFilterComponentsByTypologyAndExecutionEngine(ConnectorTypology wantedTypology, ExecutionEngine executionEngine, int expectedResults) throws IOException { // given Map<String, ComponentDefinition> definitions = getComponentsDefinitions(); BDDMockito.given(delegate.getDefinitionsMapByType(ComponentDefinition.class)) // .willReturn(definitions); // when final Response response = when().get(getVersionPrefix() + "/definitions/components?typology=" + wantedTypology + "&executionEngine=" + executionEngine.name()).andReturn(); // then assertEquals(OK.value(), response.getStatusCode()); List<DefinitionDTO> actual = objectMapper.readValue(response.asInputStream(), new TypeReference<List<DefinitionDTO>>() { }); assertEquals(expectedResults, actual.size()); assertEquals(expectedResults, actual.stream().filter(dto -> dto.getTypologies().contains(wantedTypology.name())) .filter(dto -> dto.getExecutionEngines().contains(executionEngine.name())).count()); }
@Test public void testGetBuildingsByName() throws InterruptedException { // given String nameBuilding1 = UUID.randomUUID().toString(); String nameBuilding2 = UUID.randomUUID().toString(); buildingEntityGateway.save(BuildingFactory.create().createBuilding("id1", nameBuilding1)); buildingEntityGateway.save(BuildingFactory.create().createBuilding("id2", nameBuilding2)); // expect when() .get("/building?nameStartsWith=" + nameBuilding1) .then() .statusCode(200) .body("size()", greaterThanOrEqualTo(1)); }
@Test public void shouldHaveIconKeyIfPresent() throws Exception { // given Map<String, ComponentDefinition> definitions = getComponentsDefinitions(); BDDMockito.given(delegate.getDefinitionsMapByType(ComponentDefinition.class)) // .willReturn(definitions); // then when().get(getVersionPrefix() + "/definitions/components").then() // .statusCode(OK.value()) // .body("iconKey", hasSize(14)) // total including nulls .body("iconKey.findAll { iconKey -> iconKey != null }", hasSize(5)); // total non-null }
public void shouldFilterComponentsByTypology(ConnectorTypology wantedTypology, int expectedResults) throws IOException { // given Map<String, ComponentDefinition> definitions = getComponentsDefinitions(); BDDMockito.given(delegate.getDefinitionsMapByType(ComponentDefinition.class)) // .willReturn(definitions); // when final Response response = when().get(getVersionPrefix() + "/definitions/components?typology=" + wantedTypology.name()) .andReturn(); // then assertEquals(OK.value(), response.getStatusCode()); List<DefinitionDTO> actual = objectMapper.readValue(response.asInputStream(), new TypeReference<List<DefinitionDTO>>() { }); assertEquals(expectedResults, actual.size()); assertEquals(expectedResults, actual.stream().filter(dto -> dto.getTypologies().contains(wantedTypology.name())) // it's // a // source .count()); }
public void shouldFilterComponentsByTag(String tag, int expectedResults) throws JsonParseException, JsonMappingException, IOException { Map<String, ComponentDefinition> definitions = getComponentsDefinitions(); BDDMockito.given(delegate.getDefinitionsMapByType(ComponentDefinition.class)) // .willReturn(definitions); // when final Response response = when().get(getVersionPrefix() + "/definitions/" + COMPONENT + "?tag=" + tag).andReturn(); // then assertEquals(OK.value(), response.getStatusCode()); List<DefinitionDTO> actual = objectMapper.readValue(response.asInputStream(), new TypeReference<List<DefinitionDTO>>() { }); assertEquals(expectedResults, actual.size()); }
@Test public void shouldNotFilterTypology() throws Exception { // given Map<String, ComponentDefinition> definitions = getComponentsDefinitions(); BDDMockito.given(delegate.getDefinitionsMapByType(ComponentDefinition.class)) // .willReturn(definitions); // when final Response response = when().get(getVersionPrefix() + "/definitions/components").andReturn(); // then assertEquals(OK.value(), response.getStatusCode()); List<DefinitionDTO> actual = objectMapper.readValue(response.asInputStream(), new TypeReference<List<DefinitionDTO>>() { }); assertEquals(14, actual.size()); }
@Test public void testGetAllBuildings() throws InterruptedException { // given buildingEntityGateway.save(BuildingFactory.create().createBuilding("id1", "building1")); buildingEntityGateway.save(BuildingFactory.create().createBuilding("id2", "building2")); // expect when() .get("/building") .then() .statusCode(200) .body("size()", greaterThan(2)); }
@Test public void testAddTenantToBuilding() throws InterruptedException { // given final String buildingId = "id1"; buildingEntityGateway.save(BuildingFactory.create().createBuilding(buildingId, "building1")); String payload = "{\"name\":\"testTenant\"}"; // expect when() .get("/building/{0}", buildingId) .then() .statusCode(200) .body("tenants.size()", is(0)); given() .body(payload) .contentType("application/json") .when() .post("/building/{0}/tenant", buildingId) .then() .statusCode(201); // and when() .get("/building/{0}", buildingId) .then() .statusCode(200) .body("tenants.size()", is(1)) .body("tenants[0].name", is("testTenant")); }
@Test public void testCreateBuilding() throws InterruptedException { // given String payload = "{\"name\":\"testBuilding\"}"; // expect String createdId = given() .body(payload) .contentType("application/json") .when() .post("/building") .then() .statusCode(201) .extract().header("X-Created-Id"); // and when() .get("/building/" + createdId) .then() .statusCode(200) .body("name", is("testBuilding")); }