Remove jackson from feature-pooling-dmaap
[policy/drools-pdp.git] / feature-pooling-dmaap / src / test / java / org / onap / policy / drools / pooling / message / SupportBasicMessageTester.java
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP
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
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.message;
22
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertNull;
25 import static org.junit.Assert.fail;
26
27 import org.junit.Test;
28 import org.onap.policy.drools.pooling.PoolingFeatureException;
29 import org.onap.policy.drools.pooling.Serializer;
30
31 /**
32  * Superclass used to test subclasses of {@link Message}.
33  * 
34  * @param <T> type of {@link Message} subclass that this tests
35  */
36 public abstract class SupportBasicMessageTester<T extends Message> {
37     // values set by makeValidMessage()
38     public static final String VALID_HOST_PREDECESSOR = "hostA";
39     public static final String VALID_HOST = "hostB";
40     public static final String VALID_CHANNEL = "channelC";
41
42     /**
43      * Used to perform JSON serialization and de-serialization.
44      */
45     public final Serializer mapper = new Serializer();
46
47     /**
48      * The subclass of the type of Message being tested.
49      */
50     private final Class<T> subclazz;
51
52     /**
53      * Constructor.
54      * 
55      * @param subclazz subclass of {@link Message} being tested
56      */
57     public SupportBasicMessageTester(Class<T> subclazz) {
58         this.subclazz = subclazz;
59     }
60
61     /**
62      * Creates a default Message and verifies that the source and channel are
63      * {@code null}.
64      * 
65      */
66     @Test
67     public final void testDefaultConstructor() {
68         testDefaultConstructorFields(makeDefaultMessage());
69     }
70
71     /**
72      * Tests that the Message has the correct source, and that the channel is
73      * {@code null}.
74      * 
75      */
76     @Test
77     public final void testConstructorWithArgs() {
78         testValidFields(makeValidMessage());
79     }
80
81     /**
82      * Makes a valid message and then verifies that it can be serialized and
83      * de-serialized. Verifies that the de-serialized message is of the same
84      * type, and has the same content, as the original.
85      * 
86      * @throws Exception if an error occurs
87      */
88     @Test
89     public final void testJsonEncodeDecode() throws Exception {
90         T originalMsg = makeValidMessage();
91
92         Message msg;
93         if (originalMsg.getClass() == Message.class) {
94             msg = originalMsg;
95         } else {
96             msg = mapper.decodeMsg(mapper.encodeMsg(originalMsg));
97         }
98         
99         assertEquals(subclazz, msg.getClass());
100
101         msg.checkValidity();
102
103         testValidFields(subclazz.cast(msg));
104     }
105
106     /**
107      * Creates a valid Message and verifies that checkValidity() passes.
108      * 
109      * @throws PoolingFeatureException if an error occurs
110      */
111     @Test
112     public final void testCheckValidity_Ok() throws PoolingFeatureException {
113         T msg = makeValidMessage();
114         msg.checkValidity();
115
116         testValidFields(subclazz.cast(msg));
117     }
118
119     /**
120      * Creates a default Message and verifies that checkValidity() fails. Does
121      * not throw an exception.
122      */
123     @Test
124     public final void testCheckValidity_DefaultConstructor() {
125         try {
126             makeDefaultMessage().checkValidity();
127             fail("missing exception");
128
129         } catch (PoolingFeatureException expected) {
130             // success
131         }
132     }
133
134     /**
135      * Creates a message via {@link #makeValidMessage()}, updates it via the
136      * given function, and then invokes the checkValidity() method on it. It is
137      * expected that the checkValidity() will throw an exception.
138      * 
139      * @param func function to update the message prior to invoking
140      *        checkValidity()
141      */
142     public void expectCheckValidityFailure(MessageUpdateFunction<T> func) {
143         try {
144             T msg = makeValidMessage();
145             func.update(msg);
146
147             msg.checkValidity();
148
149             fail("missing exception");
150
151         } catch (PoolingFeatureException expected) {
152             // success
153         }
154     }
155
156     /**
157      * Creates a message via {@link #makeValidMessage()}, updates one of its
158      * fields via the given function, and then invokes the checkValidity()
159      * method on it. It is expected that the checkValidity() will throw an
160      * exception. It checks both the case when the message's field is set to
161      * {@code null}, and when it is set to empty (i.e., "").
162      * 
163      * @param func function to update the message's field prior to invoking
164      *        checkValidity()
165      */
166     public void expectCheckValidityFailure_NullOrEmpty(MessageFieldUpdateFunction<T> func) {
167         expectCheckValidityFailure(msg -> func.update(msg, null));
168         expectCheckValidityFailure(msg -> func.update(msg, ""));
169     }
170
171     /**
172      * Makes a message using the default constructor.
173      * 
174      * @return a new Message
175      */
176     public final T makeDefaultMessage() {
177         try {
178             return subclazz.getConstructor().newInstance();
179
180         } catch (Exception e) {
181             throw new AssertionError(e);
182         }
183     }
184
185
186     // the remaining methods will typically be overridden
187
188     /**
189      * Makes a message that will pass the validity check. Note: this should use
190      * the non-default constructor, and the source and channel should be set to
191      * {@link VALID_HOST} and {@link VALID_CHANNEL}, respectively.
192      * 
193      * @return a valid Message
194      */
195     public abstract T makeValidMessage();
196
197     /**
198      * Verifies that fields are set as expected by
199      * {@link #makeDefaultMessage()}.
200      * 
201      * @param msg the default Message
202      */
203     public void testDefaultConstructorFields(T msg) {
204         assertNull(msg.getSource());
205         assertNull(msg.getChannel());
206     }
207
208     /**
209      * Verifies that fields are set as expected by {@link #makeValidMessage()}.
210      * 
211      * @param msg message whose fields are to be validated
212      */
213     public void testValidFields(T msg) {
214         assertEquals(VALID_HOST, msg.getSource());
215         assertEquals(VALID_CHANNEL, msg.getChannel());
216     }
217
218     /**
219      * Function that updates a message.
220      * 
221      * @param <T> type of Message the function updates
222      */
223     @FunctionalInterface
224     public static interface MessageUpdateFunction<T extends Message> {
225
226         /**
227          * Updates a message.
228          * 
229          * @param msg message to be updated
230          */
231         public void update(T msg);
232     }
233
234     /**
235      * Function that updates a single field within a message.
236      * 
237      * @param <T> type of Message the function updates
238      */
239     @FunctionalInterface
240     public static interface MessageFieldUpdateFunction<T extends Message> {
241
242         /**
243          * Updates a field within a message.
244          * 
245          * @param msg message to be updated
246          * @param newValue new field value
247          */
248         public void update(T msg, String newValue);
249     }
250 }