use of org.drools.codegen.common.GeneratedFile in project kogito-runtimes by kiegroup.
the class DecisionModelResourcesProviderGeneratorTest method generateDecisionModelResourcesProvider.
@Test
public void generateDecisionModelResourcesProvider() {
final KogitoBuildContext context = QuarkusKogitoBuildContext.builder().withAddonsConfig(AddonsConfig.builder().withTracing(true).build()).build();
final Collection<CollectedResource> collectedResources = CollectedResourceProducer.fromPaths(Paths.get("src/test/resources/decision/models/vacationDays").toAbsolutePath(), Paths.get("src/test/resources/decision/models/vacationDaysAlt").toAbsolutePath());
final long numberOfModels = collectedResources.stream().filter(r -> r.resource().getResourceType() == ResourceType.DMN).count();
final DecisionCodegen codeGenerator = DecisionCodegen.ofCollectedResources(context, collectedResources);
final Collection<GeneratedFile> generatedFiles = codeGenerator.generate();
// the two resources below, see https://github.com/kiegroup/kogito-runtimes/commit/18ec525f530b1ff1bddcf18c0083f14f86aff171#diff-edd3a09d62dc627ee10fe37925944217R53
assertThat(generatedFiles.size()).isGreaterThanOrEqualTo(2);
// Align this FAI-215 test (#621) with unknown order of generated files (ie.: additional generated files might be present)
// A Rest endpoint is always generated per model.
List<GeneratedFile> generatedRESTFiles = generatedFiles.stream().filter(gf -> gf.type().equals(AbstractGenerator.REST_TYPE)).collect(toList());
assertFalse(generatedRESTFiles.isEmpty());
assertEquals(numberOfModels, generatedRESTFiles.size());
List<GeneratedFile> generatedCLASSFile = generatedFiles.stream().filter(gf -> gf.type().equals(GeneratedFileType.SOURCE)).collect(toList());
assertEquals(1, generatedCLASSFile.size());
GeneratedFile classFile = generatedCLASSFile.get(0);
assertEquals("org/kie/kogito/app/DecisionModelResourcesProvider.java", 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("getResources")).orElseThrow(() -> new NoSuchElementException("Class declaration doesn't contain a method named \"getResources\"!"));
assertNotNull(methodDeclaration);
assertTrue(methodDeclaration.getBody().isPresent());
final BlockStmt body = methodDeclaration.getBody().get();
assertTrue(body.getStatements().size() > 2);
assertTrue(body.getStatements().get(1).isExpressionStmt());
final ExpressionStmt expression = (ExpressionStmt) body.getStatements().get(1);
assertTrue(expression.getExpression() instanceof MethodCallExpr);
final MethodCallExpr call = (MethodCallExpr) expression.getExpression();
assertEquals("add", call.getName().getIdentifier());
assertTrue(call.getScope().isPresent());
assertTrue(call.getScope().get().isNameExpr());
final NameExpr nameExpr = call.getScope().get().asNameExpr();
assertEquals("resourcePaths", nameExpr.getName().getIdentifier());
long numberOfAddStms = body.getStatements().stream().filter(this::isAddStatement).count();
assertEquals(numberOfModels, numberOfAddStms);
List<NodeList<Expression>> defaultDecisionModelResources = body.getStatements().stream().filter(this::isAddStatement).map(stm -> stm.asExpressionStmt().getExpression().asMethodCallExpr().getArguments()).collect(toList());
// verify .add(..) number of parameters
defaultDecisionModelResources.forEach(nodeList -> assertEquals(1, nodeList.size()));
Set<String> distinctDefaultDecisionModelResources = defaultDecisionModelResources.stream().map(nodeList -> nodeList.get(0).toString()).collect(toSet());
assertEquals(defaultDecisionModelResources.size(), distinctDefaultDecisionModelResources.size());
}
use of org.drools.codegen.common.GeneratedFile in project kogito-runtimes by kiegroup.
the class CustomDashboardGeneratedUtilsTest method addToGeneratedFiles.
@ParameterizedTest
@MethodSource("org.kie.kogito.codegen.api.utils.KogitoContextTestUtils#contextBuilders")
void addToGeneratedFiles(KogitoBuildContext.Builder contextBuilder) {
final KogitoBuildContext context = contextBuilder.withAppPaths(AppPaths.fromTestDir(new File(".").toPath())).build();
Collection<CollectedResource> collectedResources = CollectedResourceProducer.fromPaths(context.getAppPaths().getPaths());
Map<String, List<Resource>> dashboardJsonsMap = CustomDashboardGeneratedUtils.getMappedJsons(collectedResources);
Collection<GeneratedFile> toPopulate = new ArrayList<>();
CustomDashboardGeneratedUtils.addToGeneratedFiles(dashboardJsonsMap.get(OPERATIONAL_DASHBOARD_PREFIX), toPopulate, operationalFunction, OPERATIONAL_DASHBOARD_PREFIX);
assertEquals(dashboardJsonsMap.get(OPERATIONAL_DASHBOARD_PREFIX).size(), toPopulate.size());
String sourcePath = dashboardJsonsMap.get(OPERATIONAL_DASHBOARD_PREFIX).get(0).getSourcePath();
String originalFileName = sourcePath.substring(sourcePath.lastIndexOf(File.separator) + 1);
validateGeneratedFile(toPopulate.iterator().next(), OPERATIONAL_DASHBOARD_PREFIX, originalFileName);
toPopulate = new ArrayList<>();
CustomDashboardGeneratedUtils.addToGeneratedFiles(dashboardJsonsMap.get(DOMAIN_DASHBOARD_PREFIX), toPopulate, domainFunction, DOMAIN_DASHBOARD_PREFIX);
assertEquals(dashboardJsonsMap.get(DOMAIN_DASHBOARD_PREFIX).size(), toPopulate.size());
sourcePath = dashboardJsonsMap.get(DOMAIN_DASHBOARD_PREFIX).get(0).getSourcePath();
originalFileName = sourcePath.substring(sourcePath.lastIndexOf(File.separator) + 1);
validateGeneratedFile(toPopulate.iterator().next(), DOMAIN_DASHBOARD_PREFIX, originalFileName);
}
use of org.drools.codegen.common.GeneratedFile in project kogito-runtimes by kiegroup.
the class CustomDashboardGeneratedUtilsTest method loadCustomGrafanaDashboardsList.
@ParameterizedTest
@MethodSource("org.kie.kogito.codegen.api.utils.KogitoContextTestUtils#contextBuilders")
void loadCustomGrafanaDashboardsList(KogitoBuildContext.Builder contextBuilder) {
final KogitoBuildContext context = contextBuilder.withAppPaths(AppPaths.fromTestDir(new File(".").toPath())).build();
Collection<GeneratedFile> retrieved = CustomDashboardGeneratedUtils.loadCustomGrafanaDashboardsList(context);
assertNotNull(retrieved);
// 2 = valid *.json files inside src/test/META-INF/dashboards
assertEquals(2, retrieved.size());
}
use of org.drools.codegen.common.GeneratedFile in project kogito-runtimes by kiegroup.
the class GeneratedFileValidationTest method validateGeneratedFileTypes.
@Test
public void validateGeneratedFileTypes() {
List<GeneratedFile> generatedFiles = Arrays.asList(new GeneratedFile(GeneratedFileType.SOURCE, "myPath1", ""), new GeneratedFile(GeneratedFileType.INTERNAL_RESOURCE, "myPath2", ""), new GeneratedFile(GeneratedFileType.COMPILED_CLASS, "myPath3", ""), new GeneratedFile(GeneratedFileType.STATIC_HTTP_RESOURCE, "myPath4", ""));
GeneratedFileValidation.validateGeneratedFileTypes(generatedFiles, Arrays.asList(GeneratedFileType.Category.SOURCE, GeneratedFileType.Category.INTERNAL_RESOURCE, GeneratedFileType.Category.STATIC_HTTP_RESOURCE, GeneratedFileType.Category.COMPILED_CLASS));
Set<GeneratedFileType.Category> categories = Collections.singleton(GeneratedFileType.Category.SOURCE);
Assertions.assertThatThrownBy(() -> GeneratedFileValidation.validateGeneratedFileTypes(generatedFiles, categories)).isInstanceOf(IllegalStateException.class).hasMessageContaining("myPath2").hasMessageContaining("myPath3").hasMessageContaining("myPath4");
}
use of org.drools.codegen.common.GeneratedFile in project kogito-runtimes by kiegroup.
the class PredictionCodegenTest method generateAllFiles.
private static void generateAllFiles(KogitoBuildContext context, PredictionCodegen codeGenerator, int expectedTotalFiles, int expectedJavaSources, int expectedRestEndpoints, boolean assertReflect) {
Collection<GeneratedFile> generatedFiles = codeGenerator.generate();
int expectedGeneratedFilesSize = expectedTotalFiles - (context.hasRESTForGenerator(codeGenerator) ? 0 : expectedRestEndpoints * 2);
assertEquals(expectedGeneratedFilesSize, generatedFiles.size());
assertEquals(expectedJavaSources, generatedFiles.stream().filter(generatedFile -> generatedFile.category().equals(GeneratedFileType.Category.SOURCE) && generatedFile.type().name().equals(PMML) && generatedFile.relativePath().endsWith(".java")).count());
int expectedReflectResource = assertReflect ? 1 : 0;
assertEquals(expectedReflectResource, generatedFiles.stream().filter(generatedFile -> generatedFile.category().equals(GeneratedFileType.Category.INTERNAL_RESOURCE) && generatedFile.type().name().equals(GeneratedFileType.INTERNAL_RESOURCE.name()) && generatedFile.relativePath().endsWith(REFLECT_JSON)).count());
assertEndpoints(context, generatedFiles, expectedRestEndpoints, codeGenerator);
Optional<ApplicationSection> optionalApplicationSection = codeGenerator.section();
assertTrue(optionalApplicationSection.isPresent());
CompilationUnit compilationUnit = optionalApplicationSection.get().compilationUnit();
assertNotNull(compilationUnit);
}
Aggregations