Skip to content

Commit

Permalink
Merge c55a819 into 77abc19
Browse files Browse the repository at this point in the history
  • Loading branch information
ascandella committed Jul 9, 2015
2 parents 77abc19 + c55a819 commit f4f68b2
Show file tree
Hide file tree
Showing 5 changed files with 212 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,22 @@ public JSONObject postComment(String message) throws IOException, InterruptedExc
}

protected JSONObject callConduit(String methodName, Map<String, String> params) throws IOException, InterruptedException, ArcanistUsageException {
ArcanistClient arc = new ArcanistClient(this.arcPath, "call-conduit", params, this.conduitToken, methodName);
ArcanistClient arc = getArcanistClient(methodName, params);
return arc.parseConduit(this.launcher.launch(), this.launcher.getStderr());
}

/**
* Get a new arcanist client.
* @param params parameters to pass to arcanist
* @return a new ArcanistClient
*/
protected ArcanistClient getArcanistClient(String methodName, Map<String, String> params) {
return new ArcanistClient(
this.arcPath,
"call-conduit",
params,
conduitToken,
methodName
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// Copyright (c) 2015 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

package com.uber.jenkins.phabricator;


import com.uber.jenkins.phabricator.utils.TestUtils;
import org.junit.Rule;
import org.junit.Test;
import org.jvnet.hudson.test.JenkinsRule;

import java.io.ByteArrayOutputStream;
import java.io.OutputStream;

import static org.junit.Assert.assertEquals;

public class LauncherFactoryTest {
@Rule
public JenkinsRule j = new JenkinsRule();

@Test
public void testLauncherFactoryIsSane() throws Exception {
LauncherFactory factory = TestUtils.createLauncherFactory(j);
assertEquals(factory.getStderr(), System.err);

OutputStream out = new ByteArrayOutputStream();
factory.launch().stdout(out).cmdAsSingleString("echo hello").join();
assertEquals(out.toString(), "hello\n");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
// Copyright (c) 2015 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

package com.uber.jenkins.phabricator.conduit;

import com.uber.jenkins.phabricator.utils.TestUtils;
import net.sf.json.JSONObject;
import net.sf.json.groovy.JsonSlurper;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.jvnet.hudson.test.JenkinsRule;

import java.io.IOException;
import java.io.InputStream;

import static junit.framework.TestCase.assertEquals;
import static org.mockito.Matchers.*;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.spy;

public class DifferentialClientTest {
@Rule
public JenkinsRule j = new JenkinsRule();

private final String DUMMY_DIFF_ID = "123";
private final String DUMMY_CONDUIT_TOKEN = "notarealtoken";
private final String DUMMY_ARC_PATH = "echo";

private DifferentialClient client;

@Before
public void setUp() throws Exception {
client = createClient();
}

@Test
public void testPostComment() throws Exception {
JSONObject sentinel = new JSONObject();
sentinel.put("hi", "there");

mockConduitResponse(client, sentinel);

JSONObject response = client.postComment("hello", true, "none");
assertEquals(sentinel, response);
}

@Test
public void testPostCommentSingleArgument() throws Exception {
JSONObject sentinel = new JSONObject();
sentinel.put("something", "here");

mockConduitResponse(client, sentinel);

JSONObject response = client.postComment("hello");
assertEquals(response, sentinel);
}

@Test(expected = ArcanistUsageException.class)
public void testFetchDiffWithEmptyResponse() throws Exception {
JSONObject empty = new JSONObject();
mockConduitResponse(client, empty);

client.fetchDiff();
}

@Test(expected = ArcanistUsageException.class)
public void testFetchDiffWithNoDiff() throws Exception {
JSONObject noDiff = new JSONObject();
noDiff.put("response", null);
mockConduitResponse(client, noDiff);

client.fetchDiff();
}

@Test
public void testFetchDiffWithValidResponse() throws Exception {
JSONObject realResponse = getValidFetchDiffResponse();
mockConduitResponse(client, realResponse);

JSONObject response = client.fetchDiff();
assertEquals("world", response.get("hello"));
}

private void mockConduitResponse(DifferentialClient client, JSONObject response) throws InterruptedException, ArcanistUsageException, IOException {
doReturn(response).when(client).callConduit(
anyString(),
anyMap()
);
}

private DifferentialClient createClient() throws Exception {
return spy(new DifferentialClient(
DUMMY_DIFF_ID,
TestUtils.createLauncherFactory(j),
DUMMY_CONDUIT_TOKEN,
DUMMY_ARC_PATH
));
}

private JSONObject getValidFetchDiffResponse() throws IOException {
InputStream input = getClass().getResourceAsStream(
"validFetchDiffResponse.json"
);
return (JSONObject) new JsonSlurper().parse(input);
}
}
19 changes: 19 additions & 0 deletions src/test/java/com/uber/jenkins/phabricator/utils/TestUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,19 @@
package com.uber.jenkins.phabricator.utils;

import com.uber.jenkins.phabricator.CodeCoverageMetrics;
import com.uber.jenkins.phabricator.LauncherFactory;
import com.uber.jenkins.phabricator.conduit.ArcanistClient;
import com.uber.jenkins.phabricator.uberalls.UberallsClient;
import hudson.EnvVars;
import hudson.FilePath;
import hudson.plugins.cobertura.Ratio;
import hudson.plugins.cobertura.targets.CoverageMetric;
import hudson.plugins.cobertura.targets.CoverageResult;
import org.jvnet.hudson.test.JenkinsRule;

import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.util.Map;

import static org.mockito.Mockito.*;

Expand All @@ -51,6 +57,19 @@ public static UberallsClient getDefaultUberallsClient() {
return getUberallsClient(TEST_BASE_URL, getDefaultLogger(), TEST_REPOSITORY, TEST_BRANCH);
}

public static EnvVars getDefaultEnvVars() {
return new EnvVars();
}

public static LauncherFactory createLauncherFactory(JenkinsRule j) throws Exception {
return new LauncherFactory(
j.createLocalLauncher(),
getDefaultEnvVars(),
System.err,
new FilePath(j.getWebAppRoot())
);
}

public static CodeCoverageMetrics getCodeCoverageMetrics(String sha1,
float packagesCoveragePercent,
float filesCoveragePercent,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"response": {
"123": {
"hello": "world"
}
}
}

0 comments on commit f4f68b2

Please sign in to comment.