Search in sources :

Example 1 with GeneratedFile

use of org.drools.codegen.common.GeneratedFile in project kogito-runtimes by kiegroup.

the class DecisionModelResourcesProviderGeneratorTest method generateDecisionModelResourcesProvider.

@Test
public void generateDecisionModelResourcesProvider() {
    final KogitoBuildContext context = QuarkusKogitoBuildContext.builder().withAddonsConfig(AddonsConfig.builder().withTracing(true).build()).build();
    final Collection<CollectedResource> collectedResources = CollectedResourceProducer.fromPaths(Paths.get("src/test/resources/decision/models/vacationDays").toAbsolutePath(), Paths.get("src/test/resources/decision/models/vacationDaysAlt").toAbsolutePath());
    final long numberOfModels = collectedResources.stream().filter(r -> r.resource().getResourceType() == ResourceType.DMN).count();
    final DecisionCodegen codeGenerator = DecisionCodegen.ofCollectedResources(context, collectedResources);
    final Collection<GeneratedFile> generatedFiles = codeGenerator.generate();
    // the two resources below, see https://github.com/kiegroup/kogito-runtimes/commit/18ec525f530b1ff1bddcf18c0083f14f86aff171#diff-edd3a09d62dc627ee10fe37925944217R53
    assertThat(generatedFiles.size()).isGreaterThanOrEqualTo(2);
    // Align this FAI-215 test (#621) with unknown order of generated files (ie.: additional generated files might be present)
    // A Rest endpoint is always generated per model.
    List<GeneratedFile> generatedRESTFiles = generatedFiles.stream().filter(gf -> gf.type().equals(AbstractGenerator.REST_TYPE)).collect(toList());
    assertFalse(generatedRESTFiles.isEmpty());
    assertEquals(numberOfModels, generatedRESTFiles.size());
    List<GeneratedFile> generatedCLASSFile = generatedFiles.stream().filter(gf -> gf.type().equals(GeneratedFileType.SOURCE)).collect(toList());
    assertEquals(1, generatedCLASSFile.size());
    GeneratedFile classFile = generatedCLASSFile.get(0);
    assertEquals("org/kie/kogito/app/DecisionModelResourcesProvider.java", classFile.relativePath());
    final CompilationUnit compilationUnit = parse(new ByteArrayInputStream(classFile.contents()));
    final ClassOrInterfaceDeclaration classDeclaration = compilationUnit.findFirst(ClassOrInterfaceDeclaration.class).orElseThrow(() -> new NoSuchElementException("Compilation unit doesn't contain a class or interface declaration!"));
    assertNotNull(classDeclaration);
    final MethodDeclaration methodDeclaration = classDeclaration.findFirst(MethodDeclaration.class, d -> d.getName().getIdentifier().equals("getResources")).orElseThrow(() -> new NoSuchElementException("Class declaration doesn't contain a method named \"getResources\"!"));
    assertNotNull(methodDeclaration);
    assertTrue(methodDeclaration.getBody().isPresent());
    final BlockStmt body = methodDeclaration.getBody().get();
    assertTrue(body.getStatements().size() > 2);
    assertTrue(body.getStatements().get(1).isExpressionStmt());
    final ExpressionStmt expression = (ExpressionStmt) body.getStatements().get(1);
    assertTrue(expression.getExpression() instanceof MethodCallExpr);
    final MethodCallExpr call = (MethodCallExpr) expression.getExpression();
    assertEquals("add", call.getName().getIdentifier());
    assertTrue(call.getScope().isPresent());
    assertTrue(call.getScope().get().isNameExpr());
    final NameExpr nameExpr = call.getScope().get().asNameExpr();
    assertEquals("resourcePaths", nameExpr.getName().getIdentifier());
    long numberOfAddStms = body.getStatements().stream().filter(this::isAddStatement).count();
    assertEquals(numberOfModels, numberOfAddStms);
    List<NodeList<Expression>> defaultDecisionModelResources = body.getStatements().stream().filter(this::isAddStatement).map(stm -> stm.asExpressionStmt().getExpression().asMethodCallExpr().getArguments()).collect(toList());
    // verify .add(..) number of parameters
    defaultDecisionModelResources.forEach(nodeList -> assertEquals(1, nodeList.size()));
    Set<String> distinctDefaultDecisionModelResources = defaultDecisionModelResources.stream().map(nodeList -> nodeList.get(0).toString()).collect(toSet());
    assertEquals(defaultDecisionModelResources.size(), distinctDefaultDecisionModelResources.size());
}
Also used : Assertions.assertNotNull(org.junit.jupiter.api.Assertions.assertNotNull) ExpressionStmt(com.github.javaparser.ast.stmt.ExpressionStmt) StaticJavaParser.parse(com.github.javaparser.StaticJavaParser.parse) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) ResourceType(org.kie.api.io.ResourceType) ByteArrayInputStream(java.io.ByteArrayInputStream) Assertions.assertFalse(org.junit.jupiter.api.Assertions.assertFalse) CollectedResource(org.kie.kogito.codegen.api.io.CollectedResource) AbstractGenerator(org.kie.kogito.codegen.core.AbstractGenerator) Expression(com.github.javaparser.ast.expr.Expression) CompilationUnit(com.github.javaparser.ast.CompilationUnit) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) NoSuchElementException(java.util.NoSuchElementException) Collectors.toSet(java.util.stream.Collectors.toSet) AddonsConfig(org.kie.kogito.codegen.api.AddonsConfig) NodeList(com.github.javaparser.ast.NodeList) KogitoBuildContext(org.kie.kogito.codegen.api.context.KogitoBuildContext) Collection(java.util.Collection) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr) Set(java.util.Set) NameExpr(com.github.javaparser.ast.expr.NameExpr) Statement(com.github.javaparser.ast.stmt.Statement) Test(org.junit.jupiter.api.Test) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) QuarkusKogitoBuildContext(org.kie.kogito.codegen.api.context.impl.QuarkusKogitoBuildContext) Paths(java.nio.file.Paths) CollectedResourceProducer(org.kie.kogito.codegen.core.io.CollectedResourceProducer) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) GeneratedFile(org.drools.codegen.common.GeneratedFile) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) GeneratedFileType(org.drools.codegen.common.GeneratedFileType) CompilationUnit(com.github.javaparser.ast.CompilationUnit) CollectedResource(org.kie.kogito.codegen.api.io.CollectedResource) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) NodeList(com.github.javaparser.ast.NodeList) NameExpr(com.github.javaparser.ast.expr.NameExpr) GeneratedFile(org.drools.codegen.common.GeneratedFile) ExpressionStmt(com.github.javaparser.ast.stmt.ExpressionStmt) ByteArrayInputStream(java.io.ByteArrayInputStream) KogitoBuildContext(org.kie.kogito.codegen.api.context.KogitoBuildContext) QuarkusKogitoBuildContext(org.kie.kogito.codegen.api.context.impl.QuarkusKogitoBuildContext) NoSuchElementException(java.util.NoSuchElementException) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr) Test(org.junit.jupiter.api.Test)

