-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
102 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
up-test/src/test/java/com/github/restup/test/RpcApiAssertionsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
|
||
} |