Search in sources :

Example 86 with EntityDto

use of org.motechproject.mds.dto.EntityDto 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 87 with EntityDto

use of org.motechproject.mds.dto.EntityDto 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 88 with EntityDto

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

the class AllEntityDraftsContextIT method shouldCreateAndDeleteDrafts.

@Test
public void shouldCreateAndDeleteDrafts() {
    EntityDto dto = new EntityDto();
    dto.setClassName("DraftCls");
    Entity entity = allEntities.create(dto);
    allEntityDrafts.create(entity, USERNAME);
    allEntityDrafts.create(entity, USERNAME_2);
    EntityDraft draft = allEntityDrafts.retrieve(entity, USERNAME);
    assertNotNull(draft);
    assertEquals("DraftCls", draft.getClassName());
    assertEquals(USERNAME, draft.getDraftOwnerUsername());
    draft = allEntityDrafts.retrieve(entity, USERNAME_2);
    assertNotNull(draft);
    assertEquals("DraftCls", draft.getClassName());
    assertEquals(USERNAME_2, draft.getDraftOwnerUsername());
    assertNull(allEntityDrafts.retrieve(entity, "otherUser"));
    allEntityDrafts.deleteAll(entity);
    assertTrue(allEntityDrafts.retrieveAll(entity).isEmpty());
    assertNull(allEntityDrafts.retrieve(entity, USERNAME));
    assertNull(allEntityDrafts.retrieve(entity, USERNAME_2));
}
Also used : EntityDto(org.motechproject.mds.dto.EntityDto) Entity(org.motechproject.mds.domain.Entity) EntityDraft(org.motechproject.mds.domain.EntityDraft) Test(org.junit.Test)

Example 89 with EntityDto

use of org.motechproject.mds.dto.EntityDto 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 90 with EntityDto

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

the class EntityServiceContextIT method shouldRetrieveOnlyEntitiesUserHasAccessTo.

@Test
public void shouldRetrieveOnlyEntitiesUserHasAccessTo() throws IOException {
    entityService.createEntity(new EntityDto(null, null, SIMPLE_NAME, null, null, SecurityMode.EVERYONE, null));
    entityService.createEntity(new EntityDto(null, null, SIMPLE_NAME_2, null, null, SecurityMode.USERS, new HashSet<>(Arrays.asList("motech", "motech2"))));
    entityService.createEntity(new EntityDto(null, null, SIMPLE_NAME_3, null, null, SecurityMode.USERS, new HashSet<>(Arrays.asList("no_motech", "definitely_no_motech"))));
    List<EntityDto> result = entityService.listEntities(true);
    List<String> expected = asList(SIMPLE_NAME, SIMPLE_NAME_2);
    List<String> actual = extract(result, on(EntityDto.class).getName());
    Collections.sort(expected);
    Collections.sort(actual);
    assertEquals(expected, actual);
}
Also used : EntityDto(org.motechproject.mds.dto.EntityDto) HashSet(java.util.HashSet) Test(org.junit.Test)

Aggregations

EntityDto (org.motechproject.mds.dto.EntityDto)136 Test (org.junit.Test)61 FieldDto (org.motechproject.mds.dto.FieldDto)53 ArrayList (java.util.ArrayList)34 LookupFieldDto (org.motechproject.mds.dto.LookupFieldDto)32 MotechDataService (org.motechproject.mds.service.MotechDataService)26 LookupDto (org.motechproject.mds.dto.LookupDto)24 List (java.util.List)19 BasicEntityRecord (org.motechproject.mds.web.domain.BasicEntityRecord)12 Method (java.lang.reflect.Method)11 FieldTestHelper.lookupFieldDto (org.motechproject.mds.testutil.FieldTestHelper.lookupFieldDto)11 Arrays.asList (java.util.Arrays.asList)9 FieldBasicDto (org.motechproject.mds.dto.FieldBasicDto)9 EntityRecord (org.motechproject.mds.web.domain.EntityRecord)9 AdvancedSettingsDto (org.motechproject.mds.dto.AdvancedSettingsDto)7 TypeDto (org.motechproject.mds.dto.TypeDto)7 HashMap (java.util.HashMap)6 SchemaHolder (org.motechproject.mds.dto.SchemaHolder)6 BasicFieldRecord (org.motechproject.mds.web.domain.BasicFieldRecord)6 FieldRecord (org.motechproject.mds.web.domain.FieldRecord)6