Search in sources :

Example 1 with RestResponse

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

the class MdsRestBundleIT method testBasicCrud.

@Test
public void testBasicCrud() throws Exception {
    final MotechDataService dataService = getDataService();
    // CREATE
    // create 11 records using REST
    getLogger().info("Creating instance via REST");
    for (int i = 0; i < 11; i++) {
        HttpPost post = new HttpPost(ENTITY_URL);
        post.setEntity(new StringEntity(recordJsonString("string" + i, i), ContentType.APPLICATION_JSON));
        HttpResponse response = getHttpClient().execute(post);
        assertNotNull(response);
        assertEquals(HttpStatus.SC_OK, response.getStatusLine().getStatusCode());
    }
    // UPDATE
    // update records 4 & 5
    // we add "updated" at the end of the string
    // GET object Ids
    // TODO: should we return ID in REST responses?
    // TODO: invoke this lookup through REST
    QueryParams queryParams = new QueryParams(new Order("intField", Order.Direction.ASC));
    List objList = (List) MethodUtils.invokeMethod(dataService, "byIntSet", new Object[] { new HashSet<>(asList(4, 5)), queryParams });
    // verify that the lookup works correctly just in case
    assertNotNull(objList);
    assertEquals(2, objList.size());
    for (Object record : objList) {
        HttpPut put = new HttpPut(ENTITY_URL);
        put.setEntity(new StringEntity(recordJsonString(PropertyUtil.getProperty(record, "strField") + "Updated", (int) PropertyUtil.getProperty(record, "intField"), (long) PropertyUtil.getProperty(record, "id"))));
        HttpResponse response = getHttpClient().execute(put);
        assertNotNull(response);
        assertEquals(HttpStatus.SC_OK, response.getStatusLine().getStatusCode());
    }
    // READ
    // read records from 5 to 1
    getLogger().info("Reading instances via REST");
    String body = getHttpClient().get(ENTITY_URL + "?page=2&pageSize=5&sort=intField&order=desc", new BasicResponseHandler());
    assertNotNull(body);
    RestResponse response = OBJECT_MAPPER.readValue(body, RestResponse.class);
    assertEquals(5, response.getData().size());
    for (int i = 0; i < 5; i++) {
        int expectedIntField = 5 - i;
        // we updated the int fields X 10 for these two records
        boolean fieldWasUpdated = expectedIntField == 5 || expectedIntField == 4;
        // and added "Updated" to the string field
        String expectedStrField = "string" + expectedIntField;
        if (fieldWasUpdated) {
            expectedStrField += "Updated";
        }
        Object record = response.getData().get(i);
        assertEquals(expectedStrField, PropertyUtils.getProperty(record, "strField"));
        assertEquals(expectedIntField, PropertyUtils.getProperty(record, "intField"));
    }
    for (Object rec1 : response.getData()) {
        String responseBody = getHttpClient().get(ENTITY_URL + "?id=" + PropertyUtils.getProperty(rec1, "id"), new BasicResponseHandler());
        assertNotNull(responseBody);
        RestResponse response2 = OBJECT_MAPPER.readValue(responseBody, RestResponse.class);
        assertEquals(response2.getData().get(0).get("strField"), PropertyUtils.getProperty(rec1, "strField"));
    }
    // DELETE
    // delete 5 records using REST
    getLogger().info("Delete instances via REST");
    List list2 = dataService.retrieveAll();
    for (int i = 0; i < 5; i++) {
        Object record2 = list2.get(i);
        long id = (long) PropertyUtils.getProperty(record2, "id");
        HttpDelete delete = new HttpDelete(ENTITY_URL + "/" + id);
        HttpResponse response2 = getHttpClient().execute(delete);
        assertNotNull(response2);
        assertEquals(HttpStatus.SC_OK, response2.getStatusLine().getStatusCode());
        assertNull(dataService.findById(id));
    }
    assertEquals(dataService.retrieveAll().size(), 6);
}
Also used : Order(org.motechproject.mds.util.Order) HttpPost(org.apache.http.client.methods.HttpPost) HttpDelete(org.apache.http.client.methods.HttpDelete) RestResponse(org.motechproject.mds.rest.RestResponse) BasicResponseHandler(org.apache.http.impl.client.BasicResponseHandler) HttpResponse(org.apache.http.HttpResponse) MotechDataService(org.motechproject.mds.service.MotechDataService) HttpPut(org.apache.http.client.methods.HttpPut) StringEntity(org.apache.http.entity.StringEntity) QueryParams(org.motechproject.mds.query.QueryParams) Arrays.asList(java.util.Arrays.asList) List(java.util.List) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 2 with RestResponse

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

the class MdsRestBundleIT method testRestExposedFields.

