Search in sources :

Example 11 with CollectedResource

use of org.kie.kogito.codegen.api.io.CollectedResource in project kogito-runtimes by kiegroup.

the class ProcessGenerationIT method testProcessGeneration.

@ParameterizedTest
@MethodSource("processesProvider")
public void testProcessGeneration(String processFile) throws Exception {
    // for some tests this needs to be set to true
    System.setProperty("jbpm.enable.multi.con", "true");
    ProcessCodegen processCodeGen = ProcessCodegen.ofCollectedResources(newContext(), Collections.singletonList(new CollectedResource(BASE_PATH, new FileSystemResource(new File(BASE_PATH.toString(), processFile)))));
    RuleFlowProcess expected = (RuleFlowProcess) processCodeGen.processes().iterator().next();
    Application app = generateCodeProcessesOnly(processFile);
    AbstractProcess<? extends Model> process = (AbstractProcess<? extends Model>) app.get(Processes.class).processById(expected.getId());
    assertThat(process).isNotNull().isSameAs(app.get(Processes.class).processById(expected.getId()));
    RuleFlowProcess current = (RuleFlowProcess) process.get();
    assertNotNull(current);
    assertEquals(expected.getId(), current.getId(), "Id");
    assertEquals(expected.getName(), current.getName(), "Name");
    assertEquals(expected.getPackageName(), current.getPackageName(), "PackageName");
    assertEquals(expected.getVisibility(), current.getVisibility(), "Visibility");
    assertEquals(expected.getType(), current.getType(), "Type");
    assertEquals(expected.isAutoComplete(), current.isAutoComplete(), "AutoComplete");
    assertEquals(expected.isDynamic(), current.isDynamic(), "Dynamic");
    if (expected.getVersion() != null) {
        assertEquals(expected.getVersion(), current.getVersion());
    } else {
        assertEquals("1.0", current.getVersion());
    }
    assertEquals(expected.getImports(), current.getImports(), "Imports");
    assertEquals(expected.getFunctionImports(), current.getFunctionImports(), "FunctionImports");
    assertMetadata(expected.getMetaData(), current.getMetaData(), IGNORED_PROCESS_META);
    assertNodes(expected.getNodes(), current.getNodes());
    System.clearProperty("jbpm.enable.multi.con");
}
Also used : CollectedResource(org.kie.kogito.codegen.api.io.CollectedResource) RuleFlowProcess(org.jbpm.ruleflow.core.RuleFlowProcess) AbstractProcess(org.kie.kogito.process.impl.AbstractProcess) Model(org.kie.kogito.Model) FileSystemResource(org.drools.core.io.impl.FileSystemResource) File(java.io.File) Application(org.kie.kogito.Application) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 12 with CollectedResource

use of org.kie.kogito.codegen.api.io.CollectedResource in project kogito-runtimes by kiegroup.

the class SampleCodegenTest method generate.

@ParameterizedTest
@MethodSource("org.kie.kogito.codegen.api.utils.KogitoContextTestUtils#contextBuilders")
void generate(KogitoBuildContext.Builder contextBuilder) {
    KogitoBuildContext context = contextBuilder.build();
    Collection<CollectedResource> resources = Arrays.asList(CollectedResourcesTestUtils.toCollectedResource("/sampleFile1.txt"), CollectedResourcesTestUtils.toCollectedResource("/sampleFile2.txt"));
    SampleCodegen codeGenerator = SampleCodegen.ofCollectedResources(context, resources);
    Collection<GeneratedFile> generatedFiles = codeGenerator.generate();
    if (contextBuilder.build().hasRESTForGenerator(codeGenerator)) {
        assertThat(generatedFiles).hasSize(1);
        List<GeneratedFile> generatedRests = generatedFiles.stream().filter(gf -> gf.type() == REST_TYPE).collect(Collectors.toList());
        assertThat(generatedRests).hasSize(1);
        CompilationUnit compilationUnit = StaticJavaParser.parse(new String(generatedRests.get(0).contents()));
        Optional<FieldDeclaration> optionalFieldDeclaration = compilationUnit.findFirst(FieldDeclaration.class, SampleCodegen::isSampleRuntimeField);
        assertThat(optionalFieldDeclaration).isNotEmpty();
        FieldDeclaration fieldDeclaration = optionalFieldDeclaration.get();
        if (context.hasDI()) {
            assertThat(fieldDeclaration.getAnnotations()).isNotEmpty();
        } else {
            assertThat(fieldDeclaration.getVariable(0).getInitializer()).isNotEmpty();
        }
    } else {
        assertThat(generatedFiles).isEmpty();
    }
}
Also used : Arrays(java.util.Arrays) KogitoBuildContext(org.kie.kogito.codegen.api.context.KogitoBuildContext) JavaKogitoBuildContext(org.kie.kogito.codegen.api.context.impl.JavaKogitoBuildContext) Collection(java.util.Collection) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Collectors(java.util.stream.Collectors) Test(org.junit.jupiter.api.Test) List(java.util.List) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) StaticJavaParser(com.github.javaparser.StaticJavaParser) FieldDeclaration(com.github.javaparser.ast.body.FieldDeclaration) REST_TYPE(org.kie.kogito.codegen.api.Generator.REST_TYPE) CollectedResource(org.kie.kogito.codegen.api.io.CollectedResource) Optional(java.util.Optional) GeneratedFile(org.kie.kogito.codegen.api.GeneratedFile) CollectedResourcesTestUtils(org.kie.kogito.codegen.api.utils.CollectedResourcesTestUtils) CompilationUnit(com.github.javaparser.ast.CompilationUnit) Collections(java.util.Collections) MethodSource(org.junit.jupiter.params.provider.MethodSource) CompilationUnit(com.github.javaparser.ast.CompilationUnit) CollectedResource(org.kie.kogito.codegen.api.io.CollectedResource) KogitoBuildContext(org.kie.kogito.codegen.api.context.KogitoBuildContext) JavaKogitoBuildContext(org.kie.kogito.codegen.api.context.impl.JavaKogitoBuildContext) GeneratedFile(org.kie.kogito.codegen.api.GeneratedFile) FieldDeclaration(com.github.javaparser.ast.body.FieldDeclaration) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 13 with CollectedResource

