2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2018 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 com.fasterxml.jackson.databind.ObjectMapper;
28 import org.junit.Test;
29 import org.onap.policy.drools.pooling.PoolingFeatureException;
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 ObjectMapper mapper = new ObjectMapper();
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();
92 Message msg = mapper.readValue(mapper.writeValueAsString(originalMsg), Message.class);
93 assertEquals(subclazz, msg.getClass());
97 testValidFields(subclazz.cast(msg));
101 * Creates a valid Message and verifies that checkValidity() passes.
103 * @throws PoolingFeatureException if an error occurs
106 public final void testCheckValidity_Ok() throws PoolingFeatureException {
107 T msg = makeValidMessage();
110 testValidFields(subclazz.cast(msg));
114 * Creates a default Message and verifies that checkValidity() fails. Does
115 * not throw an exception.
118 public final void testCheckValidity_DefaultConstructor() {
120 makeDefaultMessage().checkValidity();
121 fail("missing exception");
123 } catch (PoolingFeatureException expected) {
129 * Creates a message via {@link #makeValidMessage()}, updates it via the
130 * given function, and then invokes the checkValidity() method on it. It is
131 * expected that the checkValidity() will throw an exception.
133 * @param func function to update the message prior to invoking
136 public void expectCheckValidityFailure(MessageUpdateFunction<T> func) {
138 T msg = makeValidMessage();
143 fail("missing exception");
145 } catch (PoolingFeatureException expected) {
151 * Creates a message via {@link #makeValidMessage()}, updates one of its
152 * fields via the given function, and then invokes the checkValidity()
153 * method on it. It is expected that the checkValidity() will throw an
154 * exception. It checks both the case when the message's field is set to
155 * {@code null}, and when it is set to empty (i.e., "").
157 * @param func function to update the message's field prior to invoking
160 public void expectCheckValidityFailure_NullOrEmpty(MessageFieldUpdateFunction<T> func) {
161 expectCheckValidityFailure(msg -> func.update(msg, null));
162 expectCheckValidityFailure(msg -> func.update(msg, ""));
166 * Makes a message using the default constructor.
168 * @return a new Message
170 public final T makeDefaultMessage() {
172 return subclazz.getConstructor().newInstance();
174 } catch (Exception e) {
175 throw new AssertionError(e);
180 // the remaining methods will typically be overridden
183 * Makes a message that will pass the validity check. Note: this should use
184 * the non-default constructor, and the source and channel should be set to
185 * {@link VALID_HOST} and {@link VALID_CHANNEL}, respectively.
187 * @return a valid Message
189 public abstract T makeValidMessage();
192 * Verifies that fields are set as expected by
193 * {@link #makeDefaultMessage()}.
195 * @param msg the default Message
197 public void testDefaultConstructorFields(T msg) {
198 assertNull(msg.getSource());
199 assertNull(msg.getChannel());
203 * Verifies that fields are set as expected by {@link #makeValidMessage()}.
205 * @param msg message whose fields are to be validated
207 public void testValidFields(T msg) {
208 assertEquals(VALID_HOST, msg.getSource());
209 assertEquals(VALID_CHANNEL, msg.getChannel());
213 * Function that updates a message.
215 * @param <T> type of Message the function updates
218 public static interface MessageUpdateFunction<T extends Message> {
223 * @param msg message to be updated
225 public void update(T msg);
229 * Function that updates a single field within a message.
231 * @param <T> type of Message the function updates
234 public static interface MessageFieldUpdateFunction<T extends Message> {
237 * Updates a field within a message.
239 * @param msg message to be updated
240 * @param newValue new field value
242 public void update(T msg, String newValue);