2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.policy.pap.main.comm.msgdata;
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;
32 import java.util.ArrayList;
33 import java.util.Arrays;
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;
44 public class UpdateReqTest extends CommonRequestBase {
46 private UpdateReq data;
47 private PdpUpdate update;
48 private PdpStatus response;
53 * @throws Exception if an error occurs
56 public void setUp() throws Exception {
59 response = new PdpStatus();
61 update = makeUpdate();
63 response.setName(MY_NAME);
64 response.setPdpGroup(update.getPdpGroup());
65 response.setPdpSubgroup(update.getPdpSubgroup());
67 update.getPolicies().stream().map(ToscaPolicy::getIdentifier).collect(Collectors.toList()));
69 data = new UpdateReq(reqParams, MY_REQ_NAME, update);
70 data.setNotifier(notifier);
74 public void testGetMessage() {
75 assertEquals(MY_REQ_NAME, data.getName());
76 assertSame(update, data.getMessage());
80 public void testCheckResponse() {
81 assertNull(data.checkResponse(response));
84 // both policy lists null
85 update.setPolicies(null);
86 response.setPolicies(null);
87 assertNull(data.checkResponse(response));
91 public void testCheckResponse_NullName() {
92 response.setName(null);
94 assertEquals("null PDP name", data.checkResponse(response));
99 public void testCheckResponse_NullMsgName() {
100 update.setName(null);
102 assertEquals(null, data.checkResponse(response));
107 public void testUpdateReqCheckResponse_MismatchedGroup() {
108 response.setPdpGroup(DIFFERENT);
110 assertEquals("group does not match", data.checkResponse(response));
115 public void testUpdateReqCheckResponse_MismatchedSubGroup() {
116 response.setPdpSubgroup(DIFFERENT);
118 assertEquals("subgroup does not match", data.checkResponse(response));
123 public void testUpdateReqCheckResponse_MismatchedPolicies() {
124 ArrayList<ToscaPolicy> policies = new ArrayList<>(update.getPolicies());
125 policies.set(0, makePolicy(DIFFERENT, "10.0.0"));
127 response.setPolicies(policies.stream().map(ToscaPolicy::getIdentifier).collect(Collectors.toList()));
129 assertEquals("policies do not match", data.checkResponse(response));
134 public void testUpdateReqCheckResponse_MismatchedPolicies_Null_NotNull() {
135 update.setPolicies(null);
137 assertEquals("policies do not match", data.checkResponse(response));
142 public void testUpdateReqCheckResponse_MismatchedPolicies_NotNull_Null() {
143 response.setPolicies(null);
145 assertEquals("policies do not match", data.checkResponse(response));
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());
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());
160 // different content - should succeed and install NEW message
161 msg2.setPdpGroup(DIFFERENT);
162 assertTrue(data.reconfigure(msg2));
163 assertSame(msg2, data.getMessage());
167 public void isSameContent() {
168 PdpUpdate msg2 = new PdpUpdate(update);
169 msg2.setName("world");
170 assertTrue(data.isSameContent(msg2));
172 // both policy lists null
173 update.setPolicies(null);
174 msg2.setPolicies(null);
175 assertTrue(data.isSameContent(msg2));
179 public void isSameContent_BothGroupNamesNull() {
180 PdpUpdate msg2 = new PdpUpdate(update);
181 msg2.setPdpGroup(null);
182 update.setPdpGroup(null);
183 assertTrue(data.isSameContent(msg2));
187 public void isSameContent_BothSubGroupNamesNull() {
188 PdpUpdate msg2 = new PdpUpdate(update);
189 msg2.setPdpSubgroup(null);
190 update.setPdpSubgroup(null);
191 assertTrue(data.isSameContent(msg2));
195 public void isSameContent_DiffGroup() {
196 PdpUpdate msg2 = new PdpUpdate(update);
197 msg2.setPdpGroup(null);
198 assertFalse(data.isSameContent(msg2));
200 msg2.setPdpGroup(DIFFERENT);
201 assertFalse(data.isSameContent(msg2));
203 update.setPdpGroup(null);
204 assertFalse(data.isSameContent(msg2));
208 public void isSameContent_DiffSubGroup() {
209 PdpUpdate msg2 = new PdpUpdate(update);
210 msg2.setPdpSubgroup(null);
211 assertFalse(data.isSameContent(msg2));
213 msg2.setPdpSubgroup(DIFFERENT);
214 assertFalse(data.isSameContent(msg2));
216 update.setPdpSubgroup(null);
217 assertFalse(data.isSameContent(msg2));
221 public void isSameContent_DiffPolicies() {
222 PdpUpdate msg2 = new PdpUpdate(update);
224 ArrayList<ToscaPolicy> policies = new ArrayList<>(update.getPolicies());
225 policies.set(0, makePolicy(DIFFERENT, "10.0.0"));
226 msg2.setPolicies(policies);
228 assertFalse(data.isSameContent(msg2));
232 public void isSameContent_DiffPolicies_NotNull_Null() {
233 PdpUpdate msg2 = new PdpUpdate(update);
234 msg2.setPolicies(null);
236 assertFalse(data.isSameContent(msg2));
240 public void isSameContent_DiffPolicies_Null_NotNull() {
241 PdpUpdate msg2 = new PdpUpdate(update);
243 update.setPolicies(null);
245 assertFalse(data.isSameContent(msg2));
248 @SuppressWarnings("unchecked")
249 private void verifyResponse() {
250 verify(notifier).processResponse(any(), any(Set.class));
253 @SuppressWarnings("unchecked")
254 private void verifyNoResponse() {
255 verify(notifier, never()).processResponse(any(), any(Set.class));
259 * Makes an update message.
261 * @return a new update message
263 private PdpUpdate makeUpdate() {
264 PdpUpdate upd = new PdpUpdate();
266 upd.setDescription("update-description");
267 upd.setName(MY_NAME);
268 upd.setPdpGroup(MY_GROUP);
269 upd.setPdpSubgroup(MY_SUBGROUP);
271 ToscaPolicy policy1 = makePolicy("policy-1-a", "1.0.0");
272 ToscaPolicy policy2 = makePolicy("policy-2-a", "1.1.0");
274 upd.setPolicies(Arrays.asList(policy1, policy2));