Generate notifications when policies change
[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 isSameContent() {
151         PdpUpdate msg2 = new PdpUpdate(update);
152         msg2.setName("world");
153         assertTrue(data.isSameContent(new UpdateReq(reqParams, MY_REQ_NAME, msg2)));
154
155         // different request type
156         assertFalse(data.isSameContent(new StateChangeReq(reqParams, MY_REQ_NAME, new PdpStateChange())));
157
158         // both policy lists null
159         update.setPolicies(null);
160         msg2.setPolicies(null);
161         assertTrue(data.isSameContent(new UpdateReq(reqParams, MY_REQ_NAME, msg2)));
162     }
163
164     @Test
165     public void isSameContent_BothGroupNamesNull() {
166         PdpUpdate msg2 = new PdpUpdate(update);
167         msg2.setPdpGroup(null);
168         update.setPdpGroup(null);
169         assertTrue(data.isSameContent(new UpdateReq(reqParams, MY_REQ_NAME, msg2)));
170     }
171
172     @Test
173     public void isSameContent_BothSubGroupNamesNull() {
174         PdpUpdate msg2 = new PdpUpdate(update);
175         msg2.setPdpSubgroup(null);
176         update.setPdpSubgroup(null);
177         assertTrue(data.isSameContent(new UpdateReq(reqParams, MY_REQ_NAME, msg2)));
178     }
179
180     @Test
181     public void isSameContent_DiffGroup() {
182         PdpUpdate msg2 = new PdpUpdate(update);
183         msg2.setPdpGroup(null);
184         assertFalse(data.isSameContent(new UpdateReq(reqParams, MY_REQ_NAME, msg2)));
185
186         msg2.setPdpGroup(DIFFERENT);
187         assertFalse(data.isSameContent(new UpdateReq(reqParams, MY_REQ_NAME, msg2)));
188
189         update.setPdpGroup(null);
190         assertFalse(data.isSameContent(new UpdateReq(reqParams, MY_REQ_NAME, msg2)));
191     }
192
193     @Test
194     public void isSameContent_DiffSubGroup() {
195         PdpUpdate msg2 = new PdpUpdate(update);
196         msg2.setPdpSubgroup(null);
197         assertFalse(data.isSameContent(new UpdateReq(reqParams, MY_REQ_NAME, msg2)));
198
199         msg2.setPdpSubgroup(DIFFERENT);
200         assertFalse(data.isSameContent(new UpdateReq(reqParams, MY_REQ_NAME, msg2)));
201
202         update.setPdpSubgroup(null);
203         assertFalse(data.isSameContent(new UpdateReq(reqParams, MY_REQ_NAME, msg2)));
204     }
205
206     @Test
207     public void isSameContent_DiffPolicies() {
208         PdpUpdate msg2 = new PdpUpdate(update);
209
210         ArrayList<ToscaPolicy> policies = new ArrayList<>(update.getPolicies());
211         policies.set(0, makePolicy(DIFFERENT, "10.0.0"));
212         msg2.setPolicies(policies);
213
214         assertFalse(data.isSameContent(new UpdateReq(reqParams, MY_REQ_NAME, msg2)));
215     }
216
217     @Test
218     public void isSameContent_DiffPolicies_NotNull_Null() {
219         PdpUpdate msg2 = new PdpUpdate(update);
220         msg2.setPolicies(null);
221
222         assertFalse(data.isSameContent(new UpdateReq(reqParams, MY_REQ_NAME, msg2)));
223     }
224
225     @Test
226     public void isSameContent_DiffPolicies_Null_NotNull() {
227         PdpUpdate msg2 = new PdpUpdate(update);
228
229         update.setPolicies(null);
230
231         assertFalse(data.isSameContent(new UpdateReq(reqParams, MY_REQ_NAME, msg2)));
232     }
233
234     @Test
235     public void testGetPriority() {
236         assertTrue(data.getPriority() > new StateChangeReq(reqParams, MY_REQ_NAME, new PdpStateChange()).getPriority());
237     }
238
239     @SuppressWarnings("unchecked")
240     private void verifyResponse() {
241         verify(notifier).processResponse(any(), any(Set.class));
242     }
243
244     @SuppressWarnings("unchecked")
245     private void verifyNoResponse() {
246         verify(notifier, never()).processResponse(any(), any(Set.class));
247     }
248
249     /**
250      * Makes an update message.
251      *
252      * @return a new update message
253      */
254     private PdpUpdate makeUpdate() {
255         PdpUpdate upd = new PdpUpdate();
256
257         upd.setDescription("update-description");
258         upd.setName(MY_NAME);
259         upd.setPdpGroup(MY_GROUP);
260         upd.setPdpSubgroup(MY_SUBGROUP);
261
262         ToscaPolicy policy1 = makePolicy("policy-1-a", "1.0.0");
263         ToscaPolicy policy2 = makePolicy("policy-2-a", "1.1.0");
264
265         upd.setPolicies(Arrays.asList(policy1, policy2));
266
267         return upd;
268     }
269 }