BootstrapUrlValidator { public void validateCloudifyUrls(final ValidationContext validationContext) throws CloudProvisioningException { final String baseCloudifyUrl = cloud.getProvider().getCloudifyUrl(); final SystemDefaultHttpClient client = new SystemDefaultHttpClient(); if (baseCloudifyUrl.endsWith(".tar.gz") || baseCloudifyUrl.endsWith(".zip")) { validateUrl(client, baseCloudifyUrl, validationContext); } else { final Set<String> scriptLanguages = getScriptLanguages(); if (scriptLanguages.contains(ScriptLanguages.LINUX_SHELL.toString())) { validateUrl(client, baseCloudifyUrl + ".tar.gz", validationContext); } if (scriptLanguages.contains(ScriptLanguages.WINDOWS_BATCH.toString())) { validateUrl(client, baseCloudifyUrl + ".zip", validationContext); } } } BootstrapUrlValidator(final Cloud cloud); void validateCloudifyUrls(final ValidationContext validationContext);  }
@Test public void testWarningOnBadHost() throws CloudProvisioningException { final Cloud cloud = new Cloud(); final ComputeTemplate template = new ComputeTemplate(); cloud.getProvider().setCloudifyUrl("http: cloud.getCloudCompute().getTemplates().put("atemplate", template); final ValidationContext context = Mockito.mock(ValidationContext.class); final MutableBoolean found = new MutableBoolean(false); Mockito.doAnswer(new Answer<Void>() { @Override public Void answer(final InvocationOnMock invocation) throws Throwable { final Object[] args = invocation.getArguments(); if (args[0].equals(ValidationMessageType.TOP_LEVEL_VALIDATION_MESSAGE) && args[1].toString().contains("Unable to validate URL")) { found.setValue(true); } return null; } }).when(context).validationOngoingEvent(Matchers.any(ValidationMessageType.class), Matchers.any(String.class)); new BootstrapUrlValidator(cloud).validateCloudifyUrls(context); Assert.assertTrue("Missing validation event", found.booleanValue()); }
@Test public void testProxy() throws Exception { final Cloud cloud = new Cloud(); final ComputeTemplate template = new ComputeTemplate(); cloud.getProvider().setCloudifyUrl("http: cloud.getCloudCompute().getTemplates().put("atemplate", template); final ValidationContext context = Mockito.mock(ValidationContext.class); final AtomicInteger counter = new AtomicInteger(); final ProxyHandler handler = new ProxyHandler(counter); final Server httpServer = new Server(18080); httpServer.setHandler(handler); final String proxyHostBefore = System.getProperty(HTTP_PROXY_HOST); final String proxyPortBefore = System.getProperty(HTTP_PROXY_PORT); System.setProperty(HTTP_PROXY_HOST, "localhost"); System.setProperty(HTTP_PROXY_PORT, "" + PROXY_PORT); try { httpServer.start(); try { new BootstrapUrlValidator(cloud).validateCloudifyUrls(context); Assert.fail("Expected a validation error"); } catch (final CloudProvisioningException e) { System.out.println(e); } Assert.assertEquals("Missing proxy access", 1, counter.get()); } finally { try { httpServer.stop(); } catch (final Exception e) { } if (proxyHostBefore == null) { System.clearProperty(HTTP_PROXY_HOST); } else { System.setProperty(HTTP_PROXY_HOST, proxyHostBefore); } if (proxyPortBefore == null) { System.clearProperty(HTTP_PROXY_PORT); } else { System.setProperty(HTTP_PROXY_PORT, proxyPortBefore); } } }