Search in sources :

Example 96 with FieldDto

use of org.motechproject.mds.dto.FieldDto in project motech by motech.

the class MdsBundleIT method testUserPreferences.

@Test
public void testUserPreferences() {
    EntityDto entityDto = createEntityForPreferencesTest();
    // first retrieve - should create default user preferences for entity
    UserPreferencesDto userPreferencesDto = userPreferencesService.getUserPreferences(entityDto.getId(), USERNAME);
    assertEquals(new Integer(50), userPreferencesDto.getGridRowsNumber());
    assertEquals(3, userPreferencesDto.getVisibleFields().size());
    assertTrue(userPreferencesDto.getVisibleFields().contains("someBoolean"));
    assertTrue(userPreferencesDto.getVisibleFields().contains("someString"));
    assertTrue(userPreferencesDto.getVisibleFields().contains("someInteger"));
    assertEquals(0, userPreferencesDto.getSelectedFields().size());
    assertEquals(0, userPreferencesDto.getUnselectedFields().size());
    userPreferencesService.updateGridSize(entityDto.getId(), USERNAME, 100);
    userPreferencesDto = userPreferencesService.getUserPreferences(entityDto.getId(), USERNAME);
    assertEquals(new Integer(100), userPreferencesDto.getGridRowsNumber());
    // if null then default value from settings will be used
    userPreferencesService.updateGridSize(entityDto.getId(), USERNAME, null);
    userPreferencesDto = userPreferencesService.getUserPreferences(entityDto.getId(), USERNAME);
    assertEquals(new Integer(50), userPreferencesDto.getGridRowsNumber());
    userPreferencesService.unselectField(entityDto.getId(), USERNAME, "someString");
    userPreferencesDto = userPreferencesService.getUserPreferences(entityDto.getId(), USERNAME);
    assertEquals(2, userPreferencesDto.getVisibleFields().size());
    assertTrue(userPreferencesDto.getVisibleFields().contains("someBoolean"));
    assertTrue(userPreferencesDto.getVisibleFields().contains("someInteger"));
    assertEquals(0, userPreferencesDto.getSelectedFields().size());
    assertEquals(1, userPreferencesDto.getUnselectedFields().size());
    assertTrue(userPreferencesDto.getUnselectedFields().contains("someString"));
    userPreferencesService.selectField(entityDto.getId(), USERNAME, "otherInteger");
    userPreferencesDto = userPreferencesService.getUserPreferences(entityDto.getId(), USERNAME);
    assertEquals(3, userPreferencesDto.getVisibleFields().size());
    assertTrue(userPreferencesDto.getVisibleFields().contains("someBoolean"));
    assertTrue(userPreferencesDto.getVisibleFields().contains("someInteger"));
    assertTrue(userPreferencesDto.getVisibleFields().contains("otherInteger"));
    assertEquals(1, userPreferencesDto.getSelectedFields().size());
    assertTrue(userPreferencesDto.getSelectedFields().contains("otherInteger"));
    assertEquals(1, userPreferencesDto.getUnselectedFields().size());
    assertTrue(userPreferencesDto.getUnselectedFields().contains("someString"));
    userPreferencesService.selectField(entityDto.getId(), USERNAME, "someString");
    userPreferencesDto = userPreferencesService.getUserPreferences(entityDto.getId(), USERNAME);
    assertEquals(4, userPreferencesDto.getVisibleFields().size());
    assertTrue(userPreferencesDto.getVisibleFields().contains("someBoolean"));
    assertTrue(userPreferencesDto.getVisibleFields().contains("otherInteger"));
    assertTrue(userPreferencesDto.getVisibleFields().contains("otherInteger"));
    assertTrue(userPreferencesDto.getVisibleFields().contains("someString"));
    assertEquals(2, userPreferencesDto.getSelectedFields().size());
    assertTrue(userPreferencesDto.getSelectedFields().contains("otherInteger"));
    assertTrue(userPreferencesDto.getSelectedFields().contains("someString"));
    assertEquals(0, userPreferencesDto.getUnselectedFields().size());
    userPreferencesService.unselectFields(entityDto.getId(), USERNAME);
    userPreferencesDto = userPreferencesService.getUserPreferences(entityDto.getId(), USERNAME);
    assertEquals(0, userPreferencesDto.getVisibleFields().size());
    assertEquals(0, userPreferencesDto.getSelectedFields().size());
    assertEquals(10, userPreferencesDto.getUnselectedFields().size());
    userPreferencesService.selectFields(entityDto.getId(), USERNAME);
    userPreferencesDto = userPreferencesService.getUserPreferences(entityDto.getId(), USERNAME);
    assertEquals(10, userPreferencesDto.getVisibleFields().size());
    assertEquals(10, userPreferencesDto.getSelectedFields().size());
    assertEquals(0, userPreferencesDto.getUnselectedFields().size());
    // if field will be removed from entity then it should be removed also from preferences (CASCADE)
    EntityDraft draft = entityService.getEntityDraft(entityDto.getId());
    List<FieldDto> fields1 = entityService.getEntityFields(draft.getId());
    Long someIntegerId = getFieldIdByName(fields1, "someInteger");
    DraftData draftData = DraftBuilder.forFieldRemoval(someIntegerId);
    entityService.saveDraftEntityChanges(entityDto.getId(), draftData);
    entityService.commitChanges(entityDto.getId());
    userPreferencesDto = userPreferencesService.getUserPreferences(entityDto.getId(), USERNAME);
    assertEquals(9, userPreferencesDto.getVisibleFields().size());
    assertTrue(userPreferencesDto.getSelectedFields().contains("someBoolean"));
    assertTrue(userPreferencesDto.getSelectedFields().contains("someString"));
    assertFalse(userPreferencesDto.getSelectedFields().contains("someInteger"));
}
Also used : EntityDto(org.motechproject.mds.dto.EntityDto) DraftData(org.motechproject.mds.dto.DraftData) UserPreferencesDto(org.motechproject.mds.dto.UserPreferencesDto) EntityDraft(org.motechproject.mds.domain.EntityDraft) LookupFieldDto(org.motechproject.mds.dto.LookupFieldDto) FieldDto(org.motechproject.mds.dto.FieldDto) Test(org.junit.Test)

