use of org.kie.kogito.codegen.api.io.CollectedResource in project kogito-runtimes by kiegroup.
the class CollectedResourceProducer method fromPaths.
/**
* Returns a collection of CollectedResource from the given paths.
* If a path is a jar, then walks inside the jar.
*/
public static Collection<CollectedResource> fromPaths(Path... paths) {
Collection<CollectedResource> resources = new ArrayList<>();
for (Path path : paths) {
if (path.toFile().isDirectory()) {
Collection<CollectedResource> res = fromDirectory(path);
resources.addAll(res);
} else if (path.getFileName().toString().endsWith(".jar") || path.getFileName().toString().endsWith(".jar.original")) {
Collection<CollectedResource> res = fromJarFile(path);
resources.addAll(res);
} else if (!path.toFile().exists()) {
LOGGER.debug("Skipping '{}' because doesn't exist", path);
} else {
throw new IllegalArgumentException("Expected directory or archive, file given: " + path);
}
}
return resources;
}
use of org.kie.kogito.codegen.api.io.CollectedResource in project kogito-runtimes by kiegroup.
the class CollectedResourceProducer method fromJarFile.
/**
* Returns a collection of CollectedResource from the given jar file.
*/
public static Collection<CollectedResource> fromJarFile(Path jarPath) {
Collection<CollectedResource> resources = new ArrayList<>();
try (ZipFile zipFile = new ZipFile(jarPath.toFile())) {
Enumeration<? extends ZipEntry> entries = zipFile.entries();
while (entries.hasMoreElements()) {
ZipEntry entry = entries.nextElement();
InternalResource resource = new ByteArrayResource(readBytesFromInputStream(zipFile.getInputStream(entry)));
resource.setSourcePath(entry.getName());
resources.add(toCollectedResource(jarPath, entry.getName(), resource));
}
return resources;
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
use of org.kie.kogito.codegen.api.io.CollectedResource 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.kie.kogito.codegen.api.io.CollectedResource in project kogito-runtimes by kiegroup.
the class OpenApiClientServerlessWorkflowIT method openApiSpecInClasspath.
@ParameterizedTest
@ValueSource(strings = { "openapi/petstore-classpath.sw.json" })
public void openApiSpecInClasspath(final String resource) {
final KogitoBuildContext context = this.newContext();
final Collection<CollectedResource> resources = toCollectedResources(Collections.singletonList(resource));
// OpenApi Generation
final OpenApiClientCodegen openApiClientCodegen = OpenApiClientCodegen.ofCollectedResources(context, resources);
assertThat(openApiClientCodegen.getOpenAPISpecResources()).isNotEmpty();
Collection<GeneratedFile> openApiGeneratedFiles = openApiClientCodegen.generate();
assertThat(openApiGeneratedFiles).isNotEmpty();
assertThat(context.getContextAttribute(ContextAttributesConstants.OPENAPI_DESCRIPTORS, List.class)).isNotEmpty();
// Process Code Generation
final ProcessCodegen processCodegen = ProcessCodegen.ofCollectedResources(context, resources);
Collection<GeneratedFile> processGeneratedFiles = processCodegen.generate();
assertThat(processGeneratedFiles).isNotEmpty();
}
use of org.kie.kogito.codegen.api.io.CollectedResource in project kogito-runtimes by kiegroup.
the class ProcessCodegen method processSVG.
private static void processSVG(FileSystemResource resource, Collection<CollectedResource> resources, Collection<Process> processes, Map<String, String> processSVGMap) {
File f = resource.getFile();
String processFileCompleteName = f.getName();
String fileName = processFileCompleteName.substring(0, processFileCompleteName.lastIndexOf("."));
processes.stream().forEach(process -> {
if (isFilenameValid(process.getId() + ".svg")) {
resources.stream().filter(r -> r.resource().getSourcePath().endsWith(String.format(SVG_EXPORT_NAME_EXPRESION, fileName))).forEach(svg -> {
try {
processSVGMap.put(process.getId(), new String(Files.readAllBytes(Paths.get(svg.resource().getSourcePath()))));
} catch (IOException e) {
LOGGER.error("\n IOException trying to add " + svg.resource().getSourcePath() + " with processId:" + process.getId() + "\n" + e.getMessage(), e);
}
});
}
});
}
Aggregations