aboutsummaryrefslogtreecommitdiffstats
path: root/models-interactions
diff options
context:
space:
mode:
Diffstat (limited to 'models-interactions')
-rw-r--r--models-interactions/model-impl/rest/pom.xml16
-rw-r--r--models-interactions/model-impl/rest/src/main/java/org/onap/policy/rest/RestManager.java21
-rw-r--r--models-interactions/model-impl/rest/src/test/java/org/onap/policy/rest/GetTest.java67
-rw-r--r--models-interactions/model-impl/rest/src/test/java/org/onap/policy/rest/HttpDeleteWithBodyTest.java7
-rw-r--r--models-interactions/model-impl/rest/src/test/java/org/onap/policy/rest/PostTest.java51
-rw-r--r--models-interactions/model-impl/rest/src/test/java/org/onap/policy/rest/PutTest.java51
-rw-r--r--models-interactions/model-impl/rest/src/test/java/org/onap/policy/rest/RestTest.java390
7 files changed, 424 insertions, 179 deletions
diff --git a/models-interactions/model-impl/rest/pom.xml b/models-interactions/model-impl/rest/pom.xml
index a37c7accf..ca15b0bf2 100644
--- a/models-interactions/model-impl/rest/pom.xml
+++ b/models-interactions/model-impl/rest/pom.xml
@@ -33,11 +33,6 @@
<dependencies>
<dependency>
- <groupId>junit</groupId>
- <artifactId>junit</artifactId>
- <scope>test</scope>
- </dependency>
- <dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<scope>provided</scope>
@@ -47,6 +42,17 @@
<artifactId>httpclient</artifactId>
<scope>provided</scope>
</dependency>
+ <dependency>
+ <groupId>junit</groupId>
+ <artifactId>junit</artifactId>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.onap.policy.common</groupId>
+ <artifactId>policy-endpoints</artifactId>
+ <version>${policy.common.version}</version>
+ <scope>test</scope>
+ </dependency>
</dependencies>
<build>
diff --git a/models-interactions/model-impl/rest/src/main/java/org/onap/policy/rest/RestManager.java b/models-interactions/model-impl/rest/src/main/java/org/onap/policy/rest/RestManager.java
index f19ed7872..2ee47a9fc 100644
--- a/models-interactions/model-impl/rest/src/main/java/org/onap/policy/rest/RestManager.java
+++ b/models-interactions/model-impl/rest/src/main/java/org/onap/policy/rest/RestManager.java
@@ -27,6 +27,7 @@ import java.util.Map.Entry;
import javax.xml.bind.DatatypeConverter;
import org.apache.http.HttpHeaders;
import org.apache.http.HttpResponse;
+import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
@@ -124,7 +125,8 @@ public class RestManager {
}
/**
- * Perform REST Delete.
+ * Perform REST Delete. <br/>
+ * <i>Note: Many REST endpoints will return a 400 error for delete requests with a non-empty body</i>
*
* @param url the url
* @param username the user name
@@ -138,8 +140,8 @@ public class RestManager {
String contentType, String body) {
HttpDeleteWithBody delete = new HttpDeleteWithBody(url);
addHeaders(delete, username, password, headers);
- delete.addHeader("Content-Type", contentType);
if (body != null && !body.isEmpty()) {
+ delete.addHeader("Content-Type", contentType);
try {
StringEntity input = new StringEntity(body);
input.setContentType(contentType);
@@ -153,6 +155,21 @@ public class RestManager {
}
/**
+ * Perform REST Delete.
+ *
+ * @param url the url
+ * @param username the user name
+ * @param password the password
+ * @param headers any headers
+ * @return the response status code and the body
+ */
+ public Pair<Integer, String> delete(String url, String username, String password, Map<String, String> headers) {
+ HttpDelete delete = new HttpDelete(url);
+ addHeaders(delete, username, password, headers);
+ return sendRequest(delete);
+ }
+
+ /**
* Send REST request.
*
* @param request http request to send
diff --git a/models-interactions/model-impl/rest/src/test/java/org/onap/policy/rest/GetTest.java b/models-interactions/model-impl/rest/src/test/java/org/onap/policy/rest/GetTest.java
deleted file mode 100644
index a14d86941..000000000
--- a/models-interactions/model-impl/rest/src/test/java/org/onap/policy/rest/GetTest.java
+++ /dev/null
@@ -1,67 +0,0 @@
-/*
- * ============LICENSE_START=======================================================
- * rest
- * ================================================================================
- * Copyright (C) 2017-2019 AT&T Intellectual Property. All rights reserved.
- * Modifications Copyright (C) 2019 Nordix Foundation.
- * ================================================================================
- * 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 GetTest {
-
- @Test(expected = NullPointerException.class)
- public void testUrlNull() {
- RestManager mgr = new RestManager();
- mgr.get(null, "user", null, null);
- }
-
- @Test
- public void testUsernameNull() {
- RestManager mgr = new RestManager();
-
- Pair<Integer, String> result = mgr.get("http://www.example.org", null, null, null);
- assertEquals((Integer)200, result.first);
- assertTrue(result.second != null);
- assertTrue(result.second.length() > 0);
- }
-
- @Test
- public void testUsernameEmpty() {
- RestManager mgr = new RestManager();
-
- Pair<Integer, String> result = mgr.get("http://www.example.org", "", null, null);
- assertEquals((Integer)200, result.first);
- assertTrue(result.second != null);
- assertTrue(result.second.length() > 0);
- }
-
- @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.first);
- assertTrue(result.second != null);
- assertTrue(result.second.length() > 0);
- }
-}
diff --git a/models-interactions/model-impl/rest/src/test/java/org/onap/policy/rest/HttpDeleteWithBodyTest.java b/models-interactions/model-impl/rest/src/test/java/org/onap/policy/rest/HttpDeleteWithBodyTest.java
index 2a114f590..cfdc859b1 100644
--- a/models-interactions/model-impl/rest/src/test/java/org/onap/policy/rest/HttpDeleteWithBodyTest.java
+++ b/models-interactions/model-impl/rest/src/test/java/org/onap/policy/rest/HttpDeleteWithBodyTest.java
@@ -27,11 +27,12 @@ import org.junit.Test;
public class HttpDeleteWithBodyTest {
+ private static final String NO_URI = "BlahBlah";
+
@Test
public void getMethod() {
- String url = "http://www.example.org";
- HttpDeleteWithBody deleteWithBody = new HttpDeleteWithBody(url);
+ HttpDeleteWithBody deleteWithBody = new HttpDeleteWithBody(NO_URI);
assertEquals("DELETE", deleteWithBody.getMethod());
- assertEquals(url, deleteWithBody.getURI().toString());
+ assertEquals(NO_URI, deleteWithBody.getURI().toString());
}
} \ No newline at end of file
diff --git a/models-interactions/model-impl/rest/src/test/java/org/onap/policy/rest/PostTest.java b/models-interactions/model-impl/rest/src/test/java/org/onap/policy/rest/PostTest.java
deleted file mode 100644
index 9cda5f03f..000000000
--- a/models-interactions/model-impl/rest/src/test/java/org/onap/policy/rest/PostTest.java
+++ /dev/null
@@ -1,51 +0,0 @@
-/*
- * ============LICENSE_START=======================================================
- * rest
- * ================================================================================
- * Copyright (C) 2017-2019 AT&T Intellectual Property. All rights reserved.
- * Modifications Copyright (C) 2019 Nordix Foundation.
- * ================================================================================
- * 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 PostTest {
-
- @Test
- public void testUsernameNull() {
- RestManager mgr = new RestManager();
- Pair<Integer, String> result = mgr.post("http://www.example.org", null, null, null, null, null);
- assertEquals(null, result);
- }
-
- @Test
- public void testUsernameEmpty() {
- RestManager mgr = new RestManager();
- Pair<Integer, String> result = mgr.post("http://www.example.org", "", null, null, null, null);
- assertEquals(null, result);
- }
-
- @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/models-interactions/model-impl/rest/src/test/java/org/onap/policy/rest/PutTest.java b/models-interactions/model-impl/rest/src/test/java/org/onap/policy/rest/PutTest.java
deleted file mode 100644
index 3c7220554..000000000
--- a/models-interactions/model-impl/rest/src/test/java/org/onap/policy/rest/PutTest.java
+++ /dev/null
@@ -1,51 +0,0 @@
-/*
- * ============LICENSE_START=======================================================
- * rest
- * ================================================================================
- * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
- * ================================================================================
- * 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 PutTest {
-
- @Test
- public void testUsernameNull() {
- RestManager mgr = new RestManager();
- Pair<Integer, String> result = mgr.put("http://www.example.org", null, null, null, null, null);
- assertEquals(null, result);
- }
-
- @Test
- public void testUsernameEmpty() {
- RestManager mgr = new RestManager();
- Pair<Integer, String> result = mgr.put("http://www.example.org", "", null, null, null, null);
- assertEquals(null, result);
- }
-
- @Test
- public void testBodyNull() {
- RestManager mgr = new RestManager();
- Pair<Integer, String> result = mgr.put("http://www.example.org", "user", null, null, null, null);
- assertEquals(null, result);
- }
-}
-
diff --git a/models-interactions/model-impl/rest/src/test/java/org/onap/policy/rest/RestTest.java b/models-interactions/model-impl/rest/src/test/java/org/onap/policy/rest/RestTest.java
new file mode 100644
index 000000000..903ec2fa3
--- /dev/null
+++ b/models-interactions/model-impl/rest/src/test/java/org/onap/policy/rest/RestTest.java
@@ -0,0 +1,390 @@
+/*
+ * ============LICENSE_START=======================================================
+ * rest
+ * ================================================================================
+ * Copyright (C) 2017-2019 AT&T Intellectual Property. All rights reserved.
+ * Modifications Copyright (C) 2019 Nordix Foundation.
+ * ================================================================================
+ * 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 javax.ws.rs.Consumes;
+import javax.ws.rs.DELETE;
+import javax.ws.rs.DefaultValue;
+import javax.ws.rs.GET;
+import javax.ws.rs.POST;
+import javax.ws.rs.PUT;
+import javax.ws.rs.Path;
+import javax.ws.rs.PathParam;
+import javax.ws.rs.Produces;
+import javax.ws.rs.QueryParam;
+import javax.ws.rs.core.MediaType;
+import org.junit.AfterClass;
+import org.junit.BeforeClass;
+import org.junit.Test;
+import org.onap.policy.common.endpoints.http.server.HttpServletServer;
+import org.onap.policy.common.endpoints.http.server.HttpServletServerFactoryInstance;
+import org.onap.policy.common.utils.network.NetworkUtil;
+import org.onap.policy.rest.RestManager.Pair;
+
+@Path("RestTest")
+public class RestTest {
+
+
+ private static final String NAME_PARAM = "Bob";
+ private static final String AGE_PARAM = "10";
+ private static final String PAYLOAD = "At last! ";
+ private static final String RETURN_STRING = "Hello There ";
+ private static final String EXPECT_STRING = RETURN_STRING + NAME_PARAM + " aged " + AGE_PARAM;
+
+ private static final String LOCALHOST = "localhost";
+ private static final String BASE = "base";
+
+ private static int port;
+ private static String baseUri;
+ private static String getUri;
+ private static String deleteUri;
+ private static String putUri;
+ private static String putUriBlank;
+ private static String postUri;
+ private static String postUriBlank;
+
+ private static HttpServletServer server;
+
+ /**
+ * Sets server endpoint for the tests.
+ */
+ @BeforeClass
+ public static void setUp() throws Exception {
+
+ port = NetworkUtil.allocPort();
+ baseUri = "http://" + LOCALHOST + ":" + port + "/" + BASE + "/";
+ getUri = baseUri + "RestTest/GetHello/" + NAME_PARAM + "?age=" + AGE_PARAM;
+ deleteUri = baseUri + "RestTest/DeleteHello/" + NAME_PARAM + "?age=" + AGE_PARAM;
+ putUri = baseUri + "RestTest/PutHello/" + NAME_PARAM + "?age=" + AGE_PARAM;
+ putUriBlank = baseUri + "RestTest/PutBlank";
+ postUri = baseUri + "RestTest/PostHello/" + NAME_PARAM + "?age=" + AGE_PARAM;
+ postUriBlank = baseUri + "RestTest/PostBlank";
+
+ server = HttpServletServerFactoryInstance.getServerFactory()
+ .build("RestTest", LOCALHOST, port, "/" + BASE, false, true);
+ server.addServletClass("/*", RestTest.class.getName());
+ server.waitedStart(5000);
+
+ }
+
+ /**
+ * Tear down server endpoint for the tests.
+ *
+ * @throws Exception if there is a problem
+ */
+ @AfterClass
+ public static void tearDown() throws Exception {
+ HttpServletServerFactoryInstance.getServerFactory().destroy();
+ }
+
+ @Test(expected = NullPointerException.class)
+ public void testGetUrlNull() {
+ RestManager mgr = new RestManager();
+ mgr.get(null, "user", null, null);
+ }
+
+ @Test(expected = NullPointerException.class)
+ public void testPutUrlNull() {
+ RestManager mgr = new RestManager();
+ mgr.put(null, "user", null, null, MediaType.TEXT_PLAIN, PAYLOAD);
+ }
+
+ @Test(expected = NullPointerException.class)
+ public void testPostUrlNull() {
+ RestManager mgr = new RestManager();
+ mgr.post(null, "user", null, null, MediaType.TEXT_PLAIN, PAYLOAD);
+ }
+
+ @Test(expected = NullPointerException.class)
+ public void testDeleteUrlNull() {
+ RestManager mgr = new RestManager();
+ mgr.delete(null, "user", null, null, null, null);
+ }
+
+ @Test
+ public void testUsernameNull() {
+ RestManager mgr = new RestManager();
+
+ Pair<Integer, String> result = mgr.get(getUri, null, null, null);
+ assertEquals((Integer)200, result.first);
+ assertTrue(result.second != null);
+ assertTrue(result.second.length() > 0);
+ assertEquals("GOT: " + EXPECT_STRING, result.second);
+
+ result = mgr.delete(deleteUri, null, null, null, null, null);
+ assertEquals((Integer)200, result.first);
+ assertTrue(result.second != null);
+ assertTrue(result.second.length() > 0);
+ assertEquals("DELETE: " + EXPECT_STRING, result.second);
+
+ result = mgr.delete(deleteUri, null, null, null);
+ assertEquals((Integer)200, result.first);
+ assertTrue(result.second != null);
+ assertTrue(result.second.length() > 0);
+ assertEquals("DELETE: " + EXPECT_STRING, result.second);
+
+ result = mgr.put(putUri, null, null, null, MediaType.TEXT_PLAIN, PAYLOAD);
+ assertEquals((Integer)200, result.first);
+ assertTrue(result.second != null);
+ assertTrue(result.second.length() > 0);
+ assertEquals("PUT: " + PAYLOAD + EXPECT_STRING, result.second);
+
+ result = mgr.put(putUriBlank, null, null, null, MediaType.TEXT_PLAIN, PAYLOAD);
+ assertEquals((Integer)200, result.first);
+ assertTrue(result.second != null);
+ assertTrue(result.second.length() > 0);
+ assertEquals("PUT: " + PAYLOAD + RETURN_STRING, result.second);
+
+ result = mgr.post(postUri, null, null, null, MediaType.TEXT_PLAIN, PAYLOAD);
+ assertEquals((Integer)200, result.first);
+ assertTrue(result.second != null);
+ assertTrue(result.second.length() > 0);
+ assertEquals("POST: " + PAYLOAD + EXPECT_STRING, result.second);
+
+ result = mgr.post(postUriBlank, null, null, null, MediaType.TEXT_PLAIN, PAYLOAD);
+ assertEquals((Integer)200, result.first);
+ assertTrue(result.second != null);
+ assertTrue(result.second.length() > 0);
+ assertEquals("POST: " + PAYLOAD + RETURN_STRING, result.second);
+ }
+
+ @Test
+ public void testUsernameEmpty() {
+ RestManager mgr = new RestManager();
+
+ Pair<Integer, String> result = mgr.get(getUri, "", null, null);
+ assertEquals((Integer)200, result.first);
+ assertTrue(result.second != null);
+ assertTrue(result.second.length() > 0);
+ assertEquals("GOT: " + EXPECT_STRING, result.second);
+
+ result = mgr.delete(deleteUri, "", null, null, null, null);
+ assertEquals((Integer)200, result.first);
+ assertTrue(result.second != null);
+ assertTrue(result.second.length() > 0);
+ assertEquals("DELETE: " + EXPECT_STRING, result.second);
+
+ result = mgr.put(putUri, "", null, null, MediaType.TEXT_PLAIN, PAYLOAD);
+ assertEquals((Integer)200, result.first);
+ assertTrue(result.second != null);
+ assertTrue(result.second.length() > 0);
+ assertEquals("PUT: " + PAYLOAD + EXPECT_STRING, result.second);
+
+ result = mgr.put(putUriBlank, "", null, null, MediaType.TEXT_PLAIN, PAYLOAD);
+ assertEquals((Integer)200, result.first);
+ assertTrue(result.second != null);
+ assertTrue(result.second.length() > 0);
+ assertEquals("PUT: " + PAYLOAD + RETURN_STRING, result.second);
+
+ result = mgr.post(postUri, "", null, null, MediaType.TEXT_PLAIN, PAYLOAD);
+ assertEquals((Integer)200, result.first);
+ assertTrue(result.second != null);
+ assertTrue(result.second.length() > 0);
+ assertEquals("POST: " + PAYLOAD + EXPECT_STRING, result.second);
+
+ result = mgr.post(postUriBlank, "", null, null, MediaType.TEXT_PLAIN, PAYLOAD);
+ assertEquals((Integer)200, result.first);
+ assertTrue(result.second != null);
+ assertTrue(result.second.length() > 0);
+ assertEquals("POST: " + PAYLOAD + RETURN_STRING, result.second);
+ }
+
+ @Test
+ public void testGoodUrl() {
+ RestManager mgr = new RestManager();
+
+ Pair<Integer, String> result = mgr.get(getUri, "user", null, null);
+ assertEquals((Integer)200, result.first);
+ assertTrue(result.second != null);
+ assertTrue(result.second.length() > 0);
+ assertEquals("GOT: " + EXPECT_STRING, result.second);
+
+ result = mgr.delete(deleteUri, "user", null, null, null, null);
+ assertEquals((Integer)200, result.first);
+ assertTrue(result.second != null);
+ assertTrue(result.second.length() > 0);
+ assertEquals("DELETE: " + EXPECT_STRING, result.second);
+
+ result = mgr.put(putUri, "user", null, null, MediaType.TEXT_PLAIN, PAYLOAD);
+ assertEquals((Integer)200, result.first);
+ assertTrue(result.second != null);
+ assertTrue(result.second.length() > 0);
+ assertEquals("PUT: " + PAYLOAD + EXPECT_STRING, result.second);
+
+ result = mgr.put(putUriBlank, "user", null, null, MediaType.TEXT_PLAIN, PAYLOAD);
+ assertEquals((Integer)200, result.first);
+ assertTrue(result.second != null);
+ assertTrue(result.second.length() > 0);
+ assertEquals("PUT: " + PAYLOAD + RETURN_STRING, result.second);
+
+ result = mgr.post(postUri, "user", null, null, MediaType.TEXT_PLAIN, PAYLOAD);
+ assertEquals((Integer)200, result.first);
+ assertTrue(result.second != null);
+ assertTrue(result.second.length() > 0);
+ assertEquals("POST: " + PAYLOAD + EXPECT_STRING, result.second);
+
+ result = mgr.post(postUriBlank, "user", null, null, MediaType.TEXT_PLAIN, PAYLOAD);
+ assertEquals((Integer)200, result.first);
+ assertTrue(result.second != null);
+ assertTrue(result.second.length() > 0);
+ assertEquals("POST: " + PAYLOAD + RETURN_STRING, result.second);
+ }
+
+ @Test
+ public void testNoUrlParamUrl() {
+ RestManager mgr = new RestManager();
+
+ Pair<Integer, String> result = mgr.get(baseUri + "RestTest/GetHello/", null, null, null);
+ assertEquals((Integer)404, result.first);
+
+ result = mgr.delete(baseUri + "RestTest/DeleteHello/", null, null, null, null, null);
+ assertEquals((Integer)404, result.first);
+
+ result = mgr.put(baseUri + "RestTest/PutHello/", null, null, null, MediaType.TEXT_PLAIN, PAYLOAD);
+ assertEquals((Integer)404, result.first);
+
+ result = mgr.post(baseUri + "RestTest/PostHello/", null, null, null, MediaType.TEXT_PLAIN, PAYLOAD);
+ assertEquals((Integer)404, result.first);
+ }
+
+ @Test
+ public void testNoQueryParamUrl() {
+ RestManager mgr = new RestManager();
+
+ Pair<Integer, String> result = mgr.get(baseUri + "RestTest/GetHello/" + NAME_PARAM, null, null, null);
+ assertEquals((Integer)200, result.first);
+ assertTrue(result.second != null);
+ assertTrue(result.second.length() > 0);
+ assertEquals("GOT: " + RETURN_STRING + NAME_PARAM + " aged 90", result.second);
+
+ result = mgr.delete(baseUri + "RestTest/DeleteHello/" + NAME_PARAM, null, null, null, null, null);
+ assertEquals((Integer)200, result.first);
+ assertTrue(result.second != null);
+ assertTrue(result.second.length() > 0);
+ assertEquals("DELETE: " + RETURN_STRING + NAME_PARAM + " aged 90", result.second);
+
+ result = mgr.put(baseUri + "RestTest/PutHello/" + NAME_PARAM, null, null, null, MediaType.TEXT_PLAIN, PAYLOAD);
+ assertEquals((Integer)200, result.first);
+ assertTrue(result.second != null);
+ assertTrue(result.second.length() > 0);
+ assertEquals("PUT: " + PAYLOAD + RETURN_STRING + NAME_PARAM + " aged 90", result.second);
+
+ result = mgr.post(baseUri + "RestTest/PostHello/" + NAME_PARAM, null, null,
+ null, MediaType.TEXT_PLAIN, PAYLOAD);
+ assertEquals((Integer)200, result.first);
+ assertTrue(result.second != null);
+ assertTrue(result.second.length() > 0);
+ assertEquals("POST: " + PAYLOAD + RETURN_STRING + NAME_PARAM + " aged 90", result.second);
+ }
+
+ @Test
+ public void testBadUrl() {
+ RestManager mgr = new RestManager();
+
+ Pair<Integer, String> result = mgr.get(baseUri + "NonExistant/URL/", null, null, null);
+ assertEquals((Integer)404, result.first);
+
+ result = mgr.delete(baseUri + "NonExistant/URL/", null, null, null, null, null);
+ assertEquals((Integer)404, result.first);
+
+ result = mgr.put(baseUri + "NonExistant/URL/", null, null, null, MediaType.TEXT_PLAIN, PAYLOAD);
+ assertEquals((Integer)404, result.first);
+
+ result = mgr.post(baseUri + "NonExistant/URL/", null, null, null, MediaType.TEXT_PLAIN, PAYLOAD);
+ assertEquals((Integer)404, result.first);
+ }
+
+ @Test
+ public void testWrongUrl() {
+ RestManager mgr = new RestManager();
+
+ Pair<Integer, String> result = mgr.get(deleteUri, null, null, null);
+ assertEquals((Integer)405, result.first);
+
+ result = mgr.delete(getUri, null, null, null, null, null);
+ assertEquals((Integer)405, result.first);
+
+ result = mgr.delete(getUri, null, null, null);
+ assertEquals((Integer)405, result.first);
+
+ result = mgr.put(getUri, null, null, null, MediaType.TEXT_PLAIN, PAYLOAD);
+ assertEquals((Integer)405, result.first);
+
+ result = mgr.post(getUri, null, null, null, MediaType.TEXT_PLAIN, PAYLOAD);
+ assertEquals((Integer)405, result.first);
+ }
+
+ @GET
+ @Path("/GetHello/{name}")
+ @Produces(MediaType.TEXT_PLAIN)
+ public String getIt( @PathParam("name") String name, @DefaultValue("90") @QueryParam("age") String age) {
+ return "GOT: " + RETURN_STRING + name + " aged " + age;
+ }
+
+ @DELETE
+ @Path("/DeleteHello/{name}")
+ @Produces(MediaType.TEXT_PLAIN)
+ public String deleteIt( @PathParam("name") String name, @DefaultValue("90") @QueryParam("age") String age) {
+ return "DELETE: " + RETURN_STRING + name + " aged " + age;
+ }
+
+ @PUT
+ @Path("/PutHello/{name}")
+ @Consumes(MediaType.TEXT_PLAIN)
+ @Produces(MediaType.TEXT_PLAIN)
+ public String putBlank(
+ String payload,
+ @PathParam("name") String name,
+ @DefaultValue("90") @QueryParam("age") String age) {
+
+ return "PUT: " + payload + RETURN_STRING + name + " aged " + age;
+ }
+
+ @PUT
+ @Path("/PutBlank")
+ @Produces(MediaType.TEXT_PLAIN)
+ public String putIt( String payload) {
+ return "PUT: " + payload + RETURN_STRING;
+ }
+
+ @POST
+ @Path("/PostHello/{name}")
+ @Consumes(MediaType.TEXT_PLAIN)
+ @Produces(MediaType.TEXT_PLAIN)
+ public String postIt(
+ String payload,
+ @PathParam("name") String name,
+ @DefaultValue("90") @QueryParam("age") String age) {
+
+ return "POST: " + payload + RETURN_STRING + name + " aged " + age;
+ }
+
+ @POST
+ @Path("/PostBlank")
+ @Produces(MediaType.TEXT_PLAIN)
+ public String postBlank( String payload) {
+ return "POST: " + payload + RETURN_STRING;
+ }
+}