use of org.kie.workbench.common.services.datamodeller.driver.impl.JavaRoasterModelDriver in project kie-wb-common by kiegroup.
the class JavaRoasterModelDriverTest method modelReadTest.
@Test
public void modelReadTest() {
try {
final SourceFilter pojo1Filter = javaType -> false;
final NestedClassFilter nestedClassFilter = javaType -> javaType.isClass() && javaType.getAnnotation(Generated.class) != null;
final MethodFilter methodFilter = method -> !method.isConstructor() && method.getAnnotation(Generated.class) != null;
FilterHolder filterHolder = mock(FilterHolder.class);
when(filterHolder.getSourceFilters()).thenReturn(Collections.singleton(pojo1Filter));
when(filterHolder.getNestedClassFilters()).thenReturn(Collections.singleton(nestedClassFilter));
when(filterHolder.getMethodFilters()).thenReturn(Collections.singleton(methodFilter));
JavaRoasterModelDriver javaRoasterModelDriver = new JavaRoasterModelDriver(ioService, rootPath, getClass().getClassLoader(), filterHolder);
ModelDriverResult modelDriverResult = javaRoasterModelDriver.loadModel();
DataModel dataModelOriginal = createModel();
assertNotNull(modelDriverResult);
assertNotNull(modelDriverResult.getDataModel());
assertEquals(dataModelOriginal.getDataObjects().size(), modelDriverResult.getDataModel().getDataObjects().size());
for (DataObject dataObject : dataModelOriginal.getDataObjects()) {
DataModelerAssert.assertEqualsDataObject(dataObject, modelDriverResult.getDataModel().getDataObject(dataObject.getClassName()));
}
for (JavaEnum javaEnum : dataModelOriginal.getJavaEnums()) {
DataModelerAssert.assertEqualsJavaEnum(javaEnum, modelDriverResult.getDataModel().getJavaEnum(javaEnum.getClassName()));
}
org.uberfire.backend.vfs.Path errorFile1 = Paths.convert(rootPath.resolve(ERROR_FILE1));
org.uberfire.backend.vfs.Path errorFile2 = Paths.convert(rootPath.resolve(ERROR_FILE2));
verifyErrorFilesWereDetected(modelDriverResult, errorFile1, errorFile2);
} catch (Exception e) {
e.printStackTrace();
fail("Test failed: " + e.getMessage());
}
}
use of org.kie.workbench.common.services.datamodeller.driver.impl.JavaRoasterModelDriver in project kie-wb-common by kiegroup.
the class JavaRoasterModelDriverTest method importsUpdateTest.
@Test
public void importsUpdateTest() throws Exception {
Path importsUpdateTestFilePath = rootPath.resolve("package6").resolve("ImportsUpdateTest.java");
String source = ioService.readAllString(importsUpdateTestFilePath);
JavaClassSource importsUpdateTestJavaClassSource = (JavaClassSource) Roaster.parse(source);
ClassLoader classLoader = getClass().getClassLoader();
ClassTypeResolver classTypeResolver = DriverUtils.createClassTypeResolver(importsUpdateTestJavaClassSource, classLoader);
final SourceFilter pojo1Filter = javaType -> false;
final NestedClassFilter nestedClassFilter = javaType -> javaType.isClass() && javaType.getAnnotation(Generated.class) != null;
final MethodFilter methodFilter = method -> !method.isConstructor() && method.getAnnotation(Generated.class) != null;
FilterHolder filterHolder = mock(FilterHolder.class);
when(filterHolder.getSourceFilters()).thenReturn(Collections.singleton(pojo1Filter));
when(filterHolder.getNestedClassFilters()).thenReturn(Collections.singleton(nestedClassFilter));
when(filterHolder.getMethodFilters()).thenReturn(Collections.singleton(methodFilter));
JavaRoasterModelDriver javaRoasterModelDriver = new JavaRoasterModelDriver(ioService, rootPath, classLoader, filterHolder);
ModelDriverResult result = javaRoasterModelDriver.loadDataObject(source, importsUpdateTestFilePath);
DataObject importsUpdateTest = result.getDataModel().getDataObject("org.kie.workbench.common.services.datamodeller.driver.package6.ImportsUpdateTest");
importsUpdateTest.addImport(new ImportImpl(List.class.getName()));
importsUpdateTest.addImport(new ImportImpl(ArrayList.class.getName()));
importsUpdateTest.setName("ImportsUpdateTestResult");
Path expectedFilePath = rootPath.resolve("package6").resolve("ImportsUpdateTestResult.java");
String expectedSource = ioService.readAllString(expectedFilePath);
ModelDriverResult expectedResult = javaRoasterModelDriver.loadDataObject(expectedSource, expectedFilePath);
DataObject importsUpdateTestResult = expectedResult.getDataModel().getDataObject("org.kie.workbench.common.services.datamodeller.driver.package6.ImportsUpdateTestResult");
DataModelerAssert.assertEqualsDataObject(importsUpdateTestResult, importsUpdateTest);
DataModelerAssert.assertEqualsImports(importsUpdateTestResult.getImports(), importsUpdateTest.getImports());
javaRoasterModelDriver.updateSource(importsUpdateTestJavaClassSource, importsUpdateTest, new UpdateInfo(), classTypeResolver);
ModelDriverResult updatedResult = javaRoasterModelDriver.loadDataObject(importsUpdateTestJavaClassSource.toString(), importsUpdateTestFilePath);
DataModelerAssert.assertEqualsDataObject(importsUpdateTestResult, updatedResult.getDataModel().getDataObject("org.kie.workbench.common.services.datamodeller.driver.package6.ImportsUpdateTestResult"));
DataModelerAssert.assertEqualsImports(importsUpdateTestResult.getImports(), updatedResult.getDataModel().getDataObject("org.kie.workbench.common.services.datamodeller.driver.package6.ImportsUpdateTestResult").getImports());
}
use of org.kie.workbench.common.services.datamodeller.driver.impl.JavaRoasterModelDriver in project kie-wb-common by kiegroup.
the class JavaRoasterModelDriverTest method loadDataObjectWithFailures.
@Test
public void loadDataObjectWithFailures() throws ModelDriverException {
Path errorFile1 = rootPath.resolve(ERROR_FILE1);
Path errorFile2 = rootPath.resolve(ERROR_FILE2);
String source = ioService.readAllString(errorFile1);
JavaRoasterModelDriver javaRoasterModelDriver = new JavaRoasterModelDriver(ioService, rootPath, getClass().getClassLoader(), mock(FilterHolder.class));
ModelDriverResult result = javaRoasterModelDriver.loadDataObject(source, errorFile1);
assertTrue(result.hasErrors());
verifyErrorFilesWereDetected(result, Paths.convert(errorFile1));
source = ioService.readAllString(errorFile2);
result = javaRoasterModelDriver.loadDataObject(source, errorFile2);
assertTrue(result.hasErrors());
verifyErrorFilesWereDetected(result, Paths.convert(errorFile2));
}
use of org.kie.workbench.common.services.datamodeller.driver.impl.JavaRoasterModelDriver in project kie-wb-common by kiegroup.
the class JavaRoasterModelDriverTest method methodUpdateTest.
@Test
public void methodUpdateTest() {
try {
// First read the MethodsUpdateTest
Path methodsUpdateTestFilePath = rootPath.resolve("package4").resolve("MethodsUpdateTest.java");
String source = ioService.readAllString(methodsUpdateTestFilePath);
JavaClassSource methodsUpdateTestJavaClassSource = (JavaClassSource) Roaster.parse(source);
ClassLoader classLoader = getClass().getClassLoader();
ClassTypeResolver classTypeResolver = DriverUtils.createClassTypeResolver(methodsUpdateTestJavaClassSource, classLoader);
final SourceFilter pojo1Filter = javaType -> false;
final NestedClassFilter nestedClassFilter = javaType -> javaType.isClass() && javaType.getAnnotation(Generated.class) != null;
final MethodFilter methodFilter = method -> !method.isConstructor() && method.getAnnotation(Generated.class) != null;
FilterHolder filterHolder = mock(FilterHolder.class);
when(filterHolder.getSourceFilters()).thenReturn(Collections.singleton(pojo1Filter));
when(filterHolder.getNestedClassFilters()).thenReturn(Collections.singleton(nestedClassFilter));
when(filterHolder.getMethodFilters()).thenReturn(Collections.singleton(methodFilter));
JavaRoasterModelDriver javaRoasterModelDriver = new JavaRoasterModelDriver(ioService, rootPath, classLoader, filterHolder);
ModelDriverResult result = javaRoasterModelDriver.loadDataObject(source, methodsUpdateTestFilePath);
// 1) read the MethodsUpdateTest
DataObject methodsUpdateTest = result.getDataModel().getDataObject("org.kie.workbench.common.services.datamodeller.driver.package4.MethodsUpdateTest");
// 2) modify the MethodsUpdateTest according to the expected result.
Method getTestStringMethod = methodsUpdateTest.getMethod("getTestString", Arrays.asList(List.class.getName()));
Type methodReturnType = new TypeImpl(List.class.getName(), Arrays.asList(new TypeImpl(Integer.class.getName())));
// List<Integer>
getTestStringMethod.setReturnType(methodReturnType);
Type parameterType1 = new TypeImpl(List.class.getName(), Arrays.asList(new TypeImpl(List.class.getName(), Arrays.asList(new TypeImpl("org.kie.workbench.common.services.datamodeller.driver.package4.MethodsUpdateTestResult")))));
Parameter parameter1 = new ParameterImpl(parameterType1, "methodUpdateTestResultList");
Type parameterType2 = new TypeImpl(int.class.getName());
Parameter parameter2 = new ParameterImpl(parameterType2, "intParameter");
getTestStringMethod.getParameters().clear();
// ( List<List<MethodsUpdateTestResult>> methodUpdateTestResultList, int intParameter )
getTestStringMethod.setParameters(Arrays.asList(parameter1, parameter2));
getTestStringMethod.setBody("return Arrays.asList(1);");
Method noOpMethodWithTestAnnotation = methodsUpdateTest.getMethod("noOpMethodWithTestAnnotation", Collections.EMPTY_LIST);
noOpMethodWithTestAnnotation.setName("noOpMethodWithTestAnnotationUpdated");
noOpMethodWithTestAnnotation.setBody("return 1;");
noOpMethodWithTestAnnotation.setReturnType(new TypeImpl(Integer.class.getName()));
// @TestAnnotation1("annotationParameterUpdated")
Annotation testAnnotation = noOpMethodWithTestAnnotation.getAnnotation(TestAnnotation1.class.getName());
testAnnotation.setValue("value", "annotationParameterUpdated");
methodsUpdateTest.setName("MethodsUpdateTestResult");
// 3) compare the modified data object with the expected data object.
Path expectedFilePath = rootPath.resolve("package4").resolve("MethodsUpdateTestResult.java");
String expectedSource = ioService.readAllString(expectedFilePath);
JavaClassSource expectedJavaClassSource = (JavaClassSource) Roaster.parse(expectedSource);
ModelDriverResult expectedResult = javaRoasterModelDriver.loadDataObject(expectedSource, expectedFilePath);
DataObject methodsUpdateTestResult = expectedResult.getDataModel().getDataObject("org.kie.workbench.common.services.datamodeller.driver.package4.MethodsUpdateTestResult");
// First check, the modified data object in memory should be the same as the readed from the model.
DataModelerAssert.assertEqualsDataObject(methodsUpdateTestResult, methodsUpdateTest);
// Second check, update the JavaClassSource corresponding to the MethodsUpdateTest
javaRoasterModelDriver.updateSource(methodsUpdateTestJavaClassSource, methodsUpdateTest, new UpdateInfo(), classTypeResolver);
ModelDriverResult updatedResult = javaRoasterModelDriver.loadDataObject(methodsUpdateTestJavaClassSource.toString(), methodsUpdateTestFilePath);
// and now compare the updatedResult with the expected value.
DataModelerAssert.assertEqualsDataObject(methodsUpdateTestResult, updatedResult.getDataModel().getDataObject("org.kie.workbench.common.services.datamodeller.driver.package4.MethodsUpdateTestResult"));
logger.debug(methodsUpdateTestJavaClassSource.toString());
} catch (Exception e) {
e.printStackTrace();
fail("Test failed: " + e.getMessage());
}
}
use of org.kie.workbench.common.services.datamodeller.driver.impl.JavaRoasterModelDriver in project kie-wb-common by kiegroup.
the class DataModelerServiceImpl method loadDataObject.
public GenerationResult loadDataObject(final Path modulePath, final String source, final Path sourcePath) {
if (logger.isDebugEnabled()) {
logger.debug("Loading data object from modulePath: " + modulePath.toURI());
}
KieModule module;
DataObject dataObject = null;
try {
module = moduleService.resolveModule(modulePath);
if (module == null) {
return new GenerationResult(null, null, new ArrayList<DataModelerError>());
}
ClassLoader classLoader = classLoaderHelper.getModuleClassLoader(module);
JavaRoasterModelDriver modelDriver = new JavaRoasterModelDriver(ioService, null, classLoader, filterHolder);
ModelDriverResult driverResult = modelDriver.loadDataObject(source, Paths.convert(sourcePath));
if (!driverResult.hasErrors()) {
if (driverResult.getDataModel().getDataObjects().size() > 0) {
dataObject = driverResult.getDataModel().getDataObjects().iterator().next();
}
return new GenerationResult(source, dataObject, new ArrayList<DataModelerError>());
} else {
return new GenerationResult(source, null, serviceHelper.toDataModelerError(driverResult.getErrors()));
}
} catch (Exception e) {
logger.error("Data object couldn't be loaded, path: " + modulePath + ", modulePath: " + modulePath + ".", e);
throw new ServiceException("Data object couldn't be loaded, path: " + modulePath + ", modulePath: " + modulePath + ".", e);
}
}
Aggregations