2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2018 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.drools.pooling.state;
23 import static org.junit.Assert.assertEquals;
24 import static org.onap.policy.drools.pooling.state.FilterUtils.CLASS_AND;
25 import static org.onap.policy.drools.pooling.state.FilterUtils.CLASS_EQUALS;
26 import static org.onap.policy.drools.pooling.state.FilterUtils.CLASS_OR;
27 import static org.onap.policy.drools.pooling.state.FilterUtils.JSON_CLASS;
28 import static org.onap.policy.drools.pooling.state.FilterUtils.JSON_FIELD;
29 import static org.onap.policy.drools.pooling.state.FilterUtils.JSON_FILTERS;
30 import static org.onap.policy.drools.pooling.state.FilterUtils.JSON_VALUE;
31 import static org.onap.policy.drools.pooling.state.FilterUtils.makeAnd;
32 import static org.onap.policy.drools.pooling.state.FilterUtils.makeEquals;
33 import static org.onap.policy.drools.pooling.state.FilterUtils.makeOr;
36 import org.junit.Test;
38 public class FilterUtilsTest {
41 public void testMakeEquals() {
42 checkEquals("abc", "def", makeEquals("abc", "def"));
46 public void testMakeAnd() {
47 @SuppressWarnings("unchecked")
48 Map<String, Object> filter =
49 makeAnd(makeEquals("an1", "av1"), makeEquals("an2", "av2"), makeEquals("an3", "av3"));
51 checkArray(CLASS_AND, 3, filter);
52 checkEquals("an1", "av1", getItem(filter, 0));
53 checkEquals("an2", "av2", getItem(filter, 1));
54 checkEquals("an3", "av3", getItem(filter, 2));
58 public void testMakeOr() {
59 @SuppressWarnings("unchecked")
60 Map<String, Object> filter =
61 makeOr(makeEquals("on1", "ov1"), makeEquals("on2", "ov2"), makeEquals("on3", "ov3"));
63 checkArray(CLASS_OR, 3, filter);
64 checkEquals("on1", "ov1", getItem(filter, 0));
65 checkEquals("on2", "ov2", getItem(filter, 1));
66 checkEquals("on3", "ov3", getItem(filter, 2));
70 * Checks that the filter contains an array.
72 * @param expectedClassName type of filter this should represent
73 * @param expectedCount number of items expected in the array
74 * @param filter filter to be examined
76 protected void checkArray(String expectedClassName, int expectedCount, Map<String, Object> filter) {
77 assertEquals(expectedClassName, filter.get(JSON_CLASS));
79 Object[] val = (Object[]) filter.get(JSON_FILTERS);
80 assertEquals(expectedCount, val.length);
84 * Checks that a map represents an "equals".
86 * @param name name of the field on the left side of the equals
87 * @param value value on the right side of the equals
88 * @param map map whose content is to be examined
90 protected void checkEquals(String name, String value, Map<String, Object> map) {
91 assertEquals(CLASS_EQUALS, map.get(JSON_CLASS));
92 assertEquals(name, map.get(JSON_FIELD));
93 assertEquals(value, map.get(JSON_VALUE));
97 * Gets a particular sub-filter from the array contained within a filter.
99 * @param filter containing filter
100 * @param index index of the sub-filter of interest
101 * @return the sub-filter with the given index
103 @SuppressWarnings("unchecked")
104 protected Map<String, Object> getItem(Map<String, Object> filter, int index) {
105 Object[] val = (Object[]) filter.get(JSON_FILTERS);
107 return (Map<String, Object>) val[index];