Replaced all tabs with spaces in java and pom.xml
[so.git] / cloudify-client / src / test / java / org / onap / so / cloudify / v3 / client / ExecutionsResourceTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
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
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.onap.so.cloudify.v3.client;
22
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;
45
46 public class ExecutionsResourceTest {
47     @Rule
48     public WireMockRule wireMockRule = new WireMockRule(wireMockConfig().dynamicPort());
49
50     @Rule
51     public ExpectedException thrown = ExpectedException.none();
52
53     @Test
54     public void cloudifyClientExecutions() {
55         wireMockRule.stubFor(get(urlPathEqualTo("/api/v3/executions")).willReturn(aResponse()
56                 .withHeader("Content-Type", "application/json")
57                 .withBody(
58                         "{\"items\": [{ \"id\": \"345\" }, { \"id\": \"123\" }], \"metadata\": {\"pagination\": {\"total\": 100, \"offset\": 0, \"size\": 25}}}")
59                 .withStatus(HttpStatus.SC_OK)));
60
61         int port = wireMockRule.port();
62
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());
68     }
69
70     @Test
71     public void cloudifyClientExecutionsSorted() {
72         wireMockRule.stubFor(get(urlPathEqualTo("/api/v3/executions")).willReturn(aResponse()
73                 .withHeader("Content-Type", "application/json")
74                 .withBody(
75                         "{\"items\": [{ \"id\": \"123\" }, { \"id\": \"345\" }], \"metadata\": {\"pagination\": {\"total\": 100, \"offset\": 0, \"size\": 25}}}")
76                 .withStatus(HttpStatus.SC_OK)));
77
78         int port = wireMockRule.port();
79
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());
85     }
86
87     @Test
88     public void cloudifyClientExecutionsFilter() {
89         wireMockRule.stubFor(get(urlPathEqualTo("/api/v3/executions")).willReturn(aResponse()
90                 .withHeader("Content-Type", "application/json")
91                 .withBody(
92                         "{\"items\": [{ \"id\": \"121\" }, { \"id\": \"123\" }], \"metadata\": {\"pagination\": {\"total\": 100, \"offset\": 0, \"size\": 25}}}")
93                 .withStatus(HttpStatus.SC_OK)));
94
95         int port = wireMockRule.port();
96
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());
102     }
103
104     @Test
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)));
109
110         int port = wireMockRule.port();
111
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());
117     }
118
119     @Test
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)));
124
125         int port = wireMockRule.port();
126
127         Cloudify c = new Cloudify("http://localhost:" + port, "tenant");
128         ExecutionsResource xr = c.executions();
129
130         StartExecutionParams params = new StartExecutionParams();
131         StartExecution sx = xr.start(params);
132         Execution x = sx.execute();
133         assertEquals("123", x.getId());
134     }
135
136     @Test
137     public void cloudifyClientUpdateExecution() {
138         thrown.expect(HttpClientException.class);
139         thrown.expectMessage("Unrecognized HTTP Method: PATCH");
140
141         wireMockRule.stubFor(patch(urlPathEqualTo("/api/v3/executions"))
142                 .willReturn(aResponse().withHeader("Content-Type", "application/json").withBody("{ \"id\": \"123\" }")
143                         .withStatus(HttpStatus.SC_OK)));
144
145         int port = wireMockRule.port();
146
147         Cloudify c = new Cloudify("http://localhost:" + port, "tenant");
148         ExecutionsResource xr = c.executions();
149
150         UpdateExecution ux = xr.updateStatus("123", "good");
151         Execution x = ux.execute();
152         assertEquals("123", x.getId());
153     }
154
155     @Test
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)));
160
161         int port = wireMockRule.port();
162
163         Cloudify c = new Cloudify("http://localhost:" + port, "tenant");
164         ExecutionsResource xr = c.executions();
165
166         CancelExecutionParams params = new CancelExecutionParams();
167         CancelExecution cx = xr.cancel("123", params);
168         Execution x = cx.execute();
169         assertEquals("123", x.getId());
170     }
171
172
173
174 }