ValidateServiceExists implements UninstallServiceValidator { @Override public void validate(final UninstallServiceValidationContext validationContext) throws RestErrorException { logger.info("Validating that service exists"); final String puName = validationContext.getPuName(); ProcessingUnit pu = validationContext.getAdmin().getProcessingUnits() .waitFor(puName, TIMEOUT_SECONDS, TimeUnit.SECONDS); if (pu == null) { logger.log(Level.INFO, "Cannot uninstall service " + puName + " since it has not been discovered yet."); throw new RestErrorException(FAILED_TO_LOCATE_SERVICE, puName); } }  @Override void validate(final UninstallServiceValidationContext validationContext);  }
@Test public void test() { UninstallServiceValidationContext context = new UninstallServiceValidationContext(); DefaultAdmin admin = Mockito.mock(DefaultAdmin.class); ProcessingUnits pus = Mockito.mock(ProcessingUnits.class); Mockito.when(pus.waitFor(PU_NAME, TIMEOUT_SECONDS, TimeUnit.SECONDS)).thenReturn(null); Mockito.when(admin.getProcessingUnits()).thenReturn(pus); context.setAdmin(admin); context.setPuName(PU_NAME); ValidateServiceExists validator = new ValidateServiceExists(); try { validator.validate(context); Assert.fail("RestErrorException expected"); } catch (RestErrorException e) { Assert.assertEquals(ResponseConstants.FAILED_TO_LOCATE_SERVICE, e.getMessage()); } }