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