Add acceptance tests for group creation via REST
Change-Id: I606af16e34fda221a1c8834d995bffb744b8d3a1 Signed-off-by: Edwin Kempin <edwin.kempin@sap.com>
This commit is contained in:
@@ -0,0 +1,42 @@
|
||||
// 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;
|
||||
|
||||
import static com.google.gerrit.httpd.restapi.RestApiServlet.JSON_MAGIC;
|
||||
|
||||
import org.apache.http.HttpResponse;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.Reader;
|
||||
|
||||
public class RestResponse {
|
||||
|
||||
private HttpResponse response;
|
||||
|
||||
RestResponse(HttpResponse response) {
|
||||
this.response = response;
|
||||
}
|
||||
|
||||
public Reader getReader() throws IllegalStateException, IOException {
|
||||
Reader reader = new InputStreamReader(response.getEntity().getContent());
|
||||
reader.skip(JSON_MAGIC.length);
|
||||
return reader;
|
||||
}
|
||||
|
||||
public int getStatusCode() {
|
||||
return response.getStatusLine().getStatusCode();
|
||||
}
|
||||
}
|
||||
@@ -14,17 +14,18 @@
|
||||
|
||||
package com.google.gerrit.acceptance;
|
||||
|
||||
import static com.google.gerrit.httpd.restapi.RestApiServlet.JSON_MAGIC;
|
||||
import com.google.gson.Gson;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.Reader;
|
||||
|
||||
import org.apache.http.HttpResponse;
|
||||
import org.apache.http.auth.AuthScope;
|
||||
import org.apache.http.auth.UsernamePasswordCredentials;
|
||||
import org.apache.http.client.methods.HttpGet;
|
||||
import org.apache.http.client.methods.HttpPut;
|
||||
import org.apache.http.entity.StringEntity;
|
||||
import org.apache.http.impl.client.DefaultHttpClient;
|
||||
import org.apache.http.message.BasicHeader;
|
||||
import org.apache.http.protocol.HTTP;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
public class RestSession {
|
||||
|
||||
@@ -35,15 +36,25 @@ public class RestSession {
|
||||
this.account = account;
|
||||
}
|
||||
|
||||
public Reader get(String endPoint) throws IOException {
|
||||
public RestResponse get(String endPoint) throws IOException {
|
||||
HttpGet get = new HttpGet("http://localhost:8080/a" + endPoint);
|
||||
HttpResponse response = getClient().execute(get);
|
||||
Reader reader = new InputStreamReader(response.getEntity().getContent());
|
||||
reader.skip(JSON_MAGIC.length);
|
||||
return reader;
|
||||
return new RestResponse(getClient().execute(get));
|
||||
}
|
||||
|
||||
public Reader post(String endPoint) {
|
||||
public RestResponse put(String endPoint) throws IOException {
|
||||
return put(endPoint, null);
|
||||
}
|
||||
|
||||
public RestResponse put(String endPoint, Object content) throws IOException {
|
||||
HttpPut put = new HttpPut("http://localhost:8080/a" + endPoint);
|
||||
if (content != null) {
|
||||
put.addHeader(new BasicHeader("Content-Type", "application/json"));
|
||||
put.setEntity(new StringEntity((new Gson()).toJson(content), HTTP.UTF_8));
|
||||
}
|
||||
return new RestResponse(getClient().execute(put));
|
||||
}
|
||||
|
||||
public RestResponse post(String endPoint) {
|
||||
// TODO
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -16,14 +16,6 @@ package com.google.gerrit.acceptance;
|
||||
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.Reader;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import com.google.common.collect.Sets;
|
||||
import com.google.common.reflect.TypeToken;
|
||||
import com.google.gerrit.acceptance.rest.group.GroupInfo;
|
||||
@@ -33,8 +25,16 @@ import com.google.gson.Gson;
|
||||
import com.google.gwtorm.server.OrmException;
|
||||
import com.google.gwtorm.server.SchemaFactory;
|
||||
import com.google.inject.Inject;
|
||||
|
||||
import com.jcraft.jsch.JSchException;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* An example test that tests presence of system groups in a newly initialized
|
||||
* review site.
|
||||
@@ -73,11 +73,11 @@ public class SystemGroupsIT extends AbstractDaemonTest {
|
||||
@Test
|
||||
public void systemGroupsCreated_rest() throws IOException {
|
||||
RestSession session = new RestSession(admin);
|
||||
Reader r = session.get("/groups/");
|
||||
RestResponse r = session.get("/groups/");
|
||||
Gson gson = new Gson();
|
||||
@SuppressWarnings("serial")
|
||||
Map<String, GroupInfo> result =
|
||||
gson.fromJson(r, new TypeToken<Map<String, GroupInfo>>() {}.getType());
|
||||
gson.fromJson(r.getReader(), new TypeToken<Map<String, GroupInfo>>() {}.getType());
|
||||
Set<String> names = result.keySet();
|
||||
assertTrue(names.contains("Administrators"));
|
||||
assertTrue(names.contains("Anonymous Users"));
|
||||
|
||||
@@ -0,0 +1,108 @@
|
||||
// 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.assertGroupInfo;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
|
||||
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.gwtorm.server.OrmException;
|
||||
import com.google.inject.Inject;
|
||||
|
||||
import com.jcraft.jsch.JSchException;
|
||||
|
||||
import org.apache.http.HttpStatus;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
public class CreateGroupIT 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 testCreateGroup() throws IOException {
|
||||
final String newGroupName = "newGroup";
|
||||
RestResponse r = session.put("/groups/" + newGroupName);
|
||||
@SuppressWarnings("serial")
|
||||
GroupInfo g = (new Gson()).fromJson(r.getReader(), new TypeToken<GroupInfo>() {}.getType());
|
||||
assertEquals(newGroupName, g.name);
|
||||
AccountGroup group = groupCache.get(new AccountGroup.NameKey(newGroupName));
|
||||
assertNotNull(group);
|
||||
assertGroupInfo(group, g);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCreateGroupWithProperties() throws IOException {
|
||||
final String newGroupName = "newGroup";
|
||||
GroupInput in = new GroupInput();
|
||||
in.description = "Test description";
|
||||
in.visible_to_all = true;
|
||||
in.owner_id = groupCache.get(new AccountGroup.NameKey("Administrators")).getGroupUUID().get();
|
||||
RestResponse r = session.put("/groups/" + newGroupName, in);
|
||||
@SuppressWarnings("serial")
|
||||
GroupInfo g = (new Gson()).fromJson(r.getReader(), new TypeToken<GroupInfo>() {}.getType());
|
||||
assertEquals(newGroupName, g.name);
|
||||
AccountGroup group = groupCache.get(new AccountGroup.NameKey(newGroupName));
|
||||
assertEquals(in.description, group.getDescription());
|
||||
assertEquals(in.visible_to_all, group.isVisibleToAll());
|
||||
assertEquals(in.owner_id, group.getOwnerGroupUUID().get());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCreateGroupWithoutCapability_Forbidden() throws OrmException,
|
||||
JSchException, IOException {
|
||||
TestAccount user = accounts.create("user", "user@example.com", "User");
|
||||
RestResponse r = (new RestSession(user)).put("/groups/newGroup");
|
||||
assertEquals(HttpStatus.SC_FORBIDDEN, r.getStatusCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCreateGroupWhenGroupAlreadyExists_PreconditionFailed()
|
||||
throws OrmException, JSchException, IOException {
|
||||
RestResponse r = session.put("/groups/Administrators");
|
||||
assertEquals(HttpStatus.SC_PRECONDITION_FAILED, r.getStatusCode());
|
||||
}
|
||||
|
||||
private static class GroupInput {
|
||||
String description;
|
||||
Boolean visible_to_all;
|
||||
String owner_id;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
// 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 org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import com.google.gerrit.reviewdb.client.AccountGroup;
|
||||
|
||||
import java.util.Set;
|
||||
|
||||
public class GroupAssert {
|
||||
|
||||
public static void assertGroups(Iterable<String> expected, Set<String> actual) {
|
||||
for (String g : expected) {
|
||||
assertTrue("missing group " + g, actual.remove(g));
|
||||
}
|
||||
assertTrue("unexpected groups: " + actual, actual.isEmpty());
|
||||
}
|
||||
|
||||
public static void assertGroupInfo(AccountGroup group, GroupInfo info) {
|
||||
if (info.name != null) {
|
||||
// 'name' is not set if returned in a map
|
||||
assertEquals(group.getName(), info.name);
|
||||
}
|
||||
assertEquals(group.getGroupUUID().get(), info.id);
|
||||
assertEquals(Integer.valueOf(group.getId().get()), info.group_id);
|
||||
assertEquals("#/admin/groups/uuid-" + 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);
|
||||
}
|
||||
|
||||
private static boolean toBoolean(Boolean b) {
|
||||
if (b == null) {
|
||||
return false;
|
||||
}
|
||||
return b.booleanValue();
|
||||
}
|
||||
}
|
||||
@@ -14,8 +14,8 @@
|
||||
|
||||
package com.google.gerrit.acceptance.rest.group;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static com.google.gerrit.acceptance.rest.group.GroupAssert.assertGroupInfo;
|
||||
import static com.google.gerrit.acceptance.rest.group.GroupAssert.assertGroups;
|
||||
|
||||
import com.google.common.base.Function;
|
||||
import com.google.common.collect.Iterables;
|
||||
@@ -23,6 +23,7 @@ import com.google.common.collect.Sets;
|
||||
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;
|
||||
@@ -37,7 +38,6 @@ import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.Reader;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
@@ -71,10 +71,10 @@ public class ListGroupsIT extends AbstractDaemonTest {
|
||||
return group.getName();
|
||||
}
|
||||
});
|
||||
Reader r = session.get("/groups/");
|
||||
RestResponse r = session.get("/groups/");
|
||||
@SuppressWarnings("serial")
|
||||
Map<String, GroupInfo> result =
|
||||
(new Gson()).fromJson(r, new TypeToken<Map<String, GroupInfo>>() {}.getType());
|
||||
(new Gson()).fromJson(r.getReader(), new TypeToken<Map<String, GroupInfo>>() {}.getType());
|
||||
assertGroups(expectedGroups, result.keySet());
|
||||
}
|
||||
|
||||
@@ -85,10 +85,10 @@ public class ListGroupsIT extends AbstractDaemonTest {
|
||||
expectedGroups.add("Anonymous Users");
|
||||
expectedGroups.add("Registered Users");
|
||||
TestAccount user = accounts.create("user", "user@example.com", "User");
|
||||
Reader r = (new RestSession(user)).get("/groups/");
|
||||
RestResponse r = (new RestSession(user)).get("/groups/");
|
||||
@SuppressWarnings("serial")
|
||||
Map<String, GroupInfo> result =
|
||||
(new Gson()).fromJson(r, new TypeToken<Map<String, GroupInfo>>() {}.getType());
|
||||
(new Gson()).fromJson(r.getReader(), new TypeToken<Map<String, GroupInfo>>() {}.getType());
|
||||
assertGroups(expectedGroups, result.keySet());
|
||||
}
|
||||
|
||||
@@ -96,35 +96,11 @@ public class ListGroupsIT extends AbstractDaemonTest {
|
||||
public void testAllGroupInfoFieldsSetCorrectly() throws IOException,
|
||||
OrmException {
|
||||
AccountGroup adminGroup = groupCache.get(new AccountGroup.NameKey("Administrators"));
|
||||
Reader r = session.get("/groups/?q=" + adminGroup.getName());
|
||||
RestResponse r = session.get("/groups/?q=" + adminGroup.getName());
|
||||
@SuppressWarnings("serial")
|
||||
Map<String, GroupInfo> result =
|
||||
(new Gson()).fromJson(r, new TypeToken<Map<String, GroupInfo>>() {}.getType());
|
||||
(new Gson()).fromJson(r.getReader(), new TypeToken<Map<String, GroupInfo>>() {}.getType());
|
||||
GroupInfo adminGroupInfo = result.get(adminGroup.getName());
|
||||
assertEquals(adminGroup.getGroupUUID().get(), adminGroupInfo.id);
|
||||
assertEquals(Integer.valueOf(adminGroup.getId().get()), adminGroupInfo.group_id);
|
||||
assertEquals("#/admin/groups/uuid-" + adminGroup.getGroupUUID().get(), adminGroupInfo.url);
|
||||
assertEquals(adminGroup.isVisibleToAll(), toBoolean(adminGroupInfo.options.visible_to_all));
|
||||
assertEquals(adminGroup.getDescription(), adminGroupInfo.description);
|
||||
assertEquals(adminGroup.getOwnerGroupUUID().get(), adminGroupInfo.owner_id);
|
||||
if (adminGroupInfo.name != null) {
|
||||
// 'name' is not set if returned in a map,
|
||||
// but if 'name' is set it must contain the correct name
|
||||
assertEquals(adminGroup.getName(), adminGroupInfo.name);
|
||||
}
|
||||
}
|
||||
|
||||
private static void assertGroups(Iterable<String> expected, Set<String> actual) {
|
||||
for (String g : expected) {
|
||||
assertTrue("missing group " + g, actual.remove(g));
|
||||
}
|
||||
assertTrue("unexpected groups: " + actual, actual.isEmpty());
|
||||
}
|
||||
|
||||
private static boolean toBoolean(Boolean b) {
|
||||
if (b == null) {
|
||||
return false;
|
||||
}
|
||||
return b.booleanValue();
|
||||
assertGroupInfo(adminGroup, adminGroupInfo);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user