Search in sources :

Example 76 with FieldDto

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

the class EntityMetadataBuilderTest method shouldAddOneToManyRelationshipMetadata.

@Test
public void shouldAddOneToManyRelationshipMetadata() {
    FieldDto oneToManyField = fieldDto("oneToManyName", OneToManyRelationship.class);
    oneToManyField.addMetadata(new MetadataDto(RELATED_CLASS, "org.motechproject.test.MyClass"));
    FieldMetadata fmd = mock(FieldMetadata.class);
    CollectionMetadata collMd = mock(CollectionMetadata.class);
    when(entity.getName()).thenReturn(ENTITY_NAME);
    when(entity.getId()).thenReturn(2L);
    when(schemaHolder.getFields(entity)).thenReturn(singletonList(oneToManyField));
    when(entity.getTableName()).thenReturn(TABLE_NAME);
    when(jdoMetadata.newPackageMetadata(PACKAGE)).thenReturn(packageMetadata);
    when(packageMetadata.newClassMetadata(ENTITY_NAME)).thenReturn(classMetadata);
    when(classMetadata.newFieldMetadata("oneToManyName")).thenReturn(fmd);
    when(fmd.getCollectionMetadata()).thenReturn(collMd);
    when(fmd.getName()).thenReturn("oneToManyName");
    entityMetadataBuilder.addEntityMetadata(jdoMetadata, entity, Sample.class, schemaHolder);
    verifyCommonClassMetadata();
    verify(fmd).setDefaultFetchGroup(true);
    verify(collMd).setEmbeddedElement(false);
    verify(collMd).setSerializedElement(false);
    verify(collMd).setElementType("org.motechproject.test.MyClass");
}
Also used : CollectionMetadata(javax.jdo.metadata.CollectionMetadata) FieldMetadata(javax.jdo.metadata.FieldMetadata) MetadataDto(org.motechproject.mds.dto.MetadataDto) LookupFieldDto(org.motechproject.mds.dto.LookupFieldDto) FieldDto(org.motechproject.mds.dto.FieldDto) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 77 with FieldDto

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

the class EntityMetadataBuilderTest method shouldSetIndexOnMetadataLookupField.

@Test
public void shouldSetIndexOnMetadataLookupField() throws Exception {
    FieldDto lookupField = fieldDto("lookupField", String.class);
    LookupDto lookup = new LookupDto();
    lookup.setLookupName("A lookup");
    lookup.setLookupFields(singletonList(new LookupFieldDto("lookupField", LookupFieldType.VALUE)));
    lookup.setIndexRequired(true);
    lookupField.setLookups(singletonList(lookup));
    FieldMetadata fmd = mock(FieldMetadata.class);
    when(entity.getName()).thenReturn(ENTITY_NAME);
    when(entity.getId()).thenReturn(14L);
    when(schemaHolder.getFields(entity)).thenReturn(singletonList(lookupField));
    when(entity.getTableName()).thenReturn(TABLE_NAME);
    when(jdoMetadata.newPackageMetadata(PACKAGE)).thenReturn(packageMetadata);
    when(packageMetadata.newClassMetadata(ENTITY_NAME)).thenReturn(classMetadata);
    when(classMetadata.newFieldMetadata("lookupField")).thenReturn(fmd);
    when(fmd.newIndexMetadata()).thenReturn(indexMetadata);
    PowerMockito.mockStatic(FieldUtils.class);
    when(FieldUtils.getDeclaredField(eq(Sample.class), anyString(), eq(true))).thenReturn(Sample.class.getDeclaredField("notInDefFg"));
    entityMetadataBuilder.addEntityMetadata(jdoMetadata, entity, Sample.class, schemaHolder);
    verifyCommonClassMetadata();
    verify(fmd).newIndexMetadata();
    verify(indexMetadata).setName("lkp_idx_" + ENTITY_NAME + "_lookupField_14");
}
Also used : FieldMetadata(javax.jdo.metadata.FieldMetadata) AnotherSample(org.motechproject.mds.annotations.internal.samples.AnotherSample) Sample(org.motechproject.mds.builder.Sample) LookupDto(org.motechproject.mds.dto.LookupDto) LookupFieldDto(org.motechproject.mds.dto.LookupFieldDto) FieldDto(org.motechproject.mds.dto.FieldDto) LookupFieldDto(org.motechproject.mds.dto.LookupFieldDto) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 78 with FieldDto

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

the class EntityMetadataBuilderTest method shouldAddObjectValueGeneratorToAppropriateFields.

@Test
public void shouldAddObjectValueGeneratorToAppropriateFields() throws Exception {
    when(entity.getName()).thenReturn(ENTITY_NAME);
    when(entity.getTableName()).thenReturn(TABLE_NAME);
    when(jdoMetadata.newPackageMetadata(anyString())).thenReturn(packageMetadata);
    when(packageMetadata.newClassMetadata(anyString())).thenReturn(classMetadata);
    List<FieldDto> fields = new ArrayList<>();
    // for these fields the appropriate generator should be added
    fields.add(fieldDto(1L, CREATOR_FIELD_NAME, String.class.getName(), CREATOR_DISPLAY_FIELD_NAME, null));
    fields.add(fieldDto(2L, OWNER_FIELD_NAME, String.class.getName(), OWNER_DISPLAY_FIELD_NAME, null));
    fields.add(fieldDto(3L, CREATION_DATE_FIELD_NAME, DateTime.class.getName(), CREATION_DATE_DISPLAY_FIELD_NAME, null));
    fields.add(fieldDto(4L, MODIFIED_BY_FIELD_NAME, String.class.getName(), MODIFIED_BY_DISPLAY_FIELD_NAME, null));
    fields.add(fieldDto(5L, MODIFICATION_DATE_FIELD_NAME, DateTime.class.getName(), MODIFICATION_DATE_DISPLAY_FIELD_NAME, null));
    doReturn(fields).when(schemaHolder).getFields(CLASS_NAME);
    final List<FieldMetadata> list = new ArrayList<>();
    doAnswer(new Answer() {

        @Override
        public Object answer(InvocationOnMock invocation) throws Throwable {
            // we create a mock ...
            FieldMetadata metadata = mock(FieldMetadata.class);
            // ... and it should return correct name
            doReturn(invocation.getArguments()[0]).when(metadata).getName();
            // Because we want to check that appropriate methods was executed
            // we added metadata to list and later we will verify conditions
            list.add(metadata);
            // in the end we have to return the mock
            return metadata;
        }
    }).when(classMetadata).newFieldMetadata(anyString());
    PowerMockito.mockStatic(FieldUtils.class);
    when(FieldUtils.getDeclaredField(eq(Sample.class), anyString(), eq(true))).thenReturn(Sample.class.getDeclaredField("notInDefFg"));
    entityMetadataBuilder.addEntityMetadata(jdoMetadata, entity, Sample.class, schemaHolder);
    for (FieldMetadata metadata : list) {
        String name = metadata.getName();
        // the id field should not have set object value generator metadata
        int invocations = "id".equalsIgnoreCase(name) ? 0 : 1;
        verify(classMetadata).newFieldMetadata(name);
        verify(metadata, times(invocations)).setPersistenceModifier(PersistenceModifier.PERSISTENT);
        verify(metadata, times(invocations)).setDefaultFetchGroup(true);
        verify(metadata, times(invocations)).newExtensionMetadata(DATANUCLEUS, VALUE_GENERATOR, "ovg." + name);
    }
}
Also used : FieldMetadata(javax.jdo.metadata.FieldMetadata) AnotherSample(org.motechproject.mds.annotations.internal.samples.AnotherSample) Sample(org.motechproject.mds.builder.Sample) ArrayList(java.util.ArrayList) Matchers.anyString(org.mockito.Matchers.anyString) Mockito.doAnswer(org.mockito.Mockito.doAnswer) Answer(org.mockito.stubbing.Answer) InvocationOnMock(org.mockito.invocation.InvocationOnMock) LookupFieldDto(org.motechproject.mds.dto.LookupFieldDto) FieldDto(org.motechproject.mds.dto.FieldDto) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 79 with FieldDto

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

the class MdsRestFacadeImpl method getLookupFieldsMapping.

private Map<String, FieldDto> getLookupFieldsMapping(EntityInfo entity, LookupDto lookup) {
    Map<String, FieldDto> fieldMap = new HashMap<>();
    for (LookupFieldDto lookupField : lookup.getLookupFields()) {
        FieldDto field;
        if (StringUtils.isNotBlank(lookupField.getRelatedName())) {
            RelationshipHolder relHolder = new RelationshipHolder(entity.getField(lookupField.getName()).getField());
            EntityInfo relatedEntity = entityInfoReader.getEntityInfo(relHolder.getRelatedClass());
            field = relatedEntity.getField(lookupField.getRelatedName()).getField();
        } else {
            field = entity.getField(lookupField.getName()).getField();
        }
        fieldMap.put(lookupField.getLookupFieldName(), field);
    }
    return fieldMap;
}
Also used : RelationshipHolder(org.motechproject.mds.domain.RelationshipHolder) HashMap(java.util.HashMap) EntityInfo(org.motechproject.mds.entityinfo.EntityInfo) FieldDto(org.motechproject.mds.dto.FieldDto) LookupFieldDto(org.motechproject.mds.dto.LookupFieldDto) LookupFieldDto(org.motechproject.mds.dto.LookupFieldDto)

Example 80 with FieldDto

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

the class InstanceServiceImpl method convertToHistoryRecord.

private HistoryRecord convertToHistoryRecord(Object object, EntityDto entity, Long instanceId, MotechDataService service) {
    Long entityId = entity.getId();
    List<FieldDto> fields = getEntityFields(entityId);
    Map<String, List<FieldDto>> relatedEntitiesFields = getRelatedEntitiesFields(fields);
    EntityRecord entityRecord = instanceToRecord(object, entity, entityService.getEntityFieldsForUI(entityId), service, EntityType.HISTORY, relatedEntitiesFields);
    Long historyInstanceSchemaVersion = (Long) PropertyUtil.safeGetProperty(object, HistoryTrashClassHelper.historySchemaVersion(object.getClass()));
    Long currentSchemaVersion = entityService.getCurrentSchemaVersion(entity.getClassName());
    return new HistoryRecord(entityRecord.getId(), instanceId, historyInstanceSchemaVersion.equals(currentSchemaVersion), entityRecord.getFields());
}
Also used : EntityRecord(org.motechproject.mds.web.domain.EntityRecord) BasicEntityRecord(org.motechproject.mds.web.domain.BasicEntityRecord) ArrayList(java.util.ArrayList) List(java.util.List) BasicHistoryRecord(org.motechproject.mds.web.domain.BasicHistoryRecord) HistoryRecord(org.motechproject.mds.web.domain.HistoryRecord) FieldDto(org.motechproject.mds.dto.FieldDto)

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