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