summaryrefslogtreecommitdiffstats
path: root/controlloop/common/model-impl
diff options
context:
space:
mode:
authorvdmeer <sven.van.der.meer@ericsson.com>2018-01-17 23:13:14 +0000
committervdmeer <sven.van.der.meer@ericsson.com>2018-01-17 23:23:50 +0000
commit6f37785a393d45d1925dc0b4dd84f0107b3aab57 (patch)
treef94180fbef46c691ccf0b42c34e47f27fc202ae9 /controlloop/common/model-impl
parentbdf5640a2c68a0899363503dafa0f7cb1557e4a6 (diff)
Fix Technical Debt, basic JUnit tests for Rest
Basic tests for get and post (not testing full user/pw/body) Issue-ID: POLICY-455 Change-Id: I186ebf969abc239e5056199c2fd8da9f3253d2fc Signed-off-by: vdmeer.sven <sven.van.der.meer@ericsson.com>
Diffstat (limited to 'controlloop/common/model-impl')
-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
4 files changed, 233 insertions, 85 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);
+ }
+}