2 * ============LICENSE_START=======================================================
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
22 package org.onap.policy.drools.pooling.message;
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;
28 import org.junit.jupiter.api.Test;
29 import org.onap.policy.drools.pooling.PoolingFeatureException;
30 import org.onap.policy.drools.pooling.Serializer;
33 * Superclass used to test subclasses of {@link Message}.
35 * @param <T> type of {@link Message} subclass that this tests
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";
44 * Used to perform JSON serialization and de-serialization.
46 public final Serializer mapper = new Serializer();
49 * The subclass of the type of Message being tested.
51 private final Class<T> subclazz;
56 * @param subclazz subclass of {@link Message} being tested
58 public SupportBasicMessageTester(Class<T> subclazz) {
59 this.subclazz = subclazz;
63 * Creates a default Message and verifies that the source and channel are
68 public final void testDefaultConstructor() {
69 testDefaultConstructorFields(makeDefaultMessage());
73 * Tests that the Message has the correct source, and that the channel is
78 public final void testConstructorWithArgs() {
79 testValidFields(makeValidMessage());
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.
87 * @throws Exception if an error occurs
90 public final void testJsonEncodeDecode() throws Exception {
91 T originalMsg = makeValidMessage();
94 if (originalMsg.getClass() == Message.class) {
97 msg = mapper.decodeMsg(mapper.encodeMsg(originalMsg));
100 assertEquals(subclazz, msg.getClass());
104 testValidFields(subclazz.cast(msg));
108 * Creates a valid Message and verifies that checkValidity() passes.
110 * @throws PoolingFeatureException if an error occurs
113 public final void testCheckValidity_Ok() throws PoolingFeatureException {
114 T msg = makeValidMessage();
117 testValidFields(subclazz.cast(msg));
121 * Creates a default Message and verifies that checkValidity() fails. Does
122 * not throw an exception.
125 public final void testCheckValidity_DefaultConstructor() {
127 makeDefaultMessage().checkValidity();
128 fail("missing exception");
130 } catch (PoolingFeatureException expected) {
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.
140 * @param func function to update the message prior to invoking
143 public void expectCheckValidityFailure(MessageUpdateFunction<T> func) {
145 T msg = makeValidMessage();
150 fail("missing exception");
152 } catch (PoolingFeatureException expected) {
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., "").
164 * @param func function to update the message's field prior to invoking
167 public void expectCheckValidityFailure_NullOrEmpty(MessageFieldUpdateFunction<T> func) {
168 expectCheckValidityFailure(msg -> func.update(msg, null));
169 expectCheckValidityFailure(msg -> func.update(msg, ""));
173 * Makes a message using the default constructor.
175 * @return a new Message
177 public final T makeDefaultMessage() {
179 return subclazz.getConstructor().newInstance();
181 } catch (Exception e) {
182 throw new AssertionError(e);
187 // the remaining methods will typically be overridden
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.
194 * @return a valid Message
196 public abstract T makeValidMessage();
199 * Verifies that fields are set as expected by
200 * {@link #makeDefaultMessage()}.
202 * @param msg the default Message
204 public void testDefaultConstructorFields(T msg) {
205 assertNull(msg.getSource());
206 assertNull(msg.getChannel());
210 * Verifies that fields are set as expected by {@link #makeValidMessage()}.
212 * @param msg message whose fields are to be validated
214 public void testValidFields(T msg) {
215 assertEquals(VALID_HOST, msg.getSource());
216 assertEquals(VALID_CHANNEL, msg.getChannel());
220 * Function that updates a message.
222 * @param <T> type of Message the function updates
225 public static interface MessageUpdateFunction<T extends Message> {
230 * @param msg message to be updated
236 * Function that updates a single field within a message.
238 * @param <T> type of Message the function updates
241 public static interface MessageFieldUpdateFunction<T extends Message> {
244 * Updates a field within a message.
246 * @param msg message to be updated
247 * @param newValue new field value
249 void update(T msg, String newValue);