summaryrefslogtreecommitdiffstats
path: root/feature-pooling-dmaap/src/test/java/org/onap/policy/drools/pooling/message/ForwardTest.java
blob: bc92fa273a65acfda8b008b3931915a96fcf4c3b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/*
 * ============LICENSE_START=======================================================
 * ONAP
 * ================================================================================
 * Copyright (C) 2018 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */

package org.onap.policy.drools.pooling.message;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import org.junit.Test;
import org.onap.policy.drools.event.comm.Topic.CommInfrastructure;

public class ForwardTest extends BasicMessageTester<Forward> {
    // values set by makeValidMessage()
    public static final CommInfrastructure VALID_PROTOCOL = CommInfrastructure.UEB;
    public static final int VALID_HOPS = 0;
    public static final String VALID_TOPIC = "topicA";
    public static final String VALID_PAYLOAD = "payloadA";
    public static final String VALID_REQUEST_ID = "requestIdA";

    /**
     * Time, in milliseconds, after which the most recent message was created.
     */
    private static long tcreateMs;

    public ForwardTest() {
        super(Forward.class);
    }

    @Test
    public void testBumpNumHops() {
        Forward msg = makeValidMessage();

        for (int x = 0; x < 3; ++x) {
            assertEquals("x=" + x, x, msg.getNumHops());
            msg.bumpNumHops();
        }
    }

    @Test
    public void testGetNumHops_testSetNumHops() {
        Forward msg = makeValidMessage();

        // from constructor
        assertEquals(VALID_HOPS, msg.getNumHops());

        msg.setNumHops(5);
        assertEquals(5, msg.getNumHops());

        msg.setNumHops(7);
        assertEquals(7, msg.getNumHops());
    }

    @Test
    public void testGetCreateTimeMs_testSetCreateTimeMs() {
        Forward msg = makeValidMessage();

        // from constructor
        assertTrue(msg.getCreateTimeMs() >= tcreateMs);

        msg.setCreateTimeMs(1000L);
        assertEquals(1000L, msg.getCreateTimeMs());

        msg.setCreateTimeMs(2000L);
        assertEquals(2000L, msg.getCreateTimeMs());
    }

    @Test
    public void testGetProtocol_testSetProtocol() {
        Forward msg = makeValidMessage();

        // from constructor
        assertEquals(CommInfrastructure.UEB, msg.getProtocol());

        msg.setProtocol(CommInfrastructure.DMAAP);
        assertEquals(CommInfrastructure.DMAAP, msg.getProtocol());

        msg.setProtocol(CommInfrastructure.UEB);
        assertEquals(CommInfrastructure.UEB, msg.getProtocol());
    }

    @Test
    public void testGetTopic_testSetTopic() {
        Forward msg = makeValidMessage();

        // from constructor
        assertEquals(VALID_TOPIC, msg.getTopic());

        msg.setTopic("topicX");
        assertEquals("topicX", msg.getTopic());

        msg.setTopic("topicY");
        assertEquals("topicY", msg.getTopic());
    }

    @Test
    public void testGetPayload_testSetPayload() {
        Forward msg = makeValidMessage();

        // from constructor
        assertEquals(VALID_PAYLOAD, msg.getPayload());

        msg.setPayload("payloadX");
        assertEquals("payloadX", msg.getPayload());

        msg.setPayload("payloadY");
        assertEquals("payloadY", msg.getPayload());
    }

    @Test
    public void testGetRequestId_testSetRequestId() {
        Forward msg = makeValidMessage();

        // from constructor
        assertEquals(VALID_REQUEST_ID, msg.getRequestId());

        msg.setRequestId("reqX");
        assertEquals("reqX", msg.getRequestId());

        msg.setRequestId("reqY");
        assertEquals("reqY", msg.getRequestId());
    }

    @Test
    public void testIsExpired() {
        Forward msg = makeValidMessage();

        long tcreate = msg.getCreateTimeMs();
        assertTrue(msg.isExpired(tcreate + 1));
        assertTrue(msg.isExpired(tcreate + 10));

        assertFalse(msg.isExpired(tcreate));
        assertFalse(msg.isExpired(tcreate - 1));
        assertFalse(msg.isExpired(tcreate - 10));
    }

    @Test
    public void testCheckValidity_InvalidFields() throws Exception {
        // null source (i.e., superclass field)
        expectCheckValidityFailure(msg -> msg.setSource(null));
        
        // null protocol
        expectCheckValidityFailure(msg -> msg.setProtocol(null));
        
        // null or empty topic
        expectCheckValidityFailure_NullOrEmpty((msg, value) -> msg.setTopic(value));
        
        // null payload
        expectCheckValidityFailure(msg -> msg.setPayload(null));
        
        // empty payload should NOT throw an exception
        Forward forward = makeValidMessage();
        forward.setPayload("");
        forward.checkValidity();
        
        // null or empty requestId
        expectCheckValidityFailure_NullOrEmpty((msg, value) -> msg.setRequestId(value));
        
        // invalid hop count
        expectCheckValidityFailure(msg -> msg.setNumHops(-1));
    }

    @Override
    public Forward makeValidMessage() {
        tcreateMs = System.currentTimeMillis();

        Forward msg = new Forward(VALID_HOST, VALID_PROTOCOL, VALID_TOPIC, VALID_PAYLOAD, VALID_REQUEST_ID);
        msg.setChannel(VALID_CHANNEL);

        return msg;
    }

    @Override
    public void testDefaultConstructorFields(Forward msg) {
        super.testDefaultConstructorFields(msg);
        
        assertEquals(VALID_HOPS, msg.getNumHops());
        assertEquals(0, msg.getCreateTimeMs());
        assertNull(msg.getPayload());
        assertNull(msg.getProtocol());
        assertNull(msg.getRequestId());
        assertNull(msg.getTopic());
    }

    @Override
    public void testValidFields(Forward msg) {
        super.testValidFields(msg);
        
        assertEquals(VALID_HOPS, msg.getNumHops());
        assertTrue(msg.getCreateTimeMs() >= tcreateMs);
        assertEquals(VALID_PAYLOAD, msg.getPayload());
        assertEquals(VALID_PROTOCOL, msg.getProtocol());
        assertEquals(VALID_REQUEST_ID, msg.getRequestId());
        assertEquals(VALID_TOPIC, msg.getTopic());
    }
}