Search in sources :

Example 21 with GeneratedFile

use of org.drools.codegen.common.GeneratedFile 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.drools.codegen.common.GeneratedFile) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 22 with GeneratedFile

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

the class SampleConfigGeneratorTest method generate.

@ParameterizedTest
@MethodSource("org.kie.kogito.codegen.api.utils.KogitoContextTestUtils#contextBuilders")
void generate(KogitoBuildContext.Builder contextBuilder) {
    KogitoBuildContext context = contextBuilder.build();
    SampleConfigGenerator sampleConfigGenerator = new SampleConfigGenerator(context);
    GeneratedFile generate = sampleConfigGenerator.generate();
    CompilationUnit compilationUnit = StaticJavaParser.parse(new String(generate.contents()));
    Optional<ClassOrInterfaceDeclaration> optionalClassDeclaration = compilationUnit.findFirst(ClassOrInterfaceDeclaration.class);
    assertThat(optionalClassDeclaration).isNotEmpty();
    ClassOrInterfaceDeclaration classDeclaration = optionalClassDeclaration.get();
    if (context.hasDI()) {
        assertThat(classDeclaration.getAnnotations()).isNotEmpty();
    } else {
        assertThat(classDeclaration.getAnnotations()).isEmpty();
    }
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) KogitoBuildContext(org.kie.kogito.codegen.api.context.KogitoBuildContext) GeneratedFile(org.drools.codegen.common.GeneratedFile) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 23 with GeneratedFile

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

the class SampleCodegen method generate.

@Override
public Collection<GeneratedFile> generate() {
    if (isEmpty()) {
        return Collections.emptyList();
    }
    TemplatedGenerator generator = TemplatedGenerator.builder().withFallbackContext(QuarkusKogitoBuildContext.CONTEXT_NAME).build(context(), "SampleRestResource");
    CompilationUnit compilationUnit = generator.compilationUnitOrThrow();
    if (context.hasDI()) {
        compilationUnit.findAll(FieldDeclaration.class, SampleCodegen::isSampleRuntimeField).forEach(fd -> context.getDependencyInjectionAnnotator().withInjection(fd));
    } else {
        compilationUnit.findAll(FieldDeclaration.class, SampleCodegen::isSampleRuntimeField).forEach(SampleCodegen::initializeSampleRuntimeField);
    }
    return context.hasRESTForGenerator(this) ? Collections.singleton(new GeneratedFile(REST_TYPE, generator.generatedFilePath(), compilationUnit.toString())) : Collections.emptyList();
}
Also used : TemplatedGenerator(org.kie.kogito.codegen.api.template.TemplatedGenerator) CompilationUnit(com.github.javaparser.ast.CompilationUnit) FieldDeclaration(com.github.javaparser.ast.body.FieldDeclaration) GeneratedFile(org.drools.codegen.common.GeneratedFile)

Example 24 with GeneratedFile

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

the class GenerateModelMojo method generateModel.

protected void generateModel() throws MojoExecutionException {
    setSystemProperties(properties);
    ApplicationGenerator appGen = ApplicationGeneratorDiscovery.discover(discoverKogitoRuntimeContext(projectClassLoader()));
    Collection<GeneratedFile> generatedFiles;
    if (generatePartial) {
        generatedFiles = appGen.generateComponents();
    } else {
        generatedFiles = appGen.generate();
    }
    writeGeneratedFiles(generatedFiles);
    if (!keepSources) {
        deleteDrlFiles();
    }
}
Also used : ApplicationGenerator(org.kie.kogito.codegen.core.ApplicationGenerator) GeneratedFile(org.drools.codegen.common.GeneratedFile)

Example 25 with GeneratedFile

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

the class JsonSchemaGeneratorTest method testJsonSchemaGeneratorInputOutput.

@Test
public void testJsonSchemaGeneratorInputOutput() throws IOException {
    Collection<GeneratedFile> files = new JsonSchemaGenerator.ClassBuilder(Stream.of(PersonInputOutputParams.class)).build().generate();
    assertEquals(1, files.size());
    GeneratedFile file = files.iterator().next();
    assertTaskSchema("InputOutput_test.json", file, SchemaVersion.DRAFT_2019_09, Arrays.asList("name", "address", "color"), List.of("age"));
}
Also used : GeneratedFile(org.drools.codegen.common.GeneratedFile) Test(org.junit.jupiter.api.Test)

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