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.assertFalse;
25 import static org.junit.Assert.assertNull;
26 import static org.junit.Assert.assertTrue;
28 import org.junit.Test;
29 import org.onap.policy.common.endpoints.event.comm.Topic.CommInfrastructure;
31 public class ForwardTest extends SupportBasicMessageTester<Forward> {
32 // values set by makeValidMessage()
33 public static final CommInfrastructure VALID_PROTOCOL = CommInfrastructure.UEB;
34 public static final int VALID_HOPS = 0;
35 public static final String VALID_TOPIC = "topicA";
36 public static final String VALID_PAYLOAD = "payloadA";
37 public static final String VALID_REQUEST_ID = "requestIdA";
40 * Time, in milliseconds, after which the most recent message was created.
42 private static long tcreateMs;
44 public ForwardTest() {
49 public void testBumpNumHops() {
50 Forward msg = makeValidMessage();
52 for (int x = 0; x < 3; ++x) {
53 assertEquals("x=" + x, x, msg.getNumHops());
59 public void testGetNumHops_testSetNumHops() {
60 Forward msg = makeValidMessage();
63 assertEquals(VALID_HOPS, msg.getNumHops());
66 assertEquals(5, msg.getNumHops());
69 assertEquals(7, msg.getNumHops());
73 public void testGetCreateTimeMs_testSetCreateTimeMs() {
74 Forward msg = makeValidMessage();
77 assertTrue(msg.getCreateTimeMs() >= tcreateMs);
79 msg.setCreateTimeMs(1000L);
80 assertEquals(1000L, msg.getCreateTimeMs());
82 msg.setCreateTimeMs(2000L);
83 assertEquals(2000L, msg.getCreateTimeMs());
87 public void testGetProtocol_testSetProtocol() {
88 Forward msg = makeValidMessage();
91 assertEquals(CommInfrastructure.UEB, msg.getProtocol());
93 msg.setProtocol(CommInfrastructure.DMAAP);
94 assertEquals(CommInfrastructure.DMAAP, msg.getProtocol());
96 msg.setProtocol(CommInfrastructure.UEB);
97 assertEquals(CommInfrastructure.UEB, msg.getProtocol());
101 public void testGetTopic_testSetTopic() {
102 Forward msg = makeValidMessage();
105 assertEquals(VALID_TOPIC, msg.getTopic());
107 msg.setTopic("topicX");
108 assertEquals("topicX", msg.getTopic());
110 msg.setTopic("topicY");
111 assertEquals("topicY", msg.getTopic());
115 public void testGetPayload_testSetPayload() {
116 Forward msg = makeValidMessage();
119 assertEquals(VALID_PAYLOAD, msg.getPayload());
121 msg.setPayload("payloadX");
122 assertEquals("payloadX", msg.getPayload());
124 msg.setPayload("payloadY");
125 assertEquals("payloadY", msg.getPayload());
129 public void testGetRequestId_testSetRequestId() {
130 Forward msg = makeValidMessage();
133 assertEquals(VALID_REQUEST_ID, msg.getRequestId());
135 msg.setRequestId("reqX");
136 assertEquals("reqX", msg.getRequestId());
138 msg.setRequestId("reqY");
139 assertEquals("reqY", msg.getRequestId());
143 public void testIsExpired() {
144 Forward msg = makeValidMessage();
146 long tcreate = msg.getCreateTimeMs();
147 assertTrue(msg.isExpired(tcreate + 1));
148 assertTrue(msg.isExpired(tcreate + 10));
150 assertFalse(msg.isExpired(tcreate));
151 assertFalse(msg.isExpired(tcreate - 1));
152 assertFalse(msg.isExpired(tcreate - 10));
156 public void testCheckValidity_InvalidFields() throws Exception {
157 // null source (i.e., superclass field)
158 expectCheckValidityFailure(msg -> msg.setSource(null));
161 expectCheckValidityFailure(msg -> msg.setProtocol(null));
163 // null or empty topic
164 expectCheckValidityFailure_NullOrEmpty((msg, value) -> msg.setTopic(value));
167 expectCheckValidityFailure(msg -> msg.setPayload(null));
169 // empty payload should NOT throw an exception
170 Forward forward = makeValidMessage();
171 forward.setPayload("");
172 forward.checkValidity();
174 // null or empty requestId
175 expectCheckValidityFailure_NullOrEmpty((msg, value) -> msg.setRequestId(value));
178 expectCheckValidityFailure(msg -> msg.setNumHops(-1));
182 public Forward makeValidMessage() {
183 tcreateMs = System.currentTimeMillis();
185 Forward msg = new Forward(VALID_HOST, VALID_PROTOCOL, VALID_TOPIC, VALID_PAYLOAD, VALID_REQUEST_ID);
186 msg.setChannel(VALID_CHANNEL);
192 public void testDefaultConstructorFields(Forward msg) {
193 super.testDefaultConstructorFields(msg);
195 assertEquals(VALID_HOPS, msg.getNumHops());
196 assertEquals(0, msg.getCreateTimeMs());
197 assertNull(msg.getPayload());
198 assertNull(msg.getProtocol());
199 assertNull(msg.getRequestId());
200 assertNull(msg.getTopic());
204 public void testValidFields(Forward msg) {
205 super.testValidFields(msg);
207 assertEquals(VALID_HOPS, msg.getNumHops());
208 assertTrue(msg.getCreateTimeMs() >= tcreateMs);
209 assertEquals(VALID_PAYLOAD, msg.getPayload());
210 assertEquals(VALID_PROTOCOL, msg.getProtocol());
211 assertEquals(VALID_REQUEST_ID, msg.getRequestId());
212 assertEquals(VALID_TOPIC, msg.getTopic());