// 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 com.yahoo.config.provision.Environment;
import com.yahoo.config.provision.InstanceName;
import com.yahoo.config.provision.RegionName;
import org.custommonkey.xmlunit.XMLUnit;
import org.junit.Test;
import org.w3c.dom.Document;
import javax.xml.transform.TransformerException;
import java.io.StringReader;
/**
* @author Ulf Lilleengen
*/
public class OverrideProcessorTest {
static {
XMLUnit.setIgnoreWhitespace(true);
}
private static final String input =
"" +
"" +
" " +
" " +
" " +
" " +
" " +
" " +
" " +
" 1" +
" " +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" " +
" " +
" \n" +
" \n" +
" \n" +
" " +
" " +
" \n" +
" \n" +
" " +
" " +
" " +
" " +
" " +
" " +
" " +
" " +
" " +
" " +
" " +
" " +
" " +
" " +
" " +
" " +
" " +
" " +
" " +
" " +
" " +
" " +
" " +
" " +
" " +
" " +
" " +
" " +
"";
@Test
public void testParsingDefault() throws TransformerException {
String expected = "" +
"" +
" " +
" " +
" " +
" " +
" 1" +
" " +
" " +
" " +
" " +
" " +
" " +
" " +
" " +
" " +
" " +
" " +
" " +
" " +
" " +
" " +
"";
assertOverride(Environment.test, RegionName.defaultName(), expected);
}
@Test
public void testParsingEnvironmentAndRegion() throws TransformerException {
String expected =
"" +
"" +
" " +
" " +
" " +
" " +
" 1" +
" " +
" " +
" " +
" " +
" " +
" " +
" " +
" " +
" " +
" " +
" " +
" " +
" " +
" " +
" " +
" " +
" " +
" " +
"";
assertOverride(Environment.from("prod"), RegionName.from("us-west"), expected);
}
@Test
public void testParsingEnvironmentUnknownRegion() throws TransformerException {
String expected =
"" +
"" +
" " +
" " +
" " +
" " +
" 1" +
" " +
" \n" +
" \n" +
" " +
" " +
" " +
" " +
" " +
" " +
" " +
" " +
" " +
" " +
" " +
" " +
" " +
" " +
" " +
" " +
"";
assertOverride(Environment.valueOf("prod"), RegionName.from("unknown"), expected);
}
@Test
public void testParsingEnvironmentNoRegion() throws TransformerException {
String expected =
"" +
"" +
" " +
" " +
" " +
" " +
" 1" +
" " +
" \n" +
" \n" +
" " +
" " +
" " +
" " +
" " +
" " +
" " +
" " +
" " +
" " +
" " +
" " +
" " +
" " +
" " +
" " +
"";
assertOverride(Environment.from("prod"), RegionName.defaultName(), expected);
}
@Test
public void testParsingDevEnvironment() throws TransformerException {
String expected =
"" +
"" +
" " +
" " +
" " +
" " +
" 1" +
" " +
" " +
" " +
" " +
" " +
" " +
" " +
" " +
" " +
" " +
" " +
" " +
" " +
" " +
" " +
"";
assertOverride(Environment.from("dev"), RegionName.defaultName(), expected);
}
@Test
public void testParsingDevEnvironmentAndRegion() throws Exception {
String expected =
"" +
"" +
" " +
" " +
" " +
" " +
" 1" +
" " +
" " +
" " +
" " +
" " +
" " +
" " +
" " +
" " +
" " +
" " +
" " +
" " +
" " +
"";
assertOverride(Environment.from("dev"), RegionName.from("us-east-1"), expected);
}
@Test
public void testParsingTestEnvironmentUnknownRegion() throws TransformerException {
String expected =
"" +
"" +
" " +
" " +
" " +
" " +
" 1" +
" " +
" " +
" " +
" " +
" " +
" " +
" " +
" " +
" " +
" " +
" " +
" " +
" " +
" " +
" " +
"";
assertOverride(Environment.from("test"), RegionName.from("us-west"), expected);
}
@Test
public void testParsingInheritEnvironment() throws TransformerException {
String expected =
"" +
"" +
" " +
" " +
" " +
" " +
" 1" +
" " +
" \n" +
" \n" +
" \n" +
" " +
" " +
// node1 is specified for us-west but does not match because region overrides implies environment=prod
" " +
" " +
" " +
" " +
" " +
" " +
" " +
" " +
" " +
" " +
" " +
"";
assertOverride(Environment.from("staging"), RegionName.from("us-west"), expected);
}
@Test(expected = IllegalArgumentException.class)
public void testParsingDifferentEnvInParentAndChild() throws TransformerException {
String in = "" +
"" +
" " +
" " +
" " +
"";
Document inputDoc = Xml.getDocument(new StringReader(in));
new OverrideProcessor(InstanceName.from("default"), Environment.from("prod"), RegionName.from("us-west")).process(inputDoc);
}
@Test(expected = IllegalArgumentException.class)
public void testParsingDifferentRegionInParentAndChild() throws TransformerException {
String in = "" +
"" +
" " +
" " +
" " +
"";
Document inputDoc = Xml.getDocument(new StringReader(in));
new OverrideProcessor(InstanceName.from("default"), Environment.defaultEnvironment(), RegionName.from("us-west")).process(inputDoc);
}
private void assertOverride(Environment environment, RegionName region, String expected) throws TransformerException {
Document inputDoc = Xml.getDocument(new StringReader(OverrideProcessorTest.input));
Document newDoc = new OverrideProcessor(InstanceName.from("default"), environment, region).process(inputDoc);
TestBase.assertDocument(expected, newDoc);
}
}