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());
}
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);
}
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));
}
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"));
}
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);
}
Aggregations