Merge "Define constant for String literal"
[so.git] / bpmn / MSOCommonBPMN / src / test / java / org / onap / so / client / exception / ExceptionBuilderTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2017 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.so.client.exception;
22
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertNotNull;
25 import static org.mockito.ArgumentMatchers.any;
26 import static org.mockito.Mockito.when;
27 import org.camunda.bpm.engine.delegate.BpmnError;
28 import org.junit.Before;
29 import org.junit.Test;
30 import org.mockito.ArgumentMatchers;
31 import org.mockito.InjectMocks;
32 import org.mockito.Mock;
33 import org.mockito.Mockito;
34 import org.mockito.Spy;
35 import org.onap.aai.domain.yang.Vserver;
36 import org.onap.so.BaseTest;
37 import org.onap.so.bpmn.common.DelegateExecutionImpl;
38 import org.onap.so.bpmn.core.WorkflowException;
39 import org.onap.so.bpmn.mock.FileUtil;
40 import org.onap.so.bpmn.servicedecomposition.entities.ResourceKey;
41 import org.onap.so.bpmn.servicedecomposition.tasks.ExtractPojosForBB;
42 import org.onap.so.client.aai.AAIObjectType;
43 import org.onap.so.client.graphinventory.GraphInventoryCommonObjectMapperProvider;
44 import org.onap.so.objects.audit.AAIObjectAudit;
45 import org.onap.so.objects.audit.AAIObjectAuditList;
46 import org.springframework.beans.BeanUtils;
47 import com.fasterxml.jackson.core.JsonProcessingException;
48
49 public class ExceptionBuilderTest extends BaseTest {
50
51     private static final String RESOURCE_PATH = "__files/";
52     private static final String VALID_ERROR_MESSAGE = "{test error message}";
53
54     @Mock
55     protected ExtractPojosForBB extractPojosForBB;
56
57     @Spy
58     @InjectMocks
59     private ExceptionBuilder exceptionBuilder = new ExceptionBuilder();
60
61     GraphInventoryCommonObjectMapperProvider objectMapper = new GraphInventoryCommonObjectMapperProvider();
62
63     @Before
64     public void before() throws BBObjectNotFoundException, JsonProcessingException {
65         setCloudRegion();
66         when(extractPojosForBB.extractByKey(any(), ArgumentMatchers.eq(ResourceKey.VF_MODULE_ID)))
67                 .thenReturn(buildVfModule());
68         AAIObjectAuditList auditList = new AAIObjectAuditList();
69         auditList.setAuditType("create");
70         auditList.setHeatStackName("testStackName");
71         AAIObjectAudit vserver = new AAIObjectAudit();
72         vserver.setAaiObjectType(AAIObjectType.VSERVER.typeName());
73         vserver.setDoesObjectExist(false);
74         Vserver vs = new Vserver();
75         vs.setVserverId("testVServerId");
76         Vserver vServerShallow = new Vserver();
77         BeanUtils.copyProperties(vs, vServerShallow, "LInterfaces");
78         vserver.setAaiObject(vServerShallow);
79         auditList.getAuditList().add(vserver);
80
81         execution.setVariable("auditInventoryResult", objectMapper.getMapper().writeValueAsString(auditList));
82     }
83
84
85     @Test
86     public void buildAndThrowWorkflowExceptionTest() {
87         try {
88             ExceptionBuilder exceptionBuilder = new ExceptionBuilder();
89             exceptionBuilder.buildAndThrowWorkflowException(execution, 7000,
90                     new NullPointerException(VALID_ERROR_MESSAGE));
91         } catch (BpmnError bpmnException) {
92             assertEquals("MSOWorkflowException", bpmnException.getErrorCode());
93         }
94     }
95
96     @Test
97     public void buildAndThrowWorkflowExceptionInvalidMessageTest() {
98         try {
99             ExceptionBuilder exceptionBuilder = new ExceptionBuilder();
100             String invalidErrorMessage = FileUtil.readResourceFile(RESOURCE_PATH + "invalidErrorMessage.txt");
101             exceptionBuilder.buildAndThrowWorkflowException(execution, 7000,
102                     new NullPointerException(invalidErrorMessage));
103         } catch (BpmnError bpmnException) {
104             assertEquals("MSOWorkflowException", bpmnException.getErrorCode());
105         }
106     }
107
108     @Test
109     public void buildAndThrowWorkflowExceptionInvalidMessageFlagTest() {
110         try {
111             ExceptionBuilder exceptionBuilder = new ExceptionBuilder();
112             String invalidErrorMessage = FileUtil.readResourceFile(RESOURCE_PATH + "invalidErrorMessageFlag.txt");
113             exceptionBuilder.buildAndThrowWorkflowException(execution, 7000,
114                     new NullPointerException(invalidErrorMessage));
115         } catch (BpmnError bpmnException) {
116             assertEquals("MSOWorkflowException", bpmnException.getErrorCode());
117         }
118     }
119
120     @Test
121     public void buildAndThrowWorkflowExceptionNullMessageTest() {
122         try {
123             ExceptionBuilder exceptionBuilder = new ExceptionBuilder();
124             exceptionBuilder.buildAndThrowWorkflowException(execution, 7000, new NullPointerException());
125         } catch (BpmnError bpmnException) {
126             assertEquals("MSOWorkflowException", bpmnException.getErrorCode());
127         }
128     }
129
130     @Test
131     public void processAuditExceptionTest() {
132         try {
133             Mockito.doReturn(extractPojosForBB).when(exceptionBuilder).getExtractPojosForBB();
134             exceptionBuilder.processAuditException((DelegateExecutionImpl) execution);
135         } catch (BpmnError bpmnException) {
136             assertEquals("AAIInventoryFailure", bpmnException.getErrorCode());
137             WorkflowException we = execution.getVariable("WorkflowException");
138             assertNotNull(we);
139             assertEquals(
140                     "create VF-Module testVfModuleId1 failed due to incomplete A&AI vserver inventory population after stack testStackName was successfully created in cloud region testLcpCloudRegionId. MSO Audit indicates that AIC RO did not create vserver testVServerId in AAI. Recommendation - Wait for nightly RO Audit to run and fix the data issue and resume vf-module creation in VID. If problem persists then report problem to AIC/RO Ops.",
141                     we.getErrorMessage());
142         }
143     }
144
145 }