0b098c13b7122d9af3c91acbd5a0de85d45c84f7
[policy/drools-pdp.git] / feature-pooling-dmaap / src / test / java / org / onap / policy / drools / pooling / SerializerTest.java
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.onap.policy.drools.pooling;
22
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;
29
30 import java.util.Map;
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;
35
36 public class SerializerTest {
37
38     @Test
39     public void testSerializer() {
40         new Serializer();
41     }
42
43     @Test
44     @SuppressWarnings("unchecked")
45     public void testEncodeFilter() throws Exception {
46         final Serializer ser = new Serializer();
47
48         /*
49          * Ensure raw maps serialize as expected. Use a TreeMap so the field
50          * order is predictable.
51          */
52         Map<String, Object> top = new TreeMap<>();
53         Map<String, Object> inner = new TreeMap<>();
54         top.put("abc", 20);
55         top.put("def", inner);
56         top.put("ghi", true);
57         inner.put("xyz", 30);
58         assertEquals("{'abc':20,'def':{'xyz':30},'ghi':true}".replace('\'', '"'), ser.encodeFilter(top));
59
60         /*
61          * Ensure we can encode a complicated filter without throwing an
62          * exception
63          */
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());
68     }
69
70     @Test
71     public void testEncodeMsg_testDecodeMsg() throws Exception {
72         Serializer ser = new Serializer();
73
74         Query msg = new Query("hostA");
75         msg.setChannel("channelB");
76
77         String encoded = ser.encodeMsg(msg);
78         assertNotNull(encoded);
79
80         Message decoded = ser.decodeMsg(encoded);
81         assertEquals(Query.class, decoded.getClass());
82
83         assertEquals(msg.getSource(), decoded.getSource());
84         assertEquals(msg.getChannel(), decoded.getChannel());
85
86         // should work a second time, too
87         encoded = ser.encodeMsg(msg);
88         assertNotNull(encoded);
89
90         decoded = ser.decodeMsg(encoded);
91         assertEquals(Query.class, decoded.getClass());
92
93         assertEquals(msg.getSource(), decoded.getSource());
94         assertEquals(msg.getChannel(), decoded.getChannel());
95     }
96
97 }