2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019 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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.so.apihandlerinfra;
23 import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
24 import static com.github.tomakehurst.wiremock.client.WireMock.get;
25 import static com.github.tomakehurst.wiremock.client.WireMock.urlMatching;
26 import static com.shazam.shazamcrest.matcher.Matchers.sameBeanAs;
27 import static org.junit.Assert.assertEquals;
28 import static org.junit.Assert.assertThat;
30 import java.io.IOException;
31 import java.nio.file.Files;
32 import java.nio.file.Paths;
33 import java.text.ParseException;
34 import java.util.ArrayList;
35 import java.util.List;
36 import javax.ws.rs.core.MediaType;
37 import javax.ws.rs.core.Response;
38 import org.json.JSONException;
39 import org.junit.Test;
40 import org.onap.so.apihandlerinfra.workflowspecificationbeans.WorkflowInputParameter;
41 import org.onap.so.apihandlerinfra.workflowspecificationbeans.WorkflowSpecifications;
42 import org.onap.so.db.catalog.beans.ActivitySpec;
43 import org.onap.so.db.catalog.beans.ActivitySpecUserParameters;
44 import org.onap.so.db.catalog.beans.UserParameters;
45 import org.onap.so.db.catalog.beans.Workflow;
46 import org.onap.so.db.catalog.beans.WorkflowActivitySpecSequence;
47 import org.skyscreamer.jsonassert.JSONAssert;
48 import org.springframework.beans.factory.annotation.Autowired;
49 import org.springframework.beans.factory.annotation.Value;
50 import org.springframework.http.HttpEntity;
51 import org.springframework.http.HttpHeaders;
52 import org.springframework.http.HttpMethod;
53 import org.springframework.http.ResponseEntity;
54 import org.springframework.util.ResourceUtils;
55 import org.springframework.web.util.UriComponentsBuilder;
56 import com.fasterxml.jackson.core.JsonParseException;
57 import com.fasterxml.jackson.databind.DeserializationFeature;
58 import com.fasterxml.jackson.databind.JsonMappingException;
59 import com.fasterxml.jackson.databind.ObjectMapper;
61 public class WorkflowSpecificationsHandlerTest extends BaseTest {
63 WorkflowSpecificationsHandler workflowSpecificationsHandler;
65 @Value("${wiremock.server.port}")
66 private String wiremockPort;
68 private final String basePath = "onap/so/infra/workflowSpecifications/v1/workflows";
71 public void queryWorkflowSpecifications_Test_Success()
72 throws ParseException, JSONException, JsonParseException, JsonMappingException, IOException {
74 HttpHeaders headers = new HttpHeaders();
75 headers.set("Accept", MediaType.APPLICATION_JSON);
76 headers.set("Content-Type", MediaType.APPLICATION_JSON);
77 HttpEntity<String> entity = new HttpEntity<String>(null, headers);
79 wireMockServer.stubFor(get(urlMatching(
80 "/workflow/search/findWorkflowByModelUUID[?]vnfResourceModelUUID=b5fa707a-f55a-11e7-a796-005056856d52"))
81 .willReturn(aResponse().withHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
82 .withBody(getWiremockResponseForCatalogdb("WorkflowSpecificationsQuery_Response.json"))
83 .withStatus(org.apache.http.HttpStatus.SC_OK)));
85 wireMockServer.stubFor(get(urlMatching("/workflow/1/workflowActivitySpecSequence"))
86 .willReturn(aResponse().withHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
87 .withBody(getWiremockResponseForCatalogdb("WorkflowActivitySpecSequence_Response.json"))
88 .withStatus(org.apache.http.HttpStatus.SC_OK)));
90 wireMockServer.stubFor(get(urlMatching("/workflowActivitySpecSequence/1/activitySpec"))
91 .willReturn(aResponse().withHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
92 .withBody(getWiremockResponseForCatalogdb("ActivitySpecSequence1_Response.json"))
93 .withStatus(org.apache.http.HttpStatus.SC_OK)));
95 wireMockServer.stubFor(get(urlMatching("/workflowActivitySpecSequence/2/activitySpec"))
96 .willReturn(aResponse().withHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
97 .withBody(getWiremockResponseForCatalogdb("ActivitySpecSequence2_Response.json"))
98 .withStatus(org.apache.http.HttpStatus.SC_OK)));
100 wireMockServer.stubFor(get(urlMatching("/workflowActivitySpecSequence/3/activitySpec"))
101 .willReturn(aResponse().withHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
102 .withBody(getWiremockResponseForCatalogdb("ActivitySpecSequence3_Response.json"))
103 .withStatus(org.apache.http.HttpStatus.SC_OK)));
105 wireMockServer.stubFor(get(urlMatching("/activitySpec/1/activitySpecUserParameters"))
106 .willReturn(aResponse().withHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
107 .withBody(getWiremockResponseForCatalogdb("ActivitySpecUserParameters1_Response.json"))
108 .withStatus(org.apache.http.HttpStatus.SC_OK)));
110 wireMockServer.stubFor(get(urlMatching("/activitySpec/2/activitySpecUserParameters"))
111 .willReturn(aResponse().withHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
112 .withBody(getWiremockResponseForCatalogdb("ActivitySpecUserParameters2_Response.json"))
113 .withStatus(org.apache.http.HttpStatus.SC_OK)));
115 wireMockServer.stubFor(get(urlMatching("/activitySpec/3/activitySpecUserParameters"))
116 .willReturn(aResponse().withHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
117 .withBody(getWiremockResponseForCatalogdb("ActivitySpecUserParameters3_Response.json"))
118 .withStatus(org.apache.http.HttpStatus.SC_OK)));
120 wireMockServer.stubFor(get(urlMatching("/activitySpecUserParameters/1/userParameters"))
121 .willReturn(aResponse().withHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
122 .withBody(getWiremockResponseForCatalogdb("UserParameters1_Response.json"))
123 .withStatus(org.apache.http.HttpStatus.SC_OK)));
125 wireMockServer.stubFor(get(urlMatching("/activitySpecUserParameters/2/userParameters"))
126 .willReturn(aResponse().withHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
127 .withBody(getWiremockResponseForCatalogdb("UserParameters2_Response.json"))
128 .withStatus(org.apache.http.HttpStatus.SC_OK)));
130 wireMockServer.stubFor(get(urlMatching("/activitySpecUserParameters/3/userParameters"))
131 .willReturn(aResponse().withHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
132 .withBody(getWiremockResponseForCatalogdb("UserParameters3_Response.json"))
133 .withStatus(org.apache.http.HttpStatus.SC_OK)));
135 wireMockServer.stubFor(get(urlMatching("/activitySpecUserParameters/4/userParameters"))
136 .willReturn(aResponse().withHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
137 .withBody(getWiremockResponseForCatalogdb("UserParameters4_Response.json"))
138 .withStatus(org.apache.http.HttpStatus.SC_OK)));
140 wireMockServer.stubFor(get(urlMatching("/activitySpecUserParameters/5/userParameters"))
141 .willReturn(aResponse().withHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
142 .withBody(getWiremockResponseForCatalogdb("UserParameters5_Response.json"))
143 .withStatus(org.apache.http.HttpStatus.SC_OK)));
145 wireMockServer.stubFor(get(urlMatching("/activitySpecUserParameters/6/userParameters"))
146 .willReturn(aResponse().withHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
147 .withBody(getWiremockResponseForCatalogdb("UserParameters6_Response.json"))
148 .withStatus(org.apache.http.HttpStatus.SC_OK)));
150 UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(createURLWithPort(basePath))
151 .queryParam("vnfModelVersionId", "b5fa707a-f55a-11e7-a796-005056856d52");
153 ResponseEntity<String> response =
154 restTemplate.exchange(builder.toUriString(), HttpMethod.GET, entity, String.class);
156 assertEquals(Response.Status.OK.getStatusCode(), response.getStatusCode().value());
158 ObjectMapper mapper = new ObjectMapper();
159 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
161 WorkflowSpecifications expectedResponse = mapper.readValue(
163 .readAllBytes(Paths.get("src/test/resources/__files/catalogdb/WorkflowSpecifications.json"))),
164 WorkflowSpecifications.class);
165 WorkflowSpecifications realResponse = mapper.readValue(response.getBody(), WorkflowSpecifications.class);
167 assertEquals(Response.Status.OK.getStatusCode(), response.getStatusCode().value());
168 assertThat(expectedResponse, sameBeanAs(realResponse));
169 assertEquals("application/json", response.getHeaders().get(HttpHeaders.CONTENT_TYPE).get(0));
170 assertEquals("0", response.getHeaders().get("X-MinorVersion").get(0));
171 assertEquals("0", response.getHeaders().get("X-PatchVersion").get(0));
172 assertEquals("1.0.0", response.getHeaders().get("X-LatestVersion").get(0));
176 public void mapWorkflowsToWorkflowSpecifications_Test_Success() throws Exception {
177 List<Workflow> workflows = new ArrayList<Workflow>();
178 Workflow workflow = new Workflow();
179 workflow.setArtifactUUID("ab6478e4-ea33-3346-ac12-ab121484a333");
180 workflow.setArtifactName("inPlaceSoftwareUpdate-1_0.bpmn");
181 workflow.setVersion(1.0);
182 workflow.setDescription("xyz xyz");
183 workflow.setName("inPlaceSoftwareUpdate");
184 workflow.setOperationName("inPlaceSoftwareUpdate");
185 workflow.setSource("sdc");
186 workflow.setResourceTarget("vnf");
188 UserParameters userParameter1 = new UserParameters();
189 userParameter1.setLabel("Operations Timeout");
190 userParameter1.setType("text");
191 userParameter1.setIsRequried(true);
192 userParameter1.setMaxLength(50);
193 userParameter1.setAllowableChars("someRegEx");
194 userParameter1.setName("operations_timeout");
195 userParameter1.setPayloadLocation("userParams");
197 UserParameters userParameter2 = new UserParameters();
198 userParameter2.setLabel("Existing Software Version");
199 userParameter2.setType("text");
200 userParameter2.setIsRequried(true);
201 userParameter2.setMaxLength(50);
202 userParameter2.setAllowableChars("someRegEx");
203 userParameter2.setName("existing_software_version");
204 userParameter2.setPayloadLocation("userParams");
206 UserParameters userParameter3 = new UserParameters();
207 userParameter3.setLabel("Cloud Owner");
208 userParameter3.setType("text");
209 userParameter3.setIsRequried(true);
210 userParameter3.setMaxLength(7);
211 userParameter3.setAllowableChars("someRegEx");
212 userParameter3.setName("cloudOwner");
213 userParameter3.setPayloadLocation("cloudConfiguration");
215 UserParameters userParameter4 = new UserParameters();
216 userParameter4.setLabel("Tenant/Project ID");
217 userParameter4.setType("text");
218 userParameter4.setIsRequried(true);
219 userParameter4.setMaxLength(36);
220 userParameter4.setAllowableChars("someRegEx");
221 userParameter4.setName("tenantId");
222 userParameter4.setPayloadLocation("cloudConfiguration");
224 UserParameters userParameter5 = new UserParameters();
225 userParameter5.setLabel("New Software Version");
226 userParameter5.setType("text");
227 userParameter5.setIsRequried(true);
228 userParameter5.setMaxLength(50);
229 userParameter5.setAllowableChars("someRegEx");
230 userParameter5.setName("new_software_version");
231 userParameter5.setPayloadLocation("userParams");
233 UserParameters userParameter6 = new UserParameters();
234 userParameter6.setLabel("Cloud Region ID");
235 userParameter6.setType("text");
236 userParameter6.setIsRequried(true);
237 userParameter6.setMaxLength(7);
238 userParameter6.setAllowableChars("someRegEx");
239 userParameter6.setName("lcpCloudRegionId");
240 userParameter6.setPayloadLocation("cloudConfiguration");
243 List<ActivitySpecUserParameters> activitySpecUserParameters = new ArrayList<ActivitySpecUserParameters>();
245 ActivitySpecUserParameters activitySpecUserParameter1 = new ActivitySpecUserParameters();
246 activitySpecUserParameter1.setUserParameters(userParameter1);
247 activitySpecUserParameters.add(activitySpecUserParameter1);
249 ActivitySpecUserParameters activitySpecUserParameter2 = new ActivitySpecUserParameters();
250 activitySpecUserParameter2.setUserParameters(userParameter2);
251 activitySpecUserParameters.add(activitySpecUserParameter2);
253 ActivitySpecUserParameters activitySpecUserParameter3 = new ActivitySpecUserParameters();
254 activitySpecUserParameter3.setUserParameters(userParameter3);
255 activitySpecUserParameters.add(activitySpecUserParameter3);
258 ActivitySpecUserParameters activitySpecUserParameter4 = new ActivitySpecUserParameters();
259 activitySpecUserParameter4.setUserParameters(userParameter4);
260 activitySpecUserParameters.add(activitySpecUserParameter4);
262 ActivitySpecUserParameters activitySpecUserParameter5 = new ActivitySpecUserParameters();
263 activitySpecUserParameter5.setUserParameters(userParameter5);
264 activitySpecUserParameters.add(activitySpecUserParameter5);
266 ActivitySpecUserParameters activitySpecUserParameter6 = new ActivitySpecUserParameters();
267 activitySpecUserParameter6.setUserParameters(userParameter6);
268 activitySpecUserParameters.add(activitySpecUserParameter6);
270 List<WorkflowActivitySpecSequence> workflowActivitySpecSequences =
271 new ArrayList<WorkflowActivitySpecSequence>();
273 ActivitySpec activitySpec1 = new ActivitySpec();
274 activitySpec1.setName("VNFQuiesceTrafficActivity");
275 activitySpec1.setDescription("Activity to QuiesceTraffic on VNF");
276 activitySpec1.setActivitySpecUserParameters(activitySpecUserParameters);
277 WorkflowActivitySpecSequence workflowActivitySpecSequence1 = new WorkflowActivitySpecSequence();
278 workflowActivitySpecSequence1.setActivitySpec(activitySpec1);
279 workflowActivitySpecSequences.add(workflowActivitySpecSequence1);
281 ActivitySpec activitySpec2 = new ActivitySpec();
282 activitySpec2.setName("VNFHealthCheckActivity");
283 activitySpec2.setDescription("Activity to HealthCheck VNF");
284 activitySpec2.setActivitySpecUserParameters(activitySpecUserParameters);
285 WorkflowActivitySpecSequence workflowActivitySpecSequence2 = new WorkflowActivitySpecSequence();
286 workflowActivitySpecSequence2.setActivitySpec(activitySpec2);
287 workflowActivitySpecSequences.add(workflowActivitySpecSequence2);
289 ActivitySpec activitySpec3 = new ActivitySpec();
290 activitySpec3.setName("FlowCompleteActivity");
291 activitySpec3.setDescription("Activity to Complete the BPMN Flow");
292 activitySpec3.setActivitySpecUserParameters(activitySpecUserParameters);
293 WorkflowActivitySpecSequence workflowActivitySpecSequence3 = new WorkflowActivitySpecSequence();
294 workflowActivitySpecSequence3.setActivitySpec(activitySpec3);
295 workflowActivitySpecSequences.add(workflowActivitySpecSequence3);
297 workflow.setWorkflowActivitySpecSequence(workflowActivitySpecSequences);
298 workflows.add(workflow);
300 WorkflowSpecifications workflowSpecifications =
301 workflowSpecificationsHandler.mapWorkflowsToWorkflowSpecifications(workflows);
302 ObjectMapper mapper = new ObjectMapper();
304 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
305 String workflowSpecificationsJson = mapper.writeValueAsString(workflowSpecifications);
306 WorkflowSpecifications expectedResult = mapper.readValue(
308 .readAllBytes(Paths.get("src/test/resources/__files/catalogdb/WorkflowSpecifications.json"))),
309 WorkflowSpecifications.class);
310 String expectedResultJson = mapper.writeValueAsString(expectedResult);
312 JSONAssert.assertEquals(expectedResultJson, workflowSpecificationsJson, false);
313 assertThat(expectedResult, sameBeanAs(workflowSpecifications).ignoring(WorkflowInputParameter.class));
316 private String getWiremockResponseForCatalogdb(String file) {
318 File resource = ResourceUtils.getFile("classpath:__files/catalogdb/" + file);
319 return new String(Files.readAllBytes(resource.toPath())).replaceAll("localhost:8090",
320 "localhost:" + wiremockPort);
321 } catch (IOException e) {