Drools support for kafka topics
[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-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
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.assertThatCode;
24 import static org.assertj.core.api.Assertions.assertThatThrownBy;
25 import static org.junit.Assert.assertEquals;
26 import static org.junit.Assert.assertNotNull;
27
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;
32
33 public class SerializerTest {
34
35     @Test
36     public void testSerializer() {
37         assertThatCode(() -> new Serializer()).doesNotThrowAnyException();
38     }
39
40     @Test
41     public void testEncodeMsg_testDecodeMsg() throws Exception {
42         Serializer ser = new Serializer();
43
44         Query msg = new Query("hostA");
45         msg.setChannel("channelB");
46
47         String encoded = ser.encodeMsg(msg);
48         assertNotNull(encoded);
49
50         Message decoded = ser.decodeMsg(encoded);
51         assertEquals(Query.class, decoded.getClass());
52
53         assertEquals(msg.getSource(), decoded.getSource());
54         assertEquals(msg.getChannel(), decoded.getChannel());
55
56         // should work a second time, too
57         encoded = ser.encodeMsg(msg);
58         assertNotNull(encoded);
59
60         decoded = ser.decodeMsg(encoded);
61         assertEquals(Query.class, decoded.getClass());
62
63         assertEquals(msg.getSource(), decoded.getSource());
64         assertEquals(msg.getChannel(), decoded.getChannel());
65
66         // invalid subclass when encoding
67         Message msg2 = new Message() {};
68         assertThatThrownBy(() -> ser.encodeMsg(msg2)).isInstanceOf(JsonParseException.class)
69                         .hasMessageContaining("cannot serialize");
70
71         // missing type when decoding
72         final String enc2 = encoded.replaceAll("type", "other-field-name");
73
74         assertThatThrownBy(() -> ser.decodeMsg(enc2)).isInstanceOf(JsonParseException.class)
75                         .hasMessageContaining("does not contain a field named");
76
77         // invalid type
78         final String enc3 = encoded.replaceAll("query", "invalid-type");
79
80         assertThatThrownBy(() -> ser.decodeMsg(enc3)).isInstanceOf(JsonParseException.class)
81                         .hasMessage("cannot deserialize \"invalid-type\"");
82     }
83
84 }