50b97e4d05efdee2534582c972438f000a1b439c
[aai/search-data-service.git] / src / test / java / org / onap / aai / sa / searchdbabstraction / searchapi / TermQueryTest.java
1 /**\r
2  * ============LICENSE_START=======================================================\r
3  * org.onap.aai\r
4  * ================================================================================\r
5  * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.\r
6  * Copyright © 2017-2018 Amdocs\r
7  * ================================================================================\r
8  * Licensed under the Apache License, Version 2.0 (the "License");\r
9  * you may not use this file except in compliance with the License.\r
10  * You may obtain a copy of the License at\r
11  *\r
12  *       http://www.apache.org/licenses/LICENSE-2.0\r
13  *\r
14  * Unless required by applicable law or agreed to in writing, software\r
15  * distributed under the License is distributed on an "AS IS" BASIS,\r
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
17  * See the License for the specific language governing permissions and\r
18  * limitations under the License.\r
19  * ============LICENSE_END=========================================================\r
20  */\r
21 package org.onap.aai.sa.searchdbabstraction.searchapi;\r
22 \r
23 import com.fasterxml.jackson.databind.ObjectMapper;\r
24 import org.junit.Assert;\r
25 import org.junit.Test;\r
26 \r
27 import java.io.IOException;\r
28 \r
29 public class TermQueryTest {\r
30 \r
31     @Test\r
32     public void testAllMethods() throws IOException {\r
33 \r
34         String field = "searchTags.nested";\r
35         String stringValue = "theValue.nested";\r
36         String termQueryWithStringValueJson = "{\"field\": \"" + field + "\", \"value\": \"" + stringValue + "\"}";\r
37         String termQueryWithStringValueExpectedES = "{\"term\": {\"" + field + "\" : \"" + stringValue + "\"}}";\r
38 \r
39         ObjectMapper mapper = new ObjectMapper();\r
40         TermQuery stringTermQuery = mapper.readValue(termQueryWithStringValueJson, TermQuery.class);\r
41         Assert.assertEquals(stringValue, stringTermQuery.getValue());\r
42         Assert.assertEquals("searchTags.nested", stringTermQuery.getField());\r
43         stringTermQuery.setOperator("operator-1");\r
44         Assert.assertEquals("operator-1", stringTermQuery.getOperator());\r
45         stringTermQuery.setSearchAnalyzer("search-1");\r
46         Assert.assertEquals("search-1", stringTermQuery.getSearchAnalyzer());\r
47 \r
48         String field1 = "searchTags-1 searchTags.second";\r
49         String stringValue1 = "theValue-1 theValue.second";\r
50         String multiFieldTermQueryJSon = "{\"field\": \"" + field1 + "\", \"value\": \"" + stringValue1 + "\"}";\r
51         TermQuery multiFieldTermQuery = mapper.readValue(multiFieldTermQueryJSon, TermQuery.class);\r
52         multiFieldTermQuery.setOperator("and");\r
53         multiFieldTermQuery.setSearchAnalyzer("search-1");\r
54         Assert.assertNotNull(multiFieldTermQuery.toElasticSearch());\r
55         Assert.assertNotNull(multiFieldTermQuery.pathForNestedField(field1));\r
56 \r
57         String field2 = "search11 search2";\r
58         String stringValue2 = "theValue1 theValue2";\r
59         String multiFieldTermJSon = "{\"field\": \"" + field2 + "\", \"value\": \"" + stringValue2 + "\"}";\r
60         TermQuery multiFieldTerm = mapper.readValue(multiFieldTermJSon, TermQuery.class);\r
61         multiFieldTerm.setOperator("or");\r
62         multiFieldTerm.setSearchAnalyzer("search-1");\r
63         Assert.assertNotNull(multiFieldTerm.toElasticSearch());\r
64     }\r
65 \r
66 }\r