Search in sources :

Example 6 with ReflectionMarshallerGenerator

use of org.kie.kogito.codegen.process.persistence.marshaller.ReflectionMarshallerGenerator in project kogito-runtimes by kiegroup.

the class FileSystemPersistenceGeneratorTest method test.

@ParameterizedTest
@MethodSource("persistenceTestContexts")
void test(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));
    Collection<GeneratedFile> generatedFiles = persistenceGenerator.generate();
    int factoryFiles = context.hasRESTForGenerator(persistenceGenerator) ? 1 : 0;
    int marshallerFiles = hasProtoMarshaller(context) ? 14 : 0;
    int dataIndexFiles = hasDataIndexProto(context) ? 2 : 0;
    int expectedNumberOfFiles = factoryFiles + marshallerFiles + dataIndexFiles;
    assertThat(generatedFiles).hasSize(expectedNumberOfFiles);
    if (context.hasDI()) {
        Optional<GeneratedFile> persistenceFactoryImpl = generatedFiles.stream().filter(gf -> gf.relativePath().equals("org/kie/kogito/persistence/KogitoProcessInstancesFactoryImpl.java")).findFirst();
        assertThat(persistenceFactoryImpl).isNotEmpty();
        final CompilationUnit compilationUnit = parse(new ByteArrayInputStream(persistenceFactoryImpl.get().contents()));
        final ClassOrInterfaceDeclaration classDeclaration = compilationUnit.findFirst(ClassOrInterfaceDeclaration.class).orElseThrow(() -> new NoSuchElementException("Compilation unit doesn't contain a class or interface declaration!"));
        final Optional<MethodDeclaration> methodDeclaration = classDeclaration.findFirst(MethodDeclaration.class, d -> d.getName().getIdentifier().equals(PATH_NAME));
        assertThat(methodDeclaration).isNotEmpty();
        final Optional<FieldDeclaration> fieldDeclaration = classDeclaration.findFirst(FieldDeclaration.class);
        assertThat(fieldDeclaration).isNotEmpty();
        assertThat(fieldDeclaration.get().getVariables()).hasSize(1);
        assertThat(fieldDeclaration.get().getVariables().get(0).getName().asString()).isEqualTo(PATH_NAME);
    }
}
Also used : ReflectionProtoGenerator(org.kie.kogito.codegen.process.persistence.proto.ReflectionProtoGenerator) KogitoBuildContext(org.kie.kogito.codegen.api.context.KogitoBuildContext) PersistenceGenerator.hasDataIndexProto(org.kie.kogito.codegen.process.persistence.PersistenceGenerator.hasDataIndexProto) Collection(java.util.Collection) StaticJavaParser.parse(com.github.javaparser.StaticJavaParser.parse) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) ReflectionMarshallerGenerator(org.kie.kogito.codegen.process.persistence.marshaller.ReflectionMarshallerGenerator) GeneratedPOJO(org.kie.kogito.codegen.data.GeneratedPOJO) FILESYSTEM_PERSISTENCE_TYPE(org.kie.kogito.codegen.process.persistence.PersistenceGenerator.FILESYSTEM_PERSISTENCE_TYPE) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) ByteArrayInputStream(java.io.ByteArrayInputStream) FieldDeclaration(com.github.javaparser.ast.body.FieldDeclaration) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) PATH_NAME(org.kie.kogito.codegen.process.persistence.PersistenceGenerator.PATH_NAME) PersistenceGenerator.hasProtoMarshaller(org.kie.kogito.codegen.process.persistence.PersistenceGenerator.hasProtoMarshaller) KOGITO_PERSISTENCE_TYPE(org.kie.kogito.codegen.process.persistence.PersistenceGenerator.KOGITO_PERSISTENCE_TYPE) Optional(java.util.Optional) GeneratedFile(org.kie.kogito.codegen.api.GeneratedFile) CompilationUnit(com.github.javaparser.ast.CompilationUnit) NoSuchElementException(java.util.NoSuchElementException) Collections(java.util.Collections) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) MethodSource(org.junit.jupiter.params.provider.MethodSource) CompilationUnit(com.github.javaparser.ast.CompilationUnit) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) GeneratedFile(org.kie.kogito.codegen.api.GeneratedFile) FieldDeclaration(com.github.javaparser.ast.body.FieldDeclaration) ByteArrayInputStream(java.io.ByteArrayInputStream) 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) NoSuchElementException(java.util.NoSuchElementException) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 7 with ReflectionMarshallerGenerator

