ValidateServiceNotAlreadyDeployed implements InstallServiceValidator { @Override public void validate(final InstallServiceValidationContext validationContext) throws RestErrorException { logger.info("Validating that service is not already deployed"); final String puName = validationContext.getPuName(); final ProcessingUnits pus = validationContext.getAdmin().getProcessingUnits(); for (ProcessingUnit pu : pus) { if (pu.getName().equals(puName)) { throw new RestErrorException(CloudifyErrorMessages.SERVICE_ALREADY_INSTALLED.getName(), puName); } } }  @Override void validate(final InstallServiceValidationContext validationContext);  }
@Test public void test() { DefaultAdmin adminMock = Mockito.mock(DefaultAdmin.class); Iterator<ProcessingUnit> iter = Mockito.mock(Iterator.class); ProcessingUnit pu = Mockito.mock(ProcessingUnit.class); Mockito.when(pu.getName()).thenReturn(PU_NAME); Mockito.when(iter.next()).thenReturn(pu); Mockito.when(iter.hasNext()).thenReturn(true, false); ProcessingUnits pus = Mockito.mock(DefaultProcessingUnits.class); Mockito.when(pus.iterator()).thenReturn(iter); Mockito.when(adminMock.getProcessingUnits()).thenReturn(pus); InstallServiceValidationContext validationContext = new InstallServiceValidationContext(); validationContext.setAdmin(adminMock); validationContext.setPuName(PU_NAME); ValidatorsTestsUtils.validate( new ValidateServiceNotAlreadyDeployed(), validationContext , CloudifyErrorMessages.SERVICE_ALREADY_INSTALLED.getName()); }