aboutsummaryrefslogtreecommitdiffstats
path: root/src/test/java/org/onap/aai/sa/searchdbabstraction/searchapi/RangeQueryTest.java
blob: e58e545ff50e038db83c81c8afcdd998989d40e7 (plain)
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/**
 * ============LICENSE_START=======================================================
 * org.onap.aai
 * ================================================================================
 * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
 * Copyright © 2017-2018 Amdocs
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */
package org.onap.aai.sa.searchdbabstraction.searchapi;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;

import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.File;
import java.io.IOException;
import org.junit.Assert;
import org.junit.Test;
import org.onap.aai.sa.rest.TestUtils;

public class RangeQueryTest {

    static {
        // Set the location of the payload translation JSON file.
        System.setProperty("CONFIG_HOME", "src/test/resources/json");
    }

    @Test(expected = IllegalArgumentException.class)
    public void testSetGt() {
        RangeQuery rq = new RangeQuery();
        rq.setLt(new String("2x"));
        Assert.assertEquals("2x", rq.getLt());
        Assert.assertNotNull(rq.toElasticSearch());
        Assert.assertNotNull(rq.toString());
        rq.setGt(new Integer(1));
    }

    @Test(expected = IllegalArgumentException.class)
    public void testSetGte() {
        RangeQuery rq = new RangeQuery();
        rq.setGt(new Integer(1));
        Assert.assertNotNull(rq.toElasticSearch());
        Assert.assertNotNull(rq.toString());
        rq.setGte(new Integer(1));
    }

    @Test(expected = IllegalArgumentException.class)
    public void testSetLt() {
        RangeQuery rq = new RangeQuery();
        rq.setLt(new Integer(1));
        rq.setFormat("format-1");
        assertThat(rq.getFormat(), is(equalTo("format-1")));
        Assert.assertNotNull(rq.toElasticSearch());
        Assert.assertNotNull(rq.toString());

        rq.setGt(new Integer(1));
        Assert.assertNotNull(rq.toElasticSearch());
        Assert.assertNotNull(rq.toString());
        rq.setLt(new String("10"));
    }

    @Test(expected = IllegalArgumentException.class)
    public void testSetLte() {
        RangeQuery rq = new RangeQuery();
        rq.setGt(new Integer(1));
        rq.setTimeZone("CT");
        assertThat(rq.getTimeZone(), is(equalTo("CT")));
        Assert.assertNotNull(rq.toElasticSearch());
        Assert.assertNotNull(rq.toString());

        rq.setLte(new String("10"));
    }

    @Test
    public void testSearchStatementAggregations() throws IOException {
        File queryWithSubrangeFile = new File("src/test/resources/json/queries/query-with-subrange.json");
        String queryWithSubrangeStr = TestUtils.readFileToString(queryWithSubrangeFile);

        ObjectMapper mapper = new ObjectMapper();
        SearchStatement ss = mapper.readValue(queryWithSubrangeStr, SearchStatement.class);

        Aggregation a1 = getAggregationObject();
        Aggregation a2 = getAggregationObject();
        Aggregation[] aggs = new Aggregation[] {a1, a2};
        ss.setAggregations(aggs);
        Assert.assertNotNull(ss.toString());
    }

    private Aggregation getAggregationObject() {
        Aggregation a = new Aggregation();

        AggregationStatement as = new AggregationStatement();
        DateHistogramAggregation dha = new DateHistogramAggregation();
        dha.setField("field-1");
        dha.setInterval("interval-1");
        assertThat(dha.getInterval(), is(equalTo("interval-1")));
        dha.setTimeZone("CT");
        assertThat(dha.getTimeZone(), is(equalTo("CT")));
        dha.setFormat("format-1");
        assertThat(dha.getFormat(), is(equalTo("format-1")));
        dha.setSize(10);
        dha.setMinThreshold(1);
        Assert.assertNotNull(dha.toElasticSearch());
        Assert.assertNotNull(dha.toString());
        as.setDateHist(dha);
        as.toString();

        as.getNestedPath();

        DateRangeAggregation dra = new DateRangeAggregation();
        dra.setField("field-1");
        dra.setMinThreshold(1);
        dra.setFormat("format-1");
        assertThat(dra.getFormat(), is(equalTo("format-1")));
        DateRange dr = new DateRange();
        dr.setFromDate("01-12-2017");
        assertThat(dr.getFromDate(), is(equalTo("01-12-2017")));
        dr.setToDate("21-12-2017");
        assertThat(dr.getToDate(), is(equalTo("21-12-2017")));
        DateRange[] drs = {dr};
        dra.setDateRanges(drs);
        Assert.assertTrue(dra.getDateRanges().length == 1);
        Assert.assertNotNull(dra.toElasticSearch());
        Assert.assertNotNull(dra.toString());
        as.setDateRange(dra);
        as.toString();

        as.getNestedPath();

        GroupByAggregation gba = new GroupByAggregation();
        gba.setField("field-1");
        gba.setMinThreshold(1);
        Assert.assertNotNull(gba.toElasticSearch());
        Assert.assertNotNull(gba.toString());
        as.setGroupBy(gba);
        Assert.assertNotNull(as.toString());

        a.setStatement(as);
        Assert.assertNotNull(a.toString());
        return a;
    }
}