2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2018-2020 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.assertThatCode;
24 import static org.assertj.core.api.Assertions.assertThatThrownBy;
25 import static org.junit.Assert.assertEquals;
26 import static org.junit.Assert.assertNotNull;
28 import com.google.gson.JsonParseException;
29 import org.junit.Test;
30 import org.onap.policy.drools.pooling.message.Message;
31 import org.onap.policy.drools.pooling.message.Query;
33 public class SerializerTest {
36 public void testSerializer() {
37 assertThatCode(() -> new Serializer()).doesNotThrowAnyException();
41 public void testEncodeMsg_testDecodeMsg() throws Exception {
42 Serializer ser = new Serializer();
44 Query msg = new Query("hostA");
45 msg.setChannel("channelB");
47 String encoded = ser.encodeMsg(msg);
48 assertNotNull(encoded);
50 Message decoded = ser.decodeMsg(encoded);
51 assertEquals(Query.class, decoded.getClass());
53 assertEquals(msg.getSource(), decoded.getSource());
54 assertEquals(msg.getChannel(), decoded.getChannel());
56 // should work a second time, too
57 encoded = ser.encodeMsg(msg);
58 assertNotNull(encoded);
60 decoded = ser.decodeMsg(encoded);
61 assertEquals(Query.class, decoded.getClass());
63 assertEquals(msg.getSource(), decoded.getSource());
64 assertEquals(msg.getChannel(), decoded.getChannel());
66 // invalid subclass when encoding
67 Message msg2 = new Message() {};
68 assertThatThrownBy(() -> ser.encodeMsg(msg2)).isInstanceOf(JsonParseException.class)
69 .hasMessageContaining("cannot serialize");
71 // missing type when decoding
72 final String enc2 = encoded.replaceAll("type", "other-field-name");
74 assertThatThrownBy(() -> ser.decodeMsg(enc2)).isInstanceOf(JsonParseException.class)
75 .hasMessageContaining("does not contain a field named");
78 final String enc3 = encoded.replaceAll("query", "invalid-type");
80 assertThatThrownBy(() -> ser.decodeMsg(enc3)).isInstanceOf(JsonParseException.class)
81 .hasMessage("cannot deserialize \"invalid-type\"");