use of org.motechproject.mds.dto.SchemaHolder in project motech by motech.
the class MdsBundleIT method verifyComboboxDataMigration.
private void verifyComboboxDataMigration() throws NoSuchFieldException, ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException {
Long entityId = entityService.getEntityByClassName(FOO_CLASS).getId();
Long fieldId = getFieldIdByName(entityService.getFields(entityId), "someEnum");
DraftData draft = DraftBuilder.forFieldEdit(fieldId, "settings.2.value", true);
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);
assertValuesEqual(getExpectedComboboxValues(), getValues(service.detachedCopyAll(service.retrieveAll())));
}
use of org.motechproject.mds.dto.SchemaHolder in project motech by motech.
the class MdsDataProviderBuilderImplTest method shouldGenerateJson.
@Test
public void shouldGenerateJson() {
String json = "{\n" + " \"name\": \"data-services\",\n" + " \"objects\": [ {\n" + " \"displayName\": \"TestEntity\",\n" + " \"type\": \"org.motechproject.TestEntity\",\n" + " \"lookupFields\": [ {\n" + " \"displayName\": \"TestLookupName\",\n" + " \"fields\": [\n" + " \"TestFieldName\" ]\n" + " }, {\n" + " \"displayName\": \"mds.dataprovider.byinstanceid\",\n" + " \"fields\": [\n" + " \"mds.dataprovider.instanceid\"\n" + " ]\n" + " }\n" + " ],\n" + " \"fields\": [\n" + " {\n" + " \"displayName\": \"TestFieldDisplayName\",\n" + " \"fieldKey\": \"TestFieldName\"\n" + " } ]\n" + " } ]\n" + "}\n";
EntityDto entity = new EntityDto();
entity.setId(Long.valueOf("1"));
entity.setName("TestEntity");
entity.setClassName("org.motechproject.TestEntity");
FieldDto field = new FieldDto();
FieldBasicDto fieldBasicDto = new FieldBasicDto();
fieldBasicDto.setName("TestFieldName");
fieldBasicDto.setDisplayName("TestFieldDisplayName");
field.setBasic(fieldBasicDto);
fieldList.add(field);
LookupDto lookup = new LookupDto();
lookup.setLookupName("TestLookupName");
List<LookupFieldDto> lookupFields = new LinkedList<>();
lookupFields.add(FieldTestHelper.lookupFieldDto("TestFieldName"));
lookup.setLookupFields(lookupFields);
lookupList.add(lookup);
AdvancedSettingsDto advancedSettings = new AdvancedSettingsDto();
advancedSettings.setIndexes(lookupList);
SchemaHolder schema = new SchemaHolder();
schema.addEntity(entity, advancedSettings, fieldList);
String generatedJson = mdsDataProviderBuilder.generateDataProvider(schema);
assertEquals(json, generatedJson.replace("\r\n", "\n"));
}
use of org.motechproject.mds.dto.SchemaHolder in project motech by motech.
the class MdsDataProviderBuilderImplTest method shouldGenerateEmptyJson.
@Test
public void shouldGenerateEmptyJson() {
String generatedJson = mdsDataProviderBuilder.generateDataProvider(new SchemaHolder());
assertEquals("", generatedJson);
}
use of org.motechproject.mds.dto.SchemaHolder in project motech by motech.
the class EntityServiceContextIT method shouldCreateEntity.
@Test
public void shouldCreateEntity() throws Exception {
// given
EntityDto entityDto = new EntityDto();
entityDto.setName(SIMPLE_NAME);
// when
entityService.createEntity(entityDto);
setProperty(monitor, "bundleStarted", true);
setProperty(monitor, "bundleInstalled", true);
setProperty(monitor, "contextInitialized", true);
SchemaHolder schemaHolder = entityService.getSchema();
jarGeneratorService.regenerateMdsDataBundle(schemaHolder);
// then
// 1. new entry in db should be added
String className = String.format("%s.%s", Constants.PackagesGenerated.ENTITY, "Test");
assertTrue(String.format("Not found %s in database", className), containsEntity(className));
// 2. there should be ability to create a new instance of created entity
Class<?> clazz = MDSClassLoader.getInstance().loadClass(className);
Object instance = clazz.newInstance();
assertNotNull(instance);
ClassLoader oldClassLoader = Thread.currentThread().getContextClassLoader();
try {
// We want to use the current factory for persisting so this hack is required
// Normally these classes live in a separate bundle
Thread.currentThread().setContextClassLoader(MDSClassLoader.getInstance());
getPersistenceManagerFactory().registerMetadata(metadataHolder.getJdoMetadata());
getPersistenceManager().makePersistent(instance);
List<?> list = cast(clazz, (Collection) getPersistenceManager().newQuery(clazz).execute());
assertNotNull(list);
assertFalse("The instance of entity should be saved in database", list.isEmpty());
} finally {
Thread.currentThread().setContextClassLoader(oldClassLoader);
}
}
use of org.motechproject.mds.dto.SchemaHolder in project motech by motech.
the class BaseInstanceIT method setUpEntity.
private void setUpEntity() throws Exception {
String entityClass = getEntityClassName();
entity = new EntityDto(entityClass);
entity.setRecordHistory(true);
entity = entityService.createEntity(entity);
entityService.addFields(entity, getEntityFields());
TrackingDto tracking = entityService.getAdvancedSettings(entity.getId(), true).getTracking();
tracking.setAllowCreateEvent(false);
tracking.setAllowUpdateEvent(false);
tracking.setAllowDeleteEvent(false);
entityService.updateTracking(entity.getId(), tracking);
SchemaHolder schemaHolder = entityService.getSchema();
mdsConstructor.constructEntities(schemaHolder);
PersistenceManagerFactory factory = getDataPersistenceManagerFactory();
if (null == factory.getMetadata(entityClass)) {
factory.registerMetadata(metadataHolder.getJdoMetadata());
}
CtClass ctClass = MotechClassPool.getDefault().get(getRepositoryClass());
MDSClassLoader.getInstance().safeDefineClass(getRepositoryClass(), ctClass.toBytecode());
ctClass = MotechClassPool.getDefault().get(getInterfaceClass());
MDSClassLoader.getInstance().safeDefineClass(getInterfaceClass(), ctClass.toBytecode());
ctClass = MotechClassPool.getDefault().get(getServiceClass());
MDSClassLoader.getInstance().safeDefineClass(getServiceClass(), ctClass.toBytecode());
}
Aggregations