Search in sources :

Example 6 with DraftData

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");
}
Also used : FieldSetting(org.motechproject.mds.domain.FieldSetting) EntityDto(org.motechproject.mds.dto.EntityDto) Field(org.motechproject.mds.domain.Field) Type(org.motechproject.mds.domain.Type) FieldMetadata(org.motechproject.mds.domain.FieldMetadata) TypeSetting(org.motechproject.mds.domain.TypeSetting) Lookup(org.motechproject.mds.domain.Lookup) DraftData(org.motechproject.mds.dto.DraftData) EntityDraft(org.motechproject.mds.domain.EntityDraft) HashSet(java.util.HashSet) LookupFieldDto(org.motechproject.mds.dto.LookupFieldDto) FieldDto(org.motechproject.mds.dto.FieldDto) Test(org.junit.Test)

Example 7 with DraftData

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);
}
Also used : DraftData(org.motechproject.mds.dto.DraftData) SchemaHolder(org.motechproject.mds.dto.SchemaHolder)

Example 8 with DraftData

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"));
}
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 9 with DraftData

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);
}
Also used : DraftData(org.motechproject.mds.dto.DraftData) Test(org.junit.Test)

Example 10 with DraftData

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

Aggregations

DraftData (org.motechproject.mds.dto.DraftData)16 Test (org.junit.Test)11 HashMap (java.util.HashMap)6 EntityDto (org.motechproject.mds.dto.EntityDto)5 FieldDto (org.motechproject.mds.dto.FieldDto)5 ObjectMapper (org.codehaus.jackson.map.ObjectMapper)4 EntityDraft (org.motechproject.mds.domain.EntityDraft)3 LookupFieldDto (org.motechproject.mds.dto.LookupFieldDto)3 SchemaHolder (org.motechproject.mds.dto.SchemaHolder)2 ArrayList (java.util.ArrayList)1 Arrays.asList (java.util.Arrays.asList)1 Collections.singletonList (java.util.Collections.singletonList)1 HashSet (java.util.HashSet)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 Matchers.anyString (org.mockito.Matchers.anyString)1 Field (org.motechproject.mds.domain.Field)1 FieldMetadata (org.motechproject.mds.domain.FieldMetadata)1 FieldSetting (org.motechproject.mds.domain.FieldSetting)1 Lookup (org.motechproject.mds.domain.Lookup)1