Merge "Refactor request map"
[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
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;
39
40 public class UpdateReqTest extends CommonRequestBase {
41
42     private UpdateReq data;
43     private PdpUpdate update;
44     private PdpStatus response;
45
46     /**
47      * Sets up.
48      * @throws Exception if an error occurs
49      */
50     @Before
51     public void setUp() throws Exception {
52         super.setUp();
53
54         response = new PdpStatus();
55
56         update = makeUpdate();
57
58         response.setName(MY_NAME);
59         response.setPdpGroup(update.getPdpGroup());
60         response.setPdpSubgroup(update.getPdpSubgroup());
61         response.setPolicies(
62                         update.getPolicies().stream().map(ToscaPolicy::getIdentifier).collect(Collectors.toList()));
63
64         data = new UpdateReq(reqParams, MY_REQ_NAME, update);
65     }
66
67     @Test
68     public void testGetMessage() {
69         assertEquals(MY_REQ_NAME, data.getName());
70         assertSame(update, data.getMessage());
71     }
72
73     @Test
74     public void testCheckResponse() {
75         assertNull(data.checkResponse(response));
76     }
77
78     @Test
79     public void testCheckResponse_NullName() {
80         response.setName(null);
81
82         assertEquals("null PDP name", data.checkResponse(response));
83     }
84
85     @Test
86     public void testCheckResponse_NullMsgName() {
87         update.setName(null);
88
89         assertEquals(null, data.checkResponse(response));
90     }
91
92     @Test
93     public void testUpdateReqCheckResponse_MismatchedGroup() {
94         response.setPdpGroup(DIFFERENT);
95
96         assertEquals("group does not match", data.checkResponse(response));
97     }
98
99     @Test
100     public void testUpdateReqCheckResponse_MismatchedSubGroup() {
101         response.setPdpSubgroup(DIFFERENT);
102
103         assertEquals("subgroup does not match", data.checkResponse(response));
104     }
105
106     @Test
107     public void testUpdateReqCheckResponse_MismatchedPolicies() {
108         ArrayList<ToscaPolicy> policies = new ArrayList<>(update.getPolicies());
109         policies.set(0, makePolicy(DIFFERENT, "10.0.0"));
110
111         response.setPolicies(policies.stream().map(ToscaPolicy::getIdentifier).collect(Collectors.toList()));
112
113         assertEquals("policies do not match", data.checkResponse(response));
114     }
115
116     @Test
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)));
121
122         // different request type
123         assertFalse(data.isSameContent(new StateChangeReq(reqParams, MY_REQ_NAME, new PdpStateChange())));
124     }
125
126     @Test
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)));
132     }
133
134     @Test
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)));
140     }
141
142     @Test
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)));
147
148         msg2.setPdpGroup(DIFFERENT);
149         assertFalse(data.isSameContent(new UpdateReq(reqParams, MY_REQ_NAME, msg2)));
150
151         update.setPdpGroup(null);
152         assertFalse(data.isSameContent(new UpdateReq(reqParams, MY_REQ_NAME, msg2)));
153     }
154
155     @Test
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)));
160
161         msg2.setPdpSubgroup(DIFFERENT);
162         assertFalse(data.isSameContent(new UpdateReq(reqParams, MY_REQ_NAME, msg2)));
163
164         update.setPdpSubgroup(null);
165         assertFalse(data.isSameContent(new UpdateReq(reqParams, MY_REQ_NAME, msg2)));
166     }
167
168     @Test
169     public void isSameContent_DiffPolicies() {
170         PdpUpdate msg2 = new PdpUpdate(update);
171
172         ArrayList<ToscaPolicy> policies = new ArrayList<>(update.getPolicies());
173         policies.set(0, makePolicy(DIFFERENT, "10.0.0"));
174         msg2.setPolicies(policies);
175
176         assertFalse(data.isSameContent(new UpdateReq(reqParams, MY_REQ_NAME, msg2)));
177     }
178
179     @Test
180     public void testGetPriority() {
181         assertTrue(data.getPriority() > new StateChangeReq(reqParams, MY_REQ_NAME, new PdpStateChange()).getPriority());
182     }
183
184     /**
185      * Makes an update message.
186      *
187      * @return a new update message
188      */
189     private PdpUpdate makeUpdate() {
190         PdpUpdate upd = new PdpUpdate();
191
192         upd.setDescription("update-description");
193         upd.setName(MY_NAME);
194         upd.setPdpGroup(MY_GROUP);
195         upd.setPdpSubgroup(MY_SUBGROUP);
196
197         ToscaPolicy policy1 = makePolicy("policy-1-a", "1.0.0");
198         ToscaPolicy policy2 = makePolicy("policy-2-a", "1.1.0");
199
200         upd.setPolicies(Arrays.asList(policy1, policy2));
201
202         return upd;
203     }
204 }