HttpContentProviderImpl implements HttpContentProvider { @NotNull public String getOctopusContent(CacheManager.CacheNames cacheName, @NotNull String uriPath, UUID correlationId) throws IOException, UnexpectedResponseCodeException, InvalidOctopusApiKeyException, InvalidOctopusUrlException, URISyntaxException, ProjectNotFoundException, NoSuchAlgorithmException, KeyStoreException, KeyManagementException, InvalidCacheConfigurationException { final URI uri = new URL(octopusUrl + uriPath).toURI(); HashMap<String,String> headers = new HashMap<>(); headers.put("X-Octopus-ApiKey", this.apiKey); try { return getContent(cacheName, uri, headers, correlationId); } catch (UnknownHostException e) { LOG.warn(String.format("%s: Unknown host exception while getting response from %s", correlationId, uri)); throw new InvalidOctopusUrlException(uri, e); } catch (UnexpectedResponseCodeException ex) { final int statusCode = ex.code; if (statusCode == 401) { throw new InvalidOctopusApiKeyException(statusCode, ex.reason); } if (statusCode == 404 && uriPath.matches(".*Projects-\\d*")) { Pattern p = Pattern.compile(".*(Projects-\\d*)$"); Matcher m = p.matcher(uriPath); m.find(); throw new ProjectNotFoundException(m.group(1)); } if (statusCode == 404) { throw new InvalidOctopusUrlException(uri); } throw ex; } } HttpContentProviderImpl(String octopusUrl, String apiKey, @NotNull Integer connectionTimeoutInMilliseconds, CacheManager cacheManager, MetricRegistry metricRegistry); String getUrl(); @NotNull String getOctopusContent(CacheManager.CacheNames cacheName, @NotNull String uriPath, UUID correlationId); @NotNull String getContent(CacheManager.CacheNames cacheName, @NotNull URI uri, UUID correlationId);  }
@Test(expectedExceptions = InvalidOctopusUrlException.class, groups = {"needs-real-server"}) public void get_response_from_real_server_with_octopus_url_with_invalid_host_throws_exception() throws ProjectNotFoundException, InvalidOctopusApiKeyException, InvalidOctopusUrlException, NoSuchAlgorithmException, KeyStoreException, KeyManagementException, UnexpectedResponseCodeException, IOException, URISyntaxException, InvalidCacheConfigurationException { HttpContentProvider contentProvider = new HttpContentProviderImpl("http: UUID correlationId = UUID.randomUUID(); contentProvider.getOctopusContent(CacheManager.CacheNames.ApiRoot, "/api", correlationId); }
@Test(expectedExceptions = InvalidOctopusUrlException.class, groups = {"needs-real-server"}) public void get_response_from_real_server_with_octopus_url_with_invalid_path_throws_exception() throws ProjectNotFoundException, InvalidOctopusApiKeyException, InvalidOctopusUrlException, NoSuchAlgorithmException, KeyStoreException, KeyManagementException, UnexpectedResponseCodeException, IOException, URISyntaxException, InvalidCacheConfigurationException { HttpContentProvider contentProvider = new HttpContentProviderImpl(realOctopusUrl + "/not-an-octopus-instance", octopusApiKey, timeoutInMilliseconds, new FakeCacheManager(), new FakeMetricRegistry()); UUID correlationId = UUID.randomUUID(); contentProvider.getOctopusContent(CacheManager.CacheNames.ApiRoot, "/api", correlationId); }
@Test(expectedExceptions = InvalidOctopusApiKeyException.class, groups = {"needs-real-server"}) public void get_response_from_real_server_with_invalid_octopus_api_key_throws_exception() throws ProjectNotFoundException, InvalidOctopusApiKeyException, InvalidOctopusUrlException, NoSuchAlgorithmException, KeyStoreException, KeyManagementException, UnexpectedResponseCodeException, IOException, URISyntaxException, InvalidCacheConfigurationException { HttpContentProvider contentProvider = new HttpContentProviderImpl(realOctopusUrl, "invalid-api-key", timeoutInMilliseconds, new FakeCacheManager(), new FakeMetricRegistry()); UUID correlationId = UUID.randomUUID(); contentProvider.getOctopusContent(CacheManager.CacheNames.ApiProjects, "/api/projects", correlationId); }
@Test(expectedExceptions = ProjectNotFoundException.class, groups = {"needs-real-server"}) public void get_response_from_real_server_with_invalid_project_id_throws_exception() throws ProjectNotFoundException, InvalidOctopusApiKeyException, InvalidOctopusUrlException, NoSuchAlgorithmException, KeyStoreException, KeyManagementException, UnexpectedResponseCodeException, IOException, URISyntaxException, InvalidCacheConfigurationException { HttpContentProvider contentProvider = new HttpContentProviderImpl(realOctopusUrl, realOctopusApiKey, timeoutInMilliseconds, new FakeCacheManager(), new FakeMetricRegistry()); UUID correlationId = UUID.randomUUID(); contentProvider.getOctopusContent(CacheManager.CacheNames.ApiProjects, "/api/projects/Projects-00", correlationId); }
@Test(groups = {"needs-real-server"}) public void get_response_from_real_server_with_from_valid_server() throws ProjectNotFoundException, InvalidOctopusApiKeyException, InvalidOctopusUrlException, NoSuchAlgorithmException, KeyStoreException, KeyManagementException, UnexpectedResponseCodeException, IOException, URISyntaxException, InvalidCacheConfigurationException { HttpContentProvider contentProvider = new HttpContentProviderImpl(realOctopusUrl, realOctopusApiKey, timeoutInMilliseconds, new FakeCacheManager(), new FakeMetricRegistry()); UUID correlationId = UUID.randomUUID(); String result = contentProvider.getOctopusContent(CacheManager.CacheNames.ApiRoot, "/api", correlationId); Assert.assertTrue(result.contains("\"Application\": \"Octopus Deploy\",")); }
@Test(groups = {"needs-real-server"}) public void saves_response_in_cache() throws IOException, URISyntaxException, ProjectNotFoundException, NoSuchAlgorithmException, UnexpectedResponseCodeException, KeyStoreException, InvalidOctopusUrlException, KeyManagementException, InvalidOctopusApiKeyException, InvalidCacheConfigurationException { CacheManager cacheManager = new FakeCacheManager(); final URI uri = new URL(realOctopusUrl + "/api").toURI(); UUID correlationId = UUID.randomUUID(); String originalValue = cacheManager.getFromCache(CacheManager.CacheNames.ApiRoot, uri, correlationId); Assert.assertEquals(originalValue, null); HttpContentProvider contentProvider = new HttpContentProviderImpl(realOctopusUrl, realOctopusApiKey, timeoutInMilliseconds, cacheManager, new FakeMetricRegistry()); String result = contentProvider.getOctopusContent(CacheManager.CacheNames.ApiRoot, "/api", correlationId); String newValue = cacheManager.getFromCache(CacheManager.CacheNames.ApiRoot, uri, correlationId); Assert.assertEquals(newValue, result); }
@Test(groups = {"needs-real-server"}) public void uses_cached_response() throws IOException, URISyntaxException, NoSuchAlgorithmException, KeyStoreException, KeyManagementException, ProjectNotFoundException, InvalidOctopusUrlException, UnexpectedResponseCodeException, InvalidOctopusApiKeyException, InvalidCacheConfigurationException { CacheManager cacheManager = new FakeCacheManager(); final URI uri = new URL(realOctopusUrl + "/api").toURI(); UUID correlationId = UUID.randomUUID(); cacheManager.addToCache(CacheManager.CacheNames.ApiRoot, uri, "cached value", correlationId); HttpContentProvider contentProvider = new HttpContentProviderImpl(realOctopusUrl, realOctopusApiKey, timeoutInMilliseconds, cacheManager, new FakeMetricRegistry()); String result = contentProvider.getOctopusContent(CacheManager.CacheNames.ApiRoot, "/api", correlationId); Assert.assertEquals(result, "cached value"); }