use of org.kie.kogito.codegen.api.context.KogitoBuildContext in project kogito-runtimes by kiegroup.
the class CustomDashboardGeneratedUtilsTest method getMappedJsons.
@ParameterizedTest
@MethodSource("org.kie.kogito.codegen.api.utils.KogitoContextTestUtils#contextBuilders")
void getMappedJsons(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>> retrieved = CustomDashboardGeneratedUtils.getMappedJsons(collectedResources);
// 2 = valid *.json files inside src/test/META-INF/dashboards
assertEquals(2, retrieved.size());
assertEquals(1, retrieved.get(OPERATIONAL_DASHBOARD_PREFIX).size());
assertEquals(1, retrieved.get(DOMAIN_DASHBOARD_PREFIX).size());
}
use of org.kie.kogito.codegen.api.context.KogitoBuildContext 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.kie.kogito.codegen.api.context.KogitoBuildContext 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.kie.kogito.codegen.api.context.KogitoBuildContext in project kogito-runtimes by kiegroup.
the class AbstractKogitoBuildContextTest method packageNameValidation.
@Test
public void packageNameValidation() {
assertThat(builder.build().getPackageName()).isEqualTo(KogitoBuildContext.DEFAULT_PACKAGE_NAME);
assertThatThrownBy(() -> builder.withPackageName(null)).isInstanceOf(NullPointerException.class);
assertThatThrownBy(() -> builder.withPackageName("i.am.an-invalid.package-name.sorry")).isInstanceOf(IllegalArgumentException.class);
KogitoBuildContext context = builder.withPackageName(AbstractKogitoBuildContext.DEFAULT_GROUP_ID).build();
assertThat(context.getPackageName()).isNotEqualTo(AbstractKogitoBuildContext.DEFAULT_GROUP_ID);
}
use of org.kie.kogito.codegen.api.context.KogitoBuildContext 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());
}
Aggregations