Search in sources :

Example 36 with QueryParams

use of org.motechproject.mds.query.QueryParams in project motech by motech.

the class InstanceControllerTest method shouldExportInstancesWithAllRecordsAsCsv.

@Test
public void shouldExportInstancesWithAllRecordsAsCsv() throws Exception {
    when(response.getWriter()).thenReturn(writer);
    ArgumentCaptor<QueryParams> captor = ArgumentCaptor.forClass(QueryParams.class);
    GridSettings gridSettings = new GridSettings();
    gridSettings.setLookup("lookup");
    instanceController.exportEntityInstances(1L, gridSettings, "all", "csv", response);
    verify(instanceService).verifyEntityAccess(1L);
    verify(csvImportExportService).exportCsv(eq(1L), eq(writer), eq("lookup"), captor.capture(), any(List.class), any(Map.class));
    verify(response).setContentType("text/csv");
    verify(response).setHeader("Content-Disposition", "attachment; filename=Entity_1_instances.csv");
    assertNull(captor.getValue().getPageSize());
    assertTrue(captor.getValue().isOrderSet());
    assertEquals(1, captor.getValue().getOrderList().size());
    assertEquals(Constants.Util.ID_FIELD_NAME, captor.getValue().getOrderList().get(0).getField());
    assertEquals(Order.Direction.ASC, captor.getValue().getOrderList().get(0).getDirection());
}
Also used : QueryParams(org.motechproject.mds.query.QueryParams) ArrayList(java.util.ArrayList) Arrays.asList(java.util.Arrays.asList) List(java.util.List) GridSettings(org.motechproject.mds.web.domain.GridSettings) Map(java.util.Map) Test(org.junit.Test)

Example 37 with QueryParams

use of org.motechproject.mds.query.QueryParams in project motech by motech.

the class InstanceControllerTest method shouldRetrieveRelatedFieldValues.

@Test
public void shouldRetrieveRelatedFieldValues() throws Exception {
    Records<BasicEntityRecord> records = new Records<>(2, 5, 7, recordsList());
    when(instanceService.getRelatedFieldValue(eq(1L), eq(6L), eq("relField"), any(RelationshipsUpdate.class), any(QueryParams.class))).thenReturn(records);
    controller.perform(post("/instances/1/instance/6/relField?rows=5&page=2&sortColumn=age&sortDirection=desc")).andExpect(status().isOk()).andExpect(content().type(RestTestUtil.JSON_UTF8)).andExpect(content().string(new ObjectMapper().writeValueAsString(records)));
    ArgumentCaptor<QueryParams> captor = ArgumentCaptor.forClass(QueryParams.class);
    verify(instanceService).getRelatedFieldValue(eq(1L), eq(6L), eq("relField"), any(RelationshipsUpdate.class), captor.capture());
    QueryParams queryParams = captor.getValue();
    // check query params
    assertNotNull(queryParams);
    assertEquals(Integer.valueOf(5), queryParams.getPageSize());
    assertEquals(Integer.valueOf(2), queryParams.getPage());
    assertNotNull(queryParams.getOrderList());
    assertEquals(2, queryParams.getOrderList().size());
    assertEquals("age", queryParams.getOrderList().get(0).getField());
    assertEquals(Order.Direction.DESC, queryParams.getOrderList().get(0).getDirection());
    assertEquals(Constants.Util.ID_FIELD_NAME, queryParams.getOrderList().get(1).getField());
    assertEquals(Order.Direction.ASC, queryParams.getOrderList().get(1).getDirection());
}
Also used : RelationshipsUpdate(org.motechproject.mds.web.domain.RelationshipsUpdate) QueryParams(org.motechproject.mds.query.QueryParams) BasicEntityRecord(org.motechproject.mds.web.domain.BasicEntityRecord) Records(org.motechproject.mds.web.domain.Records) ObjectMapper(org.codehaus.jackson.map.ObjectMapper) Test(org.junit.Test)

Example 38 with QueryParams

use of org.motechproject.mds.query.QueryParams in project motech by motech.

the class MdsRestFacadeTest method shouldExecuteLookupWithBlobField.

@Test
public void shouldExecuteLookupWithBlobField() {
    Map<String, String> lookupMap = asLookupMap(null, "44");
    QueryParams queryParams = mock(QueryParams.class);
    when(dataService.supportedLookup(null, 44, queryParams)).thenReturn(asList(recordOne));
    RestResponse result = (RestResponse) mdsRestFacade.executeLookup(SUPPORTED_LOOKUP_NAME, lookupMap, queryParams, true);
    assertEquals(1, result.getData().size());
    assertEquals(3, result.getData().get(0).size());
    assertEquals(recordOne.getValue(), result.getData().get(0).get(VALUE_FIELD));
    assertEquals(recordOne.getDate(), result.getData().get(0).get(DATE_FIELD));
    assertArrayEquals((byte[]) result.getData().get(0).get(BLOB_FIELD), encodedBlobField);
    verify(dataService).supportedLookup(null, 44, queryParams);
}
Also used : QueryParams(org.motechproject.mds.query.QueryParams) Matchers.anyString(org.mockito.Matchers.anyString) Test(org.junit.Test)

Example 39 with QueryParams

use of org.motechproject.mds.query.QueryParams in project motech by motech.

the class MdsRestFacadeTest method shouldThrowExceptionForUnsupportedRead.

@Test(expected = RestOperationNotSupportedException.class)
public void shouldThrowExceptionForUnsupportedRead() {
    setUpCrudAccess(true, false, true, true);
    mdsRestFacade.get(new QueryParams(1, 10), false);
}
Also used : QueryParams(org.motechproject.mds.query.QueryParams) Test(org.junit.Test)

Example 40 with QueryParams

use of org.motechproject.mds.query.QueryParams in project motech by motech.

the class MdsRestFacadeTest method shouldThrowExceptionForEmptyResult.

@Test(expected = RestNoLookupResultException.class)
public void shouldThrowExceptionForEmptyResult() {
    Map<String, String> lookupMap = asLookupMap(null, "44");
    QueryParams queryParams = mock(QueryParams.class);
    when(dataService.supportedLookup(null, 44, queryParams)).thenReturn(null);
    mdsRestFacade.executeLookup(SUPPORTED_LOOKUP_NAME, lookupMap, queryParams, false);
}
Also used : QueryParams(org.motechproject.mds.query.QueryParams) Matchers.anyString(org.mockito.Matchers.anyString) Test(org.junit.Test)

Aggregations

QueryParams (org.motechproject.mds.query.QueryParams)48 Test (org.junit.Test)27 Order (org.motechproject.mds.util.Order)14 ArrayList (java.util.ArrayList)8 BasicEntityRecord (org.motechproject.mds.web.domain.BasicEntityRecord)8 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)7 DateTime (org.joda.time.DateTime)6 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)6 Arrays.asList (java.util.Arrays.asList)5 List (java.util.List)5 Map (java.util.Map)4 Records (org.motechproject.mds.web.domain.Records)4 TaskActivityType (org.motechproject.tasks.domain.enums.TaskActivityType)4 HashSet (java.util.HashSet)3 Matchers.anyString (org.mockito.Matchers.anyString)3 EntityDto (org.motechproject.mds.dto.EntityDto)3 RestResponse (org.motechproject.mds.rest.RestResponse)3 TaskActivity (org.motechproject.tasks.domain.mds.task.TaskActivity)3 TaskActivityDto (org.motechproject.tasks.dto.TaskActivityDto)3 JsonObject (com.google.gson.JsonObject)2