aboutsummaryrefslogtreecommitdiffstats
path: root/config/src/test/java/com/yahoo/config/subscription/ConfigInstanceSerializerTest.java
blob: 255380304686ccb72f34685bf68353ee1cb8446f (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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.config.subscription;

import com.yahoo.config.ConfigInstance;
import com.yahoo.foo.ArraytypesConfig;
import com.yahoo.foo.MaptypesConfig;
import com.yahoo.foo.SimpletypesConfig;
import com.yahoo.foo.SpecialtypesConfig;
import com.yahoo.foo.StructtypesConfig;
import com.yahoo.slime.JsonFormat;
import com.yahoo.slime.Slime;
import org.junit.Test;

import java.io.ByteArrayOutputStream;
import java.io.IOException;

import static com.yahoo.test.json.JsonTestHelper.assertJsonEquals;
import static com.yahoo.test.json.JsonTestHelper.inputJson;
import static org.junit.Assert.fail;

/**
 * @author Ulf Lilleengen
 * @author Vegard Sjonfjell
 */
public class ConfigInstanceSerializerTest {

    @Test
    public void test_that_leaf_types_are_serialized_to_json_types() {
        SimpletypesConfig.Builder builder = new SimpletypesConfig.Builder();
        builder.boolval(false);
        builder.stringval("foo");
        builder.intval(13);
        builder.longval(14);
        builder.doubleval(3.14);
        builder.enumval(SimpletypesConfig.Enumval.Enum.VAL2);

        final SimpletypesConfig config = new SimpletypesConfig(builder);
        final String expectedJson = inputJson(
                "{",
                "    'boolval': false,",
                "    'doubleval': 3.14,",
                "    'enumval': 'VAL2',",
                "    'intval': 13,",
                "    'longval': 14,",
                "    'stringval': 'foo'",
                "}"
        );

        assertConfigEquals(expectedJson, config);
    }

    @Test
    public void test_that_nested_structs_are_formatted_to_json() {
        StructtypesConfig.Builder builder = new StructtypesConfig.Builder();
        StructtypesConfig.Nested.Builder nestedBuilder = new StructtypesConfig.Nested.Builder();
        StructtypesConfig.Nested.Inner.Builder innerBuilder = new StructtypesConfig.Nested.Inner.Builder();
        innerBuilder.name("foo");
        innerBuilder.emails("Ulf Lilleengen@foo");
        innerBuilder.emails("Ulf Lilleengen@bar");
        innerBuilder.gender(StructtypesConfig.Nested.Inner.Gender.Enum.MALE);
        nestedBuilder.inner(innerBuilder);
        builder.nested(nestedBuilder);
        StructtypesConfig.Nestedarr.Builder nestedArrBuilder = new StructtypesConfig.Nestedarr.Builder();
        StructtypesConfig.Nestedarr.Inner.Builder innerNestedArrBuilder = new StructtypesConfig.Nestedarr.Inner.Builder();
        innerNestedArrBuilder.emails("foo@bar");
        innerNestedArrBuilder.name("bar");
        innerNestedArrBuilder.gender(StructtypesConfig.Nestedarr.Inner.Gender.Enum.FEMALE);
        nestedArrBuilder.inner(innerNestedArrBuilder);
        builder.nestedarr(nestedArrBuilder);

        final StructtypesConfig config = new StructtypesConfig(builder);
        final String expectedJson = inputJson(
                "{",
                "    'complexarr': [],",
                "    'nested': {",
                "        'inner': {",
                "            'emails': [",
                "                'Ulf Lilleengen@foo',",
                "                'Ulf Lilleengen@bar'",
                "            ],",
                "            'gender': 'MALE',",
                "            'name': 'foo'",
                "        }",
                "    },",
                "    'nestedarr': [",
                "        {",
                "            'inner': {",
                "                'emails': [",
                "                    'foo@bar'",
                "                ],",
                "                'gender': 'FEMALE',",
                "                'name': 'bar'",
                "            }",
                "        }",
                "    ],",
                "    'simple': {",
                "        'emails': [],",
                "        'gender': 'MALE',",
                "        'name': '_default_'",
                "    },",
                "    'simplearr': []",
                "}"
        );

        assertConfigEquals(expectedJson, config);
    }

    @Test
    public void test_that_arrays_are_formatted_to_json() {
        ArraytypesConfig.Builder builder = new ArraytypesConfig.Builder();
        builder.boolarr(true);
        builder.boolarr(false);
        builder.doublearr(1.2);
        builder.doublearr(1.1);
        builder.enumarr(ArraytypesConfig.Enumarr.Enum.VAL1);
        builder.enumarr(ArraytypesConfig.Enumarr.Enum.VAL2);
        builder.intarr(3);
        builder.longarr(4L);
        builder.stringarr("foo");

        final ArraytypesConfig config = new ArraytypesConfig(builder);
        final String expectedJson = inputJson(
                "{",
                "    'boolarr': [",
                "        true,",
                "        false",
                "    ],",
                "    'doublearr': [",
                "        1.2,",
                "        1.1",
                "    ],",
                "    'enumarr': [",
                "        'VAL1',",
                "        'VAL2'",
                "    ],",
                "    'intarr': [",
                "        3",
                "    ],",
                "    'longarr': [",
                "        4",
                "    ],",
                "    'stringarr': [",
                "        'foo'",
                "    ]",
                "}"
        );

        assertConfigEquals(expectedJson, config);
    }

    @Test
    public void test_that_maps_are_formatted_to_json() {
        MaptypesConfig.Builder builder = new MaptypesConfig.Builder();
        builder.boolmap("foo", true);
        builder.intmap("bar", 3);
        builder.stringmap("hei", "hallo");
        MaptypesConfig.Innermap.Builder inner = new MaptypesConfig.Innermap.Builder();
        inner.foo(133);
        builder.innermap("baz", inner);
        MaptypesConfig.Nestedmap.Builder nested = new MaptypesConfig.Nestedmap.Builder();
        nested.inner("foo", 33);
        builder.nestedmap("bim", nested);

        final MaptypesConfig config = new MaptypesConfig(builder);
        final String expectedJson = inputJson(
                "{",
                "  'boolmap': {",
                "        'foo': true",
                "    },",
                "    'doublemap': {},",
                "    'filemap': {},",
                "    'innermap': {",
                "        'baz': {",
                "            'foo': 133",
                "        }",
                "    },",
                "    'intmap': {",
                "        'bar': 3",
                "    },",
                "    'longmap': {},",
                "    'nestedmap': {",
                "        'bim': {",
                "            'inner': {",
                "                'foo': 33",
                "            }",
                "        }",
                "    },",
                "    'stringmap': {",
                "        'hei': 'hallo'",
                "    }",
                "}"
        );

        assertConfigEquals(expectedJson, config);
    }

    @Test
    public void test_that_non_standard_types_are_formatted_as_json_strings() {
        SpecialtypesConfig.Builder builder = new SpecialtypesConfig.Builder();
        builder.myfile("thefilename");
        builder.myref("thereference");

        final SpecialtypesConfig config = new SpecialtypesConfig(builder);
        final String expectedJson = inputJson(
                "{",
                "    'myfile': 'thefilename',",
                "    'myref': 'thereference'",
                "}"
        );

        assertConfigEquals(expectedJson, config);
    }

    static void assertConfigEquals(String expectedJson, ConfigInstance config) {
        Slime slime = new Slime();
        ConfigInstance.serialize(config, new ConfigInstanceSerializer(slime));
        JsonFormat jsonFormat = new JsonFormat(true);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();

        try {
            jsonFormat.encode(baos, slime);
        } catch (IOException e) {
            fail();
        }

        assertJsonEquals(baos.toString(), expectedJson);
    }

}