Example 2 with GeneratedFile

use of org.drools.codegen.common.GeneratedFile in project kogito-runtimes by kiegroup.

the class CustomDashboardGeneratedUtilsTest method addToGeneratedFiles.

@ParameterizedTest
@MethodSource("org.kie.kogito.codegen.api.utils.KogitoContextTestUtils#contextBuilders")
void addToGeneratedFiles(KogitoBuildContext.Builder contextBuilder) {
    final KogitoBuildContext context = contextBuilder.withAppPaths(AppPaths.fromTestDir(new File(".").toPath())).build();
    Collection<CollectedResource> collectedResources = CollectedResourceProducer.fromPaths(context.getAppPaths().getPaths());
    Map<String, List<Resource>> dashboardJsonsMap = CustomDashboardGeneratedUtils.getMappedJsons(collectedResources);
    Collection<GeneratedFile> toPopulate = new ArrayList<>();
    CustomDashboardGeneratedUtils.addToGeneratedFiles(dashboardJsonsMap.get(OPERATIONAL_DASHBOARD_PREFIX), toPopulate, operationalFunction, OPERATIONAL_DASHBOARD_PREFIX);
    assertEquals(dashboardJsonsMap.get(OPERATIONAL_DASHBOARD_PREFIX).size(), toPopulate.size());
    String sourcePath = dashboardJsonsMap.get(OPERATIONAL_DASHBOARD_PREFIX).get(0).getSourcePath();
    String originalFileName = sourcePath.substring(sourcePath.lastIndexOf(File.separator) + 1);
    validateGeneratedFile(toPopulate.iterator().next(), OPERATIONAL_DASHBOARD_PREFIX, originalFileName);
    toPopulate = new ArrayList<>();
    CustomDashboardGeneratedUtils.addToGeneratedFiles(dashboardJsonsMap.get(DOMAIN_DASHBOARD_PREFIX), toPopulate, domainFunction, DOMAIN_DASHBOARD_PREFIX);
    assertEquals(dashboardJsonsMap.get(DOMAIN_DASHBOARD_PREFIX).size(), toPopulate.size());
    sourcePath = dashboardJsonsMap.get(DOMAIN_DASHBOARD_PREFIX).get(0).getSourcePath();
    originalFileName = sourcePath.substring(sourcePath.lastIndexOf(File.separator) + 1);
    validateGeneratedFile(toPopulate.iterator().next(), DOMAIN_DASHBOARD_PREFIX, originalFileName);
}
Also used : CollectedResource(org.kie.kogito.codegen.api.io.CollectedResource) ArrayList(java.util.ArrayList) KogitoBuildContext(org.kie.kogito.codegen.api.context.KogitoBuildContext) ArrayList(java.util.ArrayList) List(java.util.List) File(java.io.File) GeneratedFile(org.drools.codegen.common.GeneratedFile) GeneratedFile(org.drools.codegen.common.GeneratedFile) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 3 with GeneratedFile

