use of org.apache.nifi.minifi.c2.api.ConfigurationProviderException in project nifi-minifi by apache.
the class FileSystemConfigurationCacheTest method getConfigurationTest.
@Test
public void getConfigurationTest() throws IOException, ConfigurationProviderException {
final String pathRoot = "files";
final String pathPattern = "config";
FileSystemConfigurationCache cache = new FileSystemConfigurationCache(pathRoot, pathPattern);
Map<String, List<String>> parameters = new HashMap<>();
ConfigurationCacheFileInfo info = cache.getCacheFileInfo("text/yaml", parameters);
WriteableConfiguration configuration = info.getConfiguration(1);
assertEquals("config.text.yaml.v1", configuration.getName());
assertEquals("1", configuration.getVersion());
assertTrue(configuration.exists());
}
use of org.apache.nifi.minifi.c2.api.ConfigurationProviderException in project nifi-minifi by apache.
the class FileSystemConfigurationCacheTest method getCachedConfigurationsTest.
@Test
public void getCachedConfigurationsTest() throws IOException, ConfigurationProviderException {
final String pathRoot = "files";
final String pathPattern = "config";
FileSystemConfigurationCache cache = new FileSystemConfigurationCache(pathRoot, pathPattern);
Map<String, List<String>> parameters = new HashMap<>();
ConfigurationCacheFileInfo info = cache.getCacheFileInfo("text/yaml", parameters);
Stream<WriteableConfiguration> configs = info.getCachedConfigurations();
assertEquals(1, configs.count());
}
use of org.apache.nifi.minifi.c2.api.ConfigurationProviderException in project nifi-minifi by apache.
the class FileSystemConfigurationCacheTest method getNonexistantConfigurationTest.
@Test
public void getNonexistantConfigurationTest() throws IOException, ConfigurationProviderException {
final String pathRoot = "files";
final String pathPattern = "config";
FileSystemConfigurationCache cache = new FileSystemConfigurationCache(pathRoot, pathPattern);
Map<String, List<String>> parameters = new HashMap<>();
ConfigurationCacheFileInfo info = cache.getCacheFileInfo("test/contenttype", parameters);
WriteableConfiguration configuration = info.getConfiguration(1);
assertEquals("config.test.contenttype.v1", configuration.getName());
assertEquals("1", configuration.getVersion());
assertFalse(configuration.exists());
}
use of org.apache.nifi.minifi.c2.api.ConfigurationProviderException in project nifi-minifi by apache.
the class DelegatingConfigurationProvider method getDelegateConnection.
protected HttpURLConnection getDelegateConnection(String contentType, Map<String, List<String>> parameters) throws ConfigurationProviderException {
StringBuilder queryStringBuilder = new StringBuilder();
try {
parameters.entrySet().stream().sorted(Comparator.comparing(Map.Entry::getKey)).forEachOrdered(e -> e.getValue().stream().sorted().forEachOrdered(v -> {
try {
queryStringBuilder.append(URLEncoder.encode(e.getKey(), "UTF-8")).append("=").append(URLEncoder.encode(v, "UTF-8"));
} catch (UnsupportedEncodingException ex) {
throw new ConfigurationProviderException("Unsupported encoding.", ex).wrap();
}
queryStringBuilder.append("&");
}));
} catch (ConfigurationProviderException.Wrapper e) {
throw e.unwrap();
}
String url = "/c2/config";
if (queryStringBuilder.length() > 0) {
queryStringBuilder.setLength(queryStringBuilder.length() - 1);
url = url + "?" + queryStringBuilder.toString();
}
HttpURLConnection httpURLConnection = httpConnector.get(url);
httpURLConnection.setRequestProperty("Accepts", contentType);
try {
int responseCode;
try {
responseCode = httpURLConnection.getResponseCode();
} catch (IOException e) {
Matcher matcher = errorPattern.matcher(e.getMessage());
if (matcher.matches()) {
responseCode = Integer.parseInt(matcher.group(1));
} else {
throw e;
}
}
if (responseCode >= 400) {
String message = "";
InputStream inputStream = httpURLConnection.getErrorStream();
if (inputStream != null) {
try {
message = IOUtils.toString(inputStream, StandardCharsets.UTF_8);
} finally {
inputStream.close();
}
}
if (responseCode == 400) {
throw new InvalidParameterException(message);
} else if (responseCode == 403) {
throw new AuthorizationException("Got authorization exception from upstream server " + message);
} else {
throw new ConfigurationProviderException(message);
}
}
} catch (IOException e) {
throw new ConfigurationProviderException("Unable to get response code from upstream server.", e);
}
return httpURLConnection;
}
use of org.apache.nifi.minifi.c2.api.ConfigurationProviderException in project nifi-minifi by apache.
the class DelegatingConfigurationProviderTest method testGetConfigurationDoesntExistWithNoVersion.
@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());
}
Aggregations