Skip to content

Commit

Permalink
increase test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
abuttaro committed Feb 13, 2018
1 parent 0f58161 commit 6c75375
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public static class Builder {
private HttpMethod method;
private Object[] args;
private int okStatus = 200;
private boolean testNameAsMethodName;
private String testName;
private MediaType mediaType;
private boolean https;

Expand All @@ -43,7 +43,6 @@ public static class Builder {
this.unitTest = unitTest;
this.path = path;
this.defaultArgs = args;
this.testNameAsMethodName = true;
mediaType = MediaType.APPLICATION_JSON;
}

Expand Down Expand Up @@ -98,8 +97,8 @@ private Builder itemResource(boolean item) {
/**
* If true, the test name will be detected and applied automatically based upon the calling method's name
*/
public Builder testNameAsMethodName(boolean testNameAsMethodName) {
this.testNameAsMethodName = testNameAsMethodName;
public Builder test(String testName) {
this.testName = testName;
return me();
}

Expand Down Expand Up @@ -207,7 +206,7 @@ private RpcApiAssertions.Builder build() {
return new RpcApiAssertions.Builder(executor, unitTest, testPath, defaultArgs)
.pathArgs(args)
.method(method)
.testNameAsMethodName(testNameAsMethodName)
.test(testName)
.mediaType(mediaType)
.https(https)
.okStatus(okStatus);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,11 @@ private Contents body(String contents) {
}

private RestApiAssertions.Builder api() {
return RestApiAssertions.builder(executor, "/foo", 1);
return api("/foo");
}

private RestApiAssertions.Builder api(String endPoint) {
return RestApiAssertions.builder(executor, endPoint, 1);
}

private Contents body() {
Expand Down Expand Up @@ -70,13 +74,36 @@ public void testPatchOK() {
assertItemRequest(requestCaptor, HttpMethod.PATCH, body);
}

@Test
public void testUpdateOK() {
ArgumentCaptor<ApiRequest> requestCaptor = request();

Contents body = body();

api("foo/{id}").update(body).expectBody(expectedContents).ok();

assertItemRequest(requestCaptor, HttpMethod.PUT, body);
}

@Test
public void testTestFileNotFound() {
request();
assertThrows(".json not found", () -> api().update().ok());
}

@Test
public void testTestFileNotFoundTestName() {
request();
assertThrows("foo.json not found", () -> api().test("foo").update().ok());
}

@Test
public void testContentTypeHalError() {
request();

Contents body = body();

assertThrows("Expected: Content-Type=", () -> api().hal()
assertThrows("Expected: Content-Type=", () -> api("foo/{id}").hal()
.patch(body.getContentAsByteArray()).ok());
}

Expand All @@ -86,6 +113,14 @@ public void testBodyError() {
assertThrows("Expected: foo", () -> api()
.patch(body().getContentAsString())
.expectBody("foo").ok());

assertThrows("Expected: foo", () -> api()
.update(body().getContentAsString())
.expectBody("foo").ok());

assertThrows("Expected: foo", () -> api()
.update(body().getContentAsByteArray())
.expectBody("foo").ok());
}

@Test
Expand Down Expand Up @@ -149,7 +184,7 @@ public void testDeleteOK() {
public void testListOK() {
ArgumentCaptor<ApiRequest> requestCaptor = request();

RestApiAssertions.builder(executor, getClass(), "/foo", 1)
RestApiAssertions.builder(executor, getClass(), "/foo/{id}", 1)
.mediaType(MediaType.APPLICATION_JSON)
.list()
.expectBody(expectedContents).ok();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,73 @@
package com.github.restup.test;

import static com.github.restup.test.assertions.Assertions.assertPrivateConstructor;
import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.when;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import com.github.restup.test.resource.Contents;

@RunWith(MockitoJUnitRunner.class)
public class RpcApiAssertionsTest {

@Mock
ApiExecutor executor;
@Mock
ApiResponse<String[]> response;
@Mock
Contents expectedContents;
@Mock
Contents resultContents;

private ArgumentCaptor<ApiRequest> request() {
return request(200, "application/json");
}

private ArgumentCaptor<ApiRequest> request(int status, String contentType) {
ArgumentCaptor<ApiRequest> request = ArgumentCaptor.forClass(ApiRequest.class);

when(executor.execute(request.capture())).thenReturn(response);
when(response.getStatus()).thenReturn(status);
when(response.getHeader("Content-Type")).thenReturn(new String[] {contentType});
when(response.getBody()).thenReturn(resultContents);
return request;
}

@Test
public void testPrivateConstructor() {
assertPrivateConstructor(RpcApiAssertions.class);
}

@Test
public void testGet() {
ArgumentCaptor<ApiRequest> requestCaptor = request();

RpcApiAssertions.builder(executor, getClass(), "/foo/some/{non}/{restful}/{restful}", 1, 2, 3)
.expectBody(expectedContents)
.ok();

assertItemRequest(requestCaptor, HttpMethod.GET);
}

private void assertItemRequest(ArgumentCaptor<ApiRequest> requestCaptor, HttpMethod method) {
assertItemRequest(requestCaptor, method, null, false, "application/json");
}

private void assertItemRequest(ArgumentCaptor<ApiRequest> requestCaptor, HttpMethod method, Contents contents, boolean https, String contentType) {
assertRequest(requestCaptor, method, "/foo/some/1/2/3", contents, https, contentType);
}


private void assertRequest(ArgumentCaptor<ApiRequest> requestCaptor, HttpMethod method, String url, Contents contents, boolean https, String contentType) {
ApiRequest request = requestCaptor.getValue();

assertEquals(https, request.isHttps());
assertEquals(contents, request.getBody());
assertEquals(method, request.getMethod());
assertEquals(url, request.getUrl());
}

}

0 comments on commit 6c75375

Please sign in to comment.