Search in sources :

Example 11 with QueryParams

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

the class InstanceServiceTest method shouldReturnInstancesFromTrash.

@Test
public void shouldReturnInstancesFromTrash() {
    mockDataService();
    mockSampleFields();
    mockEntity();
    QueryParams queryParams = new QueryParams(1, 10);
    when(trashService.getInstancesFromTrash(anyString(), eq(queryParams))).thenReturn(sampleCollection());
    List<BasicEntityRecord> records = instanceService.getTrashRecords(ENTITY_ID, queryParams);
    verify(trashService).getInstancesFromTrash(anyString(), eq(queryParams));
    assertNotNull(records);
    assertEquals(records.size(), 1);
    // Make sure all fields that were in the instance are still available
    assertEquals(records.get(0).getFields().size(), 5);
    // should not perform update when username is null or blank
    verify(userPreferencesService, never()).updateGridSize(anyLong(), anyString(), anyInt());
}
Also used : QueryParams(org.motechproject.mds.query.QueryParams) BasicEntityRecord(org.motechproject.mds.web.domain.BasicEntityRecord) Test(org.junit.Test)

Example 12 with QueryParams

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

the class InstanceServiceTest method shouldReturnRelatedInstances.

@Test
public void shouldReturnRelatedInstances() {
    mockDataService();
    mockAnotherEntity();
    mockEntity();
    mockSampleFields();
    mockAnotherEntityFields();
    mockTestClassEntity();
    mockTestClassService();
    mockTestClassFields();
    when(serviceForAnotherSample.findById(INSTANCE_ID)).thenReturn(sampleForRelationshipTesting());
    QueryParams queryParams = new QueryParams(1, 2, new Order(Constants.Util.ID_FIELD_NAME, Order.Direction.ASC));
    Records<BasicEntityRecord> records = instanceService.getRelatedFieldValue(ANOTHER_ENTITY_ID, INSTANCE_ID, "testClasses", new RelationshipsUpdate(), queryParams);
    assertNotNull(records);
    // page 1
    assertEquals(Integer.valueOf(1), records.getPage());
    // 2 pages total
    assertEquals(Integer.valueOf(2), records.getTotal());
    // 3 records total
    assertEquals(Integer.valueOf(3), records.getRecords());
    assertEquals(asList(1L, 2L), extract(records.getRows(), on(BasicEntityRecord.class).getFieldByName("id").getValue()));
    RelationshipsUpdate filter = new RelationshipsUpdate();
    filter.setRemovedIds(Arrays.asList(1L, 2L));
    filter.setAddedIds(Arrays.asList(50L));
    when(testClassMotechDataService.findByIds(filter.getAddedIds())).thenReturn(Arrays.asList(new TestClass(50)));
    records = instanceService.getRelatedFieldValue(ANOTHER_ENTITY_ID, INSTANCE_ID, "testClasses", filter, queryParams);
    assertNotNull(records);
    // page 1
    assertEquals(Integer.valueOf(1), records.getPage());
    // 1 page total
    assertEquals(Integer.valueOf(1), records.getTotal());
    // 2 records total
    assertEquals(Integer.valueOf(2), records.getRecords());
    // 1L and 2L removed, 50L added
    assertEquals(asList(3L, 50L), extract(records.getRows(), on(BasicEntityRecord.class).getFieldByName("id").getValue()));
}
Also used : Order(org.motechproject.mds.util.Order) RelationshipsUpdate(org.motechproject.mds.web.domain.RelationshipsUpdate) QueryParams(org.motechproject.mds.query.QueryParams) BasicEntityRecord(org.motechproject.mds.web.domain.BasicEntityRecord) Test(org.junit.Test)

Example 13 with QueryParams

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

the class InstanceServiceTest method shouldUpdateGridSize.

@Test
public void shouldUpdateGridSize() {
    setUpSecurityContext();
    EntityDto entityDto = new EntityDto();
    entityDto.setReadOnlySecurityMode(null);
    entityDto.setSecurityMode(null);
    entityDto.setClassName(TestSample.class.getName());
    EntityRecord entityRecord = new EntityRecord(ENTITY_ID + 1, null, new ArrayList<>());
    when(entityService.getEntity(ENTITY_ID + 1)).thenReturn(entityDto);
    mockDataService();
    instanceService.getEntityRecords(entityRecord.getId(), new QueryParams(1, 100));
    verify(entityService).getEntityFieldsForUI(ENTITY_ID + 1);
    verify(userPreferencesService).updateGridSize(ENTITY_ID + 1, "motech", 100);
}
Also used : BasicEntityRecord(org.motechproject.mds.web.domain.BasicEntityRecord) EntityRecord(org.motechproject.mds.web.domain.EntityRecord) EntityDto(org.motechproject.mds.dto.EntityDto) QueryParams(org.motechproject.mds.query.QueryParams) Test(org.junit.Test)

Example 14 with QueryParams

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

the class QueryParamsBuilderTest method shouldBuildQueryParamsFromGridSettings.

@Test
public void shouldBuildQueryParamsFromGridSettings() {
    when(gridSettings.getPage()).thenReturn(3);
    when(gridSettings.getRows()).thenReturn(50);
    when(gridSettings.getSortColumn()).thenReturn("field");
    when(gridSettings.getSortDirection()).thenReturn("desc");
    QueryParams queryParams = QueryParamsBuilder.buildQueryParams(gridSettings);
    assertNotNull(queryParams);
    assertPagination(queryParams, 3, 50);
    assertEquals(2, queryParams.getOrderList().size());
    assertOrderPresent(queryParams, 0, "field", Order.Direction.DESC);
    assertDefaultIdOrder(queryParams, 1);
}
Also used : QueryParams(org.motechproject.mds.query.QueryParams) Test(org.junit.Test)

Example 15 with QueryParams

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

the class QueryParamsBuilderTest method shouldBuildQueryParamsForLookups.

@Test
public void shouldBuildQueryParamsForLookups() {
    when(gridSettings.getPage()).thenReturn(7);
    when(gridSettings.getRows()).thenReturn(80);
    // the values are not important
    Map<String, Object> lookupMap = new LinkedHashMap<>();
    lookupMap.put("field1", null);
    lookupMap.put("field2", null);
    lookupMap.put("field3", null);
    QueryParams queryParams = QueryParamsBuilder.buildQueryParams(gridSettings, lookupMap);
    assertNotNull(queryParams);
    assertPagination(queryParams, 7, 80);
    assertEquals(4, queryParams.getOrderList().size());
    assertOrderPresent(queryParams, 0, "field1", Order.Direction.ASC);
    assertOrderPresent(queryParams, 1, "field2", Order.Direction.ASC);
    assertOrderPresent(queryParams, 2, "field3", Order.Direction.ASC);
    assertDefaultIdOrder(queryParams, 3);
}
Also used : QueryParams(org.motechproject.mds.query.QueryParams) LinkedHashMap(java.util.LinkedHashMap) 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