if @dashboard.editable_by?(current_user)
render :partial => 'edit_form', :resource => params[:resource]
else
- redirect_to :action => 'index', :resource => params[:resource], :status => 401
+ access_denied
end
end
render :partial => 'dashboards/edit_form', :status => 400, :resource => params[:resource]
end
else
- render :text => @dashboard.id.to_s, :resource => params[:resource], :status => 401
+ access_denied
end
end
*/
public class DefaultUserClient implements UserClient {
+ private static final String BASE_URL = "/api/users/";
+ private static final String SEARCH_URL = BASE_URL + "search";
+ private static final String CREATE_URL = BASE_URL + "create";
+ private static final String UPDATE_URL = BASE_URL + "update";
+ private static final String DELETE_URL = BASE_URL + "delete";
+
private final HttpRequestFactory requestFactory;
/**
@Override
public List<User> find(UserQuery query) {
- String json = requestFactory.get(UserQuery.BASE_URL, query.urlParams());
+ String json = requestFactory.get(SEARCH_URL, query.urlParams());
List<User> result = new ArrayList<User>();
Map jsonRoot = (Map) JSONValue.parse(json);
List<Map> jsonUsers = (List<Map>) jsonRoot.get("users");
return result;
}
+ @Override
+ public User create(UserParameters userParameters) {
+ String json = requestFactory.post(CREATE_URL, userParameters.urlParams());
+ Map jsonRoot = (Map) JSONValue.parse(json);
+ Map jsonUser = (Map) jsonRoot.get("user");
+ return new User(jsonUser);
+ }
+
+ @Override
+ public User update(UserParameters userParameters) {
+ String json = requestFactory.post(UPDATE_URL, userParameters.urlParams());
+ Map jsonRoot = (Map) JSONValue.parse(json);
+ Map jsonUser = (Map) jsonRoot.get("user");
+ return new User(jsonUser);
+ }
+
+ @Override
+ public void delete(UserParameters userParameters) {
+ requestFactory.post(DELETE_URL, userParameters.urlParams());
+ }
}
List<User> find(UserQuery query);
+ User create(UserParameters userParameters);
+
+ User update(UserParameters userParameters);
+
+ void delete(UserParameters userParameters);
}
--- /dev/null
+package org.sonar.wsclient.user;
+
+import java.util.HashMap;
+import java.util.Map;
+
+public class UserParameters {
+
+ private final Map<String, Object> params = new HashMap<String, Object>();
+
+ private UserParameters() {
+ }
+
+ public static UserParameters create() {
+ return new UserParameters();
+ }
+
+ public Map<String, Object> urlParams() {
+ return params;
+ }
+
+ public UserParameters login(String s) {
+ params.put("login", s);
+ return this;
+ }
+
+ public UserParameters name(String s) {
+ params.put("name", s);
+ return this;
+ }
+
+ public UserParameters password(String s) {
+ params.put("password", s);
+ return this;
+ }
+
+ public UserParameters passwordConfirmation(String s) {
+ params.put("password_confirmation", s);
+ return this;
+ }
+
+ public UserParameters email(String s) {
+ params.put("email", s);
+ return this;
+ }
+}
* @since 3.6
*/
public class UserQuery {
- static final String BASE_URL = "/api/users/search";
private final Map<String, Object> params = new HashMap<String, Object>();
server.stop();
}
- public MockHttpServerInterceptor doReturnBody(String body) {
+ public MockHttpServerInterceptor stubResponseBody(String body) {
server.doReturnBody(body);
return this;
}
- public MockHttpServerInterceptor doReturnStatus(int status) {
+ public MockHttpServerInterceptor stubStatusCode(int status) {
server.doReturnStatus(status);
return this;
}
@Test
public void test_get() {
- httpServer.doReturnStatus(200).doReturnBody("{'issues': []}");
+ httpServer.stubStatusCode(200).stubResponseBody("{'issues': []}");
HttpRequestFactory factory = new HttpRequestFactory(httpServer.url());
String json = factory.get("/api/issues", Collections.<String, Object> emptyMap());
@Test
public void test_post() {
- httpServer.doReturnStatus(200).doReturnBody("{}");
+ httpServer.stubStatusCode(200).stubResponseBody("{}");
HttpRequestFactory factory = new HttpRequestFactory(httpServer.url());
String json = factory.post("/api/issues/change", Collections.<String, Object> emptyMap());
@Test
public void test_authentication() {
- httpServer.doReturnStatus(200).doReturnBody("{}");
+ httpServer.stubStatusCode(200).stubResponseBody("{}");
HttpRequestFactory factory = new HttpRequestFactory(httpServer.url()).setLogin("karadoc").setPassword("legrascestlavie");
String json = factory.get("/api/issues", Collections.<String, Object> emptyMap());
@Test
public void should_encode_characters() {
HttpRequestFactory requestFactory = new HttpRequestFactory(httpServer.url());
- httpServer.doReturnBody("{\"issues\": [{\"key\": \"ABCDE\"}]}");
+ httpServer.stubResponseBody("{\"issues\": [{\"key\": \"ABCDE\"}]}");
IssueClient client = new DefaultIssueClient(requestFactory);
client.find(IssueQuery.create().issues("ABC DE"));
@Test
public void should_find_action_plans() {
HttpRequestFactory requestFactory = new HttpRequestFactory(httpServer.url());
- httpServer.doReturnBody("{\"actionPlans\": [{\"key\": \"382f6f2e-ad9d-424a-b973-9b065e04348a\",\n" +
+ httpServer.stubResponseBody("{\"actionPlans\": [{\"key\": \"382f6f2e-ad9d-424a-b973-9b065e04348a\",\n" +
"\"name\": \"Long term\",\n" +
"\"desc\": \"Long term acton plan\",\n" +
"\"status\": \"CLOSED\",\n" +
@Test
public void should_create_action_plan() throws Exception {
HttpRequestFactory requestFactory = new HttpRequestFactory(httpServer.url());
- httpServer.doReturnBody("{\"actionPlan\": {\"key\": \"382f6f2e-ad9d-424a-b973-9b065e04348a\"}}");
+ httpServer.stubResponseBody("{\"actionPlan\": {\"key\": \"382f6f2e-ad9d-424a-b973-9b065e04348a\"}}");
ActionPlanClient client = new DefaultActionPlanClient(requestFactory);
ActionPlan result = client.create(
@Test
public void should_update_action_plan() {
HttpRequestFactory requestFactory = new HttpRequestFactory(httpServer.url());
- httpServer.doReturnBody("{\"actionPlan\": {\"key\": \"382f6f2e-ad9d-424a-b973-9b065e04348a\"}}");
+ httpServer.stubResponseBody("{\"actionPlan\": {\"key\": \"382f6f2e-ad9d-424a-b973-9b065e04348a\"}}");
ActionPlanClient client = new DefaultActionPlanClient(requestFactory);
ActionPlan result = client.update(
@Test
public void should_fail_to_delete_action_plan() {
HttpRequestFactory requestFactory = new HttpRequestFactory(httpServer.url());
- httpServer.doReturnStatus(500);
+ httpServer.stubStatusCode(500);
ActionPlanClient client = new DefaultActionPlanClient(requestFactory);
try {
@Test
public void should_open_action_plan() {
HttpRequestFactory requestFactory = new HttpRequestFactory(httpServer.url());
- httpServer.doReturnBody("{\"actionPlan\": {\"key\": \"382f6f2e-ad9d-424a-b973-9b065e04348a\"}}");
+ httpServer.stubResponseBody("{\"actionPlan\": {\"key\": \"382f6f2e-ad9d-424a-b973-9b065e04348a\"}}");
ActionPlanClient client = new DefaultActionPlanClient(requestFactory);
ActionPlan result = client.open("382f6f2e-ad9d-424a-b973-9b065e04348a");
@Test
public void should_close_action_plan() {
HttpRequestFactory requestFactory = new HttpRequestFactory(httpServer.url());
- httpServer.doReturnBody("{\"actionPlan\": {\"key\": \"382f6f2e-ad9d-424a-b973-9b065e04348a\"}}");
+ httpServer.stubResponseBody("{\"actionPlan\": {\"key\": \"382f6f2e-ad9d-424a-b973-9b065e04348a\"}}");
ActionPlanClient client = new DefaultActionPlanClient(requestFactory);
ActionPlan result = client.close("382f6f2e-ad9d-424a-b973-9b065e04348a");
import org.sonar.wsclient.base.HttpException;
import org.sonar.wsclient.internal.HttpRequestFactory;
import org.sonar.wsclient.issue.*;
-import org.sonar.wsclient.issue.internal.DefaultIssueClient;
import java.util.List;
@Test
public void should_find_issues() {
HttpRequestFactory requestFactory = new HttpRequestFactory(httpServer.url());
- httpServer.doReturnBody("{\"issues\": [{\"key\": \"ABCDE\"}]}");
+ httpServer.stubResponseBody("{\"issues\": [{\"key\": \"ABCDE\"}]}");
IssueClient client = new DefaultIssueClient(requestFactory);
IssueQuery query = IssueQuery.create().issues("ABCDE");
@Test
public void should_fail_to_find_issues() {
HttpRequestFactory requestFactory = new HttpRequestFactory(httpServer.url());
- httpServer.doReturnStatus(500);
+ httpServer.stubStatusCode(500);
IssueClient client = new DefaultIssueClient(requestFactory);
try {
@Test
public void should_set_severity() {
HttpRequestFactory requestFactory = new HttpRequestFactory(httpServer.url());
- httpServer.doReturnBody("{\"issue\": {\"key\": \"ABCDE\"}}");
+ httpServer.stubResponseBody("{\"issue\": {\"key\": \"ABCDE\"}}");
IssueClient client = new DefaultIssueClient(requestFactory);
Issue result = client.setSeverity("ABCDE", "BLOCKER");
@Test
public void should_assign() {
HttpRequestFactory requestFactory = new HttpRequestFactory(httpServer.url());
- httpServer.doReturnBody("{\"issue\": {\"key\": \"ABCDE\"}}");
+ httpServer.stubResponseBody("{\"issue\": {\"key\": \"ABCDE\"}}");
IssueClient client = new DefaultIssueClient(requestFactory);
Issue result = client.assign("ABCDE", "emmerik");
@Test
public void should_unassign() {
HttpRequestFactory requestFactory = new HttpRequestFactory(httpServer.url());
- httpServer.doReturnBody("{\"issue\": {\"key\": \"ABCDE\"}}");
+ httpServer.stubResponseBody("{\"issue\": {\"key\": \"ABCDE\"}}");
IssueClient client = new DefaultIssueClient(requestFactory);
Issue result = client.assign("ABCDE", null);
@Test
public void should_plan() {
HttpRequestFactory requestFactory = new HttpRequestFactory(httpServer.url());
- httpServer.doReturnBody("{\"issue\": {\"key\": \"ABCDE\"}}");
+ httpServer.stubResponseBody("{\"issue\": {\"key\": \"ABCDE\"}}");
IssueClient client = new DefaultIssueClient(requestFactory);
Issue result = client.plan("ABCDE", "DEFGH");
@Test
public void should_unplan() {
HttpRequestFactory requestFactory = new HttpRequestFactory(httpServer.url());
- httpServer.doReturnBody("{\"issue\": {\"key\": \"ABCDE\"}}");
+ httpServer.stubResponseBody("{\"issue\": {\"key\": \"ABCDE\"}}");
IssueClient client = new DefaultIssueClient(requestFactory);
Issue result = client.plan("ABCDE", null);
@Test
public void should_create_issue() {
HttpRequestFactory requestFactory = new HttpRequestFactory(httpServer.url());
- httpServer.doReturnBody("{\"issue\": {\"key\": \"ABCDE\"}}");
+ httpServer.stubResponseBody("{\"issue\": {\"key\": \"ABCDE\"}}");
IssueClient client = new DefaultIssueClient(requestFactory);
Issue result = client.create(NewIssue.create().component("Action.java").rule("squid:AvoidCycle"));
@Test
public void should_get_transitions() {
HttpRequestFactory requestFactory = new HttpRequestFactory(httpServer.url());
- httpServer.doReturnBody("{\n" +
+ httpServer.stubResponseBody("{\n" +
" \"transitions\": [\n" +
" \"resolve\",\n" +
" \"falsepositive\"\n" +
@Test
public void should_apply_transition() {
HttpRequestFactory requestFactory = new HttpRequestFactory(httpServer.url());
- httpServer.doReturnBody("{\"issue\": {\"key\": \"ABCDE\"}}");
+ httpServer.stubResponseBody("{\"issue\": {\"key\": \"ABCDE\"}}");
IssueClient client = new DefaultIssueClient(requestFactory);
Issue result = client.doTransition("ABCDE", "resolve");
@Test
public void should_add_comment() throws Exception {
HttpRequestFactory requestFactory = new HttpRequestFactory(httpServer.url());
- httpServer.doReturnBody(IOUtils.toString(getClass().getResourceAsStream("/org/sonar/wsclient/issue/internal/DefaultIssueClientTest/add_comment_result.json")));
+ httpServer.stubResponseBody(IOUtils.toString(getClass().getResourceAsStream("/org/sonar/wsclient/issue/internal/DefaultIssueClientTest/add_comment_result.json")));
IssueClient client = new DefaultIssueClient(requestFactory);
IssueComment comment = client.addComment("ISSUE-1", "this is my comment");
@Test
public void should_get_actions() {
HttpRequestFactory requestFactory = new HttpRequestFactory(httpServer.url());
- httpServer.doReturnBody("{\n" +
+ httpServer.stubResponseBody("{\n" +
" \"actions\": [\n" +
" \"link-to-jira\",\n" +
" \"tweet\"\n" +
@Test
public void should_apply_action() {
HttpRequestFactory requestFactory = new HttpRequestFactory(httpServer.url());
- httpServer.doReturnBody("{\"issue\": {\"key\": \"ABCDE\"}}");
+ httpServer.stubResponseBody("{\"issue\": {\"key\": \"ABCDE\"}}");
IssueClient client = new DefaultIssueClient(requestFactory);
Issue result = client.doAction("ABCDE", "tweet");
*/
package org.sonar.wsclient.user;
+import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.sonar.wsclient.MockHttpServerInterceptor;
import org.sonar.wsclient.internal.HttpRequestFactory;
+import java.util.Arrays;
import java.util.List;
import static org.fest.assertions.Assertions.assertThat;
public class DefaultUserClientTest {
+
+ private HttpRequestFactory requestFactory;
+ private DefaultUserClient client;
+
@Rule
public MockHttpServerInterceptor httpServer = new MockHttpServerInterceptor();
+ @Before
+ public void setUp() {
+ requestFactory = new HttpRequestFactory(httpServer.url());
+ client = new DefaultUserClient(requestFactory);
+ }
+
@Test
public void should_find_issues() {
- HttpRequestFactory requestFactory = new HttpRequestFactory(httpServer.url());
- httpServer.doReturnBody("{\"users\": [{\"login\": \"simon\", \"name\": \"Simon\", \"active\": true}]}");
+ httpServer.stubResponseBody("{\"users\": [{\"login\": \"simon\", \"name\": \"Simon\", \"active\": true}]}");
- UserClient client = new DefaultUserClient(requestFactory);
UserQuery query = UserQuery.create().logins("simon", "loic");
List<User> users = client.find(query);
- assertThat(httpServer.requestedPath()).isEqualTo("/api/users/search?logins=simon,loic");
+ assertThatRequestUrlContains("/api/users/search?", "logins=simon,loic");
assertThat(users).hasSize(1);
User simon = users.get(0);
assertThat(simon.login()).isEqualTo("simon");
assertThat(simon.email()).isNull();
assertThat(simon.active()).isTrue();
}
+
+ @Test
+ public void should_create_user() throws Exception {
+ httpServer.stubResponseBody("{\"user\":{\"login\":\"daveloper\",\"name\":\"daveloper\",\"email\":null}}");
+
+ UserParameters params = UserParameters.create().login("daveloper").password("pass1").passwordConfirmation("pass1");
+ User createdUser = client.create(params);
+
+ assertThatRequestUrlContains("/api/users/create?", "login=daveloper", "password=pass1", "password_confirmation=pass1");
+ assertThat(createdUser).isNotNull();
+ assertThat(createdUser.login()).isEqualTo("daveloper");
+ assertThat(createdUser.name()).isEqualTo("daveloper");
+ assertThat(createdUser.email()).isNull();
+ }
+
+ @Test
+ public void should_update_user() throws Exception {
+ httpServer.stubResponseBody("{\"user\":{\"login\":\"daveloper\",\"name\":\"daveloper\",\"email\":\"new_email\"}}");
+
+ UserParameters params = UserParameters.create().login("daveloper").email("new_email");
+ User updatedUser = client.update(params);
+
+ assertThatRequestUrlContains("/api/users/update?", "login=daveloper", "email=new_email");
+ assertThat(updatedUser).isNotNull();
+ assertThat(updatedUser.login()).isEqualTo("daveloper");
+ assertThat(updatedUser.name()).isEqualTo("daveloper");
+ assertThat(updatedUser.email()).isEqualTo("new_email");
+ }
+
+ @Test
+ public void should_delete_user() throws Exception {
+ httpServer.stubStatusCode(200);
+
+ UserParameters params = UserParameters.create().login("daveloper");
+ client.delete(params);
+
+ assertThatRequestUrlContains("/api/users/delete?", "login=daveloper");
+ }
+
+ private void assertThatRequestUrlContains(String baseUrl, String... parameters) {
+ assertThat(httpServer.requestedPath()).startsWith(baseUrl);
+ List<String> requestParameters = Arrays.asList(httpServer.requestedPath().substring(baseUrl.length()).split("&"));
+ assertThat(requestParameters).containsOnly(parameters);
+ }
}