Search in sources :

Example 21 with MetadataDto

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

the class EntityBuilderTest method shouldBuildRelationshipFields.

@Test
public void shouldBuildRelationshipFields() throws Exception {
    FieldDto oneToOneField = fieldDto("oto", OneToOneRelationship.class);
    oneToOneField.setReadOnly(true);
    oneToOneField.addMetadata(new MetadataDto(Constants.MetadataKeys.RELATED_CLASS, RelatedClass.class.getName()));
    FieldDto oneToManyField = fieldDto("otm", OneToManyRelationship.class);
    oneToManyField.setReadOnly(true);
    oneToManyField.addMetadata(new MetadataDto(Constants.MetadataKeys.RELATED_CLASS, RelatedClass.class.getName()));
    fields.addAll(asList(oneToOneField, oneToManyField));
    when(entity.getClassName()).thenReturn(EntBuilderTestClass.class.getName());
    ClassData classData = entityBuilder.buildDDE(entity, fields, bundle);
    Class<?> builtClass = MDSClassLoader.getStandaloneInstance().defineClass(classData.getClassName(), classData.getBytecode());
    assertField(builtClass, "oto", RelatedClass.class);
    assertField(builtClass, "otm", List.class);
    assertGenericType(builtClass, "otm", List.class, RelatedClass.class);
}
Also used : EntBuilderTestClass(org.motechproject.mds.testutil.EntBuilderTestClass) ClassData(org.motechproject.mds.domain.ClassData) MetadataDto(org.motechproject.mds.dto.MetadataDto) FieldDto(org.motechproject.mds.dto.FieldDto) Test(org.junit.Test)

Example 22 with MetadataDto

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

the class EntityBuilderTest method shouldBuildAnEntityWithFields.

@Test
public void shouldBuildAnEntityWithFields() throws Exception {
    FieldDto enumListField = fieldDto("enumList", List.class);
    enumListField.addMetadata(new MetadataDto(Constants.MetadataKeys.ENUM_CLASS_NAME, FieldEnum.class.getName()));
    enumListField.getType().setDisplayName("mds.field.combobox");
    enumListField.addSetting(new SettingDto(Constants.Settings.ALLOW_MULTIPLE_SELECTIONS, "true"));
    fields.addAll(asList(fieldDto("count", Integer.class), fieldDto("time", Time.class), fieldDto("str", String.class), fieldDto("dec", Double.class), fieldDto("bool", Boolean.class), fieldDto("date", Date.class), fieldDto("dt", DateTime.class), fieldDto("ld", LocalDate.class), fieldDto("locale", Locale.class), enumListField, fieldDto("CapitalizedName", String.class), fieldDto("jd", java.time.LocalDate.class), fieldDto("jdt", LocalDateTime.class), fieldDto("blob", Byte[].class.getName())));
    Class<?> clazz = buildClass();
    assertNotNull(clazz);
    assertField(clazz, "count", Integer.class);
    assertField(clazz, "time", Time.class);
    assertField(clazz, "str", String.class);
    assertField(clazz, "dec", Double.class);
    assertField(clazz, "bool", Boolean.class);
    assertField(clazz, "date", Date.class);
    assertField(clazz, "dt", DateTime.class);
    assertField(clazz, "ld", LocalDate.class);
    assertField(clazz, "locale", Locale.class);
    assertField(clazz, "jd", java.time.LocalDate.class);
    assertField(clazz, "jdt", LocalDateTime.class);
    assertField(clazz, "blob", Byte[].class);
    // should use uncapitalized version
    assertField(clazz, "capitalizedName", String.class);
}
Also used : SettingDto(org.motechproject.mds.dto.SettingDto) MetadataDto(org.motechproject.mds.dto.MetadataDto) FieldDto(org.motechproject.mds.dto.FieldDto) Test(org.junit.Test)

Example 23 with MetadataDto

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

the class EntityBuilderTest method shouldBuildEnumListFieldProperly.

@Test
public void shouldBuildEnumListFieldProperly() throws Exception {
    FieldDto enumListField = fieldDto("enumList", Collection.class);
    enumListField.addMetadata(new MetadataDto(Constants.MetadataKeys.ENUM_CLASS_NAME, FieldEnum.class.getName()));
    enumListField.addSetting(new SettingDto(Constants.Settings.ALLOW_MULTIPLE_SELECTIONS, "true"));
    fields.add(enumListField);
    Class<?> clazz = buildClass();
    assertNotNull(clazz);
    assertField(clazz, "enumList", List.class);
    // verify that getters and setters have correct generic signatures
    Method setter = clazz.getDeclaredMethod("setEnumList", List.class);
    assertNotNull(setter);
    ParameterizedType pType = (ParameterizedType) setter.getGenericParameterTypes()[0];
    assertEquals(FieldEnum.class, pType.getActualTypeArguments()[0]);
    Method getter = clazz.getDeclaredMethod("getEnumList");
    assertNotNull(getter);
    pType = (ParameterizedType) getter.getGenericReturnType();
    assertEquals(FieldEnum.class, pType.getActualTypeArguments()[0]);
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) SettingDto(org.motechproject.mds.dto.SettingDto) Method(java.lang.reflect.Method) MetadataDto(org.motechproject.mds.dto.MetadataDto) FieldDto(org.motechproject.mds.dto.FieldDto) Test(org.junit.Test)

Example 24 with MetadataDto

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

the class EntityMetadataBuilderTest method shouldAddOneToOneRelationshipMetadata.

