Upgrade Java 17 in policy-drools-apps
[policy/drools-applications.git] / controlloop / common / controller-usecases / src / test / java / org / onap / policy / drools / apps / controller / usecases / LockOperation2Test.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP
4  * ================================================================================
5  * Copyright (C) 2020-2021 AT&T Intellectual Property. All rights reserved.
6  * Modifications Copyright (C) 2023 Nordix Foundation.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.drools.apps.controller.usecases;
23
24 import static org.assertj.core.api.Assertions.assertThat;
25 import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
26 import static org.junit.jupiter.api.Assertions.assertEquals;
27 import static org.junit.jupiter.api.Assertions.assertNull;
28 import static org.junit.jupiter.api.Assertions.assertSame;
29 import static org.mockito.ArgumentMatchers.anyString;
30 import static org.mockito.Mockito.mock;
31 import static org.mockito.Mockito.when;
32
33 import java.util.List;
34 import java.util.Map;
35 import java.util.concurrent.CompletableFuture;
36 import org.junit.jupiter.api.BeforeEach;
37 import org.junit.jupiter.api.Test;
38 import org.onap.aai.domain.yang.GenericVnf;
39 import org.onap.policy.controlloop.VirtualControlLoopEvent;
40 import org.onap.policy.controlloop.actorserviceprovider.OperationOutcome;
41 import org.onap.policy.controlloop.actorserviceprovider.OperationProperties;
42 import org.onap.policy.controlloop.actorserviceprovider.parameters.ControlLoopOperationParams;
43 import org.onap.policy.controlloop.eventmanager.StepContext;
44
45 class LockOperation2Test {
46     private static final String MY_PNF = "my-pnf";
47     private static final String MY_VNF = "my-vnf";
48     private static final String MY_ACTOR = "my-actor";
49     private static final String MY_OPERATION = "my-operation";
50
51     private final StepContext stepContext = mock(StepContext.class);
52     private final ControlLoopOperationParams params = mock(ControlLoopOperationParams.class);
53
54     private VirtualControlLoopEvent event;
55     private CompletableFuture<OperationOutcome> future;
56     private GenericVnf vnf;
57     private LockOperation2 oper;
58
59     /**
60      * Sets up.
61      */
62     @BeforeEach
63     public void setUp() {
64         event = new VirtualControlLoopEvent();
65         event.setTarget("pnf.pnf-name");
66         event.setAai(Map.of("pnf.pnf-name", MY_PNF));
67
68         future = new CompletableFuture<>();
69
70         vnf = new GenericVnf();
71         vnf.setVnfId(MY_VNF);
72
73         when(stepContext.requestLock(anyString())).thenReturn(future);
74
75         when(params.getActor()).thenReturn(MY_ACTOR);
76         when(params.getOperation()).thenReturn(MY_OPERATION);
77
78         oper = new LockOperation2(stepContext, params);
79     }
80
81     @Test
82     void testGetPropertyNames() {
83         assertThat(oper.getPropertyNames()).isEqualTo(List.of(OperationProperties.AAI_TARGET_ENTITY));
84     }
85
86     @Test
87     void testStart() {
88         // missing data
89         assertThatIllegalStateException().isThrownBy(() -> oper.start())
90                         .withMessage("target lock entity has not been determined yet");
91
92         oper.setProperty(OperationProperties.AAI_TARGET_ENTITY, "some-target");
93         assertSame(future, oper.start());
94     }
95
96     @Test
97     void testSetProperty() {
98         oper.setProperty("unknown-property", "some data");
99         assertNull(oper.getTargetEntity());
100
101         oper.setProperty(OperationProperties.AAI_TARGET_ENTITY, "other data");
102         assertEquals("other data", oper.getTargetEntity());
103     }
104
105     @Test
106     void testGetActorName_testGetName() {
107         assertEquals(MY_ACTOR, oper.getActorName());
108         assertEquals(MY_OPERATION, oper.getName());
109     }
110 }