diff options
Diffstat (limited to 'controlloop/common/model-impl/so/src/test')
3 files changed, 219 insertions, 131 deletions
diff --git a/controlloop/common/model-impl/so/src/test/java/org/onap/policy/so/TestSoDummyServer.java b/controlloop/common/model-impl/so/src/test/java/org/onap/policy/so/TestSoDummyServer.java index 5eb0a01ee..0d719e50d 100644 --- a/controlloop/common/model-impl/so/src/test/java/org/onap/policy/so/TestSoDummyServer.java +++ b/controlloop/common/model-impl/so/src/test/java/org/onap/policy/so/TestSoDummyServer.java @@ -25,6 +25,7 @@ package org.onap.policy.so; import com.google.gson.Gson; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; +import javax.ws.rs.DELETE; import javax.ws.rs.GET; import javax.ws.rs.POST; import javax.ws.rs.Path; @@ -38,6 +39,7 @@ public class TestSoDummyServer { private static int putMessagesReceived = 0; private static int statMessagesReceived = 0; private static int getMessagesReceived = 0; + private static int deleteMessagesReceived = 0; private static Map<String, SOResponse> ongoingRequestMap = new ConcurrentHashMap<>(); @@ -51,7 +53,9 @@ public class TestSoDummyServer { public Response serviceGetStats() { statMessagesReceived++; return Response.status(200).entity("{\"GET\": " + getMessagesReceived + ",\"STAT\": " + statMessagesReceived - + ",\"POST\": " + postMessagesReceived + ",\"PUT\": " + putMessagesReceived + "}").build(); + + ",\"POST\": " + postMessagesReceived + ",\"PUT\": " + putMessagesReceived + + ",\"DELETE\": " + deleteMessagesReceived + "}").build(); + } /** @@ -77,98 +81,78 @@ public class TestSoDummyServer { @Path("/serviceInstantiation/v7") public Response servicePostRequest(final String jsonString) { postMessagesReceived++; + return buildResponse(jsonString); + } - if (jsonString == null) { - return Response.status(400).build(); - } - - SORequest request = null; - try { - request = new Gson().fromJson(jsonString, SORequest.class); - } catch (Exception e) { - return Response.status(400).build(); - } - - if (request == null) { - return Response.status(400).build(); - } - - if (request.getRequestType() == null) { - return Response.status(400).build(); - } - - if ("ReturnBadJson".equals(request.getRequestType())) { - return Response.status(200) - .entity("{\"GET\": , " + getMessagesReceived + ",\"STAT\": " + statMessagesReceived - + ",\"POST\": , " + postMessagesReceived + ",\"PUT\": " - + putMessagesReceived + "}") - .build(); - } - - SOResponse response = new SOResponse(); - response.setRequest(request); - response.setRequestReferences(new SORequestReferences()); - response.getRequestReferences().setRequestId(request.getRequestId().toString()); - - if ("ReturnCompleted".equals(request.getRequestType())) { - response.getRequest().getRequestStatus().setRequestState("COMPLETE"); - response.setHttpResponseCode(200); - String responseString = new Gson().toJson(response, SOResponse.class); - return Response.status(response.getHttpResponseCode()).entity(responseString).build(); - } - - if ("ReturnFailed".equals(request.getRequestType())) { - response.getRequest().getRequestStatus().setRequestState("FAILED"); - response.setHttpResponseCode(200); - String responseString = new Gson().toJson(response, SOResponse.class); - return Response.status(response.getHttpResponseCode()).entity(responseString).build(); - } - - if ("ReturnOnging202".equals(request.getRequestType())) { - ongoingRequestMap.put(request.getRequestId().toString(), response); + /** + * Post. + * + * @param serviceInstanceId service instance id + * @param vnfInstanceId vnf instance id + * @param jsonString json body + * @return http response + */ + @POST + @Path("/serviceInstantiation/v7/{serviceInstanceId}/vnfs/{vnfInstanceId}/vfModules/scaleOut") + public Response servicePostRequestVfModules(@PathParam("serviceInstanceId") final String serviceInstanceId, + @PathParam("vnfInstanceId") final String vnfInstanceId, final String jsonString) { + postMessagesReceived++; + return buildResponse(jsonString); + } - response.getRequest().getRequestStatus().setRequestState("ONGOING"); - response.setHttpResponseCode(202); - String responseString = new Gson().toJson(response, SOResponse.class); - return Response.status(response.getHttpResponseCode()).entity(responseString).build(); - } + /** + * Get instance ID. + * + * @param nsInstanceId node instance id + * @return http response + */ + @GET + @Path("/orchestrationRequests/v5/{nsInstanceId}") + public Response soRequestStatus(@PathParam("nsInstanceId") final String nsInstanceId) { - if ("ReturnOnging200".equals(request.getRequestType())) { - ongoingRequestMap.put(request.getRequestId().toString(), response); + SOResponse response = ongoingRequestMap.get(nsInstanceId); - response.getRequest().getRequestStatus().setRequestState("ONGOING"); - response.setHttpResponseCode(200); + int iterationsLeft = Integer.valueOf(response.getRequest().getRequestScope()); + if (--iterationsLeft > 0) { + response.getRequest().setRequestScope(new Integer(iterationsLeft).toString()); String responseString = new Gson().toJson(response, SOResponse.class); return Response.status(response.getHttpResponseCode()).entity(responseString).build(); } + ongoingRequestMap.remove(nsInstanceId); - if ("ReturnBadAfterWait".equals(request.getRequestType())) { - ongoingRequestMap.put(request.getRequestId().toString(), response); - - response.getRequest().getRequestStatus().setRequestState("ONGOING"); - response.setHttpResponseCode(200); - String responseString = new Gson().toJson(response, SOResponse.class); - return Response.status(response.getHttpResponseCode()).entity(responseString).build(); + if ("ReturnBadAfterWait".equals(response.getRequest().getRequestType())) { + return Response.status(400).build(); } - return null; + response.getRequest().getRequestStatus().setRequestState("COMPLETE"); + response.getRequest().setRequestScope("0"); + response.setHttpResponseCode(200); + String responseString = new Gson().toJson(response, SOResponse.class); + return Response.status(response.getHttpResponseCode()).entity(responseString).build(); } /** - * Post. - * + * Delete. + * * @param serviceInstanceId service instance id * @param vnfInstanceId vnf instance id + * @param vfModuleInstanceId vf module instance id * @param jsonString json body * @return http response */ - @POST - @Path("/serviceInstantiation/v7/{serviceInstanceId}/vnfs/{vnfInstanceId}/vfModules/scaleOut") - public Response servicePostRequestVfModules(@PathParam("serviceInstanceId") final String serviceInstanceId, - @PathParam("vnfInstanceId") final String vnfInstanceId, final String jsonString) { - postMessagesReceived++; + @DELETE + @Path("/serviceInstances/v7/{serviceInstanceId}/vnfs/{vnfInstanceId}/vfModules/{vfModuleInstanceId}") + public Response serviceDeleteRequestVfModules( + @PathParam("serviceInstanceId") final String serviceInstanceId, + @PathParam("vnfInstanceId") final String vnfInstanceId, + @PathParam("vfModuleInstanceId") final String vfModuleInstanceId, + final String jsonString) { + deleteMessagesReceived++; + return buildResponse(jsonString); + } + private Response buildResponse(String jsonString) { if (jsonString == null) { return Response.status(400).build(); } @@ -190,10 +174,9 @@ public class TestSoDummyServer { if ("ReturnBadJson".equals(request.getRequestType())) { return Response.status(200) - .entity("{\"GET\": , " + getMessagesReceived + ",\"STAT\": " + statMessagesReceived - + ",\"POST\": , " + postMessagesReceived + ",\"PUT\": " - + putMessagesReceived + "}") - .build(); + .entity("{\"GET\": , " + getMessagesReceived + ",\"STAT\": " + statMessagesReceived + + ",\"POST\":" + " , " + postMessagesReceived + ",\"PUT\": " + putMessagesReceived + + ",\"DELETE\": " + deleteMessagesReceived + "}").build(); } SOResponse response = new SOResponse(); @@ -205,14 +188,18 @@ public class TestSoDummyServer { response.getRequest().getRequestStatus().setRequestState("COMPLETE"); response.setHttpResponseCode(200); String responseString = new Gson().toJson(response, SOResponse.class); - return Response.status(response.getHttpResponseCode()).entity(responseString).build(); + return Response.status(response.getHttpResponseCode()) + .entity(responseString) + .build(); } if ("ReturnFailed".equals(request.getRequestType())) { response.getRequest().getRequestStatus().setRequestState("FAILED"); response.setHttpResponseCode(200); String responseString = new Gson().toJson(response, SOResponse.class); - return Response.status(response.getHttpResponseCode()).entity(responseString).build(); + return Response.status(response.getHttpResponseCode()) + .entity(responseString) + .build(); } if ("ReturnOnging202".equals(request.getRequestType())) { @@ -221,7 +208,9 @@ public class TestSoDummyServer { response.getRequest().getRequestStatus().setRequestState("ONGOING"); response.setHttpResponseCode(202); String responseString = new Gson().toJson(response, SOResponse.class); - return Response.status(response.getHttpResponseCode()).entity(responseString).build(); + return Response.status(response.getHttpResponseCode()) + .entity(responseString) + .build(); } if ("ReturnOnging200".equals(request.getRequestType())) { @@ -230,51 +219,21 @@ public class TestSoDummyServer { response.getRequest().getRequestStatus().setRequestState("ONGOING"); response.setHttpResponseCode(200); String responseString = new Gson().toJson(response, SOResponse.class); - return Response.status(response.getHttpResponseCode()).entity(responseString).build(); + return Response.status(response.getHttpResponseCode()) + .entity(responseString) + .build(); } - if ("ReturnBadAfterWait".equals(request.getRequestType())) { ongoingRequestMap.put(request.getRequestId().toString(), response); response.getRequest().getRequestStatus().setRequestState("ONGOING"); response.setHttpResponseCode(200); String responseString = new Gson().toJson(response, SOResponse.class); - return Response.status(response.getHttpResponseCode()).entity(responseString).build(); + return Response.status(response.getHttpResponseCode()) + .entity(responseString) + .build(); } - return null; } - - /** - * Get instance ID. - * - * @param nsInstanceId node instance id - * @return http response - */ - @GET - @Path("/orchestrationRequests/v5/{nsInstanceId}") - public Response soRequestStatus(@PathParam("nsInstanceId") final String nsInstanceId) { - - SOResponse response = ongoingRequestMap.get(nsInstanceId); - - int iterationsLeft = Integer.valueOf(response.getRequest().getRequestScope()); - if (--iterationsLeft > 0) { - response.getRequest().setRequestScope(new Integer(iterationsLeft).toString()); - String responseString = new Gson().toJson(response, SOResponse.class); - return Response.status(response.getHttpResponseCode()).entity(responseString).build(); - } - - ongoingRequestMap.remove(nsInstanceId); - - if ("ReturnBadAfterWait".equals(response.getRequest().getRequestType())) { - return Response.status(400).build(); - } - - response.getRequest().getRequestStatus().setRequestState("COMPLETE"); - response.getRequest().setRequestScope("0"); - response.setHttpResponseCode(200); - String responseString = new Gson().toJson(response, SOResponse.class); - return Response.status(response.getHttpResponseCode()).entity(responseString).build(); - } } diff --git a/controlloop/common/model-impl/so/src/test/java/org/onap/policy/so/TestSoManager.java b/controlloop/common/model-impl/so/src/test/java/org/onap/policy/so/TestSoManager.java index 38bfcd398..20cb54fca 100644 --- a/controlloop/common/model-impl/so/src/test/java/org/onap/policy/so/TestSoManager.java +++ b/controlloop/common/model-impl/so/src/test/java/org/onap/policy/so/TestSoManager.java @@ -51,10 +51,18 @@ public class TestSoManager { private static final String BASE_SO_URI = BASE_URI + "/SO"; private static HttpServer server; + /** + * Set up test class. + */ @BeforeClass - public static void setUp() { + public static void setUp() throws IOException { final ResourceConfig rc = new ResourceConfig(TestSoDummyServer.class); - server = GrizzlyHttpServerFactory.createHttpServer(URI.create(BASE_URI), rc); + //Grizzly by default doesn't allow payload for HTTP methods (ex: DELETE), for which HTTP spec doesn't + // explicitly state that. + //allow it before starting the server + server = GrizzlyHttpServerFactory.createHttpServer(URI.create(BASE_URI), rc, false); + server.getServerConfiguration().setAllowPayloadForUndefinedHttpMethods(true); + server.start(); } @AfterClass @@ -69,7 +77,8 @@ public class TestSoManager { CloseableHttpResponse response = httpclient.execute(httpGet); String returnBody = EntityUtils.toString(response.getEntity(), "UTF-8"); - assertTrue(returnBody.matches("^\\{\"GET\": [0-9]*,\"STAT\": [0-9]*,\"POST\": [0-9]*,\"PUT\": [0-9]*\\}$")); + assertTrue(returnBody.matches("^\\{\"GET\": [0-9]*,\"STAT\": [0-9]*,\"POST\": [0-9]*,\"PUT\": [0-9]*," + + "\"DELETE\": [0-9]*\\}$")); } @Test @@ -78,8 +87,8 @@ public class TestSoManager { assertNotNull(manager); manager.setRestGetTimeout(100); - SOResponse response = - manager.createModuleInstance("http:/localhost:99999999", BASE_SO_URI, "sean", "citizen", null); + SOResponse response = manager.createModuleInstance("http:/localhost:99999999", BASE_SO_URI, "sean", + "citizen", null); assertNull(response); response = manager.createModuleInstance(BASE_SO_URI + "/serviceInstantiation/v7", BASE_SO_URI, "sean", @@ -158,9 +167,11 @@ public class TestSoManager { WorkingMemory wm = new DummyWorkingMemory(); + SORequest soRequest = new SORequest(); + soRequest.setOperationType(SoOperationType.SCALE_OUT); PolicyEngine.manager.setEnvironmentProperty("so.url", "http:/localhost:99999999"); Future<SOResponse> asyncRestCallFuture = manager.asyncSORestCall(UUID.randomUUID().toString(), wm, - UUID.randomUUID().toString(), UUID.randomUUID().toString(), null); + UUID.randomUUID().toString(), UUID.randomUUID().toString(), soRequest); try { SOResponse response = asyncRestCallFuture.get(); assertEquals(999, response.getHttpResponseCode()); @@ -170,7 +181,7 @@ public class TestSoManager { PolicyEngine.manager.setEnvironmentProperty("so.url", BASE_SO_URI); asyncRestCallFuture = manager.asyncSORestCall(UUID.randomUUID().toString(), wm, UUID.randomUUID().toString(), - UUID.randomUUID().toString(), null); + UUID.randomUUID().toString(), soRequest); try { SOResponse response = asyncRestCallFuture.get(); assertEquals(999, response.getHttpResponseCode()); @@ -178,8 +189,122 @@ public class TestSoManager { fail("test should not throw an exception"); } + SORequest request = new SORequest(); + request.setRequestId(UUID.randomUUID()); + request.setRequestScope("Test"); + request.setRequestType("ReturnBadJson"); + request.setStartTime("2018-03-23 16:31"); + request.setRequestStatus(new SORequestStatus()); + request.getRequestStatus().setRequestState("ONGOING"); + request.setOperationType(SoOperationType.SCALE_OUT); + + asyncRestCallFuture = manager.asyncSORestCall(UUID.randomUUID().toString(), wm, UUID.randomUUID().toString(), + UUID.randomUUID().toString(), request); + try { + SOResponse response = asyncRestCallFuture.get(); + assertEquals(999, response.getHttpResponseCode()); + } catch (Exception e) { + fail("test should not throw an exception"); + } + + request.setRequestType("ReturnCompleted"); + + asyncRestCallFuture = manager.asyncSORestCall(UUID.randomUUID().toString(), wm, UUID.randomUUID().toString(), + UUID.randomUUID().toString(), request); + try { + SOResponse response = asyncRestCallFuture.get(); + assertEquals("COMPLETE", response.getRequest().getRequestStatus().getRequestState()); + } catch (Exception e) { + fail("test should not throw an exception"); + } + + request.setRequestType("ReturnFailed"); + asyncRestCallFuture = manager.asyncSORestCall(UUID.randomUUID().toString(), wm, UUID.randomUUID().toString(), + UUID.randomUUID().toString(), request); + try { + SOResponse response = asyncRestCallFuture.get(); + assertEquals("FAILED", response.getRequest().getRequestStatus().getRequestState()); + } catch (Exception e) { + fail("test should not throw an exception"); + } + + // Use scope to set the number of iterations we'll wait for + + request.setRequestType("ReturnOnging200"); + request.setRequestScope(new Integer(10).toString()); + asyncRestCallFuture = manager.asyncSORestCall(UUID.randomUUID().toString(), wm, UUID.randomUUID().toString(), + UUID.randomUUID().toString(), request); + try { + SOResponse response = asyncRestCallFuture.get(); + assertNotNull(response.getRequest()); + assertEquals("COMPLETE", response.getRequest().getRequestStatus().getRequestState()); + } catch (Exception e) { + fail("test should not throw an exception"); + } + + request.setRequestType("ReturnOnging202"); + request.setRequestScope(new Integer(20).toString()); + asyncRestCallFuture = manager.asyncSORestCall(UUID.randomUUID().toString(), wm, UUID.randomUUID().toString(), + UUID.randomUUID().toString(), request); + try { + SOResponse response = asyncRestCallFuture.get(); + assertNotNull(response.getRequest()); + assertEquals("COMPLETE", response.getRequest().getRequestStatus().getRequestState()); + } catch (Exception e) { + fail("test should not throw an exception"); + } + + // Test timeout after 20 attempts for a response + request.setRequestType("ReturnOnging202"); + request.setRequestScope(new Integer(21).toString()); + asyncRestCallFuture = manager.asyncSORestCall(UUID.randomUUID().toString(), wm, UUID.randomUUID().toString(), + UUID.randomUUID().toString(), request); + try { + SOResponse response = asyncRestCallFuture.get(); + assertEquals(999, response.getHttpResponseCode()); + } catch (Exception e) { + fail("test should not throw an exception"); + } + + // Test bad response after 3 attempts for a response + request.setRequestType("ReturnBadAfterWait"); + request.setRequestScope(new Integer(3).toString()); + asyncRestCallFuture = manager.asyncSORestCall(UUID.randomUUID().toString(), wm, UUID.randomUUID().toString(), + UUID.randomUUID().toString(), request); + try { + SOResponse response = asyncRestCallFuture.get(); + assertEquals(999, response.getHttpResponseCode()); + } catch (Exception e) { + fail("test should not throw an exception"); + } + } + + @Test + public void testVfModuleDeletion() { + SOManager manager = new SOManager(); + assertNotNull(manager); + manager.setRestGetTimeout(100); + + PolicyEngine.manager.setEnvironmentProperty("so.username", "sean"); + PolicyEngine.manager.setEnvironmentProperty("so.password", "citizen"); + + WorkingMemory wm = new DummyWorkingMemory(); + + SORequest soRequest = new SORequest(); + soRequest.setOperationType(SoOperationType.DELETE_VF_MODULE); + PolicyEngine.manager.setEnvironmentProperty("so.url", "http:/localhost:99999999"); + Future<SOResponse> asyncRestCallFuture = manager.asyncSORestCall(UUID.randomUUID().toString(), wm, + UUID.randomUUID().toString(), UUID.randomUUID().toString(), UUID.randomUUID().toString(), soRequest); + try { + SOResponse response = asyncRestCallFuture.get(); + assertEquals(999, response.getHttpResponseCode()); + } catch (Exception e) { + fail("test should not throw an exception"); + } + + PolicyEngine.manager.setEnvironmentProperty("so.url", BASE_SO_URI); asyncRestCallFuture = manager.asyncSORestCall(UUID.randomUUID().toString(), wm, UUID.randomUUID().toString(), - UUID.randomUUID().toString(), new SORequest()); + UUID.randomUUID().toString(), UUID.randomUUID().toString(), soRequest); try { SOResponse response = asyncRestCallFuture.get(); assertEquals(999, response.getHttpResponseCode()); @@ -194,9 +319,10 @@ public class TestSoManager { request.setStartTime("2018-03-23 16:31"); request.setRequestStatus(new SORequestStatus()); request.getRequestStatus().setRequestState("ONGOING"); + request.setOperationType(SoOperationType.DELETE_VF_MODULE); asyncRestCallFuture = manager.asyncSORestCall(UUID.randomUUID().toString(), wm, UUID.randomUUID().toString(), - UUID.randomUUID().toString(), request); + UUID.randomUUID().toString(), UUID.randomUUID().toString(), request); try { SOResponse response = asyncRestCallFuture.get(); assertEquals(999, response.getHttpResponseCode()); @@ -207,7 +333,7 @@ public class TestSoManager { request.setRequestType("ReturnCompleted"); asyncRestCallFuture = manager.asyncSORestCall(UUID.randomUUID().toString(), wm, UUID.randomUUID().toString(), - UUID.randomUUID().toString(), request); + UUID.randomUUID().toString(), UUID.randomUUID().toString(), request); try { SOResponse response = asyncRestCallFuture.get(); assertEquals("COMPLETE", response.getRequest().getRequestStatus().getRequestState()); @@ -217,7 +343,7 @@ public class TestSoManager { request.setRequestType("ReturnFailed"); asyncRestCallFuture = manager.asyncSORestCall(UUID.randomUUID().toString(), wm, UUID.randomUUID().toString(), - UUID.randomUUID().toString(), request); + UUID.randomUUID().toString(), UUID.randomUUID().toString(), request); try { SOResponse response = asyncRestCallFuture.get(); assertEquals("FAILED", response.getRequest().getRequestStatus().getRequestState()); @@ -230,7 +356,7 @@ public class TestSoManager { request.setRequestType("ReturnOnging200"); request.setRequestScope(new Integer(10).toString()); asyncRestCallFuture = manager.asyncSORestCall(UUID.randomUUID().toString(), wm, UUID.randomUUID().toString(), - UUID.randomUUID().toString(), request); + UUID.randomUUID().toString(), UUID.randomUUID().toString(), request); try { SOResponse response = asyncRestCallFuture.get(); assertNotNull(response.getRequest()); @@ -242,7 +368,7 @@ public class TestSoManager { request.setRequestType("ReturnOnging202"); request.setRequestScope(new Integer(20).toString()); asyncRestCallFuture = manager.asyncSORestCall(UUID.randomUUID().toString(), wm, UUID.randomUUID().toString(), - UUID.randomUUID().toString(), request); + UUID.randomUUID().toString(), UUID.randomUUID().toString(), request); try { SOResponse response = asyncRestCallFuture.get(); assertNotNull(response.getRequest()); @@ -255,7 +381,7 @@ public class TestSoManager { request.setRequestType("ReturnOnging202"); request.setRequestScope(new Integer(21).toString()); asyncRestCallFuture = manager.asyncSORestCall(UUID.randomUUID().toString(), wm, UUID.randomUUID().toString(), - UUID.randomUUID().toString(), request); + UUID.randomUUID().toString(), UUID.randomUUID().toString(), request); try { SOResponse response = asyncRestCallFuture.get(); assertEquals(999, response.getHttpResponseCode()); @@ -267,7 +393,7 @@ public class TestSoManager { request.setRequestType("ReturnBadAfterWait"); request.setRequestScope(new Integer(3).toString()); asyncRestCallFuture = manager.asyncSORestCall(UUID.randomUUID().toString(), wm, UUID.randomUUID().toString(), - UUID.randomUUID().toString(), request); + UUID.randomUUID().toString(), UUID.randomUUID().toString(), request); try { SOResponse response = asyncRestCallFuture.get(); assertEquals(999, response.getHttpResponseCode()); diff --git a/controlloop/common/model-impl/so/src/test/java/org/onap/policy/so/TestSoRequest.java b/controlloop/common/model-impl/so/src/test/java/org/onap/policy/so/TestSoRequest.java index f58254184..2f9bf68bb 100755 --- a/controlloop/common/model-impl/so/src/test/java/org/onap/policy/so/TestSoRequest.java +++ b/controlloop/common/model-impl/so/src/test/java/org/onap/policy/so/TestSoRequest.java @@ -65,6 +65,9 @@ public class TestSoRequest { obj.setRequestType("requestType"); assertEquals("requestType", obj.getRequestType()); + obj.setOperationType(SoOperationType.DELETE_VF_MODULE); + assertEquals(SoOperationType.DELETE_VF_MODULE, obj.getOperationType()); + LocalDateTime startTime = LocalDateTime.now(); obj.setStartTime(startTime.toString()); assertEquals(startTime.toString(), obj.getStartTime()); |