Search in sources :

Example 1 with MockResponseBuilder

use of com.linkedin.restli.client.testutils.MockResponseBuilder in project rest.li by linkedin.

the class TestMockResponseBuilder method testBuild.

@Test
public void testBuild() {
    MockResponseBuilder<Long, Greeting> mockResponseBuilder = new MockResponseBuilder<Long, Greeting>();
    Greeting greeting = new Greeting().setId(1L).setMessage("message");
    Map<String, String> headers = Collections.singletonMap("foo", "bar");
    RestLiResponseException restLiResponseException = EasyMock.createMock(RestLiResponseException.class);
    EasyMock.expect(restLiResponseException.getErrorSource()).andReturn("foo").once();
    EasyMock.replay(restLiResponseException);
    // build a response object using all the setters. This response does not make sense logically but the goal here
    // is to test that the builder is working correctly.
    mockResponseBuilder.setEntity(greeting).setHeaders(headers).setCookies(Collections.singletonList(new HttpCookie("cookie", "value"))).setStatus(200).setRestLiResponseException(restLiResponseException);
    Response<Greeting> response = mockResponseBuilder.build();
    // when we build the Response the ID is put into the headers
    Map<String, String> builtHeaders = new HashMap<String, String>(headers);
    builtHeaders.put(RestConstants.HEADER_RESTLI_PROTOCOL_VERSION, AllProtocolVersions.BASELINE_PROTOCOL_VERSION.toString());
    Assert.assertEquals(response.getEntity(), greeting);
    Assert.assertEquals(response.getHeaders(), builtHeaders);
    Assert.assertEquals(response.getCookies(), Collections.singletonList(new HttpCookie("cookie", "value")));
    Assert.assertEquals(response.getStatus(), 200);
    Assert.assertEquals(response.getError().getErrorSource(), "foo");
}
Also used : Greeting(com.linkedin.restli.examples.greetings.api.Greeting) HashMap(java.util.HashMap) RestLiResponseException(com.linkedin.restli.client.RestLiResponseException) HttpCookie(java.net.HttpCookie) MockResponseBuilder(com.linkedin.restli.client.testutils.MockResponseBuilder) Test(org.testng.annotations.Test)

Aggregations

RestLiResponseException (com.linkedin.restli.client.RestLiResponseException)1 MockResponseBuilder (com.linkedin.restli.client.testutils.MockResponseBuilder)1 Greeting (com.linkedin.restli.examples.greetings.api.Greeting)1 HttpCookie (java.net.HttpCookie)1 HashMap (java.util.HashMap)1 Test (org.testng.annotations.Test)1