aboutsummaryrefslogtreecommitdiffstats
path: root/messagebus/src/test/java/com/yahoo/messagebus/routing/RoutingSpecTestCase.java
blob: 669e9e25bf2c5743b3cbdc46a0262507a6a2a79d (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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.messagebus.routing;

import com.yahoo.messagebus.ConfigAgent;
import com.yahoo.messagebus.ConfigHandler;
import org.junit.jupiter.api.Test;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import static org.junit.jupiter.api.Assertions.*;

/**
 * @author Simon Thoresen Hult
 */
public class RoutingSpecTestCase {

    @Test
    void testConfig() {
        assertConfig(new RoutingSpec());
        assertConfig(new RoutingSpec().addTable(new RoutingTableSpec("mytable1")));
        assertConfig(new RoutingSpec().addTable(new RoutingTableSpec("mytable1")
                .addHop(new HopSpec("myhop1", "myselector1"))));
        assertConfig(new RoutingSpec().addTable(new RoutingTableSpec("mytable1")
                .addHop(new HopSpec("myhop1", "myselector1"))
                .addRoute(new RouteSpec("myroute1").addHop("myhop1"))));
        assertConfig(new RoutingSpec().addTable(new RoutingTableSpec("mytable1")
                .addHop(new HopSpec("myhop1", "myselector1"))
                .addHop(new HopSpec("myhop2", "myselector2"))
                .addRoute(new RouteSpec("myroute1").addHop("myhop1"))
                .addRoute(new RouteSpec("myroute2").addHop("myhop2"))
                .addRoute(new RouteSpec("myroute12").addHop("myhop1").addHop("myhop2"))));
        assertConfig(new RoutingSpec().addTable(new RoutingTableSpec("mytable1")
                .addHop(new HopSpec("myhop1", "myselector1"))
                .addHop(new HopSpec("myhop2", "myselector2"))
                .addRoute(new RouteSpec("myroute1").addHop("myhop1"))
                .addRoute(new RouteSpec("myroute2").addHop("myhop2"))
                .addRoute(new RouteSpec("myroute12").addHop("myhop1").addHop("myhop2")))
                .addTable(new RoutingTableSpec("mytable2")));
        assertEquals("routingtable[2]\n" +
                "routingtable[0].protocol \"mytable1\"\n" +
                "routingtable[1].protocol \"mytable2\"\n" +
                "routingtable[1].hop[3]\n" +
                "routingtable[1].hop[0].name \"myhop1\"\n" +
                "routingtable[1].hop[0].selector \"myselector1\"\n" +
                "routingtable[1].hop[1].name \"myhop2\"\n" +
                "routingtable[1].hop[1].selector \"myselector2\"\n" +
                "routingtable[1].hop[1].ignoreresult true\n" +
                "routingtable[1].hop[2].name \"myhop1\"\n" +
                "routingtable[1].hop[2].selector \"myselector3\"\n" +
                "routingtable[1].hop[2].recipient[2]\n" +
                "routingtable[1].hop[2].recipient[0] \"myrecipient1\"\n" +
                "routingtable[1].hop[2].recipient[1] \"myrecipient2\"\n" +
                "routingtable[1].route[1]\n" +
                "routingtable[1].route[0].name \"myroute1\"\n" +
                "routingtable[1].route[0].hop[1]\n" +
                "routingtable[1].route[0].hop[0] \"myhop1\"\n",
                new RoutingSpec()
                        .addTable(new RoutingTableSpec("mytable1"))
                        .addTable(new RoutingTableSpec("mytable2")
                                .addHop(new HopSpec("myhop1", "myselector1"))
                                .addHop(new HopSpec("myhop2", "myselector2").setIgnoreResult(true))
                                .addHop(new HopSpec("myhop1", "myselector3")
                                        .addRecipient("myrecipient1")
                                        .addRecipient("myrecipient2"))
                                .addRoute(new RouteSpec("myroute1").addHop("myhop1"))).toString());
    }

    @Test
    void testApplicationSpec() {
        assertApplicationSpec(List.of("foo"),
                              Arrays.asList("foo",
                        "*"));
        assertApplicationSpec(List.of("foo/bar"),
                              Arrays.asList("foo/bar",
                        "foo/*",
                        "*/bar",
                        "*/*"));
        assertApplicationSpec(Arrays.asList("foo/0/baz",
                        "foo/1/baz",
                        "foo/2/baz"),
                Arrays.asList("foo/0/baz",
                        "foo/1/baz",
                        "foo/2/baz",
                        "foo/0/*",
                        "foo/1/*",
                        "foo/2/*",
                        "foo/*/baz",
                        "*/0/baz",
                        "*/1/baz",
                        "*/2/baz",
                        "foo/*/*",
                        "*/0/*",
                        "*/1/*",
                        "*/2/*",
                        "*/*/baz",
                        "*/*/*"));
    }

    @Test
    void testVeriyfOk() {
        assertVerifyOk(new RoutingSpec().addTable(new RoutingTableSpec("mytable")
                .addHop(new HopSpec("hop1", "myservice1"))),
                new ApplicationSpec().addService("mytable", "myservice1"));
        assertVerifyOk(new RoutingSpec().addTable(new RoutingTableSpec("mytable")
                .addRoute(new RouteSpec("route1").addHop("myservice1"))),
                new ApplicationSpec().addService("mytable", "myservice1"));
        assertVerifyOk(new RoutingSpec().addTable(new RoutingTableSpec("mytable")
                .addHop(new HopSpec("hop1", "myservice1"))
                .addRoute(new RouteSpec("route1").addHop("hop1"))),
                new ApplicationSpec().addService("mytable", "myservice1"));
        assertVerifyOk(new RoutingSpec().addTable(new RoutingTableSpec("mytable")
                .addHop(new HopSpec("hop1", "route:route2"))
                .addHop(new HopSpec("hop2", "myservice1"))
                .addRoute(new RouteSpec("route1").addHop("hop1"))
                .addRoute(new RouteSpec("route2").addHop("hop2"))),
                new ApplicationSpec().addService("mytable", "myservice1"));
        assertVerifyOk(new RoutingSpec().addTable(new RoutingTableSpec("mytable")
                .addHop(new HopSpec("myhop1", "foo/[bar]/baz").addRecipient("foo/0/baz").addRecipient("foo/1/baz"))),
                new ApplicationSpec()
                        .addService("mytable", "foo/0/baz")
                        .addService("mytable", "foo/1/baz"));
    }

    @Test
    void testVerifyToggle() {
        assertVerifyOk(new RoutingSpec(false)
                        .addTable(new RoutingTableSpec("mytable"))
                        .addTable(new RoutingTableSpec("mytable")),
                new ApplicationSpec());
        assertVerifyOk(new RoutingSpec().addTable(new RoutingTableSpec("mytable", false)
                .addHop(new HopSpec("foo", "bar"))
                .addHop(new HopSpec("foo", "baz"))),
                new ApplicationSpec());
        assertVerifyOk(new RoutingSpec().addTable(new RoutingTableSpec("mytable")
                .addHop(new HopSpec("foo", "", false))),
                new ApplicationSpec());
        assertVerifyOk(new RoutingSpec().addTable(new RoutingTableSpec("mytable")
                .addRoute(new RouteSpec("foo", false))),
                new ApplicationSpec());
    }

    @Test
    void testVerifyFail() {
        // Duplicate table.
        assertVerifyFail(new RoutingSpec()
                        .addTable(new RoutingTableSpec("mytable"))
                        .addTable(new RoutingTableSpec("mytable")),
                new ApplicationSpec(),
                         List.of("Routing table 'mytable' is defined 2 times."));

        // Duplicate hop.
        assertVerifyFail(new RoutingSpec().addTable(new RoutingTableSpec("mytable")
                .addHop(new HopSpec("foo", "bar"))
                .addHop(new HopSpec("foo", "baz"))),
                new ApplicationSpec()
                        .addService("mytable", "bar")
                        .addService("mytable", "baz"),
                         List.of("Hop 'foo' in routing table 'mytable' is defined 2 times."));

        // Duplicate route.
        assertVerifyFail(new RoutingSpec().addTable(new RoutingTableSpec("mytable")
                .addRoute(new RouteSpec("foo").addHop("bar"))
                .addRoute(new RouteSpec("foo").addHop("baz"))),
                new ApplicationSpec()
                        .addService("mytable", "bar")
                        .addService("mytable", "baz"),
                         List.of("Route 'foo' in routing table 'mytable' is defined 2 times."));

        // Empty hop.
        assertVerifyFail(new RoutingSpec().addTable(new RoutingTableSpec("mytable")
                .addHop(new HopSpec("foo", ""))),
                new ApplicationSpec(),
                         List.of("For hop 'foo' in routing table 'mytable'; Failed to parse empty string."));

        // Empty route.
        assertVerifyFail(new RoutingSpec().addTable(new RoutingTableSpec("mytable")
                .addRoute(new RouteSpec("foo"))),
                new ApplicationSpec(),
                         List.of("Route 'foo' in routing table 'mytable' has no hops."));

        // Hop error.
        assertVerifyFail(new RoutingSpec().addTable(new RoutingTableSpec("mytable")
                .addHop(new HopSpec("foo", "bar/baz cox"))),
                new ApplicationSpec(),
                         List.of("For hop 'foo' in routing table 'mytable'; Failed to completely parse 'bar/baz cox'."));

        // Hop error in recipient.
        assertVerifyFail(new RoutingSpec().addTable(new RoutingTableSpec("mytable")
                .addHop(new HopSpec("foo", "[bar]").addRecipient("bar/baz cox"))),
                new ApplicationSpec(),
                         List.of("For recipient 'bar/baz cox' in hop 'foo' in routing table 'mytable'; Failed to completely parse 'bar/baz cox'."));

        // Hop error in route.
        assertVerifyFail(new RoutingSpec().addTable(new RoutingTableSpec("mytable")
                .addRoute(new RouteSpec("foo").addHop("bar/baz cox"))),
                new ApplicationSpec(),
                         List.of("For hop 1 in route 'foo' in routing table 'mytable'; Failed to completely parse 'bar/baz cox'."));

        // Hop not found.
        assertVerifyFail(new RoutingSpec().addTable(new RoutingTableSpec("mytable")
                .addRoute(new RouteSpec("foo").addHop("bar"))),
                new ApplicationSpec(),
                         List.of("Hop 1 in route 'foo' in routing table 'mytable' references 'bar' which is neither a service, a route nor another hop."));

        // Mismatched recipient.
        assertVerifyFail(new RoutingSpec().addTable(new RoutingTableSpec("mytable")
                .addHop(new HopSpec("foo", "bar/[baz]/cox").addRecipient("cox/0/bar"))),
                new ApplicationSpec(),
                         List.of("Selector 'bar/[baz]/cox' does not match recipient 'cox/0/bar' in hop 'foo' in routing table 'mytable'."));

        // Route not found.
        assertVerifyFail(new RoutingSpec().addTable(new RoutingTableSpec("mytable")
                .addHop(new HopSpec("foo", "route:bar"))),
                new ApplicationSpec(),
                         List.of("Hop 'foo' in routing table 'mytable' references route 'bar' which does not exist."));

        // Route not found in route.
        assertVerifyFail(new RoutingSpec().addTable(new RoutingTableSpec("mytable")
                .addRoute(new RouteSpec("foo").addHop("route:bar"))),
                new ApplicationSpec(),
                         List.of("Hop 1 in route 'foo' in routing table 'mytable' references route 'bar' which does not exist."));

        // Service not found.
        assertVerifyFail(new RoutingSpec().addTable(new RoutingTableSpec("mytable")
                .addHop(new HopSpec("foo", "bar/baz"))),
                new ApplicationSpec(),
                         List.of("Hop 'foo' in routing table 'mytable' references 'bar/baz' which is neither a service, a route nor another hop."));

        // Unexpected recipient.
        assertVerifyFail(new RoutingSpec().addTable(new RoutingTableSpec("mytable")
                .addHop(new HopSpec("foo", "bar").addRecipient("baz"))),
                new ApplicationSpec()
                        .addService("mytable", "bar")
                        .addService("mytable", "baz"),
                         List.of("Hop 'foo' in routing table 'mytable' has recipients but no policy directive."));

        // Multiple errors.
        assertVerifyFail(new RoutingSpec()
                        .addTable(new RoutingTableSpec("mytable"))
                        .addTable(new RoutingTableSpec("mytable")
                                .addHop(new HopSpec("hop1", "bar"))
                                .addHop(new HopSpec("hop1", "baz"))
                                .addHop(new HopSpec("hop2", ""))
                                .addHop(new HopSpec("hop3", "bar/baz cox"))
                                .addHop(new HopSpec("hop4", "[bar]").addRecipient("bar/baz cox"))
                                .addHop(new HopSpec("hop5", "bar/[baz]/cox").addRecipient("cox/0/bar"))
                                .addHop(new HopSpec("hop6", "route:route69"))
                                .addHop(new HopSpec("hop7", "bar/baz"))
                                .addHop(new HopSpec("hop8", "bar").addRecipient("baz"))
                                .addRoute(new RouteSpec("route1").addHop("bar"))
                                .addRoute(new RouteSpec("route1").addHop("baz"))
                                .addRoute(new RouteSpec("route2").addHop(""))
                                .addRoute(new RouteSpec("route3").addHop("bar/baz cox"))
                                .addRoute(new RouteSpec("route4").addHop("hop69"))
                                .addRoute(new RouteSpec("route5").addHop("route:route69"))),
                new ApplicationSpec()
                        .addService("mytable", "bar")
                        .addService("mytable", "baz"),
                Arrays.asList("Routing table 'mytable' is defined 2 times.",
                        "For hop 'hop2' in routing table 'mytable'; Failed to parse empty string.",
                        "For hop 'hop3' in routing table 'mytable'; Failed to completely parse 'bar/baz cox'.",
                        "For hop 1 in route 'route2' in routing table 'mytable'; Failed to parse empty string.",
                        "For hop 1 in route 'route3' in routing table 'mytable'; Failed to completely parse 'bar/baz cox'.",
                        "For recipient 'bar/baz cox' in hop 'hop4' in routing table 'mytable'; Failed to completely parse 'bar/baz cox'.",
                        "Hop 'hop1' in routing table 'mytable' is defined 2 times.",
                        "Hop 'hop6' in routing table 'mytable' references route 'route69' which does not exist.",
                        "Hop 'hop7' in routing table 'mytable' references 'bar/baz' which is neither a service, a route nor another hop.",
                        "Hop 'hop8' in routing table 'mytable' has recipients but no policy directive.",
                        "Hop 1 in route 'route4' in routing table 'mytable' references 'hop69' which is neither a service, a route nor another hop.",
                        "Hop 1 in route 'route5' in routing table 'mytable' references route 'route69' which does not exist.",
                        "Route 'route1' in routing table 'mytable' is defined 2 times.",
                        "Selector 'bar/[baz]/cox' does not match recipient 'cox/0/bar' in hop 'hop5' in routing table 'mytable'."));
    }

    private static void assertVerifyOk(RoutingSpec routing, ApplicationSpec app) {
        assertVerifyFail(routing, app, new ArrayList<String>());
    }

    private static void assertVerifyFail(RoutingSpec routing, ApplicationSpec app, List<String> expectedErrors) {
        List<String> errors = new ArrayList<>();
        routing.verify(app, errors);

        Collections.sort(errors);

        expectedErrors = new ArrayList<>(expectedErrors);
        Collections.sort(expectedErrors);
        assertEquals(expectedErrors.toString(), errors.toString());
    }

    private static void assertConfig(RoutingSpec routing) {
        assertEquals(routing, routing);
        assertEquals(routing, new RoutingSpec(routing));

        ConfigStore store = new ConfigStore();
        ConfigAgent subscriber = new ConfigAgent("raw:" + routing.toString(), store);
        subscriber.subscribe();
        assertEquals(store.routing, routing);
    }

    private static void assertApplicationSpec(List<String> services, List<String> patterns) {
        ApplicationSpec app = new ApplicationSpec();
        for (String pattern : patterns) {
            assertFalse(app.isService("foo", pattern));
            assertFalse(app.isService("bar", pattern));
        }
        for (String service : services) {
            app.addService("foo", service);
        }
        for (String pattern : patterns) {
            assertTrue(app.isService("foo", pattern));
            assertFalse(app.isService("bar", pattern));
        }
        for (String service : services) {
            app.addService("bar", service);
        }
        for (String pattern : patterns) {
            assertTrue(app.isService("foo", pattern));
            assertTrue(app.isService("bar", pattern));
        }
    }

    private static class ConfigStore implements ConfigHandler {

        RoutingSpec routing = null;

        public void setupRouting(RoutingSpec routing) {
            this.routing = routing;
        }
    }

}