ec7435fca52b058a85100138aa4faafa3ab7c389
[so.git] / cloudify-client / src / test / java / org / openecomp / mso / cloudify / v3 / client / BlueprintsResourceTest.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.openecomp.mso.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
31 import java.io.ByteArrayInputStream;
32 import java.io.InputStream;
33 import java.nio.charset.StandardCharsets;
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.openecomp.mso.cloudify.v3.client.BlueprintsResource.DeleteBlueprint;
40 import org.openecomp.mso.cloudify.v3.client.BlueprintsResource.GetBlueprint;
41 import org.openecomp.mso.cloudify.v3.client.BlueprintsResource.ListBlueprints;
42 import org.openecomp.mso.cloudify.v3.client.BlueprintsResource.UploadBlueprint;
43 import org.openecomp.mso.cloudify.v3.model.Blueprint;
44 import org.openecomp.mso.cloudify.v3.model.Blueprints;
45
46 import com.github.tomakehurst.wiremock.junit.WireMockRule;
47
48 public class BlueprintsResourceTest {
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 cloudifyClientBlueprintFromStream() {
57                 wireMockRule.stubFor(put(urlPathEqualTo("/api/v3/blueprints/")).willReturn(aResponse().withHeader("Content-Type", "application/json")
58                                 .withBody("{\"id\": \"123\"}")
59                                 .withStatus(HttpStatus.SC_OK)));
60                 
61                 int port = wireMockRule.port();
62
63                 Cloudify c = new Cloudify("http://localhost:"+port, "tenant");
64                 BlueprintsResource br = c.blueprints();
65                 InputStream is = new ByteArrayInputStream("{}".getBytes(StandardCharsets.UTF_8));
66                 UploadBlueprint ub = br.uploadFromStream("123", "blueprint.json", is);
67                 Blueprint b = ub.execute();
68                 assertEquals("123", b.getId());
69         }
70
71         @Test
72         public void cloudifyClientBlueprintFromUrl() {
73                 wireMockRule.stubFor(put(urlPathEqualTo("/api/v3/blueprints/")).willReturn(aResponse().withHeader("Content-Type", "application/json")
74                                 .withBody("{\"id\": \"123\"}")
75                                 .withStatus(HttpStatus.SC_OK)));
76                 
77                 int port = wireMockRule.port();
78
79                 Cloudify c = new Cloudify("http://localhost:"+port, "tenant");
80                 BlueprintsResource br = c.blueprints();
81                 UploadBlueprint ub = br.uploadFromUrl("123", "blueprint.json", "http://localhost:"+port+"/blueprint");
82                 Blueprint b = ub.execute();
83                 assertEquals("123", b.getId());
84         }
85
86         @Test
87         public void cloudifyClientBlueprintDelete() {
88                 wireMockRule.stubFor(delete(urlPathEqualTo("/api/v3/blueprints/")).willReturn(aResponse().withHeader("Content-Type", "application/json")
89                                 .withBody("{\"id\": \"123\"}")
90                                 .withStatus(HttpStatus.SC_OK)));
91                 
92                 int port = wireMockRule.port();
93
94                 Cloudify c = new Cloudify("http://localhost:"+port, "tenant");
95                 BlueprintsResource br = c.blueprints();
96                 DeleteBlueprint db = br.deleteById("123");
97                 Blueprint b = db.execute();
98                 assertEquals("123", b.getId());
99         }
100
101         @Test
102         public void cloudifyClientBlueprintList() {
103                 wireMockRule.stubFor(get(urlPathEqualTo("/api/v3/blueprints")).willReturn(aResponse().withHeader("Content-Type", "application/json")
104                                 .withBody("{\"items\": [{\"id\": \"123\"}]}")
105                                 .withStatus(HttpStatus.SC_OK)));
106                 
107                 int port = wireMockRule.port();
108
109                 Cloudify c = new Cloudify("http://localhost:"+port, "tenant");
110                 BlueprintsResource br = c.blueprints();
111                 ListBlueprints lb = br.list();
112                 Blueprints b = lb.execute();
113                 assertEquals("123", b.getItems().get(0).getId());
114         }
115
116         @Test
117         public void cloudifyClientBlueprintGetById() {
118                 wireMockRule.stubFor(get(urlPathEqualTo("/api/v3/blueprints/")).willReturn(aResponse().withHeader("Content-Type", "application/json")
119                                 .withBody("{\"id\": \"123\"}")
120                                 .withStatus(HttpStatus.SC_OK)));
121                 
122                 int port = wireMockRule.port();
123
124                 Cloudify c = new Cloudify("http://localhost:"+port, "tenant");
125                 BlueprintsResource br = c.blueprints();
126                 GetBlueprint gb = br.getById("123");
127                 Blueprint b = gb.execute();
128                 assertEquals("123", b.getId());
129         }
130
131
132 }