Skip to content

Instantly share code, notes, and snippets.

@christophd
Created February 1, 2019 07:38
Show Gist options
  • Save christophd/6101f20875738f4213cc5faeb5845fb4 to your computer and use it in GitHub Desktop.
Save christophd/6101f20875738f4213cc5faeb5845fb4 to your computer and use it in GitHub Desktop.
A Citrus test to call Syndesis test web hook.
/*
* Copyright (C) 2016 Red Hat, Inc.
*
* 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 io.syndesis.connector.sheets;
import javax.net.ssl.SSLContext;
import java.security.KeyManagementException;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import com.consol.citrus.annotations.CitrusResource;
import com.consol.citrus.annotations.CitrusTest;
import com.consol.citrus.dsl.endpoint.CitrusEndpoints;
import com.consol.citrus.dsl.junit.JUnit4CitrusTest;
import com.consol.citrus.dsl.runner.TestRunner;
import com.consol.citrus.http.client.HttpClient;
import org.apache.http.conn.ssl.NoopHostnameVerifier;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.conn.ssl.TrustAllStrategy;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.ssl.SSLContexts;
import org.junit.Test;
import org.springframework.beans.factory.BeanCreationException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpStatus;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.test.context.ContextConfiguration;
/**
* @author Christoph Deppisch
*/
@ContextConfiguration(classes = SyndesisWebHookClientTest.EndpointConfig.class)
public class SyndesisWebHookClientTest extends JUnit4CitrusTest {
@Autowired
private HttpClient syndesisWebHookClient;
@Test
@CitrusTest
public void testWebHook(@CitrusResource TestRunner runner) {
runner.http(builder -> builder.client(syndesisWebHookClient)
.send()
.post()
.payload("{\"first_name\":\"John\",\"company\":\"incorrect company\"}"));
runner.http(builder -> builder.client(syndesisWebHookClient)
.receive()
.response(HttpStatus.NO_CONTENT));
}
@Configuration
public static class EndpointConfig {
@Bean
public HttpClient syndesisWebHookClient() {
return CitrusEndpoints.http().client()
.requestUrl("https://i-webhook-to-db-syndesis.192.168.64.12.nip.io/webhook/test-webhook")
.requestFactory(sslRequestFactory())
.build();
}
@Bean
public HttpComponentsClientHttpRequestFactory sslRequestFactory() {
return new HttpComponentsClientHttpRequestFactory(httpClient());
}
@Bean
public org.apache.http.client.HttpClient httpClient() {
try {
SSLContext sslcontext = SSLContexts.custom()
.loadTrustMaterial(new TrustAllStrategy())
.build();
SSLConnectionSocketFactory sslSocketFactory = new SSLConnectionSocketFactory(
sslcontext, NoopHostnameVerifier.INSTANCE);
return HttpClients.custom()
.setSSLSocketFactory(sslSocketFactory)
.setSSLHostnameVerifier(NoopHostnameVerifier.INSTANCE)
.build();
} catch (NoSuchAlgorithmException | KeyManagementException | KeyStoreException e) {
throw new BeanCreationException("Failed to create http client for ssl connection", e);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment