aboutsummaryrefslogtreecommitdiffstats
path: root/indexinglanguage/src/test/java/com/yahoo/vespa/indexinglanguage/SimpleAdapterFactoryTestCase.java
blob: 453e2ddbe959751dd1e1e1e5b8bf765bf02e8d28 (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.indexinglanguage;

import com.yahoo.document.DataType;
import com.yahoo.document.DocumentType;
import com.yahoo.document.DocumentUpdate;
import com.yahoo.document.Field;
import com.yahoo.document.datatypes.IntegerFieldValue;
import com.yahoo.document.update.FieldUpdate;
import com.yahoo.document.update.ValueUpdate;
import org.junit.Test;

import java.util.List;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertNotNull;

/**
 * @author <a href="mailto:magnarn@yahoo-inc.com">Magnar Nedland</a>
 */
public class SimpleAdapterFactoryTestCase {

    @Test
    public void requireThatCompleteUpdatesAreCombined() {
        DocumentType docType = new DocumentType("my_type");
        DocumentUpdate update = new DocumentUpdate(docType, "id:foo:my_type::1");
        Field field1 = new Field("int1", DataType.INT);
        Field field2 = new Field("int2", DataType.INT);
        Field field3 = new Field("int3", DataType.INT);
        docType.addField(field1);
        docType.addField(field2);
        docType.addField(field3);
        update.addFieldUpdate(FieldUpdate.createAssign(field1, new IntegerFieldValue(10)));
        update.addFieldUpdate(FieldUpdate.createAssign(field2, new IntegerFieldValue(20)));
        update.addFieldUpdate(FieldUpdate.createIncrement(field3, 30));

        SimpleAdapterFactory factory = new SimpleAdapterFactory();
        List<UpdateAdapter> adapters = factory.newUpdateAdapterList(update);
        assertEquals(2, adapters.size());
    }

    @Test
    public void requireThatFieldUpdateCanHaveManyPartialUpdatesForOneField() {
        DocumentType docType = new DocumentType("my_type");
        DocumentUpdate docUpdate = new DocumentUpdate(docType, "id:foo:my_type::1");
        Field field = new Field("my_int", DataType.INT);
        docType.addField(field);
        FieldUpdate fieldUpdate = FieldUpdate.create(field);
        fieldUpdate.addValueUpdate(ValueUpdate.createIncrement(1));
        fieldUpdate.addValueUpdate(ValueUpdate.createIncrement(2));
        fieldUpdate.addValueUpdate(ValueUpdate.createIncrement(4));
        docUpdate.addFieldUpdate(fieldUpdate);

        SimpleAdapterFactory factory = new SimpleAdapterFactory();
        List<UpdateAdapter> adapters = factory.newUpdateAdapterList(docUpdate);
        assertEquals(4, adapters.size());

        UpdateAdapter adapter = adapters.get(0);
        assertNotNull(adapter);
        assertEquals(new IntegerFieldValue(1), adapter.getInputValue("my_int"));
        assertNotNull(adapter = adapters.get(1));
        assertEquals(new IntegerFieldValue(2), adapter.getInputValue("my_int"));
        assertNotNull(adapter = adapters.get(2));
        assertEquals(new IntegerFieldValue(4), adapter.getInputValue("my_int"));
        assertNotNull(adapter = adapters.get(3));
        assertNull(adapter.getInputValue("my_int")); // always add an adapter for complete updates
    }
}