aboutsummaryrefslogtreecommitdiffstats
path: root/docproc/src/test/java/com/yahoo/docproc/CallbackTestCase.java
blob: 18e555032d7baf02013df983979424f8edd9c521 (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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.docproc;

import com.yahoo.docproc.impl.DocprocService;
import com.yahoo.docproc.impl.ProcessingEndpoint;
import com.yahoo.document.DataType;
import com.yahoo.document.DocumentId;
import com.yahoo.document.DocumentOperation;
import com.yahoo.document.DocumentPut;
import com.yahoo.document.DocumentType;
import com.yahoo.document.datatypes.StringFieldValue;
import org.junit.Before;
import org.junit.Test;

import java.util.ArrayList;
import java.util.List;

import static org.junit.Assert.assertEquals;

/**
 * @author Einar M R Rosenvinge
 */
public class CallbackTestCase {

    private DocumentPut put1;
    private DocumentPut put2;
    private final List<DocumentOperation> operations = new ArrayList<>(2);
    DocprocService service;

    @Before
    public void setUp() {
        service = new DocprocService("callback");
        service.setCallStack(new CallStack().addNext(new TestCallbackDp()));
        service.setInService(true);

        // Create documents
        DocumentType type = new DocumentType("test");
        type.addField("status", DataType.STRING);
        put1 = new DocumentPut(type, new DocumentId("id:callback:test::1"));
        put2 = new DocumentPut(type, new DocumentId("id:callback:test::2"));
        operations.add(new DocumentPut(type, new DocumentId("id:callback:test::3")));
        operations.add(new DocumentPut(type, new DocumentId("id:callback:test::4")));
    }

    @Test
    public void testProcessingWithCallbackSingleDoc() {
        ProcessingEndpoint drecv = new TestProcessingEndpoint();

        service.process(put1, drecv);
        while (service.doWork()) { }

        assertEquals(new StringFieldValue("received"), put1.getDocument().getFieldValue("status"));
    }

    @Test
    public void testProcessingWithCallbackMultipleDocs() {
        ProcessingEndpoint drecv = new TestProcessingEndpoint();

        service.process(toProcessing(operations), drecv);
        while (service.doWork()) { }

        assertEquals(new StringFieldValue("received"), ((DocumentPut) operations.get(0)).getDocument().getFieldValue("status"));
        assertEquals(new StringFieldValue("received"), ((DocumentPut) operations.get(1)).getDocument().getFieldValue("status"));
    }

    private Processing toProcessing(List<DocumentOperation> documentOperations) {
        Processing processing = new Processing();
        for (DocumentOperation op : documentOperations)
            processing.addDocumentOperation(op);
        return processing;
    }

    @Test
    public void testProcessingWithCallbackProcessing() {
        ProcessingEndpoint drecv = new TestProcessingEndpoint();

        Processing processing = new Processing("default", put2, service.getCallStack());

        service.process(processing, drecv);
        while (service.doWork()) { }

        assertEquals(new StringFieldValue("received"), put2.getDocument().getFieldValue("status"));
    }

    public class TestProcessingEndpoint implements ProcessingEndpoint {
        public void processingDone(Processing processing) {
            for (DocumentOperation op : processing.getDocumentOperations()) {
                ((DocumentPut)op).getDocument().setFieldValue("status", new StringFieldValue("received"));
            }
        }

        public void processingFailed(Processing processing, Exception exception) {
            //do nothing here for now
        }
    }

    public static class TestCallbackDp extends com.yahoo.docproc.SimpleDocumentProcessor {
    }

}