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

import com.yahoo.document.*;
import com.yahoo.document.datatypes.FieldValue;
import com.yahoo.document.datatypes.IntegerFieldValue;
import com.yahoo.document.datatypes.PredicateFieldValue;
import com.yahoo.document.datatypes.Struct;
import com.yahoo.document.predicate.BinaryFormat;
import com.yahoo.document.predicate.Conjunction;
import com.yahoo.document.predicate.FeatureRange;
import com.yahoo.document.predicate.FeatureSet;
import com.yahoo.document.predicate.Predicate;
import com.yahoo.document.serialization.DeserializationException;
import com.yahoo.tensor.TensorType;
import org.junit.Test;

import javax.xml.stream.XMLStreamReader;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.Base64;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.mockito.Mockito.*;

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

    @Test
    public void requireThatPredicateFieldValuesCanBeRead() throws Exception {
        assertReadable(new Conjunction(new FeatureSet("foo", "bar"),
                                       new FeatureRange("baz", 6L, 9L)));
    }

    @Test
    public void requireThatArrayItemDeserializeExceptionIncludesFieldName() throws Exception {
        assertThrows(new Field("my_field", DataType.getArray(DataType.BYTE)),
                     "<item>-129</item>",
                     "Field 'my_field': Invalid byte \"-129\". (at line 1, column 74)");
    }

    @Test
    public void requireThatMapKeyDeserializeExceptionIncludesFieldName() throws Exception {
        assertThrows(new Field("my_field", DataType.getMap(DataType.BYTE, DataType.STRING)),
                     "<item><key>-129</key><value>foo</value></item>",
                     "Field 'my_field': Invalid byte \"-129\". (at line 1, column 78)");
    }

    @Test
    public void requireThatMapValueDeserializeExceptionIncludesFieldName() throws Exception {
        assertThrows(new Field("my_field", DataType.getMap(DataType.STRING, DataType.BYTE)),
                     "<item><key>foo</key><value>-129</value></item>",
                     "Field 'my_field': Invalid byte \"-129\". (at line 1, column 96)");
    }

    @Test
    public void requireThatStructFieldDeserializeExceptionIncludesFieldName() throws Exception {
        StructDataType structType = new StructDataType("my_struct");
        structType.addField(new Field("my_byte", DataType.BYTE));
        assertThrows(new Field("my_field", structType),
                     "<my_byte>-129</my_byte>",
                     "Field 'my_byte': Invalid byte \"-129\". (at line 1, column 80)");
    }

    @Test
    public void requireThatWSetItemDeserializeExceptionIncludesFieldName() throws Exception {
        assertThrows(new Field("my_field", DataType.getWeightedSet(DataType.BYTE)),
                     "<item>-129</item>",
                     "Field 'my_field': Invalid byte \"-129\". (at line 1, column 74)");
    }

    @Test
    public void requireThatPutsForTensorFieldsAreNotSupported() throws Exception {
        assertThrows(new Field("my_tensor", new TensorDataType(TensorType.empty)), "",
                     "Field 'my_tensor': XML input for fields of type TENSOR is not supported. Please use JSON input instead.");
    }

    private class MockedReaderFixture {
        public DocumentTypeManager mgr;
        public DocumentType docType;
        public XMLStreamReader xmlReader;
        public VespaXMLFieldReader fieldReader;

        public MockedReaderFixture() {
            mgr = new DocumentTypeManager();
            mgr.register(PositionDataType.INSTANCE);
            docType = new DocumentType("my_doc");
            docType.addField("my_pos", PositionDataType.INSTANCE);
            mgr.registerDocumentType(docType);

            xmlReader = mock(XMLStreamReader.class);
            fieldReader = new VespaXMLFieldReader(xmlReader, mgr);
        }

        public void assertReadPositionEquals(int x, int y) {
            Struct pos = new Struct(PositionDataType.INSTANCE);
            fieldReader.read(docType.getField("my_pos"), pos);

            assertEquals(new IntegerFieldValue(x), pos.getFieldValue(PositionDataType.FIELD_X));
            assertEquals(new IntegerFieldValue(y), pos.getFieldValue(PositionDataType.FIELD_Y));
        }
    }

    @Test
    public void requireThatPositionFieldCanBeReadInSingleEvent() throws Exception {
        MockedReaderFixture fixture = new MockedReaderFixture();
        XMLStreamReader xmlReader = fixture.xmlReader;

        when(xmlReader.getAttributeCount()).thenReturn(0);
        when(xmlReader.hasNext()).thenReturn(true, true, false);
        when(xmlReader.next()).thenReturn(
                XMLStreamReader.CHARACTERS, XMLStreamReader.END_ELEMENT);
        when(xmlReader.getText()).thenReturn("E3;N4");

        fixture.assertReadPositionEquals(3000000, 4000000);
    }

    @Test
    public void requireThatPositionFieldCanBeReadAcrossMultipleEvents() throws Exception {
        MockedReaderFixture fixture = new MockedReaderFixture();
        XMLStreamReader xmlReader = fixture.xmlReader;

        when(xmlReader.getAttributeCount()).thenReturn(0);
        when(xmlReader.hasNext()).thenReturn(true, true, true, true, false);
        when(xmlReader.next()).thenReturn(
                XMLStreamReader.CHARACTERS, XMLStreamReader.CHARACTERS,
                XMLStreamReader.CHARACTERS, XMLStreamReader.END_ELEMENT);
        when(xmlReader.getText()).thenReturn("E3;", "N", "4");

        fixture.assertReadPositionEquals(3000000, 4000000);
    }

    private static void assertThrows(Field field, String fieldXml, String expected) throws Exception {
        DocumentTypeManager docManager = new DocumentTypeManager();
        DocumentType docType = new DocumentType("my_type");
        docType.addField(field);
        docManager.register(docType);

        String documentXml = "<document id='id:ns:my_type::' type='my_type'><" + field.getName() + ">" +
                             fieldXml + "</" + field.getName() + "></document>";
        InputStream in = new ByteArrayInputStream(documentXml.getBytes(StandardCharsets.UTF_8));
        Document doc = new Document(docType, "id:ns:my_type::");
        try {
            new VespaXMLFieldReader(in, docManager).read(null, doc);
            fail();
        } catch (DeserializationException e) {
            assertEquals(expected, e.getMessage());
        }
    }

    private static void assertReadable(Predicate predicate) throws Exception {
        assertRead(predicate,
                   "<document id='id:ns:my_type::' type='my_type'>" +
                   "  <my_predicate>" + predicate + "</my_predicate>" +
                   "</document>");
        assertRead(predicate,
                   "<document id='id:ns:my_type::' type='my_type'>" +
                   "  <my_predicate binaryencoding='base64'>" +
                           Base64.getMimeEncoder().encodeToString(BinaryFormat.encode(predicate)) +
                   "  </my_predicate>" +
                   "</document>");
    }

    private static void assertRead(Predicate expected, String documentXml) throws Exception {
        DocumentTypeManager docManager = new DocumentTypeManager();
        DocumentType docType = new DocumentType("my_type");
        docType.addField("my_predicate", DataType.PREDICATE);
        docManager.register(docType);

        InputStream in = new ByteArrayInputStream(documentXml.getBytes(StandardCharsets.UTF_8));
        Document doc = new Document(docType, "id:ns:my_type::");
        new VespaXMLFieldReader(in, docManager).read(null, doc);
        FieldValue value = doc.getFieldValue("my_predicate");
        assertTrue(value instanceof PredicateFieldValue);
        assertEquals(expected, ((PredicateFieldValue)value).getPredicate());
    }
}