Search in sources :

Example 1 with TestDocumentProcessor1

use of com.yahoo.docproc.DocumentProcessingAbstractTestCase.TestDocumentProcessor1 in project vespa by vespa-engine.

the class SchemaMappingAndAccessesTest method testMappedDocAPI.

public void testMappedDocAPI() {
    Document doc = getDoc();
    Map<String, String> fieldMap = new HashMap<>();
    fieldMap.put("t", "title");
    fieldMap.put("a", "artist");
    ProxyDocument mapped = new ProxyDocument(new TestDocumentProcessor1(), doc, fieldMap);
    assertEquals(mapped.getFieldValue("title"), doc.getFieldValue("title"));
    assertEquals(mapped.getFieldValue(new com.yahoo.document.Field("title")), doc.getFieldValue((new com.yahoo.document.Field("title"))));
    mapped.setFieldValue("title", "foo");
    assertEquals(doc.getFieldValue("title").getWrappedValue(), "foo");
    assertEquals(mapped.getWrappedDocumentOperation().getId().toString(), "doc:map:test:1");
    assertEquals(doc, mapped);
    assertEquals(doc.toString(), mapped.toString());
    assertEquals(doc.hashCode(), mapped.hashCode());
    assertEquals(doc.clone(), mapped.clone());
    assertEquals(doc.iterator().hasNext(), mapped.iterator().hasNext());
    assertEquals(doc.getId(), mapped.getId());
    assertEquals(doc.getDataType(), mapped.getDataType());
    mapped.setLastModified(56l);
    assertEquals(doc.getLastModified(), (Long) 56l);
    assertEquals(mapped.getLastModified(), (Long) 56l);
    mapped.setId(new DocumentId("doc:map:test:2"));
    assertEquals(mapped.getId().toString(), "doc:map:test:2");
    assertEquals(doc.getId().toString(), "doc:map:test:2");
    assertEquals(doc.getHeader(), mapped.getHeader());
    assertEquals(doc.getBody(), mapped.getBody());
    assertEquals(doc.getSerializedSize(), mapped.getSerializedSize());
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    ByteArrayOutputStream bos2 = new ByteArrayOutputStream();
    mapped.serialize(bos);
    doc.serialize(bos2);
    assertEquals(bos.toString(), bos2.toString());
    assertEquals(mapped.toXml(), doc.toXml());
    assertEquals(mapped.getFieldCount(), doc.getFieldCount());
    assertTrue(mapped.getDocument() == doc);
    mapped.clear();
    assertNull(mapped.getFieldValue("title"));
    assertNull(doc.getFieldValue("title"));
    mapped.setDataType(new DocumentType("newType"));
    assertEquals(doc.getDataType().getName(), "newType");
}
Also used : HashMap(java.util.HashMap) TestDocumentProcessor1(com.yahoo.docproc.DocumentProcessingAbstractTestCase.TestDocumentProcessor1) DocumentId(com.yahoo.document.DocumentId) DocumentType(com.yahoo.document.DocumentType) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Document(com.yahoo.document.Document) Field(com.yahoo.docproc.Accesses.Field)

Example 2 with TestDocumentProcessor1

use of com.yahoo.docproc.DocumentProcessingAbstractTestCase.TestDocumentProcessor1 in project vespa by vespa-engine.

the class SchemaMappingAndAccessesTest method testMappedDocStruct.

