1710 Rebase - Second Attempt
[so.git] / bpmn / MSOCommonBPMN / src / test / java / org / openecomp / mso / bpmn / common / WorkflowTest.java
index 49ad6d6..4deac22 100644 (file)
@@ -1,22 +1,22 @@
-/*- \r
- * ============LICENSE_START======================================================= \r
- * OPENECOMP - MSO \r
- * ================================================================================ \r
- * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. \r
- * ================================================================================ \r
- * Licensed under the Apache License, Version 2.0 (the "License"); \r
- * you may not use this file except in compliance with the License. \r
- * You may obtain a copy of the License at \r
- * \r
- *      http://www.apache.org/licenses/LICENSE-2.0 \r
- * \r
- * Unless required by applicable law or agreed to in writing, software \r
- * distributed under the License is distributed on an "AS IS" BASIS, \r
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. \r
- * See the License for the specific language governing permissions and \r
- * limitations under the License. \r
- * ============LICENSE_END========================================================= \r
- */ \r
+/*-\r
+ * ============LICENSE_START=======================================================\r
+ * OPENECOMP - MSO\r
+ * ================================================================================\r
+ * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.\r
+ * ================================================================================\r
+ * Licensed under the Apache License, Version 2.0 (the "License");\r
+ * you may not use this file except in compliance with the License.\r
+ * You may obtain a copy of the License at\r
+ *\r
+ *      http://www.apache.org/licenses/LICENSE-2.0\r
+ *\r
+ * Unless required by applicable law or agreed to in writing, software\r
+ * distributed under the License is distributed on an "AS IS" BASIS,\r
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ * See the License for the specific language governing permissions and\r
+ * limitations under the License.\r
+ * ============LICENSE_END=========================================================\r
+ */\r
 \r
 package org.openecomp.mso.bpmn.common;\r
 \r
@@ -60,6 +60,8 @@ import org.camunda.bpm.engine.variable.impl.VariableMapImpl;
 import org.custommonkey.xmlunit.DetailedDiff;\r
 import org.custommonkey.xmlunit.XMLUnit;\r
 import org.jboss.resteasy.spi.AsynchronousResponse;\r
+import org.json.JSONArray;\r
+import org.json.JSONObject;\r
 import org.junit.Before;\r
 import org.junit.Rule;\r
 import org.openecomp.mso.bpmn.common.adapter.sdnc.CallbackHeader;\r
@@ -73,11 +75,15 @@ import org.openecomp.mso.bpmn.common.adapter.vnf.UpdateVnfNotification;
 import org.openecomp.mso.bpmn.common.adapter.vnf.VnfRollback;\r
 import org.openecomp.mso.bpmn.common.workflow.service.SDNCAdapterCallbackServiceImpl;\r
 import org.openecomp.mso.bpmn.common.workflow.service.VnfAdapterNotifyServiceImpl;\r
-import org.openecomp.mso.bpmn.common.workflow.service.WorkflowAsyncCommonResource;\r
+import org.openecomp.mso.bpmn.common.workflow.service.WorkflowAsyncResource;\r
 import org.openecomp.mso.bpmn.common.workflow.service.WorkflowMessageResource;\r
 import org.openecomp.mso.bpmn.common.workflow.service.WorkflowResponse;\r
 import org.openecomp.mso.bpmn.core.CamundaDBSetup;\r
 import org.openecomp.mso.bpmn.core.PropertyConfigurationSetup;\r
+import org.openecomp.mso.bpmn.core.domain.Resource;\r
+import org.openecomp.mso.bpmn.core.domain.ServiceDecomposition;\r
+\r
+import static org.openecomp.mso.bpmn.core.json.JsonUtils.*;\r
 import org.w3c.dom.Document;\r
 import org.w3c.dom.Element;\r
 import org.w3c.dom.Node;\r
