aboutsummaryrefslogtreecommitdiffstats
path: root/container-search/src/test/java/com/yahoo/prelude/query/textualrepresentation/test/TextualQueryRepresentationTestCase.java
blob: f5dc598485a16f414d4446ff3df0bd98e0d44d64 (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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.prelude.query.textualrepresentation.test;

import java.io.BufferedReader;
import java.io.FileReader;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;

import com.yahoo.prelude.query.Item;

import static org.junit.jupiter.api.Assertions.assertEquals;
import com.yahoo.prelude.query.textualrepresentation.Discloser;
import com.yahoo.prelude.query.textualrepresentation.TextualQueryRepresentation;
import org.junit.jupiter.api.Test;

/**
 * Test of TextualQueryRepresentation.
 *
 * @author Tony Vaagenes
 */
@SuppressWarnings({"rawtypes", "unchecked"})
public class TextualQueryRepresentationTestCase {

    private enum ExampleEnum {
        example;
    }

    private class MockItem extends Item {
        private final String name;

        @Override
        public void setIndexName(String index) {
        }

        @Override
        public ItemType getItemType() {
            return null;
        }

        @Override
        public String getName() {
            return name;
        }

        @Override
        public int encode(ByteBuffer buffer) {
            return 0;
        }

        @Override
        public int getTermCount() {
            return 0;
        }

        @Override
        protected void appendBodyString(StringBuilder buffer) {
        }

        MockItem(String name) {
            this.name = name;
        }
    }

    private final Item basic = new MockItem("basic") {
        @Override
        public void disclose(Discloser discloser) {
            Map<Integer, Object> exampleMap = new HashMap<>();
            exampleMap.put(1, "one");
            exampleMap.put(2, "two");
            exampleMap.put(3, Arrays.asList('x', 'y', 'z'));

            discloser.addProperty("01", null);
            discloser.addProperty("02", "a string.");
            discloser.addProperty("03", 1234);
            discloser.addProperty("04", true);
            discloser.addProperty("05", ExampleEnum.example);
            discloser.addProperty("06", new int[]{1, 2, 3});
            discloser.addProperty("07", Arrays.asList('x', 'y', 'z'));
            discloser.addProperty("08", new ArrayList());
            discloser.addProperty("09", new HashSet(Arrays.asList(1, 2, 3)));
            discloser.addProperty("10", exampleMap);

            discloser.setValue("example-value: \"12\"");
        }
    };

    private final Item composite = new MockItem("composite") {
        @Override
        public void disclose(Discloser discloser) {
            discloser.addProperty("reference", basic);
            discloser.addChild(basic);
            discloser.addChild(basic.clone());
        }
    };

    private String getTextualQueryRepresentation(Item item) {
        return new TextualQueryRepresentation(item).toString();
    }

    @Test
    void testBasic() throws Exception {
        String basicText = getTextualQueryRepresentation(basic);
        assertEquals(getCorrect("basic.txt"), basicText);

    }

    @Test
    void testComposite() throws Exception {
        String compositeText = getTextualQueryRepresentation(composite);
        assertEquals(getCorrect("composite.txt"), compositeText);
    }

    private String getCorrect(String filename) throws Exception {
        BufferedReader reader = new BufferedReader(new FileReader(
                "src/test/java/com/yahoo/prelude/query/textualrepresentation/test/" + filename));
        StringBuilder result = new StringBuilder();
        for (String line; (line = reader.readLine()) != null;)
            result.append(line).append('\n');
        return result.toString();
    }

}