use of org.motechproject.mds.dto.LookupDto in project motech by motech.
the class MdsDummyDataGeneratorImpl method prepareDummyEntity.
private void prepareDummyEntity(int number, int fieldsPerEntity, int lookupsPerEntity) throws IOException {
EntityDto entityDto = new EntityDto(Long.valueOf(number), entityPrefix.concat(String.valueOf(number)));
entityDto = entityService.createEntity(entityDto);
List<FieldDto> fields = new ArrayList<>();
for (int i = 0; i < fieldsPerEntity; i++) {
TypeDto type = pickRandomFieldType();
fields.add(new FieldDto(null, entityDto.getId(), type, new FieldBasicDto(fieldPrefix.concat(String.valueOf(i)), fieldPrefix.concat(String.valueOf(i))), false, null, null, settingsFor(type), null));
}
entityService.addFields(entityDto, fields);
List<LookupDto> lookups = new ArrayList<>();
for (int i = 0; i < lookupsPerEntity; i++) {
List<LookupFieldDto> lookupFields = new ArrayList<>();
List<FieldDto> entityFields = entityService.getFields(entityDto.getId());
int amountOfFields = RAND.nextInt(entityFields.size());
for (int j = 0; j < amountOfFields; j++) {
lookupFields.add(new LookupFieldDto(null, entityFields.get(j).getBasic().getName(), LookupFieldType.VALUE));
}
lookups.add(new LookupDto(lookupPrefix.concat(String.valueOf(i)), RAND.nextBoolean(), RAND.nextBoolean(), lookupFields, false));
}
entityService.addLookups(entityDto.getId(), lookups);
}
use of org.motechproject.mds.dto.LookupDto in project motech by motech.
the class InstanceServiceTest method mockLookups.
private void mockLookups() {
LookupDto lookup = new LookupDto(TestDataService.LOOKUP_1_NAME, true, true, asList(FieldTestHelper.lookupFieldDto(1L, "strField")), true, "singleObject", asList("strField"));
when(entityService.getLookupByName(ENTITY_ID, TestDataService.LOOKUP_1_NAME)).thenReturn(lookup);
Map<String, FieldDto> mapping = new HashMap<>();
mapping.put("strField", FieldTestHelper.fieldDto(1L, "strField", String.class.getName(), "String field", "Default"));
when(entityService.getLookupFieldsMapping(ENTITY_ID, TestDataService.LOOKUP_1_NAME)).thenReturn(mapping);
lookup = new LookupDto(TestDataService.LOOKUP_2_NAME, false, true, asList(FieldTestHelper.lookupFieldDto(1L, "strField")), false, "multiObject", asList("strField"));
when(entityService.getLookupByName(ENTITY_ID, TestDataService.LOOKUP_2_NAME)).thenReturn(lookup);
when(entityService.getLookupFieldsMapping(ENTITY_ID, TestDataService.LOOKUP_2_NAME)).thenReturn(mapping);
lookup = new LookupDto(TestDataService.NULL_EXPECTING_LOOKUP_NAME, false, true, asList(FieldTestHelper.lookupFieldDto(3L, "dtField")), false, "nullParamExpected", asList("dtField"));
when(entityService.getLookupByName(ENTITY_ID, TestDataService.NULL_EXPECTING_LOOKUP_NAME)).thenReturn(lookup);
mapping = new HashMap<>();
mapping.put("dtField", FieldTestHelper.fieldDto(3L, "dtField", DateTime.class.getName(), "DateTime field", null));
when(entityService.getLookupFieldsMapping(ENTITY_ID, TestDataService.NULL_EXPECTING_LOOKUP_NAME)).thenReturn(mapping);
}
use of org.motechproject.mds.dto.LookupDto in project motech by motech.
the class EntityServiceImplTest method shouldAddNewLookup.
@Test
public void shouldAddNewLookup() {
// given
long entityId = 1L;
LookupDto lookupDto = new LookupDto("lookupName", true, false, null, false);
lookupDto.getLookupFields().add(lookupFieldDto("zero"));
lookupDto.getLookupFields().add(lookupFieldDto("two"));
int fieldCount = 4;
List<Field> fields = new ArrayList<>();
String[] fieldNames = { "zero", "one", "two", "three" };
for (long idx = 0; idx < fieldCount; ++idx) {
Field field = mock(Field.class);
doReturn(idx).when(field).getId();
doReturn(field).when(entity).getField(idx);
String fieldName = fieldNames[(int) idx];
doReturn(field).when(entity).getField(fieldName);
doReturn(fieldName).when(field).getName();
if (idx % 2 == 0) {
fields.add(field);
}
}
// when
doReturn(entity).when(allEntities).retrieveById(entityId);
doReturn(null).when(entity).getLookupById(anyLong());
entityService.addLookups(entityId, asList(lookupDto));
// then
verify(allEntities).retrieveById(entityId);
verify(entity).getLookupByName("lookupName");
verify(entity).addLookup(lookupCaptor.capture());
for (long idx = 0; idx < fieldCount; ++idx) {
int wantedNumberOfInvocations = (int) Math.abs(idx % 2 - 1);
verify(entity, times(wantedNumberOfInvocations)).getField(fieldNames[(int) idx]);
}
Lookup lookup = lookupCaptor.getValue();
assertEquals(lookupDto.getLookupName(), lookup.getLookupName());
assertEquals(lookupDto.isSingleObjectReturn(), lookup.isSingleObjectReturn());
assertEquals(lookupDto.isExposedViaRest(), lookup.isExposedViaRest());
assertEquals(fields, lookup.getFields());
}
use of org.motechproject.mds.dto.LookupDto in project motech by motech.
the class EntityServiceImplTest method shouldUpdateExistingLookup.
@Test
public void shouldUpdateExistingLookup() {
// given
long entityId = 1L;
LookupDto lookupDto = new LookupDto("lookupName", true, false, null, false);
lookupDto.getLookupFields().add(lookupFieldDto("zero"));
lookupDto.getLookupFields().add(lookupFieldDto("two"));
Lookup lookup = new Lookup();
int fieldCount = 4;
List<Field> fields = new ArrayList<>();
String[] fieldNames = { "zero", "one", "two", "three" };
for (long idx = 0; idx < fieldCount; ++idx) {
Field field = mock(Field.class);
doReturn(idx).when(field).getId();
doReturn(field).when(entity).getField(idx);
String fieldName = fieldNames[(int) idx];
doReturn(field).when(entity).getField(fieldName);
doReturn(fieldName).when(field).getName();
if (idx % 2 == 0) {
fields.add(field);
}
}
// when
doReturn(entity).when(allEntities).retrieveById(entityId);
doReturn(lookup).when(entity).getLookupByName("lookupName");
entityService.addLookups(entityId, asList(lookupDto));
// then
verify(allEntities).retrieveById(entityId);
verify(entity).getLookupByName("lookupName");
verify(entity, never()).addLookup(any(Lookup.class));
assertEquals(lookupDto.getLookupName(), lookup.getLookupName());
assertEquals(lookupDto.isSingleObjectReturn(), lookup.isSingleObjectReturn());
assertEquals(lookupDto.isExposedViaRest(), lookup.isExposedViaRest());
assertEquals(fields, lookup.getFields());
}
use of org.motechproject.mds.dto.LookupDto in project motech by motech.
the class EditableLookupsLoaderTest method shouldAddLookupIfJsonIsValid.
@Test
public void shouldAddLookupIfJsonIsValid() throws Exception {
JsonLookupDto expectedJsonLookup = new JsonLookupDto(CLASS_NAME, ORIGIN_LOOKUP_NAME);
LookupDto expectedLookup = loadLookup();
when(bundle.getResource(MDS_LOOKUPS_JSON)).thenReturn(VALID_JSON);
when(jsonLookupService.exists(CLASS_NAME, ORIGIN_LOOKUP_NAME)).thenReturn(false);
editableLookupsLoader.addEditableLookups(output, bundle);
verify(jsonLookupService, times(1)).exists(matches(CLASS_NAME), matches(ORIGIN_LOOKUP_NAME));
verify(jsonLookupService, times(1)).createJsonLookup(jsonLookupCaptor.capture());
assertLookupsEquals(expectedJsonLookup, jsonLookupCaptor.getValue());
assertEquals(1, output.getLookupProcessorOutputs().size());
assertEquals(1, output.getLookupProcessorOutputs().get(CLASS_NAME).size());
assertEquals(expectedLookup, output.getLookupProcessorOutputs().get(CLASS_NAME).get(0));
}
Aggregations