+ @Test
+ public void Given_Request_Is_HTTP_PUT_And_Feed_Version_Change_is_Requested_Bad_Request_Response_Is_Generated() throws Exception {
+ when(request.getPathInfo()).thenReturn("/2");
+ JSONObject JSObject = buildRequestJsonObject();
+ FeedServlet feedServlet = new FeedServlet() {
+ protected JSONObject getJSONfromInput(HttpServletRequest req) {
+ JSONObject jo = new JSONObject();
+ jo.put("name", "AafFeed");
+ jo.put("version", "v0.2");
+ jo.put("authorization", JSObject);
+ return jo;
+ }
+ };
+ feedServlet.doPut(request, response);
+ verify(response).sendError(eq(HttpServletResponse.SC_BAD_REQUEST), contains("version of the feed may not be updated"));
+ }
+
+ @Test
+ public void Given_Request_Is_HTTP_PUT_And_Request_Is_Not_Authorized_Then_Forbidden_Response_Is_Generated() throws Exception {
+ setAuthoriserToReturnRequestNotAuthorized();
+ when(request.getPathInfo()).thenReturn("/2");
+ JSONObject JSObject = buildRequestJsonObject();
+ FeedServlet feedServlet = new FeedServlet() {
+ protected JSONObject getJSONfromInput(HttpServletRequest req) {
+ JSONObject jo = new JSONObject();
+ jo.put("name", "AafFeed");
+ jo.put("version", "v0.1");
+ jo.put("authorization", JSObject);
+ return jo;
+ }
+ };
+ feedServlet.doPut(request, response);
+ verify(response).sendError(eq(HttpServletResponse.SC_FORBIDDEN), contains("Policy Engine disallows access"));
+ }
+
+ @Test
+ public void Given_Request_Is_HTTP_PUT_And_AAF_Feed_Without_Permissions_Then_Forbidden_Response_Is_Generated() throws Exception {
+ when(request.getPathInfo()).thenReturn("/2");
+ JSONObject JSObject = buildRequestJsonObject();
+ FeedServlet feedServlet = new FeedServlet() {
+ protected JSONObject getJSONfromInput(HttpServletRequest req) {
+ JSONObject jo = new JSONObject();
+ jo.put("name", "AafFeed");
+ jo.put("version", "v0.1");
+ jo.put("authorization", JSObject);
+ jo.put("aaf_instance", "https://aaf-onap-test.osaaf.org:8095");
+ return jo;
+ }
+ };
+ feedServlet.doPut(request, response);
+ verify(response).sendError(eq(HttpServletResponse.SC_FORBIDDEN), contains("AAF disallows access to permission"));
+ }
+
+ @Test
+ public void Given_Request_Is_HTTP_PUT_And_AAF_Feed_With_Permissions_Then_STATUS_OK__Response_Is_Generated() throws Exception {
+ ServletOutputStream outStream = mock(ServletOutputStream.class);
+ when(response.getOutputStream()).thenReturn(outStream);
+ when(request.getPathInfo()).thenReturn("/2");
+ when(request.isUserInRole("org.onap.dmaap-dr.feed|*|edit")).thenReturn(true);
+ JSONObject JSObject = buildRequestJsonObject();
+ FeedServlet feedServlet = new FeedServlet() {
+ protected JSONObject getJSONfromInput(HttpServletRequest req) {
+ JSONObject jo = new JSONObject();
+ jo.put("name", "AafFeed");
+ jo.put("version", "v0.1");
+ jo.put("authorization", JSObject);
+ jo.put("aaf_instance", "*");
+ return jo;
+ }
+ @Override
+ protected boolean doUpdate(Updateable bean) {
+ return true;
+ }
+
+ };
+ feedServlet.doPut(request, response);
+ verify(response).setStatus(eq(HttpServletResponse.SC_OK));
+ verifyEnteringExitCalled(listAppender);
+ }
+
+ @Test
+ public void Given_Request_Is_HTTP_PUT_And_Change_On_Feeds_Fails_An_Internal_Server_Error_Response_Is_Generated() throws Exception {
+ ServletOutputStream outStream = mock(ServletOutputStream.class);
+ when(response.getOutputStream()).thenReturn(outStream);
+ when(request.getPathInfo()).thenReturn("/2");
+ JSONObject JSObject = buildRequestJsonObject();
+ FeedServlet feedServlet = new FeedServlet() {
+ protected JSONObject getJSONfromInput(HttpServletRequest req) {
+ JSONObject jo = new JSONObject();
+ jo.put("name", "AafFeed");
+ jo.put("version", "v0.1");
+ jo.put("authorization", JSObject);
+ return jo;
+ }
+
+ @Override
+ protected boolean doUpdate(Updateable bean) {
+ return false;
+ }
+ };
+ feedServlet.doPut(request, response);
+ verify(response).sendError(eq(HttpServletResponse.SC_INTERNAL_SERVER_ERROR), argThat(notNullValue(String.class)));
+ }
+
+ @Test
+ public void Given_Request_Is_HTTP_PUT_And_Change_On_Feeds_Suceeds_A_STATUS_OK_Response_Is_Generated() throws Exception {
+ ServletOutputStream outStream = mock(ServletOutputStream.class);
+ when(response.getOutputStream()).thenReturn(outStream);
+ when(request.getPathInfo()).thenReturn("/2");
+ JSONObject JSObject = buildRequestJsonObject();
+ FeedServlet feedServlet = new FeedServlet() {
+ protected JSONObject getJSONfromInput(HttpServletRequest req) {
+ JSONObject jo = new JSONObject();
+ jo.put("name", "AafFeed");
+ jo.put("version", "v0.1");
+ jo.put("authorization", JSObject);
+ return jo;
+ }
+ @Override
+ protected boolean doUpdate(Updateable bean) {
+ return true;
+ }
+
+ };
+ feedServlet.doPut(request, response);
+ verify(response).setStatus(eq(HttpServletResponse.SC_OK));
+ verifyEnteringExitCalled(listAppender);
+ }
+
+ @Test
+ public void Given_Request_Is_HTTP_POST_SC_METHOD_NOT_ALLOWED_Response_Is_Generated() throws Exception {
+ feedServlet.doPost(request, response);
+ verify(response).sendError(eq(HttpServletResponse.SC_METHOD_NOT_ALLOWED), argThat(notNullValue(String.class)));
+ verifyEnteringExitCalled(listAppender);
+ }
+
+ @NotNull
+ private JSONObject buildRequestJsonObject() {
+ JSONObject JSObject = new JSONObject();
+ JSONArray endpointIDs = new JSONArray();
+ JSONObject JOEndpointIDs = new JSONObject();
+ JOEndpointIDs.put("id", "stub_endpoint_id");
+ JOEndpointIDs.put("password", "stub_endpoint_password");
+ endpointIDs.put(JOEndpointIDs);
+
+ JSONArray endpointAddresses = new JSONArray();
+ endpointAddresses.put("127.0.0.1");
+
+ JSObject.put("classification", "stub_classification");
+ JSObject.put("endpoint_ids", endpointIDs);
+ JSObject.put("endpoint_addrs", endpointAddresses);
+ return JSObject;