diff options
author | Jim Hahn <jrh3@att.com> | 2021-08-20 20:25:09 +0000 |
---|---|---|
committer | Gerrit Code Review <gerrit@onap.org> | 2021-08-20 20:25:09 +0000 |
commit | d30b27e2686c8f344ecec7fefe7b406c3134d85f (patch) | |
tree | c0d94e2505fd25b98f5651b73337453446b5938e /runtime-controlloop/src/test/java | |
parent | d0a1bcee60c43a736a0526d49c07c564632c4f02 (diff) | |
parent | a5ff342daf396e3e7e35b5b957edaf64ef88734e (diff) |
Merge "Added New Commissioning Unit Tests"
Diffstat (limited to 'runtime-controlloop/src/test/java')
2 files changed, 223 insertions, 0 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 448df4199..232d6d83c 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 @@ -24,29 +24,45 @@ import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.junit.jupiter.api.Assertions.assertTrue; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.PropertyNamingStrategies; +import com.fasterxml.jackson.module.jsonSchema.factories.SchemaFactoryWrapper; import java.util.ArrayList; import java.util.List; +import java.util.Map; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.Test; 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.StandardCoder; 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.tosca.authorative.concepts.ToscaCapabilityType; import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier; +import org.onap.policy.models.tosca.authorative.concepts.ToscaDataType; 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.ToscaPolicyType; +import org.onap.policy.models.tosca.authorative.concepts.ToscaRelationshipType; import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate; +import org.onap.policy.models.tosca.authorative.concepts.ToscaTopologyTemplate; class CommissioningProviderTest { private static final String TOSCA_SERVICE_TEMPLATE_YAML = "src/test/resources/rest/servicetemplates/pmsh_multiple_cl_tosca.yaml"; + private static final String COMMON_TOSCA_SERVICE_TEMPLATE_YAML = + "src/test/resources/rest/servicetemplates/full-tosca-with-common-properties.yaml"; private static final String TEMPLATE_IS_NULL = ".*serviceTemplate is marked non-null but is null"; private static final YamlJsonTranslator yamlTranslator = new YamlJsonTranslator(); private PolicyModelsProvider modelsProvider = null; private ControlLoopProvider clProvider = null; + private static final Coder CODER = new StandardCoder(); + private final ObjectMapper mapper = new ObjectMapper(); + @AfterEach void close() throws Exception { @@ -125,6 +141,113 @@ class CommissioningProviderTest { } /** + * Test the fetching of a full ToscaServiceTemplate object - as opposed to the reduced template that is being + * tested in the testGetToscaServiceTemplateReduced() test. + * + */ + @Test + void testGetToscaServiceTemplate() throws Exception { + ClRuntimeParameterGroup clRuntimeParameterGroup = CommonTestData.geParameterGroup("getCLDefinitions"); + modelsProvider = + CommonTestData.getPolicyModelsProvider(clRuntimeParameterGroup.getDatabaseProviderParameters()); + clProvider = new ControlLoopProvider(clRuntimeParameterGroup.getDatabaseProviderParameters()); + + CommissioningProvider provider = new CommissioningProvider(modelsProvider, clProvider); + ToscaServiceTemplate serviceTemplate = yamlTranslator + .fromYaml(ResourceUtils + .getResourceAsString(COMMON_TOSCA_SERVICE_TEMPLATE_YAML), ToscaServiceTemplate.class); + + provider.createControlLoopDefinitions(serviceTemplate); + + ToscaServiceTemplate returnedServiceTemplate = provider.getToscaServiceTemplate(null, null); + assertThat(returnedServiceTemplate).isNotNull(); + + Map<String, ToscaNodeTemplate> nodeTemplates = returnedServiceTemplate + .getToscaTopologyTemplate().getNodeTemplates(); + + assertThat(nodeTemplates).hasSize(8); + } + + /** + * Test the fetching of a reduced ToscaServiceTemplate with only some of the objects from the full template. + * The reduced template does not contain: DataTypesAsMap or PolicyTypesAsMap. + * + */ + @Test + void testGetToscaServiceTemplateReduced() throws Exception { + ClRuntimeParameterGroup clRuntimeParameterGroup = CommonTestData.geParameterGroup("getCLDefinitions"); + modelsProvider = + CommonTestData.getPolicyModelsProvider(clRuntimeParameterGroup.getDatabaseProviderParameters()); + clProvider = new ControlLoopProvider(clRuntimeParameterGroup.getDatabaseProviderParameters()); + + CommissioningProvider provider = new CommissioningProvider(modelsProvider, clProvider); + ToscaServiceTemplate serviceTemplate = yamlTranslator + .fromYaml(ResourceUtils + .getResourceAsString(COMMON_TOSCA_SERVICE_TEMPLATE_YAML), ToscaServiceTemplate.class); + + provider.createControlLoopDefinitions(serviceTemplate); + + String returnedServiceTemplate = provider.getToscaServiceTemplateReduced(null, null); + assertThat(returnedServiceTemplate).isNotNull(); + ToscaServiceTemplate parsedServiceTemplate = CODER.decode(returnedServiceTemplate, ToscaServiceTemplate.class); + + assertThat(parsedServiceTemplate.getToscaTopologyTemplate().getNodeTemplates()).hasSize(8); + } + + /** + * Tests the different schemas being returned from the schema endpoint. As schemas of the different + * sections of the Tosca Service Templates can be returned by the API, this test must cover all of the + * different sections. + * + */ + @Test + void testGetToscaServiceTemplateSchema() throws Exception { + + ClRuntimeParameterGroup clRuntimeParameterGroup = CommonTestData.geParameterGroup("getCLDefinitions"); + modelsProvider = + CommonTestData.getPolicyModelsProvider(clRuntimeParameterGroup.getDatabaseProviderParameters()); + clProvider = new ControlLoopProvider(clRuntimeParameterGroup.getDatabaseProviderParameters()); + + CommissioningProvider provider = new CommissioningProvider(modelsProvider, clProvider); + ToscaServiceTemplate serviceTemplate = yamlTranslator + .fromYaml(ResourceUtils + .getResourceAsString(COMMON_TOSCA_SERVICE_TEMPLATE_YAML), ToscaServiceTemplate.class); + + provider.createControlLoopDefinitions(serviceTemplate); + + mapper.setPropertyNamingStrategy(PropertyNamingStrategies.SNAKE_CASE); + + Map<String, Class> sections = Map.of( + "all", ToscaServiceTemplate.class, + "data_types", ToscaDataType.class, + "capability_types", ToscaCapabilityType.class, + "node_types", ToscaNodeType.class, + "relationship_types", ToscaRelationshipType.class, + "policy_types", ToscaPolicyType.class, + "topology_template", ToscaTopologyTemplate.class, + "node_templates", List.class); + + for (Map.Entry<String, Class> entry : sections.entrySet()) { + String returnedServiceTemplateSchema = provider.getToscaServiceTemplateSchema(entry.getKey()); + assertThat(returnedServiceTemplateSchema).isNotNull(); + + var visitor = new SchemaFactoryWrapper(); + + if (entry.getKey().equals("node_templates")) { + mapper.acceptJsonFormatVisitor( + mapper.getTypeFactory().constructCollectionType(List.class, ToscaNodeTemplate.class), + visitor); + } else { + mapper.acceptJsonFormatVisitor(mapper.constructType(entry.getValue()), visitor); + } + + var jsonSchema = visitor.finalSchema(); + String localServiceTemplateSchema = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(jsonSchema); + assertThat(localServiceTemplateSchema).isEqualTo(returnedServiceTemplateSchema); + } + } + + /** * Test the deletion of control loop definitions (ToscaServiceTemplate). * * @throws Exception . 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 85bb2d164..58ae1f658 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 @@ -27,13 +27,17 @@ import static org.junit.jupiter.api.Assertions.assertNull; import static org.junit.jupiter.api.Assertions.assertTrue; import java.util.List; +import java.util.Map; import javax.ws.rs.client.Entity; import javax.ws.rs.client.Invocation; import javax.ws.rs.core.Response; +import org.junit.jupiter.api.AfterEach; 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.junit.jupiter.api.parallel.Execution; +import org.junit.jupiter.api.parallel.ExecutionMode; 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; @@ -41,6 +45,7 @@ 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.ToscaNodeTemplate; import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; @@ -52,13 +57,17 @@ import org.springframework.test.context.junit.jupiter.SpringExtension; @ExtendWith(SpringExtension.class) @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT) @TestPropertySource(locations = {"classpath:application_test.properties"}) +@Execution(ExecutionMode.SAME_THREAD) class CommissioningControllerTest extends CommonRestController { private static final String TOSCA_SERVICE_TEMPLATE_YAML = "src/test/resources/rest/servicetemplates/pmsh_multiple_cl_tosca.yaml"; + private static final String COMMON_TOSCA_SERVICE_TEMPLATE_YAML = + "src/test/resources/rest/servicetemplates/full-tosca-with-common-properties.yaml"; private static final YamlJsonTranslator yamlTranslator = new YamlJsonTranslator(); private static final String COMMISSIONING_ENDPOINT = "commission"; private static ToscaServiceTemplate serviceTemplate = new ToscaServiceTemplate(); + private static ToscaServiceTemplate commonPropertiesServiceTemplate = new ToscaServiceTemplate(); @Autowired private ClRuntimeParameterGroup clRuntimeParameterGroup; @@ -76,6 +85,9 @@ class CommissioningControllerTest extends CommonRestController { serviceTemplate = yamlTranslator.fromYaml(ResourceUtils.getResourceAsString(TOSCA_SERVICE_TEMPLATE_YAML), ToscaServiceTemplate.class); + commonPropertiesServiceTemplate = yamlTranslator.fromYaml(ResourceUtils + .getResourceAsString(COMMON_TOSCA_SERVICE_TEMPLATE_YAML), + ToscaServiceTemplate.class); } @BeforeEach @@ -83,6 +95,12 @@ class CommissioningControllerTest extends CommonRestController { super.setHttpPrefix(randomServerPort); } + @AfterEach + public void cleanDatabase() throws Exception { + deleteEntryInDB(serviceTemplate.getName(), serviceTemplate.getVersion()); + deleteEntryInDB(commonPropertiesServiceTemplate.getName(), commonPropertiesServiceTemplate.getVersion()); + } + @Test void testSwagger() throws Exception { super.testSwagger(COMMISSIONING_ENDPOINT); @@ -109,6 +127,62 @@ class CommissioningControllerTest extends CommonRestController { } @Test + void testUnauthorizedQueryToscaServiceTemplate() throws Exception { + assertUnauthorizedGet(COMMISSIONING_ENDPOINT + "/toscaservicetemplate"); + } + + @Test + void testUnauthorizedQueryToscaServiceTemplateSchema() throws Exception { + assertUnauthorizedGet(COMMISSIONING_ENDPOINT + "/toscaServiceTemplateSchema"); + } + + @Test + void testUnauthorizedQueryToscaServiceCommonOrInstanceProperties() throws Exception { + assertUnauthorizedGet(COMMISSIONING_ENDPOINT + "/getCommonOrInstanceProperties"); + } + + @Test + void testQueryToscaServiceTemplate() throws Exception { + createFullEntryInDbWithCommonProps(); + + Invocation.Builder invocationBuilder = super.sendRequest(COMMISSIONING_ENDPOINT + + "/toscaservicetemplate"); + Response rawresp = invocationBuilder.buildGet().invoke(); + assertEquals(Response.Status.OK.getStatusCode(), rawresp.getStatus()); + ToscaServiceTemplate template = rawresp.readEntity(ToscaServiceTemplate.class); + assertNotNull(template); + assertThat(template.getNodeTypes()).hasSize(8); + + } + + @Test + void testQueryToscaServiceTemplateSchema() throws Exception { + createFullEntryInDbWithCommonProps(); + + Invocation.Builder invocationBuilder = super.sendRequest(COMMISSIONING_ENDPOINT + + "/toscaServiceTemplateSchema"); + Response rawresp = invocationBuilder.buildGet().invoke(); + assertEquals(Response.Status.OK.getStatusCode(), rawresp.getStatus()); + String schema = rawresp.readEntity(String.class); + assertNotNull(schema); + + } + + @Test + void testQueryCommonOrInstanceProperties() throws Exception { + createFullEntryInDbWithCommonProps(); + + Invocation.Builder invocationBuilder = super.sendRequest(COMMISSIONING_ENDPOINT + + "/getCommonOrInstanceProperties" + "?common=true&name=ToscaServiceTemplateSimple&version=1.0.0"); + Response rawresp = invocationBuilder.buildGet().invoke(); + assertEquals(Response.Status.OK.getStatusCode(), rawresp.getStatus()); + Map<String, ToscaNodeTemplate> commonProperties = rawresp.readEntity(Map.class); + assertNotNull(commonProperties); + assertThat(commonProperties).hasSize(6); + + } + + @Test void testCreateBadRequest() throws Exception { Invocation.Builder invocationBuilder = super.sendRequest(COMMISSIONING_ENDPOINT); Response resp = invocationBuilder.post(Entity.json("NotToscaServiceTempalte")); @@ -134,6 +208,7 @@ class CommissioningControllerTest extends CommonRestController { assertTrue(commissioningResponse.getAffectedControlLoopDefinitions().stream() .anyMatch(ac -> ac.getName().equals(nodeTemplateName))); } + } @Test @@ -145,6 +220,7 @@ class CommissioningControllerTest extends CommonRestController { assertEquals(Response.Status.OK.getStatusCode(), rawresp.getStatus()); List<?> entityList = rawresp.readEntity(List.class); assertThat(entityList).isEmpty(); + } @Test @@ -157,6 +233,7 @@ class CommissioningControllerTest extends CommonRestController { List<?> entityList = rawresp.readEntity(List.class); assertNotNull(entityList); assertThat(entityList).hasSize(2); + } @Test @@ -167,6 +244,7 @@ class CommissioningControllerTest extends CommonRestController { Invocation.Builder invocationBuilder = super.sendRequest(COMMISSIONING_ENDPOINT + "/elements"); Response resp = invocationBuilder.buildGet().invoke(); assertEquals(Response.Status.NOT_ACCEPTABLE.getStatusCode(), resp.getStatus()); + } @Test @@ -180,6 +258,7 @@ class CommissioningControllerTest extends CommonRestController { List<?> entityList = rawresp.readEntity(List.class); assertNotNull(entityList); assertThat(entityList).hasSize(4); + } @Test @@ -190,6 +269,7 @@ class CommissioningControllerTest extends CommonRestController { //Call delete with no info Response resp = invocationBuilder.delete(); assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), resp.getStatus()); + } @Test @@ -207,12 +287,32 @@ class CommissioningControllerTest extends CommonRestController { List<ToscaServiceTemplate> templatesInDB = modelsProvider.getServiceTemplateList(null, null); assertThat(templatesInDB).isEmpty(); } + } private synchronized void createEntryInDB() throws Exception { try (PolicyModelsProvider modelsProvider = new PolicyModelsProviderFactory() .createPolicyModelsProvider(clRuntimeParameterGroup.getDatabaseProviderParameters())) { + deleteEntryInDB(commonPropertiesServiceTemplate.getName(), commonPropertiesServiceTemplate.getVersion()); modelsProvider.createServiceTemplate(serviceTemplate); } } + + // Delete entries from the DB after relevant tests + private synchronized void deleteEntryInDB(String name, String version) throws Exception { + try (PolicyModelsProvider modelsProvider = new PolicyModelsProviderFactory() + .createPolicyModelsProvider(clRuntimeParameterGroup.getDatabaseProviderParameters())) { + if (!modelsProvider.getServiceTemplateList(null, null).isEmpty()) { + modelsProvider.deleteServiceTemplate(name, version); + } + } + } + + private synchronized void createFullEntryInDbWithCommonProps() throws Exception { + try (PolicyModelsProvider modelsProvider = new PolicyModelsProviderFactory() + .createPolicyModelsProvider(clRuntimeParameterGroup.getDatabaseProviderParameters())) { + deleteEntryInDB(commonPropertiesServiceTemplate.getName(), commonPropertiesServiceTemplate.getVersion()); + modelsProvider.createServiceTemplate(commonPropertiesServiceTemplate); + } + } } |