aboutsummaryrefslogtreecommitdiffstats
path: root/config-application-package/src/test/java/com/yahoo/config/application/PropertiesProcessorTest.java
blob: 5aaf22da2dd18ecb5a95aa3a8cb1a6fab3ca837b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.config.application;

import org.junit.Test;
import org.w3c.dom.Document;

import javax.xml.transform.TransformerException;
import java.io.StringReader;
import java.util.Map;

import static org.junit.Assert.assertEquals;

/**
 * @author hmusum
 */
public class PropertiesProcessorTest {

    @Test
    public void testPropertyValues() throws TransformerException {
        String input = """
                       <?xml version="1.0" encoding="UTF-8" standalone="no"?>
                       <services xmlns:deploy="vespa" xmlns:preprocess="properties" version="1.0">
                         <preprocess:properties>
                           <slobrok.port>4099</slobrok.port>
                           <redundancy>2</redundancy>
                         </preprocess:properties>
                         <admin version="2.0">
                           <adminserver hostalias="node0"/>
                           <slobroks>
                             <slobrok hostalias="node1" baseport="${slobrok.port}"/>
                           </slobroks>
                         </admin>
                       </services>""";

        PropertiesProcessor p = new PropertiesProcessor();
        p.process(Xml.getDocument(new StringReader(input)));
        Map<String, String> properties = p.getProperties();
        assertEquals(2, properties.size());
        assertEquals("4099", properties.get("slobrok.port"));
        assertEquals("2", properties.get("redundancy"));
    }

    @Test
    public void testPropertyApplying() throws TransformerException {
        String input = """
                       <?xml version="1.0" encoding="UTF-8" standalone="no"?>
                       <services xmlns:deploy="vespa" xmlns:preprocess="properties" version="1.0">
                         <preprocess:properties>
                           <slobrok.port>4099</slobrok.port>
                           <redundancy>2</redundancy>
                           <doctype>music</doctype>
                           <zero>0</zero>
                         </preprocess:properties>
                         <admin version="2.0">
                           <adminserver hostalias="node0"/>
                           <slobroks>
                             <slobrok hostalias="node1" baseport="${slobrok.port}"/>
                           </slobroks>
                         </admin>
                         <content id="foo" version="1.0">
                           <redundancy>${redundancy}</redundancy>
                           <documents>
                             <document mode="index" type="${doctype}.sd"/>
                           </documents>
                           <nodes>
                             <node distribution-key="${zero}" hostalias="node${zero}"/>
                           </nodes>
                         </content>
                       </services>""";

        String expected = """
                          <?xml version="1.0" encoding="UTF-8" standalone="no"?>
                          <services xmlns:deploy="vespa" xmlns:preprocess="properties" version="1.0">
                            <admin version="2.0">
                              <adminserver hostalias="node0"/>
                              <slobroks>
                                <slobrok hostalias="node1" baseport="4099"/>
                              </slobroks>
                            </admin>
                            <content id="foo" version="1.0">
                              <redundancy>2</redundancy>
                              <documents>
                                <document mode="index" type="music.sd"/>
                              </documents>
                              <nodes>
                                <node distribution-key="0" hostalias="node0"/>
                              </nodes>
                            </content>
                          </services>""";


        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 TransformerException {
        String input = """
                       <?xml version="1.0" encoding="UTF-8" standalone="no"?>
                       <services xmlns:deploy="vespa" xmlns:preprocess="properties" version="1.0">
                         <preprocess:properties>
                           <slobrok.port>4099</slobrok.port>
                           <slobrok.port>5000</slobrok.port>
                           <redundancy>2</redundancy>
                         </preprocess:properties>
                         <admin version="2.0">
                           <adminserver hostalias="node0"/>
                           <slobroks>
                             <slobrok hostalias="node1" baseport="${slobrok.port}"/>
                           </slobroks>
                         </admin>
                       </services>""";


        // Should get the last defined value
        String expected = """
                          <?xml version="1.0" encoding="UTF-8" standalone="no"?>
                          <services xmlns:deploy="vespa" xmlns:preprocess="properties" version="1.0">
                            <admin version="2.0">
                              <adminserver hostalias="node0"/>
                              <slobroks>
                                <slobrok hostalias="node1" baseport="5000"/>
                              </slobroks>
                            </admin>
                          </services>""";

        Document inputDoc = Xml.getDocument(new StringReader(input));
        Document newDoc = new PropertiesProcessor().process(inputDoc);
        TestBase.assertDocument(expected, newDoc);
    }
}