Search in sources :

Example 11 with Order

use of org.motechproject.mds.util.Order in project motech by motech.

the class MdsBundleIT method verifyCsvImport.

private void verifyCsvImport() throws Exception {
    getLogger().info("Verifying CSV Import");
    CsvImportExportService csvImportExportService = ServiceRetriever.getService(bundleContext, CsvImportExportService.class);
    try (InputStream in = new ClassPathResource("csv/import.csv").getInputStream()) {
        Reader reader = new InputStreamReader(in);
        CsvImportResults results = csvImportExportService.importCsv(FOO_CLASS, reader, "import.csv", false);
        assertNotNull(results);
        assertEquals(2, results.totalNumberOfImportedInstances());
        assertEquals(2, results.newInstanceCount());
        assertEquals(0, results.updatedInstanceCount());
    }
    assertEquals(7, service.count());
    // get the imported instances through a lookup
    QueryParams queryParams = new QueryParams(new Order("someTime", Order.Direction.DESC));
    List list = (List) MethodUtils.invokeExactMethod(service, "matchesOperator", new Object[] { "fromCsv", queryParams });
    assertNotNull(list);
    assertEquals(2, list.size());
    assertInstance(list.get(0), false, "fromCsv2", "Capital CSV", Collections.emptyList(), null, new LocalDate(2012, 10, 14), null, new Period(2, 0, 0, 0, 0, 0, 0, 0), null, new DateTime(2014, 12, 2, 16, 13, 40, 0, DateTimeZone.UTC).toDate(), null, new Time(20, 20), null, null, null, null);
    assertInstance(list.get(1), true, "fromCsv1", "Capital CSV", new ArrayList(asList("one", "two")), new DateTime(2014, 12, 2, 13, 10, 40, 0, DateTimeZone.UTC).withZone(DateTimeZone.getDefault()), new LocalDate(2012, 10, 15), null, new Period(1, 0, 0, 0, 0, 0, 0, 0), null, new DateTime(2014, 12, 2, 13, 13, 40, 0, DateTimeZone.UTC).toDate(), null, new Time(10, 30), null, null, null, null);
}
Also used : Order(org.motechproject.mds.util.Order) InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) StringReader(java.io.StringReader) Period(org.joda.time.Period) Time(org.motechproject.commons.date.model.Time) LocalDateTime(java.time.LocalDateTime) DateTime(org.joda.time.DateTime) LocalDate(org.joda.time.LocalDate) ClassPathResource(org.springframework.core.io.ClassPathResource) LocalDateTime(java.time.LocalDateTime) DateTime(org.joda.time.DateTime) CsvImportResults(org.motechproject.mds.dto.CsvImportResults) CsvImportExportService(org.motechproject.mds.service.CsvImportExportService) QueryParams(org.motechproject.mds.query.QueryParams) Collections.singletonList(java.util.Collections.singletonList) Arrays.asList(java.util.Arrays.asList) ArrayList(java.util.ArrayList) List(java.util.List) LinkedList(java.util.LinkedList)

Example 12 with Order

use of org.motechproject.mds.util.Order in project motech by motech.

the class MdsBundleIT method verifyInstanceCreatingAndRetrieving.

