Ngsi2Client { public ListenableFuture<String> addSubscription(Subscription subscription) { ListenableFuture<ResponseEntity<Void>> s = request(HttpMethod.POST, UriComponentsBuilder.fromHttpUrl(baseURL).path("v2/subscriptions").toUriString(), subscription, Void.class); return new ListenableFutureAdapter<String, ResponseEntity<Void>>(s) { @Override protected String adapt(ResponseEntity<Void> result) throws ExecutionException { return extractId(result); } }; } 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 testAddSubscription_OK() throws Exception { HttpHeaders responseHeader = new HttpHeaders(); responseHeader.add("Location", "/v2/subscriptions/abcde98765"); mockServer.expect(requestTo(baseURL + "/v2/subscriptions")) .andExpect(method(HttpMethod.POST)) .andExpect(header("Content-Type", MediaType.APPLICATION_JSON_VALUE)) .andExpect(jsonPath("$.subject.entities[0].type").value("Room")) .andExpect(jsonPath("$.subject.condition.attributes[0]").value("temperature")) .andExpect(jsonPath("$.subject.condition.expression.q").value("temperature>40")) .andRespond(withNoContent().headers(responseHeader)); SubjectEntity subjectEntity = new SubjectEntity(); subjectEntity.setType(Optional.of("Room")); Condition condition = new Condition(); condition.setAttributes(Collections.singletonList("temperature")); condition.setExpression("q", "temperature>40"); SubjectSubscription subjectSubscription = new SubjectSubscription(Collections.singletonList(subjectEntity), condition); List<String> attributes = new ArrayList<>(); attributes.add("temperature"); attributes.add("humidity"); Notification notification = new Notification(attributes, new URL("http: notification.setThrottling(Optional.of(new Long(5))); Subscription subscription = new Subscription(); subscription.setSubject(subjectSubscription); subscription.setNotification(notification); subscription.setExpires(Instant.parse("2016-04-05T14:00:00.20Z")); assertEquals("abcde98765", ngsiClient.addSubscription(subscription).get()); }
@Test public void testAddSubscription_returnEmpty() throws Exception { HttpHeaders responseHeader = new HttpHeaders(); responseHeader.add("Location", ""); mockServer.expect(requestTo(baseURL + "/v2/subscriptions")) .andExpect(method(HttpMethod.POST)) .andExpect(header("Content-Type", MediaType.APPLICATION_JSON_VALUE)) .andExpect(jsonPath("$.subject.entities[0].type").value("Room")) .andExpect(jsonPath("$.subject.condition.attributes[0]").value("temperature")) .andExpect(jsonPath("$.subject.condition.expression.q").value("temperature>40")) .andRespond(withNoContent().headers(responseHeader)); SubjectEntity subjectEntity = new SubjectEntity(); subjectEntity.setType(Optional.of("Room")); Condition condition = new Condition(); condition.setAttributes(Collections.singletonList("temperature")); condition.setExpression("q", "temperature>40"); SubjectSubscription subjectSubscription = new SubjectSubscription(Collections.singletonList(subjectEntity), condition); List<String> attributes = new ArrayList<>(); attributes.add("temperature"); attributes.add("humidity"); Notification notification = new Notification(attributes, new URL("http: notification.setThrottling(Optional.of(new Long(5))); Subscription subscription = new Subscription(); subscription.setSubject(subjectSubscription); subscription.setNotification(notification); subscription.setExpires(Instant.parse("2016-04-05T14:00:00.20Z")); assertEquals("", ngsiClient.addSubscription(subscription).get()); }