Skip to content

Commit

Permalink
chore: Add ResourceUtils to resolve file resources
Browse files Browse the repository at this point in the history
- ResourceUtils automatically resolves file resources based on the feature file package
  • Loading branch information
christophd committed Apr 5, 2024
1 parent 9c112ef commit 5e54fac
Show file tree
Hide file tree
Showing 27 changed files with 242 additions and 84 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import org.citrusframework.util.FileUtils;
import org.citrusframework.yaks.camelk.actions.integration.CreateIntegrationAction;
import org.citrusframework.yaks.kubernetes.KubernetesSupport;
import org.citrusframework.yaks.util.ResourceUtils;

import static org.citrusframework.actions.CreateVariablesAction.Builder.createVariable;
import static org.citrusframework.container.Assert.Builder.assertException;
Expand Down Expand Up @@ -167,7 +168,7 @@ public void createIntegration(String name, String language, Map<String, String>
@Given("^load Camel K integration ([a-zA-Z0-9][a-zA-Z0-9-\\.]+[a-zA-Z0-9])\\.([a-z0-9-]+)$")
public void loadIntegrationFromFile(String name, String language) {
try {
createIntegration(name, language, FileUtils.readToString(FileUtils.getFileResource(name + "." + language)));
createIntegration(name, language, FileUtils.readToString(ResourceUtils.resolve(name + "." + language, context)));
} catch (IOException e) {
throw new CitrusRuntimeException(String.format("Failed to load Camel K integration from resource %s", name + "." + language), e);
}
Expand All @@ -176,7 +177,7 @@ public void loadIntegrationFromFile(String name, String language) {
@Given("^load Camel K integration ([a-zA-Z0-9][a-zA-Z0-9-\\.]+[a-zA-Z0-9])\\.([a-z0-9-]+) with configuration:?$")
public void loadIntegrationFromFile(String name, String language, Map<String, String> configuration) {
try {
createIntegration(name, language, FileUtils.readToString(FileUtils.getFileResource(name + "." + language)), configuration);
createIntegration(name, language, FileUtils.readToString(ResourceUtils.resolve(name + "." + language, context)), configuration);
} catch (IOException e) {
throw new CitrusRuntimeException(String.format("Failed to load Camel K integration from resource %s", name + "." + language), e);
}
Expand Down Expand Up @@ -283,7 +284,7 @@ private void createIntegration(String name, String language, String source, Map<
String openApiSpec = configuration.getOrDefault("openapi", "");
if (!openApiSpec.isEmpty()) {
try {
Resource file = FileUtils.getFileResource(openApiSpec);
Resource file = ResourceUtils.resolve(openApiSpec, context);
create.openApi(FileUtils.getFileName(file.getLocation()), FileUtils.readToString(file));
} catch (IOException e) {
throw new CitrusRuntimeException(String.format("Failed to read openapi spec form file path %s", openApiSpec));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,17 @@
import org.citrusframework.TestCaseRunner;
import org.citrusframework.annotations.CitrusFramework;
import org.citrusframework.annotations.CitrusResource;
import org.citrusframework.context.TestContext;
import org.citrusframework.exceptions.CitrusRuntimeException;
import org.citrusframework.spi.Resource;
import org.citrusframework.spi.Resources;
import org.citrusframework.yaks.camelk.model.Kamelet;
import org.citrusframework.yaks.camelk.model.KameletSpec;
import org.citrusframework.yaks.camelk.model.Pipe;
import org.citrusframework.yaks.camelk.model.PipeSpec;
import org.citrusframework.yaks.kafka.KafkaSettings;
import org.citrusframework.yaks.knative.KnativeSettings;
import org.citrusframework.yaks.kubernetes.KubernetesSupport;
import org.citrusframework.yaks.util.ResourceUtils;
import org.springframework.util.StringUtils;

import static org.citrusframework.actions.CreateVariablesAction.Builder.createVariable;
Expand All @@ -52,6 +53,9 @@ public class KameletSteps {
@CitrusFramework
private Citrus citrus;

@CitrusResource
private TestContext context;

private KubernetesClient k8sClient;

private String kameletApiVersion = KameletSettings.getKameletApiVersion();
Expand Down Expand Up @@ -216,7 +220,7 @@ public void bindKameletToKnativeChannel(String kameletName, String channel, Stri

@Given("^load Kamelet ([a-z0-9-]+).kamelet.yaml$")
public void loadKameletFromFile(String fileName) {
Resource resource = Resources.fromClasspath(fileName + ".kamelet.yaml");
Resource resource = ResourceUtils.resolve(fileName + ".kamelet.yaml", context);
runner.run(camelk()
.client(k8sClient)
.createKamelet(fileName)
Expand All @@ -234,7 +238,7 @@ public void loadKameletFromFile(String fileName) {

@Given("^load (?:Pipe|KameletBinding) ([a-z0-9-]+).yaml$")
public void loadPipeFromFile(String fileName) {
Resource resource = Resources.fromClasspath(fileName + ".yaml");
Resource resource = ResourceUtils.resolve(fileName + ".yaml", context);
runner.run(camelk()
.client(k8sClient)
.createPipe(fileName)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
import org.citrusframework.yaks.camelk.model.IntegrationList;
import org.citrusframework.yaks.camelk.model.IntegrationSpec;
import org.citrusframework.yaks.kubernetes.KubernetesSupport;
import org.citrusframework.yaks.util.ResourceUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -327,7 +328,7 @@ private void addPropertyConfigurationSpec(Integration.Builder integrationBuilder
for (String pf : propertyFiles){
try {
Properties props = new Properties();
props.load(FileUtils.getFileResource(context.replaceDynamicContentInString(pf)).getInputStream());
props.load(ResourceUtils.resolve(pf, context).getInputStream());
props.forEach((key, value) -> configurationList.add(
new IntegrationSpec.Configuration("property", createPropertySpec(key.toString(), value.toString(), context))));
} catch (IOException e) {
Expand Down Expand Up @@ -361,7 +362,7 @@ private void addBuildPropertyConfigurationSpec(Integration.Builder integrationBu
for (String pf : buildPropertyFiles){
try {
Properties props = new Properties();
props.load(FileUtils.getFileResource(context.replaceDynamicContentInString(pf)).getInputStream());
props.load(ResourceUtils.resolve(pf, context).getInputStream());
props.forEach((key, value) -> addTraitSpec(String.format("%s=%s",
traitName,
createPropertySpec(key.toString(), value.toString(), context)), traitConfigMap));
Expand Down
11 changes: 5 additions & 6 deletions java/steps/yaks-camel/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@
<name>YAKS :: Steps :: Apache Camel</name>

<dependencies>
<dependency>
<groupId>org.citrusframework.yaks</groupId>
<artifactId>yaks-standard</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.citrusframework.yaks</groupId>
<artifactId>yaks-groovy</artifactId>
Expand Down Expand Up @@ -81,12 +86,6 @@
</dependency>

<!-- Test scope -->
<dependency>
<groupId>org.citrusframework.yaks</groupId>
<artifactId>yaks-standard</artifactId>
<version>${project.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-junit</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,10 @@
import org.citrusframework.context.TestContext;
import org.citrusframework.exceptions.CitrusRuntimeException;
import org.citrusframework.spi.Resource;
import org.citrusframework.spi.Resources;
import org.citrusframework.util.FileUtils;
import org.citrusframework.xml.StringSource;
import org.citrusframework.yaks.groovy.GroovyShellUtils;
import org.citrusframework.yaks.util.ResourceUtils;
import org.codehaus.groovy.control.customizers.ImportCustomizer;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.GenericXmlApplicationContext;
Expand Down Expand Up @@ -187,7 +187,7 @@ public void bindComponent(String name, String configurationScript) {

@Given("^load to Camel registry ([^\"\\s]+)\\.groovy$")
public void loadComponent(String filePath) throws IOException {
Resource scriptFile = FileUtils.getFileResource(filePath + ".groovy");
Resource scriptFile = ResourceUtils.resolve(filePath + ".groovy", context);
String script = FileUtils.readToString(scriptFile);
final String fileName = FileUtils.getFileName(scriptFile.getLocation());
final String baseName = Optional.ofNullable(fileName)
Expand Down Expand Up @@ -267,7 +267,7 @@ protected void configureRoute(RouteDefinition route) {

@Given("^load Camel route ([^\\s]+)\\.(groovy|xml)")
public void loadCamelRoute(String fileName, String language) throws Exception {
String route = FileUtils.readToString(Resources.create(fileName));
String route = FileUtils.readToString(ResourceUtils.resolve(fileName, context));
switch (language) {
case "groovy":
camelRouteGroovy(fileName, route);
Expand Down Expand Up @@ -330,7 +330,7 @@ public void setExchangeBody(String body) {
@Then("^(?:expect|verify) Camel exchange body loaded from ([^\\s]+)$")
public void loadExchangeBody(String file) {
try {
this.body = FileUtils.readToString(FileUtils.getFileResource(file));
this.body = FileUtils.readToString(ResourceUtils.resolve(file, context));
} catch (IOException e) {
throw new CitrusRuntimeException(String.format("Failed to load body from file resource %s", file));
}
Expand Down
12 changes: 6 additions & 6 deletions java/steps/yaks-groovy/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@
<name>YAKS :: Steps :: Groovy</name>

<dependencies>
<dependency>
<groupId>org.citrusframework.yaks</groupId>
<artifactId>yaks-standard</artifactId>
<version>${project.version}</version>
</dependency>

<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-java</artifactId>
Expand Down Expand Up @@ -95,12 +101,6 @@
<artifactId>citrus-validation-text</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.citrusframework.yaks</groupId>
<artifactId>yaks-standard</artifactId>
<version>${project.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.citrusframework.yaks</groupId>
<artifactId>yaks-http</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import org.citrusframework.yaks.groovy.dsl.ConfigurationScript;
import org.citrusframework.yaks.groovy.dsl.actions.ActionScript;
import org.citrusframework.yaks.groovy.dsl.endpoints.EndpointConfigurationScript;
import org.citrusframework.yaks.util.ResourceUtils;
import org.codehaus.groovy.control.customizers.ImportCustomizer;

/**
Expand Down Expand Up @@ -70,7 +71,7 @@ public void createConfiguration(String config) {

@Given("^load configuration ([^\"\\s]+)\\.groovy$")
public void loadConfiguration(String filePath) throws IOException {
Resource scriptFile = FileUtils.getFileResource(filePath + ".groovy");
Resource scriptFile = ResourceUtils.resolve(filePath + ".groovy", context);
String script = FileUtils.readToString(scriptFile);
createConfiguration(script);
}
Expand All @@ -92,7 +93,7 @@ public void createEndpoint(String name, String configurationScript) {

@Given("^load endpoint ([^\"\\s]+)\\.groovy$")
public void loadEndpoint(String filePath) throws IOException {
Resource scriptFile = FileUtils.getFileResource(filePath + ".groovy");
Resource scriptFile = ResourceUtils.resolve(filePath + ".groovy", context);
String script = FileUtils.readToString(scriptFile);
final String fileName = FileUtils.getFileName(scriptFile.getLocation());
final String baseName = Optional.ofNullable(fileName)
Expand All @@ -117,7 +118,7 @@ public void createComponent(String name, String configurationScript) {

@Given("^load component ([^\"\\s]+)\\.groovy$")
public void loadComponent(String filePath) throws IOException {
Resource scriptFile = FileUtils.getFileResource(filePath + ".groovy");
Resource scriptFile = ResourceUtils.resolve(filePath + ".groovy", context);
String script = FileUtils.readToString(scriptFile);
final String fileName = FileUtils.getFileName(scriptFile.getLocation());
final String baseName = Optional.ofNullable(fileName)
Expand All @@ -135,7 +136,7 @@ public void createActionScript(String scriptName, String code) {

@Given("^load actions ([^\"\\s]+)\\.groovy$")
public void loadActionScript(String filePath) throws IOException {
Resource scriptFile = FileUtils.getFileResource(filePath + ".groovy");
Resource scriptFile = ResourceUtils.resolve(filePath + ".groovy", context);
String script = FileUtils.readToString(scriptFile);
final String fileName = FileUtils.getFileName(scriptFile.getLocation());
final String baseName = Optional.ofNullable(fileName)
Expand Down Expand Up @@ -183,7 +184,7 @@ public void applyScriptMultiline(String script) {

@Given("^(?:apply|run) script ([^\"\\s]+)\\.groovy$")
public void applyScriptFile(String filePath) throws IOException {
Resource scriptFile = FileUtils.getFileResource(filePath + ".groovy");
Resource scriptFile = ResourceUtils.resolve(filePath + ".groovy", context);
applyScript(context.replaceDynamicContentInString(FileUtils.readToString(scriptFile)));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -95,4 +95,17 @@ public static String removeComments(String script) {
return script.trim();
}
}

/**
* Remove package declaration.
* @param script
* @return
*/
public static String removePackageDeclaration(String script) {
if (script.startsWith("package ")) {
return script.substring(script.indexOf("\n")).trim();
} else {
return script.trim();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public void execute(TestActionRunner runner) {

private String normalize(String script) {
String normalized = GroovyShellUtils.removeComments(script);
normalized = GroovyShellUtils.removePackageDeclaration(normalized);

if (isActionScript(normalized)) {
if (normalized.startsWith("$(")) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
import org.citrusframework.http.message.HttpMessage;
import org.citrusframework.util.FileUtils;
import org.citrusframework.variable.dictionary.DataDictionary;
import org.citrusframework.yaks.util.ResourceUtils;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
Expand Down Expand Up @@ -285,7 +286,7 @@ public void setRequestBodyMultiline(String body) {
@Given("^load HTTP request body ([^\\s]+)$")
public void loadRequestBody(String file) {
try {
setRequestBody(FileUtils.readToString(FileUtils.getFileResource(file)));
setRequestBody(FileUtils.readToString(ResourceUtils.resolve(file, context)));
} catch (IOException e) {
throw new CitrusRuntimeException(String.format("Failed to load body from file resource %s", file));
}
Expand All @@ -304,7 +305,7 @@ public void setResponseBodyMultiline(String body) {
@Given("^(?:expect|verify) HTTP response body loaded from ([^\\s]+)$")
public void loadResponseBody(String file) {
try {
setResponseBody(FileUtils.readToString(FileUtils.getFileResource(file)));
setResponseBody(FileUtils.readToString(ResourceUtils.resolve(file, context)));
} catch (IOException e) {
throw new CitrusRuntimeException(String.format("Failed to load body from file resource %s", file));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
import org.citrusframework.http.server.HttpServerBuilder;
import org.citrusframework.util.FileUtils;
import org.citrusframework.variable.dictionary.DataDictionary;
import org.citrusframework.yaks.util.ResourceUtils;
import org.eclipse.jetty.http.HttpVersion;
import org.eclipse.jetty.server.HttpConfiguration;
import org.eclipse.jetty.server.HttpConnectionFactory;
Expand Down Expand Up @@ -318,7 +319,7 @@ public void setResponseBodyMultiline(String body) {
@Given("^load HTTP response body ([^\\s]+)$")
public void loadResponseBody(String file) {
try {
setResponseBody(FileUtils.readToString(FileUtils.getFileResource(file)));
setResponseBody(FileUtils.readToString(ResourceUtils.resolve(file, context)));
} catch (IOException e) {
throw new CitrusRuntimeException(String.format("Failed to load body from file resource %s", file));
}
Expand All @@ -337,7 +338,7 @@ public void setRequestBodyMultiline(String body) {
@Then("^(?:expect|verify) HTTP request body loaded from ([^\\s]+)$")
public void loadRequestBody(String file) {
try {
setRequestBody(FileUtils.readToString(FileUtils.getFileResource(file)));
setRequestBody(FileUtils.readToString(ResourceUtils.resolve(file, context)));
} catch (IOException e) {
throw new CitrusRuntimeException(String.format("Failed to load body from file resource %s", file));
}
Expand Down Expand Up @@ -566,12 +567,12 @@ private String getKeyStorePathPath() throws IOException {
if (sslKeyStorePath.equals(HttpSettings.SECURE_KEYSTORE_PATH_DEFAULT)) {
File tmpKeyStore = File.createTempFile("http-server", ".jks");

try (InputStream in = FileUtils.getFileResource(sslKeyStorePath).getInputStream()) {
try (InputStream in = ResourceUtils.resolve(sslKeyStorePath, context).getInputStream()) {
Files.copy(in, tmpKeyStore.toPath(), StandardCopyOption.REPLACE_EXISTING);
return tmpKeyStore.getPath();
}
} else {
return FileUtils.getFileResource(context.replaceDynamicContentInString(sslKeyStorePath)).getLocation();
return ResourceUtils.resolve(sslKeyStorePath, context).getLocation();
}
}

Expand Down
12 changes: 6 additions & 6 deletions java/steps/yaks-jms/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@
<name>YAKS :: Steps :: JMS</name>

<dependencies>
<dependency>
<groupId>org.citrusframework.yaks</groupId>
<artifactId>yaks-standard</artifactId>
<version>${project.version}</version>
</dependency>

<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-java</artifactId>
Expand Down Expand Up @@ -81,12 +87,6 @@
<artifactId>citrus-validation-text</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.citrusframework.yaks</groupId>
<artifactId>yaks-standard</artifactId>
<version>${project.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>artemis-jakarta-server</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import org.citrusframework.jms.endpoint.JmsEndpointBuilder;
import org.citrusframework.util.FileUtils;
import org.citrusframework.yaks.jms.connection.ConnectionFactoryCreator;
import org.citrusframework.yaks.util.ResourceUtils;

import static org.citrusframework.actions.ReceiveMessageAction.Builder.receive;
import static org.citrusframework.actions.SendMessageAction.Builder.send;
Expand Down Expand Up @@ -169,7 +170,7 @@ public void setMessageBodyMultiline(String body) {
@Given("^(?:expect|verify) (?:JMS|jms) message body loaded from ([^\\s]+)$")
public void loadMessageBody(String file) {
try {
setMessageBody(FileUtils.readToString(FileUtils.getFileResource(file)));
setMessageBody(FileUtils.readToString(ResourceUtils.resolve(file, context)));
} catch (IOException e) {
throw new CitrusRuntimeException(String.format("Failed to load body from file resource %s", file));
}
Expand Down
Loading

0 comments on commit 5e54fac

Please sign in to comment.