Search in sources :

Example 46 with LookupDto

use of org.motechproject.mds.dto.LookupDto in project motech by motech.

the class SchemaComparatorTest method shouldCompareOnlyReadOnlyLookups.

@Test
public void shouldCompareOnlyReadOnlyLookups() {
    LookupDto lookupDto = new LookupDto("lookupName", true, true, new ArrayList<LookupFieldDto>(), true, "methodName", new ArrayList<String>());
    LookupDto lookupDto2 = new LookupDto();
    lookupDto2.setReadOnly(false);
    entityLookups = asList(lookupDto, lookupDto2);
    newLookups = asList(lookupDto);
    when(entityService.getEntityLookups(ENTITY_ID)).thenReturn(entityLookups);
    boolean lookupsDiffer = schemaComparator.lookupsDiffer(ENTITY_ID, newLookups);
    assertFalse(lookupsDiffer);
}
Also used : LookupDto(org.motechproject.mds.dto.LookupDto) LookupFieldDto(org.motechproject.mds.dto.LookupFieldDto) Test(org.junit.Test)

Example 47 with LookupDto

use of org.motechproject.mds.dto.LookupDto in project motech by motech.

the class MDSDataProvider method findUsingLookup.

private Object findUsingLookup(String type, String lookupName, Map<String, String> lookupMap) {
    Object obj = null;
    LookupDto lookup = null;
    EntityDto entity = entityService.getEntityByClassName(type);
    if (entity != null) {
        lookup = entityService.getLookupByName(entity.getId(), lookupName);
    }
    if (entity != null && lookup != null) {
        String serviceName = MotechClassPool.getInterfaceName(type);
        MotechDataService service = OSGiServiceUtils.findService(bundleContext, serviceName);
        if (service != null) {
            Map<String, FieldDto> fieldsByName = entityService.getLookupFieldsMapping(entity.getId(), lookupName);
            LookupExecutor executor = new LookupExecutor(service, lookup, fieldsByName);
            obj = executor.execute(lookupMap);
        } else {
            getLogger().error("Service %s not found", serviceName);
        }
    }
    // we allow executing lookups that return multiple objects
    // if such a lookup returns more then 1 object we throw an exception
    Object result = null;
    if (obj instanceof Collection) {
        Collection collection = (Collection) obj;
        if (collection.size() == 1) {
            result = collection.iterator().next();
        } else if (collection.size() > 1) {
            throw new IllegalArgumentException(String.format("Data provided lookup for %s returned more then 1 object, number of objects found: %d", type, collection.size()));
        }
    } else {
        result = obj;
    }
    return result;
}
Also used : EntityDto(org.motechproject.mds.dto.EntityDto) LookupDto(org.motechproject.mds.dto.LookupDto) Collection(java.util.Collection) LookupExecutor(org.motechproject.mds.lookup.LookupExecutor) MotechDataService(org.motechproject.mds.service.MotechDataService) FieldDto(org.motechproject.mds.dto.FieldDto)

Example 48 with LookupDto

use of org.motechproject.mds.dto.LookupDto in project motech by motech.

the class LookupProcessorTest method shouldNotUpdateRestExposedValueForLookupsThatHaveThatModifiedByUser.

