2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019-2021 AT&T Intellectual Property. All rights reserved.
6 * Modifications Copyright (C) 2021-2022 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.stubbing.Answer;
40 import org.onap.policy.common.endpoints.event.comm.Topic.CommInfrastructure;
41 import org.onap.policy.common.endpoints.listeners.RequestIdDispatcher;
42 import org.onap.policy.common.endpoints.listeners.TypedMessageListener;
43 import org.onap.policy.common.utils.services.Registry;
44 import org.onap.policy.models.pdp.concepts.PdpMessage;
45 import org.onap.policy.models.pdp.concepts.PdpStateChange;
46 import org.onap.policy.models.pdp.concepts.PdpStatus;
47 import org.onap.policy.models.pdp.concepts.PdpUpdate;
48 import org.onap.policy.models.pdp.enums.PdpState;
49 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
50 import org.onap.policy.pap.main.PapConstants;
51 import org.onap.policy.pap.main.comm.msgdata.RequestListener;
52 import org.onap.policy.pap.main.comm.msgdata.StateChangeReq;
53 import org.onap.policy.pap.main.comm.msgdata.UpdateReq;
54 import org.onap.policy.pap.main.notification.PolicyNotifier;
55 import org.onap.policy.pap.main.parameters.PdpModifyRequestMapParams;
56 import org.onap.policy.pap.main.parameters.PdpParameters;
57 import org.onap.policy.pap.main.parameters.PdpStateChangeParameters;
58 import org.onap.policy.pap.main.parameters.PdpUpdateParameters;
59 import org.onap.policy.pap.main.parameters.RequestParams;
62 * Common base class for request tests.
64 public class CommonRequestBase {
65 protected static final String PDP1 = "pdp_1";
66 protected static final String PDP2 = "pdp_2";
67 protected static final String PDP3 = "pdp_3";
68 protected static final String PDP4 = "pdp_4";
69 protected static final String MY_REQ_NAME = "my-request";
70 protected static final String DIFFERENT = "different-value";
71 protected static final String MY_GROUP = "my-group";
72 protected static final String MY_GROUP2 = "my-group-2";
73 protected static final String MY_SUBGROUP = "my-subgroup";
74 protected static final String MY_SUBGROUP2 = "my-subgroup-2";
75 protected static final String MY_NAME = "my-name";
76 protected static final PdpState MY_STATE = PdpState.SAFE;
77 protected static final PdpState DIFF_STATE = PdpState.TERMINATED;
78 protected static final int RETRIES = 1;
79 protected static final String PDP_PAP_TOPIC = "POLICY-PDP-PAP";
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((Answer<Object>) invocation -> {
116 queue.add(invocation.getArgument(0, QueueToken.class));
118 }).when(publisher).enqueue(any());
120 when(timers.register(any(), any())).thenReturn(timer);
122 PdpStateChangeParameters stateParams = mock(PdpStateChangeParameters.class);
123 when(stateParams.getMaxRetryCount()).thenReturn(RETRIES);
124 when(pdpParams.getStateChangeParameters()).thenReturn(stateParams);
126 PdpUpdateParameters updateParams = mock(PdpUpdateParameters.class);
127 when(updateParams.getMaxRetryCount()).thenReturn(RETRIES);
128 when(pdpParams.getUpdateParameters()).thenReturn(updateParams);
130 reqParams = new RequestParams().setMaxRetryCount(RETRIES).setModifyLock(lock).setPdpPublisher(publisher)
131 .setResponseDispatcher(dispatcher).setTimers(timers);
133 mapParams = PdpModifyRequestMapParams.builder().modifyLock(lock).pdpPublisher(publisher)
134 .responseDispatcher(dispatcher)
135 .updateTimers(timers).stateChangeTimers(timers).params(pdpParams)
136 .maxPdpAgeMs(100).build();
140 * Gets the listener that was registered with the dispatcher and invokes it.
142 * @param response the response to pass to the listener
144 @SuppressWarnings("unchecked")
145 protected void invokeProcessResponse(PdpStatus response) {
146 @SuppressWarnings("rawtypes")
147 ArgumentCaptor<TypedMessageListener> processResp = ArgumentCaptor.forClass(TypedMessageListener.class);
149 verify(dispatcher).register(any(), processResp.capture());
151 processResp.getValue().onTopicEvent(CommInfrastructure.NOOP, PDP_PAP_TOPIC, response);
155 * Gets the timeout handler that was registered with the timer manager and invokes it.
157 @SuppressWarnings("unchecked")
158 protected void invokeTimeoutHandler() {
159 @SuppressWarnings("rawtypes")
160 ArgumentCaptor<Consumer> timeoutHdlr = ArgumentCaptor.forClass(Consumer.class);
162 verify(timers).register(any(), timeoutHdlr.capture());
164 timeoutHdlr.getValue().accept(PDP1);
168 * Creates a policy with the given name and version.
170 * @param name policy name
171 * @param version policy version
172 * @return a new policy
174 protected ToscaPolicy makePolicy(String name, String version) {
175 ToscaPolicy policy = new ToscaPolicy();
177 policy.setName(name);
178 policy.setVersion(version);
184 * Makes an update request with a new message.
186 * @param pdpName PDP name
187 * @param group group name
188 * @param subgroup subgroup name
189 * @return a new update request
191 protected UpdateReq makeUpdateReq(String pdpName, String group, String subgroup) {
192 UpdateReq req = mock(UpdateReq.class);
194 when(req.getName()).thenReturn(MY_REQ_NAME);
195 when(req.getMessage()).thenReturn(makeUpdate(pdpName, group, subgroup));
201 * Makes an update message.
203 * @param pdpName PDP name
204 * @param group group name
205 * @param subgroup subgroup name
206 * @return a new update message
208 protected PdpUpdate makeUpdate(String pdpName, String group, String subgroup) {
209 PdpUpdate message = new PdpUpdate();
211 message.setName(pdpName);
212 message.setPoliciesToBeDeployed(Collections.emptyList());
213 message.setPoliciesToBeUndeployed(Collections.emptyList());
214 message.setPdpGroup(group);
215 message.setPdpSubgroup(subgroup);
221 * Makes a state-change request with a new message.
223 * @param pdpName PDP name
224 * @param state desired PDP state
225 * @return a new state-change request
227 protected StateChangeReq makeStateChangeReq(String pdpName, PdpState state) {
228 StateChangeReq req = mock(StateChangeReq.class);
230 when(req.getName()).thenReturn(MY_REQ_NAME);
231 when(req.getMessage()).thenReturn(makeStateChange(pdpName, state));
237 * Makes a state-change message.
239 * @param pdpName PDP name
240 * @param state desired PDP state
241 * @return a new state-change message
243 protected PdpStateChange makeStateChange(String pdpName, PdpState state) {
244 PdpStateChange message = new PdpStateChange();
246 message.setName(pdpName);
247 message.setState(state);