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 isSameContent() {
151 PdpUpdate msg2 = new PdpUpdate(update);
152 msg2.setName("world");
153 assertTrue(data.isSameContent(new UpdateReq(reqParams, MY_REQ_NAME, msg2)));
155 // different request type
156 assertFalse(data.isSameContent(new StateChangeReq(reqParams, MY_REQ_NAME, new PdpStateChange())));
158 // both policy lists null
159 update.setPolicies(null);
160 msg2.setPolicies(null);
161 assertTrue(data.isSameContent(new UpdateReq(reqParams, MY_REQ_NAME, msg2)));
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)));
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)));
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)));
186 msg2.setPdpGroup(DIFFERENT);
187 assertFalse(data.isSameContent(new UpdateReq(reqParams, MY_REQ_NAME, msg2)));
189 update.setPdpGroup(null);
190 assertFalse(data.isSameContent(new UpdateReq(reqParams, MY_REQ_NAME, msg2)));
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)));
199 msg2.setPdpSubgroup(DIFFERENT);
200 assertFalse(data.isSameContent(new UpdateReq(reqParams, MY_REQ_NAME, msg2)));
202 update.setPdpSubgroup(null);
203 assertFalse(data.isSameContent(new UpdateReq(reqParams, MY_REQ_NAME, msg2)));
207 public void isSameContent_DiffPolicies() {
208 PdpUpdate msg2 = new PdpUpdate(update);
210 ArrayList<ToscaPolicy> policies = new ArrayList<>(update.getPolicies());
211 policies.set(0, makePolicy(DIFFERENT, "10.0.0"));
212 msg2.setPolicies(policies);
214 assertFalse(data.isSameContent(new UpdateReq(reqParams, MY_REQ_NAME, msg2)));
218 public void isSameContent_DiffPolicies_NotNull_Null() {
219 PdpUpdate msg2 = new PdpUpdate(update);
220 msg2.setPolicies(null);
222 assertFalse(data.isSameContent(new UpdateReq(reqParams, MY_REQ_NAME, msg2)));
226 public void isSameContent_DiffPolicies_Null_NotNull() {
227 PdpUpdate msg2 = new PdpUpdate(update);
229 update.setPolicies(null);
231 assertFalse(data.isSameContent(new UpdateReq(reqParams, MY_REQ_NAME, msg2)));
235 public void testGetPriority() {
236 assertTrue(data.getPriority() > new StateChangeReq(reqParams, MY_REQ_NAME, new PdpStateChange()).getPriority());
239 @SuppressWarnings("unchecked")
240 private void verifyResponse() {
241 verify(notifier).processResponse(any(), any(Set.class));
244 @SuppressWarnings("unchecked")
245 private void verifyNoResponse() {
246 verify(notifier, never()).processResponse(any(), any(Set.class));
250 * Makes an update message.
252 * @return a new update message
254 private PdpUpdate makeUpdate() {
255 PdpUpdate upd = new PdpUpdate();
257 upd.setDescription("update-description");
258 upd.setName(MY_NAME);
259 upd.setPdpGroup(MY_GROUP);
260 upd.setPdpSubgroup(MY_SUBGROUP);
262 ToscaPolicy policy1 = makePolicy("policy-1-a", "1.0.0");
263 ToscaPolicy policy2 = makePolicy("policy-2-a", "1.1.0");
265 upd.setPolicies(Arrays.asList(policy1, policy2));