Removing deprecated DMAAP library
[policy/drools-pdp.git] / policy-management / src / test / java / org / onap / policy / drools / protocol / coders / JsonProtocolFilterTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP
4  * ================================================================================
5  * Copyright (C) 2017-2019 AT&T Intellectual Property. All rights reserved.
6  * Modifications Copyright (C) 2024 Nordix Foundation.
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.policy.drools.protocol.coders;
23
24 import static org.junit.jupiter.api.Assertions.assertEquals;
25 import static org.junit.jupiter.api.Assertions.assertFalse;
26 import static org.junit.jupiter.api.Assertions.assertThrows;
27 import static org.junit.jupiter.api.Assertions.assertTrue;
28
29 import org.junit.jupiter.api.Test;
30
31 class JsonProtocolFilterTest {
32
33     private static final String JSON =
34                     "{\"requestID\":\"38adde30-cc22-11e8-a8d5-f2801f1b9fd1\",\"entity\":\"controller\","
35                                     + "\"controllers\":[{\"name\":\"test-controller\","
36                                     + "\"drools\":{\"groupId\":\"org.onap.policy.drools.test\","
37                                     + "\"artifactId\":\"test\",\"version\":\"0.0.1\"},\"operation\":\"update\"}]}";
38
39     /**
40      * Tests getting the rule expression of the filter.
41      */
42     @Test
43     void getRuleTest() {
44         assertEquals("$.test", new JsonProtocolFilter("$.test").getRule());
45     }
46
47     /**
48      * Tests setting the rule expression of the filter.
49      */
50     @Test
51     void setRuleTest() {
52         JsonProtocolFilter filter = new JsonProtocolFilter();
53         assertEquals(JsonProtocolFilter.MATCH_ANY, filter.getRule());
54         filter.setRule("$.test");
55         assertEquals("$.test", filter.getRule());
56     }
57
58     /**
59      * Tests that the rule expression will be set to match anything if an empty string is passed.
60      */
61     @Test
62     void setRuleEmptyTest() {
63         assertEquals(JsonProtocolFilter.MATCH_ANY, new JsonProtocolFilter("").getRule());
64     }
65
66     /**
67      * Tests that the rule expression will be set to match anything if a null string is passed.
68      */
69     @Test
70     void setRuleNullTest() {
71         assertEquals(JsonProtocolFilter.MATCH_ANY, new JsonProtocolFilter(null).getRule());
72     }
73
74     /**
75      * Tests accepting a message if all filter rules pass.
76      */
77     @Test
78     void acceptPassTest() {
79         assertTrue(new JsonProtocolFilter(
80                         "$.controllers[?(@.drools.version =~ /\\d\\.\\d\\.\\d/ && @.operation == 'update')]")
81                                         .accept(JSON));
82     }
83
84     /**
85      * Tests accepting a message without having to filter if the rule is set to match anything.
86      */
87     @Test
88     void acceptAnyTest() {
89         assertTrue(new JsonProtocolFilter(null).accept(JSON));
90     }
91
92     /**
93      * Tests rejecting a message if one or more of the filter rules fail.
94      */
95     @Test
96     void acceptFailTest() {
97         assertFalse(
98             new JsonProtocolFilter("$.controllers[?(@.drools.version =~ /\\\\d\\\\.\\\\d\\\\.2/)]")
99                 .accept(JSON));
100     }
101
102     /**
103      * Tests finding field matches for a filter rule corresponding to a topic.
104      */
105     @Test
106     void filterPassTest() {
107         assertEquals("38adde30-cc22-11e8-a8d5-f2801f1b9fd1", new JsonProtocolFilter("$.requestID").filter(JSON).get(0));
108     }
109
110     /**
111      * Tests that an empty list is returned when no matches are found.
112      */
113     @Test
114     void filterFailTest() {
115         assertTrue(new JsonProtocolFilter("$.test").filter(JSON).isEmpty());
116     }
117
118     /**
119      * Tests static method for filtering a JSON string with an arbitrary expression.
120      */
121     @Test
122     void staticFilterPassTest() {
123         assertEquals("controller", JsonProtocolFilter.filter(JSON, "$.entity").get(0));
124     }
125
126     /**
127      * Tests that an empty list is returned when the static filter() method does not find any matches.
128      */
129     @Test
130     void staticFilterFailTest() {
131         assertTrue(JsonProtocolFilter.filter(JSON, "$.test").isEmpty());
132     }
133
134     /**
135      * Tests that an exception is thrown if a null JSON string is passed.
136      */
137     @Test
138     void staticFilterNullJsonTest() {
139         assertThrows(IllegalArgumentException.class, () -> JsonProtocolFilter.filter(null, "[?($ =~ /.*/"));
140     }
141
142     /**
143      * Tests that an exception is thrown if an empty JSON string is passed.
144      */
145     @Test
146     void staticFilterEmptyJsonTest() {
147         assertThrows(IllegalArgumentException.class, () -> JsonProtocolFilter.filter("", "[?($ =~ /.*/"));
148     }
149
150     /**
151      * Tests that an exception is thrown if a null expression string is passed.
152      */
153     @Test
154     void staticFilterNullExpressionTest() {
155         assertThrows(IllegalArgumentException.class, () -> JsonProtocolFilter.filter("{\"hello\":\"world\"}", null));
156     }
157
158     /**
159      * Tests that an exception is thrown if an empty expression string is passed.
160      */
161     @Test
162     void staticFilterEmptyExpressionTest() {
163         assertThrows(IllegalArgumentException.class, () -> JsonProtocolFilter.filter("{\"hello\":\"world\"}", ""));
164     }
165 }