2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2018-2020 AT&T Intellectual Property. All rights reserved.
6 * Modifications Copyright (C) 2024 Nordix Foundation.
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.drools.pooling;
24 import static org.assertj.core.api.Assertions.assertThatCode;
25 import static org.assertj.core.api.Assertions.assertThatThrownBy;
26 import static org.junit.jupiter.api.Assertions.assertEquals;
27 import static org.junit.jupiter.api.Assertions.assertNotNull;
29 import com.google.gson.JsonParseException;
30 import org.junit.jupiter.api.Test;
31 import org.onap.policy.drools.pooling.message.Message;
32 import org.onap.policy.drools.pooling.message.Query;
34 class SerializerTest {
37 void testSerializer() {
38 assertThatCode(Serializer::new).doesNotThrowAnyException();
42 void testEncodeMsg_testDecodeMsg() {
43 Serializer ser = new Serializer();
45 Query msg = new Query("hostA");
46 msg.setChannel("channelB");
48 String encoded = ser.encodeMsg(msg);
49 assertNotNull(encoded);
51 Message decoded = ser.decodeMsg(encoded);
52 assertEquals(Query.class, decoded.getClass());
54 assertEquals(msg.getSource(), decoded.getSource());
55 assertEquals(msg.getChannel(), decoded.getChannel());
57 // should work a second time, too
58 encoded = ser.encodeMsg(msg);
59 assertNotNull(encoded);
61 decoded = ser.decodeMsg(encoded);
62 assertEquals(Query.class, decoded.getClass());
64 assertEquals(msg.getSource(), decoded.getSource());
65 assertEquals(msg.getChannel(), decoded.getChannel());
67 // invalid subclass when encoding
68 Message msg2 = new Message() {};
69 assertThatThrownBy(() -> ser.encodeMsg(msg2)).isInstanceOf(JsonParseException.class)
70 .hasMessageContaining("cannot serialize");
72 // missing type when decoding
73 final String enc2 = encoded.replaceAll("type", "other-field-name");
75 assertThatThrownBy(() -> ser.decodeMsg(enc2)).isInstanceOf(JsonParseException.class)
76 .hasMessageContaining("does not contain a field named");
79 final String enc3 = encoded.replaceAll("query", "invalid-type");
81 assertThatThrownBy(() -> ser.decodeMsg(enc3)).isInstanceOf(JsonParseException.class)
82 .hasMessage("cannot deserialize \"invalid-type\"");