policy/pap jdk11 upgrades
[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-2020 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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20
21 package org.onap.policy.pap.main.comm;
22
23 import static org.mockito.ArgumentMatchers.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;
28
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;
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 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;
72
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;
85
86     /**
87      * Sets up.
88      *
89      * @throws Exception if an error occurs
90      */
91     @Before
92     @SuppressWarnings("unchecked")
93     public void setUp() throws Exception {
94         publisher = mock(Publisher.class);
95         notifier = mock(PolicyNotifier.class);
96         dispatcher = mock(RequestIdDispatcher.class);
97         lock = new Object();
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);
104
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         when(daoFactory.create()).thenReturn(dao);
118
119         PdpStateChangeParameters stateParams = mock(PdpStateChangeParameters.class);
120         when(stateParams.getMaxRetryCount()).thenReturn(RETRIES);
121         when(pdpParams.getStateChangeParameters()).thenReturn(stateParams);
122
123         PdpUpdateParameters updateParams = mock(PdpUpdateParameters.class);
124         when(updateParams.getMaxRetryCount()).thenReturn(RETRIES);
125         when(pdpParams.getUpdateParameters()).thenReturn(updateParams);
126
127         reqParams = new RequestParams().setMaxRetryCount(RETRIES).setModifyLock(lock).setPdpPublisher(publisher)
128                         .setResponseDispatcher(dispatcher).setTimers(timers);
129
130         mapParams = new PdpModifyRequestMapParams().setModifyLock(lock).setPdpPublisher(publisher)
131                         .setPolicyNotifier(notifier).setResponseDispatcher(dispatcher).setDaoFactory(daoFactory)
132                         .setUpdateTimers(timers).setStateChangeTimers(timers).setParams(pdpParams);
133     }
134
135     /**
136      * Gets the listener that was registered with the dispatcher and invokes it.
137      *
138      * @param response the response to pass to the listener
139      */
140     @SuppressWarnings("unchecked")
141     protected void invokeProcessResponse(PdpStatus response) {
142         @SuppressWarnings("rawtypes")
143         ArgumentCaptor<TypedMessageListener> processResp = ArgumentCaptor.forClass(TypedMessageListener.class);
144
145         verify(dispatcher).register(any(), processResp.capture());
146
147         processResp.getValue().onTopicEvent(CommInfrastructure.NOOP, PapConstants.TOPIC_POLICY_PDP_PAP, response);
148     }
149
150     /**
151      * Gets the timeout handler that was registered with the timer manager and invokes it.
152      */
153     @SuppressWarnings("unchecked")
154     protected void invokeTimeoutHandler() {
155         @SuppressWarnings("rawtypes")
156         ArgumentCaptor<Consumer> timeoutHdlr = ArgumentCaptor.forClass(Consumer.class);
157
158         verify(timers).register(any(), timeoutHdlr.capture());
159
160         timeoutHdlr.getValue().accept(PDP1);
161     }
162
163     /**
164      * Creates a policy with the given name and version.
165      *
166      * @param name policy name
167      * @param version policy version
168      * @return a new policy
169      */
170     protected ToscaPolicy makePolicy(String name, String version) {
171         ToscaPolicy policy = new ToscaPolicy();
172
173         policy.setName(name);
174         policy.setVersion(version);
175
176         return policy;
177     }
178
179     /**
180      * Makes an update request with a new message.
181      *
182      * @param pdpName PDP name
183      * @param group group name
184      * @param subgroup subgroup name
185      * @return a new update request
186      */
187     protected UpdateReq makeUpdateReq(String pdpName, String group, String subgroup) {
188         UpdateReq req = mock(UpdateReq.class);
189
190         when(req.getName()).thenReturn(MY_REQ_NAME);
191         when(req.getMessage()).thenReturn(makeUpdate(pdpName, group, subgroup));
192
193         return req;
194     }
195
196     /**
197      * Makes an update message.
198      *
199      * @param pdpName PDP name
200      * @param group group name
201      * @param subgroup subgroup name
202      * @return a new update message
203      */
204     protected PdpUpdate makeUpdate(String pdpName, String group, String subgroup) {
205         PdpUpdate message = new PdpUpdate();
206
207         message.setName(pdpName);
208         message.setPolicies(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 }