blob: 3f3408bf93e6b9aeaccb03a79c2b0bd6a748c4ed (
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
|
// Copyright 2017 Yahoo Holdings. 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.Array;
import com.yahoo.document.datatypes.FieldValue;
import com.yahoo.document.datatypes.StringFieldValue;
import com.yahoo.document.update.AddValueUpdate;
import com.yahoo.document.update.FieldUpdate;
import com.yahoo.document.update.ValueUpdate;
import org.junit.Test;
import java.util.Iterator;
import static org.junit.Assert.*;
/**
* @author <a href="mailto:simon@yahoo-inc.com">Simon Thoresen</a>
*/
public class UriParserTestCase {
@Test
public void requireThatUriFieldsCanBeParsed() throws Exception {
DocumentTypeManager mgr = new DocumentTypeManager();
DocumentType docType = new DocumentType("my_doc");
docType.addField("my_uri", DataType.URI);
docType.addField("my_arr", DataType.getArray(DataType.URI));
mgr.registerDocumentType(docType);
VespaXMLFeedReader parser = new VespaXMLFeedReader("src/test/vespaxmlparser/test_uri.xml", mgr);
Iterator<VespaXMLFeedReader.Operation> it = parser.readAll().iterator();
Document doc = nextDocument(it);
assertNotNull(doc);
assertEquals(new StringFieldValue("scheme://host"), doc.getFieldValue("my_uri"));
assertNull(doc.getFieldValue("my_arr"));
assertNotNull(doc = nextDocument(it));
assertNull(doc.getFieldValue("my_uri"));
FieldValue val = doc.getFieldValue("my_arr");
assertNotNull(val);
assertTrue(val instanceof Array);
Array arr = (Array)val;
assertEquals(1, arr.size());
assertEquals(new StringFieldValue("scheme://host"), arr.get(0));
DocumentUpdate upd = nextUpdate(it);
assertNotNull(upd);
assertEquals(1, upd.getFieldUpdates().size());
FieldUpdate fieldUpd = upd.getFieldUpdate(0);
assertNotNull(fieldUpd);
assertEquals(docType.getField("my_arr"), fieldUpd.getField());
assertEquals(1, fieldUpd.getValueUpdates().size());
ValueUpdate valueUpd = fieldUpd.getValueUpdate(0);
assertNotNull(valueUpd);
assertTrue(valueUpd instanceof AddValueUpdate);
assertEquals(new StringFieldValue("scheme://host"), valueUpd.getValue());
assertFalse(it.hasNext());
}
private static Document nextDocument(Iterator<VespaXMLFeedReader.Operation> it) {
assertTrue(it.hasNext());
VespaXMLFeedReader.Operation op = it.next();
assertNotNull(op);
assertEquals(VespaXMLFeedReader.OperationType.DOCUMENT, op.getType());
Document doc = op.getDocument();
assertNotNull(doc);
return doc;
}
private static DocumentUpdate nextUpdate(Iterator<VespaXMLFeedReader.Operation> it) {
assertTrue(it.hasNext());
VespaXMLFeedReader.Operation op = it.next();
assertNotNull(op);
assertEquals(VespaXMLFeedReader.OperationType.UPDATE, op.getType());
DocumentUpdate upd = op.getDocumentUpdate();
assertNotNull(upd);
return upd;
}
}
|