aboutsummaryrefslogtreecommitdiffstats
path: root/controlloop/common/controller-usecases/src/test/java/org/onap/policy/drools/apps/controller/usecases/GetTargetEntityOperation2Test.java
blob: d4dbbe2711014e3ce26745b477414f9f0b671312 (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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
/*-
 * ============LICENSE_START=======================================================
 * ONAP
 * ================================================================================
 * Copyright (C) 2020-2021 AT&T Intellectual Property. All rights reserved.
 * Modifications Copyright (C) 2023 Nordix Foundation.
 * ================================================================================
 * 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.drools.apps.controller.usecases;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import java.util.List;
import java.util.Map;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.onap.aai.domain.yang.GenericVnf;
import org.onap.policy.controlloop.VirtualControlLoopEvent;
import org.onap.policy.controlloop.actorserviceprovider.OperationProperties;
import org.onap.policy.controlloop.actorserviceprovider.TargetType;
import org.onap.policy.controlloop.actorserviceprovider.parameters.ControlLoopOperationParams;
import org.onap.policy.controlloop.eventmanager.StepContext;

class GetTargetEntityOperation2Test {
    private static final String GENERIC_VNF_ID = "generic-vnf.vnf-id";
    private static final String VSERVER_NAME = "vserver.vserver-name";
    private static final String GENERIC_VNF_NAME = "generic-vnf.vnf-name";
    private static final String MY_PNF = "my-pnf";
    private static final String MY_VNF = "my-vnf";
    private static final String MY_ACTOR = "my-actor";
    private static final String MY_OPERATION = "my-operation";

    private final StepContext stepContext = mock(StepContext.class);
    private final ControlLoopOperationParams params = mock(ControlLoopOperationParams.class);

    private VirtualControlLoopEvent event;
    private GenericVnf vnf;
    private GetTargetEntityOperation2 oper;

    /**
     * Sets up.
     */
    @BeforeEach
    public void setUp() {
        event = new VirtualControlLoopEvent();
        event.setTarget("pnf.pnf-name");
        event.setAai(Map.of("pnf.pnf-name", MY_PNF));

        vnf = new GenericVnf();
        vnf.setVnfId(MY_VNF);

        when(params.getTargetType()).thenReturn(TargetType.PNF);
        when(params.getActor()).thenReturn(MY_ACTOR);
        when(params.getOperation()).thenReturn(MY_OPERATION);

        oper = new GetTargetEntityOperation2(stepContext, event, params);
    }

    @Test
    void testGetPropertyNames() {
        // already have the data - no properties needed
        assertThat(oper.getPropertyNames()).isEmpty();

        // try an operation that needs data
        remakeWithoutData();
        assertEquals(List.of(UsecasesConstants.AAI_DEFAULT_GENERIC_VNF), oper.getPropertyNames());

        // tell it the entity is available
        when(stepContext.contains(OperationProperties.AAI_TARGET_ENTITY)).thenReturn(true);
        assertThat(oper.getPropertyNames()).isEmpty();
    }

    @Test
    void testStart() {
        assertThatThrownBy(() -> oper.start()).isInstanceOf(UnsupportedOperationException.class)
                        .hasMessage("cannot start get-target-entity operation");
    }

    /**
     * Tests detmTarget() when the target has already been determined.
     */
    @Test
    void testDetmTargetRepeat() {
        remakeWithoutData();
        assertFalse(oper.getPropertyNames().isEmpty());

        // tell it the entity is available
        when(stepContext.contains(OperationProperties.AAI_TARGET_ENTITY)).thenReturn(true);
        assertThat(oper.getPropertyNames()).isEmpty();

        // repeat
        assertThat(oper.getPropertyNames()).isEmpty();
    }

    /**
     * Tests detmTarget() when the target type is {@code null}.
     */
    @Test
    void testDetmTargetNullType() {
        remakeWithoutData();
        when(params.getTargetType()).thenReturn(null);

        assertThatIllegalArgumentException().isThrownBy(() -> oper.getPropertyNames())
                        .withMessage("The target type is null");
    }

    /**
     * Tests detmTarget() when the target type is VM and enrichment data is provided.
     */
    @Test
    void testDetmTargetVm() {
        when(params.getTargetType()).thenReturn(TargetType.VM);
        enrichTarget(VSERVER_NAME);
        assertThat(oper.getPropertyNames()).isEmpty();
        verifyTarget(MY_VNF);
    }

    /**
     * Tests detmTarget() when the target type is VNF and enrichment data is provided.
     */
    @Test
    void testDetmTargetVnf() {
        when(params.getTargetType()).thenReturn(TargetType.VNF);
        enrichTarget(VSERVER_NAME);
        assertThat(oper.getPropertyNames()).isEmpty();
        verifyTarget(MY_VNF);
    }

    /**
     * Tests detmTarget() when the target type is VF Module and enrichment data is
     * provided.
     */
    @Test
    void testDetmTargetVfModule() {
        when(params.getTargetType()).thenReturn(TargetType.VFMODULE);
        enrichTarget(VSERVER_NAME);
        assertThat(oper.getPropertyNames()).isEmpty();
        verifyTarget(MY_VNF);
    }

    /**
     * Tests detmTarget() when the target type is unknown.
     */
    @Test
    void testDetmTargetUnknownType() {
        when(params.getTargetType()).thenReturn(TargetType.VFC);
        enrichTarget(VSERVER_NAME);
        assertThatIllegalArgumentException().isThrownBy(() -> oper.getPropertyNames())
                        .withMessage("The target type is not supported");
    }

    @Test
    void testDetmPnfTargetPnf() {
        assertThat(oper.getPropertyNames()).isEmpty();
        verifyTarget(MY_PNF);
    }

    /**
     * Tests detmPnfTarget() when the target name is incorrect.
     */
    @Test
    void testDetmPnfTargetInvalidName() {
        event.setTarget("unknown-pnf");
        assertThatIllegalArgumentException().isThrownBy(() -> oper.getPropertyNames())
                        .withMessage("Target does not match target type");
    }

    /**
     * Tests detmPnfTarget() when the enrichment data is missing.
     */
    @Test
    void testDetmPnfTargetPnfNoEnrichment() {
        event.setAai(Map.of());
        assertThatIllegalArgumentException().isThrownBy(() -> oper.getPropertyNames())
                        .withMessage("AAI section is missing pnf.pnf-name");
    }

    /**
     * Tests detmVfModuleTarget() when the target is null.
     */
    @Test
    void testDetmVfModuleTargetNullTarget() {
        when(params.getTargetType()).thenReturn(TargetType.VM);
        event.setTarget(null);
        assertThatIllegalArgumentException().isThrownBy(() -> oper.getPropertyNames()).withMessage("Target is null");
    }

    /**
     * Tests detmVfModuleTarget() when the target is the vserver name.
     */
    @Test
    void testDetmVfModuleTargetVserverName() {
        when(params.getTargetType()).thenReturn(TargetType.VM);
        enrichTarget(VSERVER_NAME);
        assertThat(oper.getPropertyNames()).isEmpty();
        verifyTarget(MY_VNF);
    }

    /**
     * Tests detmVfModuleTarget() when the target is the VNF ID.
     */
    @Test
    void testDetmVfModuleTargetVnfId() {
        when(params.getTargetType()).thenReturn(TargetType.VM);
        enrichTarget(GENERIC_VNF_ID);
        assertThat(oper.getPropertyNames()).isEmpty();
        verifyTarget(MY_VNF);
    }

    /**
     * Tests detmVfModuleTarget() when the target is the VNF name. Note: the enrichment
     * data is actually stored in the VNF ID field.
     */
    @Test
    void testDetmVfModuleTargetVnfName() {
        when(params.getTargetType()).thenReturn(TargetType.VM);
        enrichTarget(GENERIC_VNF_ID);

        // set this AFTER setting enrichment data
        event.setTarget("generic-vnf.vnf-name");

        assertThat(oper.getPropertyNames()).isEmpty();
        verifyTarget(MY_VNF);
    }

    /**
     * Tests detmVfModuleTarget() when the target is unknown.
     */
    @Test
    void testDetmVfModuleTargetUnknown() {
        when(params.getTargetType()).thenReturn(TargetType.VM);
        enrichTarget(VSERVER_NAME);
        event.setTarget("unknown-vnf");
        assertThatIllegalArgumentException().isThrownBy(() -> oper.getPropertyNames())
                        .withMessage("Target does not match target type");
    }

    /**
     * Tests detmVfModuleTarget() when the enrichment data is missing.
     */
    @Test
    void testDetmVfModuleTargetMissingEnrichment() {
        when(params.getTargetType()).thenReturn(TargetType.VM);
        event.setTarget(VSERVER_NAME);
        assertThatIllegalArgumentException().isThrownBy(() -> oper.getPropertyNames())
                        .withMessage("Enrichment data is missing " + VSERVER_NAME);
    }

    /**
     * Tests detmVnfName() when enrichment data is available.
     */
    @Test
    void testDetmVnfNameHaveData() {
        when(params.getTargetType()).thenReturn(TargetType.VM);
        event.setTarget(GENERIC_VNF_NAME);
        event.setAai(Map.of(GENERIC_VNF_ID, MY_VNF));
        assertThat(oper.getPropertyNames()).isEmpty();
        verifyTarget(MY_VNF);
    }

    /**
     * Tests detmVnfName() when enrichment data is missing.
     */
    @Test
    void testDetmVnfNameNoData() {
        when(params.getTargetType()).thenReturn(TargetType.VM);
        event.setTarget(GENERIC_VNF_NAME);
        assertThat(oper.getPropertyNames()).isEqualTo(List.of(UsecasesConstants.AAI_DEFAULT_GENERIC_VNF));
    }

    @Test
    void testSetProperty() {
        event.setTarget(GENERIC_VNF_NAME);

        // not a property of interest - should be ignored
        oper.setProperty("unknown-property", vnf);
        verify(stepContext, never()).setProperty(any(), any());

        // now set the desired property and try again
        oper.setProperty(UsecasesConstants.AAI_DEFAULT_GENERIC_VNF, vnf);
        verifyTarget(MY_VNF);
    }

    @Test
    void testGetActorName_testGetName() {
        assertEquals(MY_ACTOR, oper.getActorName());
        assertEquals(MY_OPERATION, oper.getName());
    }

    private void verifyTarget(String targetEntity) {
        verify(stepContext).setProperty(OperationProperties.AAI_TARGET_ENTITY, targetEntity);
    }

    private void remakeWithoutData() {
        when(params.getTargetType()).thenReturn(TargetType.VNF);
        event.setTarget(GENERIC_VNF_NAME);
        oper = new GetTargetEntityOperation2(stepContext, event, params);
    }

    private void enrichTarget(String enrichmentTargetType) {
        event.setTarget(enrichmentTargetType);
        event.setAai(Map.of(enrichmentTargetType, MY_VNF));
    }
}