public void testMappedDocStruct() {
    StructDataType materialsStructType = new StructDataType("materialstype");
    materialsStructType.addField(new com.yahoo.document.Field("ceiling", DataType.STRING));
    materialsStructType.addField(new com.yahoo.document.Field("walls", DataType.STRING));
    DocumentType docType = new DocumentType("album");
    docType.addField("title", DataType.STRING);
    docType.addField("artist", DataType.STRING);
    StructDataType storeStructType = new StructDataType("storetype");
    storeStructType.addField(new com.yahoo.document.Field("name", DataType.STRING));
    storeStructType.addField(new com.yahoo.document.Field("city", DataType.STRING));
    storeStructType.addField(new com.yahoo.document.Field("materials", materialsStructType));
    docType.addField("store", storeStructType);
    Document doc = new Document(docType, new DocumentId("doc:map:test:1"));
    doc.setFieldValue("title", new StringFieldValue("Black Rock"));
    doc.setFieldValue("artist", new StringFieldValue("Joe Bonamassa"));
    Struct material = new Struct(materialsStructType);
    material.setFieldValue("ceiling", new StringFieldValue("wood"));
    material.setFieldValue("walls", new StringFieldValue("brick"));
    Struct store = new Struct(storeStructType);
    store.setFieldValue("name", new StringFieldValue("Platekompaniet"));
    store.setFieldValue("city", new StringFieldValue("Trondheim"));
    store.setFieldValue(storeStructType.getField("materials"), material);
    doc.setFieldValue(docType.getField("store"), store);
    Map<String, String> fieldMap = new HashMap<>();
    fieldMap.put("t", "title");
    fieldMap.put("c", "store.city");
    fieldMap.put("w", "store.materials.walls");
    ProxyDocument mapped = new ProxyDocument(new TestDocumentProcessor1(), doc, fieldMap);
    assertEquals(new StringFieldValue("Trondheim"), mapped.getFieldValue("c"));
    assertEquals(new StringFieldValue("Black Rock"), mapped.getFieldValue("t"));
    assertEquals(new StringFieldValue("brick"), mapped.getFieldValue("w"));
    assertEquals(new StringFieldValue("brick"), material.getFieldValue("walls"));
    mapped.setFieldValue("c", new StringFieldValue("Steinkjer"));
    mapped.setFieldValue("w", new StringFieldValue("plaster"));
    assertEquals(new StringFieldValue("plaster"), mapped.getFieldValue("w"));
    assertEquals(new StringFieldValue("plaster"), material.getFieldValue("walls"));
    assertEquals(new StringFieldValue("Steinkjer"), store.getFieldValue("city"));
    assertEquals(new StringFieldValue("Steinkjer"), mapped.getFieldValue("c"));
    assertEquals(new StringFieldValue("Steinkjer"), mapped.getFieldValue("c"));
    mapped.setFieldValue("c", new StringFieldValue("Levanger"));
    assertEquals(new StringFieldValue("Levanger"), store.getFieldValue("city"));
    assertEquals(new StringFieldValue("Levanger"), mapped.getFieldValue("c"));
    mapped.setFieldValue("c", mapped.getFieldValue("c") + "Kommune");
    assertEquals(new StringFieldValue("LevangerKommune"), mapped.getFieldValue("c"));
// mapped.set(mapped.getField("c"), mapped.get("c")+"Styre");
// assertEquals(new StringFieldValue("LevangerKommuneStyre"), mapped.getFieldValue("c"));
}
Also used : HashMap(java.util.HashMap) TestDocumentProcessor1(com.yahoo.docproc.DocumentProcessingAbstractTestCase.TestDocumentProcessor1) DocumentId(com.yahoo.document.DocumentId) DocumentType(com.yahoo.document.DocumentType) Document(com.yahoo.document.Document) Struct(com.yahoo.document.datatypes.Struct) StringFieldValue(com.yahoo.document.datatypes.StringFieldValue) StructDataType(com.yahoo.document.StructDataType)

Example 3 with TestDocumentProcessor1

use of com.yahoo.docproc.DocumentProcessingAbstractTestCase.TestDocumentProcessor1 in project vespa by vespa-engine.

the class SchemaMappingAndAccessesTest method testMappedDoc.