Example 97 with FieldDto

use of org.motechproject.mds.dto.FieldDto in project motech by motech.

the class EntityServiceImplTest method shouldAddNewField.

@Test
public void shouldAddNewField() {
    // given
    long entityId = 1L;
    EntityDto dto = new EntityDto(entityId, CLASS_NAME);
    TypeDto type = TypeDto.INTEGER;
    Type integerType = new Type(Integer.class);
    FieldBasicDto basic = new FieldBasicDto();
    basic.setDisplayName("pi");
    basic.setName("pi");
    basic.setRequired(true);
    basic.setDefaultValue("3.14");
    basic.setTooltip("Sets the value of the PI number");
    basic.setPlaceholder("3.14");
    FieldDto fieldDto = new FieldDto();
    fieldDto.setEntityId(dto.getId());
    fieldDto.setType(type);
    fieldDto.setBasic(basic);
    // when
    doReturn(entity).when(allEntities).retrieveById(dto.getId());
    doReturn(entityId).when(entity).getId();
    doReturn(integerType).when(allTypes).retrieveByClassName(type.getTypeClass());
    entityService.addFields(dto, asList(fieldDto));
    // then
    verify(allEntities).retrieveById(dto.getId());
    verify(entity).addField(fieldCaptor.capture());
    Field field = fieldCaptor.getValue();
    assertEquals(basic.getName(), field.getName());
    assertEquals(basic.getDisplayName(), field.getDisplayName());
    assertEquals(basic.isRequired(), field.isRequired());
    assertEquals(basic.getDefaultValue(), field.getDefaultValue());
    assertEquals(basic.getTooltip(), field.getTooltip());
    assertEquals(basic.getPlaceholder(), field.getPlaceholder());
    assertEquals(fieldDto.getType().getTypeClass(), field.getType().getTypeClass().getName());
    assertEquals(fieldDto.getEntityId(), field.getEntity().getId());
}
Also used : EntityDto(org.motechproject.mds.dto.EntityDto) Field(org.motechproject.mds.domain.Field) Type(org.motechproject.mds.domain.Type) FieldBasicDto(org.motechproject.mds.dto.FieldBasicDto) TypeDto(org.motechproject.mds.dto.TypeDto) FieldTestHelper.lookupFieldDto(org.motechproject.mds.testutil.FieldTestHelper.lookupFieldDto) FieldDto(org.motechproject.mds.dto.FieldDto) Test(org.junit.Test)

Example 98 with FieldDto

use of org.motechproject.mds.dto.FieldDto in project motech by motech.

the class EntityServiceImplTest method shouldUpdateExistingField.

