8c257f0b3f2a1f4f3f5cffb67e9e39e71c266bc8
[policy/pap.git] / main / src / test / java / org / onap / policy / pap / main / comm / PdpRequestsTest.java
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP PAP
4  * ================================================================================
5  * Copyright (C) 2019 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.pap.main.comm;
22
23 import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertFalse;
26 import static org.junit.Assert.assertSame;
27 import static org.junit.Assert.assertTrue;
28 import static org.mockito.Mockito.never;
29 import static org.mockito.Mockito.times;
30 import static org.mockito.Mockito.verify;
31 import static org.mockito.Mockito.when;
32
33 import org.junit.Before;
34 import org.junit.Test;
35 import org.onap.policy.pap.main.comm.msgdata.StateChangeReq;
36 import org.onap.policy.pap.main.comm.msgdata.UpdateReq;
37
38 public class PdpRequestsTest extends CommonRequestBase {
39
40     private PdpRequests data;
41     private UpdateReq update;
42     private StateChangeReq change;
43
44     /**
45      * Sets up.
46      */
47     @Before
48     public void setUp() {
49         update = makeUpdateReq(PDP1, MY_GROUP, MY_SUBGROUP);
50         change = makeStateChangeReq(PDP1, MY_STATE);
51
52         data = new PdpRequests(PDP1, notifier);
53     }
54
55     @Test
56     public void testPdpRequests_testGetLastGroupName() {
57         assertEquals(PDP1, data.getPdpName());
58         assertSame(notifier, data.getNotifier());
59     }
60
61     @Test
62     public void testAddSingleton() {
63         data.addSingleton(update);
64
65         verify(update).setNotifier(notifier);
66         verify(update).startPublishing();
67     }
68
69     @Test
70     public void testAddSingleton_SameAsExisting() {
71         data.addSingleton(update);
72
73         // add duplicate update
74         UpdateReq req2 = makeUpdateReq(PDP1, MY_GROUP, MY_SUBGROUP);
75         data.addSingleton(req2);
76
77         // should not publish duplicate
78         verify(req2, never()).startPublishing();
79
80         // should not re-publish original
81         verify(update, times(1)).startPublishing();
82     }
83
84     @Test
85     public void testAddSingleton_AnotherRequest() {
86         data.addSingleton(update);
87
88         // add another request
89         data.addSingleton(change);
90
91         // add duplicate requests
92         StateChangeReq change2 = makeStateChangeReq(PDP1, MY_STATE);
93         when(change.reconfigure(change2.getMessage())).thenReturn(true);
94         data.addSingleton(change2);
95
96         UpdateReq update2 = makeUpdateReq(PDP1, MY_GROUP, MY_SUBGROUP);
97         when(update.reconfigure(update2.getMessage())).thenReturn(true);
98         data.addSingleton(update2);
99
100         // should still only be publishing the first request
101         verify(update).startPublishing();
102
103         verify(change, never()).startPublishing();
104         verify(change2, never()).startPublishing();
105         verify(update2, never()).startPublishing();
106     }
107
108     @Test
109     public void testAddSingleton_Broadcast() {
110         UpdateReq req = makeUpdateReq(null, MY_GROUP, MY_SUBGROUP);
111         assertThatIllegalArgumentException().isThrownBy(() -> data.addSingleton(req))
112                         .withMessage("unexpected broadcast for pdp_1");
113     }
114
115     @Test
116     public void testStopPublishing() {
117         // nothing in the queue - nothing should happen
118         data.stopPublishing();
119
120         data.addSingleton(update);
121         data.addSingleton(change);
122
123         data.stopPublishing();
124
125         verify(update).stopPublishing();
126         verify(change, never()).stopPublishing();
127
128         // repeat, but with only one request in the queue
129         data.addSingleton(update);
130         data.stopPublishing();
131         verify(update, times(2)).stopPublishing();
132         verify(change, never()).stopPublishing();
133     }
134
135     @Test
136     public void testStartNextRequest_NothingToStart() {
137         assertFalse(data.startNextRequest(update));
138
139         // should not have published it
140         verify(update, never()).startPublishing();
141     }
142
143     /**
144      * Tests addSingleton() when only one request is in the queue.
145      */
146     @Test
147     public void testStartNextRequest_OneRequest() {
148         data.addSingleton(update);
149         assertFalse(data.startNextRequest(update));
150
151         // invoke again
152         assertFalse(data.startNextRequest(update));
153
154         // should have only been published once
155         verify(update, times(1)).startPublishing();
156     }
157
158     /**
159      * Tests addSingleton() when more than one request is in the queue.
160      */
161     @Test
162     public void testStartNextRequest_MultipleRequests() {
163         data.addSingleton(update);
164         data.addSingleton(change);
165
166         // update is still active - should return true
167         assertTrue(data.startNextRequest(change));
168
169         // invoke again
170         assertTrue(data.startNextRequest(change));
171
172         // should not have published yet
173         verify(change, never()).startPublishing();
174
175         // should publish "change" once update completes
176         assertTrue(data.startNextRequest(update));
177         verify(change).startPublishing();
178
179         // nothing more in the queue once it completes
180         assertFalse(data.startNextRequest(change));
181     }
182
183     @Test
184     public void testIsFirstInQueue() {
185         // test with empty queue
186         assertFalse(data.isFirstInQueue(update));
187
188         data.addSingleton(update);
189         assertTrue(data.isFirstInQueue(update));
190
191         data.addSingleton(change);
192         assertTrue(data.isFirstInQueue(update));
193         assertFalse(data.isFirstInQueue(change));
194     }
195
196     @Test
197     public void testGetPdpName() {
198         assertEquals(PDP1, data.getPdpName());
199     }
200 }