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;
29 import java.util.ArrayList;
30 import java.util.Arrays;
31 import java.util.stream.Collectors;
32 import org.junit.Before;
33 import org.junit.Test;
34 import org.onap.policy.models.pdp.concepts.PdpStateChange;
35 import org.onap.policy.models.pdp.concepts.PdpStatus;
36 import org.onap.policy.models.pdp.concepts.PdpUpdate;
37 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
38 import org.onap.policy.pap.main.comm.CommonRequestBase;
40 public class UpdateReqTest extends CommonRequestBase {
42 private UpdateReq data;
43 private PdpUpdate update;
44 private PdpStatus response;
48 * @throws Exception if an error occurs
51 public void setUp() throws Exception {
54 response = new PdpStatus();
56 update = makeUpdate();
58 response.setName(MY_NAME);
59 response.setPdpGroup(update.getPdpGroup());
60 response.setPdpSubgroup(update.getPdpSubgroup());
62 update.getPolicies().stream().map(ToscaPolicy::getIdentifier).collect(Collectors.toList()));
64 data = new UpdateReq(reqParams, MY_REQ_NAME, update);
68 public void testGetMessage() {
69 assertEquals(MY_REQ_NAME, data.getName());
70 assertSame(update, data.getMessage());
74 public void testCheckResponse() {
75 assertNull(data.checkResponse(response));
79 public void testCheckResponse_NullName() {
80 response.setName(null);
82 assertEquals("null PDP name", data.checkResponse(response));
86 public void testCheckResponse_NullMsgName() {
89 assertEquals(null, data.checkResponse(response));
93 public void testUpdateReqCheckResponse_MismatchedGroup() {
94 response.setPdpGroup(DIFFERENT);
96 assertEquals("group does not match", data.checkResponse(response));
100 public void testUpdateReqCheckResponse_MismatchedSubGroup() {
101 response.setPdpSubgroup(DIFFERENT);
103 assertEquals("subgroup does not match", data.checkResponse(response));
107 public void testUpdateReqCheckResponse_MismatchedPolicies() {
108 ArrayList<ToscaPolicy> policies = new ArrayList<>(update.getPolicies());
109 policies.set(0, makePolicy(DIFFERENT, "10.0.0"));
111 response.setPolicies(policies.stream().map(ToscaPolicy::getIdentifier).collect(Collectors.toList()));
113 assertEquals("policies do not match", data.checkResponse(response));
117 public void isSameContent() {
118 PdpUpdate msg2 = new PdpUpdate(update);
119 msg2.setName("world");
120 assertTrue(data.isSameContent(new UpdateReq(reqParams, MY_REQ_NAME, msg2)));
122 // different request type
123 assertFalse(data.isSameContent(new StateChangeReq(reqParams, MY_REQ_NAME, new PdpStateChange())));
127 public void isSameContent_BothGroupNamesNull() {
128 PdpUpdate msg2 = new PdpUpdate(update);
129 msg2.setPdpGroup(null);
130 update.setPdpGroup(null);
131 assertTrue(data.isSameContent(new UpdateReq(reqParams, MY_REQ_NAME, msg2)));
135 public void isSameContent_BothSubGroupNamesNull() {
136 PdpUpdate msg2 = new PdpUpdate(update);
137 msg2.setPdpSubgroup(null);
138 update.setPdpSubgroup(null);
139 assertTrue(data.isSameContent(new UpdateReq(reqParams, MY_REQ_NAME, msg2)));
143 public void isSameContent_DiffGroup() {
144 PdpUpdate msg2 = new PdpUpdate(update);
145 msg2.setPdpGroup(null);
146 assertFalse(data.isSameContent(new UpdateReq(reqParams, MY_REQ_NAME, msg2)));
148 msg2.setPdpGroup(DIFFERENT);
149 assertFalse(data.isSameContent(new UpdateReq(reqParams, MY_REQ_NAME, msg2)));
151 update.setPdpGroup(null);
152 assertFalse(data.isSameContent(new UpdateReq(reqParams, MY_REQ_NAME, msg2)));
156 public void isSameContent_DiffSubGroup() {
157 PdpUpdate msg2 = new PdpUpdate(update);
158 msg2.setPdpSubgroup(null);
159 assertFalse(data.isSameContent(new UpdateReq(reqParams, MY_REQ_NAME, msg2)));
161 msg2.setPdpSubgroup(DIFFERENT);
162 assertFalse(data.isSameContent(new UpdateReq(reqParams, MY_REQ_NAME, msg2)));
164 update.setPdpSubgroup(null);
165 assertFalse(data.isSameContent(new UpdateReq(reqParams, MY_REQ_NAME, msg2)));
169 public void isSameContent_DiffPolicies() {
170 PdpUpdate msg2 = new PdpUpdate(update);
172 ArrayList<ToscaPolicy> policies = new ArrayList<>(update.getPolicies());
173 policies.set(0, makePolicy(DIFFERENT, "10.0.0"));
174 msg2.setPolicies(policies);
176 assertFalse(data.isSameContent(new UpdateReq(reqParams, MY_REQ_NAME, msg2)));
180 public void testGetPriority() {
181 assertTrue(data.getPriority() > new StateChangeReq(reqParams, MY_REQ_NAME, new PdpStateChange()).getPriority());
185 * Makes an update message.
187 * @return a new update message
189 private PdpUpdate makeUpdate() {
190 PdpUpdate upd = new PdpUpdate();
192 upd.setDescription("update-description");
193 upd.setName(MY_NAME);
194 upd.setPdpGroup(MY_GROUP);
195 upd.setPdpSubgroup(MY_SUBGROUP);
197 ToscaPolicy policy1 = makePolicy("policy-1-a", "1.0.0");
198 ToscaPolicy policy2 = makePolicy("policy-2-a", "1.1.0");
200 upd.setPolicies(Arrays.asList(policy1, policy2));