Simplify request handling in PAP
[policy/pap.git] / main / src / test / java / org / onap / policy / pap / main / comm / msgdata / UpdateReqTest.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.msgdata;
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.assertSame;
27 import static org.junit.Assert.assertTrue;
28 import static org.mockito.Matchers.any;
29 import static org.mockito.Mockito.never;
30 import static org.mockito.Mockito.verify;
31
32 import java.util.ArrayList;
33 import java.util.Arrays;
34 import java.util.Set;
35 import java.util.stream.Collectors;
36 import org.junit.Before;
37 import org.junit.Test;
38 import org.onap.policy.models.pdp.concepts.PdpStateChange;
39 import org.onap.policy.models.pdp.concepts.PdpStatus;
40 import org.onap.policy.models.pdp.concepts.PdpUpdate;
41 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
42 import org.onap.policy.pap.main.comm.CommonRequestBase;
43
44 public class UpdateReqTest extends CommonRequestBase {
45
46     private UpdateReq data;
47     private PdpUpdate update;
48     private PdpStatus response;
49
50     /**
51      * Sets up.
52      *
53      * @throws Exception if an error occurs
54      */
55     @Before
56     public void setUp() throws Exception {
57         super.setUp();
58
59         response = new PdpStatus();
60
61         update = makeUpdate();
62
63         response.setName(MY_NAME);
64         response.setPdpGroup(update.getPdpGroup());
65         response.setPdpSubgroup(update.getPdpSubgroup());
66         response.setPolicies(
67                         update.getPolicies().stream().map(ToscaPolicy::getIdentifier).collect(Collectors.toList()));
68
69         data = new UpdateReq(reqParams, MY_REQ_NAME, update);
70         data.setNotifier(notifier);
71     }
72
73     @Test
74     public void testGetMessage() {
75         assertEquals(MY_REQ_NAME, data.getName());
76         assertSame(update, data.getMessage());
77     }
78
79     @Test
80     public void testCheckResponse() {
81         assertNull(data.checkResponse(response));
82         verifyResponse();
83
84         // both policy lists null
85         update.setPolicies(null);
86         response.setPolicies(null);
87         assertNull(data.checkResponse(response));
88     }
89
90     @Test
91     public void testCheckResponse_NullName() {
92         response.setName(null);
93
94         assertEquals("null PDP name", data.checkResponse(response));
95         verifyNoResponse();
96     }
97
98     @Test
99     public void testCheckResponse_NullMsgName() {
100         update.setName(null);
101
102         assertEquals(null, data.checkResponse(response));
103         verifyResponse();
104     }
105
106     @Test
107     public void testUpdateReqCheckResponse_MismatchedGroup() {
108         response.setPdpGroup(DIFFERENT);
109
110         assertEquals("group does not match", data.checkResponse(response));
111         verifyResponse();
112     }
113
114     @Test
115     public void testUpdateReqCheckResponse_MismatchedSubGroup() {
116         response.setPdpSubgroup(DIFFERENT);
117
118         assertEquals("subgroup does not match", data.checkResponse(response));
119         verifyResponse();
120     }
121
122     @Test
123     public void testUpdateReqCheckResponse_MismatchedPolicies() {
124         ArrayList<ToscaPolicy> policies = new ArrayList<>(update.getPolicies());
125         policies.set(0, makePolicy(DIFFERENT, "10.0.0"));
126
127         response.setPolicies(policies.stream().map(ToscaPolicy::getIdentifier).collect(Collectors.toList()));
128
129         assertEquals("policies do not match", data.checkResponse(response));
130         verifyResponse();
131     }
132
133     @Test
134     public void testUpdateReqCheckResponse_MismatchedPolicies_Null_NotNull() {
135         update.setPolicies(null);
136
137         assertEquals("policies do not match", data.checkResponse(response));
138         verifyResponse();
139     }
140
141     @Test
142     public void testUpdateReqCheckResponse_MismatchedPolicies_NotNull_Null() {
143         response.setPolicies(null);
144
145         assertEquals("policies do not match", data.checkResponse(response));
146         verifyResponse();
147     }
148
149     @Test
150     public void testReconfigure() {
151         // different message type should fail and leave message unchanged
152         assertFalse(data.reconfigure(new PdpStateChange()));
153         assertSame(update, data.getMessage());
154
155         // same content - should succeed, but leave message unchanged
156         PdpUpdate msg2 = new PdpUpdate(update);
157         assertTrue(data.reconfigure(msg2));
158         assertSame(update, data.getMessage());
159
160         // different content - should succeed and install NEW message
161         msg2.setPdpGroup(DIFFERENT);
162         assertTrue(data.reconfigure(msg2));
163         assertSame(msg2, data.getMessage());
164     }
165
166     @Test
167     public void isSameContent() {
168         PdpUpdate msg2 = new PdpUpdate(update);
169         msg2.setName("world");
170         assertTrue(data.isSameContent(msg2));
171
172         // both policy lists null
173         update.setPolicies(null);
174         msg2.setPolicies(null);
175         assertTrue(data.isSameContent(msg2));
176     }
177
178     @Test
179     public void isSameContent_BothGroupNamesNull() {
180         PdpUpdate msg2 = new PdpUpdate(update);
181         msg2.setPdpGroup(null);
182         update.setPdpGroup(null);
183         assertTrue(data.isSameContent(msg2));
184     }
185
186     @Test
187     public void isSameContent_BothSubGroupNamesNull() {
188         PdpUpdate msg2 = new PdpUpdate(update);
189         msg2.setPdpSubgroup(null);
190         update.setPdpSubgroup(null);
191         assertTrue(data.isSameContent(msg2));
192     }
193
194     @Test
195     public void isSameContent_DiffGroup() {
196         PdpUpdate msg2 = new PdpUpdate(update);
197         msg2.setPdpGroup(null);
198         assertFalse(data.isSameContent(msg2));
199
200         msg2.setPdpGroup(DIFFERENT);
201         assertFalse(data.isSameContent(msg2));
202
203         update.setPdpGroup(null);
204         assertFalse(data.isSameContent(msg2));
205     }
206
207     @Test
208     public void isSameContent_DiffSubGroup() {
209         PdpUpdate msg2 = new PdpUpdate(update);
210         msg2.setPdpSubgroup(null);
211         assertFalse(data.isSameContent(msg2));
212
213         msg2.setPdpSubgroup(DIFFERENT);
214         assertFalse(data.isSameContent(msg2));
215
216         update.setPdpSubgroup(null);
217         assertFalse(data.isSameContent(msg2));
218     }
219
220     @Test
221     public void isSameContent_DiffPolicies() {
222         PdpUpdate msg2 = new PdpUpdate(update);
223
224         ArrayList<ToscaPolicy> policies = new ArrayList<>(update.getPolicies());
225         policies.set(0, makePolicy(DIFFERENT, "10.0.0"));
226         msg2.setPolicies(policies);
227
228         assertFalse(data.isSameContent(msg2));
229     }
230
231     @Test
232     public void isSameContent_DiffPolicies_NotNull_Null() {
233         PdpUpdate msg2 = new PdpUpdate(update);
234         msg2.setPolicies(null);
235
236         assertFalse(data.isSameContent(msg2));
237     }
238
239     @Test
240     public void isSameContent_DiffPolicies_Null_NotNull() {
241         PdpUpdate msg2 = new PdpUpdate(update);
242
243         update.setPolicies(null);
244
245         assertFalse(data.isSameContent(msg2));
246     }
247
248     @SuppressWarnings("unchecked")
249     private void verifyResponse() {
250         verify(notifier).processResponse(any(), any(Set.class));
251     }
252
253     @SuppressWarnings("unchecked")
254     private void verifyNoResponse() {
255         verify(notifier, never()).processResponse(any(), any(Set.class));
256     }
257
258     /**
259      * Makes an update message.
260      *
261      * @return a new update message
262      */
263     private PdpUpdate makeUpdate() {
264         PdpUpdate upd = new PdpUpdate();
265
266         upd.setDescription("update-description");
267         upd.setName(MY_NAME);
268         upd.setPdpGroup(MY_GROUP);
269         upd.setPdpSubgroup(MY_SUBGROUP);
270
271         ToscaPolicy policy1 = makePolicy("policy-1-a", "1.0.0");
272         ToscaPolicy policy2 = makePolicy("policy-2-a", "1.1.0");
273
274         upd.setPolicies(Arrays.asList(policy1, policy2));
275
276         return upd;
277     }
278 }