aboutsummaryrefslogtreecommitdiffstats
path: root/document/src/test/java/com/yahoo/document/IdIdStringTest.java
blob: 7396ec2390b4cdc9b28260e1d8ec0428828f33fd (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.document;

import com.yahoo.document.idstring.IdIdString;
import com.yahoo.document.idstring.IdString;
import org.junit.Test;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;

/**
 * Created with IntelliJ IDEA.
 * User: magnarn
 * Date: 10/16/12
 * Time: 9:10 AM
 */
public class IdIdStringTest {
    @Test
    public void requireThatIdIdStringGeneratesProperString() {
        DocumentId docId = new DocumentId(new IdIdString("namespace", "type", "g=group", "foobar"));
        assertEquals("id:namespace:type:g=group:foobar", docId.toString());
    }

    @Test
    public void requireThatEmptyKeyValuesAreOk() {
        DocumentId docId = new DocumentId(new IdIdString("namespace", "type", "", "foobar"));
        assertEquals("id:namespace:type::foobar", docId.toString());
    }

    @Test
    public void requireThatIdIdStringCanBehaveLikeGroupDoc() {
        DocumentId docId1 = new DocumentId(new IdIdString("namespace", "type", "g=foo", "foo"));
        DocumentId docId2 = new DocumentId(new IdIdString("namespace", "type", "g=foo", "bar"));
        DocumentId docId3 = new DocumentId(new IdIdString("namespace", "type", "g=bar", "baz"));
        assertEquals(docId1.getScheme().getLocation(), docId2.getScheme().getLocation());
        assert(docId1.getScheme().getLocation() != docId3.getScheme().getLocation());
    }

    @Test
    public void requireThatIdIdStringCanBehaveLikeUserDoc() {
        DocumentId docId1 = new DocumentId(new IdIdString("namespace", "type", "n=10", "foo"));
        DocumentId docId2 = new DocumentId(new IdIdString("namespace", "type", "n=10", "bar"));
        DocumentId docId3 = new DocumentId(new IdIdString("namespace", "type", "n=20", "baz"));
        assertEquals(docId1.getScheme().getLocation(), docId2.getScheme().getLocation());
        assert(docId1.getScheme().getLocation() != docId3.getScheme().getLocation());
    }

    @Test
    public void requireThatIllegalKeyValuesThrow() {
        try {
            new IdIdString("namespace", "type", "illegal=key", "foo");
            fail();
        } catch (IllegalArgumentException e) {
            assertEquals("Illegal key 'illegal'", e.getMessage());
        }
    }

    @Test
    public void requireThatKeysWithoutValuesThrow() {
        try {
            new IdIdString("namespace", "type", "illegal-pair", "foo");
            fail();
        } catch (IllegalArgumentException e) {
            assertEquals("Illegal key-value pair 'illegal-pair'", e.getMessage());
        }
    }

    @Test
    public void requireTooLongIdThrowsWhileParsing()  {
        StringBuilder builder = new StringBuilder("id:ns:type::namespacespecificpart_01");
        for (int i = 0; i < 0x10000; i++) {
            builder.append('n');
        }
        try {
            IdString.createIdString(builder.toString());
            fail();
        } catch (IllegalArgumentException e) {
            assertEquals("Document id length 65572 is longer than max length of 65536", e.getMessage());
        }
        // But there is a backdoor
        assertEquals(65572, IdString.createIdStringLessStrict(builder.toString()).toString().length());
    }

    @Test
    public void requireThatTooLongPreNamespaceSpecificThrowsWhileParsing() {
        StringBuilder builder = new StringBuilder("id:");
        for (int i = 0; i < 0xff00; i++) {
            builder.append('n');
        }
        builder.append(":type::namespacespecificpart_01");
        try {
            IdString.createIdString(builder.toString());
            fail();
        } catch (IllegalArgumentException e) {
            assertEquals("Document id prior to the namespace specific part, 65289, is longer than 65280", e.getMessage().substring(0, 77));
        }
    }
    @Test
    public void requireThatTooLongPreNamespaceSpecificThrowsOnConstruction() {
        StringBuilder builder = new StringBuilder();
        for (int i = 0; i < 0xff00; i++) {
            builder.append('n');
        }
        try {
            new IdIdString(builder.toString(), "type", "", "namespacespecificpart_01");
            fail();
        } catch (IllegalArgumentException e) {
            assertEquals("Length of namespace(65280) + doctype(4) + key/values(0), is longer than 65275", e.getMessage());
        }
    }

}