summaryrefslogtreecommitdiffstats
path: root/container-core/src/test/java/com/yahoo/restapi/PathTest.java
blob: 886b3ba9c87e7b750ec79693fabf31a2e6a0e746 (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
// Copyright 2018 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.restapi;

import org.junit.Test;

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

/**
 * @author bratseth
 */
public class PathTest {

    @Test
    public void testWithPrefix() {
        // Test that a path with a prefix matches spec without the prefix
        Path path = new Path("/ball/a/1/bar/fuz", "/ball");
        assertTrue(path.matches("/a/{foo}/bar/{b}"));
        assertEquals("1", path.get("foo"));
        assertEquals("fuz", path.get("b"));

        // Also test that prefix does not cause false matches
        assertFalse(path.matches("/ball/a/{foo}/zoo/{b}"));
    }


    @Test
    public void testPath() {
        assertFalse(new Path("").matches("/a/{foo}/bar/{b}"));
        assertFalse(new Path("///").matches("/a/{foo}/bar/{b}"));
        assertFalse(new Path("///foo").matches("/a/{foo}/bar/{b}"));
        assertFalse(new Path("///bar/").matches("/a/{foo}/bar/{b}"));
        Path path = new Path("/a/1/bar/fuz");
        assertTrue(path.matches("/a/{foo}/bar/{b}"));
        assertEquals("1", path.get("foo"));
        assertEquals("fuz", path.get("b"));
    }

    @Test
    public void testPathWithRest() {
        {
            Path path = new Path("/a/1/bar/fuz/");
            assertTrue(path.matches("/a/{foo}/bar/{b}/{*}"));
            assertEquals("1", path.get("foo"));
            assertEquals("fuz", path.get("b"));
            assertEquals("", path.getRest());
        }

        {
            Path path = new Path("/a/1/bar/fuz/kanoo");
            assertTrue(path.matches("/a/{foo}/bar/{b}/{*}"));
            assertEquals("1", path.get("foo"));
            assertEquals("fuz", path.get("b"));
            assertEquals("kanoo", path.getRest());
        }

        {
            Path path = new Path("/a/1/bar/fuz/kanoo/trips");
            assertTrue(path.matches("/a/{foo}/bar/{b}/{*}"));
            assertEquals("1", path.get("foo"));
            assertEquals("fuz", path.get("b"));
            assertEquals("kanoo/trips", path.getRest());
        }

        {
            Path path = new Path("/a/1/bar/fuz/kanoo/trips/");
            assertTrue(path.matches("/a/{foo}/bar/{b}/{*}"));
            assertEquals("1", path.get("foo"));
            assertEquals("fuz", path.get("b"));
            assertEquals("kanoo/trips/", path.getRest());
        }
    }

}