Skip to content

Commit

Permalink
fix(#90): Inject environment variables as test variables before scenario
Browse files Browse the repository at this point in the history
  • Loading branch information
christophd committed Apr 24, 2020
1 parent 1c7853f commit 7cbed0b
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 )\"(.+)\"$")
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,6 @@ Feature: Standard steps
"""
YAKS rocks!
"""

Scenario: should inject env vars
Given print '${CLUSTER_WILDCARD_DOMAIN}'

0 comments on commit 7cbed0b

Please sign in to comment.