071b7638e626ccfd30811e6f7919d99bbe9c1886
[so.git] / cloudify-client / src / test / java / org / onap / so / cloudify / v3 / client / DeploymentsResourceTest.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.delete;
25 import static com.github.tomakehurst.wiremock.client.WireMock.get;
26 import static com.github.tomakehurst.wiremock.client.WireMock.put;
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 static org.junit.Assert.assertNull;
31
32 import java.util.HashMap;
33 import java.util.Map;
34
35 import org.apache.http.HttpStatus;
36 import org.junit.Rule;
37 import org.junit.Test;
38 import org.junit.rules.ExpectedException;
39 import org.onap.so.cloudify.v3.client.DeploymentsResource.CreateDeployment;
40 import org.onap.so.cloudify.v3.client.DeploymentsResource.DeleteDeployment;
41 import org.onap.so.cloudify.v3.client.DeploymentsResource.GetDeployment;
42 import org.onap.so.cloudify.v3.client.DeploymentsResource.GetDeploymentOutputs;
43 import org.onap.so.cloudify.v3.client.DeploymentsResource.ListDeployments;
44 import org.onap.so.cloudify.v3.model.CreateDeploymentParams;
45 import org.onap.so.cloudify.v3.model.Deployment;
46 import org.onap.so.cloudify.v3.model.Deployments;
47 import org.onap.so.cloudify.v3.model.DeploymentOutputs;
48
49 import com.github.tomakehurst.wiremock.junit.WireMockRule;
50
51 public class DeploymentsResourceTest {
52         @Rule
53         public WireMockRule wireMockRule = new WireMockRule(wireMockConfig().dynamicPort());
54         
55         @Rule
56         public ExpectedException thrown = ExpectedException.none();
57
58         @Test
59         public void cloudifyDeploymentsCreate() {
60                 wireMockRule.stubFor(put(urlPathEqualTo("/api/v3/deployments/123")).willReturn(aResponse().withHeader("Content-Type", "application/json")
61                                 .withBody("{ \"id\": \"123\" }")
62                                 .withStatus(HttpStatus.SC_OK)));
63                 
64                 int port = wireMockRule.port();
65
66                 Cloudify c = new Cloudify("http://localhost:"+port, "tenant");
67                 DeploymentsResource br = c.deployments();
68
69                 CreateDeploymentParams cdp = new CreateDeploymentParams();
70                 cdp.setBlueprintId("123");
71                 Map<String, Object> inputs = new HashMap<String, Object>();
72                 cdp.setInputs(inputs);
73                 CreateDeployment cd = br.create("123", cdp);
74                 Deployment d = cd.execute();
75                 assertEquals("123", d.getId());
76         }
77
78         @Test
79         public void cloudifyDeploymentsList() {
80                 wireMockRule.stubFor(get(urlPathEqualTo("/api/v3/deployments")).willReturn(aResponse().withHeader("Content-Type", "application/json")
81                                 .withBody("{ \"items\": {\"id\": \"123\" } } ")
82                                 .withStatus(HttpStatus.SC_OK)));
83                 
84                 int port = wireMockRule.port();
85
86                 Cloudify c = new Cloudify("http://localhost:"+port, "tenant");
87                 DeploymentsResource br = c.deployments();
88                 ListDeployments ld = br.list();
89                 Deployments d = ld.execute();
90                 assertEquals("123", d.getItems().get(0).getId());
91         }
92
93         @Test
94         public void cloudifyDeploymentsGet() {
95                 wireMockRule.stubFor(get(urlPathEqualTo("/api/v3/deployments/123")).willReturn(aResponse().withHeader("Content-Type", "application/json")
96                                 .withBody("{ \"id\": \"123\" }")
97                                 .withStatus(HttpStatus.SC_OK)));
98                 
99                 int port = wireMockRule.port();
100
101                 Cloudify c = new Cloudify("http://localhost:"+port, "tenant");
102                 DeploymentsResource br = c.deployments();
103                 GetDeployment gd = br.byId("123");
104                 Deployment d = gd.execute();
105                 assertEquals("123", d.getId());
106         }
107
108         @Test
109         public void cloudifyDeploymentsGetOutputs() {
110                 wireMockRule.stubFor(get(urlPathEqualTo("/api/v3/deployments/123/outputs")).willReturn(aResponse().withHeader("Content-Type", "application/json")
111                                 .withBody("{ \"deployment_id\": \"123\" }")
112                                 .withStatus(HttpStatus.SC_OK)));
113                 
114                 int port = wireMockRule.port();
115
116                 Cloudify c = new Cloudify("http://localhost:"+port, "tenant");
117                 DeploymentsResource br = c.deployments();
118                 GetDeploymentOutputs gdo = br.outputsById("123");
119                 DeploymentOutputs d = gdo.execute();
120                 assertEquals("123", d.getDeploymentId());
121                 
122                 Map<String, Object> map = new HashMap<String, Object>();
123                 map.put("test", "answer");
124                 assertEquals("answer", d.getMapValue(map, "test", String.class));
125
126                 Integer i = d.getMapValue(map, "nil", Integer.class);
127                 assertNull( i );
128         
129                 i = d.getMapValue(map, "test", Integer.class);
130                 assertNull( i );
131         }
132
133         @Test
134         public void cloudifyDeploymentsDelete() {
135                 wireMockRule.stubFor(delete(urlPathEqualTo("/api/v3/deployments/name")).willReturn(aResponse().withHeader("Content-Type", "application/json")
136                                 .withBody("{ \"id\": \"123\" }")
137                                 .withStatus(HttpStatus.SC_OK)));
138                 
139                 int port = wireMockRule.port();
140
141                 Cloudify c = new Cloudify("http://localhost:"+port, "tenant");
142                 DeploymentsResource br = c.deployments();
143                 DeleteDeployment cd = br.deleteByName("name");
144                 Deployment d = cd.execute();
145                 assertEquals("123", d.getId());
146         }
147
148 }