public void testMappedDoc() {
    Document doc = getDoc();
    Map<String, String> fieldMap = new HashMap<>();
    fieldMap.put("t", "title");
    fieldMap.put("a", "artist");
    ProxyDocument mapped = new ProxyDocument(new TestDocumentProcessor1(), doc, fieldMap);
    // Document mapped=doc;
    // mapped.setFieldMap(fieldMap);
    assertEquals(new StringFieldValue("Black Rock"), mapped.getFieldValue("t"));
    // assertEquals(new StringFieldValue("Black Rock"), proxy.getFieldValue(new com.yahoo.document.Field("t")));
    assertEquals(new StringFieldValue("Joe Bonamassa").getWrappedValue(), mapped.getFieldValue("a").getWrappedValue());
    mapped.setFieldValue("t", new StringFieldValue("The Ballad Of John Henry"));
    StringFieldValue bona = new StringFieldValue("Bonamassa");
    mapped.setFieldValue("a", bona);
    // mapped.setFieldValue("a", new StringFieldValue("Bonamassa"));
    assertEquals(new StringFieldValue("The Ballad Of John Henry"), doc.getFieldValue("title"));
    assertEquals(new StringFieldValue("The Ballad Of John Henry"), mapped.getFieldValue("t"));
    assertEquals(new StringFieldValue("Bonamassa"), doc.getFieldValue("artist"));
    assertEquals(new StringFieldValue("Bonamassa"), mapped.getFieldValue("a"));
    mapped.setFieldValue("a", mapped.getFieldValue("a") + "Hughes");
    assertEquals(new StringFieldValue("BonamassaHughes"), mapped.getFieldValue("a"));
    // Verify consistency when using string values to manipluate annotation span trees
    StringFieldValue unmapped1 = (StringFieldValue) doc.getFieldValue("artist");
    StringFieldValue unmapped2 = (StringFieldValue) doc.getFieldValue("artist");
    assertTrue(unmapped1 == unmapped2);
    unmapped1.setSpanTree(new SpanTree("test"));
    assertEquals(unmapped2.getSpanTree("test").getName(), "test");
    StringFieldValue mapped1 = (StringFieldValue) mapped.getFieldValue("a");
    mapped1.setSpanTree(new SpanTree("test2"));
    StringFieldValue mapped2 = (StringFieldValue) mapped.getFieldValue("a");
    assertTrue(mapped1 == mapped2);
    assertEquals(mapped2.getSpanTree("test2").getName(), "test2");
    mapped.removeFieldValue("a");
    assertEquals(mapped.getFieldValue("a"), null);
    mapped.removeFieldValue(mapped.getField("t"));
    assertEquals(mapped.getFieldValue("t"), null);
    mapped.setFieldValue("a", new StringFieldValue("Bonamassa"));
    assertEquals(new StringFieldValue("Bonamassa"), doc.getFieldValue("artist"));
    mapped.removeFieldValue("a");
    assertEquals(mapped.getFieldValue("a"), null);
}
Also used : HashMap(java.util.HashMap) StringFieldValue(com.yahoo.document.datatypes.StringFieldValue) TestDocumentProcessor1(com.yahoo.docproc.DocumentProcessingAbstractTestCase.TestDocumentProcessor1) Document(com.yahoo.document.Document) SpanTree(com.yahoo.document.annotation.SpanTree)

Example 4 with TestDocumentProcessor1

use of com.yahoo.docproc.DocumentProcessingAbstractTestCase.TestDocumentProcessor1 in project vespa by vespa-engine.

the class SchemaMappingAndAccessesTest method testMappingSpanTrees.

public void testMappingSpanTrees() {
    Document doc = getDoc();
    Map<String, String> fieldMap = new HashMap<>();
    fieldMap.put("t", "title");
    fieldMap.put("a", "artist");
    fieldMap.put("g", "guitarist");
    ProxyDocument mapped = new ProxyDocument(new TestDocumentProcessor1(), doc, fieldMap);
    Iterator<SpanTree> itSpanTreesDoc = ((StringFieldValue) doc.getFieldValue("artist")).getSpanTrees().iterator();
    Iterator<Annotation> itAnnotDoc = itSpanTreesDoc.next().iterator();
    Iterator<SpanTree> itSpanTreesMapped = ((StringFieldValue) mapped.getFieldValue("artist")).getSpanTrees().iterator();
    Iterator<Annotation> itAnnotMapped = itSpanTreesMapped.next().iterator();
    assertEquals(itAnnotDoc.next().getType().getName(), "person");
    assertFalse(itAnnotDoc.hasNext());
    assertEquals(itAnnotMapped.next().getType().getName(), "person");
    assertFalse(itAnnotMapped.hasNext());
    AnnotationType guitaristType = new AnnotationType("guitarist");
    Annotation guitarist = new Annotation(guitaristType);
    StringFieldValue bona = new StringFieldValue("Bonamassa");
    bona.setSpanTree(new SpanTree("mytree").annotate(guitarist));
    StringFieldValue clapton = new StringFieldValue("Clapton");
    mapped.setFieldValue("a", bona);
    mapped.setFieldValue("g", clapton);
    itSpanTreesDoc = ((StringFieldValue) doc.getFieldValue("artist")).getSpanTrees().iterator();
    itAnnotDoc = itSpanTreesDoc.next().iterator();
    itSpanTreesMapped = ((StringFieldValue) mapped.getFieldValue("artist")).getSpanTrees().iterator();
    itAnnotMapped = itSpanTreesMapped.next().iterator();
    assertEquals(itAnnotDoc.next().getType().getName(), "guitarist");
    assertFalse(itAnnotDoc.hasNext());
    assertEquals(itAnnotMapped.next().getType().getName(), "guitarist");
    assertFalse(itAnnotMapped.hasNext());
    assertSame(((StringFieldValue) doc.getFieldValue("artist")).getSpanTrees().iterator().next(), ((StringFieldValue) mapped.getFieldValue("a")).getSpanTrees().iterator().next());
// assertSame(clapton, mapped.getFieldValue("g"));
// assertSame(bona, mapped.getFieldValue("a"));
}
Also used : HashMap(java.util.HashMap) StringFieldValue(com.yahoo.document.datatypes.StringFieldValue) TestDocumentProcessor1(com.yahoo.docproc.DocumentProcessingAbstractTestCase.TestDocumentProcessor1) Document(com.yahoo.document.Document) Annotation(com.yahoo.document.annotation.Annotation) AnnotationType(com.yahoo.document.annotation.AnnotationType) SpanTree(com.yahoo.document.annotation.SpanTree)