use of org.kie.kogito.codegen.process.persistence.marshaller.ReflectionMarshallerGenerator in project kogito-runtimes by kiegroup.

the class JDBCPersistenceGeneratorTest method test.

@ParameterizedTest
@MethodSource("persistenceTestContexts")
void test(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));
    assertEquals(persistenceType(), persistenceGenerator.persistenceType());
    Collection<GeneratedFile> generatedFiles = persistenceGenerator.generate();
    if (context.hasDI()) {
        Optional<GeneratedFile> persistenceFactoryImpl = generatedFiles.stream().filter(gf -> gf.relativePath().equals("org/kie/kogito/persistence/KogitoProcessInstancesFactoryImpl.java")).findFirst();
        if (hasProtoMarshaller(context)) {
            List<GeneratedFile> marshallerFiles = generatedFiles.stream().filter(gf -> gf.relativePath().endsWith("MessageMarshaller.java")).collect(Collectors.toList());
            String expectedMarshaller = "PersonMessageMarshaller";
            assertThat(persistenceFactoryImpl).isNotEmpty();
            assertThat(marshallerFiles.size()).isEqualTo(1);
            assertThat(marshallerFiles.get(0).relativePath()).endsWith(expectedMarshaller + ".java");
        }
        final CompilationUnit compilationUnit = parse(new ByteArrayInputStream(persistenceFactoryImpl.get().contents()));
        assertThat(compilationUnit.findFirst(ClassOrInterfaceDeclaration.class)).isNotEmpty();
    }
}
Also used : ReflectionProtoGenerator(org.kie.kogito.codegen.process.persistence.proto.ReflectionProtoGenerator) JDBC_PERSISTENCE_TYPE(org.kie.kogito.codegen.process.persistence.PersistenceGenerator.JDBC_PERSISTENCE_TYPE) KogitoBuildContext(org.kie.kogito.codegen.api.context.KogitoBuildContext) Collection(java.util.Collection) StaticJavaParser.parse(com.github.javaparser.StaticJavaParser.parse) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) ReflectionMarshallerGenerator(org.kie.kogito.codegen.process.persistence.marshaller.ReflectionMarshallerGenerator) GeneratedPOJO(org.kie.kogito.codegen.data.GeneratedPOJO) Collectors(java.util.stream.Collectors) List(java.util.List) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) ByteArrayInputStream(java.io.ByteArrayInputStream) PersistenceGenerator.hasProtoMarshaller(org.kie.kogito.codegen.process.persistence.PersistenceGenerator.hasProtoMarshaller) KOGITO_PERSISTENCE_TYPE(org.kie.kogito.codegen.process.persistence.PersistenceGenerator.KOGITO_PERSISTENCE_TYPE) Optional(java.util.Optional) GeneratedFile(org.kie.kogito.codegen.api.GeneratedFile) CompilationUnit(com.github.javaparser.ast.CompilationUnit) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) Collections(java.util.Collections) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) MethodSource(org.junit.jupiter.params.provider.MethodSource) CompilationUnit(com.github.javaparser.ast.CompilationUnit) ByteArrayInputStream(java.io.ByteArrayInputStream) 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 8 with ReflectionMarshallerGenerator

use of org.kie.kogito.codegen.process.persistence.marshaller.ReflectionMarshallerGenerator in project kogito-runtimes by kiegroup.

the class PostgrePersistenceGeneratorTest method testGeneratedFiles.