@Test
public void shouldNotUpdateRestExposedValueForLookupsThatHaveThatModifiedByUser() throws Exception {
    when(paranamer.lookupParameterNames(getTestMethodExposedViaRest())).thenReturn(argNames);
    AdvancedSettingsDto advanced = mock(AdvancedSettingsDto.class);
    RestOptionsDto restOptions = mock(RestOptionsDto.class);
    when(schemaHolder.getAdvancedSettings(TEST_CLASS_NAME)).thenReturn(advanced);
    when(advanced.getRestOptions()).thenReturn(restOptions);
    when(restOptions.isModifiedByUser()).thenReturn(true);
    EntityProcessorOutput eop = mockEntityProcessorOutput(new EntityDto(TestClass.class.getName()), Arrays.asList(new FieldDto("aaa", "bbb", TypeDto.STRING)));
    lookupProcessor.setEntityProcessingResult(Arrays.asList(eop));
    Method method = getTestMethodExposedViaRest();
    LookupDto dto = new LookupDto("Test Method Exposed Via Rest", true, false, lookupFieldDtos(argNames), true, "testMethodExposedViaRest", asList(argNames), true);
    lookupProcessor.process(method);
    Map<String, List<LookupDto>> elements = lookupProcessor.getProcessingResult();
    assertTrue(elements.containsKey(TEST_CLASS_NAME));
    List<LookupDto> list = elements.get(TEST_CLASS_NAME);
    assertEquals(1, list.size());
    assertEquals(dto, list.get(0));
}
Also used : EntityDto(org.motechproject.mds.dto.EntityDto) LookupDto(org.motechproject.mds.dto.LookupDto) ArrayList(java.util.ArrayList) Arrays.asList(java.util.Arrays.asList) List(java.util.List) AdvancedSettingsDto(org.motechproject.mds.dto.AdvancedSettingsDto) Method(java.lang.reflect.Method) RestOptionsDto(org.motechproject.mds.dto.RestOptionsDto) FieldTestHelper.lookupFieldDto(org.motechproject.mds.testutil.FieldTestHelper.lookupFieldDto) FieldDto(org.motechproject.mds.dto.FieldDto) LookupFieldDto(org.motechproject.mds.dto.LookupFieldDto) Test(org.junit.Test)

Example 49 with LookupDto

use of org.motechproject.mds.dto.LookupDto in project motech by motech.

the class LookupProcessorTest method shouldProcessMethodWithRangeParam.

@Test
public void shouldProcessMethodWithRangeParam() throws NoSuchMethodException {
    FieldDto arg0Field = new FieldDto("arg0Field", "Arg 0 Field", TypeDto.BOOLEAN);
    FieldDto rangeField = new FieldDto("rangeField", "Range Field", TypeDto.STRING);
    FieldDto regularFieldField = new FieldDto("regularField", "Regular Field", TypeDto.BOOLEAN);
    FieldDto rangeFieldField = new FieldDto("rangeFieldDouble", "Range Field Double", TypeDto.DOUBLE);
    EntityProcessorOutput eop = mockEntityProcessorOutput(new EntityDto(TestClass.class.getName()), Arrays.asList(arg0Field, rangeField, regularFieldField, rangeFieldField));
    lookupProcessor.setEntityProcessingResult(Arrays.asList(eop));
    LookupFieldDto[][] expectedFields = { { lookupFieldDto("arg0"), lookupFieldDto("range", RANGE) }, { lookupFieldDto("regularField"), lookupFieldDto("rangeField", RANGE) } };
    String[][] expectedFieldsOrder = { { "arg0", "range" }, { "regularField", "rangeField" } };
    // test two methods, one with @LookupField annotations, second without
    for (int i = 0; i < 2; i++) {
        Method method = getTestMethodWithRangeParam(i);
        when(paranamer.lookupParameterNames(method)).thenReturn(new String[] { "arg0", "range" });
        LookupDto expectedLookup = new LookupDto("Test Method With Range Param " + i, false, false, asList(expectedFields[i]), true, "testMethodWithRangeParam" + i, asList(expectedFieldsOrder[i]), true);
        lookupProcessor.process(method);
        Map<String, List<LookupDto>> elements = lookupProcessor.getProcessingResult();
        assertTrue(elements.containsKey(TEST_CLASS_NAME));
        List<LookupDto> list = elements.get(TEST_CLASS_NAME);
        assertEquals(1, list.size());
        assertEquals(expectedLookup, list.get(0));
        assertEquals(asList(VALUE, RANGE), extract(list.get(0).getLookupFields(), on(LookupFieldDto.class).getType()));
        lookupProcessor.clear();
    }
}
Also used : LookupDto(org.motechproject.mds.dto.LookupDto) Method(java.lang.reflect.Method) EntityDto(org.motechproject.mds.dto.EntityDto) ArrayList(java.util.ArrayList) Arrays.asList(java.util.Arrays.asList) List(java.util.List) FieldTestHelper.lookupFieldDto(org.motechproject.mds.testutil.FieldTestHelper.lookupFieldDto) FieldDto(org.motechproject.mds.dto.FieldDto) LookupFieldDto(org.motechproject.mds.dto.LookupFieldDto) LookupFieldDto(org.motechproject.mds.dto.LookupFieldDto) Test(org.junit.Test)

