Search in sources :

Example 1 with REST_TYPE

use of org.kie.kogito.codegen.api.Generator.REST_TYPE 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)

Aggregations

StaticJavaParser (com.github.javaparser.StaticJavaParser)1 CompilationUnit (com.github.javaparser.ast.CompilationUnit)1 FieldDeclaration (com.github.javaparser.ast.body.FieldDeclaration)1 Arrays (java.util.Arrays)1 Collection (java.util.Collection)1 Collections (java.util.Collections)1 List (java.util.List)1 Optional (java.util.Optional)1 Collectors (java.util.stream.Collectors)1 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)1 Test (org.junit.jupiter.api.Test)1 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)1 MethodSource (org.junit.jupiter.params.provider.MethodSource)1 GeneratedFile (org.kie.kogito.codegen.api.GeneratedFile)1 REST_TYPE (org.kie.kogito.codegen.api.Generator.REST_TYPE)1 KogitoBuildContext (org.kie.kogito.codegen.api.context.KogitoBuildContext)1 JavaKogitoBuildContext (org.kie.kogito.codegen.api.context.impl.JavaKogitoBuildContext)1 CollectedResource (org.kie.kogito.codegen.api.io.CollectedResource)1 CollectedResourcesTestUtils (org.kie.kogito.codegen.api.utils.CollectedResourcesTestUtils)1