### Question:
StaticServicesRegistrationService { public synchronized StaticRegistrationResult reloadServices() { List<InstanceInfo> oldStaticInstances = new ArrayList<>(staticInstances); staticInstances.clear(); StaticRegistrationResult result = registerServices(staticApiDefinitionsDirectories); PeerAwareInstanceRegistry registry = getRegistry(); for (InstanceInfo info: oldStaticInstances) { if (!result.getRegisteredServices().contains(info.getInstanceId())) { log.info("Instance {} is not defined in the new static API definitions. It will be removed", info.getInstanceId()); registry.cancel(info.getAppName(), info.getId(), false); } } return result; } @Autowired  StaticServicesRegistrationService(ServiceDefinitionProcessor serviceDefinitionProcessor, MetadataDefaultsService metadataDefaultsService); List<InstanceInfo> getStaticInstances(); void registerServices(); synchronized StaticRegistrationResult reloadServices();  }### Answer:
@Test public void testReloadServicesWithUnregisteringService() { String service = "service"; ServiceDefinitionProcessor serviceDefinitionProcessor = mock(ServiceDefinitionProcessor.class); InstanceInfo instance = InstanceInfo.Builder.newBuilder().setInstanceId(service).setAppName(service).build(); when(serviceDefinitionProcessor.findStaticServicesData(null)) .thenReturn(createResult(instance)) .thenReturn(createResult()); StaticServicesRegistrationService registrationService = new StaticServicesRegistrationService(serviceDefinitionProcessor, new MetadataDefaultsService()); registrationService.reloadServices(); StaticRegistrationResult result = registrationService.reloadServices(); assertThat(result.getRegisteredServices().contains(service), is(false)); verify(serviceDefinitionProcessor, times(2)).findStaticServicesData(null); verify(mockRegistry, times(1)).cancel(instance.getAppName(), instance.getId(), false); }
@Test public void testReloadServicesWithAddingNewService() { String serviceA = "serviceA"; String serviceB = "serviceB"; ServiceDefinitionProcessor serviceDefinitionProcessor = mock(ServiceDefinitionProcessor.class); InstanceInfo instanceA = InstanceInfo.Builder.newBuilder().setInstanceId(serviceA).setAppName(serviceA).build(); InstanceInfo instanceB = InstanceInfo.Builder.newBuilder().setInstanceId(serviceB).setAppName(serviceB).build(); when(serviceDefinitionProcessor.findStaticServicesData(null)) .thenReturn(createResult(instanceA)) .thenReturn(createResult(instanceA, instanceB)); StaticServicesRegistrationService registrationService = new StaticServicesRegistrationService(serviceDefinitionProcessor, new MetadataDefaultsService()); registrationService.reloadServices(); StaticRegistrationResult result = registrationService.reloadServices(); assertThat(result.getRegisteredServices().contains(serviceA), is(true)); assertThat(result.getRegisteredServices().contains(serviceB), is(true)); verify(serviceDefinitionProcessor, times(2)).findStaticServicesData(null); verify(mockRegistry, times(0)).cancel(any(String.class), any(String.class), eq(false)); }