summaryrefslogtreecommitdiffstats
path: root/docprocs/src/test/java/com/yahoo/docprocs/indexing/IndexingProcessorTestCase.java
blob: dc9b1ffba738efb5bd63c04a6cda2da395ffc72e (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
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.docprocs.indexing;

import com.yahoo.config.subscription.ConfigGetter;
import com.yahoo.docproc.Processing;
import com.yahoo.document.Document;
import com.yahoo.document.DocumentPut;
import com.yahoo.document.DocumentOperation;
import com.yahoo.document.DocumentType;
import com.yahoo.document.DocumentUpdate;
import com.yahoo.document.config.DocumentmanagerConfig;
import com.yahoo.document.datatypes.StringFieldValue;
import com.yahoo.document.update.AssignValueUpdate;
import com.yahoo.document.update.FieldUpdate;
import com.yahoo.document.update.ValueUpdate;
import com.yahoo.language.process.Encoder;
import com.yahoo.language.simple.SimpleLinguistics;
import com.yahoo.vespa.configdefinition.IlscriptsConfig;
import org.junit.Test;

import java.util.List;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;

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

    private static final String CONFIG_ID = "dir:src/test/cfg";
    private IndexingProcessor indexer = newProcessor(CONFIG_ID);

    @Test
    public void requireThatIndexerProcessesDocuments() {
        Document input = new Document(indexer.getDocumentTypeManager().getDocumentType("music"), "id:ns:music::");
        input.setFieldValue("artist", new StringFieldValue("69"));
        DocumentOperation op = process(new DocumentPut(input));
        assertTrue(op instanceof DocumentPut);

        Document output = ((DocumentPut)op).getDocument();
        assertEquals(new StringFieldValue("69"), output.getFieldValue("title"));
        assertEquals("music", output.getDataType().getName());
    }

    @Test
    public void requireThatIndexerForwardsDocumentsOfUnknownType() {
        Document input = new Document(new DocumentType("unknown"), "id:ns:unknown::");
        DocumentOperation output = process(new DocumentPut(input));
        assertTrue(output instanceof DocumentPut);
        assertSame(input, ((DocumentPut)output).getDocument());
    }

    @Test
    public void requireThatIndexerProcessesUpdates() {
        DocumentType inputType = indexer.getDocumentTypeManager().getDocumentType("music");
        DocumentUpdate input = new DocumentUpdate(inputType, "id:ns:music::");
        input.addFieldUpdate(FieldUpdate.createAssign(inputType.getField("isbn"), new StringFieldValue("isbnmarker")));
        input.addFieldUpdate(FieldUpdate.createAssign(inputType.getField("artist"), new StringFieldValue("69")));
        DocumentOperation output = process(input);

        assertTrue(output instanceof DocumentUpdate);
        DocumentUpdate docUpdate = (DocumentUpdate) output;

        assertEquals(3, docUpdate.fieldUpdates().size());
        {
            FieldUpdate fieldUpdate = docUpdate.getFieldUpdate("song");
            assertEquals("song", fieldUpdate.getField().getName());
            assertEquals(1, fieldUpdate.getValueUpdates().size());
            ValueUpdate<?> valueUpdate = fieldUpdate.getValueUpdate(0);
            assertTrue(valueUpdate instanceof AssignValueUpdate);
            assertEquals(new StringFieldValue("isbnmarker"), valueUpdate.getValue());
            fieldUpdate = docUpdate.getFieldUpdate("title");
            assertEquals("title", fieldUpdate.getField().getName());
            assertEquals(1, fieldUpdate.getValueUpdates().size());
            valueUpdate = fieldUpdate.getValueUpdate(0);
            assertTrue(valueUpdate instanceof AssignValueUpdate);
            assertEquals(new StringFieldValue("69"), valueUpdate.getValue());
        }

        {
            FieldUpdate fieldUpdate = docUpdate.getFieldUpdate("title");
            ValueUpdate<?> valueUpdate = fieldUpdate.getValueUpdate(0);
            assertEquals("title", fieldUpdate.getField().getName());
            assertTrue(valueUpdate instanceof AssignValueUpdate);
            assertEquals(new StringFieldValue("69"), valueUpdate.getValue());
        }
        {
            FieldUpdate fieldUpdate = docUpdate.getFieldUpdate("isbn");
            ValueUpdate<?> valueUpdate = fieldUpdate.getValueUpdate(0);
            assertEquals("isbn", fieldUpdate.getField().getName());
            assertTrue(valueUpdate instanceof AssignValueUpdate);
            assertEquals(new StringFieldValue("isbnmarker"), valueUpdate.getValue());
        }

    }

    @Test
    public void requireThatEmptyDocumentUpdateOutputDoesNotThrow() {
        DocumentType inputType = indexer.getDocumentTypeManager().getDocumentType("music");
        DocumentUpdate input = new DocumentUpdate(inputType, "id:ns:music::");
        Processing proc = new Processing();
        proc.getDocumentOperations().add(input);
        indexer.process(proc);
        assertEquals(0, proc.getDocumentOperations().size());
    }

    @Test
    public void requireThatIndexerForwardsUpdatesOfUnknownType() {
        DocumentUpdate input = new DocumentUpdate(new DocumentType("unknown"), "id:ns:music::");
        DocumentOperation output = process(input);
        assertSame(input, output);
    }

    private DocumentOperation process(DocumentOperation input) {
        Processing proc = new Processing();
        proc.getDocumentOperations().add(input);
        indexer.process(proc);

        List<DocumentOperation> lst = proc.getDocumentOperations();
        assertEquals(1, lst.size());
        return lst.get(0);
    }

    private static IndexingProcessor newProcessor(String configId) {
        return new IndexingProcessor(ConfigGetter.getConfig(DocumentmanagerConfig.class, configId),
                                     ConfigGetter.getConfig(IlscriptsConfig.class, configId),
                                     new SimpleLinguistics(),
                                     Encoder.throwsOnUse);
    }
}