summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--controlloop/common/model-impl/rest/src/main/java/org/onap/policy/rest/RESTManager.java182
-rwxr-xr-xcontrolloop/common/model-impl/rest/src/test/java/org/onap/policy/rest/TestGet.java52
-rwxr-xr-xcontrolloop/common/model-impl/rest/src/test/java/org/onap/policy/rest/TestPair.java42
-rwxr-xr-xcontrolloop/common/model-impl/rest/src/test/java/org/onap/policy/rest/TestPost.java42
-rw-r--r--controlloop/common/model-impl/trafficgenerator/src/main/java/org/onap/policy/vnf/trafficgenerator/PGRequest.java19
-rw-r--r--controlloop/common/model-impl/trafficgenerator/src/main/java/org/onap/policy/vnf/trafficgenerator/PGStream.java22
-rw-r--r--controlloop/common/model-impl/trafficgenerator/src/main/java/org/onap/policy/vnf/trafficgenerator/PGStreams.java18
-rw-r--r--controlloop/common/model-impl/trafficgenerator/src/main/java/org/onap/policy/vnf/trafficgenerator/util/Serialization.java9
-rw-r--r--controlloop/common/model-impl/trafficgenerator/src/test/java/org/onap/policy/vnf/trafficgenerator/TestDemo.java39
9 files changed, 285 insertions, 140 deletions
diff --git a/controlloop/common/model-impl/rest/src/main/java/org/onap/policy/rest/RESTManager.java b/controlloop/common/model-impl/rest/src/main/java/org/onap/policy/rest/RESTManager.java
index c74a75cbe..2540cb27a 100644
--- a/controlloop/common/model-impl/rest/src/main/java/org/onap/policy/rest/RESTManager.java
+++ b/controlloop/common/model-impl/rest/src/main/java/org/onap/policy/rest/RESTManager.java
@@ -22,6 +22,7 @@ package org.onap.policy.rest;
import java.io.IOException;
import java.util.Map;
+import java.util.Map.Entry;
import org.apache.http.HttpResponse;
import org.apache.http.auth.AuthScope;
@@ -39,89 +40,100 @@ import org.slf4j.LoggerFactory;
public class RESTManager {
- private static final Logger logger = LoggerFactory.getLogger(RESTManager.class);
-
- public class Pair<A, B> {
- public final A a;
- public final B b;
-
- public Pair(A a, B b) {
- this.a = a;
- this.b = b;
- }
- }
-
- public Pair<Integer, String> post(String url, String username, String password, Map<String, String> headers, String contentType, String body) {
- CredentialsProvider credentials = new BasicCredentialsProvider();
- credentials.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(username, password));
-
- logger.debug("HTTP REQUEST: {} -> {} {} -> {}", url, username, ((password!=null)?password.length():"-"), contentType);
- if (headers != null) {
- logger.debug("Headers: ");
- headers.forEach((name, value) -> {
- logger.debug("{} -> {}", name, value);
- });
- }
- logger.debug(body);
-
- try (CloseableHttpClient client = HttpClientBuilder.create().setDefaultCredentialsProvider(credentials).build()) {
-
- HttpPost post = new HttpPost(url);
- if (headers != null) {
- for (String key : headers.keySet()) {
- post.addHeader(key, headers.get(key));
- }
- }
- post.addHeader("Content-Type", contentType);
-
- StringEntity input = new StringEntity(body);
- input.setContentType(contentType);
- post.setEntity(input);
-
- HttpResponse response = client.execute(post);
- if (response != null) {
- String returnBody = EntityUtils.toString(response.getEntity(), "UTF-8");
- logger.debug("HTTP POST Response Status Code: {}", response.getStatusLine().getStatusCode());
- logger.debug("HTTP POST Response Body:");
- logger.debug(returnBody);
-
- return new Pair<Integer, String>(response.getStatusLine().getStatusCode(), returnBody);
- } else {
- logger.error("Response from {} is null", url);
- return null;
- }
- } catch (Exception e) {
- logger.error("Failed to POST to {}",url,e);
- return null;
- }
- }
-
- public Pair<Integer, String> get(String url, String username, String password, Map<String, String> headers) {
-
- CredentialsProvider credentials = new BasicCredentialsProvider();
- credentials.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(username, password));
-
- try (CloseableHttpClient client = HttpClientBuilder.create().setDefaultCredentialsProvider(credentials).build()) {
-
- HttpGet get = new HttpGet(url);
- if (headers != null) {
- for (String key : headers.keySet()) {
- get.addHeader(key, headers.get(key));
- }
- }
-
- HttpResponse response = client.execute(get);
-
- String returnBody = EntityUtils.toString(response.getEntity(), "UTF-8");
-
- logger.debug("HTTP GET Response Status Code: {}", response.getStatusLine().getStatusCode());
- logger.debug("HTTP GET Response Body:");
- logger.debug(returnBody);
-
- return new Pair<Integer, String>(response.getStatusLine().getStatusCode(), returnBody);
- } catch (IOException e) {
- logger.error("Failed to GET to {}",url,e);
- return null;
- }
- }
+ private static final Logger logger = LoggerFactory.getLogger(RESTManager.class);
+
+ public class Pair<A, B> {
+ public final A a;
+ public final B b;
+
+ public Pair(A a, B b) {
+ this.a = a;
+ this.b = b;
+ }
+ }
+
+ public Pair<Integer, String> post(String url, String username, String password,
+ Map<String, String> headers, String contentType, String body) {
+ CredentialsProvider credentials = new BasicCredentialsProvider();
+ credentials.setCredentials(AuthScope.ANY,
+ new UsernamePasswordCredentials(username, password));
+
+ logger.debug("HTTP REQUEST: {} -> {} {} -> {}", url, username,
+ ((password != null) ? password.length() : "-"), contentType);
+ if (headers != null) {
+ logger.debug("Headers: ");
+ headers.forEach((name, value) -> logger.debug("{} -> {}", name, value));
+ }
+ logger.debug(body);
+
+ try (CloseableHttpClient client =
+ HttpClientBuilder.create().setDefaultCredentialsProvider(credentials).build()) {
+
+ HttpPost post = new HttpPost(url);
+ if (headers != null) {
+ for (Entry<String, String> entry : headers.entrySet()) {
+ post.addHeader(entry.getKey(), headers.get(entry.getKey()));
+ }
+ }
+ post.addHeader("Content-Type", contentType);
+
+ StringEntity input = new StringEntity(body);
+ input.setContentType(contentType);
+ post.setEntity(input);
+
+ HttpResponse response = client.execute(post);
+ if (response != null) {
+ String returnBody = EntityUtils.toString(response.getEntity(), "UTF-8");
+ logger.debug("HTTP POST Response Status Code: {}",
+ response.getStatusLine().getStatusCode());
+ logger.debug("HTTP POST Response Body:");
+ logger.debug(returnBody);
+
+ return new Pair<>(response.getStatusLine().getStatusCode(),
+ returnBody);
+ }
+ else {
+ logger.error("Response from {} is null", url);
+ return null;
+ }
+ }
+ catch (Exception e) {
+ logger.error("Failed to POST to {}", url, e);
+ return null;
+ }
+ }
+
+ public Pair<Integer, String> get(String url, String username, String password,
+ Map<String, String> headers) {
+
+ CredentialsProvider credentials = new BasicCredentialsProvider();
+ credentials.setCredentials(AuthScope.ANY,
+ new UsernamePasswordCredentials(username, password));
+
+ try (CloseableHttpClient client =
+ HttpClientBuilder.create().setDefaultCredentialsProvider(credentials).build()) {
+
+ HttpGet get = new HttpGet(url);
+ if (headers != null) {
+ for (Entry<String, String> entry : headers.entrySet()) {
+ get.addHeader(entry.getKey(), headers.get(entry.getKey()));
+ }
+ }
+
+ HttpResponse response = client.execute(get);
+
+ String returnBody = EntityUtils.toString(response.getEntity(), "UTF-8");
+
+ logger.debug("HTTP GET Response Status Code: {}",
+ response.getStatusLine().getStatusCode());
+ logger.debug("HTTP GET Response Body:");
+ logger.debug(returnBody);
+
+ return new Pair<>(response.getStatusLine().getStatusCode(), returnBody);
+ }
+ catch (IOException e) {
+ logger.error("Failed to GET to {}", url, e);
+ return null;
+ }
+ }
}
diff --git a/controlloop/common/model-impl/rest/src/test/java/org/onap/policy/rest/TestGet.java b/controlloop/common/model-impl/rest/src/test/java/org/onap/policy/rest/TestGet.java
new file mode 100755
index 000000000..96dec30db
--- /dev/null
+++ b/controlloop/common/model-impl/rest/src/test/java/org/onap/policy/rest/TestGet.java
@@ -0,0 +1,52 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * rest
+ * ================================================================================
+ *
+ * ================================================================================
+ * 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.
+ * ============LICENSE_END=========================================================
+ */
+
+package org.onap.policy.rest;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+
+import org.junit.Test;
+import org.onap.policy.rest.RESTManager.Pair;
+
+public class TestGet {
+
+ @Test(expected = NullPointerException.class)
+ public void testUrlNull() {
+ RESTManager mgr = new RESTManager();
+ mgr.get(null, "user", null, null);
+ }
+
+ @Test(expected = IllegalArgumentException.class)
+ public void testUsernameNull() {
+ RESTManager mgr = new RESTManager();
+ mgr.get("nothing", null, null, null);
+ }
+
+ @Test
+ public void testUrlExampleOrg() {
+ RESTManager mgr = new RESTManager();
+
+ Pair<Integer, String> result = mgr.get("http://www.example.org", "user", null, null);
+ assertEquals((Integer)200, result.a);
+ assertTrue(result.b != null);
+ assertTrue(result.b.length() > 0);
+ }
+} \ No newline at end of file
diff --git a/controlloop/common/model-impl/rest/src/test/java/org/onap/policy/rest/TestPair.java b/controlloop/common/model-impl/rest/src/test/java/org/onap/policy/rest/TestPair.java
new file mode 100755
index 000000000..3ada0a74e
--- /dev/null
+++ b/controlloop/common/model-impl/rest/src/test/java/org/onap/policy/rest/TestPair.java
@@ -0,0 +1,42 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * rest
+ * ================================================================================
+ *
+ * ================================================================================
+ * 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.
+ * ============LICENSE_END=========================================================
+ */
+
+package org.onap.policy.rest;
+
+import static org.junit.Assert.assertEquals;
+
+import org.junit.Test;
+import org.onap.policy.rest.RESTManager.Pair;
+
+public class TestPair {
+
+ @Test
+ public void testPair() {
+ RESTManager mgr = new RESTManager();
+
+ Pair<Integer, Integer> pii = mgr.new Pair<>(1, 2);
+ assertEquals((Integer) 1, (Integer) pii.a);
+ assertEquals((Integer) 2, (Integer) pii.b);
+
+ Pair<Integer, String> pis = mgr.new Pair<>(1, "test");
+ assertEquals((Integer) 1, (Integer) pis.a);
+ assertEquals("test", pis.b);
+ }
+}
diff --git a/controlloop/common/model-impl/rest/src/test/java/org/onap/policy/rest/TestPost.java b/controlloop/common/model-impl/rest/src/test/java/org/onap/policy/rest/TestPost.java
new file mode 100755
index 000000000..40f1b3089
--- /dev/null
+++ b/controlloop/common/model-impl/rest/src/test/java/org/onap/policy/rest/TestPost.java
@@ -0,0 +1,42 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * rest
+ * ================================================================================
+ *
+ * ================================================================================
+ * 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.
+ * ============LICENSE_END=========================================================
+ */
+
+package org.onap.policy.rest;
+
+import static org.junit.Assert.assertEquals;
+
+import org.junit.Test;
+import org.onap.policy.rest.RESTManager.Pair;
+
+public class TestPost {
+
+ @Test(expected = IllegalArgumentException.class)
+ public void testUsernameNull() {
+ RESTManager mgr = new RESTManager();
+ mgr.post("nothing", null, null, null, null, null);
+ }
+
+ @Test
+ public void testBodyNull() {
+ RESTManager mgr = new RESTManager();
+ Pair<Integer, String> result = mgr.post("http://www.example.org", "user", null, null, null, null);
+ assertEquals(null, result);
+ }
+}
diff --git a/controlloop/common/model-impl/trafficgenerator/src/main/java/org/onap/policy/vnf/trafficgenerator/PGRequest.java b/controlloop/common/model-impl/trafficgenerator/src/main/java/org/onap/policy/vnf/trafficgenerator/PGRequest.java
index d08d9422e..7bbaf3cbf 100644
--- a/controlloop/common/model-impl/trafficgenerator/src/main/java/org/onap/policy/vnf/trafficgenerator/PGRequest.java
+++ b/controlloop/common/model-impl/trafficgenerator/src/main/java/org/onap/policy/vnf/trafficgenerator/PGRequest.java
@@ -22,20 +22,19 @@ package org.onap.policy.vnf.trafficgenerator;
import java.io.Serializable;
-
import com.google.gson.annotations.SerializedName;
public class PGRequest implements Serializable {
- /**
- *
- */
- private static final long serialVersionUID = -3283942659786236032L;
-
- @SerializedName("pg-streams")
- public PGStreams pgStreams;
+ /**
+ *
+ */
+ private static final long serialVersionUID = -3283942659786236032L;
- public PGRequest() {
- }
+ @SerializedName("pg-streams")
+ public PGStreams pgStreams;
+ public PGRequest() {
+ //required by author
+ }
}
diff --git a/controlloop/common/model-impl/trafficgenerator/src/main/java/org/onap/policy/vnf/trafficgenerator/PGStream.java b/controlloop/common/model-impl/trafficgenerator/src/main/java/org/onap/policy/vnf/trafficgenerator/PGStream.java
index de5a2c37d..a606eee4c 100644
--- a/controlloop/common/model-impl/trafficgenerator/src/main/java/org/onap/policy/vnf/trafficgenerator/PGStream.java
+++ b/controlloop/common/model-impl/trafficgenerator/src/main/java/org/onap/policy/vnf/trafficgenerator/PGStream.java
@@ -26,17 +26,17 @@ import com.google.gson.annotations.SerializedName;
public class PGStream implements Serializable {
- /**
- *
- */
- private static final long serialVersionUID = 5567635677419358210L;
-
- @SerializedName("id")
- public String streamId;
- @SerializedName("is-enabled")
- public String isEnabled;
+ /**
+ *
+ */
+ private static final long serialVersionUID = 5567635677419358210L;
- public PGStream() {
- }
+ @SerializedName("id")
+ public String streamId;
+ @SerializedName("is-enabled")
+ public String isEnabled;
+ public PGStream() {
+ //required by author
+ }
}
diff --git a/controlloop/common/model-impl/trafficgenerator/src/main/java/org/onap/policy/vnf/trafficgenerator/PGStreams.java b/controlloop/common/model-impl/trafficgenerator/src/main/java/org/onap/policy/vnf/trafficgenerator/PGStreams.java
index dc0d4084f..2264c20a9 100644
--- a/controlloop/common/model-impl/trafficgenerator/src/main/java/org/onap/policy/vnf/trafficgenerator/PGStreams.java
+++ b/controlloop/common/model-impl/trafficgenerator/src/main/java/org/onap/policy/vnf/trafficgenerator/PGStreams.java
@@ -28,15 +28,15 @@ import com.google.gson.annotations.SerializedName;
public class PGStreams implements Serializable {
- /**
- *
- */
- private static final long serialVersionUID = 5567635677419358210L;
-
- @SerializedName("pg-stream")
- public List<PGStream> pgStream= new LinkedList<PGStream>();
+ /**
+ *
+ */
+ private static final long serialVersionUID = 5567635677419358210L;
- public PGStreams() {
- }
+ @SerializedName("pg-stream")
+ public List<PGStream> pgStream = new LinkedList<>();
+ public PGStreams() {
+ // required by author
+ }
}
diff --git a/controlloop/common/model-impl/trafficgenerator/src/main/java/org/onap/policy/vnf/trafficgenerator/util/Serialization.java b/controlloop/common/model-impl/trafficgenerator/src/main/java/org/onap/policy/vnf/trafficgenerator/util/Serialization.java
index 9bfe2ffe6..3443bfcdf 100644
--- a/controlloop/common/model-impl/trafficgenerator/src/main/java/org/onap/policy/vnf/trafficgenerator/util/Serialization.java
+++ b/controlloop/common/model-impl/trafficgenerator/src/main/java/org/onap/policy/vnf/trafficgenerator/util/Serialization.java
@@ -25,11 +25,8 @@ import com.google.gson.GsonBuilder;
public final class Serialization {
- private Serialization(){
- }
+ public static final Gson gsonPretty =
+ new GsonBuilder().disableHtmlEscaping().setPrettyPrinting().create();
- final static public Gson gsonPretty = new GsonBuilder().disableHtmlEscaping()
- .setPrettyPrinting()
-// .registerTypeAdapter(AAIQueryParameters.class, new notificationTypeAdapter())
- .create();
+ private Serialization() {}
}
diff --git a/controlloop/common/model-impl/trafficgenerator/src/test/java/org/onap/policy/vnf/trafficgenerator/TestDemo.java b/controlloop/common/model-impl/trafficgenerator/src/test/java/org/onap/policy/vnf/trafficgenerator/TestDemo.java
index 4b3599d43..0ae5f134e 100644
--- a/controlloop/common/model-impl/trafficgenerator/src/test/java/org/onap/policy/vnf/trafficgenerator/TestDemo.java
+++ b/controlloop/common/model-impl/trafficgenerator/src/test/java/org/onap/policy/vnf/trafficgenerator/TestDemo.java
@@ -30,24 +30,25 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class TestDemo {
- private static final Logger logger = LoggerFactory.getLogger(TestDemo.class);
- @Test
- public void test() {
- PGRequest request = new PGRequest();
- request.pgStreams = new PGStreams();
-
- PGStream pgStream;
- for(int i = 0; i < 5; i++){
- pgStream = new PGStream();
- pgStream.streamId = "fw_udp"+(i+1);
- pgStream.isEnabled = "true";
- request.pgStreams.pgStream.add(pgStream);
- }
-
- String body = Serialization.gsonPretty.toJson(request);
- logger.debug(body);
-
- // fail("Not yet implemented");
- }
+ private static final Logger logger = LoggerFactory.getLogger(TestDemo.class);
+
+ @Test
+ public void test() {
+ PGRequest request = new PGRequest();
+ request.pgStreams = new PGStreams();
+
+ PGStream pgStream;
+ for (int i = 0; i < 5; i++) {
+ pgStream = new PGStream();
+ pgStream.streamId = "fw_udp" + (i + 1);
+ pgStream.isEnabled = "true";
+ request.pgStreams.pgStream.add(pgStream);
+ }
+
+ String body = Serialization.gsonPretty.toJson(request);
+ logger.debug(body);
+
+ // fail("Not yet implemented");
+ }
}