@ParameterizedTest
@MethodSource("persistenceTestContexts")
void testGeneratedFiles(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));
    Collection<GeneratedFile> generatedFiles = persistenceGenerator.generate();
    int factoryFiles = context instanceof SpringBootKogitoBuildContext ? 2 : 1;
    int marshallerFiles = hasProtoMarshaller(context) ? 14 : 0;
    int dataIndexFiles = hasDataIndexProto(context) ? 2 : 0;
    int expectedNumberOfFiles = factoryFiles + marshallerFiles + dataIndexFiles;
    assertThat(generatedFiles).hasSize(expectedNumberOfFiles);
    Optional<GeneratedFile> persistenceFactoryImpl = generatedFiles.stream().filter(gf -> gf.relativePath().equals("org/kie/kogito/persistence/KogitoProcessInstancesFactoryImpl.java")).findFirst();
    assertThat(persistenceFactoryImpl).isPresent();
    validateClassInCompilationUnit(persistenceFactoryImpl.get().contents());
    Optional<GeneratedFile> pgClientProducer = generatedFiles.stream().filter(gf -> gf.relativePath().equals("org/kie/kogito/persistence/PgClientProducer.java")).findFirst();
    if (context instanceof SpringBootKogitoBuildContext) {
        assertThat(pgClientProducer).isPresent();
        validateClassInCompilationUnit(pgClientProducer.get().contents());
    } else {
        assertThat(pgClientProducer).isEmpty();
    }
}
Also used : ReflectionProtoGenerator(org.kie.kogito.codegen.process.persistence.proto.ReflectionProtoGenerator) KogitoBuildContext(org.kie.kogito.codegen.api.context.KogitoBuildContext) PersistenceGenerator.hasDataIndexProto(org.kie.kogito.codegen.process.persistence.PersistenceGenerator.hasDataIndexProto) Collection(java.util.Collection) SpringBootKogitoBuildContext(org.kie.kogito.codegen.api.context.impl.SpringBootKogitoBuildContext) StaticJavaParser.parse(com.github.javaparser.StaticJavaParser.parse) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) ReflectionMarshallerGenerator(org.kie.kogito.codegen.process.persistence.marshaller.ReflectionMarshallerGenerator) GeneratedPOJO(org.kie.kogito.codegen.data.GeneratedPOJO) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) ByteArrayInputStream(java.io.ByteArrayInputStream) POSTGRESQL_PERSISTENCE_TYPE(org.kie.kogito.codegen.process.persistence.PersistenceGenerator.POSTGRESQL_PERSISTENCE_TYPE) PersistenceGenerator.hasProtoMarshaller(org.kie.kogito.codegen.process.persistence.PersistenceGenerator.hasProtoMarshaller) KOGITO_PERSISTENCE_TYPE(org.kie.kogito.codegen.process.persistence.PersistenceGenerator.KOGITO_PERSISTENCE_TYPE) Optional(java.util.Optional) GeneratedFile(org.kie.kogito.codegen.api.GeneratedFile) CompilationUnit(com.github.javaparser.ast.CompilationUnit) NoSuchElementException(java.util.NoSuchElementException) Collections(java.util.Collections) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) 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) SpringBootKogitoBuildContext(org.kie.kogito.codegen.api.context.impl.SpringBootKogitoBuildContext) 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)

Aggregations

Collection (java.util.Collection)8 GeneratedFile (org.kie.kogito.codegen.api.GeneratedFile)8 KogitoBuildContext (org.kie.kogito.codegen.api.context.KogitoBuildContext)8 ReflectionMarshallerGenerator (org.kie.kogito.codegen.process.persistence.marshaller.ReflectionMarshallerGenerator)8 ReflectionProtoGenerator (org.kie.kogito.codegen.process.persistence.proto.ReflectionProtoGenerator)8 Collections (java.util.Collections)7 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)7 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)7 MethodSource (org.junit.jupiter.params.provider.MethodSource)7 GeneratedPOJO (org.kie.kogito.codegen.data.GeneratedPOJO)7 KOGITO_PERSISTENCE_TYPE (org.kie.kogito.codegen.process.persistence.PersistenceGenerator.KOGITO_PERSISTENCE_TYPE)7 StaticJavaParser.parse (com.github.javaparser.StaticJavaParser.parse)6 CompilationUnit (com.github.javaparser.ast.CompilationUnit)6 ClassOrInterfaceDeclaration (com.github.javaparser.ast.body.ClassOrInterfaceDeclaration)6 ByteArrayInputStream (java.io.ByteArrayInputStream)6 Optional (java.util.Optional)6 List (java.util.List)5 NoSuchElementException (java.util.NoSuchElementException)5 PersistenceGenerator.hasProtoMarshaller (org.kie.kogito.codegen.process.persistence.PersistenceGenerator.hasProtoMarshaller)5 Collectors (java.util.stream.Collectors)4