dfbedc2c7f7fd840f5c47dd448d1b95a97ad2910
[policy/drools-applications.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP
4  * ================================================================================
5  * Copyright (C) 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.drools.apps.controller.usecases;
22
23 import static org.assertj.core.api.Assertions.assertThat;
24 import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
25 import static org.junit.Assert.assertEquals;
26 import static org.junit.Assert.assertNull;
27 import static org.junit.Assert.assertSame;
28 import static org.mockito.ArgumentMatchers.anyString;
29 import static org.mockito.Mockito.when;
30
31 import java.util.List;
32 import java.util.Map;
33 import java.util.concurrent.CompletableFuture;
34 import org.junit.Before;
35 import org.junit.Test;
36 import org.mockito.Mock;
37 import org.mockito.MockitoAnnotations;
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 import org.onap.policy.controlloop.policy.Target;
45 import org.onap.policy.controlloop.policy.TargetType;
46
47 public class LockOperation2Test {
48     private static final String MY_PNF = "my-pnf";
49     private static final String MY_VNF = "my-vnf";
50     private static final String MY_ACTOR = "my-actor";
51     private static final String MY_OPERATION = "my-operation";
52
53     @Mock
54     private StepContext stepContext;
55     @Mock
56     private ControlLoopOperationParams params;
57
58     private VirtualControlLoopEvent event;
59     private Target target;
60     private CompletableFuture<OperationOutcome> future;
61     private GenericVnf vnf;
62     private LockOperation2 oper;
63
64     /**
65      * Sets up.
66      */
67     @Before
68     public void setUp() {
69         MockitoAnnotations.initMocks(this);
70
71         event = new VirtualControlLoopEvent();
72         event.setTarget("pnf.pnf-name");
73         event.setAai(Map.of("pnf.pnf-name", MY_PNF));
74
75         target = new Target();
76         target.setType(TargetType.PNF);
77
78         future = new CompletableFuture<>();
79
80         vnf = new GenericVnf();
81         vnf.setVnfId(MY_VNF);
82
83         when(stepContext.requestLock(anyString())).thenReturn(future);
84
85         when(params.getTarget()).thenReturn(target);
86         when(params.getActor()).thenReturn(MY_ACTOR);
87         when(params.getOperation()).thenReturn(MY_OPERATION);
88
89         oper = new LockOperation2(stepContext, params);
90     }
91
92     @Test
93     public void testGetPropertyNames() {
94         assertThat(oper.getPropertyNames()).isEqualTo(List.of(OperationProperties.AAI_TARGET_ENTITY));
95     }
96
97     @Test
98     public void testStart() {
99         // missing data
100         assertThatIllegalStateException().isThrownBy(() -> oper.start())
101                         .withMessage("target lock entity has not been determined yet");
102
103         oper.setProperty(OperationProperties.AAI_TARGET_ENTITY, "some-target");
104         assertSame(future, oper.start());
105     }
106
107     @Test
108     public void testSetProperty() {
109         oper.setProperty("unknown-property", "some data");
110         assertNull(oper.getTargetEntity());
111
112         oper.setProperty(OperationProperties.AAI_TARGET_ENTITY, "other data");
113         assertEquals("other data", oper.getTargetEntity());
114     }
115
116     @Test
117     public void testGetActorName_testGetName() {
118         assertEquals(MY_ACTOR, oper.getActorName());
119         assertEquals(MY_OPERATION, oper.getName());
120     }
121 }