Search in sources :

Example 41 with Field

use of org.motechproject.mds.domain.Field in project motech by motech.

the class EntityServiceImplTest method shouldSetUIDisplayableOnlyToCorrectFields.

@Test
public void shouldSetUIDisplayableOnlyToCorrectFields() {
    // given
    int fieldCount = 12;
    long displayPosition = fieldCount / 4 - 1;
    List<Field> fields = new ArrayList<>(fieldCount);
    Map<String, Long> positions = new HashMap<>();
    for (int i = 0; i < fieldCount; ++i) {
        Field field = mock(Field.class);
        // every 4th field will have set the uiDisplayable flag
        if (i % 4 == 0) {
            String fieldName = "field" + i;
            doReturn(fieldName).when(field).getName();
            positions.put(fieldName, displayPosition);
            --displayPosition;
        }
        fields.add(field);
    }
    // when
    doReturn(1L).when(entityDto).getId();
    doReturn(entity).when(allEntities).retrieveById(1L);
    doReturn(fields).when(entity).getFields();
    entityService.addDisplayedFields(entityDto, positions);
    // then
    verify(allEntities).retrieveById(1L);
    verify(entity).getFields();
    for (int i = 0; i < fieldCount; ++i) {
        Field field = fields.get(i);
        // flag should be unset
        if (i % 4 == 0) {
            verify(field).setUIDisplayable(true);
            verify(field).setUIDisplayPosition(positions.get("field" + i));
        } else {
            verify(field).setUIDisplayable(false);
            verify(field).setUIDisplayPosition(null);
        }
    }
}
Also used : Field(org.motechproject.mds.domain.Field) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Matchers.anyLong(org.mockito.Matchers.anyLong) Matchers.anyString(org.mockito.Matchers.anyString) Test(org.junit.Test)

Example 42 with Field

use of org.motechproject.mds.domain.Field in project motech by motech.

the class EntityServiceContextIT method shouldUpdateUserPreferencesAfterCommit.

@Test
public void shouldUpdateUserPreferencesAfterCommit() {
    EntityDto entityDto = new EntityDto();
    entityDto.setName("UserPrefTest");
    entityDto = entityService.createEntity(entityDto);
    final Long entityId = entityDto.getId();
    entityService.saveDraftEntityChanges(entityId, DraftBuilder.forNewField("disp", "f1name", Long.class.getName()));
    List<FieldDto> fields = entityService.getFields(entityId);
    assertNotNull(fields);
    assertEquals(asList("id", "creator", "owner", "modifiedBy", "creationDate", "modificationDate", "f1name"), extract(fields, on(FieldDto.class).getBasic().getName()));
    entityService.commitChanges(entityId);
    List<Field> draftFields = entityService.getEntityDraft(entityId).getFields();
    Field field = selectFirst(draftFields, having(on(Field.class).getName(), equalTo("f1name")));
    entityService.saveDraftEntityChanges(entityDto.getId(), DraftBuilder.forFieldEdit(field.getId(), "basic.name", "newName"));
    userPreferencesService.selectFields(entityId, "motech");
    userPreferencesService.unselectField(entityId, "motech", "id");
    userPreferencesService.unselectField(entityId, "motech", "owner");
    UserPreferencesDto userPreferencesDto = userPreferencesService.getUserPreferences(entityId, "motech");
    assertEquals(5, userPreferencesDto.getVisibleFields().size());
    assertTrue(userPreferencesDto.getVisibleFields().contains("f1name"));
    assertTrue(userPreferencesDto.getVisibleFields().contains("creator"));
    assertTrue(userPreferencesDto.getVisibleFields().contains("modifiedBy"));
    assertTrue(userPreferencesDto.getVisibleFields().contains("creationDate"));
    assertTrue(userPreferencesDto.getVisibleFields().contains("modificationDate"));
    entityService.commitChanges(entityId);
    userPreferencesDto = userPreferencesService.getUserPreferences(entityId, "motech");
    assertEquals(5, userPreferencesDto.getVisibleFields().size());
    assertTrue(userPreferencesDto.getVisibleFields().contains("newName"));
    assertTrue(userPreferencesDto.getVisibleFields().contains("creator"));
    assertTrue(userPreferencesDto.getVisibleFields().contains("modifiedBy"));
    assertTrue(userPreferencesDto.getVisibleFields().contains("creationDate"));
    assertTrue(userPreferencesDto.getVisibleFields().contains("modificationDate"));
    assertEquals(2, userPreferencesDto.getUnselectedFields().size());
    assertTrue(userPreferencesDto.getUnselectedFields().contains("id"));
    assertTrue(userPreferencesDto.getUnselectedFields().contains("owner"));
}
Also used : EntityDto(org.motechproject.mds.dto.EntityDto) Field(org.motechproject.mds.domain.Field) UserPreferencesDto(org.motechproject.mds.dto.UserPreferencesDto) LookupFieldDto(org.motechproject.mds.dto.LookupFieldDto) FieldDto(org.motechproject.mds.dto.FieldDto) Test(org.junit.Test)

