1 /*******************************************************************************
2 * ============LICENSE_START========================================================================
3 * ONAP : ccsdk feature sdnr wt
4 * =================================================================================================
5 * Copyright (C) 2019 highstreet technologies GmbH Intellectual Property. All rights reserved.
6 * =================================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
8 * in compliance with the License. You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software distributed under the License
13 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
14 * or implied. See the License for the specific language governing permissions and limitations under
16 * ============LICENSE_END==========================================================================
17 ******************************************************************************/
18 package org.onap.ccsdk.features.sdnr.wt.common.test;
20 import static org.junit.Assert.assertEquals;
21 import static org.junit.Assert.assertFalse;
22 import static org.junit.Assert.assertTrue;
23 import static org.junit.Assert.fail;
25 import org.json.JSONException;
26 import org.junit.Test;
27 import org.onap.ccsdk.features.sdnr.wt.common.database.data.DbFilter;
29 public class TestDatabaseFilterConversion {
32 public void testStartsWith() {
33 String re=DbFilter.createDatabaseRegex("abc*");
34 assertEquals("abc.*", re);
37 public void testEndsWith() {
38 String re=DbFilter.createDatabaseRegex("*abc");
39 assertEquals(".*abc", re);
42 public void testMultiple() {
43 String re=DbFilter.createDatabaseRegex("abc*ff*fa");
44 assertEquals("abc.*ff.*fa", re);
48 public void testPlaceholder() {
49 String re=DbFilter.createDatabaseRegex("abc?ef");
50 assertEquals("abc.{1,1}ef", re);
53 public void testCombined() {
54 String re=DbFilter.createDatabaseRegex("abc?ff*fa");
55 assertEquals("abc.{1,1}ff.*fa", re);
58 public void testFilterCheck() {
59 assertTrue(DbFilter.hasSearchParams("abc?"));
60 assertTrue(DbFilter.hasSearchParams("bac*"));
61 assertFalse(DbFilter.hasSearchParams("abc+"));
64 public void testRangeConversion() {
66 JSONAssert.assertEquals("", "{\"query\":{\"range\":{\"port\":{\"gte\":2230,\"boost\":2}}}}",
67 DbFilter.getRangeQuery("port", ">=2230").toJSON(),true);
68 JSONAssert.assertEquals("", "{\"query\":{\"range\":{\"port\":{\"gt\":2230,\"boost\":2}}}}",
69 DbFilter.getRangeQuery("port", ">2230").toJSON(),true);
70 JSONAssert.assertEquals("", "{\"query\":{\"range\":{\"port\":{\"lte\":2230,\"boost\":2}}}}",
71 DbFilter.getRangeQuery("port", "<=2230").toJSON(),true);
72 JSONAssert.assertEquals("", "{\"query\":{\"range\":{\"port\":{\"lt\":2230,\"boost\":2}}}}",
73 DbFilter.getRangeQuery("port", "<2230").toJSON(),true);
74 JSONAssert.assertEquals("", "{\"query\":{\"range\":{\"timestamp\":{\"lt\":\"2018-01-01T23:59:59.0Z\",\"boost\":2}}}}",
75 DbFilter.getRangeQuery("timestamp", "<2018-01-01T23:59:59.0Z").toJSON(),true);
76 } catch (JSONException e) {