// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.config.application;
import org.custommonkey.xmlunit.XMLUnit;
import org.junit.Test;
import org.w3c.dom.Document;
import org.xml.sax.SAXException;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.stream.XMLStreamException;
import javax.xml.transform.TransformerException;
import java.io.IOException;
import java.io.StringReader;
import java.util.Map;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
/**
* @author hmusum
*/
public class PropertiesProcessorTest {
static {
XMLUnit.setIgnoreWhitespace(true);
}
@Test
public void testPropertyValues() throws ParserConfigurationException, TransformerException, SAXException, IOException {
String input = "" +
"" +
" " +
" 4099" +
" 2" +
" " +
" " +
" " +
" " +
" " +
" " +
" " +
"";
PropertiesProcessor p = new PropertiesProcessor();
p.process(Xml.getDocument(new StringReader(input)));
Map properties = p.getProperties();
assertThat(properties.size(), is(2));
assertThat(properties.get("slobrok.port"), is("4099"));
assertThat(properties.get("redundancy"), is("2"));
}
@Test
public void testPropertyApplying() throws IOException, SAXException, XMLStreamException, ParserConfigurationException, TransformerException {
String input = "" +
"" +
" " +
" 4099" +
" 2" +
" music" +
" 0" +
" " +
" " +
" " +
" " +
" " +
" " +
" " +
" " +
" ${redundancy}" +
" " +
" " +
" " +
" " +
" " +
" " +
" " +
"";
String expected = "" +
"" +
" " +
" " +
" " +
" " +
" " +
" " +
" " +
" 2" +
" " +
" " +
" " +
" " +
" " +
" " +
" " +
"";
Document inputDoc = Xml.getDocument(new StringReader(input));
Document newDoc = new PropertiesProcessor().process(inputDoc);
TestBase.assertDocument(expected, newDoc);
}
// TODO: Check that warning is actually logged
@Test
public void testWarnIfDuplicatePropertyForSameEnvironment() throws IOException, SAXException, XMLStreamException, ParserConfigurationException, TransformerException {
String input = "" +
"" +
" " +
" 4099" +
" 5000" +
" 2" +
" " +
" " +
" " +
" " +
" " +
" " +
" " +
"";
String expected = "" +
"" +
" " +
" " +
" " +
" " + // Should get the last defined value
" " +
" " +
"";
Document inputDoc = Xml.getDocument(new StringReader(input));
Document newDoc = new PropertiesProcessor().process(inputDoc);
TestBase.assertDocument(expected, newDoc);
}
}