Fix file formatting issues
[aai/search-data-service.git] / src / test / java / org / onap / aai / sa / searchdbabstraction / searchapi / RangeQueryTest.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * Copyright © 2017-2018 Amdocs
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *       http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21 package org.onap.aai.sa.searchdbabstraction.searchapi;
22
23 import static org.hamcrest.CoreMatchers.equalTo;
24 import static org.hamcrest.CoreMatchers.is;
25 import static org.junit.Assert.assertThat;
26
27 import com.fasterxml.jackson.databind.ObjectMapper;
28 import java.io.File;
29 import java.io.IOException;
30 import org.junit.Assert;
31 import org.junit.Test;
32 import org.onap.aai.sa.rest.TestUtils;
33
34 public class RangeQueryTest {
35
36     static {
37         // Set the location of the payload translation JSON file.
38         System.setProperty("CONFIG_HOME", "src/test/resources/json");
39     }
40
41     @Test(expected = IllegalArgumentException.class)
42     public void testSetGt() {
43         RangeQuery rq = new RangeQuery();
44         rq.setLt(new String("2x"));
45         Assert.assertEquals("2x", rq.getLt());
46         Assert.assertNotNull(rq.toElasticSearch());
47         Assert.assertNotNull(rq.toString());
48         rq.setGt(new Integer(1));
49     }
50
51     @Test(expected = IllegalArgumentException.class)
52     public void testSetGte() {
53         RangeQuery rq = new RangeQuery();
54         rq.setGt(new Integer(1));
55         Assert.assertNotNull(rq.toElasticSearch());
56         Assert.assertNotNull(rq.toString());
57         rq.setGte(new Integer(1));
58     }
59
60     @Test(expected = IllegalArgumentException.class)
61     public void testSetLt() {
62         RangeQuery rq = new RangeQuery();
63         rq.setLt(new Integer(1));
64         rq.setFormat("format-1");
65         assertThat(rq.getFormat(), is(equalTo("format-1")));
66         Assert.assertNotNull(rq.toElasticSearch());
67         Assert.assertNotNull(rq.toString());
68
69         rq.setGt(new Integer(1));
70         Assert.assertNotNull(rq.toElasticSearch());
71         Assert.assertNotNull(rq.toString());
72         rq.setLt(new String("10"));
73     }
74
75     @Test(expected = IllegalArgumentException.class)
76     public void testSetLte() {
77         RangeQuery rq = new RangeQuery();
78         rq.setGt(new Integer(1));
79         rq.setTimeZone("CT");
80         assertThat(rq.getTimeZone(), is(equalTo("CT")));
81         Assert.assertNotNull(rq.toElasticSearch());
82         Assert.assertNotNull(rq.toString());
83
84         rq.setLte(new String("10"));
85     }
86
87     @Test
88     public void testSearchStatementAggregations() throws IOException {
89         File queryWithSubrangeFile = new File("src/test/resources/json/queries/query-with-subrange.json");
90         String queryWithSubrangeStr = TestUtils.readFileToString(queryWithSubrangeFile);
91
92         ObjectMapper mapper = new ObjectMapper();
93         SearchStatement ss = mapper.readValue(queryWithSubrangeStr, SearchStatement.class);
94
95         Aggregation a1 = getAggregationObject();
96         Aggregation a2 = getAggregationObject();
97         Aggregation[] aggs = new Aggregation[] {a1, a2};
98         ss.setAggregations(aggs);
99         Assert.assertNotNull(ss.toString());
100     }
101
102     private Aggregation getAggregationObject() {
103         Aggregation a = new Aggregation();
104
105         AggregationStatement as = new AggregationStatement();
106         DateHistogramAggregation dha = new DateHistogramAggregation();
107         dha.setField("field-1");
108         dha.setInterval("interval-1");
109         assertThat(dha.getInterval(), is(equalTo("interval-1")));
110         dha.setTimeZone("CT");
111         assertThat(dha.getTimeZone(), is(equalTo("CT")));
112         dha.setFormat("format-1");
113         assertThat(dha.getFormat(), is(equalTo("format-1")));
114         dha.setSize(10);
115         dha.setMinThreshold(1);
116         Assert.assertNotNull(dha.toElasticSearch());
117         Assert.assertNotNull(dha.toString());
118         as.setDateHist(dha);
119         as.toString();
120
121         as.getNestedPath();
122
123         DateRangeAggregation dra = new DateRangeAggregation();
124         dra.setField("field-1");
125         dra.setMinThreshold(1);
126         dra.setFormat("format-1");
127         assertThat(dra.getFormat(), is(equalTo("format-1")));
128         DateRange dr = new DateRange();
129         dr.setFromDate("01-12-2017");
130         assertThat(dr.getFromDate(), is(equalTo("01-12-2017")));
131         dr.setToDate("21-12-2017");
132         assertThat(dr.getToDate(), is(equalTo("21-12-2017")));
133         DateRange[] drs = {dr};
134         dra.setDateRanges(drs);
135         Assert.assertTrue(dra.getDateRanges().length == 1);
136         Assert.assertNotNull(dra.toElasticSearch());
137         Assert.assertNotNull(dra.toString());
138         as.setDateRange(dra);
139         as.toString();
140
141         as.getNestedPath();
142
143         GroupByAggregation gba = new GroupByAggregation();
144         gba.setField("field-1");
145         gba.setMinThreshold(1);
146         Assert.assertNotNull(gba.toElasticSearch());
147         Assert.assertNotNull(gba.toString());
148         as.setGroupBy(gba);
149         Assert.assertNotNull(as.toString());
150
151         a.setStatement(as);
152         Assert.assertNotNull(a.toString());
153         return a;
154     }
155 }