private void verifyInstanceCreatingAndRetrieving(Class<?> loadedClass) throws Exception {
    getLogger().info("Verifying instance creation and retrieval");
    Object instance = loadedClass.newInstance();
    Object instance2 = loadedClass.newInstance();
    Object instance3 = loadedClass.newInstance();
    Object instance4 = loadedClass.newInstance();
    Object instance5 = loadedClass.newInstance();
    // instance 1
    updateInstance(instance, true, "trueNow", "trueNowCp", new ArrayList(asList("1", "2", "3")), NOW, LD_NOW, TEST_MAP, TEST_PERIOD, BYTE_ARRAY_VALUE, DATE_NOW, DOUBLE_VALUE_1, MORNING_TIME, 1, toEnum(loadedClass, "one"), JAVA_LD_NOW, JAVA_NOW);
    // instance 2
    updateInstance(instance2, true, "trueInRange", "trueInRangeCp", new ArrayList(asList("2", "4")), NOW.plusHours(1), LD_NOW.plusDays(1), TEST_MAP, TEST_PERIOD, BYTE_ARRAY_VALUE, DATE_NOW, DOUBLE_VALUE_1, MORNING_TIME, 2, toEnum(loadedClass, "two"), JAVA_LD_NOW.plusDays(1), JAVA_NOW.plusHours(1));
    // instance 3
    updateInstance(instance3, false, "falseInRange", "falseInRangeCp", null, NOW.plusHours(2), LD_NOW.plusDays(1), null, TEST_PERIOD, BYTE_ARRAY_VALUE, DATE_TOMORROW, DOUBLE_VALUE_2, NIGHT_TIME, 2, toEnum(loadedClass, "three"), JAVA_LD_NOW.plusDays(2), JAVA_NOW.plusHours(2));
    // instance 4
    updateInstance(instance4, true, "trueOutOfRange", "trueOutOfRangeCp", null, NOW.plusHours(3), LD_NOW.plusDays(10), null, TEST_PERIOD, BYTE_ARRAY_VALUE, DATE_TOMORROW, DOUBLE_VALUE_2, NIGHT_TIME, 3, toEnum(loadedClass, "one"), JAVA_LD_NOW.plusDays(3), JAVA_NOW.plusHours(3));
    // instance 5
    updateInstance(instance5, true, "notInSet", "notInSetCp", null, NOW.plusHours(4), LD_NOW, null, TEST_PERIOD, BYTE_ARRAY_VALUE, DATE_NOW, DOUBLE_VALUE_2, MORNING_TIME, 4, toEnum(loadedClass, "two"), JAVA_LD_NOW.plusDays(4), JAVA_NOW.plusHours(4));
    // Single object return lookup should return 0 if there are no instances
    Long emptyCount = (Long) MethodUtils.invokeMethod(service, "countByUniqueString", "trueNow");
    assertEquals(emptyCount, (Long) 0L);
    service.create(instance);
    Object retrieved = service.retrieveAll(QueryParams.ascOrder("someDateTime")).get(0);
    // Single object return lookup should return 1 if there is instance with unique value in field
    Long count = (Long) MethodUtils.invokeMethod(service, "countByUniqueString", "trueNow");
    assertEquals(count, (Long) 1L);
    assertInstanceOne(retrieved, loadedClass);
    assertEquals(1, service.retrieveAll().size());
    service.create(instance2);
    service.create(instance3);
    service.create(instance4);
    service.create(instance5);
    assertEquals(INSTANCE_COUNT, service.retrieveAll().size());
    // verify double order
    QueryParams queryParams = new QueryParams(asList(new Order("someLocalDate", Order.Direction.DESC), new Order("someString", Order.Direction.ASC)));
    List<Object> result = service.retrieveAll(queryParams);
    assertNotNull(result);
    assertEquals(5, result.size());
    assertInstanceFour(result.get(0), loadedClass);
    assertInstanceThree(result.get(1), loadedClass);
    assertInstanceTwo(result.get(2), loadedClass);
    assertInstanceFive(result.get(3), loadedClass);
    assertInstanceOne(result.get(4), loadedClass);
}
Also used : Order(org.motechproject.mds.util.Order) ArrayList(java.util.ArrayList) QueryParams(org.motechproject.mds.query.QueryParams)

Example 13 with Order

use of org.motechproject.mds.util.Order in project motech by motech.

the class HistoryServiceImpl method getLatestRevision.

