BranchingServiceCollection extends ServiceCollectionImp { public void setFirstServiceId(String s) { firstServiceId = Args.notBlank(s, "firstServiceId"); } BranchingServiceCollection();  BranchingServiceCollection(Collection<Service> list); String getFirstServiceId(); void setFirstServiceId(String s); BranchingServiceCollection withFirstServiceId(String s);  }
@Test public void testSetFirstServiceId() throws Exception { BranchingServiceCollection services = createServiceCollection(); services.setFirstServiceId(FIRST_SERVICE_ID); try { services.setFirstServiceId(null); fail("Null or empty first-service-id"); } catch (IllegalArgumentException expected) { } assertEquals(FIRST_SERVICE_ID, services.getFirstServiceId()); }
@Test public void testInit() { BranchingServiceCollection services = createServiceCollection(); try { LifecycleHelper.init(services); fail("should not be able to init without first Service ID"); } catch (CoreException e) { } services.setFirstServiceId(BRANCH_LOW); try { LifecycleHelper.init(services); } catch (CoreException e) { fail("should not get Exception once first Service ID is set"); } }
@Test public void testSetList() throws Exception { BranchingServiceCollection services = createServiceCollection(); services.setServices(Arrays.asList(new Service[] { branchService, lowService, highService })); services.setFirstServiceId(FIRST_SERVICE_ID); start(services); stop(services); NullService illegalService1 = new NullService(); illegalService1.setUniqueId(BRANCH_HIGH); try { services.setServices(Arrays.asList(new Service[] { branchService, lowService, highService, illegalService1 })); fail("Initialised with duplicate service-id"); } catch (IllegalArgumentException expected) { } NullService illegalService2 = new NullService(); illegalService2.setUniqueId(""); try { services.setServices(Arrays.asList(new Service[] { branchService, lowService, highService, illegalService2 })); fail("Initialised with '' service-id"); } catch (IllegalArgumentException expected) { } }
@Test public void testDoService() throws Exception { BranchingServiceCollection services = createServiceCollection(); branchService.changeState(StartedState.getInstance()); lowService.changeState(StartedState.getInstance()); highService.changeState(StartedState.getInstance()); services.addService(branchService); services.addService(lowService); services.addService(highService); services.setFirstServiceId(FIRST_SERVICE_ID); AdaptrisMessage msg = AdaptrisMessageFactory.getDefaultInstance().newMessage(); execute(services, msg); assertTrue("msg " + msg, msg.getMetadataValue("service-id").equals(BRANCH_LOW)); assertEquals(2, msg.getMessageLifecycleEvent().getMleMarkers().size()); services.setOutOfStateHandler(new NullOutOfStateHandler()); AdaptrisMessage msg2 = AdaptrisMessageFactory.getDefaultInstance().newMessage(); services.doService(msg2); assertTrue(msg2.getMetadataValue("service-id").equals(BRANCH_HIGH)); assertEquals(2, msg.getMessageLifecycleEvent().getMleMarkers().size()); }
@Test public void testDoService_Legacy() throws Exception { BranchingServiceCollection services = createServiceCollection(); branchService.changeState(StartedState.getInstance()); lowService.changeState(StartedState.getInstance()); highService.changeState(StartedState.getInstance()); services.addService(branchService); services.addService(lowService); services.addService(highService); services.setFirstServiceId(FIRST_SERVICE_ID); AdaptrisMessage msg = AdaptrisMessageFactory.getDefaultInstance().newMessage(); execute(services, msg); assertTrue("msg " + msg, msg.getMetadataValue("service-id").equals(BRANCH_LOW)); assertEquals(2, msg.getMessageLifecycleEvent().getMleMarkers().size()); services.setOutOfStateHandler(new NullOutOfStateHandler()); AdaptrisMessage msg2 = AdaptrisMessageFactory.getDefaultInstance().newMessage(); services.doService(msg2); assertTrue(msg2.getMetadataValue("service-id").equals(BRANCH_HIGH)); assertEquals(2, msg.getMessageLifecycleEvent().getMleMarkers().size()); }
@Test public void testDoServiceOutOfState() throws Exception { BranchingServiceCollection services = createServiceCollection(); services.addService(branchService); services.addService(lowService); services.addService(highService); services.setFirstServiceId(FIRST_SERVICE_ID); AdaptrisMessage msg = AdaptrisMessageFactory.getDefaultInstance().newMessage(); try { branchService.changeState(ClosedState.getInstance()); lowService.changeState(ClosedState.getInstance()); highService.changeState(ClosedState.getInstance()); executeWithoutStarting(services, msg); fail("Should fail because services are not 'Started'"); } catch (ServiceException ex) { } }
@Test public void testDoServiceBadServiceId() throws Exception { BranchingServiceCollection services = createServiceCollection(); services.addService(branchService); services.addService(lowService); services.addService(highService); services.setFirstServiceId("005"); AdaptrisMessage msg = AdaptrisMessageFactory.getDefaultInstance().newMessage(); try { execute(services, msg); fail("BadService ID == success"); } catch (ServiceException expected) { } }
@Test public void testDoServiceFailsNoRestart() throws Exception { doThrow(new ServiceException("Expected")).when(mockFailingService).doService(any(AdaptrisMessage.class)); when(mockFailingService.getUniqueId()).thenReturn(FIRST_SERVICE_ID); when(mockFailingService.createName()).thenReturn(Service.class.getName()); when(mockOutOfStateHandler.isInCorrectState(mockFailingService)).thenReturn(true); BranchingServiceCollection services = createServiceCollection(); services.setOutOfStateHandler(mockOutOfStateHandler); services.setFirstServiceId(FIRST_SERVICE_ID); services.setRestartAffectedServiceOnException(false); services.addService(mockFailingService); AdaptrisMessage msg = AdaptrisMessageFactory.getDefaultInstance().newMessage(); try { services.doService(msg); fail("Mock service should fail."); } catch (ServiceException expected) { verify(mockFailingService, never()).requestStop(); verify(mockFailingService, never()).requestClose(); verify(mockFailingService, never()).requestInit(); verify(mockFailingService, never()).requestStart(); } }
@Test public void testDoServiceFailsWithServiceRestart() throws Exception { doThrow(new ServiceException("Expected")).when(mockFailingService).doService(any(AdaptrisMessage.class)); when(mockFailingService.getUniqueId()).thenReturn(FIRST_SERVICE_ID); when(mockFailingService.createName()).thenReturn(Service.class.getName()); when(mockOutOfStateHandler.isInCorrectState(mockFailingService)).thenReturn(true); BranchingServiceCollection services = createServiceCollection(); services.setOutOfStateHandler(mockOutOfStateHandler); services.setFirstServiceId(FIRST_SERVICE_ID); services.setRestartAffectedServiceOnException(true); services.addService(mockFailingService); AdaptrisMessage msg = AdaptrisMessageFactory.getDefaultInstance().newMessage(); try { services.doService(msg); fail("Mock service should fail."); } catch (ServiceException expected) { verify(mockFailingService, times(1)).requestStop(); verify(mockFailingService, times(1)).requestClose(); verify(mockFailingService, times(1)).requestInit(); verify(mockFailingService, times(1)).requestStart(); } }