From 7cbed0b3434f47b22459f752014b226a9d9472d1 Mon Sep 17 00:00:00 2001 From: Christoph Deppisch Date: Fri, 24 Apr 2020 14:45:33 +0200 Subject: [PATCH] fix(#90): Inject environment variables as test variables before scenario --- .../yaks/http/HttpClientSteps.java | 15 ++--- .../yaks/http/http.client.feature | 3 +- .../yaks/hooks/InjectEnvVarsHook.java | 64 +++++++++++++++++++ .../yaks/standard/StandardFeatureTest.java | 5 +- .../yaks/standard/standard.feature | 3 + 5 files changed, 77 insertions(+), 13 deletions(-) create mode 100644 java/steps/yaks-standard/src/main/java/org/citrusframework/yaks/hooks/InjectEnvVarsHook.java diff --git a/java/steps/yaks-http/src/main/java/org/citrusframework/yaks/http/HttpClientSteps.java b/java/steps/yaks-http/src/main/java/org/citrusframework/yaks/http/HttpClientSteps.java index da0d32d1..1c24e425 100644 --- a/java/steps/yaks-http/src/main/java/org/citrusframework/yaks/http/HttpClientSteps.java +++ b/java/steps/yaks-http/src/main/java/org/citrusframework/yaks/http/HttpClientSteps.java @@ -18,8 +18,6 @@ package org.citrusframework.yaks.http; import javax.net.ssl.SSLContext; -import java.net.MalformedURLException; -import java.net.URL; import java.security.KeyManagementException; import java.security.KeyStoreException; import java.security.NoSuchAlgorithmException; @@ -118,16 +116,11 @@ public void setClient(String id) { @Given("^(?:URL|url): ([^\\s]+)$") public void setUrl(String url) { - try { - URL requestURL = new URL(url); - if (requestURL.getProtocol().equalsIgnoreCase("https")) { - httpClient.getEndpointConfiguration().setRequestFactory(sslRequestFactory()); - } - - this.requestUrl = url; - } catch (MalformedURLException e) { - throw new CitrusRuntimeException(e); + if (url.startsWith("https")) { + httpClient.getEndpointConfiguration().setRequestFactory(sslRequestFactory()); } + + this.requestUrl = url; } @Then("^(?:expect|verify) HTTP response header ([^\\s]+)(?:=| is )\"(.+)\"$") diff --git a/java/steps/yaks-http/src/test/resources/org/citrusframework/yaks/http/http.client.feature b/java/steps/yaks-http/src/test/resources/org/citrusframework/yaks/http/http.client.feature index 7804e958..4ffb84c0 100644 --- a/java/steps/yaks-http/src/test/resources/org/citrusframework/yaks/http/http.client.feature +++ b/java/steps/yaks-http/src/test/resources/org/citrusframework/yaks/http/http.client.feature @@ -1,7 +1,8 @@ Feature: Http client Background: - Given URL: http://localhost:8080 + Given variable port is "8080" + Given URL: http://localhost:${port} Scenario: GET When send GET /todo diff --git a/java/steps/yaks-standard/src/main/java/org/citrusframework/yaks/hooks/InjectEnvVarsHook.java b/java/steps/yaks-standard/src/main/java/org/citrusframework/yaks/hooks/InjectEnvVarsHook.java new file mode 100644 index 00000000..86a3712b --- /dev/null +++ b/java/steps/yaks-standard/src/main/java/org/citrusframework/yaks/hooks/InjectEnvVarsHook.java @@ -0,0 +1,64 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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.citrusframework.yaks.hooks; + +import com.consol.citrus.TestCaseRunner; +import com.consol.citrus.actions.AbstractTestAction; +import com.consol.citrus.annotations.CitrusResource; +import com.consol.citrus.context.TestContext; +import io.cucumber.java.Before; +import io.cucumber.java.Scenario; + +/** + * Cucumber hook injects environment variables as test variables before the scenario is executed. + * @author Christoph Deppisch + */ +public class InjectEnvVarsHook { + + @CitrusResource + private TestCaseRunner runner; + + @Before + public void injectEnvVars(Scenario scenario) { + runner.run(new AbstractTestAction() { + @Override + public void doExecute(TestContext context) { + context.setVariable("SCENARIO_NAME", scenario.getName()); + context.setVariable("SCENARIO_ID", scenario.getId()); + + injectEnvVar(context, "CLUSTER_WILDCARD_DOMAIN", "ns.svc.cluster.local"); + injectEnvVar(context, "YAKS_NAMESPACE", null); + } + }); + } + + /** + * Read System environment variable and set value as test variable using the very same variable name. In case variable + * is not present use default value if given. + * @param context + * @param env + * @param defaultValue + */ + private void injectEnvVar(TestContext context, String env, String defaultValue) { + if (System.getenv(env) != null) { + context.setVariable(env, System.getenv(env)); + } else if (defaultValue != null) { + context.setVariable(env, defaultValue); + } + } +} diff --git a/java/steps/yaks-standard/src/test/java/org/citrusframework/yaks/standard/StandardFeatureTest.java b/java/steps/yaks-standard/src/test/java/org/citrusframework/yaks/standard/StandardFeatureTest.java index f2110992..a134035e 100644 --- a/java/steps/yaks-standard/src/test/java/org/citrusframework/yaks/standard/StandardFeatureTest.java +++ b/java/steps/yaks-standard/src/test/java/org/citrusframework/yaks/standard/StandardFeatureTest.java @@ -27,7 +27,10 @@ @RunWith(Cucumber.class) @CucumberOptions( strict = true, - glue = { "org.citrusframework.yaks.standard" }, + glue = { + "org.citrusframework.yaks.hooks", + "org.citrusframework.yaks.standard" + }, plugin = { "org.citrusframework.yaks.report.TestReporter" } ) public class StandardFeatureTest { } diff --git a/java/steps/yaks-standard/src/test/resources/org/citrusframework/yaks/standard/standard.feature b/java/steps/yaks-standard/src/test/resources/org/citrusframework/yaks/standard/standard.feature index 9102949a..6aee7113 100644 --- a/java/steps/yaks-standard/src/test/resources/org/citrusframework/yaks/standard/standard.feature +++ b/java/steps/yaks-standard/src/test/resources/org/citrusframework/yaks/standard/standard.feature @@ -9,3 +9,6 @@ Feature: Standard steps """ YAKS rocks! """ + + Scenario: should inject env vars + Given print '${CLUSTER_WILDCARD_DOMAIN}'