aboutsummaryrefslogtreecommitdiffstats
path: root/main/src/test/java/org/onap/policy/pap/main/comm/CommonRequestBase.java
blob: 4086b6a1bc5402676fe57892bb98270c0ab8ed74 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
/*
 * ============LICENSE_START=======================================================
 * ONAP PAP
 * ================================================================================
 * Copyright (C) 2019-2021 AT&T Intellectual Property. All rights reserved.
 * Modifications Copyright (C) 2021 Nordix Foundation.
 * Modifications Copyright (C) 2022 Bell Canada. All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */

package org.onap.policy.pap.main.comm;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import io.micrometer.core.instrument.simple.SimpleMeterRegistry;
import java.util.Collections;
import java.util.LinkedList;
import java.util.Queue;
import java.util.function.Consumer;
import org.junit.Before;
import org.junit.BeforeClass;
import org.mockito.ArgumentCaptor;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
import org.onap.policy.common.endpoints.event.comm.Topic.CommInfrastructure;
import org.onap.policy.common.endpoints.listeners.RequestIdDispatcher;
import org.onap.policy.common.endpoints.listeners.TypedMessageListener;
import org.onap.policy.common.utils.services.Registry;
import org.onap.policy.models.pdp.concepts.PdpMessage;
import org.onap.policy.models.pdp.concepts.PdpStateChange;
import org.onap.policy.models.pdp.concepts.PdpStatus;
import org.onap.policy.models.pdp.concepts.PdpUpdate;
import org.onap.policy.models.pdp.enums.PdpState;
import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
import org.onap.policy.pap.main.PapConstants;
import org.onap.policy.pap.main.comm.msgdata.RequestListener;
import org.onap.policy.pap.main.comm.msgdata.StateChangeReq;
import org.onap.policy.pap.main.comm.msgdata.UpdateReq;
import org.onap.policy.pap.main.notification.PolicyNotifier;
import org.onap.policy.pap.main.parameters.PdpModifyRequestMapParams;
import org.onap.policy.pap.main.parameters.PdpParameters;
import org.onap.policy.pap.main.parameters.PdpStateChangeParameters;
import org.onap.policy.pap.main.parameters.PdpUpdateParameters;
import org.onap.policy.pap.main.parameters.RequestParams;

/**
 * Common base class for request tests.
 */
public class CommonRequestBase {
    protected static final String PDP1 = "pdp_1";
    protected static final String PDP2 = "pdp_2";
    protected static final String PDP3 = "pdp_3";
    protected static final String PDP4 = "pdp_4";
    protected static final String MY_REQ_NAME = "my-request";
    protected static final String DIFFERENT = "different-value";
    protected static final String MY_GROUP = "my-group";
    protected static final String MY_GROUP2 = "my-group-2";
    protected static final String MY_SUBGROUP = "my-subgroup";
    protected static final String MY_SUBGROUP2 = "my-subgroup-2";
    protected static final String MY_NAME = "my-name";
    protected static final PdpState MY_STATE = PdpState.SAFE;
    protected static final PdpState DIFF_STATE = PdpState.TERMINATED;
    protected static final int RETRIES = 1;

    protected Publisher<PdpMessage> publisher;
    protected PolicyNotifier notifier;
    protected RequestIdDispatcher<PdpStatus> dispatcher;
    protected Object lock;
    protected TimerManager timers;
    protected TimerManager.Timer timer;
    protected Queue<QueueToken<PdpMessage>> queue;
    protected RequestListener listener;
    protected RequestParams reqParams;
    protected PdpModifyRequestMapParams mapParams;

    @BeforeClass
    public static void setupBeforeAll() {
        Registry.registerOrReplace(PapConstants.REG_METER_REGISTRY, new SimpleMeterRegistry());
    }

