diff options
author | Jim Hahn <jrh3@att.com> | 2021-07-12 18:33:50 +0000 |
---|---|---|
committer | Gerrit Code Review <gerrit@onap.org> | 2021-07-12 18:33:50 +0000 |
commit | 3699a2d2b3a9869d9dbab2d7c0c49830fc0a7f4d (patch) | |
tree | a4422c621aaec250269608abec67518e7f4fcf5e /runtime-controlloop/src/test/java | |
parent | 26795001ac88eddb0ff18cf8e215d65f34c9d680 (diff) | |
parent | d5fb4e04527f47f78e9116e3573c21dd75ff8abb (diff) |
Merge "Convert controlloop runtime to springboot framework"
Diffstat (limited to 'runtime-controlloop/src/test/java')
11 files changed, 422 insertions, 770 deletions
diff --git a/runtime-controlloop/src/test/java/org/onap/policy/clamp/controlloop/runtime/commissioning/CommissioningProviderTest.java b/runtime-controlloop/src/test/java/org/onap/policy/clamp/controlloop/runtime/commissioning/CommissioningProviderTest.java index 956b5e911..fdddcf996 100644 --- a/runtime-controlloop/src/test/java/org/onap/policy/clamp/controlloop/runtime/commissioning/CommissioningProviderTest.java +++ b/runtime-controlloop/src/test/java/org/onap/policy/clamp/controlloop/runtime/commissioning/CommissioningProviderTest.java @@ -22,26 +22,23 @@ package org.onap.policy.clamp.controlloop.runtime.commissioning; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatThrownBy; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; +import static org.junit.jupiter.api.Assertions.assertTrue; import java.util.ArrayList; import java.util.List; -import org.junit.Before; -import org.junit.Test; +import org.junit.jupiter.api.Test; import org.onap.policy.clamp.controlloop.runtime.main.parameters.ClRuntimeParameterGroup; import org.onap.policy.common.utils.coder.Coder; import org.onap.policy.common.utils.coder.CoderException; import org.onap.policy.common.utils.coder.StandardCoder; import org.onap.policy.common.utils.coder.YamlJsonTranslator; import org.onap.policy.common.utils.resources.ResourceUtils; -import org.onap.policy.models.provider.PolicyModelsProviderParameters; import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier; import org.onap.policy.models.tosca.authorative.concepts.ToscaNodeTemplate; import org.onap.policy.models.tosca.authorative.concepts.ToscaNodeType; import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate; -public class CommissioningProviderTest { +class CommissioningProviderTest { private static final String TOSCA_SERVICE_TEMPLATE_YAML = "src/test/resources/rest/servicetemplates/pmsh_multiple_cl_tosca.yaml"; private static final String TEMPLATE_IS_NULL = ".*serviceTemplate is marked non-null but is null"; @@ -49,7 +46,6 @@ public class CommissioningProviderTest { private static final YamlJsonTranslator yamlTranslator = new YamlJsonTranslator(); private static int dbNum = 0; private static final Object lockit = new Object(); - private PolicyModelsProviderParameters databaseProviderParameters; private static String getParameterGroupAsString() { dbNum++; @@ -58,15 +54,13 @@ public class CommissioningProviderTest { } /** - * Sets up db provider parameters before each test. + * return a Cl Runtime Parameters. * * @throws CoderException . */ - @Before - public void setupDbProviderParameters() throws CoderException { + public ClRuntimeParameterGroup getClRuntimeParameterGroup() throws CoderException { synchronized (lockit) { - databaseProviderParameters = CODER.decode(getParameterGroupAsString(), ClRuntimeParameterGroup.class) - .getDatabaseProviderParameters(); + return CODER.decode(getParameterGroupAsString(), ClRuntimeParameterGroup.class); } } @@ -76,14 +70,13 @@ public class CommissioningProviderTest { * @throws Exception . */ @Test - public void testGetControlLoopDefinitions() throws Exception { + void testGetControlLoopDefinitions() throws Exception { List<ToscaNodeTemplate> listOfTemplates; + ClRuntimeParameterGroup clRuntimeParameterGroup = getClRuntimeParameterGroup(); - try (CommissioningProvider provider = new CommissioningProvider(databaseProviderParameters)) { - ToscaServiceTemplate serviceTemplate = yamlTranslator - .fromYaml(ResourceUtils.getResourceAsString(TOSCA_SERVICE_TEMPLATE_YAML), - ToscaServiceTemplate.class); - + try (var provider = new CommissioningProvider(clRuntimeParameterGroup)) { + ToscaServiceTemplate serviceTemplate = yamlTranslator.fromYaml( + ResourceUtils.getResourceAsString(TOSCA_SERVICE_TEMPLATE_YAML), ToscaServiceTemplate.class); listOfTemplates = provider.getControlLoopDefinitions(null, null); assertThat(listOfTemplates).isEmpty(); @@ -92,16 +85,16 @@ public class CommissioningProviderTest { listOfTemplates = provider.getControlLoopDefinitions(null, null); assertThat(listOfTemplates).hasSize(2); - //Test Filtering - listOfTemplates = provider.getControlLoopDefinitions("org.onap.domain.pmsh.PMSHControlLoopDefinition", - "1.2.3"); + // Test Filtering + listOfTemplates = + provider.getControlLoopDefinitions("org.onap.domain.pmsh.PMSHControlLoopDefinition", "1.2.3"); assertThat(listOfTemplates).hasSize(1); for (ToscaNodeTemplate template : listOfTemplates) { - //Other CL elements contain PMSD instead of PMSH in their name - assertFalse(template.getName().contains("PMSD")); + // Other CL elements contain PMSD instead of PMSH in their name + assertThat(template.getName()).doesNotContain("PMSD"); } - //Test Wrong Name + // Test Wrong Name listOfTemplates = provider.getControlLoopDefinitions("WrongControlLoopName", "0.0.0"); assertThat(listOfTemplates).isEmpty(); } @@ -113,18 +106,18 @@ public class CommissioningProviderTest { * @throws Exception . */ @Test - public void testCreateControlLoopDefinitions() throws Exception { + void testCreateControlLoopDefinitions() throws Exception { List<ToscaNodeTemplate> listOfTemplates; + ClRuntimeParameterGroup clRuntimeParameterGroup = getClRuntimeParameterGroup(); - try (CommissioningProvider provider = new CommissioningProvider(databaseProviderParameters)) { - //Test Service template is null + try (var provider = new CommissioningProvider(clRuntimeParameterGroup)) { + // Test Service template is null assertThatThrownBy(() -> provider.createControlLoopDefinitions(null)).hasMessageMatching(TEMPLATE_IS_NULL); listOfTemplates = provider.getControlLoopDefinitions(null, null); assertThat(listOfTemplates).isEmpty(); - ToscaServiceTemplate serviceTemplate = yamlTranslator - .fromYaml(ResourceUtils.getResourceAsString(TOSCA_SERVICE_TEMPLATE_YAML), - ToscaServiceTemplate.class); + ToscaServiceTemplate serviceTemplate = yamlTranslator.fromYaml( + ResourceUtils.getResourceAsString(TOSCA_SERVICE_TEMPLATE_YAML), ToscaServiceTemplate.class); // Response should return the number of node templates present in the service template List<ToscaConceptIdentifier> affectedDefinitions = @@ -141,13 +134,13 @@ public class CommissioningProviderTest { * @throws Exception . */ @Test - public void testDeleteControlLoopDefinitions() throws Exception { + void testDeleteControlLoopDefinitions() throws Exception { List<ToscaNodeTemplate> listOfTemplates; + ClRuntimeParameterGroup clRuntimeParameterGroup = getClRuntimeParameterGroup(); - try (CommissioningProvider provider = new CommissioningProvider(databaseProviderParameters)) { - ToscaServiceTemplate serviceTemplate = yamlTranslator - .fromYaml(ResourceUtils.getResourceAsString(TOSCA_SERVICE_TEMPLATE_YAML), - ToscaServiceTemplate.class); + try (var provider = new CommissioningProvider(clRuntimeParameterGroup)) { + ToscaServiceTemplate serviceTemplate = yamlTranslator.fromYaml( + ResourceUtils.getResourceAsString(TOSCA_SERVICE_TEMPLATE_YAML), ToscaServiceTemplate.class); listOfTemplates = provider.getControlLoopDefinitions(null, null); assertThat(listOfTemplates).isEmpty(); @@ -168,17 +161,17 @@ public class CommissioningProviderTest { * @throws Exception . */ @Test - public void testGetControlLoopElementDefinitions() throws Exception { - try (CommissioningProvider provider = new CommissioningProvider(databaseProviderParameters)) { - ToscaServiceTemplate serviceTemplate = yamlTranslator - .fromYaml(ResourceUtils.getResourceAsString(TOSCA_SERVICE_TEMPLATE_YAML), - ToscaServiceTemplate.class); + void testGetControlLoopElementDefinitions() throws Exception { + ClRuntimeParameterGroup clRuntimeParameterGroup = getClRuntimeParameterGroup(); + try (var provider = new CommissioningProvider(clRuntimeParameterGroup)) { + ToscaServiceTemplate serviceTemplate = yamlTranslator.fromYaml( + ResourceUtils.getResourceAsString(TOSCA_SERVICE_TEMPLATE_YAML), ToscaServiceTemplate.class); provider.getControlLoopDefinitions(null, null); provider.createControlLoopDefinitions(serviceTemplate); - List<ToscaNodeTemplate> controlLoopDefinitionList = provider.getControlLoopDefinitions( - "org.onap.domain.pmsh.PMSHControlLoopDefinition", "1.2.3"); + List<ToscaNodeTemplate> controlLoopDefinitionList = + provider.getControlLoopDefinitions("org.onap.domain.pmsh.PMSHControlLoopDefinition", "1.2.3"); List<ToscaNodeTemplate> controlLoopElementNodeTemplates = provider.getControlLoopElementDefinitions(controlLoopDefinitionList.get(0)); diff --git a/runtime-controlloop/src/test/java/org/onap/policy/clamp/controlloop/runtime/commissioning/rest/CommissioningControllerTest.java b/runtime-controlloop/src/test/java/org/onap/policy/clamp/controlloop/runtime/commissioning/rest/CommissioningControllerTest.java index f7749e164..85bb2d164 100644 --- a/runtime-controlloop/src/test/java/org/onap/policy/clamp/controlloop/runtime/commissioning/rest/CommissioningControllerTest.java +++ b/runtime-controlloop/src/test/java/org/onap/policy/clamp/controlloop/runtime/commissioning/rest/CommissioningControllerTest.java @@ -21,27 +21,38 @@ package org.onap.policy.clamp.controlloop.runtime.commissioning.rest; import static org.assertj.core.api.Assertions.assertThat; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertTrue; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertTrue; import java.util.List; import javax.ws.rs.client.Entity; import javax.ws.rs.client.Invocation; import javax.ws.rs.core.Response; -import org.junit.AfterClass; -import org.junit.BeforeClass; -import org.junit.Test; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; import org.onap.policy.clamp.controlloop.models.messages.rest.commissioning.CommissioningResponse; +import org.onap.policy.clamp.controlloop.runtime.main.parameters.ClRuntimeParameterGroup; import org.onap.policy.clamp.controlloop.runtime.util.rest.CommonRestController; import org.onap.policy.common.utils.coder.YamlJsonTranslator; import org.onap.policy.common.utils.resources.ResourceUtils; import org.onap.policy.models.provider.PolicyModelsProvider; import org.onap.policy.models.provider.PolicyModelsProviderFactory; import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate; - -public class CommissioningControllerTest extends CommonRestController { +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; +import org.springframework.boot.web.server.LocalServerPort; +import org.springframework.test.context.TestPropertySource; +import org.springframework.test.context.junit.jupiter.SpringExtension; + +@ExtendWith(SpringExtension.class) +@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT) +@TestPropertySource(locations = {"classpath:application_test.properties"}) +class CommissioningControllerTest extends CommonRestController { private static final String TOSCA_SERVICE_TEMPLATE_YAML = "src/test/resources/rest/servicetemplates/pmsh_multiple_cl_tosca.yaml"; @@ -49,62 +60,67 @@ public class CommissioningControllerTest extends CommonRestController { private static final String COMMISSIONING_ENDPOINT = "commission"; private static ToscaServiceTemplate serviceTemplate = new ToscaServiceTemplate(); + @Autowired + private ClRuntimeParameterGroup clRuntimeParameterGroup; + + @LocalServerPort + private int randomServerPort; + /** * starts Main and inserts a commissioning template. * * @throws Exception if an error occurs */ - @BeforeClass + @BeforeAll public static void setUpBeforeClass() throws Exception { - CommonRestController.setUpBeforeClass("CommissioningApi"); serviceTemplate = yamlTranslator.fromYaml(ResourceUtils.getResourceAsString(TOSCA_SERVICE_TEMPLATE_YAML), ToscaServiceTemplate.class); } - @AfterClass - public static void teardownAfterClass() { - CommonRestController.teardownAfterClass(); + @BeforeEach + public void setUpPort() { + super.setHttpPrefix(randomServerPort); } @Test - public void testSwagger() throws Exception { + void testSwagger() throws Exception { super.testSwagger(COMMISSIONING_ENDPOINT); } @Test - public void testUnauthorizedCreate() throws Exception { + void testUnauthorizedCreate() throws Exception { assertUnauthorizedPost(COMMISSIONING_ENDPOINT, Entity.json(serviceTemplate)); } @Test - public void testUnauthorizedQuery() throws Exception { + void testUnauthorizedQuery() throws Exception { assertUnauthorizedGet(COMMISSIONING_ENDPOINT); } @Test - public void testUnauthorizedQueryElements() throws Exception { + void testUnauthorizedQueryElements() throws Exception { assertUnauthorizedGet(COMMISSIONING_ENDPOINT + "/elements"); } @Test - public void testUnauthorizedDelete() throws Exception { + void testUnauthorizedDelete() throws Exception { assertUnauthorizedDelete(COMMISSIONING_ENDPOINT); } @Test - public void testCreateBadRequest() throws Exception { + void testCreateBadRequest() throws Exception { Invocation.Builder invocationBuilder = super.sendRequest(COMMISSIONING_ENDPOINT); Response resp = invocationBuilder.post(Entity.json("NotToscaServiceTempalte")); - assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), resp.getStatus()); + assertThat(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()).isEqualTo(resp.getStatus()); CommissioningResponse commissioningResponse = resp.readEntity(CommissioningResponse.class); - assertNotNull(commissioningResponse.getErrorDetails()); - assertNull(commissioningResponse.getAffectedControlLoopDefinitions()); + assertThat(commissioningResponse.getErrorDetails()).isNotNull(); + assertThat(commissioningResponse.getAffectedControlLoopDefinitions()).isNull(); } @Test - public void testCreate() throws Exception { + void testCreate() throws Exception { Invocation.Builder invocationBuilder = super.sendRequest(COMMISSIONING_ENDPOINT); Response resp = invocationBuilder.post(Entity.json(serviceTemplate)); assertEquals(Response.Status.OK.getStatusCode(), resp.getStatus()); @@ -121,7 +137,7 @@ public class CommissioningControllerTest extends CommonRestController { } @Test - public void testQuery_NoResultWithThisName() throws Exception { + void testQuery_NoResultWithThisName() throws Exception { createEntryInDB(); Invocation.Builder invocationBuilder = super.sendRequest(COMMISSIONING_ENDPOINT + "?name=noResultWithThisName"); @@ -132,7 +148,7 @@ public class CommissioningControllerTest extends CommonRestController { } @Test - public void testQuery() throws Exception { + void testQuery() throws Exception { createEntryInDB(); Invocation.Builder invocationBuilder = super.sendRequest(COMMISSIONING_ENDPOINT); @@ -144,7 +160,7 @@ public class CommissioningControllerTest extends CommonRestController { } @Test - public void testQueryElementsBadRequest() throws Exception { + void testQueryElementsBadRequest() throws Exception { createEntryInDB(); //Call get elements with no info @@ -154,7 +170,7 @@ public class CommissioningControllerTest extends CommonRestController { } @Test - public void testQueryElements() throws Exception { + void testQueryElements() throws Exception { createEntryInDB(); Invocation.Builder invocationBuilder = super.sendRequest(COMMISSIONING_ENDPOINT + "/elements" @@ -167,17 +183,17 @@ public class CommissioningControllerTest extends CommonRestController { } @Test - public void testDeleteBadRequest() throws Exception { + void testDeleteBadRequest() throws Exception { createEntryInDB(); Invocation.Builder invocationBuilder = super.sendRequest(COMMISSIONING_ENDPOINT); //Call delete with no info Response resp = invocationBuilder.delete(); - assertEquals(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), resp.getStatus()); + assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), resp.getStatus()); } @Test - public void testDelete() throws Exception { + void testDelete() throws Exception { createEntryInDB(); Invocation.Builder invocationBuilder = super.sendRequest(COMMISSIONING_ENDPOINT + "?name=" @@ -187,7 +203,7 @@ public class CommissioningControllerTest extends CommonRestController { assertEquals(Response.Status.OK.getStatusCode(), resp.getStatus()); try (PolicyModelsProvider modelsProvider = new PolicyModelsProviderFactory() - .createPolicyModelsProvider(CommonRestController.getParameters())) { + .createPolicyModelsProvider(clRuntimeParameterGroup.getDatabaseProviderParameters())) { List<ToscaServiceTemplate> templatesInDB = modelsProvider.getServiceTemplateList(null, null); assertThat(templatesInDB).isEmpty(); } @@ -195,7 +211,7 @@ public class CommissioningControllerTest extends CommonRestController { private synchronized void createEntryInDB() throws Exception { try (PolicyModelsProvider modelsProvider = new PolicyModelsProviderFactory() - .createPolicyModelsProvider(CommonRestController.getParameters())) { + .createPolicyModelsProvider(clRuntimeParameterGroup.getDatabaseProviderParameters())) { modelsProvider.createServiceTemplate(serviceTemplate); } } diff --git a/runtime-controlloop/src/test/java/org/onap/policy/clamp/controlloop/runtime/instantiation/ControlLoopInstantiationProviderTest.java b/runtime-controlloop/src/test/java/org/onap/policy/clamp/controlloop/runtime/instantiation/ControlLoopInstantiationProviderTest.java index ccc54b93f..b93bd0f0e 100644 --- a/runtime-controlloop/src/test/java/org/onap/policy/clamp/controlloop/runtime/instantiation/ControlLoopInstantiationProviderTest.java +++ b/runtime-controlloop/src/test/java/org/onap/policy/clamp/controlloop/runtime/instantiation/ControlLoopInstantiationProviderTest.java @@ -26,9 +26,8 @@ import static org.assertj.core.api.Assertions.assertThatThrownBy; import java.io.IOException; import java.util.ArrayList; import java.util.Collections; -import org.junit.Assert; -import org.junit.BeforeClass; -import org.junit.Test; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; import org.mockito.Mockito; import org.onap.policy.clamp.controlloop.common.exception.ControlLoopRuntimeException; import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoop; @@ -36,19 +35,20 @@ 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.messages.rest.instantiation.InstantiationCommand; import org.onap.policy.clamp.controlloop.models.messages.rest.instantiation.InstantiationResponse; -import org.onap.policy.clamp.controlloop.runtime.commissioning.CommissioningHandler; +import org.onap.policy.clamp.controlloop.runtime.commissioning.CommissioningProvider; +import org.onap.policy.clamp.controlloop.runtime.main.parameters.ClRuntimeParameterGroup; +import org.onap.policy.clamp.controlloop.runtime.monitoring.MonitoringProvider; import org.onap.policy.clamp.controlloop.runtime.supervision.SupervisionHandler; import org.onap.policy.clamp.controlloop.runtime.util.CommonTestData; import org.onap.policy.common.endpoints.event.comm.TopicSink; import org.onap.policy.models.base.PfModelException; -import org.onap.policy.models.provider.PolicyModelsProviderParameters; import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier; /** * Class to perform unit test of {@link ControlLoopInstantiationProvider}}. * */ -public class ControlLoopInstantiationProviderTest { +class ControlLoopInstantiationProviderTest { private static final String CL_INSTANTIATION_CREATE_JSON = "src/test/resources/rest/controlloops/ControlLoops.json"; private static final String CL_INSTANTIATION_UPDATE_JSON = @@ -66,10 +66,10 @@ public class ControlLoopInstantiationProviderTest { private static final String ORDERED_STATE_INVALID = "ordered state invalid or not specified on command"; private static final String CONTROLLOOP_ELEMENT_NAME_NOT_FOUND = "\"ControlLoops\" INVALID, item has status INVALID\n" - + " \"entry org.onap.domain.pmsh.PMSHControlLoopDefinition\" INVALID, item has status INVALID\n" - + " \"entry org.onap.domain.pmsh.DCAEMicroservice\" INVALID, Not FOUND\n" - + " \"entry org.onap.domain.pmsh.PMSHControlLoopDefinition\" INVALID, item has status INVALID\n" - + " \"entry org.onap.domain.pmsh.DCAEMicroservice\" INVALID, Not FOUND\n"; + + " \"entry org.onap.domain.pmsh.PMSHControlLoopDefinition\" INVALID, item has status INVALID\n" + + " \"entry org.onap.domain.pmsh.DCAEMicroservice\" INVALID, Not FOUND\n" + + " \"entry org.onap.domain.pmsh.PMSHControlLoopDefinition\" INVALID, item has status INVALID\n" + + " \"entry org.onap.domain.pmsh.DCAEMicroservice\" INVALID, Not FOUND\n"; private static final String CONTROLLOOP_DEFINITION_NOT_FOUND = "\"ControlLoops\" INVALID, item has status INVALID\n" + " \"entry org.onap.domain.PMSHControlLoopDefinition\" INVALID, item has status INVALID\n" @@ -79,62 +79,61 @@ public class ControlLoopInstantiationProviderTest { + " item \"ControlLoop\" value \"org.onap.domain.PMSHControlLoopDefinition\" INVALID," + " Commissioned control loop definition not FOUND\n"; - private static PolicyModelsProviderParameters databaseProviderParameters; + private static ClRuntimeParameterGroup controlLoopParameters; private static SupervisionHandler supervisionHandler; - private static CommissioningHandler commissioningHandler; + private static CommissioningProvider commissioningProvider; /** * setup Db Provider Parameters. * * @throws PfModelException if an error occurs */ - @BeforeClass + @BeforeAll public static void setupDbProviderParameters() throws PfModelException { - databaseProviderParameters = - CommonTestData.geParameterGroup(0, "instantproviderdb").getDatabaseProviderParameters(); - commissioningHandler = new CommissioningHandler(CommonTestData.geParameterGroup(0, "instantproviderdb")); - commissioningHandler.startProviders(); - supervisionHandler = new SupervisionHandler(CommonTestData.geParameterGroup(0, "instantproviderdb")); + controlLoopParameters = CommonTestData.geParameterGroup(0, "instantproviderdb"); + commissioningProvider = new CommissioningProvider(controlLoopParameters); + var monitoringProvider = new MonitoringProvider(controlLoopParameters); + supervisionHandler = new SupervisionHandler(controlLoopParameters, monitoringProvider, commissioningProvider); supervisionHandler.startProviders(); supervisionHandler.startAndRegisterPublishers(Collections.singletonList(Mockito.mock(TopicSink.class))); } @Test - public void testInstantiationCrud() throws Exception { + void testInstantiationCrud() throws Exception { ControlLoops controlLoopsCreate = InstantiationUtils.getControlLoopsFromResource(CL_INSTANTIATION_CREATE_JSON, "Crud"); ControlLoops controlLoopsDb = getControlLoopsFromDb(controlLoopsCreate); assertThat(controlLoopsDb.getControlLoopList()).isEmpty(); - try (ControlLoopInstantiationProvider instantiationProvider = - new ControlLoopInstantiationProvider(databaseProviderParameters)) { + try (ControlLoopInstantiationProvider instantiationProvider = new ControlLoopInstantiationProvider( + controlLoopParameters, commissioningProvider, supervisionHandler)) { // to validate control Loop, it needs to define ToscaServiceTemplate - InstantiationUtils.storeToscaServiceTemplate(TOSCA_TEMPLATE_YAML, databaseProviderParameters); + InstantiationUtils.storeToscaServiceTemplate(TOSCA_TEMPLATE_YAML, commissioningProvider); InstantiationResponse instantiationResponse = instantiationProvider.createControlLoops(controlLoopsCreate); InstantiationUtils.assertInstantiationResponse(instantiationResponse, controlLoopsCreate); controlLoopsDb = getControlLoopsFromDb(controlLoopsCreate); assertThat(controlLoopsDb.getControlLoopList()).isNotEmpty(); - Assert.assertEquals(controlLoopsCreate, controlLoopsDb); + assertThat(controlLoopsCreate).isEqualTo(controlLoopsDb); for (ControlLoop controlLoop : controlLoopsCreate.getControlLoopList()) { ControlLoops controlLoopsGet = instantiationProvider.getControlLoops(controlLoop.getName(), controlLoop.getVersion()); assertThat(controlLoopsGet.getControlLoopList()).hasSize(1); - Assert.assertEquals(controlLoop, controlLoopsGet.getControlLoopList().get(0)); + assertThat(controlLoop).isEqualTo(controlLoopsGet.getControlLoopList().get(0)); } ControlLoops controlLoopsUpdate = InstantiationUtils.getControlLoopsFromResource(CL_INSTANTIATION_UPDATE_JSON, "Crud"); - Assert.assertNotEquals(controlLoopsUpdate, controlLoopsDb); + assertThat(controlLoopsUpdate).isNotEqualTo(controlLoopsDb); instantiationResponse = instantiationProvider.updateControlLoops(controlLoopsUpdate); InstantiationUtils.assertInstantiationResponse(instantiationResponse, controlLoopsUpdate); controlLoopsDb = getControlLoopsFromDb(controlLoopsCreate); assertThat(controlLoopsDb.getControlLoopList()).isNotEmpty(); - Assert.assertEquals(controlLoopsUpdate, controlLoopsDb); + assertThat(controlLoopsUpdate).isEqualTo(controlLoopsDb); InstantiationCommand instantiationCommand = InstantiationUtils.getInstantiationCommandFromResource(CL_INSTANTIATION_CHANGE_STATE_JSON, "Crud"); @@ -145,8 +144,8 @@ public class ControlLoopInstantiationProviderTest { ControlLoops controlLoopsGet = instantiationProvider.getControlLoops(toscaConceptIdentifier.getName(), toscaConceptIdentifier.getVersion()); assertThat(controlLoopsGet.getControlLoopList()).hasSize(1); - Assert.assertEquals(instantiationCommand.getOrderedState(), - controlLoopsGet.getControlLoopList().get(0).getOrderedState()); + assertThat(instantiationCommand.getOrderedState()) + .isEqualTo(controlLoopsGet.getControlLoopList().get(0).getOrderedState()); } // in order to delete a controlLoop the state must be UNINITIALISED @@ -166,8 +165,8 @@ public class ControlLoopInstantiationProviderTest { ControlLoops controlLoopsDb = new ControlLoops(); controlLoopsDb.setControlLoopList(new ArrayList<>()); - try (ControlLoopInstantiationProvider instantiationProvider = - new ControlLoopInstantiationProvider(databaseProviderParameters)) { + try (ControlLoopInstantiationProvider instantiationProvider = new ControlLoopInstantiationProvider( + controlLoopParameters, commissioningProvider, supervisionHandler)) { for (ControlLoop controlLoop : controlLoopsSource.getControlLoopList()) { ControlLoops controlLoopsFromDb = @@ -179,22 +178,22 @@ public class ControlLoopInstantiationProviderTest { } @Test - public void testInstantiationDelete() throws Exception { + void testInstantiationDelete() throws Exception { ControlLoops controlLoops = InstantiationUtils.getControlLoopsFromResource(CL_INSTANTIATION_CREATE_JSON, "Delete"); assertThat(getControlLoopsFromDb(controlLoops).getControlLoopList()).isEmpty(); ControlLoop controlLoop0 = controlLoops.getControlLoopList().get(0); - try (ControlLoopInstantiationProvider instantiationProvider = - new ControlLoopInstantiationProvider(databaseProviderParameters)) { + try (ControlLoopInstantiationProvider instantiationProvider = new ControlLoopInstantiationProvider( + controlLoopParameters, commissioningProvider, supervisionHandler)) { // to validate control Loop, it needs to define ToscaServiceTemplate - InstantiationUtils.storeToscaServiceTemplate(TOSCA_TEMPLATE_YAML, databaseProviderParameters); + InstantiationUtils.storeToscaServiceTemplate(TOSCA_TEMPLATE_YAML, commissioningProvider); assertThatThrownBy( () -> instantiationProvider.deleteControlLoop(controlLoop0.getName(), controlLoop0.getVersion())) - .hasMessageMatching(CONTROL_LOOP_NOT_FOUND); + .hasMessageMatching(CONTROL_LOOP_NOT_FOUND); InstantiationUtils.assertInstantiationResponse(instantiationProvider.createControlLoops(controlLoops), controlLoops); @@ -225,29 +224,29 @@ public class ControlLoopInstantiationProviderTest { controlLoop.setState(state); - try (ControlLoopInstantiationProvider instantiationProvider = - new ControlLoopInstantiationProvider(databaseProviderParameters)) { + try (ControlLoopInstantiationProvider instantiationProvider = new ControlLoopInstantiationProvider( + controlLoopParameters, commissioningProvider, supervisionHandler)) { instantiationProvider.updateControlLoops(controlLoops); assertThatThrownBy( () -> instantiationProvider.deleteControlLoop(controlLoop.getName(), controlLoop.getVersion())) - .hasMessageMatching(String.format(DELETE_BAD_REQUEST, state)); + .hasMessageMatching(String.format(DELETE_BAD_REQUEST, state)); } } @Test - public void testCreateControlLoops_NoDuplicates() throws Exception { + void testCreateControlLoops_NoDuplicates() throws Exception { ControlLoops controlLoopsCreate = InstantiationUtils.getControlLoopsFromResource(CL_INSTANTIATION_CREATE_JSON, "NoDuplicates"); ControlLoops controlLoopsDb = getControlLoopsFromDb(controlLoopsCreate); assertThat(controlLoopsDb.getControlLoopList()).isEmpty(); - try (ControlLoopInstantiationProvider instantiationProvider = - new ControlLoopInstantiationProvider(databaseProviderParameters)) { + try (ControlLoopInstantiationProvider instantiationProvider = new ControlLoopInstantiationProvider( + controlLoopParameters, commissioningProvider, supervisionHandler)) { // to validate control Loop, it needs to define ToscaServiceTemplate - InstantiationUtils.storeToscaServiceTemplate(TOSCA_TEMPLATE_YAML, databaseProviderParameters); + InstantiationUtils.storeToscaServiceTemplate(TOSCA_TEMPLATE_YAML, commissioningProvider); InstantiationResponse instantiationResponse = instantiationProvider.createControlLoops(controlLoopsCreate); InstantiationUtils.assertInstantiationResponse(instantiationResponse, controlLoopsCreate); @@ -262,15 +261,15 @@ public class ControlLoopInstantiationProviderTest { } @Test - public void testCreateControlLoops_CommissionedClElementNotFound() throws Exception { + void testCreateControlLoops_CommissionedClElementNotFound() throws Exception { ControlLoops controlLoops = InstantiationUtils .getControlLoopsFromResource(CL_INSTANTIATION_DEFINITION_NAME_NOT_FOUND_JSON, "ClElementNotFound"); - try (ControlLoopInstantiationProvider provider = - new ControlLoopInstantiationProvider(databaseProviderParameters)) { + try (ControlLoopInstantiationProvider provider = new ControlLoopInstantiationProvider(controlLoopParameters, + commissioningProvider, supervisionHandler)) { // to validate control Loop, it needs to define ToscaServiceTemplate - InstantiationUtils.storeToscaServiceTemplate(TOSCA_TEMPLATE_YAML, databaseProviderParameters); + InstantiationUtils.storeToscaServiceTemplate(TOSCA_TEMPLATE_YAML, commissioningProvider); assertThat(getControlLoopsFromDb(controlLoops).getControlLoopList()).isEmpty(); @@ -280,41 +279,41 @@ public class ControlLoopInstantiationProviderTest { } @Test - public void testCreateControlLoops_CommissionedClNotFound() throws Exception { + void testCreateControlLoops_CommissionedClNotFound() throws Exception { ControlLoops controlLoops = InstantiationUtils .getControlLoopsFromResource(CL_INSTANTIATION_CONTROLLOOP_DEFINITION_NOT_FOUND_JSON, "ClNotFound"); assertThat(getControlLoopsFromDb(controlLoops).getControlLoopList()).isEmpty(); - try (ControlLoopInstantiationProvider provider = - new ControlLoopInstantiationProvider(databaseProviderParameters)) { + try (ControlLoopInstantiationProvider provider = new ControlLoopInstantiationProvider(controlLoopParameters, + commissioningProvider, supervisionHandler)) { assertThatThrownBy(() -> provider.createControlLoops(controlLoops)) .hasMessageMatching(CONTROLLOOP_DEFINITION_NOT_FOUND); } } @Test - public void testIssueControlLoopCommand_OrderedStateInvalid() throws ControlLoopRuntimeException, IOException { - try (ControlLoopInstantiationProvider instantiationProvider = - new ControlLoopInstantiationProvider(databaseProviderParameters)) { + void testIssueControlLoopCommand_OrderedStateInvalid() throws ControlLoopRuntimeException, IOException { + try (ControlLoopInstantiationProvider instantiationProvider = new ControlLoopInstantiationProvider( + controlLoopParameters, commissioningProvider, supervisionHandler)) { assertThatThrownBy(() -> instantiationProvider.issueControlLoopCommand(new InstantiationCommand())) .hasMessageMatching(ORDERED_STATE_INVALID); } } @Test - public void testInstantiationVersions() throws Exception { + void testInstantiationVersions() throws Exception { // create controlLoops V1 ControlLoops controlLoopsV1 = InstantiationUtils.getControlLoopsFromResource(CL_INSTANTIATION_CREATE_JSON, "V1"); assertThat(getControlLoopsFromDb(controlLoopsV1).getControlLoopList()).isEmpty(); - try (ControlLoopInstantiationProvider instantiationProvider = - new ControlLoopInstantiationProvider(databaseProviderParameters)) { + try (ControlLoopInstantiationProvider instantiationProvider = new ControlLoopInstantiationProvider( + controlLoopParameters, commissioningProvider, supervisionHandler)) { // to validate control Loop, it needs to define ToscaServiceTemplate - InstantiationUtils.storeToscaServiceTemplate(TOSCA_TEMPLATE_YAML, databaseProviderParameters); + InstantiationUtils.storeToscaServiceTemplate(TOSCA_TEMPLATE_YAML, commissioningProvider); InstantiationUtils.assertInstantiationResponse(instantiationProvider.createControlLoops(controlLoopsV1), controlLoopsV1); @@ -331,7 +330,7 @@ public class ControlLoopInstantiationProviderTest { ControlLoops controlLoopsGet = instantiationProvider.getControlLoops(controlLoop.getName(), controlLoop.getVersion()); assertThat(controlLoopsGet.getControlLoopList()).hasSize(1); - Assert.assertEquals(controlLoop, controlLoopsGet.getControlLoopList().get(0)); + assertThat(controlLoop).isEqualTo(controlLoopsGet.getControlLoopList().get(0)); } // DELETE controlLoops V1 @@ -351,7 +350,7 @@ public class ControlLoopInstantiationProviderTest { ControlLoops controlLoopsGet = instantiationProvider.getControlLoops(controlLoop.getName(), controlLoop.getVersion()); assertThat(controlLoopsGet.getControlLoopList()).hasSize(1); - Assert.assertEquals(controlLoop, controlLoopsGet.getControlLoopList().get(0)); + assertThat(controlLoop).isEqualTo(controlLoopsGet.getControlLoopList().get(0)); } // DELETE controlLoops V2 diff --git a/runtime-controlloop/src/test/java/org/onap/policy/clamp/controlloop/runtime/instantiation/InstantiationUtils.java b/runtime-controlloop/src/test/java/org/onap/policy/clamp/controlloop/runtime/instantiation/InstantiationUtils.java index 958d91dff..9a9fc1680 100644 --- a/runtime-controlloop/src/test/java/org/onap/policy/clamp/controlloop/runtime/instantiation/InstantiationUtils.java +++ b/runtime-controlloop/src/test/java/org/onap/policy/clamp/controlloop/runtime/instantiation/InstantiationUtils.java @@ -20,25 +20,22 @@ package org.onap.policy.clamp.controlloop.runtime.instantiation; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertTrue; +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; import java.io.File; -import org.junit.Assert; 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.messages.rest.instantiation.InstantiationCommand; import org.onap.policy.clamp.controlloop.models.messages.rest.instantiation.InstantiationResponse; +import org.onap.policy.clamp.controlloop.runtime.commissioning.CommissioningProvider; import org.onap.policy.common.utils.coder.Coder; import org.onap.policy.common.utils.coder.CoderException; import org.onap.policy.common.utils.coder.StandardCoder; import org.onap.policy.common.utils.coder.YamlJsonTranslator; import org.onap.policy.common.utils.resources.ResourceUtils; import org.onap.policy.models.base.PfModelException; -import org.onap.policy.models.provider.PolicyModelsProvider; -import org.onap.policy.models.provider.PolicyModelsProviderFactory; -import org.onap.policy.models.provider.PolicyModelsProviderParameters; import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier; import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate; @@ -91,9 +88,9 @@ public class InstantiationUtils { * @param controlLoops ControlLoops */ public static void assertInstantiationResponse(InstantiationResponse response, ControlLoops controlLoops) { - assertNotNull(response); - Assert.assertNull(response.getErrorDetails()); - assertEquals(response.getAffectedControlLoops().size(), controlLoops.getControlLoopList().size()); + assertThat(response).isNotNull(); + assertThat(response.getErrorDetails()).isNull(); + assertThat(response.getAffectedControlLoops().size()).isEqualTo(controlLoops.getControlLoopList().size()); for (ControlLoop controlLoop : controlLoops.getControlLoopList()) { assertTrue(response.getAffectedControlLoops().stream() .filter(ac -> ac.equals(controlLoop.getKey().asIdentifier())).findAny().isPresent()); @@ -107,7 +104,7 @@ public class InstantiationUtils { * @param command InstantiationCommand */ public static void assertInstantiationResponse(InstantiationResponse response, InstantiationCommand command) { - assertNotNull(response); + assertThat(response).isNotNull(); assertEquals(response.getAffectedControlLoops().size(), command.getControlLoopIdentifierList().size()); for (ToscaConceptIdentifier toscaConceptIdentifier : command.getControlLoopIdentifierList()) { assertTrue(response.getAffectedControlLoops().stream() @@ -122,8 +119,8 @@ public class InstantiationUtils { * @param controlLoop ControlLoop */ public static void assertInstantiationResponse(InstantiationResponse response, ControlLoop controlLoop) { - assertNotNull(response); - Assert.assertNull(response.getErrorDetails()); + assertThat(response).isNotNull(); + assertThat(response.getErrorDetails()).isNull(); assertEquals(1, response.getAffectedControlLoops().size()); assertEquals(0, response.getAffectedControlLoops().get(0).compareTo(controlLoop.getKey().asIdentifier())); } @@ -132,18 +129,15 @@ public class InstantiationUtils { * Store ToscaServiceTemplate from resource to DB. * * @param path path of the resource - * @param parameters The parameters for the implementation of the PolicyModelProvider + * @param commissioningProvider The CommissioningProvider * @throws PfModelException if an error occurs */ - public static void storeToscaServiceTemplate(String path, PolicyModelsProviderParameters parameters) + public static void storeToscaServiceTemplate(String path, CommissioningProvider commissioningProvider) throws PfModelException { ToscaServiceTemplate template = yamlTranslator.fromYaml(ResourceUtils.getResourceAsString(path), ToscaServiceTemplate.class); - try (PolicyModelsProvider modelsProvider = - new PolicyModelsProviderFactory().createPolicyModelsProvider(parameters)) { - modelsProvider.createServiceTemplate(template); - } + commissioningProvider.createControlLoopDefinitions(template); } } diff --git a/runtime-controlloop/src/test/java/org/onap/policy/clamp/controlloop/runtime/instantiation/rest/InstantiationControllerTest.java b/runtime-controlloop/src/test/java/org/onap/policy/clamp/controlloop/runtime/instantiation/rest/InstantiationControllerTest.java index 71e762455..6c9674dec 100644 --- a/runtime-controlloop/src/test/java/org/onap/policy/clamp/controlloop/runtime/instantiation/rest/InstantiationControllerTest.java +++ b/runtime-controlloop/src/test/java/org/onap/policy/clamp/controlloop/runtime/instantiation/rest/InstantiationControllerTest.java @@ -21,30 +21,41 @@ package org.onap.policy.clamp.controlloop.runtime.instantiation.rest; import static org.assertj.core.api.Assertions.assertThat; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertNull; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNull; import javax.ws.rs.client.Entity; import javax.ws.rs.client.Invocation; import javax.ws.rs.core.Response; -import org.junit.AfterClass; -import org.junit.BeforeClass; -import org.junit.Test; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; 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.messages.rest.instantiation.InstantiationCommand; import org.onap.policy.clamp.controlloop.models.messages.rest.instantiation.InstantiationResponse; +import org.onap.policy.clamp.controlloop.runtime.commissioning.CommissioningProvider; import org.onap.policy.clamp.controlloop.runtime.instantiation.ControlLoopInstantiationProvider; import org.onap.policy.clamp.controlloop.runtime.instantiation.InstantiationUtils; +import org.onap.policy.clamp.controlloop.runtime.main.rest.InstantiationController; import org.onap.policy.clamp.controlloop.runtime.util.rest.CommonRestController; import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; +import org.springframework.boot.web.server.LocalServerPort; +import org.springframework.test.context.TestPropertySource; +import org.springframework.test.context.junit.jupiter.SpringExtension; /** * Class to perform unit test of {@link InstantiationController}}. * */ -public class InstantiationControllerTest extends CommonRestController { +@ExtendWith(SpringExtension.class) +@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT) +@TestPropertySource(locations = {"classpath:application_test.properties"}) +class InstantiationControllerTest extends CommonRestController { private static final String CL_INSTANTIATION_CREATE_JSON = "src/test/resources/rest/controlloops/ControlLoops.json"; @@ -61,38 +72,38 @@ public class InstantiationControllerTest extends CommonRestController { private static final String INSTANTIATION_COMMAND_ENDPOINT = "instantiation/command"; + @Autowired + private ControlLoopInstantiationProvider instantiationProvider; + + @Autowired + private CommissioningProvider commissioningProvider; + + @LocalServerPort + private int randomServerPort; + /** * starts Main and inserts a commissioning template. * * @throws Exception if an error occurs */ - @BeforeClass - public static void setUpBeforeClass() throws Exception { - CommonRestController.setUpBeforeClass("InstApi"); - + @BeforeEach + public void setUpBeforeClass() throws Exception { // to validate control Loop, it needs to define ToscaServiceTemplate - InstantiationUtils.storeToscaServiceTemplate(TOSCA_TEMPLATE_YAML, getParameters()); - - ControlLoops controlLoops = - InstantiationUtils.getControlLoopsFromResource(CL_INSTANTIATION_CREATE_JSON, "Command"); - try (ControlLoopInstantiationProvider instantiationProvider = - new ControlLoopInstantiationProvider(getParameters())) { - instantiationProvider.createControlLoops(controlLoops); - } + InstantiationUtils.storeToscaServiceTemplate(TOSCA_TEMPLATE_YAML, commissioningProvider); } - @AfterClass - public static void teardownAfterClass() { - CommonRestController.teardownAfterClass(); + @BeforeEach + public void setUpPort() { + super.setHttpPrefix(randomServerPort); } @Test - public void testSwagger() throws Exception { + void testSwagger() throws Exception { super.testSwagger(INSTANTIATION_ENDPOINT); } @Test - public void testCreate_Unauthorized() throws Exception { + void testCreate_Unauthorized() throws Exception { ControlLoops controlLoops = InstantiationUtils.getControlLoopsFromResource(CL_INSTANTIATION_CREATE_JSON, "Unauthorized"); @@ -100,12 +111,12 @@ public class InstantiationControllerTest extends CommonRestController { } @Test - public void testQuery_Unauthorized() throws Exception { + void testQuery_Unauthorized() throws Exception { assertUnauthorizedGet(INSTANTIATION_ENDPOINT); } @Test - public void testUpdate_Unauthorized() throws Exception { + void testUpdate_Unauthorized() throws Exception { ControlLoops controlLoops = InstantiationUtils.getControlLoopsFromResource(CL_INSTANTIATION_UPDATE_JSON, "Unauthorized"); @@ -113,12 +124,12 @@ public class InstantiationControllerTest extends CommonRestController { } @Test - public void testDelete_Unauthorized() throws Exception { + void testDelete_Unauthorized() throws Exception { assertUnauthorizedDelete(INSTANTIATION_ENDPOINT); } @Test - public void testCommand_Unauthorized() throws Exception { + void testCommand_Unauthorized() throws Exception { InstantiationCommand instantiationCommand = InstantiationUtils .getInstantiationCommandFromResource(CL_INSTANTIATION_CHANGE_STATE_JSON, "Unauthorized"); @@ -126,7 +137,7 @@ public class InstantiationControllerTest extends CommonRestController { } @Test - public void testCreate() throws Exception { + void testCreate() throws Exception { ControlLoops controlLoopsFromRsc = InstantiationUtils.getControlLoopsFromResource(CL_INSTANTIATION_CREATE_JSON, "Create"); @@ -136,21 +147,18 @@ public class InstantiationControllerTest extends CommonRestController { InstantiationResponse instResponse = resp.readEntity(InstantiationResponse.class); InstantiationUtils.assertInstantiationResponse(instResponse, controlLoopsFromRsc); - try (ControlLoopInstantiationProvider instantiationProvider = - new ControlLoopInstantiationProvider(getParameters())) { - for (ControlLoop controlLoopFromRsc : controlLoopsFromRsc.getControlLoopList()) { - ControlLoops controlLoopsFromDb = instantiationProvider.getControlLoops( - controlLoopFromRsc.getKey().getName(), controlLoopFromRsc.getKey().getVersion()); + for (ControlLoop controlLoopFromRsc : controlLoopsFromRsc.getControlLoopList()) { + ControlLoops controlLoopsFromDb = instantiationProvider + .getControlLoops(controlLoopFromRsc.getKey().getName(), controlLoopFromRsc.getKey().getVersion()); - assertNotNull(controlLoopsFromDb); - assertThat(controlLoopsFromDb.getControlLoopList()).hasSize(1); - assertEquals(controlLoopFromRsc, controlLoopsFromDb.getControlLoopList().get(0)); - } + assertNotNull(controlLoopsFromDb); + assertThat(controlLoopsFromDb.getControlLoopList()).hasSize(1); + assertEquals(controlLoopFromRsc, controlLoopsFromDb.getControlLoopList().get(0)); } } @Test - public void testCreateBadRequest() throws Exception { + void testCreateBadRequest() throws Exception { ControlLoops controlLoopsFromRsc = InstantiationUtils.getControlLoopsFromResource(CL_INSTANTIATION_CREATE_JSON, "CreateBadRequest"); @@ -167,7 +175,7 @@ public class InstantiationControllerTest extends CommonRestController { } @Test - public void testQuery_NoResultWithThisName() throws Exception { + void testQuery_NoResultWithThisName() throws Exception { Invocation.Builder invocationBuilder = super.sendRequest(INSTANTIATION_ENDPOINT + "?name=noResultWithThisName"); Response rawresp = invocationBuilder.buildGet().invoke(); assertEquals(Response.Status.OK.getStatusCode(), rawresp.getStatus()); @@ -176,14 +184,10 @@ public class InstantiationControllerTest extends CommonRestController { } @Test - public void testQuery() throws Exception { + void testQuery() throws Exception { // inserts a ControlLoops to DB - ControlLoops controlLoops = - InstantiationUtils.getControlLoopsFromResource(CL_INSTANTIATION_CREATE_JSON, "Query"); - try (ControlLoopInstantiationProvider instantiationProvider = - new ControlLoopInstantiationProvider(getParameters())) { - instantiationProvider.createControlLoops(controlLoops); - } + var controlLoops = InstantiationUtils.getControlLoopsFromResource(CL_INSTANTIATION_CREATE_JSON, "Query"); + instantiationProvider.createControlLoops(controlLoops); for (ControlLoop controlLoopFromRsc : controlLoops.getControlLoopList()) { Invocation.Builder invocationBuilder = @@ -198,37 +202,32 @@ public class InstantiationControllerTest extends CommonRestController { } @Test - public void testUpdate() throws Exception { + void testUpdate() throws Exception { ControlLoops controlLoopsCreate = InstantiationUtils.getControlLoopsFromResource(CL_INSTANTIATION_CREATE_JSON, "Update"); - ControlLoops controlLoops = - InstantiationUtils.getControlLoopsFromResource(CL_INSTANTIATION_UPDATE_JSON, "Update"); - - try (ControlLoopInstantiationProvider instantiationProvider = - new ControlLoopInstantiationProvider(getParameters())) { - instantiationProvider.createControlLoops(controlLoopsCreate); + var controlLoops = InstantiationUtils.getControlLoopsFromResource(CL_INSTANTIATION_UPDATE_JSON, "Update"); + instantiationProvider.createControlLoops(controlLoopsCreate); - Invocation.Builder invocationBuilder = super.sendRequest(INSTANTIATION_ENDPOINT); - Response resp = invocationBuilder.put(Entity.json(controlLoops)); - assertEquals(Response.Status.OK.getStatusCode(), resp.getStatus()); + Invocation.Builder invocationBuilder = super.sendRequest(INSTANTIATION_ENDPOINT); + Response resp = invocationBuilder.put(Entity.json(controlLoops)); + assertEquals(Response.Status.OK.getStatusCode(), resp.getStatus()); - InstantiationResponse instResponse = resp.readEntity(InstantiationResponse.class); - InstantiationUtils.assertInstantiationResponse(instResponse, controlLoops); + InstantiationResponse instResponse = resp.readEntity(InstantiationResponse.class); + InstantiationUtils.assertInstantiationResponse(instResponse, controlLoops); - for (ControlLoop controlLoopUpdate : controlLoops.getControlLoopList()) { - ControlLoops controlLoopsFromDb = instantiationProvider - .getControlLoops(controlLoopUpdate.getKey().getName(), controlLoopUpdate.getKey().getVersion()); + for (ControlLoop controlLoopUpdate : controlLoops.getControlLoopList()) { + ControlLoops controlLoopsFromDb = instantiationProvider + .getControlLoops(controlLoopUpdate.getKey().getName(), controlLoopUpdate.getKey().getVersion()); - assertNotNull(controlLoopsFromDb); - assertThat(controlLoopsFromDb.getControlLoopList()).hasSize(1); - assertEquals(controlLoopUpdate, controlLoopsFromDb.getControlLoopList().get(0)); - } + assertNotNull(controlLoopsFromDb); + assertThat(controlLoopsFromDb.getControlLoopList()).hasSize(1); + assertEquals(controlLoopUpdate, controlLoopsFromDb.getControlLoopList().get(0)); } } @Test - public void testDelete_NoResultWithThisName() throws Exception { + void testDelete_NoResultWithThisName() throws Exception { Invocation.Builder invocationBuilder = super.sendRequest(INSTANTIATION_ENDPOINT + "?name=noResultWithThisName"); Response resp = invocationBuilder.delete(); assertEquals(Response.Status.NOT_FOUND.getStatusCode(), resp.getStatus()); @@ -238,56 +237,51 @@ public class InstantiationControllerTest extends CommonRestController { } @Test - public void testDelete() throws Exception { + void testDelete() throws Exception { ControlLoops controlLoopsFromRsc = InstantiationUtils.getControlLoopsFromResource(CL_INSTANTIATION_CREATE_JSON, "Delete"); - try (ControlLoopInstantiationProvider instantiationProvider = - new ControlLoopInstantiationProvider(getParameters())) { - instantiationProvider.createControlLoops(controlLoopsFromRsc); - - for (ControlLoop controlLoopFromRsc : controlLoopsFromRsc.getControlLoopList()) { - Invocation.Builder invocationBuilder = - super.sendRequest(INSTANTIATION_ENDPOINT + "?name=" + controlLoopFromRsc.getKey().getName() - + "&version=" + controlLoopFromRsc.getKey().getVersion()); - Response resp = invocationBuilder.delete(); - assertEquals(Response.Status.OK.getStatusCode(), resp.getStatus()); - InstantiationResponse instResponse = resp.readEntity(InstantiationResponse.class); - InstantiationUtils.assertInstantiationResponse(instResponse, controlLoopFromRsc); - - ControlLoops controlLoopsFromDb = instantiationProvider.getControlLoops( - controlLoopFromRsc.getKey().getName(), controlLoopFromRsc.getKey().getVersion()); - assertThat(controlLoopsFromDb.getControlLoopList()).isEmpty(); - } + + instantiationProvider.createControlLoops(controlLoopsFromRsc); + + for (ControlLoop controlLoopFromRsc : controlLoopsFromRsc.getControlLoopList()) { + Invocation.Builder invocationBuilder = super.sendRequest(INSTANTIATION_ENDPOINT + "?name=" + + controlLoopFromRsc.getKey().getName() + "&version=" + controlLoopFromRsc.getKey().getVersion()); + Response resp = invocationBuilder.delete(); + assertEquals(Response.Status.OK.getStatusCode(), resp.getStatus()); + InstantiationResponse instResponse = resp.readEntity(InstantiationResponse.class); + InstantiationUtils.assertInstantiationResponse(instResponse, controlLoopFromRsc); + + ControlLoops controlLoopsFromDb = instantiationProvider + .getControlLoops(controlLoopFromRsc.getKey().getName(), controlLoopFromRsc.getKey().getVersion()); + assertThat(controlLoopsFromDb.getControlLoopList()).isEmpty(); } } @Test - public void testDeleteBadRequest() throws Exception { + void testDeleteBadRequest() throws Exception { ControlLoops controlLoopsFromRsc = InstantiationUtils.getControlLoopsFromResource(CL_INSTANTIATION_CREATE_JSON, "DelBadRequest"); - try (ControlLoopInstantiationProvider instantiationProvider = - new ControlLoopInstantiationProvider(getParameters())) { - instantiationProvider.createControlLoops(controlLoopsFromRsc); - - for (ControlLoop controlLoopFromRsc : controlLoopsFromRsc.getControlLoopList()) { - Invocation.Builder invocationBuilder = - super.sendRequest(INSTANTIATION_ENDPOINT + "?name=" + controlLoopFromRsc.getKey().getName()); - Response resp = invocationBuilder.delete(); - // should be BAD_REQUEST - assertEquals(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), resp.getStatus()); - } + + instantiationProvider.createControlLoops(controlLoopsFromRsc); + + for (ControlLoop controlLoopFromRsc : controlLoopsFromRsc.getControlLoopList()) { + Invocation.Builder invocationBuilder = + super.sendRequest(INSTANTIATION_ENDPOINT + "?name=" + controlLoopFromRsc.getKey().getName()); + Response resp = invocationBuilder.delete(); + // should be BAD_REQUEST + assertEquals(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), resp.getStatus()); } } @Test - public void testCommand_NotFound1() throws Exception { + void testCommand_NotFound1() throws Exception { Invocation.Builder invocationBuilder = super.sendRequest(INSTANTIATION_COMMAND_ENDPOINT); Response resp = invocationBuilder.put(Entity.json(new InstantiationCommand())); assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), resp.getStatus()); } @Test - public void testCommand_NotFound2() throws Exception { + void testCommand_NotFound2() throws Exception { InstantiationCommand command = InstantiationUtils.getInstantiationCommandFromResource(CL_INSTANTIATION_CHANGE_STATE_JSON, "Command"); command.setOrderedState(null); @@ -298,7 +292,10 @@ public class InstantiationControllerTest extends CommonRestController { } @Test - public void testCommand() throws Exception { + void testCommand() throws Exception { + var controlLoops = InstantiationUtils.getControlLoopsFromResource(CL_INSTANTIATION_CREATE_JSON, "Command"); + instantiationProvider.createControlLoops(controlLoops); + InstantiationCommand command = InstantiationUtils.getInstantiationCommandFromResource(CL_INSTANTIATION_CHANGE_STATE_JSON, "Command"); @@ -309,14 +306,11 @@ public class InstantiationControllerTest extends CommonRestController { InstantiationUtils.assertInstantiationResponse(instResponse, command); // check passive state on DB - try (ControlLoopInstantiationProvider instantiationProvider = - new ControlLoopInstantiationProvider(getParameters())) { - 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()); - } + 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()); } } } diff --git a/runtime-controlloop/src/test/java/org/onap/policy/clamp/controlloop/runtime/main/rest/RestControllerTest.java b/runtime-controlloop/src/test/java/org/onap/policy/clamp/controlloop/runtime/main/rest/RestControllerTest.java deleted file mode 100644 index 4f68b4f8c..000000000 --- a/runtime-controlloop/src/test/java/org/onap/policy/clamp/controlloop/runtime/main/rest/RestControllerTest.java +++ /dev/null @@ -1,71 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * Copyright (C) 2021 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. - * - * SPDX-License-Identifier: Apache-2.0 - * ============LICENSE_END========================================================= - */ - -package org.onap.policy.clamp.controlloop.runtime.main.rest; - -import static org.assertj.core.api.Assertions.assertThat; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; - -import java.util.UUID; -import javax.ws.rs.Produces; -import javax.ws.rs.core.MediaType; -import javax.ws.rs.core.Response; -import org.junit.Test; - -/** - * Class to perform unit test of {@link RestController}}. - * - */ -public class RestControllerTest { - - @Test - public void testProduces() { - Produces annotation = RestController.class.getAnnotation(Produces.class); - assertNotNull(annotation); - assertThat(annotation.value()).contains(MediaType.APPLICATION_JSON) - .contains(RestController.APPLICATION_YAML); - } - - @Test - public void testAddVersionControlHeaders() { - RestController ctlr = new RestController(); - Response resp = ctlr.addVersionControlHeaders(Response.status(Response.Status.OK)).build(); - assertEquals("0", resp.getHeaderString(RestController.VERSION_MINOR_NAME)); - assertEquals("0", resp.getHeaderString(RestController.VERSION_PATCH_NAME)); - assertEquals("1.0.0", resp.getHeaderString(RestController.VERSION_LATEST_NAME)); - } - - @Test - public void testAddLoggingHeaders_Null() { - RestController ctlr = new RestController(); - Response resp = ctlr.addLoggingHeaders(Response.status(Response.Status.OK), null).build(); - assertNotNull(resp.getHeaderString(RestController.REQUEST_ID_NAME)); - } - - @Test - public void testAddLoggingHeaders_NonNull() { - UUID uuid = UUID.randomUUID(); - RestController ctlr = new RestController(); - Response resp = ctlr.addLoggingHeaders(Response.status(Response.Status.OK), uuid).build(); - assertEquals(uuid.toString(), resp.getHeaderString(RestController.REQUEST_ID_NAME)); - } - -} diff --git a/runtime-controlloop/src/test/java/org/onap/policy/clamp/controlloop/runtime/main/startstop/ClRuntimeActivatorTest.java b/runtime-controlloop/src/test/java/org/onap/policy/clamp/controlloop/runtime/main/startstop/ClRuntimeActivatorTest.java index da71c239d..7eb567259 100644 --- a/runtime-controlloop/src/test/java/org/onap/policy/clamp/controlloop/runtime/main/startstop/ClRuntimeActivatorTest.java +++ b/runtime-controlloop/src/test/java/org/onap/policy/clamp/controlloop/runtime/main/startstop/ClRuntimeActivatorTest.java @@ -20,63 +20,46 @@ package org.onap.policy.clamp.controlloop.runtime.main.startstop; -import static org.assertj.core.api.Assertions.assertThatExceptionOfType; import static org.assertj.core.api.Assertions.assertThatIllegalStateException; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; -import org.junit.Test; -import org.onap.policy.clamp.controlloop.common.exception.ControlLoopRuntimeException; +import org.junit.jupiter.api.Test; +import org.mockito.Mockito; import org.onap.policy.clamp.controlloop.runtime.main.parameters.ClRuntimeParameterGroup; import org.onap.policy.clamp.controlloop.runtime.main.parameters.ClRuntimeParameterHandler; -import org.onap.policy.common.utils.services.Registry; +import org.onap.policy.clamp.controlloop.runtime.supervision.SupervisionHandler; /** * Class to perform unit test of {@link ClRuntimeActivator}}. * */ -public class ClRuntimeActivatorTest { +class ClRuntimeActivatorTest { @Test - public void testStartAndStop() throws Exception { - Registry.newRegistry(); - final String[] configParameters = {"-c", "src/test/resources/parameters/TestParameters.json"}; - final ClRuntimeCommandLineArguments arguments = new ClRuntimeCommandLineArguments(); - arguments.parse(configParameters); - ClRuntimeParameterGroup parameterGroup = new ClRuntimeParameterHandler().getParameters(arguments); - ClRuntimeActivator activator = new ClRuntimeActivator(parameterGroup); - activator.isAlive(); + void testStartAndStop() throws Exception { + final String path = "src/test/resources/parameters/TestParameters.json"; + ClRuntimeParameterGroup parameterGroup = new ClRuntimeParameterHandler().getParameters(path); + var supervisionHandler = Mockito.mock(SupervisionHandler.class); - assertFalse(activator.isAlive()); - activator.start(); - assertTrue(activator.isAlive()); - assertTrue(activator.getParameterGroup().isValid()); - assertEquals(activator.getParameterGroup().getName(), - activator.getParameterGroup().getRestServerParameters().getName()); + try (var activator = new ClRuntimeActivator(parameterGroup, supervisionHandler)) { - // repeat start - should throw an exception - assertThatIllegalStateException().isThrownBy(() -> activator.start()); - assertTrue(activator.isAlive()); - assertTrue(activator.getParameterGroup().isValid()); + assertFalse(activator.isAlive()); + activator.start(); + assertTrue(activator.isAlive()); + assertTrue(activator.getParameterGroup().isValid()); - activator.stop(); - assertFalse(activator.isAlive()); + // repeat start - should throw an exception + assertThatIllegalStateException().isThrownBy(() -> activator.start()); + assertTrue(activator.isAlive()); + assertTrue(activator.getParameterGroup().isValid()); - // repeat stop - should throw an exception - assertThatIllegalStateException().isThrownBy(() -> activator.stop()); - assertFalse(activator.isAlive()); - } + activator.stop(); + assertFalse(activator.isAlive()); - @Test - public void testNull() { - assertThatExceptionOfType(ControlLoopRuntimeException.class).isThrownBy(() -> new ClRuntimeActivator(null)); - } - - @Test - public void testNotValid() { - ClRuntimeParameterGroup parameterGroup = new ClRuntimeParameterGroup("name"); - assertThatExceptionOfType(ControlLoopRuntimeException.class) - .isThrownBy(() -> new ClRuntimeActivator(parameterGroup)); + // repeat stop - should throw an exception + assertThatIllegalStateException().isThrownBy(() -> activator.stop()); + assertFalse(activator.isAlive()); + } } } diff --git a/runtime-controlloop/src/test/java/org/onap/policy/clamp/controlloop/runtime/main/startstop/MainTest.java b/runtime-controlloop/src/test/java/org/onap/policy/clamp/controlloop/runtime/main/startstop/MainTest.java deleted file mode 100644 index b06383c68..000000000 --- a/runtime-controlloop/src/test/java/org/onap/policy/clamp/controlloop/runtime/main/startstop/MainTest.java +++ /dev/null @@ -1,157 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * Copyright (C) 2021 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. - * - * SPDX-License-Identifier: Apache-2.0 - * ============LICENSE_END========================================================= - */ - -package org.onap.policy.clamp.controlloop.runtime.main.startstop; - -import static org.assertj.core.api.Assertions.assertThatCode; -import static org.assertj.core.api.Assertions.assertThatThrownBy; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertTrue; - -import org.junit.AfterClass; -import org.junit.BeforeClass; -import org.junit.Test; -import org.onap.policy.clamp.controlloop.common.ControlLoopConstants; -import org.onap.policy.clamp.controlloop.common.exception.ControlLoopRuntimeException; -import org.onap.policy.common.utils.resources.MessageConstants; -import org.onap.policy.common.utils.services.Registry; - -/** - * Class to perform unit test of {@link Main}}. - * - */ -public class MainTest { - - public static final String POLICY_CLAMP_FAILURE_MSG = - String.format(MessageConstants.START_FAILURE_MSG, MessageConstants.POLICY_CLAMP); - - /** - * Set up. - */ - @BeforeClass - public static void setUp() { - Registry.newRegistry(); - } - - /** - * Shuts "main" down. - * - * @throws Exception if an error occurs - */ - @AfterClass - public static void tearDown() throws Exception { - // shut down activator - final ClRuntimeActivator activator = - Registry.getOrDefault(ControlLoopConstants.REG_CLRUNTIME_ACTIVATOR, ClRuntimeActivator.class, null); - if (activator != null && activator.isAlive()) { - activator.shutdown(); - } - } - - @Test - public void testMain_Help() { - final String[] configParameters = {"-h"}; - Main main = new Main(configParameters); - assertFalse(main.isRunning()); - } - - @Test - public void testMain_Version() { - final String[] configParameters = {"-v"}; - Main main = new Main(configParameters); - assertFalse(main.isRunning()); - } - - @Test - public void testMain_Valid() { - final String[] configParameters = {"-c", "src/test/resources/parameters/TestParameters.json"}; - Main main = new Main(configParameters); - assertTrue(main.isRunning()); - - // ensure items were added to the registry - assertNotNull(Registry.get(ControlLoopConstants.REG_CLRUNTIME_ACTIVATOR, ClRuntimeActivator.class)); - - assertThatCode(() -> main.shutdown()).doesNotThrowAnyException(); - - assertFalse(main.isRunning()); - } - - @Test - public void testMain_NoParameter() { - assertThatConfigParameterThrownException(new String[] {}); - } - - @Test - public void testMain_FilePathNotDefined() { - assertThatConfigParameterThrownException(new String[] {"-c"}); - } - - @Test - public void testMain_TooManyCommand() { - assertThatConfigParameterThrownException(new String[] {"-h", "d"}); - } - - @Test - public void testMain_WrongParameter() { - assertThatConfigParameterThrownException(new String[] {"-d"}); - } - - private void assertThatConfigParameterThrownException(final String[] configParameters) { - assertThatThrownBy(() -> Main.main(configParameters)).isInstanceOf(ControlLoopRuntimeException.class) - .hasMessage(POLICY_CLAMP_FAILURE_MSG); - } - - @Test - public void testParticipant_NoFileWithThisName() { - assertThatConfigFileThrownException("src/test/resources/parameters/NoFileWithThisName.json"); - } - - @Test - public void testParticipant_NotValidFile() { - assertThatConfigFileThrownException("src/test/resources/parameters"); - } - - @Test - public void testParticipant_FileEmpty() { - assertThatConfigFileThrownException("src/test/resources/parameters/EmptyParameters.json"); - } - - @Test - public void testParticipant_NoParameters() { - assertThatConfigFileThrownException("src/test/resources/parameters/NoParameters.json"); - } - - @Test - public void testParticipant_InvalidParameters() { - assertThatConfigFileThrownException("src/test/resources/parameters/InvalidParameters.json"); - } - - @Test - public void testParticipant_WrongJsonFormat() { - assertThatConfigFileThrownException("src/test/resources/parameters/Unreadable.json"); - } - - private void assertThatConfigFileThrownException(final String configFilePath) { - final String[] configParameters = new String[] {"-c", configFilePath}; - assertThatThrownBy(() -> new Main(configParameters)).isInstanceOf(ControlLoopRuntimeException.class) - .hasMessage(String.format(POLICY_CLAMP_FAILURE_MSG)); - } -} diff --git a/runtime-controlloop/src/test/java/org/onap/policy/clamp/controlloop/runtime/monitoring/TestMonitoringProvider.java b/runtime-controlloop/src/test/java/org/onap/policy/clamp/controlloop/runtime/monitoring/TestMonitoringProvider.java index 56735ae12..580b30f9e 100644 --- a/runtime-controlloop/src/test/java/org/onap/policy/clamp/controlloop/runtime/monitoring/TestMonitoringProvider.java +++ b/runtime-controlloop/src/test/java/org/onap/policy/clamp/controlloop/runtime/monitoring/TestMonitoringProvider.java @@ -22,7 +22,7 @@ package org.onap.policy.clamp.controlloop.runtime.monitoring; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatThrownBy; -import static org.junit.Assert.assertEquals; +import static org.junit.jupiter.api.Assertions.assertEquals; import static org.mockito.Mockito.when; import java.io.File; @@ -32,31 +32,31 @@ import java.util.ArrayList; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; -import org.junit.BeforeClass; -import org.junit.Test; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; import org.mockito.Mockito; import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ClElementStatisticsList; import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoop; import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopElement; import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ParticipantStatisticsList; import org.onap.policy.clamp.controlloop.models.controlloop.persistence.provider.ControlLoopProvider; +import org.onap.policy.clamp.controlloop.runtime.main.parameters.ClRuntimeParameterGroup; import org.onap.policy.clamp.controlloop.runtime.util.CommonTestData; import org.onap.policy.common.utils.coder.Coder; import org.onap.policy.common.utils.coder.CoderException; import org.onap.policy.common.utils.coder.StandardCoder; -import org.onap.policy.models.provider.PolicyModelsProviderParameters; import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier; -public class TestMonitoringProvider { +class TestMonitoringProvider { private static final String CL_PARTICIPANT_STATISTICS_JSON = - "src/test/resources/rest/monitoring/TestParticipantStatistics.json"; + "src/test/resources/rest/monitoring/TestParticipantStatistics.json"; private static final String INVALID_PARTICIPANT_JSON_INPUT = - "src/test/resources/rest/monitoring/TestParticipantStatistics_Invalid.json"; + "src/test/resources/rest/monitoring/TestParticipantStatistics_Invalid.json"; private static final String CL_ELEMENT_STATISTICS_JSON = - "src/test/resources/rest/monitoring/TestClElementStatistics.json"; + "src/test/resources/rest/monitoring/TestClElementStatistics.json"; private static final String INVALID_CL_ELEMENT_JSON_INPUT = - "src/test/resources/rest/monitoring/TestClElementStatistics_Invalid.json"; + "src/test/resources/rest/monitoring/TestClElementStatistics_Invalid.json"; private static final Coder CODER = new StandardCoder(); private static final String CL_PROVIDER_FIELD = "controlLoopProvider"; @@ -67,21 +67,20 @@ public class TestMonitoringProvider { private static ClElementStatisticsList inputClElementStatistics; private static ClElementStatisticsList invalidClElementInput; - @BeforeClass + @BeforeAll public static void beforeSetupStatistics() throws CoderException { // Reading input json for statistics data inputParticipantStatistics = - CODER.decode(new File(CL_PARTICIPANT_STATISTICS_JSON), ParticipantStatisticsList.class); + CODER.decode(new File(CL_PARTICIPANT_STATISTICS_JSON), ParticipantStatisticsList.class); invalidParticipantInput = - CODER.decode(new File(INVALID_PARTICIPANT_JSON_INPUT), ParticipantStatisticsList.class); + CODER.decode(new File(INVALID_PARTICIPANT_JSON_INPUT), ParticipantStatisticsList.class); inputClElementStatistics = CODER.decode(new File(CL_ELEMENT_STATISTICS_JSON), ClElementStatisticsList.class); invalidClElementInput = CODER.decode(new File(INVALID_CL_ELEMENT_JSON_INPUT), ClElementStatisticsList.class); } @Test - public void testCreateParticipantStatistics() throws Exception { - PolicyModelsProviderParameters parameters = - CommonTestData.geParameterGroup(0, "createparStat").getDatabaseProviderParameters(); + void testCreateParticipantStatistics() throws Exception { + ClRuntimeParameterGroup parameters = CommonTestData.geParameterGroup(0, "createparStat"); try (MonitoringProvider provider = new MonitoringProvider(parameters)) { // Creating statistics data in db with null input @@ -95,18 +94,17 @@ public class TestMonitoringProvider { // Creating statistics data from input json ParticipantStatisticsList createResponse = - provider.createParticipantStatistics(inputParticipantStatistics.getStatisticsList()); + provider.createParticipantStatistics(inputParticipantStatistics.getStatisticsList()); assertThat(createResponse.getStatisticsList()).hasSize(3); assertEquals(createResponse.getStatisticsList().toString().replaceAll("\\s+", ""), - inputParticipantStatistics.getStatisticsList().toString().replaceAll("\\s+", "")); + inputParticipantStatistics.getStatisticsList().toString().replaceAll("\\s+", "")); } } @Test - public void testGetParticipantStatistics() throws Exception { - PolicyModelsProviderParameters parameters = - CommonTestData.geParameterGroup(0, "getparStat").getDatabaseProviderParameters(); + void testGetParticipantStatistics() throws Exception { + ClRuntimeParameterGroup parameters = CommonTestData.geParameterGroup(0, "getparStat"); try (MonitoringProvider provider = new MonitoringProvider(parameters)) { ParticipantStatisticsList getResponse; @@ -120,24 +118,24 @@ public class TestMonitoringProvider { getResponse = provider.fetchFilteredParticipantStatistics("name2", "1.001", 1, null, null); assertThat(getResponse.getStatisticsList()).hasSize(1); assertEquals(getResponse.getStatisticsList().get(0).toString().replaceAll("\\s+", ""), - inputParticipantStatistics.getStatisticsList().get(2).toString().replaceAll("\\s+", "")); + inputParticipantStatistics.getStatisticsList().get(2).toString().replaceAll("\\s+", "")); // Fetch statistics using timestamp getResponse = provider.fetchFilteredParticipantStatistics("name1", "1.001", 0, null, - Instant.parse("2021-01-10T15:00:00.000Z")); + Instant.parse("2021-01-10T15:00:00.000Z")); assertThat(getResponse.getStatisticsList()).hasSize(1); getResponse = provider.fetchFilteredParticipantStatistics("name1", "1.001", 0, - Instant.parse("2021-01-11T12:00:00.000Z"), Instant.parse("2021-01-11T16:00:00.000Z")); + Instant.parse("2021-01-11T12:00:00.000Z"), Instant.parse("2021-01-11T16:00:00.000Z")); assertThat(getResponse.getStatisticsList()).isEmpty(); } } @Test - public void testCreateClElementStatistics() throws Exception { - PolicyModelsProviderParameters parameters = - CommonTestData.geParameterGroup(0, "createelemstat").getDatabaseProviderParameters(); + void testCreateClElementStatistics() throws Exception { + ClRuntimeParameterGroup parameters = CommonTestData.geParameterGroup(0, "createelemstat"); + try (MonitoringProvider provider = new MonitoringProvider(parameters)) { // Creating statistics data in db with null input assertThatThrownBy(() -> { @@ -150,18 +148,18 @@ public class TestMonitoringProvider { // Creating clElement statistics data from input json ClElementStatisticsList createResponse = - provider.createClElementStatistics(inputClElementStatistics.getClElementStatistics()); + provider.createClElementStatistics(inputClElementStatistics.getClElementStatistics()); assertThat(createResponse.getClElementStatistics()).hasSize(4); assertEquals(createResponse.getClElementStatistics().toString().replaceAll("\\s+", ""), - inputClElementStatistics.getClElementStatistics().toString().replaceAll("\\s+", "")); + inputClElementStatistics.getClElementStatistics().toString().replaceAll("\\s+", "")); } } @Test - public void testGetClElementStatistics() throws Exception { - PolicyModelsProviderParameters parameters = - CommonTestData.geParameterGroup(0, "getelemstat").getDatabaseProviderParameters(); + void testGetClElementStatistics() throws Exception { + ClRuntimeParameterGroup parameters = CommonTestData.geParameterGroup(0, "getelemstat"); + try (MonitoringProvider provider = new MonitoringProvider(parameters)) { ClElementStatisticsList getResponse; @@ -175,24 +173,24 @@ public class TestMonitoringProvider { assertThat(getResponse.getClElementStatistics()).hasSize(2); assertEquals(getResponse.getClElementStatistics().get(0).toString().replaceAll("\\s+", ""), - inputClElementStatistics.getClElementStatistics().get(0).toString().replaceAll("\\s+", "")); + inputClElementStatistics.getClElementStatistics().get(0).toString().replaceAll("\\s+", "")); // Fetch specific statistics record with name, id and record count getResponse = provider.fetchFilteredClElementStatistics("name1", "1.001", - "709c62b3-8918-41b9-a747-d21eb79c6c20", null, null, 0); + "709c62b3-8918-41b9-a747-d21eb79c6c20", null, null, 0); assertThat(getResponse.getClElementStatistics()).hasSize(2); // Fetch statistics using timestamp getResponse = provider.fetchFilteredClElementStatistics("name1", "1.001", null, - Instant.parse("2021-01-10T13:45:00.000Z"), null, 0); + Instant.parse("2021-01-10T13:45:00.000Z"), null, 0); assertThat(getResponse.getClElementStatistics()).hasSize(2); } } @Test - public void testGetParticipantStatsPerCL() throws Exception { - PolicyModelsProviderParameters parameters = - CommonTestData.geParameterGroup(0, "getparStatCL").getDatabaseProviderParameters(); + void testGetParticipantStatsPerCL() throws Exception { + ClRuntimeParameterGroup parameters = CommonTestData.geParameterGroup(0, "getparStatCL"); + try (MonitoringProvider provider = Mockito.spy(new MonitoringProvider(parameters))) { provider.createParticipantStatistics(inputParticipantStatistics.getStatisticsList()); @@ -204,23 +202,23 @@ public class TestMonitoringProvider { getResponse = provider.fetchParticipantStatsPerControlLoop("testName", "1.001"); assertThat(getResponse.getStatisticsList()).hasSize(2); assertEquals(getResponse.getStatisticsList().get(0).toString().replaceAll("\\s+", ""), - inputParticipantStatistics.getStatisticsList().get(0).toString().replaceAll("\\s+", "")); + inputParticipantStatistics.getStatisticsList().get(0).toString().replaceAll("\\s+", "")); assertThat(provider.fetchParticipantStatsPerControlLoop("invalidCLName", "1.002").getStatisticsList()) - .isEmpty(); + .isEmpty(); } } @Test - public void testClElementStatsPerCL() throws Exception { - PolicyModelsProviderParameters parameters = - CommonTestData.geParameterGroup(0, "getelemstatPerCL").getDatabaseProviderParameters(); + void testClElementStatsPerCL() throws Exception { + ClRuntimeParameterGroup parameters = CommonTestData.geParameterGroup(0, "getelemstatPerCL"); + // Setup a dummy Control loop data ControlLoopElement mockClElement = new ControlLoopElement(); mockClElement.setId(inputClElementStatistics.getClElementStatistics().get(0).getId()); mockClElement.setParticipantId(new ToscaConceptIdentifier( - inputClElementStatistics.getClElementStatistics().get(0).getParticipantId().getName(), - inputClElementStatistics.getClElementStatistics().get(0).getParticipantId().getVersion())); + inputClElementStatistics.getClElementStatistics().get(0).getParticipantId().getName(), + inputClElementStatistics.getClElementStatistics().get(0).getParticipantId().getVersion())); ControlLoop mockCL = new ControlLoop(); mockCL.setElements(new LinkedHashMap<>()); mockCL.getElements().put(mockClElement.getId(), mockClElement); @@ -240,16 +238,15 @@ public class TestMonitoringProvider { assertThat(getResponse.getClElementStatistics()).hasSize(2); assertEquals(getResponse.getClElementStatistics().get(1).toString().replaceAll("\\s+", ""), - inputClElementStatistics.getClElementStatistics().get(1).toString().replaceAll("\\s+", "")); + inputClElementStatistics.getClElementStatistics().get(1).toString().replaceAll("\\s+", "")); - assertThat( - monitoringProvider.fetchClElementStatsPerControlLoop("invalidCLName", "1.002").getClElementStatistics()) - .isEmpty(); + assertThat(monitoringProvider.fetchClElementStatsPerControlLoop("invalidCLName", "1.002") + .getClElementStatistics()).isEmpty(); Map<String, ToscaConceptIdentifier> clElementIds = - monitoringProvider.getAllClElementsIdPerControlLoop("testCLName", "1.001"); + monitoringProvider.getAllClElementsIdPerControlLoop("testCLName", "1.001"); assertThat(clElementIds) - .containsKey(inputClElementStatistics.getClElementStatistics().get(0).getId().toString()); + .containsKey(inputClElementStatistics.getClElementStatistics().get(0).getId().toString()); } } } diff --git a/runtime-controlloop/src/test/java/org/onap/policy/clamp/controlloop/runtime/monitoring/rest/MonitoringQueryControllerTest.java b/runtime-controlloop/src/test/java/org/onap/policy/clamp/controlloop/runtime/monitoring/rest/MonitoringQueryControllerTest.java index a846d9317..95b2113cd 100644 --- a/runtime-controlloop/src/test/java/org/onap/policy/clamp/controlloop/runtime/monitoring/rest/MonitoringQueryControllerTest.java +++ b/runtime-controlloop/src/test/java/org/onap/policy/clamp/controlloop/runtime/monitoring/rest/MonitoringQueryControllerTest.java @@ -21,29 +21,40 @@ package org.onap.policy.clamp.controlloop.runtime.monitoring.rest; import static org.assertj.core.api.Assertions.assertThat; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; import java.io.File; import java.time.Instant; import javax.ws.rs.client.Invocation; import javax.ws.rs.core.Response; -import org.junit.AfterClass; -import org.junit.BeforeClass; -import org.junit.Test; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ClElementStatisticsList; import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ParticipantStatisticsList; +import org.onap.policy.clamp.controlloop.runtime.main.parameters.ClRuntimeParameterGroup; import org.onap.policy.clamp.controlloop.runtime.monitoring.MonitoringProvider; import org.onap.policy.clamp.controlloop.runtime.util.rest.CommonRestController; import org.onap.policy.common.utils.coder.Coder; import org.onap.policy.common.utils.coder.StandardCoder; - -public class MonitoringQueryControllerTest extends CommonRestController { +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; +import org.springframework.boot.web.server.LocalServerPort; +import org.springframework.test.context.TestPropertySource; +import org.springframework.test.context.junit.jupiter.SpringExtension; + +@ExtendWith(SpringExtension.class) +@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT) +@TestPropertySource(locations = {"classpath:application_test.properties"}) +class MonitoringQueryControllerTest extends CommonRestController { private static final String CL_PARTICIPANT_STATISTICS_JSON = - "src/test/resources/rest/monitoring/TestParticipantStatistics.json"; + "src/test/resources/rest/monitoring/TestParticipantStatistics.json"; private static final String CL_ELEMENT_STATISTICS_JSON = - "src/test/resources/rest/monitoring/TestClElementStatistics.json"; + "src/test/resources/rest/monitoring/TestClElementStatistics.json"; private static final Coder CODER = new StandardCoder(); @@ -51,64 +62,68 @@ public class MonitoringQueryControllerTest extends CommonRestController { private static ClElementStatisticsList inputClElementStatistics; private static ParticipantStatisticsList participantStatisticsList; - private static ClElementStatisticsList clElementStatisticsList; + private static ClElementStatisticsList clElementStatisticsList; private static final String CLELEMENT_STATS_ENDPOINT = "monitoring/clelement"; private static final String PARTICIPANT_STATS_ENDPOINT = "monitoring/participant"; private static final String PARTICIPANT_STATS_PER_CL_ENDPOINT = "monitoring/participants/controlloop"; private static final String CLELEMENT_STATS_PER_CL_ENDPOINT = "monitoring/clelements/controlloop"; + @Autowired + private ClRuntimeParameterGroup clRuntimeParameterGroup; + + @LocalServerPort + private int randomServerPort; /** * starts Main. * * @throws Exception if an error occurs */ - @BeforeClass - public static void setUpBeforeClass() throws Exception { - CommonRestController.setUpBeforeClass("testStatisticsQuery"); - inputParticipantStatistics = CODER.decode(new File(CL_PARTICIPANT_STATISTICS_JSON), - ParticipantStatisticsList.class); - inputClElementStatistics = CODER.decode(new File(CL_ELEMENT_STATISTICS_JSON), - ClElementStatisticsList.class); - - try (MonitoringProvider monitoringProvider = new MonitoringProvider(getParameters())) { + @BeforeAll + public static void setUpBeforeAll() throws Exception { + + inputParticipantStatistics = + CODER.decode(new File(CL_PARTICIPANT_STATISTICS_JSON), ParticipantStatisticsList.class); + inputClElementStatistics = CODER.decode(new File(CL_ELEMENT_STATISTICS_JSON), ClElementStatisticsList.class); + } + + @BeforeEach + public void setUpBeforeEach() throws Exception { + super.setHttpPrefix(randomServerPort); + + try (var monitoringProvider = new MonitoringProvider(clRuntimeParameterGroup)) { // Insert Participant statistics to DB - participantStatisticsList = monitoringProvider.createParticipantStatistics(inputParticipantStatistics - .getStatisticsList()); + participantStatisticsList = + monitoringProvider.createParticipantStatistics(inputParticipantStatistics.getStatisticsList()); // Insert CL Element statistics to DB - clElementStatisticsList = monitoringProvider.createClElementStatistics(inputClElementStatistics - .getClElementStatistics()); + clElementStatisticsList = + monitoringProvider.createClElementStatistics(inputClElementStatistics.getClElementStatistics()); } } - @AfterClass - public static void teardownAfterClass() { - CommonRestController.teardownAfterClass(); - } - @Test - public void testQuery_Unauthorized_for_ClElementStats() throws Exception { + void testQuery_Unauthorized_for_ClElementStats() throws Exception { assertUnauthorizedGet(CLELEMENT_STATS_ENDPOINT); } @Test - public void testQuery_Unauthorized_for_ClParticipantStats() throws Exception { + void testQuery_Unauthorized_for_ClParticipantStats() throws Exception { assertUnauthorizedGet(PARTICIPANT_STATS_ENDPOINT); } @Test - public void testQuery_Unauthorized_for_ParticipantStatsPerCl() throws Exception { + void testQuery_Unauthorized_for_ParticipantStatsPerCl() throws Exception { assertUnauthorizedGet(PARTICIPANT_STATS_PER_CL_ENDPOINT); } @Test - public void testQuery_Unauthorized_for_ClElementStatsPerCl() throws Exception { + void testQuery_Unauthorized_for_ClElementStatsPerCl() throws Exception { assertUnauthorizedGet(CLELEMENT_STATS_PER_CL_ENDPOINT); } @Test - public void testSwagger_ClStats() throws Exception { + void testSwagger_ClStats() throws Exception { super.testSwagger(CLELEMENT_STATS_ENDPOINT); super.testSwagger(PARTICIPANT_STATS_ENDPOINT); super.testSwagger(CLELEMENT_STATS_PER_CL_ENDPOINT); @@ -116,13 +131,12 @@ public class MonitoringQueryControllerTest extends CommonRestController { } @Test - public void testClElementStatisticsEndpoint() throws Exception { + void testClElementStatisticsEndpoint() throws Exception { // Filter statistics only based on participant Id and UUID - Invocation.Builder invokeRequest1 = - super.sendRequest(CLELEMENT_STATS_ENDPOINT + "?name=" + clElementStatisticsList - .getClElementStatistics().get(0).getParticipantId().getName() + "&version=" + clElementStatisticsList - .getClElementStatistics().get(0).getParticipantId().getVersion() + "&id=" + clElementStatisticsList - .getClElementStatistics().get(0).getId().toString()); + Invocation.Builder invokeRequest1 = super.sendRequest(CLELEMENT_STATS_ENDPOINT + "?name=" + + clElementStatisticsList.getClElementStatistics().get(0).getParticipantId().getName() + "&version=" + + clElementStatisticsList.getClElementStatistics().get(0).getParticipantId().getVersion() + "&id=" + + clElementStatisticsList.getClElementStatistics().get(0).getId().toString()); Response response1 = invokeRequest1.buildGet().invoke(); assertEquals(Response.Status.OK.getStatusCode(), response1.getStatus()); @@ -130,55 +144,49 @@ public class MonitoringQueryControllerTest extends CommonRestController { assertNotNull(result1); assertThat(result1.getClElementStatistics()).hasSize(2); - assertEquals(result1.getClElementStatistics().get(0), clElementStatisticsList - .getClElementStatistics().get(0)); + assertEquals(result1.getClElementStatistics().get(0), clElementStatisticsList.getClElementStatistics().get(0)); // Filter statistics based on timestamp - Invocation.Builder invokeRequest2 = - super.sendRequest(CLELEMENT_STATS_ENDPOINT + "?name=" + clElementStatisticsList - .getClElementStatistics().get(1).getParticipantId().getName() + "&version=" + clElementStatisticsList - .getClElementStatistics().get(1).getParticipantId().getVersion() + "&startTime=" - + Instant.parse("2021-01-10T13:00:00.000Z") + "&endTime=" + Instant.parse("2021-01-10T14:00:00.000Z")); + Invocation.Builder invokeRequest2 = super.sendRequest(CLELEMENT_STATS_ENDPOINT + "?name=" + + clElementStatisticsList.getClElementStatistics().get(1).getParticipantId().getName() + "&version=" + + clElementStatisticsList.getClElementStatistics().get(1).getParticipantId().getVersion() + + "&startTime=" + Instant.parse("2021-01-10T13:00:00.000Z") + "&endTime=" + + Instant.parse("2021-01-10T14:00:00.000Z")); Response response2 = invokeRequest2.buildGet().invoke(); assertEquals(Response.Status.OK.getStatusCode(), response2.getStatus()); ClElementStatisticsList result2 = response2.readEntity(ClElementStatisticsList.class); assertNotNull(result2); assertThat(result2.getClElementStatistics()).hasSize(1); - assertEquals(result1.getClElementStatistics().get(0), clElementStatisticsList - .getClElementStatistics().get(0)); + assertEquals(result1.getClElementStatistics().get(0), clElementStatisticsList.getClElementStatistics().get(0)); } @Test - public void testClElementStats_BadRequest() throws Exception { - Invocation.Builder invokeRequest1 = - super.sendRequest(CLELEMENT_STATS_ENDPOINT + "?version=1.0.0"); + void testClElementStats_BadRequest() throws Exception { + Invocation.Builder invokeRequest1 = super.sendRequest(CLELEMENT_STATS_ENDPOINT + "?version=1.0.0"); Response response1 = invokeRequest1.buildGet().invoke(); assertEquals(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), response1.getStatus()); } @Test - public void testParticipantStatisticsEndpoint() throws Exception { + void testParticipantStatisticsEndpoint() throws Exception { // Filter statistics only based on participant Id - Invocation.Builder invokeRequest1 = - super.sendRequest(PARTICIPANT_STATS_ENDPOINT + "?name=" + participantStatisticsList - .getStatisticsList().get(0).getParticipantId().getName() + "&version=" + participantStatisticsList - .getStatisticsList().get(0).getParticipantId().getVersion()); + Invocation.Builder invokeRequest1 = super.sendRequest(PARTICIPANT_STATS_ENDPOINT + "?name=" + + participantStatisticsList.getStatisticsList().get(0).getParticipantId().getName() + "&version=" + + participantStatisticsList.getStatisticsList().get(0).getParticipantId().getVersion()); Response response1 = invokeRequest1.buildGet().invoke(); assertEquals(Response.Status.OK.getStatusCode(), response1.getStatus()); ParticipantStatisticsList result1 = response1.readEntity(ParticipantStatisticsList.class); assertNotNull(result1); assertThat(result1.getStatisticsList()).hasSize(2); - assertEquals(result1.getStatisticsList().get(0), participantStatisticsList - .getStatisticsList().get(0)); + assertEquals(result1.getStatisticsList().get(0), participantStatisticsList.getStatisticsList().get(0)); // Filter statistics based on timestamp - Invocation.Builder invokeRequest2 = - super.sendRequest(PARTICIPANT_STATS_ENDPOINT + "?name=" + participantStatisticsList - .getStatisticsList().get(1).getParticipantId().getName() + "&version=" + participantStatisticsList - .getStatisticsList().get(1).getParticipantId().getVersion() + "&startTime=" + Invocation.Builder invokeRequest2 = super.sendRequest(PARTICIPANT_STATS_ENDPOINT + "?name=" + + participantStatisticsList.getStatisticsList().get(1).getParticipantId().getName() + "&version=" + + participantStatisticsList.getStatisticsList().get(1).getParticipantId().getVersion() + "&startTime=" + Instant.parse("2021-01-10T13:00:00.000Z") + "&endTime=" + Instant.parse("2021-01-10T14:00:00.000Z")); Response response2 = invokeRequest2.buildGet().invoke(); assertEquals(Response.Status.OK.getStatusCode(), response2.getStatus()); @@ -186,22 +194,20 @@ public class MonitoringQueryControllerTest extends CommonRestController { assertNotNull(result2); assertThat(result2.getStatisticsList()).hasSize(1); - assertEquals(result1.getStatisticsList().get(0), participantStatisticsList - .getStatisticsList().get(0)); + assertEquals(result1.getStatisticsList().get(0), participantStatisticsList.getStatisticsList().get(0)); } @Test - public void testParticipantStats_BadRequest() throws Exception { - Invocation.Builder invokeRequest1 = - super.sendRequest(PARTICIPANT_STATS_ENDPOINT + "?version=0.0"); + void testParticipantStats_BadRequest() throws Exception { + Invocation.Builder invokeRequest1 = super.sendRequest(PARTICIPANT_STATS_ENDPOINT + "?version=0.0"); Response response1 = invokeRequest1.buildGet().invoke(); assertEquals(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), response1.getStatus()); } @Test - public void testParticipantStatsPerClEndpoint() throws Exception { + void testParticipantStatsPerClEndpoint() throws Exception { Invocation.Builder invokeRequest1 = - super.sendRequest(PARTICIPANT_STATS_PER_CL_ENDPOINT + "?name=dummyName&version=1.001"); + super.sendRequest(PARTICIPANT_STATS_PER_CL_ENDPOINT + "?name=dummyName&version=1.001"); Response response1 = invokeRequest1.buildGet().invoke(); assertEquals(Response.Status.OK.getStatusCode(), response1.getStatus()); ParticipantStatisticsList result1 = response1.readEntity(ParticipantStatisticsList.class); @@ -209,17 +215,16 @@ public class MonitoringQueryControllerTest extends CommonRestController { } @Test - public void testParticipantStatsPerCl_BadRequest() throws Exception { - Invocation.Builder invokeRequest1 = - super.sendRequest(PARTICIPANT_STATS_PER_CL_ENDPOINT); + void testParticipantStatsPerCl_BadRequest() throws Exception { + Invocation.Builder invokeRequest1 = super.sendRequest(PARTICIPANT_STATS_PER_CL_ENDPOINT); Response response1 = invokeRequest1.buildGet().invoke(); assertEquals(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), response1.getStatus()); } @Test - public void testClElementStatisticsPerClEndpoint() throws Exception { + void testClElementStatisticsPerClEndpoint() throws Exception { Invocation.Builder invokeRequest1 = - super.sendRequest(CLELEMENT_STATS_PER_CL_ENDPOINT + "?name=dummyName&version=1.001"); + super.sendRequest(CLELEMENT_STATS_PER_CL_ENDPOINT + "?name=dummyName&version=1.001"); Response response1 = invokeRequest1.buildGet().invoke(); assertEquals(Response.Status.OK.getStatusCode(), response1.getStatus()); ClElementStatisticsList result1 = response1.readEntity(ClElementStatisticsList.class); @@ -227,9 +232,8 @@ public class MonitoringQueryControllerTest extends CommonRestController { } @Test - public void testClElementStatsPerCl_BadRequest() throws Exception { - Invocation.Builder invokeRequest1 = - super.sendRequest(CLELEMENT_STATS_PER_CL_ENDPOINT); + void testClElementStatsPerCl_BadRequest() throws Exception { + Invocation.Builder invokeRequest1 = super.sendRequest(CLELEMENT_STATS_PER_CL_ENDPOINT); Response response1 = invokeRequest1.buildGet().invoke(); assertEquals(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), response1.getStatus()); } diff --git a/runtime-controlloop/src/test/java/org/onap/policy/clamp/controlloop/runtime/util/rest/CommonRestController.java b/runtime-controlloop/src/test/java/org/onap/policy/clamp/controlloop/runtime/util/rest/CommonRestController.java index 00b9c6715..eebaa5281 100644 --- a/runtime-controlloop/src/test/java/org/onap/policy/clamp/controlloop/runtime/util/rest/CommonRestController.java +++ b/runtime-controlloop/src/test/java/org/onap/policy/clamp/controlloop/runtime/util/rest/CommonRestController.java @@ -20,14 +20,9 @@ package org.onap.policy.clamp.controlloop.runtime.util.rest; +import static org.assertj.core.api.Assertions.assertThat; import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; -import java.io.File; -import java.io.FileNotFoundException; -import java.io.FileOutputStream; -import java.io.IOException; -import java.nio.charset.StandardCharsets; import javax.ws.rs.client.Client; import javax.ws.rs.client.ClientBuilder; import javax.ws.rs.client.Entity; @@ -37,15 +32,8 @@ import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; import org.glassfish.jersey.client.ClientProperties; import org.glassfish.jersey.client.authentication.HttpAuthenticationFeature; -import org.onap.policy.clamp.controlloop.common.exception.ControlLoopException; -import org.onap.policy.clamp.controlloop.runtime.main.startstop.Main; -import org.onap.policy.clamp.controlloop.runtime.util.CommonTestData; import org.onap.policy.common.gson.GsonMessageBodyHandler; import org.onap.policy.common.utils.network.NetworkUtil; -import org.onap.policy.common.utils.services.Registry; -import org.onap.policy.models.provider.PolicyModelsProviderParameters; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; /** * Class to perform Rest unit tests. @@ -53,46 +41,10 @@ import org.slf4j.LoggerFactory; */ public class CommonRestController { - private static final String CONFIG_FILE = "src/test/resources/parameters/RuntimeConfigParameters%d.json"; - - private static final Logger LOGGER = LoggerFactory.getLogger(CommonRestController.class); - public static final String SELF = NetworkUtil.getHostname(); public static final String ENDPOINT_PREFIX = "onap/controlloop/v2/"; - private static int port; private static String httpPrefix; - private static Main main; - - /** - * Allocates a port for the server, writes a config file, and then starts Main. - * - * @param dbName database name - * @throws Exception if an error occurs - */ - public static void setUpBeforeClass(final String dbName) throws Exception { - port = NetworkUtil.allocPort(); - - httpPrefix = "http://" + SELF + ":" + port + "/"; - - makeConfigFile(dbName); - startMain(); - } - - /** - * Stops Main. - */ - public static void teardownAfterClass() { - try { - stopMain(); - } catch (Exception ex) { - LOGGER.error("cannot stop main", ex); - } - } - - protected static PolicyModelsProviderParameters getParameters() { - return main.getParameters().getDatabaseProviderParameters(); - } /** * Verifies that an endpoint appears within the swagger response. @@ -101,71 +53,10 @@ public class CommonRestController { * @throws Exception if an error occurs */ protected void testSwagger(final String endpoint) throws Exception { - final Invocation.Builder invocationBuilder = sendFqeRequest(httpPrefix + "swagger.yaml", true); + final Invocation.Builder invocationBuilder = sendRequest("api-docs"); final String resp = invocationBuilder.get(String.class); - assertTrue(resp.contains(ENDPOINT_PREFIX + endpoint + ":")); - } - - /** - * Makes a parameter configuration file. - * - * @param dbName database name - * @throws IOException if an error occurs writing the configuration file - * @throws FileNotFoundException if an error occurs writing the configuration file - */ - private static void makeConfigFile(final String dbName) throws FileNotFoundException, IOException { - String json = CommonTestData.getParameterGroupAsString(port, dbName); - - File file = new File(String.format(CONFIG_FILE, port)); - file.deleteOnExit(); - - try (FileOutputStream output = new FileOutputStream(file)) { - output.write(json.getBytes(StandardCharsets.UTF_8)); - } - } - - /** - * Starts the "Main". - * - * @throws InterruptedException if the NetworkUtil method calls are interrupted - * @throws IllegalStateException if a controller cannot be started on the requested port - */ - protected static void startMain() throws InterruptedException { - Registry.newRegistry(); - - // make sure port is available - if (NetworkUtil.isTcpPortOpen(SELF, port, 1, 1L)) { - throw new IllegalStateException("port " + port + " is not available"); - } - - final String[] configParameters = {"-c", String.format(CONFIG_FILE, port)}; - - main = new Main(configParameters); - - if (!NetworkUtil.isTcpPortOpen(SELF, port, 40, 250L)) { - throw new IllegalStateException("server is not listening on port " + port); - } - } - - /** - * Stops the "Main". - * - * @throws ControlLoopException if an error occurs shutting down the controller - * @throws InterruptedException if the NetworkUtil method calls are interrupted - * @throws IllegalStateException if a controller cannot be started on the requested port - */ - private static void stopMain() throws ControlLoopException, InterruptedException { - if (main != null) { - Main main2 = main; - main = null; - - main2.shutdown(); - } - // make sure port is close - if (NetworkUtil.isTcpPortOpen(SELF, port, 1, 1L)) { - throw new IllegalStateException("port " + port + " is still in use"); - } + assertThat(resp).contains(endpoint); } /** @@ -211,7 +102,7 @@ public class CommonRestController { final WebTarget webTarget = client.target(fullyQualifiedEndpoint); - return webTarget.request(MediaType.APPLICATION_JSON); + return webTarget.request(MediaType.APPLICATION_JSON).accept(MediaType.APPLICATION_JSON); } /** @@ -259,4 +150,13 @@ public class CommonRestController { Response rawresp = sendNoAuthRequest(endPoint).delete(); assertEquals(Response.Status.UNAUTHORIZED.getStatusCode(), rawresp.getStatus()); } + + /** + * Set Up httpPrefix. + * + * @param port the port + */ + protected void setHttpPrefix(int port) { + httpPrefix = "http://" + SELF + ":" + port + "/"; + } } |