use of org.motechproject.mds.domain.Field in project motech by motech.
the class EntityServiceImplTest method shouldSetUIDisplayableToAllFieldsIfMapIsEmpty.
@Test
public void shouldSetUIDisplayableToAllFieldsIfMapIsEmpty() {
// given
int fieldCount = 10;
List<Field> fields = new ArrayList<>(fieldCount);
for (int i = 0; i < fieldCount; ++i) {
Field fieldMock = mock(Field.class);
when(fieldMock.isReadOnly()).thenReturn(true);
fields.add(fieldMock);
}
// #1 when
doReturn(1L).when(entityDto).getId();
doReturn(entity).when(allEntities).retrieveById(1L);
doReturn(fields).when(entity).getFields();
entityService.addDisplayedFields(entityDto, null);
// then
verify(allEntities).retrieveById(1L);
verify(entity).getFields();
for (int i = 0; i < fieldCount; ++i) {
Field field = fields.get(i);
verify(field).setUIDisplayable(true);
verify(field).setUIDisplayPosition((long) i);
}
// #2 when
entityService.addDisplayedFields(entityDto, new HashMap<String, Long>());
// #2 then
verify(allEntities, times(2)).retrieveById(1L);
verify(entity, times(2)).getFields();
for (int i = 0; i < fieldCount; ++i) {
Field field = fields.get(i);
verify(field, times(2)).setUIDisplayable(true);
verify(field, times(2)).setUIDisplayPosition((long) i);
}
}
use of org.motechproject.mds.domain.Field 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.domain.Field 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.domain.Field in project motech by motech.
the class PropertyBuilderTest method shouldGenerateAppropriatePropertyType.
@Test
public void shouldGenerateAppropriatePropertyType() throws Exception {
Set<String> set = new HashSet<>();
Range<Integer> range = new Range<>(0, 1);
Type type = new Type("mds.field.combobox", "", List.class);
Entity entity = new Entity("org.motechproject.mds.Sample");
Field field = new Field(entity, "roles", "roles", type, true, false, false);
// should not create collection property for fields without list field
assertProperty(PropertyBuilder.create("roles", 1L, Long.class.getName()), EqualProperty.class, "roles", 1L);
field.addSetting(new FieldSetting(field, new TypeSetting(Constants.Settings.ALLOW_MULTIPLE_SELECTIONS), "true"));
// should create collection property for enum list
assertProperty(PropertyBuilder.create(field, "role"), CollectionProperty.class, "roles", Arrays.asList("role"));
field.addSetting(new FieldSetting(field, new TypeSetting(Constants.Settings.ALLOW_USER_SUPPLIED), "true"));
// should create collection property for string list
assertProperty(PropertyBuilder.create(field, "role"), CollectionProperty.class, "roles", Arrays.asList("role"));
assertProperty(PropertyBuilder.create("set", set, String.class), SetProperty.class, "set", set);
assertProperty(PropertyBuilder.create("range", range, Integer.class), RangeProperty.class, "range", range);
assertProperty(PropertyBuilder.create("equal", 1L, Long.class), EqualProperty.class, "equal", 1L);
assertProperty(PropertyBuilder.create("text", "someString", String.class.getName(), "matches()"), MatchesProperty.class, "text", ".*someString.*");
assertProperty(PropertyBuilder.create("text", "someString", String.class.getName(), "matches((?i))"), MatchesCaseInsensitiveProperty.class, "text", "(?i).*someString.*");
}
use of org.motechproject.mds.domain.Field in project motech by motech.
the class PropertyBuilderTest method shouldCreatePropertyByFieldAndValue.
@Test
public void shouldCreatePropertyByFieldAndValue() throws Exception {
Type type = new Type(String.class);
Field field = new Field(null, "ala", "ala", type, true, false, false);
assertProperty(PropertyBuilder.create(field, "cat"), "ala", "cat");
}
Aggregations