Topic names in PAP should be configurable from application.yaml
[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-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
12  *
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  *
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=========================================================
21  */
22
23 package org.onap.policy.pap.main.comm;
24
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;
30
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;
60
61 /**
62  * Common base class for request tests.
63  */
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";
80
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;
91
92     @BeforeClass
93     public static void setupBeforeAll() {
94         Registry.registerOrReplace(PapConstants.REG_METER_REGISTRY, new SimpleMeterRegistry());
95     }
96
97     /**
98      * Sets up.
99      *
100      * @throws Exception if an error occurs
101      */
102     @Before
103     @SuppressWarnings("unchecked")
104     public void setUp() throws Exception {
105         publisher = mock(Publisher.class);
106         notifier = mock(PolicyNotifier.class);
107         dispatcher = mock(RequestIdDispatcher.class);
108         lock = new Object();
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);
114
115         doAnswer((Answer<Object>) invocation -> {
116             queue.add(invocation.getArgument(0, QueueToken.class));
117             return null;
118         }).when(publisher).enqueue(any());
119
120         when(timers.register(any(), any())).thenReturn(timer);
121
122         PdpStateChangeParameters stateParams = mock(PdpStateChangeParameters.class);
123         when(stateParams.getMaxRetryCount()).thenReturn(RETRIES);
124         when(pdpParams.getStateChangeParameters()).thenReturn(stateParams);
125
126         PdpUpdateParameters updateParams = mock(PdpUpdateParameters.class);
127         when(updateParams.getMaxRetryCount()).thenReturn(RETRIES);
128         when(pdpParams.getUpdateParameters()).thenReturn(updateParams);
129
130         reqParams = new RequestParams().setMaxRetryCount(RETRIES).setModifyLock(lock).setPdpPublisher(publisher)
131                         .setResponseDispatcher(dispatcher).setTimers(timers);
132
133         mapParams = PdpModifyRequestMapParams.builder().modifyLock(lock).pdpPublisher(publisher)
134                         .responseDispatcher(dispatcher)
135                         .updateTimers(timers).stateChangeTimers(timers).params(pdpParams)
136                         .maxPdpAgeMs(100).build();
137     }
138
139     /**
140      * Gets the listener that was registered with the dispatcher and invokes it.
141      *
142      * @param response the response to pass to the listener
143      */
144     @SuppressWarnings("unchecked")
145     protected void invokeProcessResponse(PdpStatus response) {
146         @SuppressWarnings("rawtypes")
147         ArgumentCaptor<TypedMessageListener> processResp = ArgumentCaptor.forClass(TypedMessageListener.class);
148
149         verify(dispatcher).register(any(), processResp.capture());
150
151         processResp.getValue().onTopicEvent(CommInfrastructure.NOOP, PDP_PAP_TOPIC, response);
152     }
153
154     /**
155      * Gets the timeout handler that was registered with the timer manager and invokes it.
156      */
157     @SuppressWarnings("unchecked")
158     protected void invokeTimeoutHandler() {
159         @SuppressWarnings("rawtypes")
160         ArgumentCaptor<Consumer> timeoutHdlr = ArgumentCaptor.forClass(Consumer.class);
161
162         verify(timers).register(any(), timeoutHdlr.capture());
163
164         timeoutHdlr.getValue().accept(PDP1);
165     }
166
167     /**
168      * Creates a policy with the given name and version.
169      *
170      * @param name policy name
171      * @param version policy version
172      * @return a new policy
173      */
174     protected ToscaPolicy makePolicy(String name, String version) {
175         ToscaPolicy policy = new ToscaPolicy();
176
177         policy.setName(name);
178         policy.setVersion(version);
179
180         return policy;
181     }
182
183     /**
184      * Makes an update request with a new message.
185      *
186      * @param pdpName PDP name
187      * @param group group name
188      * @param subgroup subgroup name
189      * @return a new update request
190      */
191     protected UpdateReq makeUpdateReq(String pdpName, String group, String subgroup) {
192         UpdateReq req = mock(UpdateReq.class);
193
194         when(req.getName()).thenReturn(MY_REQ_NAME);
195         when(req.getMessage()).thenReturn(makeUpdate(pdpName, group, subgroup));
196
197         return req;
198     }
199
200     /**
201      * Makes an update message.
202      *
203      * @param pdpName PDP name
204      * @param group group name
205      * @param subgroup subgroup name
206      * @return a new update message
207      */
208     protected PdpUpdate makeUpdate(String pdpName, String group, String subgroup) {
209         PdpUpdate message = new PdpUpdate();
210
211         message.setName(pdpName);
212         message.setPoliciesToBeDeployed(Collections.emptyList());
213         message.setPoliciesToBeUndeployed(Collections.emptyList());
214         message.setPdpGroup(group);
215         message.setPdpSubgroup(subgroup);
216
217         return message;
218     }
219
220     /**
221      * Makes a state-change request with a new message.
222      *
223      * @param pdpName PDP name
224      * @param state desired PDP state
225      * @return a new state-change request
226      */
227     protected StateChangeReq makeStateChangeReq(String pdpName, PdpState state) {
228         StateChangeReq req = mock(StateChangeReq.class);
229
230         when(req.getName()).thenReturn(MY_REQ_NAME);
231         when(req.getMessage()).thenReturn(makeStateChange(pdpName, state));
232
233         return req;
234     }
235
236     /**
237      * Makes a state-change message.
238      *
239      * @param pdpName PDP name
240      * @param state desired PDP state
241      * @return a new state-change message
242      */
243     protected PdpStateChange makeStateChange(String pdpName, PdpState state) {
244         PdpStateChange message = new PdpStateChange();
245
246         message.setName(pdpName);
247         message.setState(state);
248
249         return message;
250     }
251 }