Remove jackson from feature-pooling-dmaap
[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-2019 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.assertj.core.api.Assertions.assertThatThrownBy;
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertFalse;
26 import static org.junit.Assert.assertNotNull;
27 import static org.onap.policy.drools.pooling.state.FilterUtils.makeAnd;
28 import static org.onap.policy.drools.pooling.state.FilterUtils.makeEquals;
29 import static org.onap.policy.drools.pooling.state.FilterUtils.makeOr;
30
31 import com.google.gson.JsonParseException;
32 import java.util.Map;
33 import java.util.TreeMap;
34 import org.junit.Test;
35 import org.onap.policy.drools.pooling.message.Message;
36 import org.onap.policy.drools.pooling.message.Query;
37
38 public class SerializerTest {
39
40     @Test
41     public void testSerializer() {
42         new Serializer();
43     }
44
45     @Test
46     @SuppressWarnings("unchecked")
47     public void testEncodeFilter() throws Exception {
48         final Serializer ser = new Serializer();
49
50         /*
51          * Ensure raw maps serialize as expected. Use a TreeMap so the field
52          * order is predictable.
53          */
54         Map<String, Object> top = new TreeMap<>();
55         Map<String, Object> inner = new TreeMap<>();
56         top.put("abc", 20);
57         top.put("def", inner);
58         top.put("ghi", true);
59         inner.put("xyz", 30);
60         assertEquals("{'abc':20,'def':{'xyz':30},'ghi':true}".replace('\'', '"'), ser.encodeFilter(top));
61
62         /*
63          * Ensure we can encode a complicated filter without throwing an
64          * exception
65          */
66         Map<String, Object> complexFilter = makeAnd(makeEquals("fieldC", "valueC"),
67                         makeOr(makeEquals("fieldA", "valueA"), makeEquals("fieldB", "valueB")));
68         String val = ser.encodeFilter(complexFilter);
69         assertFalse(val.isEmpty());
70     }
71
72     @Test
73     public void testEncodeMsg_testDecodeMsg() throws Exception {
74         Serializer ser = new Serializer();
75
76         Query msg = new Query("hostA");
77         msg.setChannel("channelB");
78
79         String encoded = ser.encodeMsg(msg);
80         assertNotNull(encoded);
81
82         Message decoded = ser.decodeMsg(encoded);
83         assertEquals(Query.class, decoded.getClass());
84
85         assertEquals(msg.getSource(), decoded.getSource());
86         assertEquals(msg.getChannel(), decoded.getChannel());
87
88         // should work a second time, too
89         encoded = ser.encodeMsg(msg);
90         assertNotNull(encoded);
91
92         decoded = ser.decodeMsg(encoded);
93         assertEquals(Query.class, decoded.getClass());
94
95         assertEquals(msg.getSource(), decoded.getSource());
96         assertEquals(msg.getChannel(), decoded.getChannel());
97
98         // invalid subclass when encoding
99         assertThatThrownBy(() -> ser.encodeMsg(new Message() {})).isInstanceOf(JsonParseException.class)
100                         .hasMessageContaining("cannot serialize");
101
102         // missing type when decoding
103         final String enc2 = encoded.replaceAll("type", "other-field-name");
104
105         assertThatThrownBy(() -> ser.decodeMsg(enc2)).isInstanceOf(JsonParseException.class)
106                         .hasMessageContaining("does not contain a field named");
107
108         // invalid type
109         final String enc3 = encoded.replaceAll("query", "invalid-type");
110
111         assertThatThrownBy(() -> ser.decodeMsg(enc3)).isInstanceOf(JsonParseException.class)
112                         .hasMessage("cannot deserialize \"invalid-type\"");
113     }
114
115 }