Add acceptance test for getting/setting group properties via REST
The new tests checks that getting/setting the group properties (name, description, options and owner) works via REST. Change-Id: I22589cbdcd1f8eae42e51c2b02f20ce9ff6ac4f5 Signed-off-by: Edwin Kempin <edwin.kempin@sap.com>
This commit is contained in:
@@ -25,17 +25,27 @@ import java.io.Reader;
|
|||||||
public class RestResponse {
|
public class RestResponse {
|
||||||
|
|
||||||
private HttpResponse response;
|
private HttpResponse response;
|
||||||
|
private Reader reader;
|
||||||
|
|
||||||
RestResponse(HttpResponse response) {
|
RestResponse(HttpResponse response) {
|
||||||
this.response = response;
|
this.response = response;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Reader getReader() throws IllegalStateException, IOException {
|
public Reader getReader() throws IllegalStateException, IOException {
|
||||||
Reader reader = new InputStreamReader(response.getEntity().getContent());
|
if (reader == null && response.getEntity() != null) {
|
||||||
|
reader = new InputStreamReader(response.getEntity().getContent());
|
||||||
reader.skip(JSON_MAGIC.length);
|
reader.skip(JSON_MAGIC.length);
|
||||||
|
}
|
||||||
return reader;
|
return reader;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void consume() throws IllegalStateException, IOException {
|
||||||
|
Reader reader = getReader();
|
||||||
|
if (reader != null) {
|
||||||
|
while (reader.read() != -1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public int getStatusCode() {
|
public int getStatusCode() {
|
||||||
return response.getStatusLine().getStatusCode();
|
return response.getStatusLine().getStatusCode();
|
||||||
}
|
}
|
||||||
|
@@ -18,6 +18,7 @@ import com.google.gson.Gson;
|
|||||||
|
|
||||||
import org.apache.http.auth.AuthScope;
|
import org.apache.http.auth.AuthScope;
|
||||||
import org.apache.http.auth.UsernamePasswordCredentials;
|
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.HttpGet;
|
||||||
import org.apache.http.client.methods.HttpPut;
|
import org.apache.http.client.methods.HttpPut;
|
||||||
import org.apache.http.entity.StringEntity;
|
import org.apache.http.entity.StringEntity;
|
||||||
@@ -59,6 +60,11 @@ public class RestSession {
|
|||||||
return null;
|
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() {
|
private DefaultHttpClient getClient() {
|
||||||
if (client == null) {
|
if (client == null) {
|
||||||
client = new DefaultHttpClient();
|
client = new DefaultHttpClient();
|
||||||
|
@@ -17,6 +17,7 @@ package com.google.gerrit.acceptance.rest.group;
|
|||||||
import static org.junit.Assert.assertEquals;
|
import static org.junit.Assert.assertEquals;
|
||||||
import static org.junit.Assert.assertTrue;
|
import static org.junit.Assert.assertTrue;
|
||||||
|
|
||||||
|
import com.google.gerrit.extensions.restapi.Url;
|
||||||
import com.google.gerrit.reviewdb.client.AccountGroup;
|
import com.google.gerrit.reviewdb.client.AccountGroup;
|
||||||
|
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
@@ -35,15 +36,15 @@ public class GroupAssert {
|
|||||||
// 'name' is not set if returned in a map
|
// 'name' is not set if returned in a map
|
||||||
assertEquals(group.getName(), info.name);
|
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(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.isVisibleToAll(), toBoolean(info.options.visible_to_all));
|
||||||
assertEquals(group.getDescription(), info.description);
|
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) {
|
if (b == null) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
@@ -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;
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user