/
OrganizationApiExpectTest.java
208 lines (178 loc) · 8.55 KB
/
OrganizationApiExpectTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jclouds.chef.features;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertNull;
import static org.testng.Assert.assertTrue;
import java.util.Set;
import javax.ws.rs.core.MediaType;
import org.jclouds.apis.ApiMetadata;
import org.jclouds.chef.BaseChefApiExpectTest;
import org.jclouds.chef.ChefApi;
import org.jclouds.chef.ChefApiMetadata;
import org.jclouds.chef.config.ChefHttpApiModule;
import org.jclouds.chef.domain.Group;
import org.jclouds.chef.domain.User;
import org.jclouds.date.TimeStamp;
import org.jclouds.http.HttpRequest;
import org.jclouds.http.HttpResponse;
import org.jclouds.http.HttpResponseException;
import org.jclouds.rest.ConfiguresHttpApi;
import org.jclouds.rest.ResourceNotFoundException;
import org.testng.annotations.Test;
import com.google.common.base.Supplier;
import com.google.inject.Module;
/**
* Expect tests for the {@link OrganizationApi} class.
*/
@Test(groups = "unit", testName = "OrganizationApiExpectTest")
public class OrganizationApiExpectTest extends BaseChefApiExpectTest<ChefApi> {
public void testGetUserReturns2xx() {
ChefApi api = requestSendsResponse(
signed(HttpRequest.builder() //
.method("GET") //
.endpoint("http://localhost:4000/users/nacx") //
.addHeader("X-Chef-Version", ChefApiMetadata.DEFAULT_API_VERSION) //
.addHeader("Accept", MediaType.APPLICATION_JSON).build()), //
HttpResponse.builder().statusCode(200)
.payload(payloadFromResourceWithContentType("/user.json", MediaType.APPLICATION_JSON)) //
.build());
User user = api.organizationApi().get().getUser("nacx");
assertEquals(user.getUsername(), "nacx");
assertEquals(user.getDisplayName(), "Ignasi Barrera");
}
public void testGetUserReturns404() {
ChefApi api = requestSendsResponse(signed(HttpRequest.builder() //
.method("GET") //
.endpoint("http://localhost:4000/users/foo") //
.addHeader("X-Chef-Version", ChefApiMetadata.DEFAULT_API_VERSION) //
.addHeader("Accept", MediaType.APPLICATION_JSON) //
.build()), //
HttpResponse.builder().statusCode(404).build());
assertNull(api.organizationApi().get().getUser("foo"));
}
public void testListGroups() {
ChefApi api = requestSendsResponse(
signed(HttpRequest.builder() //
.method("GET") //
.endpoint("http://localhost:4000/groups") //
.addHeader("X-Chef-Version", ChefApiMetadata.DEFAULT_API_VERSION) //
.addHeader("Accept", MediaType.APPLICATION_JSON).build()), //
HttpResponse.builder().statusCode(200)
.payload(payloadFromResourceWithContentType("/groups.json", MediaType.APPLICATION_JSON)) //
.build());
Set<String> groups = api.organizationApi().get().listGroups();
assertEquals(groups.size(), 5);
assertTrue(groups.contains("admins"));
}
public void testGetGroupReturns2xx() {
ChefApi api = requestSendsResponse(
signed(HttpRequest.builder() //
.method("GET") //
.endpoint("http://localhost:4000/groups/admins") //
.addHeader("X-Chef-Version", ChefApiMetadata.DEFAULT_API_VERSION) //
.addHeader("Accept", MediaType.APPLICATION_JSON).build()), //
HttpResponse.builder().statusCode(200)
.payload(payloadFromResourceWithContentType("/group.json", MediaType.APPLICATION_JSON)) //
.build());
Group group = api.organizationApi().get().getGroup("admins");
assertEquals(group.getName(), "admins");
assertEquals(group.getGroupname(), "admins");
}
public void testGetGroupReturns404() {
ChefApi api = requestSendsResponse(signed(HttpRequest.builder() //
.method("GET") //
.endpoint("http://localhost:4000/groups/foo") //
.addHeader("X-Chef-Version", ChefApiMetadata.DEFAULT_API_VERSION) //
.addHeader("Accept", MediaType.APPLICATION_JSON) //
.build()), //
HttpResponse.builder().statusCode(404).build());
assertNull(api.organizationApi().get().getGroup("foo"));
}
public void testCreateGroupReturns2xx() {
ChefApi api = requestSendsResponse(signed(HttpRequest.builder() //
.method("POST") //
.endpoint("http://localhost:4000/groups") //
.addHeader("X-Chef-Version", ChefApiMetadata.DEFAULT_API_VERSION) //
.addHeader("Accept", MediaType.APPLICATION_JSON) //
.payload(payloadFromStringWithContentType("{\"groupname\":\"foo\"}", MediaType.APPLICATION_JSON)) //
.build()), //
HttpResponse.builder().statusCode(201).build());
api.organizationApi().get().createGroup("foo");
}
public void testDeleteGroupReturns2xx() {
ChefApi api = requestSendsResponse(signed(HttpRequest.builder() //
.method("DELETE") //
.endpoint("http://localhost:4000/groups/foo") //
.addHeader("X-Chef-Version", ChefApiMetadata.DEFAULT_API_VERSION) //
.addHeader("Accept", MediaType.APPLICATION_JSON) //
.build()), //
HttpResponse.builder().statusCode(200).build());
api.organizationApi().get().deleteGroup("foo");
}
@Test(expectedExceptions = HttpResponseException.class)
public void testDeleteGroupFailsOn404() {
ChefApi api = requestSendsResponse(signed(HttpRequest.builder() //
.method("DELETE") //
.endpoint("http://localhost:4000/groups/foo") //
.addHeader("X-Chef-Version", ChefApiMetadata.DEFAULT_API_VERSION) //
.addHeader("Accept", MediaType.APPLICATION_JSON) //
.build()), //
HttpResponse.builder().statusCode(404).build());
api.organizationApi().get().deleteGroup("foo");
}
public void testUpdateGroupReturns2xx() {
ChefApi api = requestSendsResponse(signed(HttpRequest.builder() //
.method("PUT") //
.endpoint("http://localhost:4000/groups/admins") //
.addHeader("X-Chef-Version", ChefApiMetadata.DEFAULT_API_VERSION) //
.addHeader("Accept", MediaType.APPLICATION_JSON) //
.payload(payloadFromResourceWithContentType("/group-update.json", MediaType.APPLICATION_JSON)) //
.build()), //
HttpResponse.builder().statusCode(200).build());
Group group = Group.builder("admins").client("abiquo").group("admins").user("nacx").build();
api.organizationApi().get().updateGroup(group);
}
@Test(expectedExceptions = ResourceNotFoundException.class)
public void testUpdateGroupFailsOn404() {
ChefApi api = requestSendsResponse(signed(HttpRequest.builder() //
.method("PUT") //
.endpoint("http://localhost:4000/groups/admins") //
.addHeader("X-Chef-Version", ChefApiMetadata.DEFAULT_API_VERSION) //
.addHeader("Accept", MediaType.APPLICATION_JSON) //
.payload(payloadFromResourceWithContentType("/group-update.json", MediaType.APPLICATION_JSON)) //
.build()), //
HttpResponse.builder().statusCode(404).build());
Group group = Group.builder("admins").client("abiquo").group("admins").user("nacx").build();
api.organizationApi().get().updateGroup(group);
}
@Override
protected Module createModule() {
return new TestChefHttpApiModule();
}
@ConfiguresHttpApi
static class TestChefHttpApiModule extends ChefHttpApiModule {
@Override
protected String provideTimeStamp(@TimeStamp Supplier<String> cache) {
return "timestamp";
}
}
@Override
protected ApiMetadata createApiMetadata() {
return new ChefApiMetadata();
}
}