Example 50 with LookupDto

use of org.motechproject.mds.dto.LookupDto in project motech by motech.

the class LookupProcessorTest method shouldProcessMethodWithNotAnnotatedParameters.

@Test
public void shouldProcessMethodWithNotAnnotatedParameters() throws NoSuchMethodException {
    FieldDto arg1Field = new FieldDto("arg1", "Arg1", TypeDto.INTEGER);
    FieldDto secondArgumentField = new FieldDto("secondArgument", "Second Argument", TypeDto.STRING);
    lookupProcessor.setEntityProcessingResult(Arrays.asList(mockEntityProcessorOutput(new EntityDto(TestClass.class.getName()), Arrays.asList(arg1Field, secondArgumentField))));
    when(paranamer.lookupParameterNames(getTestMethod(2))).thenReturn(argNames);
    Method method = getTestMethod(2);
    lookupProcessor.process(method);
    Map<String, List<LookupDto>> elements = lookupProcessor.getElements();
    assertTrue(elements.containsKey(TEST_CLASS_NAME));
    List<LookupDto> list = elements.get(TEST_CLASS_NAME);
    LookupDto expected = new LookupDto("Test Method 2", false, false, lookupFieldDtos(argNames), true, "testMethod2", asList(argNames), true);
    assertEquals(1, list.size());
    assertEquals(expected, list.get(0));
}
Also used : EntityDto(org.motechproject.mds.dto.EntityDto) LookupDto(org.motechproject.mds.dto.LookupDto) ArrayList(java.util.ArrayList) Arrays.asList(java.util.Arrays.asList) List(java.util.List) Method(java.lang.reflect.Method) FieldTestHelper.lookupFieldDto(org.motechproject.mds.testutil.FieldTestHelper.lookupFieldDto) FieldDto(org.motechproject.mds.dto.FieldDto) LookupFieldDto(org.motechproject.mds.dto.LookupFieldDto) Test(org.junit.Test)

Aggregations

LookupDto (org.motechproject.mds.dto.LookupDto)54 LookupFieldDto (org.motechproject.mds.dto.LookupFieldDto)32 FieldDto (org.motechproject.mds.dto.FieldDto)29 ArrayList (java.util.ArrayList)26 EntityDto (org.motechproject.mds.dto.EntityDto)24 Test (org.junit.Test)23 List (java.util.List)10 Method (java.lang.reflect.Method)9 Arrays.asList (java.util.Arrays.asList)8 FieldTestHelper.lookupFieldDto (org.motechproject.mds.testutil.FieldTestHelper.lookupFieldDto)7 HashMap (java.util.HashMap)6 Field (org.motechproject.mds.domain.Field)5 Lookup (org.motechproject.mds.domain.Lookup)5 AdvancedSettingsDto (org.motechproject.mds.dto.AdvancedSettingsDto)5 FieldBasicDto (org.motechproject.mds.dto.FieldBasicDto)5 Before (org.junit.Before)4 Matchers.anyString (org.mockito.Matchers.anyString)4 RestOptionsDto (org.motechproject.mds.dto.RestOptionsDto)4 LookupExecutor (org.motechproject.mds.lookup.LookupExecutor)4 MetadataDto (org.motechproject.mds.dto.MetadataDto)3