Merge "Reformat sdnr devicemanager-onf to ONAP code style"
[ccsdk/features.git] / sdnr / wt / common / src / test / java / org / onap / ccsdk / features / sdnr / wt / common / test / TestDatabaseFilterConversion.java
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP : ccsdk features
4  * ================================================================================
5  * Copyright (C) 2019 highstreet technologies GmbH Intellectual Property.
6  * All rights reserved.
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  */
22 package org.onap.ccsdk.features.sdnr.wt.common.test;
23
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertFalse;
26 import static org.junit.Assert.assertTrue;
27 import static org.junit.Assert.fail;
28
29 import org.json.JSONException;
30 import org.junit.Test;
31 import org.onap.ccsdk.features.sdnr.wt.common.database.data.DbFilter;
32
33 public class TestDatabaseFilterConversion {
34
35     @Test
36     public void testStartsWith() {
37         String re = DbFilter.createDatabaseRegex("abc*");
38         assertEquals("abc.*", re);
39     }
40
41     @Test
42     public void testEndsWith() {
43         String re = DbFilter.createDatabaseRegex("*abc");
44         assertEquals(".*abc", re);
45     }
46
47     @Test
48     public void testMultiple() {
49         String re = DbFilter.createDatabaseRegex("abc*ff*fa");
50         assertEquals("abc.*ff.*fa", re);
51     }
52
53     @Test
54     public void testPlaceholder() {
55         String re = DbFilter.createDatabaseRegex("abc?ef");
56         assertEquals("abc.{1,1}ef", re);
57     }
58
59     @Test
60     public void testCombined() {
61         String re = DbFilter.createDatabaseRegex("abc?ff*fa");
62         assertEquals("abc.{1,1}ff.*fa", re);
63     }
64
65     @Test
66     public void testFilterCheck() {
67         assertTrue(DbFilter.hasSearchParams("abc?"));
68         assertTrue(DbFilter.hasSearchParams("bac*"));
69         assertFalse(DbFilter.hasSearchParams("abc+"));
70     }
71
72     @Test
73     public void testRangeConversion() {
74         try {
75             JSONAssert.assertEquals("", "{\"query\":{\"range\":{\"port\":{\"gte\":2230,\"boost\":2}}}}",
76                     DbFilter.getRangeQuery("port", ">=2230").toJSON(), true);
77             JSONAssert.assertEquals("", "{\"query\":{\"range\":{\"port\":{\"gt\":2230,\"boost\":2}}}}",
78                     DbFilter.getRangeQuery("port", ">2230").toJSON(), true);
79             JSONAssert.assertEquals("", "{\"query\":{\"range\":{\"port\":{\"lte\":2230,\"boost\":2}}}}",
80                     DbFilter.getRangeQuery("port", "<=2230").toJSON(), true);
81             JSONAssert.assertEquals("", "{\"query\":{\"range\":{\"port\":{\"lt\":2230,\"boost\":2}}}}",
82                     DbFilter.getRangeQuery("port", "<2230").toJSON(), true);
83             JSONAssert.assertEquals("",
84                     "{\"query\":{\"range\":{\"timestamp\":{\"lt\":\"2018-01-01T23:59:59.0Z\",\"boost\":2}}}}",
85                     DbFilter.getRangeQuery("timestamp", "<2018-01-01T23:59:59.0Z").toJSON(), true);
86         } catch (JSONException e) {
87             fail(e.getMessage());
88         }
89     }
90
91 }