@Test
public void shouldAddOneToOneRelationshipMetadata() throws NotFoundException, CannotCompileException {
    final String relClassName = "org.motechproject.test.MyClass";
    final String relFieldName = "myField";
    FieldDto oneToOneField = fieldDto("oneToOneName", OneToOneRelationship.class);
    oneToOneField.addMetadata(new MetadataDto(RELATED_CLASS, relClassName));
    oneToOneField.addMetadata(new MetadataDto(RELATED_FIELD, relFieldName));
    FieldMetadata fmd = mock(FieldMetadata.class);
    when(fmd.getName()).thenReturn("oneToOneName");
    ForeignKeyMetadata fkmd = mock(ForeignKeyMetadata.class);
    when(entity.getName()).thenReturn(ENTITY_NAME);
    when(entity.getId()).thenReturn(3L);
    when(schemaHolder.getFields(entity)).thenReturn(singletonList(oneToOneField));
    when(entity.getTableName()).thenReturn(TABLE_NAME);
    when(jdoMetadata.newPackageMetadata(PACKAGE)).thenReturn(packageMetadata);
    when(packageMetadata.newClassMetadata(ENTITY_NAME)).thenReturn(classMetadata);
    when(classMetadata.newFieldMetadata("oneToOneName")).thenReturn(fmd);
    when(fmd.newForeignKeyMetadata()).thenReturn(fkmd);
    /* We simulate configuration for the bi-directional relationship (the related class has got
           a field that links back to the main class) */
    CtClass myClass = mock(CtClass.class);
    CtClass relatedClass = mock(CtClass.class);
    CtField myField = mock(CtField.class);
    CtField relatedField = mock(CtField.class);
    when(myClass.getName()).thenReturn(relClassName);
    when(myClass.getDeclaredFields()).thenReturn(new CtField[] { myField });
    when(myField.getType()).thenReturn(relatedClass);
    when(myField.getName()).thenReturn(relFieldName);
    when(relatedClass.getDeclaredFields()).thenReturn(new CtField[] { relatedField });
    when(relatedClass.getName()).thenReturn(CLASS_NAME);
    entityMetadataBuilder.addEntityMetadata(jdoMetadata, entity, Sample.class, schemaHolder);
    verifyCommonClassMetadata();
    verify(fmd).setDefaultFetchGroup(true);
    verify(fmd).setPersistenceModifier(PersistenceModifier.PERSISTENT);
    verify(fkmd).setName("fk_Sample_oneToOneName_3");
}
Also used : CtClass(javassist.CtClass) FieldMetadata(javax.jdo.metadata.FieldMetadata) ForeignKeyMetadata(javax.jdo.metadata.ForeignKeyMetadata) CtField(javassist.CtField) Matchers.anyString(org.mockito.Matchers.anyString) 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 25 with MetadataDto

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

the class EntityMetadataBuilderImpl method setMapMetadata.

private FieldMetadata setMapMetadata(ClassMetadata cmd, EntityDto entity, FieldDto field, Class<?> definition, EntityType entityType) {
    FieldMetadata fmd = cmd.newFieldMetadata(getNameForMetadata(field));
    MetadataDto keyMetadata = field.getMetadata(MAP_KEY_TYPE);
    MetadataDto valueMetadata = field.getMetadata(MAP_VALUE_TYPE);
    boolean serialized = shouldSerializeMap(keyMetadata, valueMetadata);
    // Depending on the types of key and value of the map we either serialize the map or create a separate table for it
    fmd.setSerialized(serialized);
    addDefaultFetchGroupMetadata(fmd, definition);
    MapMetadata mmd = fmd.newMapMetadata();
    if (serialized) {
        mmd.setSerializedKey(true);
        mmd.setSerializedValue(true);
    } else {
        mmd.setKeyType(keyMetadata.getValue());
        mmd.setValueType(valueMetadata.getValue());
        fmd.setTable(ClassTableName.getTableName(cmd.getTable(), getNameForMetadata(field)));
        JoinMetadata jmd = fmd.newJoinMetadata();
        ForeignKeyMetadata fkmd = getOrCreateFkMetadata(jmd);
        fkmd.setDeleteAction(ForeignKeyAction.CASCADE);
        fkmd.setName(KeyNames.mapForeignKeyName(entity.getName(), entity.getId(), field.getBasic().getName(), entityType));
    }
    return fmd;
}
Also used : FieldMetadata(javax.jdo.metadata.FieldMetadata) ForeignKeyMetadata(javax.jdo.metadata.ForeignKeyMetadata) JoinMetadata(javax.jdo.metadata.JoinMetadata) MetadataDto(org.motechproject.mds.dto.MetadataDto) MapMetadata(javax.jdo.metadata.MapMetadata)

Aggregations

MetadataDto (org.motechproject.mds.dto.MetadataDto)28 FieldDto (org.motechproject.mds.dto.FieldDto)22 Test (org.junit.Test)9 FieldBasicDto (org.motechproject.mds.dto.FieldBasicDto)8 TypeDto (org.motechproject.mds.dto.TypeDto)7 ArrayList (java.util.ArrayList)6 SettingDto (org.motechproject.mds.dto.SettingDto)6 LookupFieldDto (org.motechproject.mds.dto.LookupFieldDto)5 LinkedList (java.util.LinkedList)3 FieldMetadata (javax.jdo.metadata.FieldMetadata)3 EntityDto (org.motechproject.mds.dto.EntityDto)3 FieldValidationDto (org.motechproject.mds.dto.FieldValidationDto)3 LookupDto (org.motechproject.mds.dto.LookupDto)3 Method (java.lang.reflect.Method)2 List (java.util.List)2 ForeignKeyMetadata (javax.jdo.metadata.ForeignKeyMetadata)2 ObjectMapper (org.codehaus.jackson.map.ObjectMapper)2 ClassData (org.motechproject.mds.domain.ClassData)2 ValidationCriterionDto (org.motechproject.mds.dto.ValidationCriterionDto)2 ResultActions (org.springframework.test.web.server.ResultActions)2