[MSO-8] Update the maven dependency
[so.git] / bpmn / MSOCommonBPMN / src / test / groovy / org / openecomp / mso / bpmn / common / scripts / ExceptionUtilTest.groovy
1 /*- 
2  * ============LICENSE_START======================================================= 
3  * OPENECOMP - MSO 
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.openecomp.mso.bpmn.common.scripts
22
23 import org.junit.Assert
24 import org.junit.runner.RunWith;
25 import org.mockito.runners.MockitoJUnitRunner;
26
27
28 import org.junit.Ignore;
29
30 import static org.mockito.Mockito.*
31
32 import org.camunda.bpm.engine.impl.persistence.entity.ExecutionEntity
33 import org.junit.Before
34 import org.junit.runner.RunWith
35 import org.mockito.MockitoAnnotations
36 import org.mockito.internal.debugging.MockitoDebuggerImpl
37 import org.mockito.runners.MockitoJUnitRunner
38 import org.openecomp.mso.bpmn.common.scripts.ExceptionUtil;
39 @RunWith(MockitoJUnitRunner.class)
40 import org.junit.Test
41
42
43
44 class ExceptionUtilTest {
45
46
47         @Before
48         public void init()
49         {
50                 MockitoAnnotations.initMocks(this)
51         }
52
53         @Test
54         public void testErrorMessageDetails() {
55
56
57
58                 String restFault = """
59                                 <Fault>
60 <requestError>
61 <serviceException>
62 <messageId>SVC3002</messageId>
63 <text>Error writing output performing %1 on %2 (msg=%3) (ec=%4)</text>
64 <variables>
65 <variable>PUTcustomer</variable>
66 <variable>SubName01</variable>
67 <variable>Unexpected error reading/updating database:Adding this property for key [service-instance-id] and value [USSTU2CFCNC0101UJZZ01] violates a uniqueness constraint [service-instance-id]</variable>
68 <variable>ERR.5.4.5105</variable>
69 </variables>
70 </serviceException>
71 </requestError>
72 </Fault>
73                         """
74                 def errorString = """<aetgt:WorkflowException xmlns:aetgt="http://org.openecomp/mso/workflow/schema/v1">
75                                 <aetgt:ErrorMessage>Received error from A&amp;AI (Error writing output performing PUTcustomer on SubName01 (msg=Unexpected error reading/updating database:Adding this property for key [service-instance-id] and value [USSTU2CFCNC0101UJZZ01] violates a uniqueness constraint [service-instance-id]) (ec=ERR.5.4.5105))</aetgt:ErrorMessage>
76                                 <aetgt:ErrorCode>5000</aetgt:ErrorCode>
77                         </aetgt:WorkflowException>""" as String
78
79                 ExecutionEntity mockExecution = mock(ExecutionEntity.class)
80
81                 ExceptionUtil util = new ExceptionUtil()
82                 Assert.assertEquals(errorString, util.MapAAIExceptionToWorkflowExceptionOld(restFault, mockExecution))
83         }
84
85
86         @Test
87         public void testErrorMessage() {
88                 String restFault = """
89                                 <rest:RESTFault xmlns:rest="http://schemas.activebpel.org/REST/2007/12/01/aeREST.xsd"
90                                 errorType="transport">
91                                 <rest:errorCode>500</rest:errorCode>
92                                 <rest:message>HTTP method: GET Internal Server Error </rest:message>
93                                 </rest:RESTFault>
94                         """
95                 def errorString = """<aetgt:WorkflowException xmlns:aetgt="http://org.openecomp/mso/workflow/schema/v1">
96                                         <aetgt:ErrorMessage>Received error from A&amp;AI (HTTP method: GET Internal Server Error)</aetgt:ErrorMessage>
97                                         <aetgt:ErrorCode>5000</aetgt:ErrorCode>
98                                 </aetgt:WorkflowException>""" as String
99
100                 ExecutionEntity mockExecution = mock(ExecutionEntity.class)
101                 when(mockExecution.getVariable("prefix")).thenReturn("test_")
102                 when(mockExecution.getVariable("test_ErrorResponse")).thenReturn("HTTP method: GET Internal Server Error")
103
104                 ExceptionUtil util = new ExceptionUtil()
105                 String msg = "HTTP method: GET Internal Server Error"
106                 Assert.assertEquals(errorString, util.MapAAIExceptionToWorkflowExceptionOld(restFault, mockExecution))
107         }
108
109         @Test
110         public void testBadResponseErrorMessage() {
111                 String restFault = "asfafasfasfd"
112                 ExecutionEntity mockExecution = mock(ExecutionEntity.class)
113                 when(mockExecution.getVariable("prefix")).thenReturn("test_")
114                 when(mockExecution.getVariable("test_ErrorResponse")).thenReturn(null)
115                 ExceptionUtil util = new ExceptionUtil()
116                 String expected = "<aetgt:WorkflowException xmlns:aetgt=\"http://org.openecomp/mso/workflow/schema/v1\">"+
117                 "\n                                     <aetgt:ErrorMessage>Received error from A&amp;AI ()</aetgt:ErrorMessage>"+
118                 "\n                                     <aetgt:ErrorCode>5000</aetgt:ErrorCode>"+
119         "\n                             </aetgt:WorkflowException>";
120                 Assert.assertEquals(expected,util.MapAAIExceptionToWorkflowExceptionOld(restFault, mockExecution))
121         }
122
123         @Test
124         public void testMapErrorCode5010(){
125                 String msg = "Connect to njcdtl20ew2988:8070  failed: Connection refused: connect"
126                 ExceptionUtil util = new ExceptionUtil()
127                 Assert.assertEquals("5010",util.MapErrorCode(msg))
128         }
129
130         @Test
131         public void testMapErrorCode5020(){
132                 String msg = "Connection timed out"
133                 ExceptionUtil util = new ExceptionUtil()
134                 Assert.assertEquals("5020",util.MapErrorCode(msg))
135         }
136
137         @Test
138         public void testMapCategoryToErrorCode5100 () {
139                 String errorCategory = "OPENSTACK"
140                 ExceptionUtil util = new ExceptionUtil()
141                 Assert.assertEquals('5100', util.MapCategoryToErrorCode(errorCategory))
142         }
143
144         @Test
145         public void testMapCategoryToErrorCode5110 () {
146                 String errorCategory = "IO"
147                 ExceptionUtil util = new ExceptionUtil()
148                 Assert.assertEquals('5110', util.MapCategoryToErrorCode(errorCategory))
149         }
150
151         @Test
152         public void testMapCategoryToErrorCode7020 () {
153                 String errorCategory = "USERDATA"
154                 ExceptionUtil util = new ExceptionUtil()
155                 Assert.assertEquals('7020', util.MapCategoryToErrorCode(errorCategory))
156         }
157
158         @Test
159         @Ignore
160         public void testMapAdapterExecptionToWorkflowException_UserDataErrorMessage () {
161                 String response = """<ns2:TenantException xmlns:ns2=\"http://org.openecomp.mso/tenant\"><category>USERDATA</category><message>Cloud Site [CHCGILNE001] not found</message></ns2:TenantException>""" as String
162                 String expected = """WorkflowException[processKey=ExceptionUtil,errorCode=7020,errorMessage=Received error from Tenant Adapter: Cloud Site [CHCGILNE001] not found]""" as String
163
164                 ExecutionEntity mockExecution = mock(ExecutionEntity.class)
165                 when(mockExecution.getVariable("prefix")).thenReturn("test_")
166                 when(mockExecution.getVariable("testProcessKey")).thenReturn("ExceptionUtil")
167
168                 ExceptionUtil util = new ExceptionUtil()
169                 String actual = util.MapAdapterExecptionToWorkflowException(response, mockExecution, "Tenant Adapter")
170
171                 verify(mockExecution).setVariable("WorkflowException",expected)
172         }
173
174         @Test
175         @Ignore
176         public void testMapAdapterExecptionToWorkflowException_IOErrorMessage () {
177                 String response = """<ns2:TenantException xmlns:ns2=\"http://org.openecomp.mso/tenant\"><category>IO</category><message>[TokenAuth] Connect to test.aic.cip.att.com:5000 [test.aic.cip.att.com/300.200.100.100] failed: Connection refused</message></ns2:TenantException>""" as String
178                 String expected = """WorkflowException[processKey=ExceptionUtil,errorCode=5110,errorMessage=Could not communicate with Platform Orchestrator]""" as String
179
180                 ExecutionEntity mockExecution = mock(ExecutionEntity.class)
181                 when(mockExecution.getVariable("prefix")).thenReturn("test_")
182                 when(mockExecution.getVariable("testProcessKey")).thenReturn("ExceptionUtil")
183
184                 ExceptionUtil util = new ExceptionUtil()
185                 String actual = util.MapAdapterExecptionToWorkflowException(response, mockExecution, "Tenant Adapter")
186
187                 verify(mockExecution).setVariable("WorkflowException",expected)
188         }
189
190 }