use of org.kie.kogito.codegen.api.io.CollectedResource in project kogito-runtimes by kiegroup.

the class SampleCodegenTest method isEmpty.

@ParameterizedTest
@MethodSource("org.kie.kogito.codegen.api.utils.KogitoContextTestUtils#contextBuilders")
public void isEmpty(KogitoBuildContext.Builder contextBuilder) {
    KogitoBuildContext context = contextBuilder.build();
    SampleCodegen emptyCodeGenerator = SampleCodegen.ofCollectedResources(context, Collections.emptyList());
    assertThat(emptyCodeGenerator.isEmpty()).isTrue();
    assertThat(emptyCodeGenerator.isEnabled()).isFalse();
    Collection<GeneratedFile> emptyGeneratedFiles = emptyCodeGenerator.generate();
    assertThat(emptyGeneratedFiles.size()).isEqualTo(0);
    Collection<CollectedResource> resources = Arrays.asList(CollectedResourcesTestUtils.toCollectedResource("/sampleFile1.txt"), CollectedResourcesTestUtils.toCollectedResource("/sampleFile2.txt"));
    SampleCodegen codeGenerator = SampleCodegen.ofCollectedResources(context, resources);
    assertThat(codeGenerator.isEmpty()).isFalse();
    assertThat(codeGenerator.isEnabled()).isTrue();
    Collection<GeneratedFile> generatedFiles = codeGenerator.generate();
    int minNumberOfResources = context.hasRESTForGenerator(codeGenerator) ? 1 : 0;
    assertThat(generatedFiles.size()).isGreaterThanOrEqualTo(minNumberOfResources);
}
Also used : CollectedResource(org.kie.kogito.codegen.api.io.CollectedResource) KogitoBuildContext(org.kie.kogito.codegen.api.context.KogitoBuildContext) JavaKogitoBuildContext(org.kie.kogito.codegen.api.context.impl.JavaKogitoBuildContext) GeneratedFile(org.kie.kogito.codegen.api.GeneratedFile) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 14 with CollectedResource

use of org.kie.kogito.codegen.api.io.CollectedResource in project kogito-runtimes by kiegroup.

the class CodegenSvgTest method testWithoutSVGAddonNoRenamedAndMoved.

@ParameterizedTest
@MethodSource("org.kie.kogito.codegen.api.utils.KogitoContextTestUtils#contextBuilders")
public void testWithoutSVGAddonNoRenamedAndMoved(KogitoBuildContext.Builder contextBuilder) {
    KogitoBuildContext context = contextBuilder.withAddonsConfig(AddonsConfig.builder().withProcessSVG(false).build()).build();
    Collection<CollectedResource> collectedResources = CollectedResourceProducer.fromPaths(BASE_PATH);
    assertThat(collectedResources.stream().anyMatch(f -> f.resource().getSourcePath().endsWith(TEST_PROCESS_SOURCE))).isTrue();
    assertThat(collectedResources.stream().anyMatch(f -> f.resource().getSourcePath().endsWith(TEST_PROCESS_GENERATED_SVG_SOURCE))).isTrue();
    ProcessCodegen codeGenerator = ProcessCodegen.ofCollectedResources(context, collectedResources);
    Collection<GeneratedFile> generatedFiles = codeGenerator.generate();
    assertThat(generatedFiles).isNotEmpty();
    List<GeneratedFile> resources = generatedFiles.stream().filter(generatedFile -> generatedFile.type().equals(GeneratedFileType.INTERNAL_RESOURCE)).collect(Collectors.toList());
    assertThat(resources.size()).isZero();
}
Also used : AddonsConfig(org.kie.kogito.codegen.api.AddonsConfig) ProcessCodegen(org.kie.kogito.codegen.process.ProcessCodegen) KogitoBuildContext(org.kie.kogito.codegen.api.context.KogitoBuildContext) GeneratedFileType(org.kie.kogito.codegen.api.GeneratedFileType) Collection(java.util.Collection) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Collectors(java.util.stream.Collectors) Test(org.junit.jupiter.api.Test) List(java.util.List) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) Paths(java.nio.file.Paths) CollectedResourceProducer(org.kie.kogito.codegen.core.io.CollectedResourceProducer) CollectedResource(org.kie.kogito.codegen.api.io.CollectedResource) GeneratedFile(org.kie.kogito.codegen.api.GeneratedFile) Path(java.nio.file.Path) MethodSource(org.junit.jupiter.params.provider.MethodSource) CollectedResource(org.kie.kogito.codegen.api.io.CollectedResource) KogitoBuildContext(org.kie.kogito.codegen.api.context.KogitoBuildContext) ProcessCodegen(org.kie.kogito.codegen.process.ProcessCodegen) GeneratedFile(org.kie.kogito.codegen.api.GeneratedFile) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 15 with CollectedResource

