Search in sources :

Example 6 with KogitoBuildContext

use of org.kie.kogito.codegen.api.context.KogitoBuildContext in project kogito-runtimes by kiegroup.

the class CustomDashboardGeneratedUtilsTest method getMappedJsons.

@ParameterizedTest
@MethodSource("org.kie.kogito.codegen.api.utils.KogitoContextTestUtils#contextBuilders")
void getMappedJsons(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>> retrieved = CustomDashboardGeneratedUtils.getMappedJsons(collectedResources);
    // 2 = valid *.json files inside src/test/META-INF/dashboards
    assertEquals(2, retrieved.size());
    assertEquals(1, retrieved.get(OPERATIONAL_DASHBOARD_PREFIX).size());
    assertEquals(1, retrieved.get(DOMAIN_DASHBOARD_PREFIX).size());
}
Also used : CollectedResource(org.kie.kogito.codegen.api.io.CollectedResource) KogitoBuildContext(org.kie.kogito.codegen.api.context.KogitoBuildContext) ArrayList(java.util.ArrayList) List(java.util.List) File(java.io.File) GeneratedFile(org.kie.kogito.codegen.api.GeneratedFile) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 7 with KogitoBuildContext

use of org.kie.kogito.codegen.api.context.KogitoBuildContext 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.kie.kogito.codegen.api.GeneratedFile) GeneratedFile(org.kie.kogito.codegen.api.GeneratedFile) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 8 with KogitoBuildContext

use of org.kie.kogito.codegen.api.context.KogitoBuildContext 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.kie.kogito.codegen.api.GeneratedFile) GeneratedFile(org.kie.kogito.codegen.api.GeneratedFile) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 9 with KogitoBuildContext

use of org.kie.kogito.codegen.api.context.KogitoBuildContext in project kogito-runtimes by kiegroup.

the class AbstractKogitoBuildContextTest method packageNameValidation.

@Test
public void packageNameValidation() {
    assertThat(builder.build().getPackageName()).isEqualTo(KogitoBuildContext.DEFAULT_PACKAGE_NAME);
    assertThatThrownBy(() -> builder.withPackageName(null)).isInstanceOf(NullPointerException.class);
    assertThatThrownBy(() -> builder.withPackageName("i.am.an-invalid.package-name.sorry")).isInstanceOf(IllegalArgumentException.class);
    KogitoBuildContext context = builder.withPackageName(AbstractKogitoBuildContext.DEFAULT_GROUP_ID).build();
    assertThat(context.getPackageName()).isNotEqualTo(AbstractKogitoBuildContext.DEFAULT_GROUP_ID);
}
Also used : KogitoBuildContext(org.kie.kogito.codegen.api.context.KogitoBuildContext) Test(org.junit.jupiter.api.Test)

Example 10 with KogitoBuildContext

use of org.kie.kogito.codegen.api.context.KogitoBuildContext 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) GeneratedFileType(org.kie.kogito.codegen.api.GeneratedFileType) 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) GeneratedFile(org.kie.kogito.codegen.api.GeneratedFile) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) 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.kie.kogito.codegen.api.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)

Aggregations

KogitoBuildContext (org.kie.kogito.codegen.api.context.KogitoBuildContext)83 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)53 MethodSource (org.junit.jupiter.params.provider.MethodSource)51 GeneratedFile (org.kie.kogito.codegen.api.GeneratedFile)41 Collection (java.util.Collection)20 Test (org.junit.jupiter.api.Test)19 List (java.util.List)17 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)17 QuarkusKogitoBuildContext (org.kie.kogito.codegen.api.context.impl.QuarkusKogitoBuildContext)17 ClassOrInterfaceDeclaration (com.github.javaparser.ast.body.ClassOrInterfaceDeclaration)16 CompilationUnit (com.github.javaparser.ast.CompilationUnit)15 Collectors (java.util.stream.Collectors)14 Collections (java.util.Collections)11 Optional (java.util.Optional)11 Paths (java.nio.file.Paths)10 Path (java.nio.file.Path)9 GeneratedFileType (org.kie.kogito.codegen.api.GeneratedFileType)9 SpringBootKogitoBuildContext (org.kie.kogito.codegen.api.context.impl.SpringBootKogitoBuildContext)9 CollectedResource (org.kie.kogito.codegen.api.io.CollectedResource)9 ReflectionMarshallerGenerator (org.kie.kogito.codegen.process.persistence.marshaller.ReflectionMarshallerGenerator)8