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