private Object getLatestRevision(Class<?> historyClass, Long instanceId) {
    Query query = initQuery(historyClass);
    QueryUtil.setQueryParams(query, new QueryParams(1, 1, new Order(ID_FIELD_NAME, Order.Direction.DESC)));
    query.setUnique(true);
    return query.execute(instanceId);
}
Also used : Order(org.motechproject.mds.util.Order) Query(javax.jdo.Query) QueryParams(org.motechproject.mds.query.QueryParams)

Example 14 with Order

use of org.motechproject.mds.util.Order in project motech by motech.

the class ParamParser method buildQueryParams.

public static QueryParams buildQueryParams(Map<String, String> requestParams) {
    Integer page;
    Integer pageSize;
    String sortBy;
    String orderDir;
    Order order;
    try {
        page = getInteger(requestParams, PAGE, 1);
    } catch (NumberFormatException e) {
        throw new InvalidParameterException("Given page is not a number!", e);
    }
    try {
        pageSize = getInteger(requestParams, PAGE_SIZE, 20);
    } catch (NumberFormatException e) {
        throw new InvalidParameterException("Given page size is not a number!", e);
    }
    sortBy = requestParams.get(SORT_BY);
    orderDir = requestParams.get(ORDER_DIR);
    order = buildOrder(sortBy, orderDir);
    return new QueryParams(page, pageSize, order);
}
Also used : Order(org.motechproject.mds.util.Order) InvalidParameterException(org.motechproject.mds.web.exception.InvalidParameterException) QueryParams(org.motechproject.mds.query.QueryParams)

Example 15 with Order

use of org.motechproject.mds.util.Order in project motech by motech.

the class MdsRestFacadeTest method shouldAppendMetadataWhenExecutingLookup.

@Test
public void shouldAppendMetadataWhenExecutingLookup() {
    when(dataService.countSupportedLookup(anyString(), anyInt())).thenReturn(81l);
    Map<String, String> lookupMap = asLookupMap(null, "44");
    QueryParams queryParams = new QueryParams(5, 20, new Order("value", Order.Direction.DESC));
    when(dataService.supportedLookup(null, 44, queryParams)).thenReturn(asList(recordOne));
    RestResponse result = (RestResponse) mdsRestFacade.executeLookup(SUPPORTED_LOOKUP_NAME, lookupMap, queryParams, false);
    assertNotNull(result.getMetadata());
    assertEquals(ENTITY_NAME, result.getMetadata().getEntity());
    assertEquals(TEST_MODULE, result.getMetadata().getModule());
    assertEquals(Record.class.getName(), result.getMetadata().getClassName());
    assertEquals(NAMESPACE, result.getMetadata().getNamespace());
    assertEquals(5, result.getMetadata().getPage());
    assertEquals(20, result.getMetadata().getPageSize());
    assertEquals(81l, result.getMetadata().getTotalCount());
}
Also used : Order(org.motechproject.mds.util.Order) QueryParams(org.motechproject.mds.query.QueryParams) Record(org.motechproject.mds.testutil.records.Record) Matchers.anyString(org.mockito.Matchers.anyString) Test(org.junit.Test)

Aggregations

Order (org.motechproject.mds.util.Order)23 QueryParams (org.motechproject.mds.query.QueryParams)14 Test (org.junit.Test)13 ArrayList (java.util.ArrayList)6 Record (org.motechproject.mds.testutil.records.Record)6 DateTime (org.joda.time.DateTime)4 Arrays.asList (java.util.Arrays.asList)3 List (java.util.List)3 EmailRecordSearchCriteria (org.motechproject.email.builder.EmailRecordSearchCriteria)2 InputStream (java.io.InputStream)1 InputStreamReader (java.io.InputStreamReader)1 Reader (java.io.Reader)1 StringReader (java.io.StringReader)1 LocalDateTime (java.time.LocalDateTime)1 Collections.singletonList (java.util.Collections.singletonList)1 Comparator (java.util.Comparator)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 LinkedList (java.util.LinkedList)1 Query (javax.jdo.Query)1