Search in sources :

Example 6 with Field

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);
    }
}
Also used : Field(org.motechproject.mds.domain.Field) ArrayList(java.util.ArrayList) Matchers.anyLong(org.mockito.Matchers.anyLong) Matchers.anyString(org.mockito.Matchers.anyString) Test(org.junit.Test)

Example 7 with Field

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());
}
Also used : Field(org.motechproject.mds.domain.Field) LookupDto(org.motechproject.mds.dto.LookupDto) ArrayList(java.util.ArrayList) Lookup(org.motechproject.mds.domain.Lookup) Matchers.anyString(org.mockito.Matchers.anyString) Test(org.junit.Test)

Example 8 with Field

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());
}
Also used : Field(org.motechproject.mds.domain.Field) LookupDto(org.motechproject.mds.dto.LookupDto) ArrayList(java.util.ArrayList) Lookup(org.motechproject.mds.domain.Lookup) Matchers.anyString(org.mockito.Matchers.anyString) Test(org.junit.Test)

Example 9 with Field

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.*");
}
Also used : FieldSetting(org.motechproject.mds.domain.FieldSetting) Entity(org.motechproject.mds.domain.Entity) Field(org.motechproject.mds.domain.Field) Type(org.motechproject.mds.domain.Type) TypeSetting(org.motechproject.mds.domain.TypeSetting) Range(org.motechproject.commons.api.Range) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 10 with Field

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");
}
Also used : Field(org.motechproject.mds.domain.Field) Type(org.motechproject.mds.domain.Type) Test(org.junit.Test)

Aggregations

Field (org.motechproject.mds.domain.Field)73 Entity (org.motechproject.mds.domain.Entity)33 Test (org.junit.Test)24 Lookup (org.motechproject.mds.domain.Lookup)16 MdsEntity (org.motechproject.mds.domain.MdsEntity)15 MdsVersionedEntity (org.motechproject.mds.domain.MdsVersionedEntity)15 Type (org.motechproject.mds.domain.Type)14 ArrayList (java.util.ArrayList)13 Transactional (org.springframework.transaction.annotation.Transactional)13 FieldDto (org.motechproject.mds.dto.FieldDto)12 LookupFieldDto (org.motechproject.mds.dto.LookupFieldDto)12 HashSet (java.util.HashSet)8 Matchers.anyString (org.mockito.Matchers.anyString)6 FieldSetting (org.motechproject.mds.domain.FieldSetting)6 TypeSetting (org.motechproject.mds.domain.TypeSetting)6 UserPreferences (org.motechproject.mds.domain.UserPreferences)5 EntityDto (org.motechproject.mds.dto.EntityDto)5 AllUserPreferences (org.motechproject.mds.repository.internal.AllUserPreferences)5 FieldMetadata (org.motechproject.mds.domain.FieldMetadata)4 LookupDto (org.motechproject.mds.dto.LookupDto)4