dd206062d30b31217697c71a7d37bfe379f3eea8
[policy/pap.git] / main / src / test / java / org / onap / policy / pap / main / comm / CommonRequestBase.java
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP PAP
4  * ================================================================================
5  * Copyright (C) 2019-2021 AT&T Intellectual Property. All rights reserved.
6  * Modifications Copyright (C) 2021 Nordix Foundation.
7  * Modifications Copyright (C) 2022 Bell Canada. All rights reserved.
8  * ================================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.policy.pap.main.comm;
24
25 import static org.mockito.ArgumentMatchers.any;
26 import static org.mockito.Mockito.doAnswer;
27 import static org.mockito.Mockito.mock;
28 import static org.mockito.Mockito.verify;
29 import static org.mockito.Mockito.when;
30
31 import java.util.Collections;
32 import java.util.LinkedList;
33 import java.util.Queue;
34 import java.util.function.Consumer;
35 import org.junit.Before;
36 import org.mockito.ArgumentCaptor;
37 import org.mockito.invocation.InvocationOnMock;
38 import org.mockito.stubbing.Answer;
39 import org.onap.policy.common.endpoints.event.comm.Topic.CommInfrastructure;
40 import org.onap.policy.common.endpoints.listeners.RequestIdDispatcher;
41 import org.onap.policy.common.endpoints.listeners.TypedMessageListener;
42 import org.onap.policy.models.pdp.concepts.PdpMessage;
43 import org.onap.policy.models.pdp.concepts.PdpStateChange;
44 import org.onap.policy.models.pdp.concepts.PdpStatus;
45 import org.onap.policy.models.pdp.concepts.PdpUpdate;
46 import org.onap.policy.models.pdp.enums.PdpState;
47 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
48 import org.onap.policy.pap.main.PapConstants;
49 import org.onap.policy.pap.main.comm.msgdata.RequestListener;
50 import org.onap.policy.pap.main.comm.msgdata.StateChangeReq;
51 import org.onap.policy.pap.main.comm.msgdata.UpdateReq;
52 import org.onap.policy.pap.main.notification.PolicyNotifier;
53 import org.onap.policy.pap.main.parameters.PdpModifyRequestMapParams;
54 import org.onap.policy.pap.main.parameters.PdpParameters;
55 import org.onap.policy.pap.main.parameters.PdpStateChangeParameters;
56 import org.onap.policy.pap.main.parameters.PdpUpdateParameters;
57 import org.onap.policy.pap.main.parameters.RequestParams;
58
59 /**
60  * Common base class for request tests.
61  */
62 public class CommonRequestBase {
63     protected static final String PDP1 = "pdp_1";
64     protected static final String PDP2 = "pdp_2";
65     protected static final String PDP3 = "pdp_3";
66     protected static final String PDP4 = "pdp_4";
67     protected static final String MY_REQ_NAME = "my-request";
68     protected static final String DIFFERENT = "different-value";
69     protected static final String MY_GROUP = "my-group";
70     protected static final String MY_GROUP2 = "my-group-2";
71     protected static final String MY_SUBGROUP = "my-subgroup";
72     protected static final String MY_SUBGROUP2 = "my-subgroup-2";
73     protected static final String MY_NAME = "my-name";
74     protected static final PdpState MY_STATE = PdpState.SAFE;
75     protected static final PdpState DIFF_STATE = PdpState.TERMINATED;
76     protected static final int RETRIES = 1;
77
78     protected Publisher<PdpMessage> publisher;
79     protected PolicyNotifier notifier;
80     protected RequestIdDispatcher<PdpStatus> dispatcher;
81     protected Object lock;
82     protected TimerManager timers;
83     protected TimerManager.Timer timer;
84     protected Queue<QueueToken<PdpMessage>> queue;
85     protected RequestListener listener;
86     protected RequestParams reqParams;
87     protected PdpModifyRequestMapParams mapParams;
88
89     /**
90      * Sets up.
91      *
92      * @throws Exception if an error occurs
93      */
94     @Before
95     @SuppressWarnings("unchecked")
96     public void setUp() throws Exception {
97         publisher = mock(Publisher.class);
98         notifier = mock(PolicyNotifier.class);
99         dispatcher = mock(RequestIdDispatcher.class);
100         lock = new Object();
101         timers = mock(TimerManager.class);
102         timer = mock(TimerManager.Timer.class);
103         queue = new LinkedList<>();
104         listener = mock(RequestListener.class);
105         PdpParameters pdpParams = mock(PdpParameters.class);
106
107         doAnswer(new Answer<Object>() {
108             @Override
109             public Object answer(InvocationOnMock invocation) throws Throwable {
110                 queue.add(invocation.getArgument(0, QueueToken.class));
111                 return null;
112             }
113         }).when(publisher).enqueue(any());
114
115         when(timers.register(any(), any())).thenReturn(timer);
116
117         PdpStateChangeParameters stateParams = mock(PdpStateChangeParameters.class);
118         when(stateParams.getMaxRetryCount()).thenReturn(RETRIES);
119         when(pdpParams.getStateChangeParameters()).thenReturn(stateParams);
120
121         PdpUpdateParameters updateParams = mock(PdpUpdateParameters.class);
122         when(updateParams.getMaxRetryCount()).thenReturn(RETRIES);
123         when(pdpParams.getUpdateParameters()).thenReturn(updateParams);
124
125         reqParams = new RequestParams().setMaxRetryCount(RETRIES).setModifyLock(lock).setPdpPublisher(publisher)
126                         .setResponseDispatcher(dispatcher).setTimers(timers);
127
128         mapParams = PdpModifyRequestMapParams.builder().modifyLock(lock).pdpPublisher(publisher)
129                         .responseDispatcher(dispatcher)
130                         .updateTimers(timers).stateChangeTimers(timers).params(pdpParams)
131                         .maxPdpAgeMs(100).build();
132     }
133
134     /**
135      * Gets the listener that was registered with the dispatcher and invokes it.
136      *
137      * @param response the response to pass to the listener
138      */
139     @SuppressWarnings("unchecked")
140     protected void invokeProcessResponse(PdpStatus response) {
141         @SuppressWarnings("rawtypes")
142         ArgumentCaptor<TypedMessageListener> processResp = ArgumentCaptor.forClass(TypedMessageListener.class);
143
144         verify(dispatcher).register(any(), processResp.capture());
145
146         processResp.getValue().onTopicEvent(CommInfrastructure.NOOP, PapConstants.TOPIC_POLICY_PDP_PAP, response);
147     }
148
149     /**
150      * Gets the timeout handler that was registered with the timer manager and invokes it.
151      */
152     @SuppressWarnings("unchecked")
153     protected void invokeTimeoutHandler() {
154         @SuppressWarnings("rawtypes")
155         ArgumentCaptor<Consumer> timeoutHdlr = ArgumentCaptor.forClass(Consumer.class);
156
157         verify(timers).register(any(), timeoutHdlr.capture());
158
159         timeoutHdlr.getValue().accept(PDP1);
160     }
161
162     /**
163      * Creates a policy with the given name and version.
164      *
165      * @param name policy name
166      * @param version policy version
167      * @return a new policy
168      */
169     protected ToscaPolicy makePolicy(String name, String version) {
170         ToscaPolicy policy = new ToscaPolicy();
171
172         policy.setName(name);
173         policy.setVersion(version);
174
175         return policy;
176     }
177
178     /**
179      * Makes an update request with a new message.
180      *
181      * @param pdpName PDP name
182      * @param group group name
183      * @param subgroup subgroup name
184      * @return a new update request
185      */
186     protected UpdateReq makeUpdateReq(String pdpName, String group, String subgroup) {
187         UpdateReq req = mock(UpdateReq.class);
188
189         when(req.getName()).thenReturn(MY_REQ_NAME);
190         when(req.getMessage()).thenReturn(makeUpdate(pdpName, group, subgroup));
191
192         return req;
193     }
194
195     /**
196      * Makes an update message.
197      *
198      * @param pdpName PDP name
199      * @param group group name
200      * @param subgroup subgroup name
201      * @return a new update message
202      */
203     protected PdpUpdate makeUpdate(String pdpName, String group, String subgroup) {
204         PdpUpdate message = new PdpUpdate();
205
206         message.setName(pdpName);
207         message.setPoliciesToBeDeployed(Collections.emptyList());
208         message.setPoliciesToBeUndeployed(Collections.emptyList());
209         message.setPdpGroup(group);
210         message.setPdpSubgroup(subgroup);
211
212         return message;
213     }
214
215     /**
216      * Makes a state-change request with a new message.
217      *
218      * @param pdpName PDP name
219      * @param state desired PDP state
220      * @return a new state-change request
221      */
222     protected StateChangeReq makeStateChangeReq(String pdpName, PdpState state) {
223         StateChangeReq req = mock(StateChangeReq.class);
224
225         when(req.getName()).thenReturn(MY_REQ_NAME);
226         when(req.getMessage()).thenReturn(makeStateChange(pdpName, state));
227
228         return req;
229     }
230
231     /**
232      * Makes a state-change message.
233      *
234      * @param pdpName PDP name
235      * @param state desired PDP state
236      * @return a new state-change message
237      */
238     protected PdpStateChange makeStateChange(String pdpName, PdpState state) {
239         PdpStateChange message = new PdpStateChange();
240
241         message.setName(pdpName);
242         message.setState(state);
243
244         return message;
245     }
246 }