Merge "Reorder modifiers"
[so.git] / bpmn / MSOCommonBPMN / src / test / groovy / org / openecomp / mso / bpmn / common / scripts / SDNCAdapterUtilsTest.groovy
1 /*- \r
2  * ============LICENSE_START======================================================= \r
3  * ONAP - SO \r
4  * ================================================================================ \r
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. \r
6  * ================================================================================ \r
7  * Licensed under the Apache License, Version 2.0 (the "License"); \r
8  * you may not use this file except in compliance with the License. \r
9  * You may obtain a copy of the License at \r
10  * \r
11  *      http://www.apache.org/licenses/LICENSE-2.0 \r
12  * \r
13  * Unless required by applicable law or agreed to in writing, software \r
14  * distributed under the License is distributed on an "AS IS" BASIS, \r
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. \r
16  * See the License for the specific language governing permissions and \r
17  * limitations under the License. \r
18  * ============LICENSE_END========================================================= \r
19  */ \r
20 \r
21 package org.openecomp.mso.bpmn.common.scripts;\r
22 \r
23 import static org.junit.Assert.*;\r
24 import static org.mockito.Mockito.*\r
25 \r
26 import org.junit.Before\r
27 import org.junit.Ignore\r
28 import org.junit.Test\r
29 import org.camunda.bpm.engine.delegate.BpmnError\r
30 import org.camunda.bpm.engine.impl.persistence.entity.ExecutionEntity\r
31 import org.camunda.bpm.engine.delegate.DelegateExecution\r
32 import org.openecomp.mso.bpmn.core.WorkflowException\r
33 import org.openecomp.mso.bpmn.common.scripts.SDNCAdapterUtils\r
34 \r
35 import org.openecomp.mso.bpmn.mock.FileUtil\r
36 \r
37 public class SDNCAdapterUtilsTest {\r
38         \r
39         private def map\r
40         private ExecutionEntity svcex\r
41         private WorkflowException wfex\r
42         private AbstractServiceTaskProcessor tp\r
43         private String resp\r
44         private SDNCAdapterUtils utils\r
45         \r
46         @Before\r
47         public void init()\r
48         {\r
49                 map = new HashMap<String,Object>()\r
50                 svcex = mock(ExecutionEntity.class)\r
51                 wfex = null\r
52                 tp = new AbstractServiceTaskProcessor() {\r
53                         @Override\r
54                         public void preProcessRequest(DelegateExecution execution) {\r
55                         }\r
56                 };\r
57                 utils = new SDNCAdapterUtils(tp)\r
58                 \r
59                 // svcex gets its variables from "map"\r
60                 when(svcex.getVariable(any())).thenAnswer(\r
61                         { invocation ->\r
62                                 return map.get(invocation.getArgumentAt(0, String.class)) })\r
63                 \r
64                 // svcex puts its variables into "map"\r
65                 when(svcex.setVariable(any(), any())).thenAnswer(\r
66                         { invocation ->\r
67                                 return map.put(\r
68                                                         invocation.getArgumentAt(0, String.class),\r
69                                                         invocation.getArgumentAt(1, String.class)) })\r
70                 \r
71                 map.put("isDebugLogEnabled", "true")\r
72                 map.put("prefix", "mypfx-")\r
73                 map.put("testProcessKey", "mykey")\r
74         }\r
75                                                                                                 \r
76         @Test\r
77         public void testValidateSDNCResponse_Success_NoCode() {\r
78                 resp = """<no-response/>"""\r
79                 \r
80                 utils.validateSDNCResponse(svcex, resp, wfex, true)\r
81                 \r
82                 assertEquals(true, map.get("mypfx-sdncResponseSuccess"))\r
83                 assertEquals("0", map.get("mypfx-sdncRequestDataResponseCode"))\r
84                 assertFalse(map.containsKey("WorkflowException"))\r
85         }\r
86                                                                                                 \r
87         @Test\r
88         public void testValidateSDNCResponse_200() {\r
89                 utils.validateSDNCResponse(svcex, makeResp("200", "OK", ""), wfex, true)\r
90                 \r
91                 assertEquals(true, map.get("mypfx-sdncResponseSuccess"))\r
92                 assertEquals("200", map.get("mypfx-sdncRequestDataResponseCode"))\r
93                 assertFalse(map.containsKey("WorkflowException"))\r
94         }\r
95                                                                                                 \r
96         @Test\r
97         public void testValidateSDNCResponse_408() {\r
98                 try {\r
99                         utils.validateSDNCResponse(svcex, makeResp("408", "failed", ""), wfex, true)\r
100                         \r
101                         // this has been commented out as, currently, the code doesn't\r
102                         // throw an exception in this case\r
103 //                      fail("missing exception")\r
104                         \r
105                 } catch(BpmnError ex) {\r
106                         ex.printStackTrace()\r
107                 }\r
108                 \r
109                 assertEquals(false, map.get("mypfx-sdncResponseSuccess"))\r
110                 assertEquals("408", map.get("mypfx-sdncRequestDataResponseCode"))\r
111                 \r
112                 wfex = map.get("WorkflowException")\r
113                 assertNotNull(wfex)\r
114                 \r
115                 assertEquals(5320, wfex.getErrorCode())\r
116                 assertEquals("Received error from SDN-C: failed", wfex.getErrorMessage())\r
117         }\r
118                                                                                                 \r
119         @Test\r
120         public void testValidateSDNCResponse_408_200() {\r
121                 \r
122                 utils.validateSDNCResponse(svcex, makeResp("408", "failed", makeReq("200", "ok")), wfex, true)\r
123                 \r
124                 assertEquals(true, map.get("mypfx-sdncResponseSuccess"))\r
125                 assertEquals("200", map.get("mypfx-sdncRequestDataResponseCode"))               \r
126                 assertFalse(map.containsKey("WorkflowException"))\r
127         }\r
128 \r
129         @Ignore // 1802 merge                                                                                           \r
130         @Test\r
131         public void testValidateSDNCResponse_408_200_WithEmbeddedLt() {\r
132                 \r
133                 utils.validateSDNCResponse(svcex, makeResp("408", "failed", makeReq("200", "<success> message")), wfex, true)\r
134                 \r
135                 assertEquals(true, map.get("mypfx-sdncResponseSuccess"))\r
136                 assertEquals("200", map.get("mypfx-sdncRequestDataResponseCode"))               \r
137                 assertFalse(map.containsKey("WorkflowException"))\r
138         }\r
139         \r
140         @Test\r
141         public void testUpdateHomingInfo() {\r
142                 String actual = utils.updateHomingInfo(null, "AIC3.0")\r
143                 println actual\r
144                 assertEquals("<l2-homing-information><aic-version>AIC3.0</aic-version></l2-homing-information>", actual)\r
145         }\r
146         \r
147         @Test\r
148         public void testUpdateHomingInfo2() {\r
149                 String homingInfo = "<l2-homing-information><preferred-aic-clli>TESTCLLI</preferred-aic-clli></l2-homing-information>" \r
150                 String actual = utils.updateHomingInfo(homingInfo, "AIC3.0")\r
151                 println actual\r
152                 assertEquals("<l2-homing-information><preferred-aic-clli>TESTCLLI</preferred-aic-clli><aic-version>AIC3.0</aic-version></l2-homing-information>", actual)\r
153         }\r
154         \r
155         @Ignore // 1802 merge - testing method that doesn't exist\r
156         @Test\r
157         public void testUpdateServiceInfo() {\r
158                 String actual = utils.updateServiceInfo(null, "96688f6f-ab06-4ef6-ae55-9d3af28ae909")\r
159                 println actual\r
160                 assertEquals("<service-information><infra-service-instance-id>96688f6f-ab06-4ef6-ae55-9d3af28ae909</infra-service-instance-id></service-information>", actual)\r
161         }\r
162         \r
163         @Ignore // 1802 merge - testing method that doesn't exist\r
164         @Test\r
165         public void testUpdateServiceInfo2() {\r
166                 String serviceInfo = "<service-information><service-type>SDN-ETHERNET-INTERNET</service-type><service-instance-id>MIS/1602/00029/SB_INTERNET</service-instance-id></service-information>"\r
167                 String actual = utils.updateServiceInfo(serviceInfo, "96688f6f-ab06-4ef6-ae55-9d3af28ae909")\r
168                 println actual\r
169                 assertEquals("<service-information><service-type>SDN-ETHERNET-INTERNET</service-type><service-instance-id>MIS/1602/00029/SB_INTERNET</service-instance-id><infra-service-instance-id>96688f6f-ab06-4ef6-ae55-9d3af28ae909</infra-service-instance-id></service-information>", actual)\r
170         }\r
171         \r
172         private String makeResp(String respcode, String respmsg, String reqdata) {\r
173                 def rc = encodeXml(respcode)\r
174                 def rm = encodeXml(respmsg)\r
175                 \r
176                 return """\r
177 <sdncadapterworkflow:SDNCAdapterWorkflowResponse xmlns:sdncadapterworkflow="http://org.openecomp/mso/workflow/schema/v1"\r
178                                                  xmlns:tag0="http://org.openecomp/workflow/sdnc/adapter/schema/v1"\r
179                                                  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">\r
180    <sdncadapterworkflow:response-data>\r
181       <tag0:CallbackHeader>\r
182          <tag0:RequestId>myreq</tag0:RequestId>\r
183          <tag0:ResponseCode>${rc}</tag0:ResponseCode>\r
184          <tag0:ResponseMessage>${rm}</tag0:ResponseMessage>\r
185       </tag0:CallbackHeader>\r
186           ${reqdata}\r
187    </sdncadapterworkflow:response-data>\r
188 </sdncadapterworkflow:SDNCAdapterWorkflowResponse>\r
189 """\r
190         \r
191         }\r
192         \r
193         private String makeReq(String respcode, String respmsg) {\r
194                 def rc = encodeXml(respcode)\r
195                 def rm = encodeXml(respmsg)\r
196                 \r
197                 def output = """\r
198 <output xmlns="org:onap:sdnc:northbound:generic-resource">\r
199                 <svc-request-id>8b46e36e-b44f-4085-9404-427be1bc8a3</svc-request-id>\r
200                 <response-code>${rc}</response-code>\r
201                 <response-message>${rm}</response-message>\r
202                 <ack-final-indicator>Y</ack-final-indicator>\r
203 </output>\r
204 """\r
205                 output = encodeXml(output)\r
206                 \r
207                 return """<tag0:RequestData xsi:type="xs:string">${output}</tag0:RequestData>"""\r
208         }\r
209         \r
210         private String encodeXml(String txt) {\r
211                 return txt.replace("&", "&amp;").replace("<", "&lt;")\r
212         }\r
213 }\r