@@ -119,6 +125,7 @@ public class WorkflowTest {
         */\r
        protected static final String JSON = "application/json; charset=UTF-8";\r
 \r
+\r
        /**\r
         * Constructor.\r
         */\r
@@ -265,7 +272,7 @@ public class WorkflowTest {
                VariableMapImpl variableMapImpl = createVariableMapImpl(variables);\r
 \r
                System.out.println("Sending " + request + " to " + processKey + " process");\r
-               WorkflowAsyncCommonResource workflowResource = new WorkflowAsyncCommonResource();\r
+               WorkflowAsyncResource workflowResource = new WorkflowAsyncResource();\r
                workflowResource.setProcessEngineServices4junit(processEngineRule);\r
 \r
                TestAsyncResponse asyncResponse = new TestAsyncResponse();\r
@@ -298,7 +305,7 @@ public class WorkflowTest {
                VariableMapImpl variableMapImpl = createVariableMapImpl(variables);\r
 \r
                System.out.println("Sending " + request + " to " + processKey + " process");\r
-               WorkflowAsyncCommonResource workflowResource = new WorkflowAsyncCommonResource();\r
+               WorkflowAsyncResource workflowResource = new WorkflowAsyncResource();\r
                workflowResource.setProcessEngineServices4junit(processEngineRule);\r
 \r
                TestAsyncResponse asyncResponse = new TestAsyncResponse();\r
@@ -619,6 +626,18 @@ public class WorkflowTest {
                                content = callbackData.getContent();\r
                                respCode = 200;\r
                                respMsg = "OK";\r
+                       } else if ("CREATED".equals(modifier)) {\r
+                               CallbackData callbackData = callbacks.get(action);\r
+\r
+                               if (callbackData == null) {\r
+                                       String msg = "No callback defined for '" + action + "' SDNC request";\r
+                                       System.out.println(msg);\r
+                                       fail(msg);\r
+                               }\r
+\r
+                               content = callbackData.getContent();\r
+                               respCode = 201;\r
+                               respMsg = "Created";\r
                        } else if ("ERR".equals(modifier)) {\r
                                content = "<svc-request-id>((REQUEST-ID))</svc-request-id><response-code>500</response-code><response-message>SIMULATED ERROR FROM SDNC ADAPTER</response-message>";\r
                                respCode = 500;\r
@@ -1385,6 +1404,127 @@ public class WorkflowTest {
                return true;\r
        }\r
 \r
+       /**\r
+        * Runs a program to inject sniro workflow messages into the test environment.\r
+        * A program is essentially just a list of keys that identify event data\r
+        * to be injected, in sequence. For more details, see\r
+        * injectSNIROCallbacks(String contentType, String messageType, String content, long timeout)\r
+        *\r
+        * Errors are handled with junit assertions and will cause the test to fail.\r
+        * NOTE: Each callback must have a workflow message type associated with it.\r
+        *\r
+        * @param callbacks an object containing event data for the program\r
+        * @param program the program to execute\r
+        */\r
+       protected void injectSNIROCallbacks(CallbackSet callbacks, String program) {\r
+\r
+               String[] cmds = program.replaceAll("\\s+", "").split(",");\r
+\r
+               for (String cmd : cmds) {\r
+                       String action = cmd;\r
+                       String modifier = "STD";\r
+\r
+                       if (cmd.contains(":")) {\r
+                               String[] parts = cmd.split(":");\r
+                               action = parts[0];\r
+                               modifier = parts[1];\r
+                       }\r
+\r
+                       String messageType = null;\r
+                       String content = null;\r
+                       String contentType = null;\r
+\r
+                       if ("STD".equals(modifier)) {\r
+                               CallbackData callbackData = callbacks.get(action);\r
+\r
+                               if (callbackData == null) {\r
+                                       String msg = "No '" + action + "' workflow message callback is defined";\r
+                                       System.out.println(msg);\r
+                                       fail(msg);\r
+                               }\r
+\r
+                               messageType = callbackData.getMessageType();\r
+\r
+                               if (messageType == null || messageType.trim().equals("")) {\r
+                                       String msg = "No workflow message type is defined in the '" + action + "' callback";\r
+                                       System.out.println(msg);\r
+                                       fail(msg);\r
+                               }\r
+\r
+                               content = callbackData.getContent();\r
+                               contentType = callbackData.getContentType();\r
+                       } else {\r
+                               String msg = "Invalid workflow message program modifier: '" + modifier + "'";\r
+                               System.out.println(msg);\r
+                               fail(msg);\r
+                       }\r
+\r
+                       if (!injectSNIROCallbacks(contentType, messageType, content, 10000)) {\r
+                               fail("Failed to inject '" + action + "' workflow message");\r
+                       }\r
+\r
+                       try {\r
+                               Thread.sleep(1000);\r
+                       } catch (InterruptedException e) {\r
+                               fail("Interrupted after injection of '" + action + "' workflow message");\r
+                       }\r
+               }\r
+       }\r
+\r
+       /**\r
+        * Injects a sniro workflow message. The specified callback response may\r
+        * contain the placeholder strings ((CORRELATOR)) and ((SERVICE_RESOURCE_ID))\r
+        * The ((CORRELATOR)) is replaced with the actual correlator value from the\r
+        * request. The ((SERVICE_RESOURCE_ID)) is replaced with the actual serviceResourceId\r
+        * value from the sniro request. Currently this only works with sniro request\r
+        * that contain only 1 resource.\r
+        *\r
+        * @param contentType the HTTP contentType for the message (possibly null)\r
+        * @param messageType the message type\r
+        * @param content the message content (possibly null)\r
+        * @param timeout the timeout in milliseconds\r
+        * @return true if the message could be injected, false otherwise\r
+        */\r
+       protected boolean injectSNIROCallbacks(String contentType, String messageType, String content, long timeout) {\r
+               String correlator = (String) getProcessVariable("ReceiveWorkflowMessage",\r
+                       messageType + "_CORRELATOR", timeout);\r
+\r
+               if (correlator == null) {\r
+                       return false;\r
+               }\r
+               if (content != null) {\r
+                       content = content.replace("((CORRELATOR))", correlator);\r
+                       if(messageType.equalsIgnoreCase("SNIROResponse")){\r
+                               //TODO figure out a solution for when there is more than 1 resource being homed (i.e. more than 1 reason in the placement list)\r
+                               ServiceDecomposition decomp = (ServiceDecomposition) getProcessVariable("Homing", "serviceDecomposition", timeout);\r
+                               List<Resource> resourceList = decomp.getServiceResources();\r
+                               if(resourceList.size() == 1){\r
+                                       String resourceId = "";\r
+                                       for(Resource resource:resourceList){\r
+                                               resourceId = resource.getResourceId();\r
+                                       }\r
+                                       String homingList = getJsonValue(content, "solutionInfo.placement");\r
+                                       JSONArray placementArr = new JSONArray(homingList);\r
+                                       if(placementArr.length() == 1){\r
+                                               content = content.replace("((SERVICE_RESOURCE_ID))", resourceId);\r
+                                       }\r
+                                       String licenseInfoList = getJsonValue(content, "solutionInfo.licenseInfo");\r
+                                       JSONArray licenseArr = new JSONArray(licenseInfoList);\r
+                                       if(licenseArr.length() == 1){\r
+                                               content = content.replace("((SERVICE_RESOURCE_ID))", resourceId);\r
+                                       }\r
+                               }\r
+                       }\r
+               }\r
+               System.out.println("Injecting " + messageType + " message");\r
+               WorkflowMessageResource workflowMessageResource = new WorkflowMessageResource();\r
+               workflowMessageResource.setProcessEngineServices4junit(processEngineRule);\r
+               Response response = workflowMessageResource.deliver(contentType, messageType, correlator, content);\r
+               System.out.println("Workflow response to " + messageType + " message: " + response);\r
+               return true;\r
+       }\r
+\r
+\r
        /**\r
         * Wait for the process to end.\r
         * @param businessKey the process business key\r
@@ -1477,8 +1617,6 @@ public class WorkflowTest {
        }\r
 \r
        /**\r
-        * @author cb645j\r
-        *\r
         * Gets the value of a subflow variable from the specified subflow's\r
         * historical process instance.\r
         *\r
@@ -1517,8 +1655,6 @@ public class WorkflowTest {
        }\r
 \r
        /**\r
-        * @author cb645j\r
-        *\r
         * Gets the value of a subflow variable from the subflow's\r
         * historical process x instance.\r
         *\r