Skip to content

Commit

Permalink
Troll for extra coverage
Browse files Browse the repository at this point in the history
This code is not really that valuable to test, but we're going for
vanity coverage, so yolo.
  • Loading branch information
ascandella committed Sep 9, 2015
1 parent f42eb06 commit d0632b9
Show file tree
Hide file tree
Showing 3 changed files with 153 additions and 5 deletions.
Expand Up @@ -108,7 +108,7 @@ public boolean recordCoverage(String sha, CodeCoverageMetrics codeCoverageMetric
params.put(CONDITIONAL_COVERAGE_KEY, codeCoverageMetrics.getConditionalCoveragePercent());

try {
HttpClient client = new HttpClient();
HttpClient client = getClient();
PostMethod request = new PostMethod(getBuilder().build().toString());
request.addRequestHeader("Content-Type", "application/json");
StringRequestEntity requestEntity = new StringRequestEntity(
Expand All @@ -123,7 +123,6 @@ public boolean recordCoverage(String sha, CodeCoverageMetrics codeCoverageMetric
return false;
}
return true;

} catch (URISyntaxException e) {
e.printStackTrace();
} catch (HttpResponseException e) {
Expand All @@ -146,7 +145,7 @@ public String getCoverage(String sha) {
.setParameter("sha", sha)
.setParameter("repository", repository);

HttpClient client = new HttpClient();
HttpClient client = getClient();
HttpMethod request = new GetMethod(builder.build().toString());
int statusCode = client.executeMethod(request);

Expand All @@ -169,6 +168,10 @@ private URIBuilder getBuilder() throws URISyntaxException {
return new URIBuilder(baseURL);
}

public HttpClient getClient() {
return new HttpClient();
}

public boolean isConfigured() {
return !CommonUtils.isBlank(baseURL);
}
Expand Down
@@ -0,0 +1,84 @@
// Copyright (c) 2015 Uber
//
// 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 org.junit.Test;

import static org.junit.Assert.*;

public class PhabricatorPostbuildActionTest {
private final PhabricatorPostbuildAction action = PhabricatorPostbuildAction.createShortText("text", "link");

@Test
public void testGetUrlName() throws Exception {
assertEquals("", action.getUrlName());
}

@Test
public void testGetDisplayName() throws Exception {
assertEquals("", action.getDisplayName());
}

@Test
public void testGetIconFileName() throws Exception {
assertNull(action.getIconFileName());
}

@Test
public void testIsTextOnly() throws Exception {
assertTrue(action.isTextOnly());
}

@Test
public void testGetIconPath() throws Exception {
assertNull(action.getIconPath());
}

@Test
public void testGetText() throws Exception {
assertEquals("text", action.getText());
}

@Test
public void testGetColor() throws Exception {
assertNotNull(action.getColor());
}

@Test
public void testGetBackground() throws Exception {
assertNotNull(action.getBackground());
}

@Test
public void testGetBorder() throws Exception {
assertEquals("0", action.getBorder());
}

@Test
public void testGetBorderColor() throws Exception {
assertNotNull(action.getBorderColor());
}

@Test
public void testGetLink() throws Exception {
assertEquals("link", action.getLink());
}
}
Expand Up @@ -3,15 +3,21 @@
import com.uber.jenkins.phabricator.coverage.CodeCoverageMetrics;
import com.uber.jenkins.phabricator.utils.TestUtils;
import net.sf.json.JSONObject;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpResponseException;
import org.apache.http.localserver.LocalTestServer;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.net.URISyntaxException;

import static org.junit.Assert.*;
import static org.mockito.Mockito.*;

public class UberallsClientTest {
private LocalTestServer server;
Expand Down Expand Up @@ -94,13 +100,68 @@ public void testRecordCoverageSuccessful() {
assertTrue(client.recordCoverage(TestUtils.TEST_SHA, TestUtils.getDefaultCodeCoverageMetrics()));
}

@Test
public void testRecordCoverageURISyntaxException() throws Exception {
assertRecordCoverageException(URISyntaxException.class);
}

@Test
public void testRecordCoverageHttpResponseException() throws Exception {
assertRecordCoverageException(HttpResponseException.class);
}

@Test
public void testRecordCoverageClientProtocolException() throws Exception {
assertRecordCoverageException(ClientProtocolException.class);
}

@Test
public void testRecordCoverageIOException() throws Exception {
assertRecordCoverageException(IOException.class);
}

@Test
public void testGetCoverageHttpResponseException() throws Exception {
assertGetCoverageException(HttpResponseException.class);
}

@Test
public void testGetCoverageRandomException() throws Exception {
assertGetCoverageException(IOException.class);
}

private void assertRecordCoverageException(Class<? extends Exception> exceptionClass) throws Exception {
HttpClient mockClient = mockClient();

doThrow(exceptionClass).when(mockClient).executeMethod(any(HttpMethod.class));
assertFalse(client.recordCoverage(TestUtils.TEST_SHA, TestUtils.getDefaultCodeCoverageMetrics()));
}

private void assertGetCoverageException(Class<? extends Exception> exceptionClass) throws IOException {
HttpClient mockClient = mockClient();

doThrow(exceptionClass).when(mockClient).executeMethod(any(HttpMethod.class));
assertNull(client.getCoverage(TestUtils.TEST_SHA));
}

private UberallsClient getDefaultClient() {
return new UberallsClient(
return spy(new UberallsClient(
getTestServerAddress(),
TestUtils.getDefaultLogger(),
TestUtils.TEST_REPOSITORY,
TestUtils.TEST_BRANCH
);
));
}

private HttpClient getMockHttpClient() {
return mock(HttpClient.class);
}

private HttpClient mockClient() {
HttpClient mockClient = getMockHttpClient();
doReturn(mockClient).when(client).getClient();

return mockClient;
}

private String getTestServerAddress() {
Expand Down

0 comments on commit d0632b9

Please sign in to comment.