2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.policy.pap.main.comm;
23 import static org.mockito.Matchers.any;
24 import static org.mockito.Mockito.doAnswer;
25 import static org.mockito.Mockito.mock;
26 import static org.mockito.Mockito.verify;
27 import static org.mockito.Mockito.when;
29 import java.util.Collections;
30 import java.util.LinkedList;
31 import java.util.Queue;
32 import java.util.function.Consumer;
33 import org.junit.Before;
34 import org.mockito.ArgumentCaptor;
35 import org.mockito.invocation.InvocationOnMock;
36 import org.mockito.stubbing.Answer;
37 import org.onap.policy.common.endpoints.event.comm.Topic.CommInfrastructure;
38 import org.onap.policy.common.endpoints.listeners.RequestIdDispatcher;
39 import org.onap.policy.common.endpoints.listeners.TypedMessageListener;
40 import org.onap.policy.models.pdp.concepts.PdpMessage;
41 import org.onap.policy.models.pdp.concepts.PdpStateChange;
42 import org.onap.policy.models.pdp.concepts.PdpStatus;
43 import org.onap.policy.models.pdp.concepts.PdpUpdate;
44 import org.onap.policy.models.pdp.enums.PdpState;
45 import org.onap.policy.models.provider.PolicyModelsProvider;
46 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
47 import org.onap.policy.pap.main.PapConstants;
48 import org.onap.policy.pap.main.PolicyModelsProviderFactoryWrapper;
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 MY_REQ_NAME = "my-request";
65 protected static final String DIFFERENT = "different-value";
66 protected static final String MY_GROUP = "my-group";
67 protected static final String MY_SUBGROUP = "my-subgroup";
68 protected static final String MY_NAME = "my-name";
69 protected static final PdpState MY_STATE = PdpState.SAFE;
70 protected static final PdpState DIFF_STATE = PdpState.TERMINATED;
71 protected static final int RETRIES = 1;
73 protected Publisher<PdpMessage> publisher;
74 protected PolicyNotifier notifier;
75 protected RequestIdDispatcher<PdpStatus> dispatcher;
76 protected Object lock;
77 protected TimerManager timers;
78 protected TimerManager.Timer timer;
79 protected Queue<QueueToken<PdpMessage>> queue;
80 protected RequestListener listener;
81 protected PolicyModelsProviderFactoryWrapper daoFactory;
82 protected PolicyModelsProvider dao;
83 protected RequestParams reqParams;
84 protected PdpModifyRequestMapParams mapParams;
89 * @throws Exception if an error occurs
92 @SuppressWarnings("unchecked")
93 public void setUp() throws Exception {
94 publisher = mock(Publisher.class);
95 notifier = mock(PolicyNotifier.class);
96 dispatcher = mock(RequestIdDispatcher.class);
98 timers = mock(TimerManager.class);
99 timer = mock(TimerManager.Timer.class);
100 queue = new LinkedList<>();
101 listener = mock(RequestListener.class);
102 daoFactory = mock(PolicyModelsProviderFactoryWrapper.class);
103 dao = mock(PolicyModelsProvider.class);
105 PdpParameters pdpParams = mock(PdpParameters.class);
107 doAnswer(new Answer<Object>() {
109 public Object answer(InvocationOnMock invocation) throws Throwable {
110 queue.add(invocation.getArgumentAt(0, QueueToken.class));
113 }).when(publisher).enqueue(any());
115 when(timers.register(any(), any())).thenReturn(timer);
117 when(daoFactory.create()).thenReturn(dao);
119 PdpStateChangeParameters stateParams = mock(PdpStateChangeParameters.class);
120 when(stateParams.getMaxRetryCount()).thenReturn(RETRIES);
121 when(pdpParams.getStateChangeParameters()).thenReturn(stateParams);
123 PdpUpdateParameters updateParams = mock(PdpUpdateParameters.class);
124 when(updateParams.getMaxRetryCount()).thenReturn(RETRIES);
125 when(pdpParams.getUpdateParameters()).thenReturn(updateParams);
127 reqParams = new RequestParams().setMaxRetryCount(RETRIES).setModifyLock(lock).setPdpPublisher(publisher)
128 .setResponseDispatcher(dispatcher).setTimers(timers);
130 mapParams = new PdpModifyRequestMapParams().setModifyLock(lock).setPdpPublisher(publisher)
131 .setPolicyNotifier(notifier).setResponseDispatcher(dispatcher).setDaoFactory(daoFactory)
132 .setUpdateTimers(timers).setStateChangeTimers(timers).setParams(pdpParams);
136 * Gets the listener that was registered with the dispatcher and invokes it.
138 * @param response the response to pass to the listener
140 @SuppressWarnings("unchecked")
141 protected void invokeProcessResponse(PdpStatus response) {
142 @SuppressWarnings("rawtypes")
143 ArgumentCaptor<TypedMessageListener> processResp = ArgumentCaptor.forClass(TypedMessageListener.class);
145 verify(dispatcher).register(any(), processResp.capture());
147 processResp.getValue().onTopicEvent(CommInfrastructure.NOOP, PapConstants.TOPIC_POLICY_PDP_PAP, response);
151 * Gets the timeout handler that was registered with the timer manager and invokes it.
153 @SuppressWarnings("unchecked")
154 protected void invokeTimeoutHandler() {
155 @SuppressWarnings("rawtypes")
156 ArgumentCaptor<Consumer> timeoutHdlr = ArgumentCaptor.forClass(Consumer.class);
158 verify(timers).register(any(), timeoutHdlr.capture());
160 timeoutHdlr.getValue().accept(PDP1);
164 * Creates a policy with the given name and version.
166 * @param name policy name
167 * @param version policy version
168 * @return a new policy
170 protected ToscaPolicy makePolicy(String name, String version) {
171 ToscaPolicy policy = new ToscaPolicy();
173 policy.setName(name);
174 policy.setVersion(version);
180 * Makes an update request with a new message.
182 * @param pdpName PDP name
183 * @param group group name
184 * @param subgroup subgroup name
185 * @return a new update request
187 protected UpdateReq makeUpdateReq(String pdpName, String group, String subgroup) {
188 UpdateReq req = mock(UpdateReq.class);
190 when(req.getName()).thenReturn(MY_REQ_NAME);
191 when(req.getPriority()).thenReturn(1);
192 when(req.getMessage()).thenReturn(makeUpdate(pdpName, group, subgroup));
198 * Makes an update message.
200 * @param pdpName PDP name
201 * @param group group name
202 * @param subgroup subgroup name
203 * @return a new update message
205 protected PdpUpdate makeUpdate(String pdpName, String group, String subgroup) {
206 PdpUpdate message = new PdpUpdate();
208 message.setName(pdpName);
209 message.setPolicies(Collections.emptyList());
210 message.setPdpGroup(group);
211 message.setPdpSubgroup(subgroup);
217 * Makes a state-change request with a new message.
219 * @param pdpName PDP name
220 * @param state desired PDP state
221 * @return a new state-change request
223 protected StateChangeReq makeStateChangeReq(String pdpName, PdpState state) {
224 StateChangeReq req = mock(StateChangeReq.class);
226 when(req.getName()).thenReturn(MY_REQ_NAME);
227 when(req.getPriority()).thenReturn(0);
228 when(req.getMessage()).thenReturn(makeStateChange(pdpName, state));
234 * Makes a state-change message.
236 * @param pdpName PDP name
237 * @param state desired PDP state
238 * @return a new state-change message
240 protected PdpStateChange makeStateChange(String pdpName, PdpState state) {
241 PdpStateChange message = new PdpStateChange();
243 message.setName(pdpName);
244 message.setState(state);