Springboot 2.0 upgrade
[so.git] / bpmn / MSOCommonBPMN / src / test / groovy / org / onap / so / bpmn / common / scripts / SDNCAdapterUtilsTest.groovy
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.bpmn.common.scripts;
22
23 import static org.junit.Assert.*;
24 import static org.mockito.Mockito.*
25
26 import org.junit.Before
27 import org.junit.Ignore
28 import org.junit.Test
29 import org.junit.runner.RunWith
30 import org.camunda.bpm.engine.delegate.BpmnError
31 import org.camunda.bpm.engine.impl.persistence.entity.ExecutionEntity
32 import org.camunda.bpm.engine.delegate.DelegateExecution
33 import org.onap.so.bpmn.core.WorkflowException
34 import org.onap.so.bpmn.common.scripts.SDNCAdapterUtils
35
36 import org.onap.so.bpmn.mock.FileUtil
37
38 import org.mockito.junit.MockitoJUnitRunner;
39
40 @RunWith(MockitoJUnitRunner.Silent.class)
41 public class SDNCAdapterUtilsTest {
42         
43         private def map
44         private ExecutionEntity svcex
45         private WorkflowException wfex
46         private AbstractServiceTaskProcessor tp
47         private String resp
48         private SDNCAdapterUtils utils
49         
50         @Before
51         public void init()
52         {
53                 map = new HashMap<String,Object>()
54                 svcex = mock(ExecutionEntity.class)
55                 wfex = null
56                 tp = new AbstractServiceTaskProcessor() {
57                         @Override
58                         public void preProcessRequest(DelegateExecution execution) {
59                         }
60                 };
61                 utils = new SDNCAdapterUtils(tp)
62                 
63                 // svcex gets its variables from "map"
64                 when(svcex.getVariable(any())).thenAnswer(
65                         { invocation ->
66                                 return map.get(invocation.getArgument(0)) })
67                 
68                 // svcex puts its variables into "map"
69                 when(svcex.setVariable(any(), any())).thenAnswer(
70                         { invocation ->
71                                 return map.put(
72                                                         invocation.getArgument(0),
73                                                         invocation.getArgument(1)) })
74                 
75                 map.put("isDebugLogEnabled", "true")
76                 map.put("prefix", "mypfx-")
77                 map.put("testProcessKey", "mykey")
78         }
79                                                                                                 
80         @Test
81         public void testValidateSDNCResponse_Success_NoCode() {
82                 resp = """<no-response/>"""
83                 
84                 utils.validateSDNCResponse(svcex, resp, wfex, true)
85                 
86                 assertEquals(true, map.get("mypfx-sdncResponseSuccess"))
87                 assertEquals("0", map.get("mypfx-sdncRequestDataResponseCode"))
88                 assertFalse(map.containsKey("WorkflowException"))
89         }
90                                                                                                 
91         @Test
92         public void testValidateSDNCResponse_200() {
93                 utils.validateSDNCResponse(svcex, makeResp("200", "OK", ""), wfex, true)
94                 
95                 assertEquals(true, map.get("mypfx-sdncResponseSuccess"))
96                 assertEquals("200", map.get("mypfx-sdncRequestDataResponseCode"))
97                 assertFalse(map.containsKey("WorkflowException"))
98         }
99                                                                                                 
100         @Test
101         public void testValidateSDNCResponse_408() {
102                 try {
103                         utils.validateSDNCResponse(svcex, makeResp("408", "failed", ""), wfex, true)
104                         
105                         // this has been commented out as, currently, the code doesn't
106                         // throw an exception in this case
107 //                      fail("missing exception")
108                         
109                 } catch(BpmnError ex) {
110                         ex.printStackTrace()
111                 }
112                 
113                 assertEquals(false, map.get("mypfx-sdncResponseSuccess"))
114                 assertEquals("408", map.get("mypfx-sdncRequestDataResponseCode"))
115                 
116                 wfex = map.get("WorkflowException")
117                 assertNotNull(wfex)
118                 
119                 assertEquals(5320, wfex.getErrorCode())
120                 assertEquals("Received error from SDN-C: failed", wfex.getErrorMessage())
121         }
122                                                                                                 
123         @Test
124         public void testValidateSDNCResponse_408_200() {
125                 
126                 utils.validateSDNCResponse(svcex, makeResp("408", "failed", makeReq("200", "ok")), wfex, true)
127                 
128                 assertEquals(true       , map.get("mypfx-sdncResponseSuccess"))
129                 assertEquals("200", map.get("mypfx-sdncRequestDataResponseCode"))               
130                 assertFalse(map.containsKey("WorkflowException"))
131         }
132                                                                                                 
133         @Test
134         public void testValidateSDNCResponse_408_200_WithEmbeddedLt() {
135                 
136                 utils.validateSDNCResponse(svcex, makeResp("408", "failed", makeReq("200", "<success> message")), wfex, true)
137                 
138                 assertEquals(true, map.get("mypfx-sdncResponseSuccess"))
139                 assertEquals("200", map.get("mypfx-sdncRequestDataResponseCode"))               
140                 assertFalse(map.containsKey("WorkflowException"))
141         }
142         
143         @Test
144         public void testUpdateHomingInfo() {
145                 String actual = utils.updateHomingInfo(null, "AIC3.0")
146                 println actual
147                 assertEquals("<l2-homing-information><aic-version>AIC3.0</aic-version></l2-homing-information>", actual)
148         }
149         
150         @Test
151         public void testUpdateHomingInfo2() {
152                 String homingInfo = "<l2-homing-information><preferred-aic-clli>TESTCLLI</preferred-aic-clli></l2-homing-information>" 
153                 String actual = utils.updateHomingInfo(homingInfo, "AIC3.0")
154                 println actual
155                 assertEquals("<l2-homing-information><preferred-aic-clli>TESTCLLI</preferred-aic-clli><aic-version>AIC3.0</aic-version></l2-homing-information>", actual)
156         }
157         
158         private String makeResp(String respcode, String respmsg, String reqdata) {
159                 def rc = respcode
160                 def rm = respmsg
161                 
162                 return """
163 <sdncadapterworkflow:SDNCAdapterWorkflowResponse xmlns:sdncadapterworkflow="http://org.onap/so/workflow/schema/v1"
164                                                  xmlns:tag0="http://org.onap/workflow/sdnc/adapter/schema/v1"
165                                                  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
166    <sdncadapterworkflow:response-data>
167       <tag0:CallbackHeader>
168          <tag0:RequestId>myreq</tag0:RequestId>
169          <tag0:ResponseCode>${MsoUtils.xmlEscape(rc)}</tag0:ResponseCode>
170          <tag0:ResponseMessage>${MsoUtils.xmlEscape(rm)}</tag0:ResponseMessage>
171       </tag0:CallbackHeader>
172           ${reqdata}
173    </sdncadapterworkflow:response-data>
174 </sdncadapterworkflow:SDNCAdapterWorkflowResponse>
175 """
176         
177         }
178         
179         private String makeReq(String respcode, String respmsg) {
180                 def rc = respcode
181                 def rm = respmsg
182                 
183                 def output = """
184 <output xmlns="org:onap:sdnc:northbound:generic-resource">
185                 <svc-request-id>8b46e36e-b44f-4085-9404-427be1bc8a3</svc-request-id>
186                 <response-code>${MsoUtils.xmlEscape(rc)}</response-code>
187                 <response-message>${MsoUtils.xmlEscape(rm)}</response-message>
188                 <ack-final-indicator>Y</ack-final-indicator>
189 </output>
190 """
191                 output = output
192                 
193                 return """<tag0:RequestData xsi:type="xs:string">${MsoUtils.xmlEscape(output)}</tag0:RequestData>"""
194         }
195 }