43319efb3aca0fc81ee788055b51aba22e9b66d1
[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.step;
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.assertSame;
27 import static org.junit.Assert.assertTrue;
28 import static org.mockito.Mockito.when;
29
30 import java.util.List;
31 import java.util.Map;
32 import java.util.UUID;
33 import org.junit.Before;
34 import org.junit.Test;
35 import org.mockito.Mock;
36 import org.mockito.MockitoAnnotations;
37 import org.onap.policy.controlloop.VirtualControlLoopEvent;
38 import org.onap.policy.controlloop.actor.guard.DecisionOperation;
39 import org.onap.policy.controlloop.actor.guard.GuardActor;
40 import org.onap.policy.controlloop.actor.so.VfModuleCreate;
41 import org.onap.policy.controlloop.actorserviceprovider.Operation;
42 import org.onap.policy.controlloop.actorserviceprovider.OperationProperties;
43 import org.onap.policy.controlloop.actorserviceprovider.controlloop.ControlLoopEventContext;
44 import org.onap.policy.controlloop.actorserviceprovider.parameters.ControlLoopOperationParams;
45 import org.onap.policy.controlloop.eventmanager.StepContext;
46
47 public class GuardStep2Test {
48     private static final String CL_NAME = "my-closed-loop";
49     private static final String MASTER_ACTOR = "master-actor";
50     private static final String MASTER_OPERATION = "master-operation";
51     private static final String MY_TARGET = "my-target";
52     private static final UUID REQ_ID = UUID.randomUUID();
53     private static final int VF_COUNT = 10;
54
55     @Mock
56     private ControlLoopEventContext context;
57     @Mock
58     private StepContext stepContext;
59     @Mock
60     private VirtualControlLoopEvent event;
61     @Mock
62     private Operation policyOper;
63
64     private ControlLoopOperationParams params;
65     private Step2 master;
66     private GuardStep2 step;
67
68     /**
69      * Sets up.
70      */
71     @Before
72     public void setUp() {
73         MockitoAnnotations.initMocks(this);
74
75         when(event.getRequestId()).thenReturn(REQ_ID);
76
77         when(context.getEvent()).thenReturn(event);
78
79         when(stepContext.getProperty(OperationProperties.AAI_TARGET_ENTITY)).thenReturn(MY_TARGET);
80         when(stepContext.contains(OperationProperties.DATA_VF_COUNT)).thenReturn(true);
81         when(stepContext.getProperty(OperationProperties.DATA_VF_COUNT)).thenReturn(VF_COUNT);
82
83
84         params = ControlLoopOperationParams.builder().actor(MASTER_ACTOR).operation(MASTER_OPERATION)
85                         .targetEntity(MY_TARGET).context(context).build();
86
87         master = new Step2(stepContext, params, event) {
88             @Override
89             protected Operation buildOperation() {
90                 return policyOper;
91             }
92         };
93
94         // force it to build the operation
95         master.init();
96
97         step = new GuardStep2(master, CL_NAME);
98     }
99
100     @Test
101     public void testConstructor() {
102         assertEquals(GuardActor.NAME, step.getActorName());
103         assertEquals(DecisionOperation.NAME, step.getOperationName());
104         assertSame(stepContext, step.stepContext);
105         assertSame(event, step.event);
106
107         // test when master is uninitialized
108         master = new Step2(stepContext, params, event);
109         assertThatIllegalStateException().isThrownBy(() -> new GuardStep2(master, CL_NAME));
110
111         ControlLoopOperationParams params2 = step.getParams();
112
113         // @formatter:off
114         assertThat(params2.getPayload()).isEqualTo(Map.of(
115                         "actor", MASTER_ACTOR,
116                         "operation", MASTER_OPERATION,
117                         "requestId", REQ_ID,
118                         "clname", CL_NAME));
119         // @formatter:on
120     }
121
122     @Test
123     public void testAcceptsEvent() {
124         // it should always accept events
125         assertTrue(step.acceptsEvent());
126     }
127
128     @Test
129     public void testGetPropertyNames() {
130         // unmatching property names
131         when(policyOper.getPropertyNames()).thenReturn(List.of("propA", "propB"));
132         assertThat(step.getPropertyNames()).isEmpty();
133
134         // matching property names
135         when(policyOper.getPropertyNames()).thenReturn(List.of("propA", OperationProperties.DATA_VF_COUNT, "propB"));
136         assertThat(step.getPropertyNames()).isEqualTo(List.of(OperationProperties.DATA_VF_COUNT));
137     }
138
139     @Test
140     public void testLoadTargetEntity() {
141         step.loadTargetEntity(OperationProperties.AAI_TARGET_ENTITY);
142         assertThat(step.getParams().getPayload().get(GuardStep2.PAYLOAD_KEY_TARGET_ENTITY)).isEqualTo(MY_TARGET);
143     }
144
145     /**
146      * Tests loadVfCount() when the policy operation is NOT "VF Module Create".
147      */
148     @Test
149     public void testLoadVfCountNotVfModuleCreate() {
150         // should decrement the count
151         step.loadVfCount("");
152         assertThat(step.getParams().getPayload().get(GuardStep2.PAYLOAD_KEY_VF_COUNT)).isEqualTo(VF_COUNT - 1);
153     }
154
155     /**
156      * Tests loadVfCount() when the policy operation is "VF Module Create".
157      */
158     @Test
159     public void testLoadVfCountVfModuleCreate() {
160         when(policyOper.getName()).thenReturn(VfModuleCreate.NAME);
161
162         // should increment the count
163         step.loadVfCount("");
164         assertThat(step.getParams().getPayload().get(GuardStep2.PAYLOAD_KEY_VF_COUNT)).isEqualTo(VF_COUNT + 1);
165     }
166 }