import java.security.SecureRandom;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
+import java.util.Map;
+import java.util.Map.Entry;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
+import javax.ws.rs.client.Entity;
+import javax.ws.rs.client.Invocation.Builder;
import javax.ws.rs.core.Response;
import org.glassfish.jersey.client.authentication.HttpAuthenticationFeature;
/**
* Constructor.
*
- * name the name
- * https is it https or not
- * selfSignedCerts are there self signed certs
- * hostname the hostname
- * port port being used
- * basePath base context
- * userName user
- * password password
+ * name the name https is it https or not selfSignedCerts are there self signed certs hostname
+ * the hostname port port being used basePath base context userName user password password
+ *
* @param busTopicParams Input parameters object
* @throws KeyManagementException key exception
* @throws NoSuchAlgorithmException no algorithm exception
return this.client.target(this.baseUrl).request().get();
}
+ @Override
+ public Response put(String path, Entity<?> entity, Map<String, Object> headers) {
+ Builder builder = this.client.target(this.baseUrl).path(path).request();
+ for (Entry<String, Object> header : headers.entrySet()) {
+ builder.header(header.getKey(), header.getValue());
+ }
+ return builder.put(entity);
+ }
@Override
public boolean start() {
package org.onap.policy.common.endpoints.http.server.test;
+import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import java.io.IOException;
+import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Properties;
+import javax.ws.rs.client.Entity;
+import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import org.junit.AfterClass;
* Setup before class method.
*
* @throws InterruptedException can be interrupted
- * @throws IOException can have an IO exception
+ * @throws IOException can have an IO exception
*/
@BeforeClass
public static void setUp() throws InterruptedException, IOException {
String keyStorePasswordSystemProperty =
System.getProperty(JettyJerseyServer.SYSTEM_KEYSTORE_PASSWORD_PROPERTY_NAME);
if (keyStorePasswordSystemProperty != null) {
- savedValuesMap.put(
- JettyJerseyServer.SYSTEM_KEYSTORE_PASSWORD_PROPERTY_NAME, keyStorePasswordSystemProperty);
+ savedValuesMap.put(JettyJerseyServer.SYSTEM_KEYSTORE_PASSWORD_PROPERTY_NAME,
+ keyStorePasswordSystemProperty);
}
String trustStoreSystemProperty = System.getProperty(JettyJerseyServer.SYSTEM_TRUSTSTORE_PROPERTY_NAME);
if (trustStoreSystemProperty != null) {
- savedValuesMap
- .put(JettyJerseyServer.SYSTEM_TRUSTSTORE_PROPERTY_NAME, trustStoreSystemProperty);
+ savedValuesMap.put(JettyJerseyServer.SYSTEM_TRUSTSTORE_PROPERTY_NAME, trustStoreSystemProperty);
}
String trustStorePasswordSystemProperty =
System.getProperty(JettyJerseyServer.SYSTEM_TRUSTSTORE_PASSWORD_PROPERTY_NAME);
if (trustStorePasswordSystemProperty != null) {
- savedValuesMap
- .put(JettyJerseyServer.SYSTEM_TRUSTSTORE_PASSWORD_PROPERTY_NAME, trustStorePasswordSystemProperty);
+ savedValuesMap.put(JettyJerseyServer.SYSTEM_TRUSTSTORE_PASSWORD_PROPERTY_NAME,
+ trustStorePasswordSystemProperty);
}
System.setProperty(JettyJerseyServer.SYSTEM_KEYSTORE_PROPERTY_NAME, "src/test/resources/keystore-test");
}
@Test
- public void testHttpNoAuthClient() throws Exception {
+ public void testHttpGetNoAuthClient() throws Exception {
logger.info("-- testHttpNoAuthClient() --");
final HttpClient client = HttpClient.factory.build(BusTopicParams.builder().clientName("testHttpNoAuthClient")
- .useHttps(false)
- .allowSelfSignedCerts(false)
- .hostname("localhost")
- .port(6666).basePath("junit/echo").userName(null).password(null).managed(true).build());
+ .useHttps(false).allowSelfSignedCerts(false).hostname("localhost").port(6666).basePath("junit/echo")
+ .userName(null).password(null).managed(true).build());
final Response response = client.get("hello");
final String body = HttpClient.getBody(response, String.class);
}
@Test
- public void testHttpAuthClient() throws Exception {
+ public void testHttpPutNoAuthClient() throws Exception {
+ logger.info("-- testHttpNoAuthClient() --");
+
+ final HttpClient client = HttpClient.factory.build(BusTopicParams.builder().clientName("testHttpNoAuthClient")
+ .useHttps(false).allowSelfSignedCerts(false).hostname("localhost").port(6666).basePath("junit/echo")
+ .userName(null).password(null).managed(true).build());
+
+ Entity<MyEntity> entity = Entity.entity(new MyEntity("myValue"), MediaType.APPLICATION_JSON);
+ final Response response = client.put("hello", entity, Collections.emptyMap());
+ final String body = HttpClient.getBody(response, String.class);
+
+ assertTrue(response.getStatus() == 200);
+ assertTrue(body.equals("hello:{myParameter=myValue}"));
+ }
+
+ @Test
+ public void testHttpGetAuthClient() throws Exception {
logger.info("-- testHttpAuthClient() --");
final HttpClient client = HttpClient.factory.build(BusTopicParams.builder().clientName("testHttpAuthClient")
- .useHttps(true)
- .allowSelfSignedCerts(true)
- .hostname("localhost")
- .port(6667)
- .basePath("junit/echo")
- .userName("x")
- .password("y")
- .managed(true).build());
+ .useHttps(true).allowSelfSignedCerts(true).hostname("localhost").port(6667).basePath("junit/echo")
+ .userName("x").password("y").managed(true).build());
final Response response = client.get("hello");
final String body = HttpClient.getBody(response, String.class);
assertTrue(body.equals("hello"));
}
+ @Test
+ public void testHttpPutAuthClient() throws Exception {
+ logger.info("-- testHttpAuthClient() --");
+
+ final HttpClient client = HttpClient.factory.build(BusTopicParams.builder().clientName("testHttpAuthClient")
+ .useHttps(true).allowSelfSignedCerts(true).hostname("localhost").port(6667).basePath("junit/echo")
+ .userName("x").password("y").managed(true).build());
+
+ Entity<MyEntity> entity = Entity.entity(new MyEntity("myValue"), MediaType.APPLICATION_JSON);
+ final Response response = client.put("hello", entity, Collections.emptyMap());
+ final String body = HttpClient.getBody(response, String.class);
+
+ assertTrue(response.getStatus() == 200);
+ assertTrue(body.equals("hello:{myParameter=myValue}"));
+ }
+
@Test
public void testHttpAuthClient401() throws Exception {
logger.info("-- testHttpAuthClient401() --");
final HttpClient client = HttpClient.factory.build(BusTopicParams.builder().clientName("testHttpAuthClient401")
- .useHttps(true)
- .allowSelfSignedCerts(true)
- .hostname("localhost")
- .port(6667)
- .basePath("junit/echo")
- .userName(null)
- .password(null)
- .managed(true).build());
+ .useHttps(true).allowSelfSignedCerts(true).hostname("localhost").port(6667).basePath("junit/echo")
+ .userName(null).password(null).managed(true).build());
final Response response = client.get("hello");
- assertTrue(response.getStatus() == 401);
+ assertEquals(new Integer(response.getStatus()).toString(), response.getStatus(), 401);
}
@Test
assertTrue(response2.getStatus() == 500);
}
+ class MyEntity {
+
+ private String myParameter;
+
+ public MyEntity(final String myParameter) {
+ this.myParameter = myParameter;
+ }
+
+ public void setMyParameter(final String myParameter) {
+ this.myParameter = myParameter;
+ }
+
+ public String getMyParameter() {
+ return myParameter;
+ }
+
+ }
}