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);
}
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;
}
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));
}
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();
}
}
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));
}
Aggregations