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

import com.yahoo.document.datatypes.ByteFieldValue;
import com.yahoo.document.datatypes.DoubleFieldValue;
import com.yahoo.document.datatypes.IntegerFieldValue;
import com.yahoo.document.datatypes.LongFieldValue;
import org.junit.Before;
import org.junit.Test;

import java.util.HashMap;

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

/**
 * @author thomasg
 */
public class DocumentCalculatorTestCase {

    DocumentType testDocType = null;
    DocumentTypeManager docMan;
    Document doc;

    @Before
    public void setUp() {
        docMan = new DocumentTypeManager();
        testDocType = new DocumentType("testdoc");

        testDocType.addField("byteattr", DataType.BYTE);
        testDocType.addField("intattr", DataType.INT);
        testDocType.addField("longattr", DataType.LONG);
        testDocType.addField("doubleattr", DataType.DOUBLE);
        testDocType.addField("missingattr", DataType.INT);

        docMan.registerDocumentType(testDocType);
        doc = new Document(testDocType, new DocumentId("id:ns:testdoc::testdoc:http://www.ntnu.no/"));
        doc.setFieldValue(testDocType.getField("byteattr"), new ByteFieldValue((byte)32));
        doc.setFieldValue(testDocType.getField("intattr"), new IntegerFieldValue(468));
        doc.setFieldValue(testDocType.getField("longattr"), new LongFieldValue((long)327));
        doc.setFieldValue(testDocType.getField("doubleattr"), new DoubleFieldValue(25.0));
    }

    @Test
    public void testConstant() throws Exception {
        DocumentCalculator calculator = new DocumentCalculator("4.0");
        assertEquals(4.0, calculator.evaluate(doc, new HashMap<>()));
    }

    @Test
    public void testSimple() throws Exception {
        DocumentCalculator calculator = new DocumentCalculator("(3 + 5) / 2");
        assertEquals(4.0, calculator.evaluate(doc, new HashMap<>()));
    }

    @Test
    public void testDivideByZero() throws Exception {
        DocumentCalculator calculator = new DocumentCalculator("(3 + 5) / 0");
        try {
            System.out.println(calculator.evaluate(doc, new HashMap<>()));
            assertTrue(false);
        } catch (IllegalArgumentException e) {
            // ok
        }
    }

    @Test
    public void testModByZero() throws Exception {
        DocumentCalculator calculator = new DocumentCalculator("(3 + 5) % 0");
        try {
            System.out.println(calculator.evaluate(doc, new HashMap<>()));
            assertTrue(false);
        } catch (IllegalArgumentException e) {
            // ok
        }
    }

    @Test
    public void testFieldDivideByZero() throws Exception {
        try {
            DocumentCalculator calculator = new DocumentCalculator("(testdoc.byteattr + testdoc.intattr) / testdoc.doubleattr");
            doc.setFieldValue("doubleattr", new DoubleFieldValue(0.0));
            calculator.evaluate(doc, new HashMap<>());
            assertTrue(false);
        } catch (IllegalArgumentException e) {
            // ok
        }
    }

    @Test
    public void testVariables() throws Exception {
        HashMap<String, Object> vars = new HashMap<>();
        vars.put("x", Double.valueOf(3.0));
        vars.put("y", Double.valueOf(5.0));
        DocumentCalculator calculator = new DocumentCalculator("($x + $y) / 2");
        assertEquals(4.0, calculator.evaluate(doc, vars));
    }

    @Test
    public void testFields() throws Exception {
        DocumentCalculator calculator = new DocumentCalculator("(testdoc.byteattr + testdoc.intattr) / testdoc.doubleattr");
        assertEquals(20.0, calculator.evaluate(doc, new HashMap<String, Object>()));
    }

    @Test
    public void testMissingField() throws Exception {
        try {
            DocumentCalculator calculator = new DocumentCalculator("(testdoc.nosuchattribute + testdoc.intattr) / testdoc.doubleattr");
            calculator.evaluate(doc, new HashMap<>());
            assertTrue(false);
        } catch (IllegalArgumentException e) {
            // ok
        }
    }

    @Test
    public void testFieldNotSet() throws Exception {
        try {
            DocumentCalculator calculator = new DocumentCalculator("(testdoc.missingattr + testdoc.intattr) / testdoc.doubleattr");
            calculator.evaluate(doc, new HashMap<>());
            assertTrue(false);
        } catch (IllegalArgumentException e) {
            // ok
        }
    }

}