Skip to content
This repository has been archived by the owner on Oct 29, 2024. It is now read-only.

Move all conduit invocations to JSONObject #78

Merged
merged 3 commits into from
Aug 18, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class ConduitAPIClient {
private static final String API_TOKEN_KEY = "token";
Expand All @@ -55,20 +54,6 @@ public ConduitAPIClient(String conduitURL, String conduitToken) {
this.conduitToken = conduitToken;
}

/**
* Call the conduit API of Phabricator
* @param action Name of the API call
* @param data The data to send to Harbormaster
* @return The result as a JSONObject
* @throws IOException If there was a problem reading the response
* @throws ConduitAPIException If there was an error calling conduit
*/
public JSONObject perform(String action, Map<String, String> data) throws IOException, ConduitAPIException {
JSONObject params = new JSONObject();
params.putAll(data);
return perform(action, params);
}

/**
* Call the conduit API of Phabricator
* @param action Name of the API call
Expand Down Expand Up @@ -98,20 +83,6 @@ public JSONObject perform(String action, JSONObject params) throws IOException,
return (JSONObject)jsonParser.parse(responseBody);
}

/**
* Post a URL-encoded "params" key with a JSON-encoded body as per the Conduit API
* @param action The name of the Conduit method
* @param data The data to be sent to the Conduit method
* @return The request to perform
* @throws UnsupportedEncodingException when the POST data can't be encoded
* @throws ConduitAPIException when the conduit URL is misconfigured
*/
public HttpUriRequest createRequest(String action, Map<String, String> data) throws UnsupportedEncodingException, ConduitAPIException {
JSONObject params = new JSONObject();
params.putAll(data);
return createRequest(action, params);
}

/**
* Post a URL-encoded "params" key with a JSON-encoded body as per the Conduit API
* @param action The name of the Conduit method
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@
import net.sf.json.JSONObject;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
* DifferentialClient handles all interaction with conduit/arc for differentials
Expand All @@ -50,11 +48,11 @@ public DifferentialClient(String diffID, ConduitAPIClient conduit) {
* @throws ConduitAPIException if any error is experienced talking to Conduit
*/
public JSONObject postComment(String revisionID, String message, boolean silent, String action) throws IOException, ConduitAPIException {
Map params = new HashMap<String, String>();
params.put("revision_id", revisionID);
params.put("action", action);
params.put("message", message);
params.put("silent", silent);
JSONObject params = new JSONObject();
params.element("revision_id", revisionID)
.element("action", action)
.element("message", message)
.element("silent", silent);

return this.callConduit("differential.createcomment", params);
}
Expand All @@ -66,8 +64,7 @@ public JSONObject postComment(String revisionID, String message, boolean silent,
* @throws ConduitAPIException if any error is experienced talking to Conduit
*/
public JSONObject fetchDiff() throws IOException, ConduitAPIException {
Map params = new HashMap<String, String>();
params.put("ids", new String[]{diffID});
JSONObject params = new JSONObject().element("ids", new String[]{diffID});
JSONObject query = this.callConduit("differential.querydiffs", params);
JSONObject response;
try {
Expand Down Expand Up @@ -99,10 +96,9 @@ public JSONObject fetchDiff() throws IOException, ConduitAPIException {
* @throws ConduitAPIException if any error is experienced talking to Conduit
*/
public JSONObject sendHarbormasterMessage(String phid, boolean pass) throws ConduitAPIException, IOException {
Map params = new HashMap<String, String>();
params.put("type", pass ? "pass" : "fail");
params.put("buildTargetPHID", phid);

JSONObject params = new JSONObject();
params.element("type", pass ? "pass" : "fail")
.element("buildTargetPHID", phid);
return this.callConduit("harbormaster.sendmessage", params);
}

Expand All @@ -115,15 +111,17 @@ public JSONObject sendHarbormasterMessage(String phid, boolean pass) throws Cond
* @throws ConduitAPIException if any error is experienced talking to Conduit
*/
public JSONObject sendHarbormasterUri(String phid, String buildUri) throws ConduitAPIException, IOException {
Map params = new HashMap<String, String>();
params.put("buildTargetPHID", phid);
params.put("artifactKey", "jenkins.uri");
params.put("artifactType", "uri");
JSONObject artifactData = new JSONObject();
artifactData = artifactData.element("uri", buildUri)
.element("name", "Jenkins")
.element("ui.external", true);
params.put("artifactData", artifactData);
.element("name", "Jenkins")
.element("ui.external", true);

JSONObject params = new JSONObject();
params.element("buildTargetPHID", phid)
.element("artifactKey", "jenkins.uri")
.element("artifactType", "uri")
.element("artifactData", artifactData);

return this.callConduit("harbormaster.createartifact", params);
}

Expand All @@ -139,10 +137,6 @@ public JSONObject postComment(String revisionID, String message) throws ConduitA
return postComment(revisionID, message, true, "none");
}

protected JSONObject callConduit(String methodName, Map<String, String> params) throws ConduitAPIException, IOException {
return conduit.perform(methodName, params);
}

protected JSONObject callConduit(String methodName, JSONObject params) throws ConduitAPIException, IOException {
return conduit.perform(methodName, params);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,13 @@
import org.junit.Test;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;

import static org.junit.Assert.assertEquals;

public class ConduitAPIClientTest {
private LocalTestServer server;
private ConduitAPIClient client;
private final Map<String, String> emptyParams = new HashMap<String, String>();
private final JSONObject emptyParams = new JSONObject();

@Before
public void setUp() throws Exception {
Expand Down Expand Up @@ -76,7 +74,7 @@ public void testBadRequestErrorCode() throws Exception {
@Test
public void testWithParams() throws UnsupportedEncodingException, ConduitAPIException {
client = new ConduitAPIClient("http://foo.bar", TestUtils.TEST_CONDUIT_TOKEN);
Map<String, String> params = new HashMap<String, String>();
JSONObject params = new JSONObject().element("hello", "world");
params.put("hello", "world");
client.createRequest("action", params);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@

import static junit.framework.TestCase.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.mockito.Matchers.anyMap;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.doReturn;

Expand Down Expand Up @@ -117,7 +117,7 @@ public void testSendHarbormasterSuccess() throws IOException, ConduitAPIExceptio
private void mockConduitResponse(DifferentialClient client, JSONObject response) throws IOException, ConduitAPIException {
doReturn(response).when(client).callConduit(
anyString(),
anyMap()
any(JSONObject.class)
);
}
}