use of org.kie.kogito.codegen.api.io.CollectedResource in project kogito-runtimes by kiegroup.

the class CodegenSvgTest method testExportedSVGRenamedAndMoved.

@ParameterizedTest
@MethodSource("org.kie.kogito.codegen.api.utils.KogitoContextTestUtils#contextBuilders")
public void testExportedSVGRenamedAndMoved(KogitoBuildContext.Builder contextBuilder) {
    KogitoBuildContext context = contextBuilder.withAddonsConfig(AddonsConfig.builder().withProcessSVG(true).build()).build();
    Collection<CollectedResource> collectedResources = CollectedResourceProducer.fromPaths(BASE_PATH);
    assertThat(collectedResources.stream().anyMatch(f -> f.resource().getSourcePath().endsWith(TEST_PROCESS_SOURCE))).isTrue();
    assertThat(collectedResources.stream().anyMatch(f -> f.resource().getSourcePath().endsWith(TEST_PROCESS_GENERATED_SVG_SOURCE))).isTrue();
    assertThat(collectedResources.stream().anyMatch(f -> f.resource().getSourcePath().endsWith(String.format(ProcessCodegen.SVG_EXPORT_NAME_EXPRESION, "hiring")))).isTrue();
    ProcessCodegen codeGenerator = ProcessCodegen.ofCollectedResources(context, collectedResources);
    Collection<GeneratedFile> generatedFiles = codeGenerator.generate();
    assertThat(generatedFiles).isNotEmpty();
    List<GeneratedFile> resources = generatedFiles.stream().filter(generatedFile -> generatedFile.type().equals(GeneratedFileType.INTERNAL_RESOURCE)).collect(Collectors.toList());
    assertThat(resources.size()).isEqualTo(1);
    assertThat("META-INF/processSVG/" + TEST_PROCESS_ID + ".svg").isEqualTo(resources.get(0).relativePath());
}
Also used : AddonsConfig(org.kie.kogito.codegen.api.AddonsConfig) ProcessCodegen(org.kie.kogito.codegen.process.ProcessCodegen) KogitoBuildContext(org.kie.kogito.codegen.api.context.KogitoBuildContext) GeneratedFileType(org.kie.kogito.codegen.api.GeneratedFileType) Collection(java.util.Collection) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Collectors(java.util.stream.Collectors) Test(org.junit.jupiter.api.Test) List(java.util.List) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) Paths(java.nio.file.Paths) CollectedResourceProducer(org.kie.kogito.codegen.core.io.CollectedResourceProducer) CollectedResource(org.kie.kogito.codegen.api.io.CollectedResource) GeneratedFile(org.kie.kogito.codegen.api.GeneratedFile) Path(java.nio.file.Path) MethodSource(org.junit.jupiter.params.provider.MethodSource) CollectedResource(org.kie.kogito.codegen.api.io.CollectedResource) KogitoBuildContext(org.kie.kogito.codegen.api.context.KogitoBuildContext) ProcessCodegen(org.kie.kogito.codegen.process.ProcessCodegen) GeneratedFile(org.kie.kogito.codegen.api.GeneratedFile) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Aggregations

CollectedResource (org.kie.kogito.codegen.api.io.CollectedResource)15 KogitoBuildContext (org.kie.kogito.codegen.api.context.KogitoBuildContext)12 GeneratedFile (org.kie.kogito.codegen.api.GeneratedFile)10 List (java.util.List)9 Collection (java.util.Collection)8 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)8 ArrayList (java.util.ArrayList)7 MethodSource (org.junit.jupiter.params.provider.MethodSource)7 GeneratedFileType (org.kie.kogito.codegen.api.GeneratedFileType)5 File (java.io.File)4 Path (java.nio.file.Path)4 Paths (java.nio.file.Paths)4 Optional (java.util.Optional)4 Collectors (java.util.stream.Collectors)4 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)4 Test (org.junit.jupiter.api.Test)4 CollectedResourceProducer (org.kie.kogito.codegen.core.io.CollectedResourceProducer)4 CompilationUnit (com.github.javaparser.ast.CompilationUnit)3 Collectors.toList (java.util.stream.Collectors.toList)3 AbstractGenerator (org.kie.kogito.codegen.core.AbstractGenerator)3