use of org.kie.workbench.common.services.datamodeller.core.Parameter 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.core.Parameter in project kie-wb-common by kiegroup.
the class JavaRoasterModelDriverTest method createMethodsUpdateTest.
private DataObject createMethodsUpdateTest() {
DataObject dataObject = createDataObject("org.kie.workbench.common.services.datamodeller.driver.package4", "MethodsUpdateTest", null);
Type methodReturnType = new TypeImpl(List.class.getName(), Arrays.asList(new TypeImpl(String.class.getName())));
Parameter parameter = new ParameterImpl(new TypeImpl(List.class.getName(), Arrays.asList(new TypeImpl("org.kie.workbench.common.services.datamodeller.driver.package4.MethodsUpdateTest"))), "methodUpdateTestList");
Method methodImpl = new MethodImpl("getTestString", Arrays.asList(parameter), "return Arrays.asList(\"testString\");", methodReturnType, Visibility.PUBLIC);
Annotation testAnnotation = createAnnotation(TestAnnotation.class);
methodImpl.addAnnotation(testAnnotation);
dataObject.addMethod(methodImpl);
methodImpl = new MethodImpl("noOpMethodWithTestAnnotation", Collections.EMPTY_LIST, "", new TypeImpl(void.class.getName()), Visibility.PUBLIC);
Annotation testAnnotation1 = createAnnotation(Generated.class);
testAnnotation1.setValue("value", "foo.bar.Generator");
methodImpl.addAnnotation(testAnnotation1);
Annotation testAnnotation2 = createAnnotation(TestAnnotation1.class);
testAnnotation2.setValue("value", "annotationParameter");
methodImpl.addAnnotation(testAnnotation2);
dataObject.addMethod(methodImpl);
return dataObject;
}
use of org.kie.workbench.common.services.datamodeller.core.Parameter in project kie-wb-common by kiegroup.
the class GenerationEngineTest method testMethodStringGeneration.
@Test
public void testMethodStringGeneration() {
DataModel dataModel = dataModelOracleDriver.createModel();
Parameter parameter1 = new ParameterImpl(new TypeImpl("com.test.Object1"), "o1");
Parameter parameter2 = new ParameterImpl(new TypeImpl("com.test.Object1"), "o2");
Type returnType = new TypeImpl("com.test.Object1");
Method method = new MethodImpl("test", Arrays.asList(parameter1, parameter2), "return o1;", returnType, Visibility.PUBLIC);
DataObject object = dataModel.addDataObject("com.test.Object1");
object.addMethod(method);
GenerationContext generationContext = new GenerationContext(dataModel);
try {
String result = engine.generateMethodString(generationContext, method);
assertEquals(results.getProperty("testMethodStringGeneration"), results.getProperty("testMethodStringGeneration"), result);
} catch (Exception e) {
e.printStackTrace();
}
}
use of org.kie.workbench.common.services.datamodeller.core.Parameter in project kie-wb-common by kiegroup.
the class GenerationTools method buildMethodParameterString.
public String buildMethodParameterString(List<Parameter> methodParameters) {
if (methodParameters == null || methodParameters.isEmpty()) {
return null;
}
StringBuilder builder = new StringBuilder();
Iterator<Parameter> iterator = methodParameters.iterator();
while (iterator.hasNext()) {
Parameter parameter = iterator.next();
Type parameterType = parameter.getType();
builder.append(parameterType.getName());
buildTypeArgumentsString(parameter.getType().getTypeArguments(), builder);
builder.append(" ");
builder.append(parameter.getName());
if (iterator.hasNext()) {
builder.append(",");
}
}
return builder.toString();
}
use of org.kie.workbench.common.services.datamodeller.core.Parameter in project kie-wb-common by kiegroup.
the class JavaRoasterModelDriverTest method createMethodsUpdateTestResult.
private DataObject createMethodsUpdateTestResult() {
DataObject dataObject = createDataObject("org.kie.workbench.common.services.datamodeller.driver.package4", "MethodsUpdateTestResult", null);
Type methodReturnType = new TypeImpl(List.class.getName(), Arrays.asList(new TypeImpl(Integer.class.getName())));
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");
Method methodImpl = new MethodImpl("getTestString", Arrays.asList(parameter1, parameter2), "return Arrays.asList(1);", methodReturnType, Visibility.PUBLIC);
Annotation testAnnotation = createAnnotation(TestAnnotation.class);
methodImpl.addAnnotation(testAnnotation);
dataObject.addMethod(methodImpl);
methodImpl = new MethodImpl("noOpMethodWithTestAnnotationUpdated", Collections.EMPTY_LIST, "return 1;", new TypeImpl(Integer.class.getName()), Visibility.PUBLIC);
Annotation testAnnotation1 = createAnnotation(Generated.class);
testAnnotation1.setValue("value", "foo.bar.Generator");
methodImpl.addAnnotation(testAnnotation1);
Annotation testAnnotation2 = createAnnotation(TestAnnotation1.class);
testAnnotation2.setValue("value", "annotationParameterUpdated");
methodImpl.addAnnotation(testAnnotation2);
dataObject.addMethod(methodImpl);
return dataObject;
}
Aggregations