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.onap.so.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;
30 import org.apache.http.HttpStatus;
31 import org.junit.Rule;
32 import org.junit.Test;
33 import org.junit.rules.ExpectedException;
34 import org.onap.so.cloudify.connector.http.HttpClientException;
35 import org.onap.so.cloudify.v3.client.ExecutionsResource.CancelExecution;
36 import org.onap.so.cloudify.v3.client.ExecutionsResource.GetExecution;
37 import org.onap.so.cloudify.v3.client.ExecutionsResource.ListExecutions;
38 import org.onap.so.cloudify.v3.client.ExecutionsResource.StartExecution;
39 import org.onap.so.cloudify.v3.client.ExecutionsResource.UpdateExecution;
40 import org.onap.so.cloudify.v3.model.CancelExecutionParams;
41 import org.onap.so.cloudify.v3.model.Execution;
42 import org.onap.so.cloudify.v3.model.Executions;
43 import org.onap.so.cloudify.v3.model.StartExecutionParams;
44 import com.github.tomakehurst.wiremock.junit.WireMockRule;
46 public class ExecutionsResourceTest {
48 public WireMockRule wireMockRule = new WireMockRule(wireMockConfig().dynamicPort());
51 public ExpectedException thrown = ExpectedException.none();
54 public void cloudifyClientExecutions() {
55 wireMockRule.stubFor(get(urlPathEqualTo("/api/v3/executions")).willReturn(aResponse()
56 .withHeader("Content-Type", "application/json")
58 "{\"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()
73 .withHeader("Content-Type", "application/json")
75 "{\"items\": [{ \"id\": \"123\" }, { \"id\": \"345\" }], \"metadata\": {\"pagination\": {\"total\": 100, \"offset\": 0, \"size\": 25}}}")
76 .withStatus(HttpStatus.SC_OK)));
78 int port = wireMockRule.port();
80 Cloudify c = new Cloudify("http://localhost:" + port, "tenant");
81 ExecutionsResource xr = c.executions();
82 ListExecutions lx = xr.listSorted("id");
83 Executions x = lx.execute();
84 assertEquals("345", x.getItems().get(1).getId());
88 public void cloudifyClientExecutionsFilter() {
89 wireMockRule.stubFor(get(urlPathEqualTo("/api/v3/executions")).willReturn(aResponse()
90 .withHeader("Content-Type", "application/json")
92 "{\"items\": [{ \"id\": \"121\" }, { \"id\": \"123\" }], \"metadata\": {\"pagination\": {\"total\": 100, \"offset\": 0, \"size\": 25}}}")
93 .withStatus(HttpStatus.SC_OK)));
95 int port = wireMockRule.port();
97 Cloudify c = new Cloudify("http://localhost:" + port, "tenant");
98 ExecutionsResource xr = c.executions();
99 ListExecutions lx = xr.listFiltered("a=b", "id");
100 Executions x = lx.execute();
101 assertEquals("123", x.getItems().get(1).getId());
105 public void cloudifyClientExecutionById() {
106 wireMockRule.stubFor(get(urlPathEqualTo("/api/v3/executions/123"))
107 .willReturn(aResponse().withHeader("Content-Type", "application/json").withBody("{ \"id\": \"123\" }")
108 .withStatus(HttpStatus.SC_OK)));
110 int port = wireMockRule.port();
112 Cloudify c = new Cloudify("http://localhost:" + port, "tenant");
113 ExecutionsResource xr = c.executions();
114 GetExecution gx = xr.byId("123");
115 Execution x = gx.execute();
116 assertEquals("123", x.getId());
120 public void cloudifyClientStartExecution() {
121 wireMockRule.stubFor(post(urlPathEqualTo("/api/v3/executions"))
122 .willReturn(aResponse().withHeader("Content-Type", "application/json").withBody("{ \"id\": \"123\" }")
123 .withStatus(HttpStatus.SC_OK)));
125 int port = wireMockRule.port();
127 Cloudify c = new Cloudify("http://localhost:" + port, "tenant");
128 ExecutionsResource xr = c.executions();
130 StartExecutionParams params = new StartExecutionParams();
131 StartExecution sx = xr.start(params);
132 Execution x = sx.execute();
133 assertEquals("123", x.getId());
137 public void cloudifyClientUpdateExecution() {
138 thrown.expect(HttpClientException.class);
139 thrown.expectMessage("Unrecognized HTTP Method: PATCH");
141 wireMockRule.stubFor(patch(urlPathEqualTo("/api/v3/executions"))
142 .willReturn(aResponse().withHeader("Content-Type", "application/json").withBody("{ \"id\": \"123\" }")
143 .withStatus(HttpStatus.SC_OK)));
145 int port = wireMockRule.port();
147 Cloudify c = new Cloudify("http://localhost:" + port, "tenant");
148 ExecutionsResource xr = c.executions();
150 UpdateExecution ux = xr.updateStatus("123", "good");
151 Execution x = ux.execute();
152 assertEquals("123", x.getId());
156 public void cloudifyClientCancelExecution() {
157 wireMockRule.stubFor(post(urlPathEqualTo("/api/v3/executions/123"))
158 .willReturn(aResponse().withHeader("Content-Type", "application/json").withBody("{ \"id\": \"123\" }")
159 .withStatus(HttpStatus.SC_OK)));
161 int port = wireMockRule.port();
163 Cloudify c = new Cloudify("http://localhost:" + port, "tenant");
164 ExecutionsResource xr = c.executions();
166 CancelExecutionParams params = new CancelExecutionParams();
167 CancelExecution cx = xr.cancel("123", params);
168 Execution x = cx.execute();
169 assertEquals("123", x.getId());