use of org.drools.codegen.common.GeneratedFile in project kogito-runtimes by kiegroup.

the class CustomDashboardGeneratedUtilsTest method loadCustomGrafanaDashboardsList.

@ParameterizedTest
@MethodSource("org.kie.kogito.codegen.api.utils.KogitoContextTestUtils#contextBuilders")
void loadCustomGrafanaDashboardsList(KogitoBuildContext.Builder contextBuilder) {
    final KogitoBuildContext context = contextBuilder.withAppPaths(AppPaths.fromTestDir(new File(".").toPath())).build();
    Collection<GeneratedFile> retrieved = CustomDashboardGeneratedUtils.loadCustomGrafanaDashboardsList(context);
    assertNotNull(retrieved);
    // 2 = valid *.json files inside src/test/META-INF/dashboards
    assertEquals(2, retrieved.size());
}
Also used : KogitoBuildContext(org.kie.kogito.codegen.api.context.KogitoBuildContext) File(java.io.File) GeneratedFile(org.drools.codegen.common.GeneratedFile) GeneratedFile(org.drools.codegen.common.GeneratedFile) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 4 with GeneratedFile

use of org.drools.codegen.common.GeneratedFile in project kogito-runtimes by kiegroup.

the class GeneratedFileValidationTest method validateGeneratedFileTypes.

