Re-encrypt drools-pdp properties
[policy/drools-pdp.git] / feature-pooling-dmaap / src / test / java / org / onap / policy / drools / pooling / message / ForwardTest.java
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.onap.policy.drools.pooling.message;
22
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;
27
28 import org.junit.Test;
29 import org.onap.policy.common.endpoints.event.comm.Topic.CommInfrastructure;
30
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";
38
39     /**
40      * Time, in milliseconds, after which the most recent message was created.
41      */
42     private static long tcreateMs;
43
44     public ForwardTest() {
45         super(Forward.class);
46     }
47
48     @Test
49     public void testBumpNumHops() {
50         Forward msg = makeValidMessage();
51
52         for (int x = 0; x < 3; ++x) {
53             assertEquals("x=" + x, x, msg.getNumHops());
54             msg.bumpNumHops();
55         }
56     }
57
58     @Test
59     public void testGetNumHops_testSetNumHops() {
60         Forward msg = makeValidMessage();
61
62         // from constructor
63         assertEquals(VALID_HOPS, msg.getNumHops());
64
65         msg.setNumHops(5);
66         assertEquals(5, msg.getNumHops());
67
68         msg.setNumHops(7);
69         assertEquals(7, msg.getNumHops());
70     }
71
72     @Test
73     public void testGetCreateTimeMs_testSetCreateTimeMs() {
74         Forward msg = makeValidMessage();
75
76         // from constructor
77         assertTrue(msg.getCreateTimeMs() >= tcreateMs);
78
79         msg.setCreateTimeMs(1000L);
80         assertEquals(1000L, msg.getCreateTimeMs());
81
82         msg.setCreateTimeMs(2000L);
83         assertEquals(2000L, msg.getCreateTimeMs());
84     }
85
86     @Test
87     public void testGetProtocol_testSetProtocol() {
88         Forward msg = makeValidMessage();
89
90         // from constructor
91         assertEquals(CommInfrastructure.UEB, msg.getProtocol());
92
93         msg.setProtocol(CommInfrastructure.DMAAP);
94         assertEquals(CommInfrastructure.DMAAP, msg.getProtocol());
95
96         msg.setProtocol(CommInfrastructure.UEB);
97         assertEquals(CommInfrastructure.UEB, msg.getProtocol());
98     }
99
100     @Test
101     public void testGetTopic_testSetTopic() {
102         Forward msg = makeValidMessage();
103
104         // from constructor
105         assertEquals(VALID_TOPIC, msg.getTopic());
106
107         msg.setTopic("topicX");
108         assertEquals("topicX", msg.getTopic());
109
110         msg.setTopic("topicY");
111         assertEquals("topicY", msg.getTopic());
112     }
113
114     @Test
115     public void testGetPayload_testSetPayload() {
116         Forward msg = makeValidMessage();
117
118         // from constructor
119         assertEquals(VALID_PAYLOAD, msg.getPayload());
120
121         msg.setPayload("payloadX");
122         assertEquals("payloadX", msg.getPayload());
123
124         msg.setPayload("payloadY");
125         assertEquals("payloadY", msg.getPayload());
126     }
127
128     @Test
129     public void testGetRequestId_testSetRequestId() {
130         Forward msg = makeValidMessage();
131
132         // from constructor
133         assertEquals(VALID_REQUEST_ID, msg.getRequestId());
134
135         msg.setRequestId("reqX");
136         assertEquals("reqX", msg.getRequestId());
137
138         msg.setRequestId("reqY");
139         assertEquals("reqY", msg.getRequestId());
140     }
141
142     @Test
143     public void testIsExpired() {
144         Forward msg = makeValidMessage();
145
146         long tcreate = msg.getCreateTimeMs();
147         assertTrue(msg.isExpired(tcreate + 1));
148         assertTrue(msg.isExpired(tcreate + 10));
149
150         assertFalse(msg.isExpired(tcreate));
151         assertFalse(msg.isExpired(tcreate - 1));
152         assertFalse(msg.isExpired(tcreate - 10));
153     }
154
155     @Test
156     public void testCheckValidity_InvalidFields() throws Exception {
157         // null source (i.e., superclass field)
158         expectCheckValidityFailure(msg -> msg.setSource(null));
159         
160         // null protocol
161         expectCheckValidityFailure(msg -> msg.setProtocol(null));
162         
163         // null or empty topic
164         expectCheckValidityFailure_NullOrEmpty((msg, value) -> msg.setTopic(value));
165         
166         // null payload
167         expectCheckValidityFailure(msg -> msg.setPayload(null));
168         
169         // empty payload should NOT throw an exception
170         Forward forward = makeValidMessage();
171         forward.setPayload("");
172         forward.checkValidity();
173         
174         // null or empty requestId
175         expectCheckValidityFailure_NullOrEmpty((msg, value) -> msg.setRequestId(value));
176         
177         // invalid hop count
178         expectCheckValidityFailure(msg -> msg.setNumHops(-1));
179     }
180
181     @Override
182     public Forward makeValidMessage() {
183         tcreateMs = System.currentTimeMillis();
184
185         Forward msg = new Forward(VALID_HOST, VALID_PROTOCOL, VALID_TOPIC, VALID_PAYLOAD, VALID_REQUEST_ID);
186         msg.setChannel(VALID_CHANNEL);
187
188         return msg;
189     }
190
191     @Override
192     public void testDefaultConstructorFields(Forward msg) {
193         super.testDefaultConstructorFields(msg);
194         
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());
201     }
202
203     @Override
204     public void testValidFields(Forward msg) {
205         super.testValidFields(msg);
206         
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());
213     }
214 }