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;
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertFalse;
25 import static org.junit.Assert.assertNotNull;
26 import static org.onap.policy.drools.pooling.state.FilterUtils.makeAnd;
27 import static org.onap.policy.drools.pooling.state.FilterUtils.makeEquals;
28 import static org.onap.policy.drools.pooling.state.FilterUtils.makeOr;
31 import java.util.TreeMap;
32 import org.junit.Test;
33 import org.onap.policy.drools.pooling.message.Message;
34 import org.onap.policy.drools.pooling.message.Query;
36 public class SerializerTest {
39 public void testSerializer() {
44 @SuppressWarnings("unchecked")
45 public void testEncodeFilter() throws Exception {
46 final Serializer ser = new Serializer();
49 * Ensure raw maps serialize as expected. Use a TreeMap so the field
50 * order is predictable.
52 Map<String, Object> top = new TreeMap<>();
53 Map<String, Object> inner = new TreeMap<>();
55 top.put("def", inner);
58 assertEquals("{'abc':20,'def':{'xyz':30},'ghi':true}".replace('\'', '"'), ser.encodeFilter(top));
61 * Ensure we can encode a complicated filter without throwing an
64 Map<String, Object> complexFilter = makeAnd(makeEquals("fieldC", "valueC"),
65 makeOr(makeEquals("fieldA", "valueA"), makeEquals("fieldB", "valueB")));
66 String val = ser.encodeFilter(complexFilter);
67 assertFalse(val.isEmpty());
71 public void testEncodeMsg_testDecodeMsg() throws Exception {
72 Serializer ser = new Serializer();
74 Query msg = new Query("hostA");
75 msg.setChannel("channelB");
77 String encoded = ser.encodeMsg(msg);
78 assertNotNull(encoded);
80 Message decoded = ser.decodeMsg(encoded);
81 assertEquals(Query.class, decoded.getClass());
83 assertEquals(msg.getSource(), decoded.getSource());
84 assertEquals(msg.getChannel(), decoded.getChannel());
86 // should work a second time, too
87 encoded = ser.encodeMsg(msg);
88 assertNotNull(encoded);
90 decoded = ser.decodeMsg(encoded);
91 assertEquals(Query.class, decoded.getClass());
93 assertEquals(msg.getSource(), decoded.getSource());
94 assertEquals(msg.getChannel(), decoded.getChannel());