@Test public void postQueryContextWithoutProvidingApplication() throws Exception { when(providingApplication.hasNext()).thenReturn(false); when(localRegistrations.findProvidingApplication(any(), any())).thenReturn(providingApplication); when(ngsiClient.queryContext(any(), any(), any())).thenReturn(queryContextResponseListenableFuture); mockMvc.perform(post("/v1/queryContext") .content(json(mapper, createQueryContextTemperature())) .contentType(MediaType.APPLICATION_JSON)) .andExpect(status().isOk()) .andExpect(MockMvcResultMatchers.jsonPath("$.errorCode").doesNotExist()) .andExpect(MockMvcResultMatchers.jsonPath("$.contextResponses[0].contextElement.id").value("S1")) .andExpect(MockMvcResultMatchers.jsonPath("$.contextResponses[0].contextElement.type").value("TempSensor")) .andExpect(MockMvcResultMatchers.jsonPath("$.contextResponses[0].contextElement.attributes[0].name").value("temp")) .andExpect(MockMvcResultMatchers.jsonPath("$.contextResponses[0].contextElement.attributes[0].type").value("float")) .andExpect(MockMvcResultMatchers.jsonPath("$.contextResponses[0].contextElement.attributes[0].value").value("15.5")) .andExpect(MockMvcResultMatchers.jsonPath("$.contextResponses[0].statusCode.code").value("200")); verify(localRegistrations).findProvidingApplication(entityIdArgumentCaptor.capture(), attributeArgumentCaptor.capture()); assertEquals("S*", entityIdArgumentCaptor.getValue().getId()); assertEquals("TempSensor", entityIdArgumentCaptor.getValue().getType()); assertTrue(entityIdArgumentCaptor.getValue().getIsPattern()); assertEquals(1, attributeArgumentCaptor.getValue().size()); assertTrue(attributeArgumentCaptor.getValue().contains("temp")); ArgumentCaptor<QueryContext> queryContextArg = ArgumentCaptor.forClass(QueryContext.class); String urlProvider = "http: ArgumentCaptor<HttpHeaders> httpHeadersArg = ArgumentCaptor.forClass(HttpHeaders.class); verify(queryContextResponseListenableFuture, atLeastOnce()).get(); verify(ngsiClient, atLeastOnce()).getRequestHeaders(eq("http: verify(configuration, atLeastOnce()).addRemoteHeaders(any()); verify(ngsiClient, atLeastOnce()).queryContext(eq(urlProvider), httpHeadersArg.capture(), queryContextArg.capture()); assertEquals(1, queryContextArg.getValue().getEntityIdList().size()); assertEquals("S*", queryContextArg.getValue().getEntityIdList().get(0).getId()); }
@Override public QueryContextResponse queryContext(final QueryContext query) throws ExecutionException, InterruptedException, MissingRemoteBrokerException { logger.debug("<= queryContext on entities: {}", query.getEntityIdList().toString()); Set<String> attributes = new HashSet<>(); if (query.getAttributeList() != null) { attributes.addAll(query.getAttributeList()); } Iterator<URI> providingApplication = localRegistrations.findProvidingApplication(query.getEntityIdList().get(0), attributes); if (providingApplication.hasNext()) { final String providerUrl = providingApplication.next().toString(); HttpHeaders httpHeaders = ngsiClient.getRequestHeaders(providerUrl); logger.debug("=> queryContext forwarded to : {} with Content-Type {}", providerUrl, httpHeaders.getContentType()); return ngsiClient.queryContext(providerUrl, httpHeaders, query).get(); } String brokerUrl = configuration.getRemoteUrl(); if (brokerUrl == null || brokerUrl.isEmpty()) { throw new MissingRemoteBrokerException("No remote.url parameter defined to forward queryContext"); } HttpHeaders httpHeaders = getRemoteBrokerHeaders(brokerUrl); logger.debug("=> queryContext forwarded to remote broker : {} with Content-Type : {}", brokerUrl, httpHeaders.getContentType()); return ngsiClient.queryContext(brokerUrl, httpHeaders, query).get(); }
NgsiController extends NgsiBaseController { @Override public QueryContextResponse queryContext(final QueryContext query) throws ExecutionException, InterruptedException, MissingRemoteBrokerException { logger.debug("<= queryContext on entities: {}", query.getEntityIdList().toString()); Set<String> attributes = new HashSet<>(); if (query.getAttributeList() != null) { attributes.addAll(query.getAttributeList()); } Iterator<URI> providingApplication = localRegistrations.findProvidingApplication(query.getEntityIdList().get(0), attributes); if (providingApplication.hasNext()) { final String providerUrl = providingApplication.next().toString(); HttpHeaders httpHeaders = ngsiClient.getRequestHeaders(providerUrl); logger.debug("=> queryContext forwarded to : {} with Content-Type {}", providerUrl, httpHeaders.getContentType()); return ngsiClient.queryContext(providerUrl, httpHeaders, query).get(); } String brokerUrl = configuration.getRemoteUrl(); if (brokerUrl == null || brokerUrl.isEmpty()) { throw new MissingRemoteBrokerException("No remote.url parameter defined to forward queryContext"); } HttpHeaders httpHeaders = getRemoteBrokerHeaders(brokerUrl); logger.debug("=> queryContext forwarded to remote broker : {} with Content-Type : {}", brokerUrl, httpHeaders.getContentType()); return ngsiClient.queryContext(brokerUrl, httpHeaders, query).get(); } }
NgsiController extends NgsiBaseController { @Override public QueryContextResponse queryContext(final QueryContext query) throws ExecutionException, InterruptedException, MissingRemoteBrokerException { logger.debug("<= queryContext on entities: {}", query.getEntityIdList().toString()); Set<String> attributes = new HashSet<>(); if (query.getAttributeList() != null) { attributes.addAll(query.getAttributeList()); } Iterator<URI> providingApplication = localRegistrations.findProvidingApplication(query.getEntityIdList().get(0), attributes); if (providingApplication.hasNext()) { final String providerUrl = providingApplication.next().toString(); HttpHeaders httpHeaders = ngsiClient.getRequestHeaders(providerUrl); logger.debug("=> queryContext forwarded to : {} with Content-Type {}", providerUrl, httpHeaders.getContentType()); return ngsiClient.queryContext(providerUrl, httpHeaders, query).get(); } String brokerUrl = configuration.getRemoteUrl(); if (brokerUrl == null || brokerUrl.isEmpty()) { throw new MissingRemoteBrokerException("No remote.url parameter defined to forward queryContext"); } HttpHeaders httpHeaders = getRemoteBrokerHeaders(brokerUrl); logger.debug("=> queryContext forwarded to remote broker : {} with Content-Type : {}", brokerUrl, httpHeaders.getContentType()); return ngsiClient.queryContext(brokerUrl, httpHeaders, query).get(); }  }
NgsiController extends NgsiBaseController { @Override public QueryContextResponse queryContext(final QueryContext query) throws ExecutionException, InterruptedException, MissingRemoteBrokerException { logger.debug("<= queryContext on entities: {}", query.getEntityIdList().toString()); Set<String> attributes = new HashSet<>(); if (query.getAttributeList() != null) { attributes.addAll(query.getAttributeList()); } Iterator<URI> providingApplication = localRegistrations.findProvidingApplication(query.getEntityIdList().get(0), attributes); if (providingApplication.hasNext()) { final String providerUrl = providingApplication.next().toString(); HttpHeaders httpHeaders = ngsiClient.getRequestHeaders(providerUrl); logger.debug("=> queryContext forwarded to : {} with Content-Type {}", providerUrl, httpHeaders.getContentType()); return ngsiClient.queryContext(providerUrl, httpHeaders, query).get(); } String brokerUrl = configuration.getRemoteUrl(); if (brokerUrl == null || brokerUrl.isEmpty()) { throw new MissingRemoteBrokerException("No remote.url parameter defined to forward queryContext"); } HttpHeaders httpHeaders = getRemoteBrokerHeaders(brokerUrl); logger.debug("=> queryContext forwarded to remote broker : {} with Content-Type : {}", brokerUrl, httpHeaders.getContentType()); return ngsiClient.queryContext(brokerUrl, httpHeaders, query).get(); }  @Override RegisterContextResponse registerContext(final RegisterContext register); @Override UpdateContextResponse updateContext(final UpdateContext update); @Override QueryContextResponse queryContext(final QueryContext query); @Override SubscribeContextResponse subscribeContext(final SubscribeContext subscribe); @Override UpdateContextSubscriptionResponse updateContextSubscription(final UpdateContextSubscription updateContextSubscription); @Override UnsubscribeContextResponse unsubscribeContext(final UnsubscribeContext unsubscribe); @ExceptionHandler(RegistrationException.class) ResponseEntity<Object> registrationExceptionHandler(HttpServletRequest req, RegistrationException registrationException); @ExceptionHandler(MissingRemoteBrokerException.class) ResponseEntity<Object> missingRemoteBrokerExceptionHandler(HttpServletRequest req, MissingRemoteBrokerException missingRemoteBrokerException); @ExceptionHandler(SubscriptionException.class) ResponseEntity<Object> subscriptionExceptionHandler(HttpServletRequest req, SubscriptionException subscriptionException); @ExceptionHandler(SubscriptionPersistenceException.class) ResponseEntity<Object> subscriptionPersistenceExceptionHandler(HttpServletRequest req, SubscriptionPersistenceException subscriptionPersistenceException); @ExceptionHandler(RegistrationPersistenceException.class) ResponseEntity<Object> registrationPersistenceExceptionHandler(HttpServletRequest req, RegistrationPersistenceException registrationPersistenceException); }
NgsiController extends NgsiBaseController { @Override public QueryContextResponse queryContext(final QueryContext query) throws ExecutionException, InterruptedException, MissingRemoteBrokerException { logger.debug("<= queryContext on entities: {}", query.getEntityIdList().toString()); Set<String> attributes = new HashSet<>(); if (query.getAttributeList() != null) { attributes.addAll(query.getAttributeList()); } Iterator<URI> providingApplication = localRegistrations.findProvidingApplication(query.getEntityIdList().get(0), attributes); if (providingApplication.hasNext()) { final String providerUrl = providingApplication.next().toString(); HttpHeaders httpHeaders = ngsiClient.getRequestHeaders(providerUrl); logger.debug("=> queryContext forwarded to : {} with Content-Type {}", providerUrl, httpHeaders.getContentType()); return ngsiClient.queryContext(providerUrl, httpHeaders, query).get(); } String brokerUrl = configuration.getRemoteUrl(); if (brokerUrl == null || brokerUrl.isEmpty()) { throw new MissingRemoteBrokerException("No remote.url parameter defined to forward queryContext"); } HttpHeaders httpHeaders = getRemoteBrokerHeaders(brokerUrl); logger.debug("=> queryContext forwarded to remote broker : {} with Content-Type : {}", brokerUrl, httpHeaders.getContentType()); return ngsiClient.queryContext(brokerUrl, httpHeaders, query).get(); }  @Override RegisterContextResponse registerContext(final RegisterContext register); @Override UpdateContextResponse updateContext(final UpdateContext update); @Override QueryContextResponse queryContext(final QueryContext query); @Override SubscribeContextResponse subscribeContext(final SubscribeContext subscribe); @Override UpdateContextSubscriptionResponse updateContextSubscription(final UpdateContextSubscription updateContextSubscription); @Override UnsubscribeContextResponse unsubscribeContext(final UnsubscribeContext unsubscribe); @ExceptionHandler(RegistrationException.class) ResponseEntity<Object> registrationExceptionHandler(HttpServletRequest req, RegistrationException registrationException); @ExceptionHandler(MissingRemoteBrokerException.class) ResponseEntity<Object> missingRemoteBrokerExceptionHandler(HttpServletRequest req, MissingRemoteBrokerException missingRemoteBrokerException); @ExceptionHandler(SubscriptionException.class) ResponseEntity<Object> subscriptionExceptionHandler(HttpServletRequest req, SubscriptionException subscriptionException); @ExceptionHandler(SubscriptionPersistenceException.class) ResponseEntity<Object> subscriptionPersistenceExceptionHandler(HttpServletRequest req, SubscriptionPersistenceException subscriptionPersistenceException); @ExceptionHandler(RegistrationPersistenceException.class) ResponseEntity<Object> registrationPersistenceExceptionHandler(HttpServletRequest req, RegistrationPersistenceException registrationPersistenceException);  }