2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2019 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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.so.adapters.audit;
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertNotNull;
25 import static org.mockito.Mockito.doReturn;
27 import java.io.IOException;
29 import java.util.Optional;
30 import org.camunda.bpm.client.task.ExternalTask;
31 import org.camunda.bpm.client.task.ExternalTaskService;
32 import org.junit.Before;
33 import org.junit.Test;
34 import org.mockito.ArgumentCaptor;
35 import org.mockito.InjectMocks;
36 import org.mockito.Mock;
37 import org.mockito.Mockito;
38 import org.mockito.MockitoAnnotations;
39 import org.onap.so.audit.beans.AuditInventory;
40 import org.onap.so.objects.audit.AAIObjectAuditList;
41 import org.springframework.core.env.Environment;
42 import com.fasterxml.jackson.core.JsonParseException;
43 import com.fasterxml.jackson.core.JsonProcessingException;
44 import com.fasterxml.jackson.databind.JsonMappingException;
45 import com.fasterxml.jackson.databind.ObjectMapper;
47 public class AuditStackServiceDataTest extends AuditCreateStackService {
50 private AuditCreateStackService auditStackService = new AuditCreateStackService();
53 private AuditQueryStackService auditQueryStackService = new AuditQueryStackService();
56 private HeatStackAudit heatStackAuditMock;
59 private Environment mockEnv;
62 private ExternalTask mockExternalTask;
65 private ExternalTaskService mockExternalTaskService;
68 private AuditDataService auditDataService;
70 private ObjectMapper objectMapper = new ObjectMapper();
72 private AuditInventory auditInventory = new AuditInventory();
74 Optional<AAIObjectAuditList> auditListOptSuccess;
76 Optional<AAIObjectAuditList> auditListOptFailure;
79 public void setup() throws JsonParseException, JsonMappingException, IOException {
80 auditInventory.setCloudOwner("cloudOwner");
81 auditInventory.setCloudRegion("cloudRegion");
82 auditInventory.setTenantId("tenantId");
83 auditInventory.setHeatStackName("stackName");
84 MockitoAnnotations.initMocks(this);
86 AAIObjectAuditList auditListSuccess = objectMapper
87 .readValue(new File("src/test/resources/ExpectedVServerFound.json"), AAIObjectAuditList.class);
88 auditListOptSuccess = Optional.of(auditListSuccess);
90 AAIObjectAuditList auditListFailure = objectMapper.readValue(
91 new File("src/test/resources/Vserver2_Found_VServer1_Not_Found.json"), AAIObjectAuditList.class);
92 auditListOptFailure = Optional.of(auditListFailure);
93 String[] retrySequence = new String[8];
94 retrySequence[0] = "1";
95 retrySequence[1] = "1";
96 retrySequence[2] = "2";
97 retrySequence[3] = "3";
98 retrySequence[4] = "5";
99 retrySequence[5] = "8";
100 retrySequence[6] = "13";
101 retrySequence[7] = "20";
102 doReturn(auditInventory).when(mockExternalTask).getVariable("auditInventory");
103 doReturn("6000").when(mockEnv).getProperty("mso.workflow.topics.retryMultiplier", "6000");
104 doReturn(retrySequence).when(mockEnv).getProperty("mso.workflow.topics.retrySequence", String[].class);
105 doReturn("aasdfasdf").when(mockExternalTask).getId();
109 public void execute_external_task_audit_success_Test() {
110 doReturn(auditListOptSuccess).when(heatStackAuditMock).auditHeatStack("cloudRegion", "cloudOwner", "tenantId",
112 auditStackService.executeExternalTask(mockExternalTask, mockExternalTaskService);
113 ArgumentCaptor<Map> captor = ArgumentCaptor.forClass(Map.class);
114 ArgumentCaptor<ExternalTask> taskCaptor = ArgumentCaptor.forClass(ExternalTask.class);
115 Mockito.verify(mockExternalTaskService).complete(taskCaptor.capture(), captor.capture());
116 Map actualMap = captor.getValue();
117 assertEquals(true, actualMap.get("auditIsSuccessful"));
118 assertNotNull(actualMap.get("auditInventoryResult"));
122 public void executeExternalTaskQueryAuditTest() throws JsonProcessingException {
123 doReturn(auditListOptSuccess).when(heatStackAuditMock).queryHeatStack("cloudOwner", "cloudRegion", "tenantId",
125 Mockito.doNothing().when(auditDataService).writeStackDataToRequestDb(Mockito.any(AuditInventory.class),
126 Mockito.any(AAIObjectAuditList.class));
127 auditQueryStackService.executeExternalTask(mockExternalTask, mockExternalTaskService);
128 ArgumentCaptor<Map> captor = ArgumentCaptor.forClass(Map.class);
129 ArgumentCaptor<ExternalTask> taskCaptor = ArgumentCaptor.forClass(ExternalTask.class);
130 Mockito.verify(mockExternalTaskService).complete(taskCaptor.capture(), captor.capture());
131 Mockito.verify(auditDataService).writeStackDataToRequestDb(Mockito.any(AuditInventory.class),
132 Mockito.any(AAIObjectAuditList.class));
136 public void execute_external_task_audit_first_failure_Test() {
137 doReturn(auditListOptFailure).when(heatStackAuditMock).auditHeatStack("cloudRegion", "cloudOwner", "tenantId",
139 doReturn(null).when(mockExternalTask).getRetries();
140 auditStackService.executeExternalTask(mockExternalTask, mockExternalTaskService);
141 Mockito.verify(mockExternalTaskService).handleFailure(mockExternalTask,
142 "Unable to find all VServers and L-Interaces in A&AI",
143 "Unable to find all VServers and L-Interaces in A&AI", 8, 10000L);
147 public void execute_external_task_audit_intermediate_failure_Test() {
148 doReturn(auditListOptFailure).when(heatStackAuditMock).auditHeatStack("cloudRegion", "cloudOwner", "tenantId",
150 doReturn(6).when(mockExternalTask).getRetries();
151 auditStackService.executeExternalTask(mockExternalTask, mockExternalTaskService);
152 Mockito.verify(mockExternalTaskService).handleFailure(mockExternalTask,
153 "Unable to find all VServers and L-Interaces in A&AI",
154 "Unable to find all VServers and L-Interaces in A&AI", 5, 12000L);
159 public void execute_external_task_audit_final_failure_Test() {
160 doReturn(auditListOptFailure).when(heatStackAuditMock).auditHeatStack("cloudRegion", "cloudOwner", "tenantId",
162 doReturn(1).when(mockExternalTask).getRetries();
163 auditStackService.executeExternalTask(mockExternalTask, mockExternalTaskService);
164 ArgumentCaptor<Map> captor = ArgumentCaptor.forClass(Map.class);
165 ArgumentCaptor<ExternalTask> taskCaptor = ArgumentCaptor.forClass(ExternalTask.class);
166 Mockito.verify(mockExternalTaskService).complete(taskCaptor.capture(), captor.capture());
167 Map actualMap = captor.getValue();
168 assertEquals(false, actualMap.get("auditIsSuccessful"));
169 assertNotNull(actualMap.get("auditInventoryResult"));
173 public void retry_sequence_calculation_Test() {
174 long firstRetry = auditStackService.calculateRetryDelay(8);
175 assertEquals(6000L, firstRetry);
176 long secondRetry = auditStackService.calculateRetryDelay(7);
177 assertEquals(6000L, secondRetry);
178 long thirdRetry = auditStackService.calculateRetryDelay(6);
179 assertEquals(12000L, thirdRetry);
180 long fourthRetry = auditStackService.calculateRetryDelay(5);
181 assertEquals(18000L, fourthRetry);
182 long fifthRetry = auditStackService.calculateRetryDelay(4);
183 assertEquals(30000L, fifthRetry);
184 long sixRetry = auditStackService.calculateRetryDelay(3);
185 assertEquals(48000L, sixRetry);
186 long seventhRetry = auditStackService.calculateRetryDelay(2);
187 assertEquals(78000L, seventhRetry);
188 long eigthRetry = auditStackService.calculateRetryDelay(1);
189 assertEquals(120000L, eigthRetry);
193 public void retry_sequence_Test() {
194 long firstRetry = auditStackService.calculateRetryDelay(8);
195 assertEquals(6000L, firstRetry);
196 long secondRetry = auditStackService.calculateRetryDelay(7);
197 assertEquals(6000L, secondRetry);
198 long thirdRetry = auditStackService.calculateRetryDelay(6);
199 assertEquals(12000L, thirdRetry);
200 long fourthRetry = auditStackService.calculateRetryDelay(5);
201 assertEquals(18000L, fourthRetry);
202 long fifthRetry = auditStackService.calculateRetryDelay(4);
203 assertEquals(30000L, fifthRetry);
204 long sixRetry = auditStackService.calculateRetryDelay(3);
205 assertEquals(48000L, sixRetry);
206 long seventhRetry = auditStackService.calculateRetryDelay(2);
207 assertEquals(78000L, seventhRetry);
208 long eigthRetry = auditStackService.calculateRetryDelay(1);
209 assertEquals(120000L, eigthRetry);
214 public void determineAuditResult_Test() throws Exception {
215 boolean actual = auditStackService.didCreateAuditFail(auditListOptSuccess);
216 assertEquals(false, actual);
220 public void determineAuditResult_Failure_Test() throws Exception {
221 boolean actual = auditStackService.didCreateAuditFail(auditListOptFailure);
222 assertEquals(true, actual);