@Test public void postUpdateContextWithoutProvidingApplicationAndWithoutRemoteBrokerButWithNotify() throws Exception { when(configuration.getRemoteUrl()).thenReturn(null); when(providingApplication.hasNext()).thenReturn(false); when(localRegistrations.findProvidingApplication(any(), any())).thenReturn(providingApplication); when(matchedSubscriptions.hasNext()).thenReturn(true, false); SubscribeContext subscribeContext = createSubscribeContextTemperature(); Subscription subscription = new Subscription("999999", Instant.now().plus(1, ChronoUnit.DAYS), subscribeContext); when(matchedSubscriptions.next()).thenReturn(subscription); when(subscriptions.findSubscriptions(any(), any())).thenReturn(matchedSubscriptions); when(ngsiClient.updateContext(any(), any(), any())).thenReturn(updateContextResponseListenableFuture); when(ngsiClient.notifyContextCustomURL(any(), any(), any())).thenReturn(notifyContextResponseListenableFuture); mockMvc.perform(post("/v1/updateContext") .content(json(mapper, createUpdateContextTempSensorAndPressure())) .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].contextElement.attributes[1].name").value("pressure")) .andExpect(MockMvcResultMatchers.jsonPath("$.contextResponses[0].contextElement.attributes[1].type").value("int")) .andExpect(MockMvcResultMatchers.jsonPath("$.contextResponses[0].contextElement.attributes[1].value").value("1015")) .andExpect(MockMvcResultMatchers.jsonPath("$.contextResponses[0].statusCode.code").value("200")); verify(localRegistrations).findProvidingApplication(entityIdArgumentCaptor.capture(), attributeArgumentCaptor.capture()); assertEquals("S1", entityIdArgumentCaptor.getValue().getId()); assertEquals("TempSensor", entityIdArgumentCaptor.getValue().getType()); assertFalse(entityIdArgumentCaptor.getValue().getIsPattern()); assertEquals(2, attributeArgumentCaptor.getValue().size()); assertTrue(attributeArgumentCaptor.getValue().contains("temp")); assertTrue(attributeArgumentCaptor.getValue().contains("pressure")); verify(updateContextResponseListenableFuture, never()).addCallback(any(), any()); verify(ngsiClient, never()).updateContext(any(), any(), any()); ArgumentCaptor<NotifyContext> notifyContextArg = ArgumentCaptor.forClass(NotifyContext.class); verify(notifyContextResponseListenableFuture, atLeastOnce()).addCallback(any(), any()); String urlReference = subscribeContext.getReference().toString(); verify(ngsiClient, atLeastOnce()).notifyContextCustomURL(eq(urlReference), any(), notifyContextArg.capture()); assertEquals(1, notifyContextArg.getValue().getContextElementResponseList().size()); ContextElementResponse contextElementResponse = notifyContextArg.getValue().getContextElementResponseList().get(0); assertEquals("S1", contextElementResponse.getContextElement().getEntityId().getId()); assertEquals("200", contextElementResponse.getStatusCode().getCode()); }
@Override public UpdateContextResponse updateContext(final UpdateContext update) throws ExecutionException, InterruptedException, URISyntaxException { ContextElement contextElement = update.getContextElements().get(0); Set<String> attributesName = contextElement.getContextAttributeList().stream().map(ContextAttribute::getName).collect(Collectors.toSet()); logger.debug("<= updateContext with entityId: {} and attributes: {} ", contextElement.getEntityId().toString(), attributesName); Iterator<URI> providingApplication = localRegistrations.findProvidingApplication(contextElement.getEntityId(), attributesName); if (providingApplication.hasNext()) { final String providerUrl = providingApplication.next().toString(); HttpHeaders httpHeaders = ngsiClient.getRequestHeaders(providerUrl); logger.debug("=> updateContext forwarded to {} with Content-Type {}", providerUrl, httpHeaders.getContentType()); return ngsiClient.updateContext(providerUrl, httpHeaders, update).get(); } if (configuration.isRemoteForwardUpdateContext()) { final String brokerUrl = configuration.getRemoteUrl(); if (brokerUrl == null || brokerUrl.isEmpty()) { logger.warn("No remote.url parameter defined to forward updateContext"); } else { HttpHeaders httpHeaders = getRemoteBrokerHeaders(brokerUrl); logger.debug("=> updateContext forwarded to remote broker {} with Content-Type {}", brokerUrl, httpHeaders.getContentType()); ngsiClient.updateContext(brokerUrl, httpHeaders, update) .addCallback(updateContextResponse -> logUpdateContextResponse(updateContextResponse, brokerUrl), throwable -> logger.warn("UpdateContext failed for {}: {}", brokerUrl, throwable.getMessage(), throwable)); } } List<ContextElementResponse> contextElementResponseList = new ArrayList<>(); StatusCode statusCode = new StatusCode(CodeEnum.CODE_200); for (ContextElement c : update.getContextElements()) { contextElementResponseList.add(new ContextElementResponse(c, statusCode)); } String originator = configuration.getLocalUrl(); if (originator == null || originator.isEmpty()) { logger.warn("No local.url parameter defined to use as originator for sending notifyContext"); } else { Iterator<Subscription> matchingSubscriptions = subscriptions.findSubscriptions(contextElement.getEntityId(), attributesName); while (matchingSubscriptions.hasNext()) { Subscription subscription = matchingSubscriptions.next(); NotifyContext notifyContext = new NotifyContext(subscription.getSubscriptionId(), new URI(originator)); notifyContext.setContextElementResponseList(contextElementResponseList); String providerUrl = subscription.getSubscribeContext().getReference().toString(); HttpHeaders httpHeaders = ngsiClient.getRequestHeaders(providerUrl); logger.debug("=> notifyContext to {} with Content-Type {}", providerUrl, httpHeaders.getContentType()); ngsiClient.notifyContextCustomURL(providerUrl, httpHeaders, notifyContext).addCallback( notifyContextResponse -> logNotifyContextResponse(notifyContextResponse, providerUrl), throwable -> logger.warn("NotifyContext failed for {}", providerUrl, throwable)); } } UpdateContextResponse updateContextResponse = new UpdateContextResponse(); updateContextResponse.setContextElementResponses(contextElementResponseList); return updateContextResponse; }
NgsiController extends NgsiBaseController { @Override public UpdateContextResponse updateContext(final UpdateContext update) throws ExecutionException, InterruptedException, URISyntaxException { ContextElement contextElement = update.getContextElements().get(0); Set<String> attributesName = contextElement.getContextAttributeList().stream().map(ContextAttribute::getName).collect(Collectors.toSet()); logger.debug("<= updateContext with entityId: {} and attributes: {} ", contextElement.getEntityId().toString(), attributesName); Iterator<URI> providingApplication = localRegistrations.findProvidingApplication(contextElement.getEntityId(), attributesName); if (providingApplication.hasNext()) { final String providerUrl = providingApplication.next().toString(); HttpHeaders httpHeaders = ngsiClient.getRequestHeaders(providerUrl); logger.debug("=> updateContext forwarded to {} with Content-Type {}", providerUrl, httpHeaders.getContentType()); return ngsiClient.updateContext(providerUrl, httpHeaders, update).get(); } if (configuration.isRemoteForwardUpdateContext()) { final String brokerUrl = configuration.getRemoteUrl(); if (brokerUrl == null || brokerUrl.isEmpty()) { logger.warn("No remote.url parameter defined to forward updateContext"); } else { HttpHeaders httpHeaders = getRemoteBrokerHeaders(brokerUrl); logger.debug("=> updateContext forwarded to remote broker {} with Content-Type {}", brokerUrl, httpHeaders.getContentType()); ngsiClient.updateContext(brokerUrl, httpHeaders, update) .addCallback(updateContextResponse -> logUpdateContextResponse(updateContextResponse, brokerUrl), throwable -> logger.warn("UpdateContext failed for {}: {}", brokerUrl, throwable.getMessage(), throwable)); } } List<ContextElementResponse> contextElementResponseList = new ArrayList<>(); StatusCode statusCode = new StatusCode(CodeEnum.CODE_200); for (ContextElement c : update.getContextElements()) { contextElementResponseList.add(new ContextElementResponse(c, statusCode)); } String originator = configuration.getLocalUrl(); if (originator == null || originator.isEmpty()) { logger.warn("No local.url parameter defined to use as originator for sending notifyContext"); } else { Iterator<Subscription> matchingSubscriptions = subscriptions.findSubscriptions(contextElement.getEntityId(), attributesName); while (matchingSubscriptions.hasNext()) { Subscription subscription = matchingSubscriptions.next(); NotifyContext notifyContext = new NotifyContext(subscription.getSubscriptionId(), new URI(originator)); notifyContext.setContextElementResponseList(contextElementResponseList); String providerUrl = subscription.getSubscribeContext().getReference().toString(); HttpHeaders httpHeaders = ngsiClient.getRequestHeaders(providerUrl); logger.debug("=> notifyContext to {} with Content-Type {}", providerUrl, httpHeaders.getContentType()); ngsiClient.notifyContextCustomURL(providerUrl, httpHeaders, notifyContext).addCallback( notifyContextResponse -> logNotifyContextResponse(notifyContextResponse, providerUrl), throwable -> logger.warn("NotifyContext failed for {}", providerUrl, throwable)); } } UpdateContextResponse updateContextResponse = new UpdateContextResponse(); updateContextResponse.setContextElementResponses(contextElementResponseList); return updateContextResponse; } }
NgsiController extends NgsiBaseController { @Override public UpdateContextResponse updateContext(final UpdateContext update) throws ExecutionException, InterruptedException, URISyntaxException { ContextElement contextElement = update.getContextElements().get(0); Set<String> attributesName = contextElement.getContextAttributeList().stream().map(ContextAttribute::getName).collect(Collectors.toSet()); logger.debug("<= updateContext with entityId: {} and attributes: {} ", contextElement.getEntityId().toString(), attributesName); Iterator<URI> providingApplication = localRegistrations.findProvidingApplication(contextElement.getEntityId(), attributesName); if (providingApplication.hasNext()) { final String providerUrl = providingApplication.next().toString(); HttpHeaders httpHeaders = ngsiClient.getRequestHeaders(providerUrl); logger.debug("=> updateContext forwarded to {} with Content-Type {}", providerUrl, httpHeaders.getContentType()); return ngsiClient.updateContext(providerUrl, httpHeaders, update).get(); } if (configuration.isRemoteForwardUpdateContext()) { final String brokerUrl = configuration.getRemoteUrl(); if (brokerUrl == null || brokerUrl.isEmpty()) { logger.warn("No remote.url parameter defined to forward updateContext"); } else { HttpHeaders httpHeaders = getRemoteBrokerHeaders(brokerUrl); logger.debug("=> updateContext forwarded to remote broker {} with Content-Type {}", brokerUrl, httpHeaders.getContentType()); ngsiClient.updateContext(brokerUrl, httpHeaders, update) .addCallback(updateContextResponse -> logUpdateContextResponse(updateContextResponse, brokerUrl), throwable -> logger.warn("UpdateContext failed for {}: {}", brokerUrl, throwable.getMessage(), throwable)); } } List<ContextElementResponse> contextElementResponseList = new ArrayList<>(); StatusCode statusCode = new StatusCode(CodeEnum.CODE_200); for (ContextElement c : update.getContextElements()) { contextElementResponseList.add(new ContextElementResponse(c, statusCode)); } String originator = configuration.getLocalUrl(); if (originator == null || originator.isEmpty()) { logger.warn("No local.url parameter defined to use as originator for sending notifyContext"); } else { Iterator<Subscription> matchingSubscriptions = subscriptions.findSubscriptions(contextElement.getEntityId(), attributesName); while (matchingSubscriptions.hasNext()) { Subscription subscription = matchingSubscriptions.next(); NotifyContext notifyContext = new NotifyContext(subscription.getSubscriptionId(), new URI(originator)); notifyContext.setContextElementResponseList(contextElementResponseList); String providerUrl = subscription.getSubscribeContext().getReference().toString(); HttpHeaders httpHeaders = ngsiClient.getRequestHeaders(providerUrl); logger.debug("=> notifyContext to {} with Content-Type {}", providerUrl, httpHeaders.getContentType()); ngsiClient.notifyContextCustomURL(providerUrl, httpHeaders, notifyContext).addCallback( notifyContextResponse -> logNotifyContextResponse(notifyContextResponse, providerUrl), throwable -> logger.warn("NotifyContext failed for {}", providerUrl, throwable)); } } UpdateContextResponse updateContextResponse = new UpdateContextResponse(); updateContextResponse.setContextElementResponses(contextElementResponseList); return updateContextResponse; }  }
NgsiController extends NgsiBaseController { @Override public UpdateContextResponse updateContext(final UpdateContext update) throws ExecutionException, InterruptedException, URISyntaxException { ContextElement contextElement = update.getContextElements().get(0); Set<String> attributesName = contextElement.getContextAttributeList().stream().map(ContextAttribute::getName).collect(Collectors.toSet()); logger.debug("<= updateContext with entityId: {} and attributes: {} ", contextElement.getEntityId().toString(), attributesName); Iterator<URI> providingApplication = localRegistrations.findProvidingApplication(contextElement.getEntityId(), attributesName); if (providingApplication.hasNext()) { final String providerUrl = providingApplication.next().toString(); HttpHeaders httpHeaders = ngsiClient.getRequestHeaders(providerUrl); logger.debug("=> updateContext forwarded to {} with Content-Type {}", providerUrl, httpHeaders.getContentType()); return ngsiClient.updateContext(providerUrl, httpHeaders, update).get(); } if (configuration.isRemoteForwardUpdateContext()) { final String brokerUrl = configuration.getRemoteUrl(); if (brokerUrl == null || brokerUrl.isEmpty()) { logger.warn("No remote.url parameter defined to forward updateContext"); } else { HttpHeaders httpHeaders = getRemoteBrokerHeaders(brokerUrl); logger.debug("=> updateContext forwarded to remote broker {} with Content-Type {}", brokerUrl, httpHeaders.getContentType()); ngsiClient.updateContext(brokerUrl, httpHeaders, update) .addCallback(updateContextResponse -> logUpdateContextResponse(updateContextResponse, brokerUrl), throwable -> logger.warn("UpdateContext failed for {}: {}", brokerUrl, throwable.getMessage(), throwable)); } } List<ContextElementResponse> contextElementResponseList = new ArrayList<>(); StatusCode statusCode = new StatusCode(CodeEnum.CODE_200); for (ContextElement c : update.getContextElements()) { contextElementResponseList.add(new ContextElementResponse(c, statusCode)); } String originator = configuration.getLocalUrl(); if (originator == null || originator.isEmpty()) { logger.warn("No local.url parameter defined to use as originator for sending notifyContext"); } else { Iterator<Subscription> matchingSubscriptions = subscriptions.findSubscriptions(contextElement.getEntityId(), attributesName); while (matchingSubscriptions.hasNext()) { Subscription subscription = matchingSubscriptions.next(); NotifyContext notifyContext = new NotifyContext(subscription.getSubscriptionId(), new URI(originator)); notifyContext.setContextElementResponseList(contextElementResponseList); String providerUrl = subscription.getSubscribeContext().getReference().toString(); HttpHeaders httpHeaders = ngsiClient.getRequestHeaders(providerUrl); logger.debug("=> notifyContext to {} with Content-Type {}", providerUrl, httpHeaders.getContentType()); ngsiClient.notifyContextCustomURL(providerUrl, httpHeaders, notifyContext).addCallback( notifyContextResponse -> logNotifyContextResponse(notifyContextResponse, providerUrl), throwable -> logger.warn("NotifyContext failed for {}", providerUrl, throwable)); } } UpdateContextResponse updateContextResponse = new UpdateContextResponse(); updateContextResponse.setContextElementResponses(contextElementResponseList); return updateContextResponse; }  @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 UpdateContextResponse updateContext(final UpdateContext update) throws ExecutionException, InterruptedException, URISyntaxException { ContextElement contextElement = update.getContextElements().get(0); Set<String> attributesName = contextElement.getContextAttributeList().stream().map(ContextAttribute::getName).collect(Collectors.toSet()); logger.debug("<= updateContext with entityId: {} and attributes: {} ", contextElement.getEntityId().toString(), attributesName); Iterator<URI> providingApplication = localRegistrations.findProvidingApplication(contextElement.getEntityId(), attributesName); if (providingApplication.hasNext()) { final String providerUrl = providingApplication.next().toString(); HttpHeaders httpHeaders = ngsiClient.getRequestHeaders(providerUrl); logger.debug("=> updateContext forwarded to {} with Content-Type {}", providerUrl, httpHeaders.getContentType()); return ngsiClient.updateContext(providerUrl, httpHeaders, update).get(); } if (configuration.isRemoteForwardUpdateContext()) { final String brokerUrl = configuration.getRemoteUrl(); if (brokerUrl == null || brokerUrl.isEmpty()) { logger.warn("No remote.url parameter defined to forward updateContext"); } else { HttpHeaders httpHeaders = getRemoteBrokerHeaders(brokerUrl); logger.debug("=> updateContext forwarded to remote broker {} with Content-Type {}", brokerUrl, httpHeaders.getContentType()); ngsiClient.updateContext(brokerUrl, httpHeaders, update) .addCallback(updateContextResponse -> logUpdateContextResponse(updateContextResponse, brokerUrl), throwable -> logger.warn("UpdateContext failed for {}: {}", brokerUrl, throwable.getMessage(), throwable)); } } List<ContextElementResponse> contextElementResponseList = new ArrayList<>(); StatusCode statusCode = new StatusCode(CodeEnum.CODE_200); for (ContextElement c : update.getContextElements()) { contextElementResponseList.add(new ContextElementResponse(c, statusCode)); } String originator = configuration.getLocalUrl(); if (originator == null || originator.isEmpty()) { logger.warn("No local.url parameter defined to use as originator for sending notifyContext"); } else { Iterator<Subscription> matchingSubscriptions = subscriptions.findSubscriptions(contextElement.getEntityId(), attributesName); while (matchingSubscriptions.hasNext()) { Subscription subscription = matchingSubscriptions.next(); NotifyContext notifyContext = new NotifyContext(subscription.getSubscriptionId(), new URI(originator)); notifyContext.setContextElementResponseList(contextElementResponseList); String providerUrl = subscription.getSubscribeContext().getReference().toString(); HttpHeaders httpHeaders = ngsiClient.getRequestHeaders(providerUrl); logger.debug("=> notifyContext to {} with Content-Type {}", providerUrl, httpHeaders.getContentType()); ngsiClient.notifyContextCustomURL(providerUrl, httpHeaders, notifyContext).addCallback( notifyContextResponse -> logNotifyContextResponse(notifyContextResponse, providerUrl), throwable -> logger.warn("NotifyContext failed for {}", providerUrl, throwable)); } } UpdateContextResponse updateContextResponse = new UpdateContextResponse(); updateContextResponse.setContextElementResponses(contextElementResponseList); return updateContextResponse; }  @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);  }