    /**
     * Sets up.
     *
     * @throws Exception if an error occurs
     */
    @Before
    @SuppressWarnings("unchecked")
    public void setUp() throws Exception {
        publisher = mock(Publisher.class);
        notifier = mock(PolicyNotifier.class);
        dispatcher = mock(RequestIdDispatcher.class);
        lock = new Object();
        timers = mock(TimerManager.class);
        timer = mock(TimerManager.Timer.class);
        queue = new LinkedList<>();
        listener = mock(RequestListener.class);
        PdpParameters pdpParams = mock(PdpParameters.class);

        doAnswer(new Answer<Object>() {
            @Override
            public Object answer(InvocationOnMock invocation) throws Throwable {
                queue.add(invocation.getArgument(0, QueueToken.class));
                return null;
            }
        }).when(publisher).enqueue(any());

        when(timers.register(any(), any())).thenReturn(timer);

        PdpStateChangeParameters stateParams = mock(PdpStateChangeParameters.class);
        when(stateParams.getMaxRetryCount()).thenReturn(RETRIES);
        when(pdpParams.getStateChangeParameters()).thenReturn(stateParams);

        PdpUpdateParameters updateParams = mock(PdpUpdateParameters.class);
        when(updateParams.getMaxRetryCount()).thenReturn(RETRIES);
        when(pdpParams.getUpdateParameters()).thenReturn(updateParams);

        reqParams = new RequestParams().setMaxRetryCount(RETRIES).setModifyLock(lock).setPdpPublisher(publisher)
                        .setResponseDispatcher(dispatcher).setTimers(timers);

        mapParams = PdpModifyRequestMapParams.builder().modifyLock(lock).pdpPublisher(publisher)
                        .responseDispatcher(dispatcher)
                        .updateTimers(timers).stateChangeTimers(timers).params(pdpParams)
                        .maxPdpAgeMs(100).build();
    }

    /**
     * Gets the listener that was registered with the dispatcher and invokes it.
     *
     * @param response the response to pass to the listener
     */
    @SuppressWarnings("unchecked")
    protected void invokeProcessResponse(PdpStatus response) {
        @SuppressWarnings("rawtypes")
        ArgumentCaptor<TypedMessageListener> processResp = ArgumentCaptor.forClass(TypedMessageListener.class);

        verify(dispatcher).register(any(), processResp.capture());

        processResp.getValue().onTopicEvent(CommInfrastructure.NOOP, PapConstants.TOPIC_POLICY_PDP_PAP, response);
    }

    /**
     * Gets the timeout handler that was registered with the timer manager and invokes it.
     */
    @SuppressWarnings("unchecked")
    protected void invokeTimeoutHandler() {
        @SuppressWarnings("rawtypes")
        ArgumentCaptor<Consumer> timeoutHdlr = ArgumentCaptor.forClass(Consumer.class);

        verify(timers).register(any(), timeoutHdlr.capture());

        timeoutHdlr.getValue().accept(PDP1);
    }

    /**
     * Creates a policy with the given name and version.
     *
     * @param name policy name
     * @param version policy version
     * @return a new policy
     */
    protected ToscaPolicy makePolicy(String name, String version) {
        ToscaPolicy policy = new ToscaPolicy();

        policy.setName(name);
        policy.setVersion(version);

        return policy;
    }

    /**
     * Makes an update request with a new message.
     *
     * @param pdpName PDP name
     * @param group group name
     * @param subgroup subgroup name
     * @return a new update request
     */
    protected UpdateReq makeUpdateReq(String pdpName, String group, String subgroup) {
        UpdateReq req = mock(UpdateReq.class);

        when(req.getName()).thenReturn(MY_REQ_NAME);
        when(req.getMessage()).thenReturn(makeUpdate(pdpName, group, subgroup));

        return req;
    }

    /**
     * Makes an update message.
     *
     * @param pdpName PDP name
     * @param group group name
     * @param subgroup subgroup name
     * @return a new update message
     */
    protected PdpUpdate makeUpdate(String pdpName, String group, String subgroup) {
        PdpUpdate message = new PdpUpdate();

        message.setName(pdpName);
        message.setPoliciesToBeDeployed(Collections.emptyList());
        message.setPoliciesToBeUndeployed(Collections.emptyList());
        message.setPdpGroup(group);
        message.setPdpSubgroup(subgroup);

        return message;
    }

    /**
     * Makes a state-change request with a new message.
     *
     * @param pdpName PDP name
     * @param state desired PDP state
     * @return a new state-change request
     */
    protected StateChangeReq makeStateChangeReq(String pdpName, PdpState state) {
        StateChangeReq req = mock(StateChangeReq.class);

        when(req.getName()).thenReturn(MY_REQ_NAME);
        when(req.getMessage()).thenReturn(makeStateChange(pdpName, state));

        return req;
    }

    /**
     * Makes a state-change message.
     *
     * @param pdpName PDP name
     * @param state desired PDP state
     * @return a new state-change message
     */
    protected PdpStateChange makeStateChange(String pdpName, PdpState state) {
        PdpStateChange message = new PdpStateChange();

        message.setName(pdpName);
        message.setState(state);

        return message;
    }
}