Search in sources :

Example 1 with ProtoGenerator

use of org.kie.kogito.codegen.process.persistence.proto.ProtoGenerator in project kogito-runtimes by kiegroup.

the class AbstractPersistenceGeneratorTest method persistenceGeneratorSanityCheck.

@ParameterizedTest
@MethodSource("persistenceTestContexts")
void persistenceGeneratorSanityCheck(KogitoBuildContext context) {
    context.setApplicationProperty(KOGITO_PERSISTENCE_TYPE, persistenceType());
    ReflectionProtoGenerator protoGenerator = ReflectionProtoGenerator.builder().build(Collections.singleton(GeneratedPOJO.class));
    PersistenceGenerator persistenceGenerator = new PersistenceGenerator(context, protoGenerator, new ReflectionMarshallerGenerator(context, null));
    Collection<GeneratedFile> generatedFiles = persistenceGenerator.generate();
    int expectedDataIndexProto = hasDataIndexProto(context) ? 2 : 0;
    int expectedListDataIndexProto = hasDataIndexProto(context) ? 1 : 0;
    assertThat(generatedFiles.stream().filter(gf -> gf.type().equals(ProtoGenerator.PROTO_TYPE)).count()).isEqualTo(expectedDataIndexProto);
    assertThat(generatedFiles.stream().filter(gf -> gf.type().equals(ProtoGenerator.PROTO_TYPE) && gf.relativePath().endsWith(".json")).count()).isEqualTo(expectedListDataIndexProto);
    int expectedProtoMarshaller = hasProtoMarshaller(context) ? 10 : 0;
    assertThat(generatedFiles.stream().filter(gf -> gf.type().equals(GeneratedFileType.SOURCE) && gf.relativePath().endsWith("Marshaller.java"))).hasSize(expectedProtoMarshaller);
}
Also used : AddonsConfig(org.kie.kogito.codegen.api.AddonsConfig) ReflectionProtoGenerator(org.kie.kogito.codegen.process.persistence.proto.ReflectionProtoGenerator) KogitoBuildContext(org.kie.kogito.codegen.api.context.KogitoBuildContext) GeneratedFileType(org.kie.kogito.codegen.api.GeneratedFileType) KogitoContextTestUtils.contextBuilders(org.kie.kogito.codegen.api.utils.KogitoContextTestUtils.contextBuilders) PersistenceGenerator.hasDataIndexProto(org.kie.kogito.codegen.process.persistence.PersistenceGenerator.hasDataIndexProto) Collection(java.util.Collection) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) ReflectionMarshallerGenerator(org.kie.kogito.codegen.process.persistence.marshaller.ReflectionMarshallerGenerator) Arguments(org.junit.jupiter.params.provider.Arguments) GeneratedPOJO(org.kie.kogito.codegen.data.GeneratedPOJO) KOGITO_PERSISTENCE_DATA_INDEX_PROTO_GENERATION(org.kie.kogito.codegen.process.persistence.PersistenceGenerator.KOGITO_PERSISTENCE_DATA_INDEX_PROTO_GENERATION) ProtoGenerator(org.kie.kogito.codegen.process.persistence.proto.ProtoGenerator) File(java.io.File) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) Stream(java.util.stream.Stream) PersistenceGenerator.hasProtoMarshaller(org.kie.kogito.codegen.process.persistence.PersistenceGenerator.hasProtoMarshaller) KOGITO_PERSISTENCE_TYPE(org.kie.kogito.codegen.process.persistence.PersistenceGenerator.KOGITO_PERSISTENCE_TYPE) GeneratedFile(org.kie.kogito.codegen.api.GeneratedFile) KOGITO_PERSISTENCE_PROTO_MARSHALLER(org.kie.kogito.codegen.process.persistence.PersistenceGenerator.KOGITO_PERSISTENCE_PROTO_MARSHALLER) Collections(java.util.Collections) MethodSource(org.junit.jupiter.params.provider.MethodSource) ReflectionProtoGenerator(org.kie.kogito.codegen.process.persistence.proto.ReflectionProtoGenerator) ReflectionMarshallerGenerator(org.kie.kogito.codegen.process.persistence.marshaller.ReflectionMarshallerGenerator) GeneratedPOJO(org.kie.kogito.codegen.data.GeneratedPOJO) GeneratedFile(org.kie.kogito.codegen.api.GeneratedFile) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 2 with ProtoGenerator

