Add Delete JUnit Tests 06/126106/4
authorbrunomilitzer <bruno.militzer@est.tech>
Mon, 6 Dec 2021 10:53:26 +0000 (10:53 +0000)
committerbrunomilitzer <bruno.militzer@est.tech>
Wed, 8 Dec 2021 10:13:43 +0000 (10:13 +0000)
Added Delete InstanceProperties Tests
Added Change OrderState Tests

Included Issue-Id: POLICY-3570

Issue-Id: POLICY-3567
Change-Id: I7cf72f78bf1a0d0e695a1721c1a8423a02cf61af
Signed-off-by: brunomilitzer <bruno.militzer@est.tech>
runtime-controlloop/src/test/java/org/onap/policy/clamp/controlloop/runtime/instantiation/rest/InstantiationControllerTest.java

index ea7dde4..5d5f4fc 100644 (file)
@@ -39,6 +39,7 @@ import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoop
 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoops;
 import org.onap.policy.clamp.controlloop.models.controlloop.persistence.provider.ParticipantProvider;
 import org.onap.policy.clamp.controlloop.models.controlloop.persistence.provider.ServiceTemplateProvider;
+import org.onap.policy.clamp.controlloop.models.controlloop.persistence.repository.ControlLoopRepository;
 import org.onap.policy.clamp.controlloop.models.messages.rest.instantiation.ControlLoopOrderStateResponse;
 import org.onap.policy.clamp.controlloop.models.messages.rest.instantiation.ControlLoopPrimedResponse;
 import org.onap.policy.clamp.controlloop.models.messages.rest.instantiation.InstancePropertiesResponse;
@@ -89,6 +90,9 @@ class InstantiationControllerTest extends CommonRestController {
 
     private static ToscaServiceTemplate serviceTemplate = new ToscaServiceTemplate();
 
+    @Autowired
+    private ControlLoopRepository controlLoopRepository;
+
     @Autowired
     private ServiceTemplateProvider serviceTemplateProvider;
 
@@ -309,6 +313,69 @@ class InstantiationControllerTest extends CommonRestController {
         }
     }
 
