### Question:
GatewayNotifier implements Runnable { public void serviceUpdated(String serviceId, String instanceId) { final Notification notification = new Notification(serviceId, instanceId, Type.SERVICE_UPDATED); addToQueue(notification); } GatewayNotifier(@Qualifier("restTemplateWithKeystore") RestTemplate restTemplate, MessageService messageService); @PostConstruct void afterPropertiesSet(); @PreDestroy void preDestroy(); void serviceUpdated(String serviceId, String instanceId); void serviceCancelledRegistration(String serviceId); void distributeInvalidatedCredentials(String instanceId); @Override void run(); static final String GATEWAY_SERVICE_ID; }### Answer:
@Test public void testServiceUpdated() { verify(restTemplate, never()).delete(anyString()); List<InstanceInfo> instances = Arrays.asList( createInstanceInfo("hostname1", 1000, 1433), createInstanceInfo("hostname2", 1000, 0) ); Application application = mock(Application.class); when(application.getInstances()).thenReturn(instances); when(registry.getApplication("GATEWAY")).thenReturn(application); gatewayNotifierSync.serviceUpdated("testService", null); verify(restTemplate, times(1)).delete("https: verify(restTemplate, times(1)).delete("http: gatewayNotifierSync.serviceUpdated(null, null); verify(restTemplate, times(1)).delete("https: verify(restTemplate, times(1)).delete("http: verify(restTemplate, times(4)).delete(anyString()); }
@Test public void testMissingGateway() { final String messageKey = "org.zowe.apiml.discovery.errorNotifyingGateway"; when(registry.getApplication(anyString())).thenReturn(null); when(messageService.createMessage(messageKey)).thenReturn(createMessage(messageKey)); gatewayNotifierSync.serviceUpdated("serviceX", null); verify(messageService, times(1)).createMessage(messageKey); }
@Test public void testServiceUpdatedNotificationFailed() { MessageTemplate messageTemplate = new MessageTemplate("key", "number", MessageType.ERROR, "text"); Message message = Message.of("requestedKey", messageTemplate, new Object[0]); when(messageService.createMessage(anyString(), (Object[]) any())).thenReturn(message); doThrow(new RuntimeException("any exception")).when(restTemplate).delete(anyString()); List<InstanceInfo> instances = new LinkedList<>(); Application application = mock(Application.class); when(application.getInstances()).thenReturn(instances); when(registry.getApplication("GATEWAY")).thenReturn(application); gatewayNotifierSync.serviceUpdated("service", "host:service:1433"); verify(restTemplate, never()).delete(anyString()); instances.add(createInstanceInfo("GATEWAY", "host", 1000, 1433)); gatewayNotifierSync.serviceUpdated("GATEWAY", "host:GATEWAY:1433"); verify(restTemplate, never()).delete(anyString()); gatewayNotifierSync.serviceUpdated("service", "host2:service:123"); verify(restTemplate, times(1)).delete(anyString()); verify(messageService).createMessage( "org.zowe.apiml.discovery.registration.gateway.notify", "https: "host2:service:123" ); }