Skip to content

Commit

Permalink
KOGITO-7235: Add endpoint to get source definition file content (apac…
Browse files Browse the repository at this point in the history
  • Loading branch information
nmirasch committed Jun 13, 2022
1 parent a246159 commit 0fa3fbc
Show file tree
Hide file tree
Showing 17 changed files with 808 additions and 59 deletions.
5 changes: 4 additions & 1 deletion addons/common/source-files/pom.xml
Expand Up @@ -13,7 +13,10 @@
<name>Kogito :: Add-Ons :: Source Files :: Common</name>

<dependencies>

<dependency>
<groupId>org.kie.kogito</groupId>
<artifactId>kogito-codegen-processes</artifactId>
</dependency>
<!-- Test -->

<dependency>
Expand Down
@@ -0,0 +1,24 @@
/*
* Copyright 2022 Red Hat, Inc. and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.kie.kogito.addon.source.files;

public class SourceFilesException extends RuntimeException {

public SourceFilesException(String message, Throwable cause) {
super(message, cause);
}
}
Expand Up @@ -16,14 +16,23 @@
package org.kie.kogito.addon.source.files;

import java.util.Collection;
import java.util.Optional;

public interface SourceFilesProvider {

/**
* Returns the source files for the given ID.
* Returns the source files for the given processId.
*
* @param id the source file ID
* @param processId the process identifier
* @return the source files collection. The collection may be empty but not null.
*/
Collection<SourceFile> getSourceFiles(String id);
Collection<SourceFile> getProcessSourceFiles(String processId);

/**
* Returns the source file for the given processId.
*
* @param processId the process identifier
* @return the source file content.
*/
Optional<String> getProcessSourceFile(String processId);
}
Expand Up @@ -15,12 +15,18 @@
*/
package org.kie.kogito.addon.source.files;

import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Optional;
import java.util.Set;

import org.apache.commons.io.IOUtils;
import org.kie.kogito.codegen.process.ProcessCodegen;

