2 * ============LICENSE_START=======================================================
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
22 package org.onap.policy.drools.protocol.coders;
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;
29 import org.junit.jupiter.api.Test;
31 class JsonProtocolFilterTest {
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\"}]}";
40 * Tests getting the rule expression of the filter.
44 assertEquals("$.test", new JsonProtocolFilter("$.test").getRule());
48 * Tests setting the rule expression of the filter.
52 JsonProtocolFilter filter = new JsonProtocolFilter();
53 assertEquals(JsonProtocolFilter.MATCH_ANY, filter.getRule());
54 filter.setRule("$.test");
55 assertEquals("$.test", filter.getRule());
59 * Tests that the rule expression will be set to match anything if an empty string is passed.
62 void setRuleEmptyTest() {
63 assertEquals(JsonProtocolFilter.MATCH_ANY, new JsonProtocolFilter("").getRule());
67 * Tests that the rule expression will be set to match anything if a null string is passed.
70 void setRuleNullTest() {
71 assertEquals(JsonProtocolFilter.MATCH_ANY, new JsonProtocolFilter(null).getRule());
75 * Tests accepting a message if all filter rules pass.
78 void acceptPassTest() {
79 assertTrue(new JsonProtocolFilter(
80 "$.controllers[?(@.drools.version =~ /\\d\\.\\d\\.\\d/ && @.operation == 'update')]")
85 * Tests accepting a message without having to filter if the rule is set to match anything.
88 void acceptAnyTest() {
89 assertTrue(new JsonProtocolFilter(null).accept(JSON));
93 * Tests rejecting a message if one or more of the filter rules fail.
96 void acceptFailTest() {
98 new JsonProtocolFilter("$.controllers[?(@.drools.version =~ /\\\\d\\\\.\\\\d\\\\.2/)]")
103 * Tests finding field matches for a filter rule corresponding to a topic.
106 void filterPassTest() {
107 assertEquals("38adde30-cc22-11e8-a8d5-f2801f1b9fd1", new JsonProtocolFilter("$.requestID").filter(JSON).get(0));
111 * Tests that an empty list is returned when no matches are found.
114 void filterFailTest() {
115 assertTrue(new JsonProtocolFilter("$.test").filter(JSON).isEmpty());
119 * Tests static method for filtering a JSON string with an arbitrary expression.
122 void staticFilterPassTest() {
123 assertEquals("controller", JsonProtocolFilter.filter(JSON, "$.entity").get(0));
127 * Tests that an empty list is returned when the static filter() method does not find any matches.
130 void staticFilterFailTest() {
131 assertTrue(JsonProtocolFilter.filter(JSON, "$.test").isEmpty());
135 * Tests that an exception is thrown if a null JSON string is passed.
138 void staticFilterNullJsonTest() {
139 assertThrows(IllegalArgumentException.class, () -> JsonProtocolFilter.filter(null, "[?($ =~ /.*/"));
143 * Tests that an exception is thrown if an empty JSON string is passed.
146 void staticFilterEmptyJsonTest() {
147 assertThrows(IllegalArgumentException.class, () -> JsonProtocolFilter.filter("", "[?($ =~ /.*/"));
151 * Tests that an exception is thrown if a null expression string is passed.
154 void staticFilterNullExpressionTest() {
155 assertThrows(IllegalArgumentException.class, () -> JsonProtocolFilter.filter("{\"hello\":\"world\"}", null));
159 * Tests that an exception is thrown if an empty expression string is passed.
162 void staticFilterEmptyExpressionTest() {
163 assertThrows(IllegalArgumentException.class, () -> JsonProtocolFilter.filter("{\"hello\":\"world\"}", ""));