Skip to content

Commit

Permalink
System.properties not recognized if set in Test Resource - part 2 fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
zbendhiba authored and jamesnetherton committed May 14, 2021
1 parent 6b3abe4 commit de42e7d
Show file tree
Hide file tree
Showing 14 changed files with 66 additions and 37 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
// Empty partial for a Camel bit unsupported by Camel Quarkus to avoid warnings when this file is included from a Camel page
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import com.github.tomakehurst.wiremock.stubbing.StubMapping;
import io.quarkus.test.common.QuarkusTestResourceLifecycleManager;
import org.apache.camel.quarkus.test.mock.backend.MockBackendUtils;
import org.eclipse.microprofile.config.ConfigProvider;
import org.jboss.logging.Logger;

import static com.github.tomakehurst.wiremock.client.WireMock.recordSpec;
Expand Down Expand Up @@ -175,8 +176,7 @@ protected boolean envVarsPresent(String... envVarNames) {
* Get the value of a given environment variable or a default value if it does not exist
*/
protected String envOrDefault(String envVarName, String defaultValue) {
String value = System.getenv(envVarName);
return value != null ? value : defaultValue;
return ConfigProvider.getConfig().getOptionalValue(envVarName, String.class).orElse(defaultValue);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,16 @@
*/
package org.apache.camel.quarkus.component.digitalocean.it;

import java.util.Optional;

import javax.enterprise.context.ApplicationScoped;
import javax.inject.Named;
import javax.ws.rs.Produces;

import com.myjeeva.digitalocean.impl.DigitalOceanClient;
import io.quarkus.arc.Unremovable;
import org.apache.camel.builder.RouteBuilder;
import org.eclipse.microprofile.config.ConfigProvider;
import org.eclipse.microprofile.config.inject.ConfigProperty;

@ApplicationScoped
Expand All @@ -41,9 +44,9 @@ public class DigitaloceanRoute extends RouteBuilder {
@Unremovable
@Named("digitalOceanClient")
DigitalOceanClient initDigitalOceanClient(MockApiService mockApiService) {
final String wireMockUrl = System.getProperty("wiremock.url.ssl");
if (wireMockUrl != null) {
return mockApiService.createDigitalOceanClient(wireMockUrl, oAuthToken);
Optional<String> wireMockUrl = ConfigProvider.getConfig().getOptionalValue("wiremock.url.ssl", String.class);
if (wireMockUrl.isPresent()) {
return mockApiService.createDigitalOceanClient(wireMockUrl.get(), oAuthToken);
}
return new DigitalOceanClient(oAuthToken);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import io.restassured.RestAssured;
import io.restassured.http.ContentType;
import org.apache.camel.quarkus.test.wiremock.MockServer;
import org.eclipse.microprofile.config.ConfigProvider;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

Expand All @@ -53,8 +54,8 @@ public class DigitaloceanDropletTest {
public static void initTimeoutUnit() {
// add timeout if not using MockServer
// when using a Digitalocean Key, it takes at least 2 minutes to create a droplet or snapshot
String key = System.getenv("DIGITALOCEAN_AUTH_TOKEN");
if (key != null) {
Optional<String> key = ConfigProvider.getConfig().getOptionalValue("DIGITALOCEAN_AUTH_TOKEN", String.class);
if (key.isPresent()) {
timeoutUnit = TimeUnit.MINUTES;
waitBlockStorageAction = true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import io.restassured.RestAssured;
import io.restassured.http.ContentType;
import org.apache.camel.quarkus.test.wiremock.MockServer;
import org.eclipse.microprofile.config.ConfigProvider;
import org.hamcrest.CoreMatchers;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
Expand All @@ -46,12 +47,8 @@ class DigitaloceanTest {

@BeforeAll
public static void initPublicKey() {
String key = System.getenv("DIGITALOCEAN_PUBLIC_KEY");
if (key != null) {
publicKey = key;
} else {
publicKey = "ssh-rsa AEXAMPLEaC1yc2EAAAADAQABAAAAQQDDHr/jh2Jy4yALcK4JyWbVkPRaWmhck3IgCoeOO3z1e2dBowLh64QAM+Qb72pxekALga2oi4GvT+TlWNhzPH4V example";
}
publicKey = ConfigProvider.getConfig().getOptionalValue("DIGITALOCEAN_PUBLIC_KEY", String.class).orElse(
"ssh-rsa AEXAMPLEaC1yc2EAAAADAQABAAAAQQDDHr/jh2Jy4yALcK4JyWbVkPRaWmhck3IgCoeOO3z1e2dBowLh64QAM+Qb72pxekALga2oi4GvT+TlWNhzPH4V example");
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,16 @@
*/
package org.apache.camel.quarkus.component.geocoder.it;

import java.util.Optional;

import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.inject.Produces;
import javax.inject.Named;

import io.quarkus.arc.Unremovable;
import org.apache.camel.CamelContext;
import org.apache.camel.component.geocoder.GeoCoderComponent;
import org.eclipse.microprofile.config.ConfigProvider;
import org.eclipse.microprofile.config.inject.ConfigProperty;

@ApplicationScoped
Expand All @@ -43,12 +46,12 @@ public class GeocoderProducers {
@Named("geocoder")
GeoCoderComponent geocoderComponent(CamelContext camelContext, MockApiService mockApiService)
throws IllegalAccessException, NoSuchFieldException, InstantiationException {
final String wireMockUrl = System.getProperty("wiremock.url");
final Optional<String> wireMockUrl = ConfigProvider.getConfig().getOptionalValue("wiremock.url", String.class);
final GeoCoderComponent result = new GeoCoderComponent();
result.setCamelContext(camelContext);

if (wireMockUrl != null) {
result.setGeoApiContext(mockApiService.createGeoApiContext(wireMockUrl, googleApiKey));
if (wireMockUrl.isPresent()) {
result.setGeoApiContext(mockApiService.createGeoApiContext(wireMockUrl.get(), googleApiKey));
}
return result;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import io.quarkus.test.common.http.TestHTTPEndpoint;
import io.quarkus.test.junit.QuarkusTest;
import io.restassured.RestAssured;
import org.eclipse.microprofile.config.ConfigProvider;
import org.junit.jupiter.api.Test;

import static org.hamcrest.Matchers.equalTo;
Expand Down Expand Up @@ -94,7 +95,7 @@ private void assertAck(List<LumberjackAckResponse> ackResponseList) {
}

private List<LumberjackAckResponse> sendPayload(String portName, boolean withSsl) throws InterruptedException {
final int port = Integer.getInteger(portName);
final int port = ConfigProvider.getConfig().getValue(portName, Integer.class);
List<LumberjackAckResponse> ackResponseList = LumberjackClientUtil.sendMessages(port, withSsl);
return ackResponseList;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

import javax.inject.Inject;
import javax.inject.Named;
Expand All @@ -45,6 +46,7 @@
import org.apache.camel.Exchange;
import org.apache.camel.ProducerTemplate;
import org.apache.camel.component.pubnub.PubNubConstants;
import org.eclipse.microprofile.config.ConfigProvider;
import org.eclipse.microprofile.config.inject.ConfigProperty;

@Path("/pubnub")
Expand Down Expand Up @@ -162,9 +164,9 @@ public PubNub pubNub() {
configuration.setSubscribeKey(subscribeKey);
configuration.setSecretKey(secretKey);

String url = System.getProperty("pubnub.url");
if (url != null) {
configuration.setOrigin(url);
Optional<String> url = ConfigProvider.getConfig().getOptionalValue("pubnub.url", String.class);
if (url.isPresent()) {
configuration.setOrigin(url.get());
configuration.setSecure(false);
configuration.setReconnectionPolicy(PNReconnectionPolicy.LINEAR);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,24 +31,28 @@
import com.slack.api.model.Message;
import org.apache.camel.ConsumerTemplate;
import org.apache.camel.ProducerTemplate;
import org.eclipse.microprofile.config.inject.ConfigProperty;

@Path("/slack")
@ApplicationScoped
public class SlackResource {

private static final String SLACK_AUTH_PARAMS = "serverUrl={{sys:slack.server-url}}&token={{sys:slack.token}}";

@Inject
ProducerTemplate producerTemplate;

@Inject
ConsumerTemplate consumerTemplate;

@ConfigProperty(name = "slack.server-url")
String slackServerUrl;

@ConfigProperty(name = "slack.token")
String slackToken;

@Path("/messages")
@GET
@Produces(MediaType.TEXT_PLAIN)
public String getSlackMessages() throws Exception {
Message message = consumerTemplate.receiveBody("slack://general?maxResults=1&" + SLACK_AUTH_PARAMS,
Message message = consumerTemplate.receiveBody("slack://general?maxResults=1&" + getSlackAuthParams(),
5000L, Message.class);
return message.getText();
}
Expand All @@ -57,9 +61,13 @@ public String getSlackMessages() throws Exception {
@POST
@Consumes(MediaType.TEXT_PLAIN)
public Response createSlackMessage(String message) throws Exception {
producerTemplate.requestBody("slack://general?" + SLACK_AUTH_PARAMS, message);
producerTemplate.requestBody("slack://general?" + getSlackAuthParams(), message);
return Response
.created(new URI("https://camel.apache.org/"))
.build();
}

private String getSlackAuthParams() {
return String.format("serverUrl=%s&token=%s", slackServerUrl, slackToken);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,13 @@

import java.util.UUID;

import com.github.tomakehurst.wiremock.WireMockServer;
import io.quarkus.test.common.QuarkusTestResource;
import io.quarkus.test.junit.QuarkusTest;
import io.restassured.RestAssured;
import io.restassured.http.ContentType;
import org.apache.camel.quarkus.test.wiremock.MockServer;
import org.eclipse.microprofile.config.ConfigProvider;
import org.junit.jupiter.api.Test;

import static org.hamcrest.CoreMatchers.equalTo;
Expand All @@ -39,6 +42,9 @@
@QuarkusTestResource(SlackTestResource.class)
class SlackTest {

@MockServer
WireMockServer server;

@Test
public void testSlackProduceConsumeMessages() {
final String message = "Hello Camel Quarkus Slack" + (externalSlackEnabled() ? " " + UUID.randomUUID() : "");
Expand All @@ -56,6 +62,6 @@ public void testSlackProduceConsumeMessages() {
}

boolean externalSlackEnabled() {
return System.getProperty("wiremock.url") == null;
return !ConfigProvider.getConfig().getOptionalValue("wiremock.url", String.class).isPresent();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import org.apache.camel.quarkus.test.wiremock.WireMockTestResourceLifecycleManager;
import org.apache.camel.util.CollectionHelper;
import org.eclipse.microprofile.config.ConfigProvider;

public class SlackTestResource extends WireMockTestResourceLifecycleManager {

Expand All @@ -32,8 +33,10 @@ public class SlackTestResource extends WireMockTestResourceLifecycleManager {
public Map<String, String> start() {
Map<String, String> properties = super.start();
String wiremockUrl = properties.get("wiremock.url");
String serverUrl = wiremockUrl != null ? wiremockUrl : System.getenv(SLACK_ENV_SERVER_URL);
String webhookUrl = wiremockUrl != null ? wiremockUrl + "/services/webhook" : System.getenv(SLACK_ENV_WEBHOOK_URL);
String serverUrl = wiremockUrl != null ? wiremockUrl
: ConfigProvider.getConfig().getValue(SLACK_ENV_SERVER_URL, String.class);
String webhookUrl = wiremockUrl != null ? wiremockUrl + "/services/webhook"
: ConfigProvider.getConfig().getValue(SLACK_ENV_WEBHOOK_URL, String.class);
return CollectionHelper.mergeMaps(properties, CollectionHelper.mapOf(
"camel.component.slack.webhook-url", webhookUrl,
"slack.server-url", serverUrl,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import io.restassured.RestAssured;
import io.restassured.http.ContentType;
import org.apache.camel.util.CollectionHelper;
import org.eclipse.microprofile.config.ConfigProvider;
import org.junit.Assert;
import org.junit.jupiter.api.Test;

Expand Down Expand Up @@ -95,7 +96,7 @@ public void testWriteStreamAndReadSaved() throws InterruptedException {
//create saved search
RestAssured.given()
.baseUri("http://localhost")
.port(Integer.parseInt(System.getProperty(SplunkResource.PARAM_REMOTE_PORT)))
.port(ConfigProvider.getConfig().getValue(SplunkResource.PARAM_REMOTE_PORT, Integer.class))
.contentType(ContentType.JSON)
.param("name", SplunkTestResource.SAVED_SEARCH_NAME)
.param("disabled", "0")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import java.util.List;
import java.util.Map;
import java.util.Optional;

import javax.inject.Inject;
import javax.inject.Named;
Expand All @@ -38,6 +39,7 @@
import com.twilio.rest.api.v2010.account.Message;
import io.quarkus.arc.Unremovable;
import org.apache.camel.ProducerTemplate;
import org.eclipse.microprofile.config.ConfigProvider;

@Path("/twilio")
public class TwilioResource {
Expand Down Expand Up @@ -80,15 +82,15 @@ public Response phoneCall() throws Exception {
@Named("restClient")
public TwilioRestClient restClient() {
// If mocking is enabled, we need to ensure Twilio API calls are directed to the mock server
String wireMockUrl = System.getProperty("wiremock.url");
if (wireMockUrl != null) {
Optional<String> wireMockUrl = ConfigProvider.getConfig().getOptionalValue("wiremock.url", String.class);
if (wireMockUrl.isPresent()) {
HttpClient client = new NetworkHttpClient() {
@Override
public com.twilio.http.Response makeRequest(Request originalRequest) {
String url = originalRequest.getUrl();

Request modified = new Request(originalRequest.getMethod(),
url.replace("https://api.twilio.com", wireMockUrl));
url.replace("https://api.twilio.com", wireMockUrl.get()));

Map<String, List<String>> headerParams = originalRequest.getHeaderParams();
for (String key : headerParams.keySet()) {
Expand Down Expand Up @@ -118,9 +120,9 @@ public com.twilio.http.Response makeRequest(Request originalRequest) {
};

return new TwilioRestClient.Builder(
System.getProperty("camel.component.twilio.username"),
System.getProperty("camel.component.twilio.password"))
.accountSid(System.getProperty("camel.component.twilio.account-sid"))
ConfigProvider.getConfig().getValue("camel.component.twilio.username", String.class),
ConfigProvider.getConfig().getValue("camel.component.twilio.password", String.class))
.accountSid(ConfigProvider.getConfig().getValue("camel.component.twilio.account-sid", String.class))
.httpClient(client)
.build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import org.apache.kafka.clients.producer.internals.DefaultPartitioner;
import org.apache.kafka.common.serialization.StringDeserializer;
import org.apache.kafka.common.serialization.StringSerializer;
import org.eclipse.microprofile.config.ConfigProvider;

public class VertxKafkaProducers {

Expand All @@ -44,7 +45,7 @@ public KafkaConsumer<String, String> createKafkaConsumer(Vertx vertx) {
config.put(ConsumerConfig.AUTO_COMMIT_INTERVAL_MS_CONFIG, "1000");
config.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest");
config.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG,
System.getProperty("camel.component.vertx-kafka.bootstrap-servers"));
ConfigProvider.getConfig().getValue("camel.component.vertx-kafka.bootstrap-servers", String.class));
config.put(ConsumerConfig.ENABLE_AUTO_COMMIT_CONFIG, "true");
config.put(ConsumerConfig.GROUP_ID_CONFIG, GROUP_ID);
config.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class.getName());
Expand All @@ -62,7 +63,7 @@ public KafkaProducer<String, String> createKafkaProducer(Vertx vertx) {
Map<String, String> config = new HashMap<>();
config.put(ProducerConfig.ACKS_CONFIG, "1");
config.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG,
System.getProperty("camel.component.vertx-kafka.bootstrap-servers"));
ConfigProvider.getConfig().getValue("camel.component.vertx-kafka.bootstrap-servers", String.class));
config.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName());
config.put(ProducerConfig.PARTITIONER_CLASS_CONFIG, DefaultPartitioner.class.getName());
config.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName());
Expand Down

0 comments on commit de42e7d

Please sign in to comment.