use of org.kie.kogito.codegen.process.persistence.proto.ProtoGenerator in project kogito-runtimes by kiegroup.

the class AbstractMarshallerGeneratorTest method testEnumMarshallers.

@Test
void testEnumMarshallers() {
    Stream.of(Answer.class, AnswerWithAnnotations.class).forEach(e -> {
        ProtoGenerator generator = protoGeneratorBuilder().withDataClasses(convertTypes(e)).build(null);
        Proto proto = generator.protoOfDataClasses("org.kie.kogito.test");
        assertThat(proto).isNotNull();
        assertThat(proto.getEnums()).hasSize(1);
        MarshallerGenerator marshallerGenerator = withGenerator(e);
        List<CompilationUnit> classes = null;
        try {
            classes = marshallerGenerator.generate(proto.serialize());
        } catch (IOException ex) {
            fail("Error generating marshaller for " + e.getName(), e);
        }
        assertThat(classes).isNotNull();
        assertThat(classes).hasSize(1);
        Optional<ClassOrInterfaceDeclaration> marshallerClass = classes.get(0).getClassByName(e.getSimpleName() + "EnumMarshaller");
        assertThat(marshallerClass).isPresent();
    });
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) Answer(org.kie.kogito.codegen.data.Answer) Proto(org.kie.kogito.codegen.process.persistence.proto.Proto) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) IOException(java.io.IOException) AnswerWithAnnotations(org.kie.kogito.codegen.data.AnswerWithAnnotations) ProtoGenerator(org.kie.kogito.codegen.process.persistence.proto.ProtoGenerator) AbstractProtoGenerator(org.kie.kogito.codegen.process.persistence.proto.AbstractProtoGenerator) Test(org.junit.jupiter.api.Test)

Example 3 with ProtoGenerator

use of org.kie.kogito.codegen.process.persistence.proto.ProtoGenerator in project kogito-runtimes by kiegroup.

the class AbstractMarshallerGeneratorTest method testEnumInPojosMarshallers.

@Test
void testEnumInPojosMarshallers() {
    Stream.of(Question.class, QuestionWithAnnotatedEnum.class).forEach(c -> {
        ProtoGenerator generator = protoGeneratorBuilder().withDataClasses(convertTypes(c)).build(null);
        Proto proto = generator.protoOfDataClasses("org.kie.kogito.test");
        assertThat(proto).isNotNull();
        assertThat(proto.getMessages()).hasSize(1);
        MarshallerGenerator marshallerGenerator = withGenerator(c);
        List<CompilationUnit> classes = null;
        try {
            classes = marshallerGenerator.generate(proto.serialize());
        } catch (IOException e) {
            fail("Error generating marshaller for " + c.getName(), e);
        }
        assertThat(classes).isNotNull();
        assertThat(classes).hasSize(2);
        Optional<ClassOrInterfaceDeclaration> marshallerClass = classes.get(0).getClassByName(c.getSimpleName() + "MessageMarshaller");
        assertThat(marshallerClass).isPresent();
        String answerType = null;
        try {
            answerType = c.getDeclaredField("answer").getType().getSimpleName();
        } catch (NoSuchFieldException e) {
            fail("Unable to get answer field type for " + c.getName(), e);
        }
        marshallerClass = classes.get(1).getClassByName(answerType + "EnumMarshaller");
        assertThat(marshallerClass).isPresent();
    });
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) Proto(org.kie.kogito.codegen.process.persistence.proto.Proto) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) Question(org.kie.kogito.codegen.data.Question) IOException(java.io.IOException) QuestionWithAnnotatedEnum(org.kie.kogito.codegen.data.QuestionWithAnnotatedEnum) ProtoGenerator(org.kie.kogito.codegen.process.persistence.proto.ProtoGenerator) AbstractProtoGenerator(org.kie.kogito.codegen.process.persistence.proto.AbstractProtoGenerator) Test(org.junit.jupiter.api.Test)

Example 4 with ProtoGenerator

use of org.kie.kogito.codegen.process.persistence.proto.ProtoGenerator in project kogito-runtimes by kiegroup.