Example 43 with Field

use of org.motechproject.mds.domain.Field in project motech by motech.

the class SwaggerFieldConverterTest method shouldConvertComboboxes.

@Test
public void shouldConvertComboboxes() {
    // not user-supplied
    Property property = SwaggerFieldConverter.fieldToProperty(field(List.class));
    verifySimpleProperty(property, "string", null);
    Entity entity = new Entity("org.example.Something");
    Field field = FieldTestHelper.fieldWithComboboxSettings(entity, "name", "disp name", List.class, true, false, asList("a", "b", "c"));
    property = SwaggerFieldConverter.fieldToProperty(field);
    verifyComboboxProperty(property, true);
    // user-supplied
    FieldTestHelper.setAllowUserSupplied(field, true);
    property = SwaggerFieldConverter.fieldToProperty(field);
    verifyComboboxProperty(property, false);
}
Also used : Entity(org.motechproject.mds.domain.Entity) Field(org.motechproject.mds.domain.Field) List(java.util.List) Arrays.asList(java.util.Arrays.asList) Property(org.motechproject.mds.docs.swagger.model.Property) Test(org.junit.Test)

Example 44 with Field

use of org.motechproject.mds.domain.Field in project motech by motech.

the class UserPreferencesServiceTest method shouldUnselectField.

@Test
public void shouldUnselectField() {
    Field field1 = getField1();
    when(allUserPreferences.retrieveByClassNameAndUsername(CLASS_NAME, USERNAME)).thenReturn(userPreferences);
    when(entity.getField("sampleField1")).thenReturn(field1);
    userPreferencesService.unselectField(15l, USERNAME, "sampleField1");
    verify(userPreferences).unselectField(fieldCaptor.capture());
    verify(allUserPreferences).update(userPreferences);
    Field field = fieldCaptor.getValue();
    assertNotNull(field);
    assertEquals("sampleField1", field.getName());
}
Also used : Field(org.motechproject.mds.domain.Field) Test(org.junit.Test)

Example 45 with Field

use of org.motechproject.mds.domain.Field in project motech by motech.

the class UserPreferencesServiceTest method shouldSelectField.

@Test
public void shouldSelectField() {
    when(allUserPreferences.retrieveByClassNameAndUsername(CLASS_NAME, USERNAME)).thenReturn(userPreferences);
    when(entity.getField("sampleField1")).thenReturn(getField1());
    userPreferencesService.selectField(15l, USERNAME, "sampleField1");
    verify(userPreferences).selectField(fieldCaptor.capture());
    verify(allUserPreferences).update(userPreferences);
    Field field = fieldCaptor.getValue();
    assertNotNull(field);
    assertEquals("sampleField1", field.getName());
}
Also used : Field(org.motechproject.mds.domain.Field) 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