@Test
public void shouldUpdateExistingField() {
    // given
    long entityId = 1L;
    EntityDto dto = new EntityDto(entityId, CLASS_NAME);
    TypeDto type = TypeDto.INTEGER;
    Field field = mock(Field.class);
    FieldBasicDto basic = new FieldBasicDto();
    basic.setDisplayName("pi");
    basic.setName("pi");
    basic.setRequired(true);
    basic.setDefaultValue("3.14");
    basic.setTooltip("Sets the value of the PI number");
    basic.setPlaceholder("3.14");
    FieldDto fieldDto = new FieldDto();
    fieldDto.setEntityId(dto.getId());
    fieldDto.setType(type);
    fieldDto.setBasic(basic);
    // when
    doReturn(entity).when(allEntities).retrieveById(dto.getId());
    doReturn(entityId).when(entity).getId();
    doReturn(field).when(entity).getField(basic.getName());
    entityService.addFields(dto, asList(fieldDto));
    // then
    verify(allEntities).retrieveById(dto.getId());
    verify(entity).getField(basic.getName());
    verify(entity, never()).addField(any(Field.class));
    verify(field).update(fieldDto);
}
Also used : EntityDto(org.motechproject.mds.dto.EntityDto) Field(org.motechproject.mds.domain.Field) FieldBasicDto(org.motechproject.mds.dto.FieldBasicDto) TypeDto(org.motechproject.mds.dto.TypeDto) FieldTestHelper.lookupFieldDto(org.motechproject.mds.testutil.FieldTestHelper.lookupFieldDto) FieldDto(org.motechproject.mds.dto.FieldDto) Test(org.junit.Test)

Example 99 with FieldDto

use of org.motechproject.mds.dto.FieldDto in project motech by motech.

the class EntityServiceImplTest method shouldMarkUniqueChangesInDraft.

@Test
public void shouldMarkUniqueChangesInDraft() {
    DraftData dd = new DraftData();
    dd.setEdit(true);
    dd.getValues().put(DraftData.VALUE, singletonList(true));
    dd.getValues().put(DraftData.PATH, "basic.unique");
    dd.getValues().put(DraftData.FIELD_ID, 2L);
    when(allEntities.retrieveById(1L)).thenReturn(entity);
    when(allEntityDrafts.retrieve(entity, "motech")).thenReturn(draft);
    when(draft.getField(2L)).thenReturn(field);
    when(draft.getParentEntity()).thenReturn(entity);
    when(entity.getField("fieldName")).thenReturn(field);
    when(field.getName()).thenReturn("fieldName");
    when(field.toDto()).thenReturn(fieldDto);
    when(fieldDto.getBasic()).thenReturn(basic);
    when(field.isUnique()).thenReturn(true);
    entityService.saveDraftEntityChanges(1L, dd);
    verify(field).update(any(FieldDto.class));
    verify(draft, never()).addUniqueToRemove(anyString());
    // test marking for removal
    dd.getValues().put(DraftData.VALUE, singletonList(false));
    entityService.saveDraftEntityChanges(1L, dd);
    verify(draft).addUniqueToRemove("fieldName");
}
Also used : DraftData(org.motechproject.mds.dto.DraftData) FieldTestHelper.lookupFieldDto(org.motechproject.mds.testutil.FieldTestHelper.lookupFieldDto) FieldDto(org.motechproject.mds.dto.FieldDto) Test(org.junit.Test)

Example 100 with FieldDto

use of org.motechproject.mds.dto.FieldDto 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)

Aggregations

FieldDto (org.motechproject.mds.dto.FieldDto)158 Test (org.junit.Test)61 LookupFieldDto (org.motechproject.mds.dto.LookupFieldDto)60 EntityDto (org.motechproject.mds.dto.EntityDto)53 ArrayList (java.util.ArrayList)47 LookupDto (org.motechproject.mds.dto.LookupDto)29 List (java.util.List)23 MetadataDto (org.motechproject.mds.dto.MetadataDto)22 FieldBasicDto (org.motechproject.mds.dto.FieldBasicDto)19 TypeDto (org.motechproject.mds.dto.TypeDto)17 Method (java.lang.reflect.Method)14 MotechDataService (org.motechproject.mds.service.MotechDataService)14 HashMap (java.util.HashMap)12 Field (org.motechproject.mds.domain.Field)12 FieldTestHelper.lookupFieldDto (org.motechproject.mds.testutil.FieldTestHelper.lookupFieldDto)12 Arrays.asList (java.util.Arrays.asList)11 SettingDto (org.motechproject.mds.dto.SettingDto)11 Before (org.junit.Before)7 AdvancedSettingsDto (org.motechproject.mds.dto.AdvancedSettingsDto)7 BasicEntityRecord (org.motechproject.mds.web.domain.BasicEntityRecord)7