Merge "Reorder modifiers"
[so.git] / bpmn / MSOCommonBPMN / src / test / java / org / openecomp / mso / bpmn / common / SDNCAdapterRestV2Test.java
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.openecomp.mso.bpmn.common;\r
22 \r
23 import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;\r
24 import static com.github.tomakehurst.wiremock.client.WireMock.post;\r
25 import static com.github.tomakehurst.wiremock.client.WireMock.stubFor;\r
26 import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo;\r
27 \r
28 import java.io.IOException;\r
29 import java.util.HashMap;\r
30 import java.util.Map;\r
31 import java.util.UUID;\r
32 \r
33 import javax.ws.rs.core.Response;\r
34 \r
35 import org.camunda.bpm.engine.test.Deployment;\r
36 import org.junit.Assert;\r
37 import org.junit.Test;\r
38 import org.openecomp.mso.bpmn.common.WorkflowTest;\r
39 import org.openecomp.mso.bpmn.common.workflow.service.WorkflowMessageResource;\r
40 import org.openecomp.mso.bpmn.mock.FileUtil;\r
41 \r
42 /**\r
43  * Unit tests for SDNCAdapterRestV2.bpmn.\r
44  * \r
45  * This version of SDNCAdapterRest allows for interim notifications to be sent for\r
46  * any non-final response received from SDNC.\r
47  */\r
48 public class SDNCAdapterRestV2Test extends WorkflowTest {\r
49                 \r
50         private final CallbackSet callbacks = new CallbackSet();\r
51         \r
52         /**\r
53          * Constructor. Insert callbacks.\r
54          * \r
55          * @throws IOException\r
56          */\r
57         public SDNCAdapterRestV2Test() throws IOException {\r
58                 String sdncCallbackFinal = FileUtil.readResourceFile("__files/SDNCAdapterRestCallbackFinal.json");\r
59                 String sdncCallbackNonFinal = FileUtil.readResourceFile("__files/SDNCAdapterRestCallbackNonFinal.json");\r
60                 callbacks.put("nonfinal", sdncCallbackNonFinal);\r
61                 callbacks.put("final", sdncCallbackFinal);\r
62         }\r
63 \r
64         /**\r
65          * Test the success path through the subflow.\r
66          */\r
67         @Test\r
68         @Deployment(resources = {\r
69                         "subprocess/SDNCAdapterRestV2.bpmn",\r
70                         "subprocess/GenericNotificationService.bpmn"            \r
71                 })\r
72         public void success() throws IOException {\r
73                 logStart();\r
74                 mocks();\r
75 \r
76                 String businessKey = UUID.randomUUID().toString();\r
77                 Map<String, Object> variables = new HashMap<String, Object>();\r
78                 variables.put("mso-request-id", "a4383a52-b9de-4bc4-bedf-02f3f9466535");\r
79                 variables.put("mso-service-instance-id", "fd8bcdbb-b799-43ce-a7ff-ed8f2965a3b5");\r
80                 variables.put("isDebugLogEnabled", "true");\r
81                 variables.put("SDNCREST_Request",\r
82                         FileUtil.readResourceFile("__files/SDNCAdapterRestV2Request.json"));\r
83                 variables.put("SDNCREST_InterimNotification1",\r
84                         FileUtil.readResourceFile("__files/SDNCInterimNotification1.json"));\r
85                 \r
86                 invokeSubProcess("SDNCAdapterRestV2", businessKey, variables);\r
87 \r
88                 injectSDNCRestCallbacks(callbacks, "nonfinal");\r
89 \r
90                 // First non-final response will have done a notification\r
91                 Object interimNotification = getVariableFromHistory(businessKey, "SDNCREST_interimNotification");\r
92                 Assert.assertNotNull(interimNotification);\r
93                 \r
94                 injectSDNCRestCallbacks(callbacks, "nonfinal");\r
95                 \r
96                 // Second non-final response will not have done a notification\r
97                 interimNotification = getVariableFromHistory(businessKey, "SDNCREST_interimNotification");\r
98                 Assert.assertNull(interimNotification);\r
99 \r
100                 injectSDNCRestCallbacks(callbacks, "final");\r
101                 \r
102                 interimNotification = this.getVariableFromHistory(businessKey, "SDNCREST_interimNotification");\r
103                 Assert.assertNull(interimNotification);\r
104 \r
105                 waitForProcessEnd(businessKey, 10000);\r
106 \r
107                 Assert.assertTrue(isProcessEnded(businessKey));\r
108                 \r
109                 logEnd();\r
110         }\r
111 \r
112         /**\r
113          * Injects a single SDNC adapter callback request. The specified callback data\r
114          * may contain the placeholder string ((REQUEST-ID)) which is replaced with\r
115          * the actual SDNC request ID. Note: this is not the requestId in the original\r
116          * MSO request.\r
117          * @param contentType the HTTP content type for the callback\r
118          * @param content the content of the callback\r
119          * @param timeout the timeout in milliseconds\r
120          * @return true if the callback could be injected, false otherwise\r
121          */\r
122         @Override\r
123         protected boolean injectSDNCRestCallback(String contentType, String content, long timeout) {\r
124                 String sdncRequestId = (String) getProcessVariable("SDNCAdapterRestV2",\r
125                         "SDNCAResponse_CORRELATOR", timeout);\r
126 \r
127                 if (sdncRequestId == null) {\r
128                         return false;\r
129                 }\r
130 \r
131                 content = content.replace("((REQUEST-ID))", sdncRequestId);\r
132                 // Deprecated usage.  All test code should switch to the (( ... )) syntax.\r
133                 content = content.replace("{{REQUEST-ID}}", sdncRequestId);\r
134 \r
135                 System.out.println("Injecting SDNC adapter callback");\r
136                 WorkflowMessageResource workflowMessageResource = new WorkflowMessageResource();\r
137                 workflowMessageResource.setProcessEngineServices4junit(processEngineRule);\r
138                 Response response = workflowMessageResource.deliver(contentType, "SDNCAResponse", sdncRequestId, content);\r
139                 System.out.println("Workflow response to SDNC adapter callback: " + response);\r
140                 return true;\r
141         }\r
142 \r
143         /**\r
144          * Defines WireMock stubs needed by these tests.\r
145          */\r
146         private void mocks() {\r
147                 stubFor(post(urlEqualTo("/SDNCAdapter/v1/sdnc/services"))\r
148                         .willReturn(aResponse()\r
149                                 .withStatus(202)\r
150                                 .withHeader("Content-Type", "application/json")));\r
151         }\r
152 }\r