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 io.micrometer.core.instrument.simple.SimpleMeterRegistry;
32 import java.util.Collections;
33 import java.util.LinkedList;
34 import java.util.Queue;
35 import java.util.function.Consumer;
36 import org.junit.Before;
37 import org.junit.BeforeClass;
38 import org.mockito.ArgumentCaptor;
39 import org.mockito.invocation.InvocationOnMock;
40 import org.mockito.stubbing.Answer;
41 import org.onap.policy.common.endpoints.event.comm.Topic.CommInfrastructure;
42 import org.onap.policy.common.endpoints.listeners.RequestIdDispatcher;
43 import org.onap.policy.common.endpoints.listeners.TypedMessageListener;
44 import org.onap.policy.common.utils.services.Registry;
45 import org.onap.policy.models.pdp.concepts.PdpMessage;
46 import org.onap.policy.models.pdp.concepts.PdpStateChange;
47 import org.onap.policy.models.pdp.concepts.PdpStatus;
48 import org.onap.policy.models.pdp.concepts.PdpUpdate;
49 import org.onap.policy.models.pdp.enums.PdpState;
50 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
51 import org.onap.policy.pap.main.PapConstants;
52 import org.onap.policy.pap.main.comm.msgdata.RequestListener;
53 import org.onap.policy.pap.main.comm.msgdata.StateChangeReq;
54 import org.onap.policy.pap.main.comm.msgdata.UpdateReq;
55 import org.onap.policy.pap.main.notification.PolicyNotifier;
56 import org.onap.policy.pap.main.parameters.PdpModifyRequestMapParams;
57 import org.onap.policy.pap.main.parameters.PdpParameters;
58 import org.onap.policy.pap.main.parameters.PdpStateChangeParameters;
59 import org.onap.policy.pap.main.parameters.PdpUpdateParameters;
60 import org.onap.policy.pap.main.parameters.RequestParams;
63 * Common base class for request tests.
65 public class CommonRequestBase {
66 protected static final String PDP1 = "pdp_1";
67 protected static final String PDP2 = "pdp_2";
68 protected static final String PDP3 = "pdp_3";
69 protected static final String PDP4 = "pdp_4";
70 protected static final String MY_REQ_NAME = "my-request";
71 protected static final String DIFFERENT = "different-value";
72 protected static final String MY_GROUP = "my-group";
73 protected static final String MY_GROUP2 = "my-group-2";
74 protected static final String MY_SUBGROUP = "my-subgroup";
75 protected static final String MY_SUBGROUP2 = "my-subgroup-2";
76 protected static final String MY_NAME = "my-name";
77 protected static final PdpState MY_STATE = PdpState.SAFE;
78 protected static final PdpState DIFF_STATE = PdpState.TERMINATED;
79 protected static final int RETRIES = 1;
81 protected Publisher<PdpMessage> publisher;
82 protected PolicyNotifier notifier;
83 protected RequestIdDispatcher<PdpStatus> dispatcher;
84 protected Object lock;
85 protected TimerManager timers;
86 protected TimerManager.Timer timer;
87 protected Queue<QueueToken<PdpMessage>> queue;
88 protected RequestListener listener;
89 protected RequestParams reqParams;
90 protected PdpModifyRequestMapParams mapParams;
93 public static void setupBeforeAll() {
94 Registry.registerOrReplace(PapConstants.REG_METER_REGISTRY, new SimpleMeterRegistry());
100 * @throws Exception if an error occurs
103 @SuppressWarnings("unchecked")
104 public void setUp() throws Exception {
105 publisher = mock(Publisher.class);
106 notifier = mock(PolicyNotifier.class);
107 dispatcher = mock(RequestIdDispatcher.class);
109 timers = mock(TimerManager.class);
110 timer = mock(TimerManager.Timer.class);
111 queue = new LinkedList<>();
112 listener = mock(RequestListener.class);
113 PdpParameters pdpParams = mock(PdpParameters.class);
115 doAnswer(new Answer<Object>() {
117 public Object answer(InvocationOnMock invocation) throws Throwable {
118 queue.add(invocation.getArgument(0, QueueToken.class));
121 }).when(publisher).enqueue(any());
123 when(timers.register(any(), any())).thenReturn(timer);
125 PdpStateChangeParameters stateParams = mock(PdpStateChangeParameters.class);
126 when(stateParams.getMaxRetryCount()).thenReturn(RETRIES);
127 when(pdpParams.getStateChangeParameters()).thenReturn(stateParams);
129 PdpUpdateParameters updateParams = mock(PdpUpdateParameters.class);
130 when(updateParams.getMaxRetryCount()).thenReturn(RETRIES);
131 when(pdpParams.getUpdateParameters()).thenReturn(updateParams);
133 reqParams = new RequestParams().setMaxRetryCount(RETRIES).setModifyLock(lock).setPdpPublisher(publisher)
134 .setResponseDispatcher(dispatcher).setTimers(timers);
136 mapParams = PdpModifyRequestMapParams.builder().modifyLock(lock).pdpPublisher(publisher)
137 .responseDispatcher(dispatcher)
138 .updateTimers(timers).stateChangeTimers(timers).params(pdpParams)
139 .maxPdpAgeMs(100).build();
143 * Gets the listener that was registered with the dispatcher and invokes it.
145 * @param response the response to pass to the listener
147 @SuppressWarnings("unchecked")
148 protected void invokeProcessResponse(PdpStatus response) {
149 @SuppressWarnings("rawtypes")
150 ArgumentCaptor<TypedMessageListener> processResp = ArgumentCaptor.forClass(TypedMessageListener.class);
152 verify(dispatcher).register(any(), processResp.capture());
154 processResp.getValue().onTopicEvent(CommInfrastructure.NOOP, PapConstants.TOPIC_POLICY_PDP_PAP, response);
158 * Gets the timeout handler that was registered with the timer manager and invokes it.
160 @SuppressWarnings("unchecked")
161 protected void invokeTimeoutHandler() {
162 @SuppressWarnings("rawtypes")
163 ArgumentCaptor<Consumer> timeoutHdlr = ArgumentCaptor.forClass(Consumer.class);
165 verify(timers).register(any(), timeoutHdlr.capture());
167 timeoutHdlr.getValue().accept(PDP1);
171 * Creates a policy with the given name and version.
173 * @param name policy name
174 * @param version policy version
175 * @return a new policy
177 protected ToscaPolicy makePolicy(String name, String version) {
178 ToscaPolicy policy = new ToscaPolicy();
180 policy.setName(name);
181 policy.setVersion(version);
187 * Makes an update request with a new message.
189 * @param pdpName PDP name
190 * @param group group name
191 * @param subgroup subgroup name
192 * @return a new update request
194 protected UpdateReq makeUpdateReq(String pdpName, String group, String subgroup) {
195 UpdateReq req = mock(UpdateReq.class);
197 when(req.getName()).thenReturn(MY_REQ_NAME);
198 when(req.getMessage()).thenReturn(makeUpdate(pdpName, group, subgroup));
204 * Makes an update message.
206 * @param pdpName PDP name
207 * @param group group name
208 * @param subgroup subgroup name
209 * @return a new update message
211 protected PdpUpdate makeUpdate(String pdpName, String group, String subgroup) {
212 PdpUpdate message = new PdpUpdate();
214 message.setName(pdpName);
215 message.setPoliciesToBeDeployed(Collections.emptyList());
216 message.setPoliciesToBeUndeployed(Collections.emptyList());
217 message.setPdpGroup(group);
218 message.setPdpSubgroup(subgroup);
224 * Makes a state-change request with a new message.
226 * @param pdpName PDP name
227 * @param state desired PDP state
228 * @return a new state-change request
230 protected StateChangeReq makeStateChangeReq(String pdpName, PdpState state) {
231 StateChangeReq req = mock(StateChangeReq.class);
233 when(req.getName()).thenReturn(MY_REQ_NAME);
234 when(req.getMessage()).thenReturn(makeStateChange(pdpName, state));
240 * Makes a state-change message.
242 * @param pdpName PDP name
243 * @param state desired PDP state
244 * @return a new state-change message
246 protected PdpStateChange makeStateChange(String pdpName, PdpState state) {
247 PdpStateChange message = new PdpStateChange();
249 message.setName(pdpName);
250 message.setState(state);