Merge "Add acceptance test for getting/setting group properties via REST"

This commit is contained in:
Edwin Kempin 2013-02-21 07:11:59 +00:00 committed by Gerrit Code Review
commit abd6cbfe00
4 changed files with 239 additions and 6 deletions

View File

@ -25,17 +25,27 @@ import java.io.Reader;
public class RestResponse {
private HttpResponse response;
private Reader reader;
RestResponse(HttpResponse response) {
this.response = response;
}
public Reader getReader() throws IllegalStateException, IOException {
Reader reader = new InputStreamReader(response.getEntity().getContent());
reader.skip(JSON_MAGIC.length);
if (reader == null && response.getEntity() != null) {
reader = new InputStreamReader(response.getEntity().getContent());
reader.skip(JSON_MAGIC.length);
}
return reader;
}
public void consume() throws IllegalStateException, IOException {
Reader reader = getReader();
if (reader != null) {
while (reader.read() != -1);
}
}
public int getStatusCode() {
return response.getStatusLine().getStatusCode();
}

View File

@ -18,6 +18,7 @@ import com.google.gson.Gson;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.entity.StringEntity;
@ -59,6 +60,11 @@ public class RestSession {
return null;
}
public RestResponse delete(String endPoint) throws IOException {
HttpDelete delete = new HttpDelete("http://localhost:8080/a" + endPoint);
return new RestResponse(getClient().execute(delete));
}
private DefaultHttpClient getClient() {
if (client == null) {
client = new DefaultHttpClient();

View File

@ -17,6 +17,7 @@ package com.google.gerrit.acceptance.rest.group;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import com.google.gerrit.extensions.restapi.Url;
import com.google.gerrit.reviewdb.client.AccountGroup;
import java.util.Set;
@ -35,15 +36,15 @@ public class GroupAssert {
// 'name' is not set if returned in a map
assertEquals(group.getName(), info.name);
}
assertEquals(group.getGroupUUID().get(), info.id);
assertEquals(group.getGroupUUID().get(), Url.decode(info.id));
assertEquals(Integer.valueOf(group.getId().get()), info.group_id);
assertEquals("#/admin/groups/uuid-" + group.getGroupUUID().get(), info.url);
assertEquals("#/admin/groups/uuid-" + Url.encode(group.getGroupUUID().get()), info.url);
assertEquals(group.isVisibleToAll(), toBoolean(info.options.visible_to_all));
assertEquals(group.getDescription(), info.description);
assertEquals(group.getOwnerGroupUUID().get(), info.owner_id);
assertEquals(group.getOwnerGroupUUID().get(), Url.decode(info.owner_id));
}
private static boolean toBoolean(Boolean b) {
public static boolean toBoolean(Boolean b) {
if (b == null) {
return false;
}

View File

@ -0,0 +1,216 @@
// Copyright (C) 2013 The Android Open Source Project
//
// Licensed 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 com.google.gerrit.acceptance.rest.group;
import static com.google.gerrit.acceptance.rest.group.GroupAssert.toBoolean;
import static com.google.gerrit.acceptance.rest.group.GroupAssert.assertGroupInfo;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import com.google.common.reflect.TypeToken;
import com.google.gerrit.acceptance.AbstractDaemonTest;
import com.google.gerrit.acceptance.AccountCreator;
import com.google.gerrit.acceptance.RestResponse;
import com.google.gerrit.acceptance.RestSession;
import com.google.gerrit.acceptance.TestAccount;
import com.google.gerrit.reviewdb.client.AccountGroup;
import com.google.gerrit.server.account.GroupCache;
import com.google.gson.Gson;
import com.google.inject.Inject;
import org.apache.http.HttpStatus;
import org.junit.Before;
import org.junit.Test;
import java.io.IOException;
public class GroupPropertiesIT extends AbstractDaemonTest {
@Inject
private AccountCreator accounts;
@Inject
private GroupCache groupCache;
private TestAccount admin;
private RestSession session;
@Before
public void setUp() throws Exception {
admin = accounts.create("admin", "admin@example.com", "Administrator",
"Administrators");
session = new RestSession(admin);
}
@Test
public void testGroupName() throws IOException {
AccountGroup.NameKey adminGroupName = new AccountGroup.NameKey("Administrators");
String url = "/groups/" + groupCache.get(adminGroupName).getGroupUUID().get() + "/name";
// get name
RestResponse r = session.get(url);
@SuppressWarnings("serial")
String name = (new Gson()).fromJson(r.getReader(), new TypeToken<String>() {}.getType());
assertEquals(HttpStatus.SC_OK, r.getStatusCode());
assertEquals("Administrators", name);
r.consume();
// set name with name conflict
GroupNameInput in = new GroupNameInput();
in.name = "Registered Users";
r = session.put(url, in);
assertEquals(HttpStatus.SC_PRECONDITION_FAILED, r.getStatusCode());
r.consume();
// set name to same name
in = new GroupNameInput();
in.name = "Administrators";
r = session.put(url, in);
assertEquals(HttpStatus.SC_OK, r.getStatusCode());
r.consume();
// rename
in = new GroupNameInput();
in.name = "Admins";
r = session.put(url, in);
@SuppressWarnings("serial")
String newName = (new Gson()).fromJson(r.getReader(), new TypeToken<String>() {}.getType());
assertEquals(HttpStatus.SC_OK, r.getStatusCode());
assertNotNull(groupCache.get(new AccountGroup.NameKey(in.name)));
assertNull(groupCache.get(adminGroupName));
assertEquals(in.name, newName);
r.consume();
}
@Test
public void testGroupDescription() throws IOException {
AccountGroup.NameKey adminGroupName = new AccountGroup.NameKey("Administrators");
AccountGroup adminGroup = groupCache.get(adminGroupName);
String url = "/groups/" + adminGroup.getGroupUUID().get() + "/description";
// get description
RestResponse r = session.get(url);
@SuppressWarnings("serial")
String description = (new Gson()).fromJson(r.getReader(), new TypeToken<String>() {}.getType());
assertEquals(HttpStatus.SC_OK, r.getStatusCode());
assertEquals(adminGroup.getDescription(), description);
r.consume();
// set description
GroupDescriptionInput in = new GroupDescriptionInput();
in.description = "All users that can administrate the Gerrit Server.";
r = session.put(url, in);
@SuppressWarnings("serial")
String newDescription = (new Gson()).fromJson(r.getReader(), new TypeToken<String>() {}.getType());
assertEquals(HttpStatus.SC_OK, r.getStatusCode());
assertEquals(in.description, newDescription);
adminGroup = groupCache.get(adminGroupName);
assertEquals(in.description, adminGroup.getDescription());
r.consume();
// delete description
r = session.delete(url);
assertEquals(HttpStatus.SC_NO_CONTENT, r.getStatusCode());
adminGroup = groupCache.get(adminGroupName);
assertNull(adminGroup.getDescription());
}
@Test
public void testGroupOptions() throws IOException {
AccountGroup.NameKey adminGroupName = new AccountGroup.NameKey("Administrators");
AccountGroup adminGroup = groupCache.get(adminGroupName);
String url = "/groups/" + adminGroup.getGroupUUID().get() + "/options";
// get options
RestResponse r = session.get(url);
@SuppressWarnings("serial")
GroupOptionsInfo options = (new Gson()).fromJson(r.getReader(), new TypeToken<GroupOptionsInfo>() {}.getType());
assertEquals(HttpStatus.SC_OK, r.getStatusCode());
assertEquals(adminGroup.isVisibleToAll(), toBoolean(options.visible_to_all));
r.consume();
// set options
GroupOptionsInput in = new GroupOptionsInput();
in.visible_to_all = !adminGroup.isVisibleToAll();
r = session.put(url, in);
@SuppressWarnings("serial")
GroupOptionsInfo newOptions = (new Gson()).fromJson(r.getReader(), new TypeToken<GroupOptionsInfo>() {}.getType());
assertEquals(HttpStatus.SC_OK, r.getStatusCode());
assertEquals(in.visible_to_all, toBoolean(newOptions.visible_to_all));
adminGroup = groupCache.get(adminGroupName);
assertEquals(in.visible_to_all, adminGroup.isVisibleToAll());
r.consume();
}
@Test
public void testGroupOwner() throws IOException {
AccountGroup.NameKey adminGroupName = new AccountGroup.NameKey("Administrators");
AccountGroup adminGroup = groupCache.get(adminGroupName);
String url = "/groups/" + adminGroup.getGroupUUID().get() + "/owner";
// get owner
RestResponse r = session.get(url);
@SuppressWarnings("serial")
GroupInfo options = (new Gson()).fromJson(r.getReader(), new TypeToken<GroupInfo>() {}.getType());
assertEquals(HttpStatus.SC_OK, r.getStatusCode());
assertGroupInfo(groupCache.get(adminGroup.getOwnerGroupUUID()), options);
r.consume();
// set owner by name
GroupOwnerInput in = new GroupOwnerInput();
in.owner = "Registered Users";
r = session.put(url, in);
@SuppressWarnings("serial")
GroupInfo newOwner = (new Gson()).fromJson(r.getReader(), new TypeToken<GroupInfo>() {}.getType());
assertEquals(HttpStatus.SC_OK, r.getStatusCode());
assertEquals(in.owner, newOwner.name);
adminGroup = groupCache.get(adminGroupName);
assertGroupInfo(groupCache.get(adminGroup.getOwnerGroupUUID()), newOwner);
r.consume();
// set owner by UUID
in = new GroupOwnerInput();
in.owner = adminGroup.getGroupUUID().get();
r = session.put(url, in);
assertEquals(HttpStatus.SC_OK, r.getStatusCode());
adminGroup = groupCache.get(adminGroupName);
assertEquals(in.owner, groupCache.get(adminGroup.getOwnerGroupUUID()).getGroupUUID().get());
r.consume();
// set non existing owner
in = new GroupOwnerInput();
in.owner = "Non-Existing Group";
r = session.put(url, in);
assertEquals(HttpStatus.SC_BAD_REQUEST, r.getStatusCode());
r.consume();
}
private static class GroupNameInput {
String name;
}
private static class GroupDescriptionInput {
String description;
}
private static class GroupOptionsInput {
Boolean visible_to_all;
}
private static class GroupOwnerInput {
String owner;
}
}