+    @Test
+    void testDeleteInstanceProperties() throws Exception {
+        Invocation.Builder invocationBuilder = super.sendRequest(INSTANTIATION_PROPERTIES);
+        Response resp = invocationBuilder.post(Entity.json(serviceTemplate));
+        assertEquals(Response.Status.OK.getStatusCode(), resp.getStatus());
+
+        invocationBuilder = super.sendRequest(INSTANTIATION_PROPERTIES + "?name=" + ID_NAME + "&version=" + ID_VERSION);
+        resp = invocationBuilder.delete();
+        assertEquals(Response.Status.OK.getStatusCode(), resp.getStatus());
+        var instanceResponse = resp.readEntity(InstantiationResponse.class);
+        assertEquals(ID_NAME, instanceResponse.getAffectedControlLoops().get(0).getName());
+        ControlLoops controlLoopsGet = instantiationProvider.getControlLoops(ID_NAME, ID_VERSION);
+        assertThat(controlLoopsGet.getControlLoopList()).isEmpty();
+    }
+
+    @Test
+    void testDeleteInstancePropertiesBadRequest() throws Exception {
+        Invocation.Builder invocationBuilder = super.sendRequest(INSTANTIATION_PROPERTIES);
+        Response resp = invocationBuilder.post(Entity.json(serviceTemplate));
+        assertEquals(Response.Status.OK.getStatusCode(), resp.getStatus());
+
+        invocationBuilder = super.sendRequest(INSTANTIATION_PROPERTIES + "?name=" + ID_NAME);
+        resp = invocationBuilder.delete();
+        assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), resp.getStatus());
+    }
+
+    @Test
+    void testDeleteInstancePropertiesPassiveMode() throws Exception {
+        Invocation.Builder invocationBuilder = super.sendRequest(INSTANTIATION_PROPERTIES);
+        Response resp = invocationBuilder.post(Entity.json(serviceTemplate));
+        assertEquals(Response.Status.OK.getStatusCode(), resp.getStatus());
+
+        var controlLoops = InstantiationUtils.getControlLoopsFromResource(CL_INSTANTIATION_CREATE_JSON, "Command");
+        instantiationProvider.createControlLoops(controlLoops);
+
+        var participants = CommonTestData.createParticipants();
+        for (var participant : participants) {
+            participantProvider.saveParticipant(participant);
+        }
+
+        InstantiationCommand command =
+            InstantiationUtils.getInstantiationCommandFromResource(CL_INSTANTIATION_CHANGE_STATE_JSON, "Command");
+
+        invocationBuilder = super.sendRequest(INSTANTIATION_COMMAND_ENDPOINT);
+        resp = invocationBuilder.put(Entity.json(command));
+        assertEquals(Response.Status.ACCEPTED.getStatusCode(), resp.getStatus());
+        InstantiationResponse instResponse = resp.readEntity(InstantiationResponse.class);
+        InstantiationUtils.assertInstantiationResponse(instResponse, command);
+
+        // check passive state on DB and delete properties
+        for (ToscaConceptIdentifier toscaConceptIdentifier : command.getControlLoopIdentifierList()) {
+            ControlLoops controlLoopsGet = instantiationProvider.getControlLoops(toscaConceptIdentifier.getName(),
+                toscaConceptIdentifier.getVersion());
+            assertThat(controlLoopsGet.getControlLoopList()).hasSize(1);
+            assertEquals(command.getOrderedState(), controlLoopsGet.getControlLoopList().get(0).getOrderedState());
+
+            invocationBuilder = super.sendRequest(INSTANTIATION_PROPERTIES + "?name="
+                + toscaConceptIdentifier.getName() + "&version=" + toscaConceptIdentifier.getVersion());
+            resp = invocationBuilder.delete();
+            assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), resp.getStatus());
+        }
+    }
+
     @Test
     void testCommand_NotFound1() throws Exception {
         Invocation.Builder invocationBuilder = super.sendRequest(INSTANTIATION_COMMAND_ENDPOINT);
@@ -383,7 +450,53 @@ class InstantiationControllerTest extends CommonRestController {
         assertThat(controlLoopsGet.getControlLoopList()).isEmpty();
     }
 
+    @Test
+    void testChangeOrderStateFromUninitializedPassiveMode() throws Exception {
+        Invocation.Builder invocationBuilder = super.sendRequest(INSTANTIATION_PROPERTIES);
+        Response resp = invocationBuilder.post(Entity.json(serviceTemplate));
+        assertEquals(Response.Status.OK.getStatusCode(), resp.getStatus());
+
+        var controlLoops = InstantiationUtils.getControlLoopsFromResource(CL_INSTANTIATION_CREATE_JSON,
+            "CommandPassive");
+        instantiationProvider.createControlLoops(controlLoops);
+
+        var participants = CommonTestData.createParticipants();
+        for (var participant : participants) {
+            participantProvider.saveParticipant(participant);
+        }
+
+        InstantiationCommand command =
+            InstantiationUtils.getInstantiationCommandFromResource(CL_INSTANTIATION_CHANGE_STATE_JSON,
+                "CommandPassive");
+
+        invocationBuilder = super.sendRequest(INSTANTIATION_COMMAND_ENDPOINT);
+        resp = invocationBuilder.put(Entity.json(command));
+        assertEquals(Response.Status.ACCEPTED.getStatusCode(), resp.getStatus());
+        InstantiationResponse instResponse = resp.readEntity(InstantiationResponse.class);
+        InstantiationUtils.assertInstantiationResponse(instResponse, command);
+    }
+
+    @Test
+    void testChangeOrderStateWithoutRegisteredParticipants() throws Exception {
+        Invocation.Builder invocationBuilder = super.sendRequest(INSTANTIATION_PROPERTIES);
+        Response resp = invocationBuilder.post(Entity.json(serviceTemplate));
+        assertEquals(Response.Status.OK.getStatusCode(), resp.getStatus());
+
+        var controlLoops = InstantiationUtils.getControlLoopsFromResource(CL_INSTANTIATION_CREATE_JSON,
+            "CommandPassive");
+        instantiationProvider.createControlLoops(controlLoops);
+
+        InstantiationCommand command =
+            InstantiationUtils.getInstantiationCommandFromResource(CL_INSTANTIATION_CHANGE_STATE_JSON,
+                "CommandPassive");
+
+        invocationBuilder = super.sendRequest(INSTANTIATION_COMMAND_ENDPOINT);
+        resp = invocationBuilder.put(Entity.json(command));
+        assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), resp.getStatus());
+    }
+
     private synchronized void deleteEntryInDB() throws Exception {
+        controlLoopRepository.deleteAll();
         var list = serviceTemplateProvider.getAllServiceTemplates();
         if (!list.isEmpty()) {
             serviceTemplateProvider.deleteServiceTemplate(list.get(0).getName(), list.get(0).getVersion());