@Test public void snitchErrorShouldThrowSnitchingException() throws IOException { String uri = "http: String json = "{ \"error\" : true }"; metadata.put(CereebroMetadata.KEY_SNITCH_URI, uri); metadata.put(CereebroMetadata.KEY_SNITCH_SYSTEM_FRAGMENT_JSON, json); Mockito.when(objectMapperMock.readValue(json, SystemFragment.class)).thenThrow(new IOException("unit test")); ServiceInstanceSnitch snitch = new ServiceInstanceSnitch(objectMapperMock, serviceInstanceMock); try { snitch.snitch(); Assertions.fail("Expected IOException"); } catch (SnitchingException e) { Assertions.assertThat(e.getSnitchUri()).isEqualTo(URI.create(uri)); } }
@Override public SystemFragment snitch() { URI uri = getUri(); String serviceId = serviceInstance.getServiceId(); try { if (hasCereebroMetadata()) { if (StringUtils.hasText(getSystemFragmentJsonString())) { LOGGER.debug("Using snitched system fragment from discovery client - uri : {}", uri); SystemFragment frag = objectMapper.readValue(getSystemFragmentJsonString(), SystemFragment.class); return StaticSnitch.of(uri, frag).snitch(); } else { LOGGER.debug("Using Snitch URL from discovery client - uri : {}", uri); return ResourceSnitch.of(objectMapper, new UrlResource(uri)).snitch(); } } else { LOGGER.debug("No Cereebro metadata, using only the serviceId : {} and instance URI : {}", serviceId, uri); Component service = Component.of(serviceId, ComponentType.HTTP_APPLICATION); return SystemFragment.of(ComponentRelationships.of(service)); } } catch (IOException | RuntimeException e) { LOGGER.warn("Could not create snitch out of service : {} - meta-data : {} - error : {}", serviceId, serviceInstance.getMetadata(), e.getMessage()); throw new SnitchingException(uri, "Error while creating snitch for uri : " + uri + " - service instance : " + serviceId, e); } }
ServiceInstanceSnitch implements Snitch { @Override public SystemFragment snitch() { URI uri = getUri(); String serviceId = serviceInstance.getServiceId(); try { if (hasCereebroMetadata()) { if (StringUtils.hasText(getSystemFragmentJsonString())) { LOGGER.debug("Using snitched system fragment from discovery client - uri : {}", uri); SystemFragment frag = objectMapper.readValue(getSystemFragmentJsonString(), SystemFragment.class); return StaticSnitch.of(uri, frag).snitch(); } else { LOGGER.debug("Using Snitch URL from discovery client - uri : {}", uri); return ResourceSnitch.of(objectMapper, new UrlResource(uri)).snitch(); } } else { LOGGER.debug("No Cereebro metadata, using only the serviceId : {} and instance URI : {}", serviceId, uri); Component service = Component.of(serviceId, ComponentType.HTTP_APPLICATION); return SystemFragment.of(ComponentRelationships.of(service)); } } catch (IOException | RuntimeException e) { LOGGER.warn("Could not create snitch out of service : {} - meta-data : {} - error : {}", serviceId, serviceInstance.getMetadata(), e.getMessage()); throw new SnitchingException(uri, "Error while creating snitch for uri : " + uri + " - service instance : " + serviceId, e); } } }
ServiceInstanceSnitch implements Snitch { @Override public SystemFragment snitch() { URI uri = getUri(); String serviceId = serviceInstance.getServiceId(); try { if (hasCereebroMetadata()) { if (StringUtils.hasText(getSystemFragmentJsonString())) { LOGGER.debug("Using snitched system fragment from discovery client - uri : {}", uri); SystemFragment frag = objectMapper.readValue(getSystemFragmentJsonString(), SystemFragment.class); return StaticSnitch.of(uri, frag).snitch(); } else { LOGGER.debug("Using Snitch URL from discovery client - uri : {}", uri); return ResourceSnitch.of(objectMapper, new UrlResource(uri)).snitch(); } } else { LOGGER.debug("No Cereebro metadata, using only the serviceId : {} and instance URI : {}", serviceId, uri); Component service = Component.of(serviceId, ComponentType.HTTP_APPLICATION); return SystemFragment.of(ComponentRelationships.of(service)); } } catch (IOException | RuntimeException e) { LOGGER.warn("Could not create snitch out of service : {} - meta-data : {} - error : {}", serviceId, serviceInstance.getMetadata(), e.getMessage()); throw new SnitchingException(uri, "Error while creating snitch for uri : " + uri + " - service instance : " + serviceId, e); } } ServiceInstanceSnitch(ObjectMapper mapper, ServiceInstance instance); }
ServiceInstanceSnitch implements Snitch { @Override public SystemFragment snitch() { URI uri = getUri(); String serviceId = serviceInstance.getServiceId(); try { if (hasCereebroMetadata()) { if (StringUtils.hasText(getSystemFragmentJsonString())) { LOGGER.debug("Using snitched system fragment from discovery client - uri : {}", uri); SystemFragment frag = objectMapper.readValue(getSystemFragmentJsonString(), SystemFragment.class); return StaticSnitch.of(uri, frag).snitch(); } else { LOGGER.debug("Using Snitch URL from discovery client - uri : {}", uri); return ResourceSnitch.of(objectMapper, new UrlResource(uri)).snitch(); } } else { LOGGER.debug("No Cereebro metadata, using only the serviceId : {} and instance URI : {}", serviceId, uri); Component service = Component.of(serviceId, ComponentType.HTTP_APPLICATION); return SystemFragment.of(ComponentRelationships.of(service)); } } catch (IOException | RuntimeException e) { LOGGER.warn("Could not create snitch out of service : {} - meta-data : {} - error : {}", serviceId, serviceInstance.getMetadata(), e.getMessage()); throw new SnitchingException(uri, "Error while creating snitch for uri : " + uri + " - service instance : " + serviceId, e); } } ServiceInstanceSnitch(ObjectMapper mapper, ServiceInstance instance); static Snitch of(ObjectMapper mapper, ServiceInstance instance); static boolean hasCereebroMetadata(ServiceInstance instance); boolean hasCereebroMetadata(); @Override URI getUri(); @Override SystemFragment snitch(); }
ServiceInstanceSnitch implements Snitch { @Override public SystemFragment snitch() { URI uri = getUri(); String serviceId = serviceInstance.getServiceId(); try { if (hasCereebroMetadata()) { if (StringUtils.hasText(getSystemFragmentJsonString())) { LOGGER.debug("Using snitched system fragment from discovery client - uri : {}", uri); SystemFragment frag = objectMapper.readValue(getSystemFragmentJsonString(), SystemFragment.class); return StaticSnitch.of(uri, frag).snitch(); } else { LOGGER.debug("Using Snitch URL from discovery client - uri : {}", uri); return ResourceSnitch.of(objectMapper, new UrlResource(uri)).snitch(); } } else { LOGGER.debug("No Cereebro metadata, using only the serviceId : {} and instance URI : {}", serviceId, uri); Component service = Component.of(serviceId, ComponentType.HTTP_APPLICATION); return SystemFragment.of(ComponentRelationships.of(service)); } } catch (IOException | RuntimeException e) { LOGGER.warn("Could not create snitch out of service : {} - meta-data : {} - error : {}", serviceId, serviceInstance.getMetadata(), e.getMessage()); throw new SnitchingException(uri, "Error while creating snitch for uri : " + uri + " - service instance : " + serviceId, e); } } ServiceInstanceSnitch(ObjectMapper mapper, ServiceInstance instance); static Snitch of(ObjectMapper mapper, ServiceInstance instance); static boolean hasCereebroMetadata(ServiceInstance instance); boolean hasCereebroMetadata(); @Override URI getUri(); @Override SystemFragment snitch();  }