2 * ============LICENSE_START=======================================================
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
23 package org.onap.policy.pap.main.comm;
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;
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;
60 * Common base class for request tests.
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;
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;
92 * @throws Exception if an error occurs
95 @SuppressWarnings("unchecked")
96 public void setUp() throws Exception {
97 publisher = mock(Publisher.class);
98 notifier = mock(PolicyNotifier.class);
99 dispatcher = mock(RequestIdDispatcher.class);
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);
107 doAnswer(new Answer<Object>() {
109 public Object answer(InvocationOnMock invocation) throws Throwable {
110 queue.add(invocation.getArgument(0, QueueToken.class));
113 }).when(publisher).enqueue(any());
115 when(timers.register(any(), any())).thenReturn(timer);
117 PdpStateChangeParameters stateParams = mock(PdpStateChangeParameters.class);
118 when(stateParams.getMaxRetryCount()).thenReturn(RETRIES);
119 when(pdpParams.getStateChangeParameters()).thenReturn(stateParams);
121 PdpUpdateParameters updateParams = mock(PdpUpdateParameters.class);
122 when(updateParams.getMaxRetryCount()).thenReturn(RETRIES);
123 when(pdpParams.getUpdateParameters()).thenReturn(updateParams);
125 reqParams = new RequestParams().setMaxRetryCount(RETRIES).setModifyLock(lock).setPdpPublisher(publisher)
126 .setResponseDispatcher(dispatcher).setTimers(timers);
128 mapParams = PdpModifyRequestMapParams.builder().modifyLock(lock).pdpPublisher(publisher)
129 .responseDispatcher(dispatcher)
130 .updateTimers(timers).stateChangeTimers(timers).params(pdpParams)
131 .maxPdpAgeMs(100).build();
135 * Gets the listener that was registered with the dispatcher and invokes it.
137 * @param response the response to pass to the listener
139 @SuppressWarnings("unchecked")
140 protected void invokeProcessResponse(PdpStatus response) {
141 @SuppressWarnings("rawtypes")
142 ArgumentCaptor<TypedMessageListener> processResp = ArgumentCaptor.forClass(TypedMessageListener.class);
144 verify(dispatcher).register(any(), processResp.capture());
146 processResp.getValue().onTopicEvent(CommInfrastructure.NOOP, PapConstants.TOPIC_POLICY_PDP_PAP, response);
150 * Gets the timeout handler that was registered with the timer manager and invokes it.
152 @SuppressWarnings("unchecked")
153 protected void invokeTimeoutHandler() {
154 @SuppressWarnings("rawtypes")
155 ArgumentCaptor<Consumer> timeoutHdlr = ArgumentCaptor.forClass(Consumer.class);
157 verify(timers).register(any(), timeoutHdlr.capture());
159 timeoutHdlr.getValue().accept(PDP1);
163 * Creates a policy with the given name and version.
165 * @param name policy name
166 * @param version policy version
167 * @return a new policy
169 protected ToscaPolicy makePolicy(String name, String version) {
170 ToscaPolicy policy = new ToscaPolicy();
172 policy.setName(name);
173 policy.setVersion(version);
179 * Makes an update request with a new message.
181 * @param pdpName PDP name
182 * @param group group name
183 * @param subgroup subgroup name
184 * @return a new update request
186 protected UpdateReq makeUpdateReq(String pdpName, String group, String subgroup) {
187 UpdateReq req = mock(UpdateReq.class);
189 when(req.getName()).thenReturn(MY_REQ_NAME);
190 when(req.getMessage()).thenReturn(makeUpdate(pdpName, group, subgroup));
196 * Makes an update message.
198 * @param pdpName PDP name
199 * @param group group name
200 * @param subgroup subgroup name
201 * @return a new update message
203 protected PdpUpdate makeUpdate(String pdpName, String group, String subgroup) {
204 PdpUpdate message = new PdpUpdate();
206 message.setName(pdpName);
207 message.setPoliciesToBeDeployed(Collections.emptyList());
208 message.setPoliciesToBeUndeployed(Collections.emptyList());
209 message.setPdpGroup(group);
210 message.setPdpSubgroup(subgroup);
216 * Makes a state-change request with a new message.
218 * @param pdpName PDP name
219 * @param state desired PDP state
220 * @return a new state-change request
222 protected StateChangeReq makeStateChangeReq(String pdpName, PdpState state) {
223 StateChangeReq req = mock(StateChangeReq.class);
225 when(req.getName()).thenReturn(MY_REQ_NAME);
226 when(req.getMessage()).thenReturn(makeStateChange(pdpName, state));
232 * Makes a state-change message.
234 * @param pdpName PDP name
235 * @param state desired PDP state
236 * @return a new state-change message
238 protected PdpStateChange makeStateChange(String pdpName, PdpState state) {
239 PdpStateChange message = new PdpStateChange();
241 message.setName(pdpName);
242 message.setState(state);