2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2018 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.openecomp.mso.cloudify.v3.client;
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.patch;
26 import static com.github.tomakehurst.wiremock.client.WireMock.post;
27 import static com.github.tomakehurst.wiremock.client.WireMock.urlPathEqualTo;
28 import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig;
29 import static org.junit.Assert.assertEquals;
31 import org.apache.http.HttpStatus;
32 import org.junit.Rule;
33 import org.junit.Test;
34 import org.junit.rules.ExpectedException;
35 import org.openecomp.mso.cloudify.connector.http.HttpClientException;
36 import org.openecomp.mso.cloudify.v3.client.ExecutionsResource.CancelExecution;
37 import org.openecomp.mso.cloudify.v3.client.ExecutionsResource.GetExecution;
38 import org.openecomp.mso.cloudify.v3.client.ExecutionsResource.ListExecutions;
39 import org.openecomp.mso.cloudify.v3.client.ExecutionsResource.StartExecution;
40 import org.openecomp.mso.cloudify.v3.client.ExecutionsResource.UpdateExecution;
41 import org.openecomp.mso.cloudify.v3.model.CancelExecutionParams;
42 import org.openecomp.mso.cloudify.v3.model.Execution;
43 import org.openecomp.mso.cloudify.v3.model.Executions;
44 import org.openecomp.mso.cloudify.v3.model.StartExecutionParams;
46 import com.github.tomakehurst.wiremock.junit.WireMockRule;
48 public class ExecutionsResourceTest {
50 public WireMockRule wireMockRule = new WireMockRule(wireMockConfig().dynamicPort());
53 public ExpectedException thrown = ExpectedException.none();
56 public void cloudifyClientExecutions() {
57 wireMockRule.stubFor(get(urlPathEqualTo("/api/v3/executions")).willReturn(aResponse().withHeader("Content-Type", "application/json")
58 .withBody("{\"items\": [{ \"id\": \"345\" }, { \"id\": \"123\" }], \"metadata\": {\"pagination\": {\"total\": 100, \"offset\": 0, \"size\": 25}}}")
59 .withStatus(HttpStatus.SC_OK)));
61 int port = wireMockRule.port();
63 Cloudify c = new Cloudify("http://localhost:"+port, "tenant");
64 ExecutionsResource xr = c.executions();
65 ListExecutions lx = xr.list();
66 Executions x = lx.execute();
67 assertEquals("123", x.getItems().get(1).getId());
71 public void cloudifyClientExecutionsSorted() {
72 wireMockRule.stubFor(get(urlPathEqualTo("/api/v3/executions")).willReturn(aResponse().withHeader("Content-Type", "application/json")
73 .withBody("{\"items\": [{ \"id\": \"123\" }, { \"id\": \"345\" }], \"metadata\": {\"pagination\": {\"total\": 100, \"offset\": 0, \"size\": 25}}}")
74 .withStatus(HttpStatus.SC_OK)));
76 int port = wireMockRule.port();
78 Cloudify c = new Cloudify("http://localhost:"+port, "tenant");
79 ExecutionsResource xr = c.executions();
80 ListExecutions lx = xr.listSorted("id");
81 Executions x = lx.execute();
82 assertEquals("345", x.getItems().get(1).getId());
86 public void cloudifyClientExecutionsFilter() {
87 wireMockRule.stubFor(get(urlPathEqualTo("/api/v3/executions")).willReturn(aResponse().withHeader("Content-Type", "application/json")
88 .withBody("{\"items\": [{ \"id\": \"121\" }, { \"id\": \"123\" }], \"metadata\": {\"pagination\": {\"total\": 100, \"offset\": 0, \"size\": 25}}}")
89 .withStatus(HttpStatus.SC_OK)));
91 int port = wireMockRule.port();
93 Cloudify c = new Cloudify("http://localhost:"+port, "tenant");
94 ExecutionsResource xr = c.executions();
95 ListExecutions lx = xr.listFiltered("a=b", "id");
96 Executions x = lx.execute();
97 assertEquals("123", x.getItems().get(1).getId());
101 public void cloudifyClientExecutionById() {
102 wireMockRule.stubFor(get(urlPathEqualTo("/api/v3/executions")).willReturn(aResponse().withHeader("Content-Type", "application/json")
103 .withBody("{ \"id\": \"123\" }")
104 .withStatus(HttpStatus.SC_OK)));
106 int port = wireMockRule.port();
108 Cloudify c = new Cloudify("http://localhost:"+port, "tenant");
109 ExecutionsResource xr = c.executions();
110 GetExecution gx = xr.byId("123");
111 Execution x = gx.execute();
112 assertEquals("123", x.getId());
116 public void cloudifyClientStartExecution() {
117 wireMockRule.stubFor(post(urlPathEqualTo("/api/v3/executions")).willReturn(aResponse().withHeader("Content-Type", "application/json")
118 .withBody("{ \"id\": \"123\" }")
119 .withStatus(HttpStatus.SC_OK)));
121 int port = wireMockRule.port();
123 Cloudify c = new Cloudify("http://localhost:"+port, "tenant");
124 ExecutionsResource xr = c.executions();
126 StartExecutionParams params = new StartExecutionParams();
127 StartExecution sx = xr.start(params);
128 Execution x = sx.execute();
129 assertEquals("123", x.getId());
133 public void cloudifyClientUpdateExecution() {
134 thrown.expect(HttpClientException.class);
135 thrown.expectMessage("Unrecognized HTTP Method: PATCH");
137 wireMockRule.stubFor(patch(urlPathEqualTo("/api/v3/executions")).willReturn(aResponse().withHeader("Content-Type", "application/json")
138 .withBody("{ \"id\": \"123\" }")
139 .withStatus(HttpStatus.SC_OK)));
141 int port = wireMockRule.port();
143 Cloudify c = new Cloudify("http://localhost:"+port, "tenant");
144 ExecutionsResource xr = c.executions();
146 UpdateExecution ux = xr.updateStatus("123", "good");
147 Execution x = ux.execute();
148 assertEquals("123", x.getId());
152 public void cloudifyClientCancelExecution() {
153 wireMockRule.stubFor(post(urlPathEqualTo("/api/v3/executions")).willReturn(aResponse().withHeader("Content-Type", "application/json")
154 .withBody("{ \"id\": \"123\" }")
155 .withStatus(HttpStatus.SC_OK)));
157 int port = wireMockRule.port();
159 Cloudify c = new Cloudify("http://localhost:"+port, "tenant");
160 ExecutionsResource xr = c.executions();
162 CancelExecutionParams params = new CancelExecutionParams();
163 CancelExecution cx = xr.cancel("123", params);
164 Execution x = cx.execute();
165 assertEquals("123", x.getId());