use of org.kie.kogito.codegen.process.persistence.PersistenceGenerator.KOGITO_PERSISTENCE_TYPE in project kogito-runtimes by kiegroup.
the class MongoDBPersistenceGeneratorTest 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();
if (context.hasDI()) {
Optional<GeneratedFile> generatedCLASSFile = generatedFiles.stream().filter(gf -> gf.category() == GeneratedFileType.SOURCE.category()).filter(f -> PERSISTENCE_FILE_PATH.equals(f.relativePath())).findAny();
assertTrue(generatedCLASSFile.isPresent());
GeneratedFile classFile = generatedCLASSFile.get();
assertEquals(PERSISTENCE_FILE_PATH, classFile.relativePath());
final CompilationUnit compilationUnit = parse(new ByteArrayInputStream(classFile.contents()));
final ClassOrInterfaceDeclaration classDeclaration = compilationUnit.findFirst(ClassOrInterfaceDeclaration.class).orElseThrow(() -> new NoSuchElementException("Compilation unit doesn't contain a class or interface declaration!"));
assertNotNull(classDeclaration);
final MethodDeclaration methodDeclaration = classDeclaration.findFirst(MethodDeclaration.class, d -> d.getName().getIdentifier().equals("dbName")).orElseThrow(() -> new NoSuchElementException("Class declaration doesn't contain a method named \"dbName\"!"));
assertNotNull(methodDeclaration);
assertTrue(methodDeclaration.getBody().isPresent());
final BlockStmt body = methodDeclaration.getBody().get();
assertThat(body.getStatements().size()).isOne();
assertTrue(body.getStatements().get(0).isReturnStmt());
final ReturnStmt returnStmt = (ReturnStmt) body.getStatements().get(0);
assertThat(returnStmt.toString()).contains("kogito");
final MethodDeclaration transactionMethodDeclaration = classDeclaration.findFirst(MethodDeclaration.class, d -> "transactionManager".equals(d.getName().getIdentifier())).orElseThrow(() -> new NoSuchElementException("Class declaration doesn't contain a method named \"transactionManager\"!"));
assertNotNull(transactionMethodDeclaration);
assertTrue(transactionMethodDeclaration.getBody().isPresent());
final BlockStmt transactionMethodBody = transactionMethodDeclaration.getBody().get();
assertThat(transactionMethodBody.getStatements().size()).isOne();
assertTrue(transactionMethodBody.getStatements().get(0).isReturnStmt());
final ReturnStmt transactionReturnStmt = (ReturnStmt) transactionMethodBody.getStatements().get(0);
assertThat(transactionReturnStmt.toString()).contains("transactionManager");
Optional<GeneratedFile> generatedTransactionCLASSFile = generatedFiles.stream().filter(gf -> gf.category() == GeneratedFileType.SOURCE.category()).filter(f -> TRANSACTION_FILE_PATH.equals(f.relativePath())).findAny();
assertTrue(generatedTransactionCLASSFile.isPresent());
final CompilationUnit transactionCompilationUnit = parse(new ByteArrayInputStream(generatedTransactionCLASSFile.get().contents()));
final ClassOrInterfaceDeclaration transactionClassDeclaration = transactionCompilationUnit.findFirst(ClassOrInterfaceDeclaration.class).orElseThrow(() -> new NoSuchElementException("Compilation unit doesn't contain a class or interface declaration!"));
assertNotNull(transactionClassDeclaration);
final List<ConstructorDeclaration> constructorDeclarations = transactionCompilationUnit.findAll(ConstructorDeclaration.class);
assertEquals(2, constructorDeclarations.size());
Optional<ConstructorDeclaration> annotatedConstructorDeclaration = constructorDeclarations.stream().filter(c -> c.isAnnotationPresent(injectionAnnotation(context))).findAny();
assertTrue(annotatedConstructorDeclaration.isPresent());
final MethodDeclaration transactionEnabledMethodDeclaration = transactionClassDeclaration.findFirst(MethodDeclaration.class, d -> d.getName().getIdentifier().equals("enabled")).orElseThrow(() -> new NoSuchElementException("Class declaration doesn't contain a method named \"enabled\"!"));
assertNotNull(transactionEnabledMethodDeclaration);
assertTrue(transactionEnabledMethodDeclaration.getBody().isPresent());
final BlockStmt enabledMethodBody = transactionEnabledMethodDeclaration.getBody().get();
assertThat(enabledMethodBody.getStatements().size()).isOne();
assertTrue(enabledMethodBody.getStatements().get(0).isReturnStmt());
final ReturnStmt enabledReturnStmt = (ReturnStmt) enabledMethodBody.getStatements().get(0);
assertThat(enabledReturnStmt.toString()).contains("enabled");
final FieldDeclaration transactionEnabledFieldDeclaration = transactionClassDeclaration.findFirst(FieldDeclaration.class, f -> f.getVariable(0).getName().getIdentifier().equals("enabled")).orElseThrow(() -> new NoSuchElementException("Class declaration doesn't contain a field named \"enabled\"!"));
assertNotNull(transactionEnabledFieldDeclaration);
final AnnotationExpr transactionEnabledAnnotationDeclaration = transactionEnabledFieldDeclaration.findFirst(AnnotationExpr.class, a -> ((Name) a.getChildNodes().get(0)).getIdentifier().equals(configInjectionAnnotation(context))).orElseThrow(() -> new NoSuchElementException("Field declaration doesn't contain an annotation named \"" + configInjectionAnnotation(context) + "\"!"));
assertNotNull(transactionEnabledAnnotationDeclaration);
assertThat(transactionEnabledAnnotationDeclaration.getChildNodes().get(1).toString()).contains("kogito.persistence.transaction.enabled");
}
}
use of org.kie.kogito.codegen.process.persistence.PersistenceGenerator.KOGITO_PERSISTENCE_TYPE 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);
}
use of org.kie.kogito.codegen.process.persistence.PersistenceGenerator.KOGITO_PERSISTENCE_TYPE in project kogito-runtimes by kiegroup.
the class InfinispanPersistenceGeneratorTest 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();
if (context.hasDI()) {
Optional<GeneratedFile> persistenceFactoryImpl = generatedFiles.stream().filter(gf -> gf.relativePath().equals("org/kie/kogito/persistence/KogitoProcessInstancesFactoryImpl.java")).findFirst();
if (hasDataIndexProto(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()));
compilationUnit.findFirst(ClassOrInterfaceDeclaration.class).orElseThrow(() -> new NoSuchElementException("Compilation unit doesn't contain a class or interface declaration!"));
}
}
use of org.kie.kogito.codegen.process.persistence.PersistenceGenerator.KOGITO_PERSISTENCE_TYPE in project kogito-runtimes by kiegroup.
the class KafkaPersistenceGeneratorTest 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();
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()));
compilationUnit.findFirst(ClassOrInterfaceDeclaration.class).orElseThrow(() -> new NoSuchElementException("Compilation unit doesn't contain a class or interface declaration!"));
}
}
use of org.kie.kogito.codegen.process.persistence.PersistenceGenerator.KOGITO_PERSISTENCE_TYPE 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);
}
}
Aggregations