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