summaryrefslogtreecommitdiffstats
path: root/models-interactions/model-impl/rest
diff options
context:
space:
mode:
Diffstat (limited to 'models-interactions/model-impl/rest')
-rw-r--r--models-interactions/model-impl/rest/src/main/java/org/onap/policy/rest/RestManager.java28
-rw-r--r--models-interactions/model-impl/rest/src/test/java/org/onap/policy/rest/RestTest.java97
2 files changed, 124 insertions, 1 deletions
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 643c629b3..dde3aa2e2 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
@@ -29,6 +29,7 @@ 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.HttpPatch;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.client.methods.HttpRequestBase;
@@ -172,6 +173,33 @@ public class RestManager {
}
/**
+ * Perform REST Patch.
+ *
+ * @param url the url
+ * @param username the user name
+ * @param password the password
+ * @param headers any headers
+ * @param body body to send
+ * @return the response status code and the body
+ */
+ public Pair<Integer, String> patch(String url, String username, String password,
+ Map<String, String> headers, String body) {
+ String contentType = "application/merge-patch+json";
+ HttpPatch patch = new HttpPatch(url);
+ addHeaders(patch, username, password, headers);
+ patch.addHeader(CONTENT_TYPE, contentType);
+ try {
+ StringEntity input = new StringEntity(body);
+ input.setContentType(contentType);
+ patch.setEntity(input);
+ } catch (Exception e) {
+ logger.error("patch threw: ", e);
+ return null;
+ }
+ return sendRequest(patch);
+ }
+
+ /**
* Send REST request.
*
* @param request http request to send
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
index 903ec2fa3..21a971651 100644
--- 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
@@ -24,10 +24,17 @@ package org.onap.policy.rest;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
+import java.lang.annotation.Documented;
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.DefaultValue;
import javax.ws.rs.GET;
+import javax.ws.rs.HttpMethod;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
@@ -45,7 +52,7 @@ import org.onap.policy.rest.RestManager.Pair;
@Path("RestTest")
public class RestTest {
-
+ private static final String MERGE_PATCH_PLUS_JSON = "application/merge-patch+json";
private static final String NAME_PARAM = "Bob";
private static final String AGE_PARAM = "10";
@@ -64,6 +71,8 @@ public class RestTest {
private static String putUriBlank;
private static String postUri;
private static String postUriBlank;
+ private static String patchUri;
+ private static String patchUriBlank;
private static HttpServletServer server;
@@ -81,6 +90,8 @@ public class RestTest {
putUriBlank = baseUri + "RestTest/PutBlank";
postUri = baseUri + "RestTest/PostHello/" + NAME_PARAM + "?age=" + AGE_PARAM;
postUriBlank = baseUri + "RestTest/PostBlank";
+ patchUri = baseUri + "RestTest/PatchHello/" + NAME_PARAM + "?age=" + AGE_PARAM;
+ patchUriBlank = baseUri + "RestTest/PatchBlank";
server = HttpServletServerFactoryInstance.getServerFactory()
.build("RestTest", LOCALHOST, port, "/" + BASE, false, true);
@@ -123,6 +134,12 @@ public class RestTest {
mgr.delete(null, "user", null, null, null, null);
}
+ @Test(expected = NullPointerException.class)
+ public void testPatchUrlNull() {
+ RestManager mgr = new RestManager();
+ mgr.patch(null, "user", null, null, PAYLOAD);
+ }
+
@Test
public void testUsernameNull() {
RestManager mgr = new RestManager();
@@ -168,6 +185,18 @@ public class RestTest {
assertTrue(result.second != null);
assertTrue(result.second.length() > 0);
assertEquals("POST: " + PAYLOAD + RETURN_STRING, result.second);
+
+ result = mgr.patch(patchUri, null, null, null, PAYLOAD);
+ assertEquals((Integer)200, result.first);
+ assertTrue(result.second != null);
+ assertTrue(result.second.length() > 0);
+ assertEquals("PATCH: " + PAYLOAD + EXPECT_STRING, result.second);
+
+ result = mgr.patch(patchUriBlank, null, null, null, PAYLOAD);
+ assertEquals((Integer)200, result.first);
+ assertTrue(result.second != null);
+ assertTrue(result.second.length() > 0);
+ assertEquals("PATCH: " + PAYLOAD + RETURN_STRING, result.second);
}
@Test
@@ -209,6 +238,18 @@ public class RestTest {
assertTrue(result.second != null);
assertTrue(result.second.length() > 0);
assertEquals("POST: " + PAYLOAD + RETURN_STRING, result.second);
+
+ result = mgr.patch(patchUri, "", null, null, PAYLOAD);
+ assertEquals((Integer)200, result.first);
+ assertTrue(result.second != null);
+ assertTrue(result.second.length() > 0);
+ assertEquals("PATCH: " + PAYLOAD + EXPECT_STRING, result.second);
+
+ result = mgr.patch(patchUriBlank, "", null, null, PAYLOAD);
+ assertEquals((Integer)200, result.first);
+ assertTrue(result.second != null);
+ assertTrue(result.second.length() > 0);
+ assertEquals("PATCH: " + PAYLOAD + RETURN_STRING, result.second);
}
@Test
@@ -250,6 +291,18 @@ public class RestTest {
assertTrue(result.second != null);
assertTrue(result.second.length() > 0);
assertEquals("POST: " + PAYLOAD + RETURN_STRING, result.second);
+
+ result = mgr.patch(patchUri, "user", null, null, PAYLOAD);
+ assertEquals((Integer)200, result.first);
+ assertTrue(result.second != null);
+ assertTrue(result.second.length() > 0);
+ assertEquals("PATCH: " + PAYLOAD + EXPECT_STRING, result.second);
+
+ result = mgr.patch(patchUriBlank, "user", null, null, PAYLOAD);
+ assertEquals((Integer)200, result.first);
+ assertTrue(result.second != null);
+ assertTrue(result.second.length() > 0);
+ assertEquals("PATCH: " + PAYLOAD + RETURN_STRING, result.second);
}
@Test
@@ -267,6 +320,9 @@ public class RestTest {
result = mgr.post(baseUri + "RestTest/PostHello/", null, null, null, MediaType.TEXT_PLAIN, PAYLOAD);
assertEquals((Integer)404, result.first);
+
+ result = mgr.patch(baseUri + "RestTest/PatchHello/", null, null, null, PAYLOAD);
+ assertEquals((Integer)404, result.first);
}
@Test
@@ -297,6 +353,13 @@ public class RestTest {
assertTrue(result.second != null);
assertTrue(result.second.length() > 0);
assertEquals("POST: " + PAYLOAD + RETURN_STRING + NAME_PARAM + " aged 90", result.second);
+
+ result = mgr.patch(baseUri + "RestTest/PatchHello/" + NAME_PARAM, null, null,
+ null, PAYLOAD);
+ assertEquals((Integer)200, result.first);
+ assertTrue(result.second != null);
+ assertTrue(result.second.length() > 0);
+ assertEquals("PATCH: " + PAYLOAD + RETURN_STRING + NAME_PARAM + " aged 90", result.second);
}
@Test
@@ -314,6 +377,9 @@ public class RestTest {
result = mgr.post(baseUri + "NonExistant/URL/", null, null, null, MediaType.TEXT_PLAIN, PAYLOAD);
assertEquals((Integer)404, result.first);
+
+ result = mgr.patch(baseUri + "NonExistant/URL/", null, null, null, PAYLOAD);
+ assertEquals((Integer)404, result.first);
}
@Test
@@ -334,6 +400,9 @@ public class RestTest {
result = mgr.post(getUri, null, null, null, MediaType.TEXT_PLAIN, PAYLOAD);
assertEquals((Integer)405, result.first);
+
+ result = mgr.patch(getUri, null, null, null, PAYLOAD);
+ assertEquals((Integer)405, result.first);
}
@GET
@@ -387,4 +456,30 @@ public class RestTest {
public String postBlank( String payload) {
return "POST: " + payload + RETURN_STRING;
}
+
+ @Target({ElementType.METHOD})
+ @Retention(RetentionPolicy.RUNTIME)
+ @HttpMethod("PATCH")
+ @Documented
+ public static @interface Patch {
+ }
+
+ @Patch
+ @Path("/PatchHello/{name}")
+ @Consumes(MERGE_PATCH_PLUS_JSON)
+ @Produces(MERGE_PATCH_PLUS_JSON)
+ public String patchIt(
+ String payload,
+ @PathParam("name") String name,
+ @DefaultValue("90") @QueryParam("age") String age) {
+
+ return "PATCH: " + payload + RETURN_STRING + name + " aged " + age;
+ }
+
+ @Patch
+ @Path("/PatchBlank")
+ @Produces(MERGE_PATCH_PLUS_JSON)
+ public String patchBlank( String payload) {
+ return "PATCH: " + payload + RETURN_STRING;
+ }
}