aboutsummaryrefslogtreecommitdiffstats
path: root/flags/src/test/java/com/yahoo/vespa/flags/json/FlagDataTest.java
blob: 5856cf99558a315f8c608db74c9b4307cc8870ef (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.flags.json;

import com.yahoo.vespa.flags.FetchVector;
import com.yahoo.vespa.flags.RawFlag;
import org.junit.Test;

import java.util.Optional;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

/**
 * @author hakonhall
 */
public class FlagDataTest {
    private final String json = "{\n" +
            "    \"id\": \"id1\",\n" +
            "    \"rules\": [\n" +
            "        {\n" +
            "            \"conditions\": [\n" +
            "                {\n" +
            "                    \"type\": \"whitelist\",\n" +
            "                    \"dimension\": \"hostname\",\n" +
            "                    \"values\": [ \"host1\", \"host2\" ]\n" +
            "                },\n" +
            "                {\n" +
            "                    \"type\": \"blacklist\",\n" +
            "                    \"dimension\": \"application\",\n" +
            "                    \"values\": [ \"app1\", \"app2\" ]\n" +
            "                }\n" +
            "            ],\n" +
            "            \"value\": true\n" +
            "        },\n" +
            "        {\n" +
            "            \"conditions\": [\n" +
            "                {\n" +
            "                    \"type\": \"whitelist\",\n" +
            "                    \"dimension\": \"zone\",\n" +
            "                    \"values\": [ \"zone1\", \"zone2\" ]\n" +
            "                }\n" +
            "            ],\n" +
            "            \"value\": false\n" +
            "        }\n" +
            "    ],\n" +
            "    \"attributes\": {\n" +
            "        \"zone\": \"zone1\"\n" +
            "    }\n" +
            "}";

    private final FetchVector vector = new FetchVector();

    @Test
    public void test() {
        // Second rule matches with the default zone matching
        verify(Optional.of("false"), vector);

        // First rule matches only if both conditions match
        verify(Optional.of("false"), vector
                .with(FetchVector.Dimension.HOSTNAME, "host1")
                .with(FetchVector.Dimension.APPLICATION_ID, "app2"));
        verify(Optional.of("true"), vector
                .with(FetchVector.Dimension.HOSTNAME, "host1")
                .with(FetchVector.Dimension.APPLICATION_ID, "app3"));

        // Verify unsetting a dimension with null works.
        verify(Optional.of("true"), vector
                .with(FetchVector.Dimension.HOSTNAME, "host1")
                .with(FetchVector.Dimension.APPLICATION_ID, "app3")
                .with(FetchVector.Dimension.APPLICATION_ID, null));

        // No rules apply if zone is overridden to an unknown zone
        verify(Optional.empty(), vector.with(FetchVector.Dimension.ZONE_ID, "unknown zone"));
    }

    private void verify(Optional<String> expectedValue, FetchVector vector) {
        FlagData data = FlagData.deserialize(json);
        assertEquals("id1", data.id().toString());
        Optional<RawFlag> rawFlag = data.resolve(vector);

        if (expectedValue.isPresent()) {
            assertTrue(rawFlag.isPresent());
            assertEquals(expectedValue.get(), rawFlag.get().asJson());
        } else {
            assertFalse(rawFlag.isPresent());
        }

    }
}