DelegatingConfigurationProvider implements ConfigurationProvider { @Override public Configuration getConfiguration(String contentType, Integer version, Map<String, List<String>> parameters) throws ConfigurationProviderException { HttpURLConnection remoteC2ServerConnection = null; try { if (version == null) { remoteC2ServerConnection = getDelegateConnection(contentType, parameters); version = Integer.parseInt(remoteC2ServerConnection.getHeaderField("X-Content-Version")); if (logger.isDebugEnabled()) { logger.debug("Got current version " + version + " from upstream."); } } ConfigurationCacheFileInfo cacheFileInfo = configurationCache.getCacheFileInfo(contentType, parameters); WriteableConfiguration configuration = cacheFileInfo.getConfiguration(version); if (!configuration.exists()) { if (remoteC2ServerConnection == null) { remoteC2ServerConnection = getDelegateConnection(contentType, parameters); } try (InputStream inputStream = remoteC2ServerConnection.getInputStream(); OutputStream outputStream = configuration.getOutputStream()) { IOUtils.copy(inputStream, outputStream); } catch (IOException e) { throw new ConfigurationProviderException("Unable to copy remote configuration to cache.", e); } } return configuration; } finally { if (remoteC2ServerConnection != null) { remoteC2ServerConnection.disconnect(); } } } DelegatingConfigurationProvider(ConfigurationCache configurationCache, String delegateUrl);  DelegatingConfigurationProvider(ConfigurationCache configurationCache, HttpConnector httpConnector); @Override List<String> getContentTypes(); @Override Configuration getConfiguration(String contentType, Integer version, Map<String, List<String>> parameters); static final Pattern errorPattern; }
@Test public void testGetConfigurationExistsWithVersion() throws ConfigurationProviderException { ConfigurationCacheFileInfo configurationCacheFileInfo = mock(ConfigurationCacheFileInfo.class); WriteableConfiguration configuration = mock(WriteableConfiguration.class); when(configurationCache.getCacheFileInfo(contentType, parameters)).thenReturn(configurationCacheFileInfo); when(configurationCacheFileInfo.getConfiguration(version)).thenReturn(configuration); when(configuration.exists()).thenReturn(true); assertEquals(configuration, delegatingConfigurationProvider.getConfiguration(contentType, version, parameters)); }
@Test public void testGetConfigurationDoesntExistWithVersion() throws ConfigurationProviderException, IOException { ConfigurationCacheFileInfo configurationCacheFileInfo = mock(ConfigurationCacheFileInfo.class); WriteableConfiguration configuration = mock(WriteableConfiguration.class); byte[] payload = "payload".getBytes(StandardCharsets.UTF_8); ByteArrayOutputStream output = new ByteArrayOutputStream(); when(httpURLConnection.getInputStream()).thenReturn(new ByteArrayInputStream(payload)); when(configuration.getOutputStream()).thenReturn(output); when(configurationCache.getCacheFileInfo(contentType, parameters)).thenReturn(configurationCacheFileInfo); when(configurationCacheFileInfo.getConfiguration(version)).thenReturn(configuration); when(configuration.exists()).thenReturn(false); assertEquals(configuration, delegatingConfigurationProvider.getConfiguration(contentType, version, parameters)); assertArrayEquals(payload, output.toByteArray()); }
@Test public void testGetConfigurationExistsWithNoVersion() throws ConfigurationProviderException { parameters.remove("version"); endpointPath = "/c2/config?class=raspi3&net=edge"; initMocks(); ConfigurationCacheFileInfo configurationCacheFileInfo = mock(ConfigurationCacheFileInfo.class); WriteableConfiguration configuration = mock(WriteableConfiguration.class); when(httpURLConnection.getHeaderField("X-Content-Version")).thenReturn("2"); when(configurationCache.getCacheFileInfo(contentType, parameters)).thenReturn(configurationCacheFileInfo); when(configurationCacheFileInfo.getConfiguration(version)).thenReturn(configuration); when(configuration.exists()).thenReturn(true); assertEquals(configuration, delegatingConfigurationProvider.getConfiguration(contentType, null, parameters)); }
@Test public void testGetConfigurationDoesntExistWithNoVersion() throws ConfigurationProviderException, IOException { parameters.remove("version"); endpointPath = "/c2/config?class=raspi3&net=edge"; initMocks(); ConfigurationCacheFileInfo configurationCacheFileInfo = mock(ConfigurationCacheFileInfo.class); WriteableConfiguration configuration = mock(WriteableConfiguration.class); byte[] payload = "payload".getBytes(StandardCharsets.UTF_8); ByteArrayOutputStream output = new ByteArrayOutputStream(); when(httpURLConnection.getInputStream()).thenReturn(new ByteArrayInputStream(payload)); when(configuration.getOutputStream()).thenReturn(output); when(httpURLConnection.getHeaderField("X-Content-Version")).thenReturn("2"); when(configurationCache.getCacheFileInfo(contentType, parameters)).thenReturn(configurationCacheFileInfo); when(configurationCacheFileInfo.getConfiguration(version)).thenReturn(configuration); when(configuration.exists()).thenReturn(false); assertEquals(configuration, delegatingConfigurationProvider.getConfiguration(contentType, null, parameters)); assertArrayEquals(payload, output.toByteArray()); }