Search in sources :

Example 1 with DefaultRequestBuilder

use of org.springframework.test.web.server.request.DefaultRequestBuilder in project motech by motech.

the class MdsRestControllerTest method testCreateUpdate.

private void testCreateUpdate(String entityName, String moduleName, String namespace, boolean update) throws Exception {
    final RestProjection record = new RestProjection();
    record.put(NAME_FIELD, "A name");
    record.put(VAL_FIELD, -98);
    final String recordJson = objectMapper.writeValueAsString(record);
    when(restFacadeRetriever.getRestFacade(entityName, moduleName, namespace)).thenReturn(restFacade);
    when(restFacade.create(any(InputStream.class))).thenReturn(record);
    when(restFacade.update(any(InputStream.class))).thenReturn(record);
    String url = buildUrl(entityName, moduleName, namespace);
    DefaultRequestBuilder requestBuilder = (update) ? put(url) : post(url);
    mockMvc.perform(requestBuilder.body(recordJson.getBytes())).andExpect(status().isOk()).andExpect(content().string(recordJson));
    ArgumentCaptor<InputStream> captor = ArgumentCaptor.forClass(InputStream.class);
    if (update) {
        verify(restFacade).update(captor.capture());
    } else {
        verify(restFacade).create(captor.capture());
    }
    try (InputStream in = captor.getValue()) {
        TestRecord testRecord = objectMapper.readValue(in, TestRecord.class);
        assertEquals(record.get(NAME_FIELD), testRecord.getName());
        assertEquals(record.get(VAL_FIELD), testRecord.getVal());
    }
}
Also used : RestProjection(org.motechproject.mds.rest.RestProjection) InputStream(java.io.InputStream) DefaultRequestBuilder(org.springframework.test.web.server.request.DefaultRequestBuilder)

Aggregations

InputStream (java.io.InputStream)1 RestProjection (org.motechproject.mds.rest.RestProjection)1 DefaultRequestBuilder (org.springframework.test.web.server.request.DefaultRequestBuilder)1