Search in sources :

Example 6 with XmlConfig

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);
}
Also used : XmlParser(org.ff4j.conf.XmlParser) XmlConfig(org.ff4j.conf.XmlConfig) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Feature(org.ff4j.core.Feature) Property(org.ff4j.property.Property) Test(org.junit.Test)

Example 7 with XmlConfig

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);
}
Also used : XmlParser(org.ff4j.conf.XmlParser) XmlConfig(org.ff4j.conf.XmlConfig) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Feature(org.ff4j.core.Feature) Property(org.ff4j.property.Property) Test(org.junit.Test)

Example 8 with XmlConfig

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);
}
Also used : XmlParser(org.ff4j.conf.XmlParser) PropertyLogLevel(org.ff4j.property.PropertyLogLevel) XmlConfig(org.ff4j.conf.XmlConfig) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Feature(org.ff4j.core.Feature) Property(org.ff4j.property.Property) Test(org.junit.Test)

Example 9 with XmlConfig

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&section=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 &quot;&amp;&gt;OK&lt;&apos;"));
    Assert.assertTrue(s.contains("<![CDATA[https://en.wikipedia.org/w/index.php?title=XML&action=edit&section=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());
}
Also used : XmlParser(org.ff4j.conf.XmlParser) XmlConfig(org.ff4j.conf.XmlConfig) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Test(org.junit.Test)

Example 10 with XmlConfig

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();
            }
        }
    }
}
Also used : XmlParser(org.ff4j.conf.XmlParser) YamlParser(org.ff4j.parser.yaml.YamlParser) XmlConfig(org.ff4j.conf.XmlConfig) ServletOutputStream(javax.servlet.ServletOutputStream) InputStream(java.io.InputStream) Date(java.util.Date) FF4jConfiguration(org.ff4j.conf.FF4jConfiguration)

Aggregations

XmlConfig (org.ff4j.conf.XmlConfig)14 XmlParser (org.ff4j.conf.XmlParser)14 InputStream (java.io.InputStream)13 Test (org.junit.Test)10 Feature (org.ff4j.core.Feature)8 ByteArrayInputStream (java.io.ByteArrayInputStream)6 Property (org.ff4j.property.Property)6 FF4jConfiguration (org.ff4j.conf.FF4jConfiguration)3 Date (java.util.Date)1 Map (java.util.Map)1 ServletOutputStream (javax.servlet.ServletOutputStream)1 FeatureStore (org.ff4j.core.FeatureStore)1 YamlParser (org.ff4j.parser.yaml.YamlParser)1 PropertyLogLevel (org.ff4j.property.PropertyLogLevel)1 PropertyStore (org.ff4j.property.store.PropertyStore)1