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);
}
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();
}
}
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();
}
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();
}
}
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"));
}
Aggregations