use of org.motechproject.mds.dto.DraftData in project motech by motech.
the class EntityServiceContextIT method testRelatedFieldGenerationForManyToManyRelationship.
@Test
public void testRelatedFieldGenerationForManyToManyRelationship() {
EntityDto entityDto1 = new EntityDto();
entityDto1.setName("RelationTestEnt1");
entityDto1 = entityService.createEntity(entityDto1);
EntityDto entityDto2 = new EntityDto();
entityDto2.setName("RelationTestEnt2");
entityDto2 = entityService.createEntity(entityDto2);
EntityDto entityDto3 = new EntityDto();
entityDto3.setName("RelationTestEnt3");
entityDto3 = entityService.createEntity(entityDto3);
EntityDraft entityDraft1 = entityService.getEntityDraft(entityDto1.getId());
Set<Lookup> fieldLookups = new HashSet<>();
Field field = new Field(entityDraft1, "newField", "Display Name", fieldLookups);
Type type = allTypes.retrieveByClassName(TypeDto.MANY_TO_MANY_RELATIONSHIP.getTypeClass());
field.setType(type);
if (type.hasSettings()) {
for (TypeSetting setting : type.getSettings()) {
field.addSetting(new FieldSetting(field, setting));
}
}
FieldMetadata metadata = new FieldMetadata(field, RELATED_CLASS);
metadata.setValue(entityDto2.getClassName());
field.addMetadata(metadata);
metadata = new FieldMetadata(field, RELATIONSHIP_COLLECTION_TYPE);
metadata.setValue("java.util.Set");
field.addMetadata(metadata);
metadata = new FieldMetadata(field, RELATED_FIELD);
metadata.setValue("relatedField");
field.addMetadata(metadata);
metadata = new FieldMetadata(field, OWNING_SIDE);
metadata.setValue("true");
entityDraft1.addField(field);
allEntityDrafts.update(entityDraft1);
entityService.commitChanges(entityDto1.getId());
FieldDto relatedField = getField(entityDto2.getId(), "relatedField");
// Changing related class
entityDraft1 = entityService.getEntityDraft(entityDto1.getId());
DraftData draftData = DraftBuilder.forFieldEdit(entityDraft1.getField("newField").getId(), "metadata.0.value", entityDto3.getClassName());
entityService.saveDraftEntityChanges(entityDto1.getId(), draftData);
entityService.commitChanges(entityDto1.getId());
// We changed related entity, so the old related entity field must be removed
relatedField = getField(entityDto2.getId(), "relatedField");
assertNull(relatedField);
relatedField = getField(entityDto3.getId(), "relatedField");
assertRelatedField(entityDto1, relatedField, "java.util.Set");
entityDraft1 = entityService.getEntityDraft(entityDto1.getId());
draftData = DraftBuilder.forFieldEdit(entityDraft1.getField("newField").getId(), "metadata.1.value", "java.util.List");
entityService.saveDraftEntityChanges(entityDto1.getId(), draftData);
draftData = DraftBuilder.forFieldEdit(entityDraft1.getField("newField").getId(), "metadata.2.value", "newNameForRelatedField");
entityService.saveDraftEntityChanges(entityDto1.getId(), draftData);
entityService.commitChanges(entityDto1.getId());
relatedField = getField(entityDto3.getId(), "newNameForRelatedField");
assertRelatedField(entityDto1, relatedField, "java.util.List");
}
use of org.motechproject.mds.dto.DraftData in project motech by motech.
the class MdsBundleIT method verifyUniqueConstraint.
private void verifyUniqueConstraint() throws Exception {
Object instance = service.getClassType().newInstance();
PropertyUtils.setProperty(instance, "someString", "uniqueVal");
service.create(instance);
Object dupeInstance = service.getClassType().newInstance();
PropertyUtils.setProperty(dupeInstance, "someString", "uniqueVal");
// this should violate the unique constraint
boolean caught = false;
try {
service.create(dupeInstance);
} catch (RuntimeException e) {
caught = true;
}
assertTrue("Unique constraint had no effect!", caught);
// verify constraint removal
Long entityId = entityService.getEntityByClassName(FOO_CLASS).getId();
Long fieldId = getFieldIdByName(entityService.getFields(entityId), "someString");
DraftData draft = DraftBuilder.forFieldEdit(fieldId, "basic.unique", false);
entityService.saveDraftEntityChanges(entityId, draft);
entityService.commitChanges(entityId);
SchemaHolder schemaHolder = entityService.getSchema();
generator.regenerateMdsDataBundle(schemaHolder, true);
service = (MotechDataService) ServiceRetriever.getService(bundleContext, ClassName.getInterfaceName(FOO_CLASS), true);
// should succeed now, no exception is enough for us at this point
dupeInstance = service.getClassType().newInstance();
PropertyUtils.setProperty(dupeInstance, "someString", "uniqueVal");
service.create(dupeInstance);
}
use of org.motechproject.mds.dto.DraftData 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"));
}
use of org.motechproject.mds.dto.DraftData in project motech by motech.
the class EntityServiceImplTest method shouldNotAllowRemovingAFieldUsedInALookup.
@Test(expected = FieldUsedInLookupException.class)
public void shouldNotAllowRemovingAFieldUsedInALookup() {
when(field.getId()).thenReturn(456L);
when(lookup.getLookupName()).thenReturn("Lookup name");
when(field.getDisplayName()).thenReturn("Field Name");
when(lookup.getFields()).thenReturn(asList(field));
when(draft.getFields()).thenReturn(asList(field));
when(draft.getLookups()).thenReturn(asList(lookup));
when(draft.getId()).thenReturn(8L);
when(draft.getField(456L)).thenReturn(field);
when(allEntities.retrieveById(8L)).thenReturn(entity);
when(allEntityDrafts.retrieve(eq(entity), anyString())).thenReturn(draft);
DraftData dd = DraftBuilder.forFieldRemoval(456L);
entityService.saveDraftEntityChanges(8L, dd);
}
use of org.motechproject.mds.dto.DraftData 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");
}
Aggregations