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