add junit coverage
[so.git] / bpmn / so-bpmn-tasks / src / test / java / org / onap / so / bpmn / infrastructure / audit / AuditTasksTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Copyright (c) 2020 Nokia
8  * ================================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * ============LICENSE_END=========================================================
21  */
22 package org.onap.so.bpmn.infrastructure.audit;
23
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertNotNull;
26 import static org.mockito.ArgumentMatchers.any;
27 import static org.mockito.ArgumentMatchers.eq;
28 import static org.mockito.Mockito.times;
29 import static org.mockito.Mockito.verify;
30 import static org.mockito.Mockito.when;
31 import org.junit.Before;
32 import org.junit.Rule;
33 import org.junit.Test;
34 import org.junit.rules.ExpectedException;
35 import org.mockito.InjectMocks;
36 import org.onap.aai.domain.yang.Vserver;
37 import org.onap.so.audit.beans.AuditInventory;
38 import org.onap.so.bpmn.BaseTaskTest;
39 import org.onap.so.bpmn.servicedecomposition.bbobjects.GenericVnf;
40 import org.onap.so.bpmn.servicedecomposition.bbobjects.ServiceInstance;
41 import org.onap.so.bpmn.servicedecomposition.bbobjects.VfModule;
42 import org.onap.so.bpmn.servicedecomposition.entities.ResourceKey;
43 import org.onap.so.client.exception.BBObjectNotFoundException;
44 import org.onap.so.client.graphinventory.GraphInventoryCommonObjectMapperProvider;
45 import org.onap.so.objects.audit.AAIObjectAudit;
46 import org.onap.so.objects.audit.AAIObjectAuditList;
47 import com.fasterxml.jackson.core.JsonProcessingException;
48
49 public class AuditTasksTest extends BaseTaskTest {
50
51     @InjectMocks
52     private AuditTasks auditTasks = new AuditTasks();
53     private ServiceInstance serviceInstance;
54     private GenericVnf genericVnf;
55     private VfModule vfModule;
56
57
58     @Rule
59     public final ExpectedException exception = ExpectedException.none();
60
61     @Before
62     public void before() throws BBObjectNotFoundException, JsonProcessingException {
63         serviceInstance = setServiceInstance();
64         genericVnf = setGenericVnf();
65         vfModule = setVfModule();
66         buildRequestContext();
67         setCloudRegion();
68         setRequestContext();
69         when(extractPojosForBB.extractByKey(any(), eq(ResourceKey.GENERIC_VNF_ID))).thenReturn(genericVnf);
70         when(extractPojosForBB.extractByKey(any(), eq(ResourceKey.VF_MODULE_ID))).thenReturn(vfModule);
71         when(extractPojosForBB.extractByKey(any(), eq(ResourceKey.SERVICE_INSTANCE_ID))).thenReturn(serviceInstance);
72         execution.setVariable("auditQuerySuccess", true);
73         AAIObjectAuditList auditList = new AAIObjectAuditList();
74         auditList.setHeatStackName("testHeatStackName");
75         AAIObjectAudit audit = new AAIObjectAudit();
76         Vserver vserver = new Vserver();
77         vserver.setVserverId("testVserverId");
78         audit.setAaiObject(vserver);
79         auditList.getAuditList().add(audit);
80         GraphInventoryCommonObjectMapperProvider objectMapper = new GraphInventoryCommonObjectMapperProvider();
81         String auditListString = objectMapper.getMapper().writeValueAsString(audit);
82         execution.setVariable("auditList", auditListString);
83     }
84
85     @Test
86     public void setupAuditVariableTest() throws Exception {
87         AuditInventory expectedAuditInventory = new AuditInventory();
88         expectedAuditInventory.setCloudOwner("testCloudOwner");
89         expectedAuditInventory.setCloudRegion("testLcpCloudRegionId");
90         expectedAuditInventory.setHeatStackName("testVfModuleName1");
91         expectedAuditInventory.setVfModuleId("testVfModuleId1");
92         expectedAuditInventory.setTenantId("testTenantId");
93         expectedAuditInventory.setGenericVnfId("testVnfId1");
94         expectedAuditInventory.setMsoRequestId("fb06f44c-c797-4f38-9b17-b4b975344600");
95         auditTasks.setupAuditVariable(execution);
96         // assertThat((AuditInventory) execution.getVariable("auditInventory"), sameBeanAs(expectedAuditInventory));
97     }
98
99     @Test
100     public void auditIsNeededTest() {
101         // given
102         when(env.getProperty("mso.infra.auditInventory")).thenReturn("true");
103         // when
104         auditTasks.isAuditNeeded(execution);
105         // then
106         assertNotNull(execution.getVariable("auditInventoryNeeded"));
107         assertEquals(execution.getVariable("auditInventoryNeeded"), true);
108     }
109
110     @Test
111     public void auditIsNotNeededTest() {
112         // given
113         when(env.getProperty("mso.infra.auditInventory")).thenReturn("false");
114         // when
115         auditTasks.isAuditNeeded(execution);
116         // then
117         assertNotNull(execution.getVariable("auditInventoryNeeded"));
118         assertEquals(execution.getVariable("auditInventoryNeeded"), false);
119     }
120
121     @Test
122     public void deleteAuditIsNeededTest() {
123         // given
124         when(env.getProperty("mso.infra.deleteAuditInventory")).thenReturn("true");
125         // when
126         auditTasks.isDeleteAuditNeeded(execution);
127         // then
128         assertNotNull(execution.getVariable("auditInventoryNeeded"));
129         assertEquals(execution.getVariable("auditInventoryNeeded"), true);
130     }
131
132     @Test
133     public void deleteAuditIsNotNeededTest() {
134         // given
135         when(env.getProperty("mso.infra.deleteAuditInventory")).thenReturn("false");
136         // when
137         auditTasks.isDeleteAuditNeeded(execution);
138         // then
139         assertNotNull(execution.getVariable("auditInventoryNeeded"));
140         assertEquals(execution.getVariable("auditInventoryNeeded"), false);
141     }
142
143     @Test
144     public void setupAuditVariable_shouldThrowWorkflowExceptionIfFails() {
145         // given
146         execution.setVariable("gBBInput", null);
147         // when
148         auditTasks.setupAuditVariable(execution);
149         // then
150         verify(exceptionUtil, times(1)).buildAndThrowWorkflowException(eq(execution), eq(7000), any(Exception.class));
151     }
152
153 }