Ngsi2Client { public ListenableFuture<Map<String, String>> getV2() { ListenableFuture<ResponseEntity<JsonNode>> responseFuture = request(HttpMethod.GET, baseURL + "v2", null, JsonNode.class); return new ListenableFutureAdapter<Map<String, String>, ResponseEntity<JsonNode>>(responseFuture) { @Override protected Map<String, String> adapt(ResponseEntity<JsonNode> result) throws ExecutionException { Map<String, String> services = new HashMap<>(); result.getBody().fields().forEachRemaining(entry -> services.put(entry.getKey(), entry.getValue().textValue())); return services; } }; } private  Ngsi2Client();  Ngsi2Client(AsyncRestTemplate asyncRestTemplate, String baseURL); ListenableFuture<Map<String, String>> getV2(); ListenableFuture<Paginated<Entity>> getEntities(Collection<String> ids, String idPattern,
            Collection<String> types, Collection<String> attrs,
            int offset, int limit, boolean count); ListenableFuture<Paginated<Entity>> getEntities(Collection<String> ids, String idPattern,
                                                           Collection<String> types, Collection<String> attrs,
                                                           String query, GeoQuery geoQuery,
                                                           Collection<String> orderBy,
                                                           int offset, int limit, boolean count); ListenableFuture<Void> addEntity(Entity entity); ListenableFuture<Entity> getEntity(String entityId, String type, Collection<String> attrs); ListenableFuture<Void> updateEntity(String entityId, String type, Map<String, Attribute> attributes, boolean append); ListenableFuture<Void> replaceEntity(String entityId, String type, Map<String, Attribute> attributes); ListenableFuture<Void> deleteEntity(String entityId, String type); ListenableFuture<Attribute> getAttribute(String entityId, String type, String attributeName); ListenableFuture<Void> updateAttribute(String entityId, String type, String attributeName, Attribute attribute); ListenableFuture<Attribute> deleteAttribute(String entityId, String type, String attributeName); ListenableFuture<Object> getAttributeValue(String entityId, String type, String attributeName); ListenableFuture<String> getAttributeValueAsString(String entityId, String type, String attributeName); ListenableFuture<Paginated<EntityType>> getEntityTypes(int offset, int limit, boolean count); ListenableFuture<EntityType> getEntityType(String entityType); ListenableFuture<List<Registration>> getRegistrations(); ListenableFuture<Void> addRegistration(Registration registration); ListenableFuture<Registration> getRegistration(String registrationId); ListenableFuture<Void> updateRegistration(String registrationId, Registration registration); ListenableFuture<Void> deleteRegistration(String registrationId); ListenableFuture<Paginated<Subscription>> getSubscriptions(int offset, int limit, boolean count); ListenableFuture<String> addSubscription(Subscription subscription); ListenableFuture<Subscription> getSubscription(String subscriptionId); ListenableFuture<Void> updateSubscription(String subscriptionId, Subscription subscription); ListenableFuture<Void> deleteSubscription(String subscriptionId); ListenableFuture<Void> bulkUpdate(BulkUpdateRequest bulkUpdateRequest); ListenableFuture<Paginated<Entity>> bulkQuery(BulkQueryRequest bulkQueryRequest, Collection<String> orderBy, int offset, int limit, boolean count); ListenableFuture<String[]> bulkRegister(BulkRegisterRequest bulkRegisterRequest); ListenableFuture<Paginated<Registration>> bulkDiscover(BulkQueryRequest bulkQueryRequest, int offset, int limit, boolean count); HttpHeaders getHttpHeaders();  }
@Test public void testGetV2_ServerError() throws Exception { thrown.expect(Ngsi2Exception.class); thrown.expectMessage("error: 500 | description: Internal Server Error | affectedItems: [item1, item2, item3]"); mockServer.expect(requestTo(baseURL + "/v2")) .andExpect(method(HttpMethod.GET)) .andRespond(withServerError().body(Utils.loadResource("json/error500Response.json"))); ngsiClient.getV2().get(); }
@Test public void testGetV2_ClientError() throws Exception { thrown.expect(Ngsi2Exception.class); thrown.expectMessage("error: 400 | description: Bad Request | affectedItems: []"); mockServer.expect(requestTo(baseURL + "/v2")) .andExpect(method(HttpMethod.GET)) .andRespond(withBadRequest().body(Utils.loadResource("json/error400Response.json"))); ngsiClient.getV2().get(); }
@Test(expected = HttpMessageNotReadableException.class) public void testGetV2_SyntaxError() throws Exception { mockServer.expect(requestTo(baseURL + "/v2")) .andExpect(method(HttpMethod.GET)) .andRespond(withSuccess("{/", MediaType.APPLICATION_JSON)); ngsiClient.getV2().get(); }
@Test public void testGetV2_OK() throws Exception { mockServer.expect(requestTo(baseURL + "/v2")) .andExpect(method(HttpMethod.GET)) .andExpect(header(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON_VALUE)) .andRespond(withSuccess(Utils.loadResource("json/getV2Response.json"), MediaType.APPLICATION_JSON)); Map<String, String> endpoints = ngsiClient.getV2().get(); assertEquals(4, endpoints.size()); assertNotNull("/v2/entities", endpoints.get("entities_url")); }