use of org.ff4j.conf.XmlConfig in project ff4j by ff4j.
the class FeatureXmlParserTest method testParsingALL.
@Test
public void testParsingALL() throws IOException {
// Given
XmlParser parser = new XmlParser();
InputStream in = getClass().getClassLoader().getResourceAsStream("ff4j-parser-all.xml");
// When
XmlConfig conf = parser.parseConfigurationFile(in);
// Then
Map<String, Feature> features = conf.getFeatures();
Assert.assertNotNull(features);
// Then
Map<String, Property<?>> properties = conf.getProperties();
Assert.assertNotNull(properties);
}
use of org.ff4j.conf.XmlConfig in project ff4j by ff4j.
the class FeatureXmlParserTest method testParsingFeatures.
@Test
public void testParsingFeatures() throws IOException {
// Given
XmlParser parser = new XmlParser();
InputStream in = getClass().getClassLoader().getResourceAsStream("ff4j-parser-features.xml");
// When
XmlConfig conf = parser.parseConfigurationFile(in);
// Then
Map<String, Feature> features = conf.getFeatures();
Assert.assertNotNull(features);
// Then
Map<String, Property<?>> properties = conf.getProperties();
Assert.assertNotNull(properties);
}
use of org.ff4j.conf.XmlConfig in project ff4j by ff4j.
the class FeatureXmlParserTest method testPropertiesParsing.
@Test
public void testPropertiesParsing() throws IOException {
// Given
XmlParser parser = new XmlParser();
InputStream in = getClass().getClassLoader().getResourceAsStream("ff4j.xml");
// When
XmlConfig conf = parser.parseConfigurationFile(in);
// Then
Map<String, Feature> features = conf.getFeatures();
Assert.assertNotNull(features);
Feature f = features.get("first");
Assert.assertNotNull(f);
Assert.assertNotNull(f.getUid());
Assert.assertNotNull(f.getCustomProperties());
Assert.assertNotNull(f.getCustomProperties().get("ppint"));
Assert.assertEquals(f.getCustomProperties().get("ppint").asInt(), 12);
Assert.assertEquals(f.getCustomProperties().get("ppdouble").asDouble(), 12.5, 0);
Assert.assertEquals(f.getCustomProperties().get("ppboolean").asBoolean(), true);
Assert.assertEquals(f.getCustomProperties().get("ppstring").asString(), "hello");
Assert.assertEquals(f.getCustomProperties().get("regionIdentifier").asString(), "AMER");
Assert.assertNotNull(f.getCustomProperties().get("regionIdentifier").getFixedValues());
Assert.assertFalse(f.getCustomProperties().get("regionIdentifier").getFixedValues().isEmpty());
PropertyLogLevel pll = (PropertyLogLevel) f.getCustomProperties().get("myLogLevel");
Assert.assertEquals(pll.getValue(), LogLevel.DEBUG);
// Then
Map<String, Property<?>> properties = conf.getProperties();
Assert.assertNotNull(properties);
}
use of org.ff4j.conf.XmlConfig in project ff4j by ff4j.
the class XmlParserTest method testParseXMLWithSpecialCharacters.
@Test
public void testParseXMLWithSpecialCharacters() throws IOException {
// Given a config file with Special Characters in XML
XmlConfig xmlConfig1 = parseFile("test-parser-specialchars.xml");
// When parsed the special characters are replaced by expected values
Assert.assertEquals("description \"&>OK<'", xmlConfig1.getFeatures().get("first").getDescription());
// When parsed values protected by CDATA to interpret special chars
Assert.assertTrue(xmlConfig1.getFeatures().get("first").getCustomProperties().get("prop2").getFixedValues().contains("https://en.wikipedia.org/w/index.php?title=XML&action=edit§ion=4"));
// Given XmlConfig with special char
ByteArrayInputStream bais = (ByteArrayInputStream) new XmlParser().exportAll(xmlConfig1);
// When converting back to XML data
int n = bais.available();
byte[] bytes = new byte[n];
bais.read(bytes, 0, n);
String s = new String(bytes, StandardCharsets.UTF_8);
// Then special characters are escaped and values without quotes are protected with CDATA.
Assert.assertTrue(s.contains("description "&>OK<'"));
Assert.assertTrue(s.contains("<![CDATA[https://en.wikipedia.org/w/index.php?title=XML&action=edit§ion=4]]>"));
// When parsing back to XML
InputStream is = new ByteArrayInputStream(s.getBytes(StandardCharsets.UTF_8));
XmlConfig xmlConfig2 = new XmlParser().parseConfigurationFile(is);
// Then I get back the same values
Assert.assertEquals(xmlConfig2.getFeatures().get("first").getDescription(), xmlConfig1.getFeatures().get("first").getDescription());
}
use of org.ff4j.conf.XmlConfig in project ff4j by ff4j.
the class ConsoleOperations method exportConfiguration.
/**
* Export configuration.
*
* @param ff4j
* feature flags for java
* @param res
* http response
* @param format
* format
* @throws IOException
* error occured when exporting
*/
public static void exportConfiguration(FF4j ff4j, HttpServletResponse res, String format) throws IOException {
if (format != null) {
InputStream in = null;
ServletOutputStream sos = null;
String fileName = "ff4j-" + SDF.format(new Date()) + ".";
try {
sos = res.getOutputStream();
if (FORMAT_XML.equals(format.toLowerCase())) {
res.setContentType(CONTENT_TYPE_XML);
res.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + FORMAT_XML + "\"");
in = new XmlParser().exportAll(new XmlConfig(ff4j));
} else if (FORMAT_YML.equals(format.toLowerCase()) || FORMAT_YAML.equals(format.toLowerCase())) {
res.setContentType(CONTENT_TYPE_YAML);
res.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + FORMAT_YML + "\"");
in = new YamlParser().export(new FF4jConfiguration(ff4j));
} else if (FORMAT_PROPERTIES.equals(format.toLowerCase())) {
res.setContentType(CONTENT_TYPE_PROPERTIES);
res.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + FORMAT_PROPERTIES + "\"");
in = new YamlParser().export(new FF4jConfiguration(ff4j));
}
if (in != null) {
org.apache.commons.io.IOUtils.copy(in, sos);
}
} finally {
if (in != null) {
in.close();
}
if (sos != null) {
sos.flush();
sos.close();
}
}
}
}
Aggregations