public final class SourceFilesProviderImpl implements SourceFilesProvider {

private final Map<String, Collection<SourceFile>> sourceFiles = new HashMap<>();
Expand All @@ -30,11 +36,27 @@ public void addSourceFile(String id, SourceFile sourceFile) {
}

@Override
public Collection<SourceFile> getSourceFiles(String id) {
return sourceFiles.getOrDefault(id, Set.of());
public Collection<SourceFile> getProcessSourceFiles(String processId) {
return sourceFiles.getOrDefault(processId, Set.of());
}

@Override
public Optional<String> getProcessSourceFile(String processId) throws SourceFilesException {
return getProcessSourceFiles(processId).stream().map(SourceFile::getUri).filter(this::isValidDefinitionSource).findFirst().flatMap(this::readFileContentFromClassPath);
}

private boolean isValidDefinitionSource(String uri) {
if (ProcessCodegen.SUPPORTED_BPMN_EXTENSIONS.stream().noneMatch(uri::endsWith)) {
return ProcessCodegen.SUPPORTED_SW_EXTENSIONS.keySet().stream().anyMatch(uri::endsWith);
}
return true;
}

public void clear() {
sourceFiles.clear();
private Optional<String> readFileContentFromClassPath(String relativeFileURI) {
try (InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream("META-INF/resources" + relativeFileURI)) {
return Optional.of(IOUtils.toString(is, StandardCharsets.UTF_8.name()));
} catch (Exception ex) {
throw new SourceFilesException("Exception trying to read definition source file with relative URI:" + relativeFileURI, ex);
}
}
}
Expand Up @@ -15,46 +15,93 @@
*/
package org.kie.kogito.addon.source.files;

import java.io.IOException;
import java.net.URISyntaxException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

class SourceFilesProviderImplTest {

private SourceFilesProviderImpl sourceFilesProvider;

private static String readFileContent(String file) throws URISyntaxException, IOException {
Path path = Paths.get(Thread.currentThread().getContextClassLoader().getResource(file).toURI());
return Files.readString(path);
}

private String getTestFileContentByFilename(String fileName) throws Exception {
return readFileContent("META-INF/resources/sources/" + fileName);
}

@BeforeEach
public void setup() {
sourceFilesProvider = new SourceFilesProviderImpl();
}

@Test
void addSourceFile() {
SourceFilesProviderImpl sourceFilesProvider = new SourceFilesProviderImpl();
sourceFilesProvider.addSourceFile("a_process", new SourceFile("myworkflow.sw.json"));

assertThat(sourceFilesProvider.getSourceFiles("a_process"))
assertThat(sourceFilesProvider.getProcessSourceFiles("a_process"))
.contains(new SourceFile("myworkflow.sw.json"));
}

@Test
void getSourceFilesByProcessId() {
SourceFilesProviderImpl sourceFilesProvider = new SourceFilesProviderImpl();
sourceFilesProvider.addSourceFile("a_process", new SourceFile("myworkflow.sw.json"));
sourceFilesProvider.addSourceFile("a_process", new SourceFile("myworkflow.sw.yaml"));

sourceFilesProvider.addSourceFile("another_process", new SourceFile("myanotherworkflow.sw.json"));
sourceFilesProvider.addSourceFile("another_process", new SourceFile("myanotherworkflow.sw.yaml"));

assertThat(sourceFilesProvider.getSourceFiles("a_process"))
assertThat(sourceFilesProvider.getProcessSourceFiles("a_process"))
.containsExactlyInAnyOrder(
new SourceFile("myworkflow.sw.json"),
new SourceFile("myworkflow.sw.yaml"));

assertThat(sourceFilesProvider.getSourceFiles("another_process"))
assertThat(sourceFilesProvider.getProcessSourceFiles("another_process"))
.containsExactlyInAnyOrder(
new SourceFile("myanotherworkflow.sw.json"),
new SourceFile("myanotherworkflow.sw.yaml"));
}

@Test
void getSourceFilesByProcessIdShouldNotReturnNull() {
SourceFilesProviderImpl sourceFilesProvider = new SourceFilesProviderImpl();

assertThat(sourceFilesProvider.getSourceFiles("a_process"))
assertThat(sourceFilesProvider.getProcessSourceFiles("a_process"))
.isEmpty();
}
}

@Test
void getValidSourceFileDefinitionByProcessIdTest() throws Exception {
sourceFilesProvider.addSourceFile("petstore_json_process", new SourceFile("petstore.json"));
sourceFilesProvider.addSourceFile("petstore_sw_json_process", new SourceFile("petstore.sw.json"));
sourceFilesProvider.addSourceFile("ymlgreet.sw_process", new SourceFile("ymlgreet.sw.yml"));
sourceFilesProvider.addSourceFile("bpmn_process", new SourceFile("hiring.bpmn"));

assertThat(sourceFilesProvider.getProcessSourceFile("petstore_sw_json_process")).contains(getTestFileContentByFilename("petstore.sw.json"));
assertThat(sourceFilesProvider.getProcessSourceFile("ymlgreet.sw_process")).contains(getTestFileContentByFilename("ymlgreet.sw.yml"));
assertThat(sourceFilesProvider.getProcessSourceFile("bpmn_process")).contains(getTestFileContentByFilename("hiring.bpmn"));
}

@Test
void getInvalidSourceFileDefinitionByProcessIdTest() {
sourceFilesProvider.addSourceFile("petstore_json_process", new SourceFile("petstore.json"));

//invalid extension
assertThat(sourceFilesProvider.getProcessSourceFile("petstore_json_process")).isEmpty();
//invalid process
assertThat(sourceFilesProvider.getProcessSourceFile("invalidProcess")).isEmpty();
// Unable to find referenced file with valid extension
sourceFilesProvider.addSourceFile("unexistingFile_sw_json_process", new SourceFile("unexistingFile.sw.json"));
assertThatThrownBy(() -> sourceFilesProvider.getProcessSourceFile("unexistingFile_sw_json_process"))
.isInstanceOf(SourceFilesException.class)
.hasMessage("Exception trying to read definition source file with relative URI:/sources/unexistingFile.sw.json");
}
}

0 comments on commit 0fa3fbc

Please sign in to comment.