2 * ============LICENSE_START=======================================================
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
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.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;
31 import com.google.gson.JsonParseException;
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;
38 public class SerializerTest {
41 public void testSerializer() {
46 @SuppressWarnings("unchecked")
47 public void testEncodeFilter() throws Exception {
48 final Serializer ser = new Serializer();
51 * Ensure raw maps serialize as expected. Use a TreeMap so the field
52 * order is predictable.
54 Map<String, Object> top = new TreeMap<>();
55 Map<String, Object> inner = new TreeMap<>();
57 top.put("def", inner);
60 assertEquals("{'abc':20,'def':{'xyz':30},'ghi':true}".replace('\'', '"'), ser.encodeFilter(top));
63 * Ensure we can encode a complicated filter without throwing an
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());
73 public void testEncodeMsg_testDecodeMsg() throws Exception {
74 Serializer ser = new Serializer();
76 Query msg = new Query("hostA");
77 msg.setChannel("channelB");
79 String encoded = ser.encodeMsg(msg);
80 assertNotNull(encoded);
82 Message decoded = ser.decodeMsg(encoded);
83 assertEquals(Query.class, decoded.getClass());
85 assertEquals(msg.getSource(), decoded.getSource());
86 assertEquals(msg.getChannel(), decoded.getChannel());
88 // should work a second time, too
89 encoded = ser.encodeMsg(msg);
90 assertNotNull(encoded);
92 decoded = ser.decodeMsg(encoded);
93 assertEquals(Query.class, decoded.getClass());
95 assertEquals(msg.getSource(), decoded.getSource());
96 assertEquals(msg.getChannel(), decoded.getChannel());
98 // invalid subclass when encoding
99 assertThatThrownBy(() -> ser.encodeMsg(new Message() {})).isInstanceOf(JsonParseException.class)
100 .hasMessageContaining("cannot serialize");
102 // missing type when decoding
103 final String enc2 = encoded.replaceAll("type", "other-field-name");
105 assertThatThrownBy(() -> ser.decodeMsg(enc2)).isInstanceOf(JsonParseException.class)
106 .hasMessageContaining("does not contain a field named");
109 final String enc3 = encoded.replaceAll("query", "invalid-type");
111 assertThatThrownBy(() -> ser.decodeMsg(enc3)).isInstanceOf(JsonParseException.class)
112 .hasMessage("cannot deserialize \"invalid-type\"");