Search in sources :

Example 1 with RestProjection

use of org.motechproject.mds.rest.RestProjection in project motech by motech.

the class MdsRestControllerTest method testRead.

private void testRead(String entityName, String moduleName, String namespace) throws Exception {
    final RestProjection record1 = new RestProjection();
    record1.put(NAME_FIELD, "T1");
    record1.put(VAL_FIELD, 5);
    final RestProjection record2 = new RestProjection();
    record1.put(NAME_FIELD, "T2");
    record1.put(VAL_FIELD, 5);
    final List<RestProjection> records = asList(record1, record2);
    final RestResponse response = new RestResponse(entityName, CLASSNAME, moduleName, namespace, 2l, new QueryParams(1, 20), records);
    final RestResponse response2 = new RestResponse(entityName, CLASSNAME, moduleName, namespace, 1l, new QueryParams(1, 1), asList(record1));
    when(restFacadeRetriever.getRestFacade(entityName, moduleName, namespace)).thenReturn(restFacade);
    when(restFacade.get(any(QueryParams.class), anyBoolean())).thenReturn(response);
    when(restFacade.get(1l, true)).thenReturn(response2);
    mockMvc.perform(get(buildUrl(entityName, moduleName, namespace) + "?" + PAGINATION_STR)).andExpect(status().isOk()).andExpect(content().string(objectMapper.writeValueAsString(response)));
    mockMvc.perform(get(buildUrl(entityName, moduleName, namespace) + "?id=1")).andExpect(status().isOk()).andExpect(content().string(objectMapper.writeValueAsString(response2)));
    ArgumentCaptor<QueryParams> captor = ArgumentCaptor.forClass(QueryParams.class);
    verify(restFacade).get(captor.capture(), anyBoolean());
    ArgumentCaptor<Long> longCaptor = ArgumentCaptor.forClass(Long.class);
    verify(restFacade).get(longCaptor.capture(), anyBoolean());
    verifyQueryParams(captor.getValue());
}
Also used : RestProjection(org.motechproject.mds.rest.RestProjection) RestResponse(org.motechproject.mds.rest.RestResponse) QueryParams(org.motechproject.mds.query.QueryParams)

Example 2 with RestProjection

use of org.motechproject.mds.rest.RestProjection 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

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