Containerization feature of SO
[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
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.onap.so.cloudify.connector.http.HttpClientException;
36 import org.onap.so.cloudify.v3.client.ExecutionsResource.CancelExecution;
37 import org.onap.so.cloudify.v3.client.ExecutionsResource.GetExecution;
38 import org.onap.so.cloudify.v3.client.ExecutionsResource.ListExecutions;
39 import org.onap.so.cloudify.v3.client.ExecutionsResource.StartExecution;
40 import org.onap.so.cloudify.v3.client.ExecutionsResource.UpdateExecution;
41 import org.onap.so.cloudify.v3.model.CancelExecutionParams;
42 import org.onap.so.cloudify.v3.model.Execution;
43 import org.onap.so.cloudify.v3.model.Executions;
44 import org.onap.so.cloudify.v3.model.StartExecutionParams;
45
46 import com.github.tomakehurst.wiremock.junit.WireMockRule;
47
48 public class ExecutionsResourceTest {
49         @Rule
50         public WireMockRule wireMockRule = new WireMockRule(wireMockConfig().dynamicPort());
51         
52         @Rule
53         public ExpectedException thrown = ExpectedException.none();
54
55         @Test
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)));
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().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)));
75                 
76                 int port = wireMockRule.port();
77
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());
83         }
84
85         @Test
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)));
90                 
91                 int port = wireMockRule.port();
92
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());
98         }
99
100         @Test
101         public void cloudifyClientExecutionById() {
102                 wireMockRule.stubFor(get(urlPathEqualTo("/api/v3/executions/123")).willReturn(aResponse().withHeader("Content-Type", "application/json")
103                                 .withBody("{ \"id\": \"123\" }")
104                                 .withStatus(HttpStatus.SC_OK)));
105                 
106                 int port = wireMockRule.port();
107
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());
113         }
114
115         @Test
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)));
120                 
121                 int port = wireMockRule.port();
122
123                 Cloudify c = new Cloudify("http://localhost:"+port, "tenant");
124                 ExecutionsResource xr = c.executions();
125
126                 StartExecutionParams params = new StartExecutionParams();
127                 StartExecution sx = xr.start(params);
128                 Execution x = sx.execute();
129                 assertEquals("123", x.getId());
130         }
131
132         @Test
133         public void cloudifyClientUpdateExecution() {
134                 thrown.expect(HttpClientException.class);
135                 thrown.expectMessage("Unrecognized HTTP Method: PATCH");
136                 
137                 wireMockRule.stubFor(patch(urlPathEqualTo("/api/v3/executions")).willReturn(aResponse().withHeader("Content-Type", "application/json")
138                                 .withBody("{ \"id\": \"123\" }")
139                                 .withStatus(HttpStatus.SC_OK)));
140                 
141                 int port = wireMockRule.port();
142
143                 Cloudify c = new Cloudify("http://localhost:"+port, "tenant");
144                 ExecutionsResource xr = c.executions();
145
146                 UpdateExecution ux = xr.updateStatus("123", "good");
147                 Execution x = ux.execute();
148                 assertEquals("123", x.getId());
149         }
150
151         @Test
152         public void cloudifyClientCancelExecution() {
153                 wireMockRule.stubFor(post(urlPathEqualTo("/api/v3/executions/123")).willReturn(aResponse().withHeader("Content-Type", "application/json")
154                                 .withBody("{ \"id\": \"123\" }")
155                                 .withStatus(HttpStatus.SC_OK)));
156                 
157                 int port = wireMockRule.port();
158
159                 Cloudify c = new Cloudify("http://localhost:"+port, "tenant");
160                 ExecutionsResource xr = c.executions();
161
162                 CancelExecutionParams params = new CancelExecutionParams();
163                 CancelExecution cx = xr.cancel("123", params);
164                 Execution x = cx.execute();
165                 assertEquals("123", x.getId());
166         }
167
168         
169
170 }