Drools support for kafka topics
[policy/drools-pdp.git] / feature-pooling-messages / src / test / java / org / onap / policy / drools / pooling / SerializerTest.java
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP
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
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
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=========================================================
20  */
21
22 package org.onap.policy.drools.pooling;
23
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;
28
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;
33
34 class SerializerTest {
35
36     @Test
37     void testSerializer() {
38         assertThatCode(Serializer::new).doesNotThrowAnyException();
39     }
40
41     @Test
42     void testEncodeMsg_testDecodeMsg() {
43         Serializer ser = new Serializer();
44
45         Query msg = new Query("hostA");
46         msg.setChannel("channelB");
47
48         String encoded = ser.encodeMsg(msg);
49         assertNotNull(encoded);
50
51         Message decoded = ser.decodeMsg(encoded);
52         assertEquals(Query.class, decoded.getClass());
53
54         assertEquals(msg.getSource(), decoded.getSource());
55         assertEquals(msg.getChannel(), decoded.getChannel());
56
57         // should work a second time, too
58         encoded = ser.encodeMsg(msg);
59         assertNotNull(encoded);
60
61         decoded = ser.decodeMsg(encoded);
62         assertEquals(Query.class, decoded.getClass());
63
64         assertEquals(msg.getSource(), decoded.getSource());
65         assertEquals(msg.getChannel(), decoded.getChannel());
66
67         // invalid subclass when encoding
68         Message msg2 = new Message() {};
69         assertThatThrownBy(() -> ser.encodeMsg(msg2)).isInstanceOf(JsonParseException.class)
70                         .hasMessageContaining("cannot serialize");
71
72         // missing type when decoding
73         final String enc2 = encoded.replaceAll("type", "other-field-name");
74
75         assertThatThrownBy(() -> ser.decodeMsg(enc2)).isInstanceOf(JsonParseException.class)
76                         .hasMessageContaining("does not contain a field named");
77
78         // invalid type
79         final String enc3 = encoded.replaceAll("query", "invalid-type");
80
81         assertThatThrownBy(() -> ser.decodeMsg(enc3)).isInstanceOf(JsonParseException.class)
82                         .hasMessage("cannot deserialize \"invalid-type\"");
83     }
84
85 }