Example 5 with TestDocumentProcessor1

use of com.yahoo.docproc.DocumentProcessingAbstractTestCase.TestDocumentProcessor1 in project vespa by vespa-engine.

the class SchemaMappingAndAccessesTest method testSchemaMap.

public void testSchemaMap() {
    SchemaMap map = new SchemaMap();
    map.addMapping("mychain", "com.yahoo.MyDocProc", "mydoctype", "inDoc1", "inProc1");
    map.addMapping("mychain", "com.yahoo.MyDocProc", "mydoctype", "inDoc2", "inProc2");
    Map<Pair<String, String>, String> cMap = map.chainMap("mychain", "com.yahoo.MyDocProc");
    assertEquals("inDoc1", cMap.get(new Pair<>("mydoctype", "inProc1")));
    assertEquals("inDoc2", cMap.get(new Pair<>("mydoctype", "inProc2")));
    assertNull(cMap.get(new Pair<>("invalidtype", "inProc2")));
    Map<Pair<String, String>, String> noMap = map.chainMap("invalidchain", "com.yahoo.MyDocProc");
    Map<Pair<String, String>, String> noMap2 = map.chainMap("mychain", "com.yahoo.MyInvalidDocProc");
    assertTrue(noMap.isEmpty());
    assertTrue(noMap2.isEmpty());
    DocumentProcessor proc = new TestDocumentProcessor1();
    proc.setFieldMap(cMap);
    Map<String, String> dMap = proc.getDocMap("mydoctype");
    assertEquals("inDoc1", dMap.get("inProc1"));
    assertEquals("inDoc2", dMap.get("inProc2"));
}
Also used : DocumentProcessor(com.yahoo.docproc.DocumentProcessor) TestDocumentProcessor1(com.yahoo.docproc.DocumentProcessingAbstractTestCase.TestDocumentProcessor1) Pair(com.yahoo.collections.Pair)

Aggregations

TestDocumentProcessor1 (com.yahoo.docproc.DocumentProcessingAbstractTestCase.TestDocumentProcessor1)6 Document (com.yahoo.document.Document)4 HashMap (java.util.HashMap)4 StringFieldValue (com.yahoo.document.datatypes.StringFieldValue)3 Pair (com.yahoo.collections.Pair)2 DocumentProcessor (com.yahoo.docproc.DocumentProcessor)2 DocumentId (com.yahoo.document.DocumentId)2 DocumentType (com.yahoo.document.DocumentType)2 SpanTree (com.yahoo.document.annotation.SpanTree)2 Field (com.yahoo.docproc.Accesses.Field)1 StructDataType (com.yahoo.document.StructDataType)1 Annotation (com.yahoo.document.annotation.Annotation)1 AnnotationType (com.yahoo.document.annotation.AnnotationType)1 Struct (com.yahoo.document.datatypes.Struct)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1