@Test
public void testRestExposedFields() throws Exception {
    getLogger().info("Testing REST exposed fields");
    final JavaType mapType = OBJECT_MAPPER.getTypeFactory().constructMapType(Map.class, String.class, Object.class);
    // CREATE
    HttpPost post = new HttpPost(FILTERED_ENTITY_URL);
    post.setEntity(new StringEntity(recordJsonString("some-string", 42), ContentType.APPLICATION_JSON));
    String body = getHttpClient().execute(post, new BasicResponseHandler());
    Map record = OBJECT_MAPPER.readValue(body, mapType);
    assertEquals(FILTERED_REST_FIELDS.size(), record.size());
    assertEquals(FILTERED_REST_FIELDS, record.keySet());
    assertEquals(42, record.get("intField"));
    // UPDATE
    HttpPut put = new HttpPut(FILTERED_ENTITY_URL);
    put.setEntity(new StringEntity(recordJsonString(record.get("strField") + "Updated", (int) record.get("intField") + 13, (long) (int) record.get("id"))));
    body = getHttpClient().execute(put, new BasicResponseHandler());
    record = OBJECT_MAPPER.readValue(body, mapType);
    assertEquals(FILTERED_REST_FIELDS.size(), record.size());
    assertEquals(FILTERED_REST_FIELDS, record.keySet());
    assertEquals(42 + 13, record.get("intField"));
    // READ
    body = getHttpClient().get(FILTERED_ENTITY_URL, new BasicResponseHandler());
    RestResponse response = OBJECT_MAPPER.readValue(body, RestResponse.class);
    assertEquals(1, response.getData().size());
    record = response.getData().get(0);
    assertEquals(FILTERED_REST_FIELDS.size(), record.size());
    assertEquals(FILTERED_REST_FIELDS, record.keySet());
    assertEquals(42 + 13, record.get("intField"));
    // Make sure the string field is ignored
    List result = getDataServiceForFilteredEntity().retrieveAll();
    assertNotNull(result);
    assertFalse(result.isEmpty());
    for (Object obj : result) {
        assertNotNull(obj);
        assertNull(PropertyUtil.getProperty(obj, "strField"));
    }
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) StringEntity(org.apache.http.entity.StringEntity) JavaType(org.codehaus.jackson.type.JavaType) RestResponse(org.motechproject.mds.rest.RestResponse) BasicResponseHandler(org.apache.http.impl.client.BasicResponseHandler) Arrays.asList(java.util.Arrays.asList) List(java.util.List) ArrayList(java.util.ArrayList) Map(java.util.Map) HttpPut(org.apache.http.client.methods.HttpPut) Test(org.junit.Test)

Example 3 with RestResponse

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

the class MdsRestBundleIT method verifySingleLookup.

private void verifySingleLookup(String lookupParam, int expectedInt) throws Exception {
    HttpGet get = new HttpGet(ENTITY_URL + "?lookup=byStr&strField=" + URLEncoder.encode(lookupParam, "UTF-8"));
    String responseBody = getHttpClient().execute(get, new BasicResponseHandler());
    assertNotNull(responseBody);
    RestResponse response = OBJECT_MAPPER.readValue(responseBody, RestResponse.class);
    assertEquals(lookupParam, response.getData().get(0).get("strField").toString());
    assertEquals(expectedInt, PropertyUtils.getProperty(response.getData().get(0), "intField"));
    assertNotNull(PropertyUtils.getProperty(response.getData().get(0), "id"));
}
Also used : RestResponse(org.motechproject.mds.rest.RestResponse) HttpGet(org.apache.http.client.methods.HttpGet) BasicResponseHandler(org.apache.http.impl.client.BasicResponseHandler)

Example 4 with RestResponse

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

the class MdsRestControllerTest method testListReturnLookup.

private void testListReturnLookup(String entityName, String moduleName, String namespace, boolean lookupNameInPath) throws Exception {
    final TestRecord record1 = new TestRecord("T1", 5);
    final TestRecord record2 = new TestRecord("T2", 5);
    final List<TestRecord> records = asList(record1, record2);
    List<String> fields = new ArrayList<>();
    fields.add(NAME_FIELD);
    fields.add(VAL_FIELD);
    final RestResponse response = new RestResponse(entityName, CLASSNAME, moduleName, namespace, 2l, new QueryParams(5, 14), RestProjection.createProjectionCollection(records, fields, new ArrayList<String>()));
    when(restFacadeRetriever.getRestFacade(entityName, moduleName, namespace)).thenReturn(restFacade);
    when(restFacade.executeLookup(eq(LOOKUP_NAME), any(Map.class), any(QueryParams.class), anyBoolean())).thenReturn(response);
    String url;
    if (lookupNameInPath) {
        url = buildUrl(entityName, moduleName, namespace, LOOKUP_NAME) + "?" + LOOKUP_PAGINATION_STR;
    } else {
        url = buildUrl(entityName, moduleName, namespace) + "?lookup=" + LOOKUP_NAME + "&" + LOOKUP_PAGINATION_STR;
    }
    mockMvc.perform(get(url)).andExpect(status().isOk()).andExpect(content().string(objectMapper.writeValueAsString(response)));
    verifyLookupExecution();
}
Also used : RestResponse(org.motechproject.mds.rest.RestResponse) ArrayList(java.util.ArrayList) QueryParams(org.motechproject.mds.query.QueryParams) Map(java.util.Map)

Example 5 with RestResponse

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

Aggregations

RestResponse (org.motechproject.mds.rest.RestResponse)6 ArrayList (java.util.ArrayList)4 BasicResponseHandler (org.apache.http.impl.client.BasicResponseHandler)4 Arrays.asList (java.util.Arrays.asList)3 List (java.util.List)3 QueryParams (org.motechproject.mds.query.QueryParams)3 Map (java.util.Map)2 HttpGet (org.apache.http.client.methods.HttpGet)2 HttpPost (org.apache.http.client.methods.HttpPost)2 HttpPut (org.apache.http.client.methods.HttpPut)2 StringEntity (org.apache.http.entity.StringEntity)2 Test (org.junit.Test)2 HashSet (java.util.HashSet)1 HttpResponse (org.apache.http.HttpResponse)1 HttpDelete (org.apache.http.client.methods.HttpDelete)1 JavaType (org.codehaus.jackson.type.JavaType)1 RestProjection (org.motechproject.mds.rest.RestProjection)1 MotechDataService (org.motechproject.mds.service.MotechDataService)1 Order (org.motechproject.mds.util.Order)1