2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.policy.drools.pooling.message;
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertNull;
25 import static org.junit.Assert.fail;
27 import org.junit.Test;
28 import org.onap.policy.drools.pooling.PoolingFeatureException;
29 import org.onap.policy.drools.pooling.Serializer;
32 * Superclass used to test subclasses of {@link Message}.
34 * @param <T> type of {@link Message} subclass that this tests
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";
43 * Used to perform JSON serialization and de-serialization.
45 public final Serializer mapper = new Serializer();
48 * The subclass of the type of Message being tested.
50 private final Class<T> subclazz;
55 * @param subclazz subclass of {@link Message} being tested
57 public SupportBasicMessageTester(Class<T> subclazz) {
58 this.subclazz = subclazz;
62 * Creates a default Message and verifies that the source and channel are
67 public final void testDefaultConstructor() {
68 testDefaultConstructorFields(makeDefaultMessage());
72 * Tests that the Message has the correct source, and that the channel is
77 public final void testConstructorWithArgs() {
78 testValidFields(makeValidMessage());
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.
86 * @throws Exception if an error occurs
89 public final void testJsonEncodeDecode() throws Exception {
90 T originalMsg = makeValidMessage();
93 if (originalMsg.getClass() == Message.class) {
96 msg = mapper.decodeMsg(mapper.encodeMsg(originalMsg));
99 assertEquals(subclazz, msg.getClass());
103 testValidFields(subclazz.cast(msg));
107 * Creates a valid Message and verifies that checkValidity() passes.
109 * @throws PoolingFeatureException if an error occurs
112 public final void testCheckValidity_Ok() throws PoolingFeatureException {
113 T msg = makeValidMessage();
116 testValidFields(subclazz.cast(msg));
120 * Creates a default Message and verifies that checkValidity() fails. Does
121 * not throw an exception.
124 public final void testCheckValidity_DefaultConstructor() {
126 makeDefaultMessage().checkValidity();
127 fail("missing exception");
129 } catch (PoolingFeatureException expected) {
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.
139 * @param func function to update the message prior to invoking
142 public void expectCheckValidityFailure(MessageUpdateFunction<T> func) {
144 T msg = makeValidMessage();
149 fail("missing exception");
151 } catch (PoolingFeatureException expected) {
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., "").
163 * @param func function to update the message's field prior to invoking
166 public void expectCheckValidityFailure_NullOrEmpty(MessageFieldUpdateFunction<T> func) {
167 expectCheckValidityFailure(msg -> func.update(msg, null));
168 expectCheckValidityFailure(msg -> func.update(msg, ""));
172 * Makes a message using the default constructor.
174 * @return a new Message
176 public final T makeDefaultMessage() {
178 return subclazz.getConstructor().newInstance();
180 } catch (Exception e) {
181 throw new AssertionError(e);
186 // the remaining methods will typically be overridden
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.
193 * @return a valid Message
195 public abstract T makeValidMessage();
198 * Verifies that fields are set as expected by
199 * {@link #makeDefaultMessage()}.
201 * @param msg the default Message
203 public void testDefaultConstructorFields(T msg) {
204 assertNull(msg.getSource());
205 assertNull(msg.getChannel());
209 * Verifies that fields are set as expected by {@link #makeValidMessage()}.
211 * @param msg message whose fields are to be validated
213 public void testValidFields(T msg) {
214 assertEquals(VALID_HOST, msg.getSource());
215 assertEquals(VALID_CHANNEL, msg.getChannel());
219 * Function that updates a message.
221 * @param <T> type of Message the function updates
224 public static interface MessageUpdateFunction<T extends Message> {
229 * @param msg message to be updated
231 public void update(T msg);
235 * Function that updates a single field within a message.
237 * @param <T> type of Message the function updates
240 public static interface MessageFieldUpdateFunction<T extends Message> {
243 * Updates a field within a message.
245 * @param msg message to be updated
246 * @param newValue new field value
248 public void update(T msg, String newValue);