the class AbstractMarshallerGeneratorTest method testPersonWithListMarshallers.

@Test
void testPersonWithListMarshallers() throws Exception {
    ProtoGenerator generator = protoGeneratorBuilder().withDataClasses(convertTypes(PersonWithList.class)).build(null);
    Proto proto = generator.protoOfDataClasses("org.kie.kogito.test");
    assertThat(proto).isNotNull();
    assertThat(proto.getMessages()).hasSize(1);
    MarshallerGenerator marshallerGenerator = withGenerator(PersonWithList.class);
    List<CompilationUnit> classes = marshallerGenerator.generate(proto.serialize());
    assertThat(classes).isNotNull();
    assertThat(classes).hasSize(1);
    Optional<ClassOrInterfaceDeclaration> marshallerClass = classes.get(0).getClassByName("PersonWithListMessageMarshaller");
    assertThat(marshallerClass).isPresent();
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) Proto(org.kie.kogito.codegen.process.persistence.proto.Proto) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) ProtoGenerator(org.kie.kogito.codegen.process.persistence.proto.ProtoGenerator) AbstractProtoGenerator(org.kie.kogito.codegen.process.persistence.proto.AbstractProtoGenerator) Test(org.junit.jupiter.api.Test)

Example 5 with ProtoGenerator

use of org.kie.kogito.codegen.process.persistence.proto.ProtoGenerator in project kogito-runtimes by kiegroup.

the class AbstractMarshallerGeneratorTest method testPersonWithAddressMarshallers.

@Test
void testPersonWithAddressMarshallers() throws Exception {
    ProtoGenerator generator = protoGeneratorBuilder().withDataClasses(convertTypes(PersonWithAddress.class)).build(null);
    Proto proto = generator.protoOfDataClasses("org.kie.kogito.test");
    assertThat(proto).isNotNull();
    assertThat(proto.getMessages()).hasSize(2);
    MarshallerGenerator marshallerGenerator = withGenerator(PersonWithAddresses.class);
    List<CompilationUnit> classes = marshallerGenerator.generate(proto.serialize());
    assertThat(classes).isNotNull();
    assertThat(classes).hasSize(2);
    Optional<ClassOrInterfaceDeclaration> marshallerClass = classes.get(0).getClassByName("AddressMessageMarshaller");
    assertThat(marshallerClass).isPresent();
    marshallerClass = classes.get(1).getClassByName("PersonWithAddressMessageMarshaller");
    assertThat(marshallerClass).isPresent();
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) Proto(org.kie.kogito.codegen.process.persistence.proto.Proto) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) ProtoGenerator(org.kie.kogito.codegen.process.persistence.proto.ProtoGenerator) AbstractProtoGenerator(org.kie.kogito.codegen.process.persistence.proto.AbstractProtoGenerator) Test(org.junit.jupiter.api.Test)

Aggregations

ProtoGenerator (org.kie.kogito.codegen.process.persistence.proto.ProtoGenerator)7 CompilationUnit (com.github.javaparser.ast.CompilationUnit)6 ClassOrInterfaceDeclaration (com.github.javaparser.ast.body.ClassOrInterfaceDeclaration)6 Test (org.junit.jupiter.api.Test)6 AbstractProtoGenerator (org.kie.kogito.codegen.process.persistence.proto.AbstractProtoGenerator)6 Proto (org.kie.kogito.codegen.process.persistence.proto.Proto)6 IOException (java.io.IOException)2 File (java.io.File)1 Collection (java.util.Collection)1 Collections (java.util.Collections)1 Stream (java.util.stream.Stream)1 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)1 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)1 Arguments (org.junit.jupiter.params.provider.Arguments)1 MethodSource (org.junit.jupiter.params.provider.MethodSource)1 AddonsConfig (org.kie.kogito.codegen.api.AddonsConfig)1 GeneratedFile (org.kie.kogito.codegen.api.GeneratedFile)1 GeneratedFileType (org.kie.kogito.codegen.api.GeneratedFileType)1 KogitoBuildContext (org.kie.kogito.codegen.api.context.KogitoBuildContext)1 KogitoContextTestUtils.contextBuilders (org.kie.kogito.codegen.api.utils.KogitoContextTestUtils.contextBuilders)1