@Test
public void validateGeneratedFileTypes() {
    List<GeneratedFile> generatedFiles = Arrays.asList(new GeneratedFile(GeneratedFileType.SOURCE, "myPath1", ""), new GeneratedFile(GeneratedFileType.INTERNAL_RESOURCE, "myPath2", ""), new GeneratedFile(GeneratedFileType.COMPILED_CLASS, "myPath3", ""), new GeneratedFile(GeneratedFileType.STATIC_HTTP_RESOURCE, "myPath4", ""));
    GeneratedFileValidation.validateGeneratedFileTypes(generatedFiles, Arrays.asList(GeneratedFileType.Category.SOURCE, GeneratedFileType.Category.INTERNAL_RESOURCE, GeneratedFileType.Category.STATIC_HTTP_RESOURCE, GeneratedFileType.Category.COMPILED_CLASS));
    Set<GeneratedFileType.Category> categories = Collections.singleton(GeneratedFileType.Category.SOURCE);
    Assertions.assertThatThrownBy(() -> GeneratedFileValidation.validateGeneratedFileTypes(generatedFiles, categories)).isInstanceOf(IllegalStateException.class).hasMessageContaining("myPath2").hasMessageContaining("myPath3").hasMessageContaining("myPath4");
}
Also used : GeneratedFile(org.drools.codegen.common.GeneratedFile) Test(org.junit.jupiter.api.Test)

Example 5 with GeneratedFile

use of org.drools.codegen.common.GeneratedFile in project kogito-runtimes by kiegroup.

the class PredictionCodegenTest method generateAllFiles.

private static void generateAllFiles(KogitoBuildContext context, PredictionCodegen codeGenerator, int expectedTotalFiles, int expectedJavaSources, int expectedRestEndpoints, boolean assertReflect) {
    Collection<GeneratedFile> generatedFiles = codeGenerator.generate();
    int expectedGeneratedFilesSize = expectedTotalFiles - (context.hasRESTForGenerator(codeGenerator) ? 0 : expectedRestEndpoints * 2);
    assertEquals(expectedGeneratedFilesSize, generatedFiles.size());
    assertEquals(expectedJavaSources, generatedFiles.stream().filter(generatedFile -> generatedFile.category().equals(GeneratedFileType.Category.SOURCE) && generatedFile.type().name().equals(PMML) && generatedFile.relativePath().endsWith(".java")).count());
    int expectedReflectResource = assertReflect ? 1 : 0;
    assertEquals(expectedReflectResource, generatedFiles.stream().filter(generatedFile -> generatedFile.category().equals(GeneratedFileType.Category.INTERNAL_RESOURCE) && generatedFile.type().name().equals(GeneratedFileType.INTERNAL_RESOURCE.name()) && generatedFile.relativePath().endsWith(REFLECT_JSON)).count());
    assertEndpoints(context, generatedFiles, expectedRestEndpoints, codeGenerator);
    Optional<ApplicationSection> optionalApplicationSection = codeGenerator.section();
    assertTrue(optionalApplicationSection.isPresent());
    CompilationUnit compilationUnit = optionalApplicationSection.get().compilationUnit();
    assertNotNull(compilationUnit);
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) ApplicationSection(org.kie.kogito.codegen.api.ApplicationSection) GeneratedFile(org.drools.codegen.common.GeneratedFile)

Aggregations

GeneratedFile (org.drools.codegen.common.GeneratedFile)79 KogitoBuildContext (org.kie.kogito.codegen.api.context.KogitoBuildContext)39 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)36 MethodSource (org.junit.jupiter.params.provider.MethodSource)36 CompilationUnit (com.github.javaparser.ast.CompilationUnit)21 Collection (java.util.Collection)20 ArrayList (java.util.ArrayList)19 List (java.util.List)19 ClassOrInterfaceDeclaration (com.github.javaparser.ast.body.ClassOrInterfaceDeclaration)15 Collectors (java.util.stream.Collectors)15 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)15 File (java.io.File)13 Test (org.junit.jupiter.api.Test)12 Paths (java.nio.file.Paths)11 Collections (java.util.Collections)10 GeneratedFileType (org.drools.codegen.common.GeneratedFileType)10 CollectedResourceProducer (org.kie.kogito.codegen.core.io.CollectedResourceProducer)10 Path (java.nio.file.Path)9 Optional (java.util.Optional)9 CollectedResource (org.kie.kogito.codegen.api.io.CollectedResource)8