diff options
author | rameshiyer27 <ramesh.murugan.iyer@est.tech> | 2022-02-28 16:55:31 +0000 |
---|---|---|
committer | Ajith Sreekumar <ajith.sreekumar@bell.ca> | 2022-03-01 16:01:04 +0000 |
commit | 06cd50a60832b5c7b23181145b035472bfff1ebe (patch) | |
tree | de5ea3f217192b6f6c218d9d341ca394920e2e22 /main/src/test/java/org | |
parent | 11234c358bf65b7700c73d8bbad66b335b0dd3c6 (diff) |
Add spring repository and service layer for tosca node templates
Removed policy, policyType providers along with models provider
configurations.
Issue-ID: POLICY-3923
Signed-off-by: zrrmmua <ramesh.murugan.iyer@est.tech>
Change-Id: I696be6380758dbcc5b53cef635f3065eb0ce9a70
Diffstat (limited to 'main/src/test/java/org')
4 files changed, 286 insertions, 6 deletions
diff --git a/main/src/test/java/org/onap/policy/api/main/rest/TestNodeTemplateController.java b/main/src/test/java/org/onap/policy/api/main/rest/TestNodeTemplateController.java index 5244f3c9..993fbee1 100644 --- a/main/src/test/java/org/onap/policy/api/main/rest/TestNodeTemplateController.java +++ b/main/src/test/java/org/onap/policy/api/main/rest/TestNodeTemplateController.java @@ -36,11 +36,9 @@ import org.junit.BeforeClass; import org.junit.Test; import org.junit.runner.RunWith; import org.onap.policy.api.main.PolicyApiApplication; -import org.onap.policy.api.main.rest.provider.NodeTemplateProvider; import org.onap.policy.api.main.rest.utils.CommonTestRestController; +import org.onap.policy.api.main.service.ToscaServiceTemplateService; import org.onap.policy.common.utils.security.SelfSignedKeyStore; -import org.onap.policy.models.base.PfModelException; -import org.onap.policy.models.base.PfModelRuntimeException; import org.onap.policy.models.errors.concepts.ErrorResponse; import org.onap.policy.models.tosca.authorative.concepts.ToscaNodeTemplate; import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate; @@ -86,7 +84,7 @@ public class TestNodeTemplateController extends CommonTestRestController { private int apiPort; @Autowired - private NodeTemplateProvider provider; + private ToscaServiceTemplateService toscaServiceTemplateService; /** * Initializes parameters and set up test environment. @@ -107,8 +105,8 @@ public class TestNodeTemplateController extends CommonTestRestController { public void clearDb() { for (String name : nodeTemplateKeys) { try { - provider.deleteToscaNodeTemplate(name, "1.0.0"); - } catch (PfModelException | PfModelRuntimeException e) { + toscaServiceTemplateService.deleteToscaNodeTemplate(name, "1.0.0"); + } catch (Exception e) { //do nothing } } diff --git a/main/src/test/java/org/onap/policy/api/main/service/TestCommonToscaServiceTemplateService.java b/main/src/test/java/org/onap/policy/api/main/service/TestCommonToscaServiceTemplateService.java index 8d80cac8..4af30937 100644 --- a/main/src/test/java/org/onap/policy/api/main/service/TestCommonToscaServiceTemplateService.java +++ b/main/src/test/java/org/onap/policy/api/main/service/TestCommonToscaServiceTemplateService.java @@ -47,6 +47,8 @@ public class TestCommonToscaServiceTemplateService { protected PolicyTypeService policyTypeService; @Mock protected PolicyService policyService; + @Mock + protected NodeTemplateService nodeTemplateService; /** * Setup the DB TOSCA service template object post create, and delete request. diff --git a/main/src/test/java/org/onap/policy/api/main/service/TestNodeTemplateService.java b/main/src/test/java/org/onap/policy/api/main/service/TestNodeTemplateService.java new file mode 100644 index 00000000..7d85eef2 --- /dev/null +++ b/main/src/test/java/org/onap/policy/api/main/service/TestNodeTemplateService.java @@ -0,0 +1,120 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP Policy API + * ================================================================================ + * Copyright (C) 2022 Nordix Foundation. All rights reserved. + * ================================================================================ + * 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.api.main.service; + + +import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; + +import java.util.Optional; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.junit.MockitoJUnitRunner; +import org.onap.policy.api.main.repository.NodeTemplateRepository; +import org.onap.policy.api.main.repository.NodeTypeRepository; +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.PfConceptKey; +import org.onap.policy.models.base.PfModelException; +import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate; +import org.onap.policy.models.tosca.simple.concepts.JpaToscaNodeTemplate; +import org.onap.policy.models.tosca.simple.concepts.JpaToscaNodeType; +import org.onap.policy.models.tosca.simple.concepts.JpaToscaServiceTemplate; + +@RunWith(MockitoJUnitRunner.class) +public class TestNodeTemplateService { + + @Mock + private NodeTemplateRepository nodeTemplateRepository; + + @Mock + private NodeTypeRepository nodeTypeRepository; + + @InjectMocks + private NodeTemplateService nodeTemplateService; + + private static final String POLICY_WITH_METADATA_SET_REF = "nodetemplates/dummy.apex.decisionmaker.policy.yaml"; + private static final String UPDATED_NODE_TEMPLATE_JSON = "nodetemplates/nodetemplates.metadatasets.update.json"; + + private static ToscaServiceTemplate updatedToscaServiceTemplate; + private StandardCoder standardCoder; + private YamlJsonTranslator yamlJsonTranslator = new YamlJsonTranslator(); + ToscaServiceTemplate policyServiceTemplate; + + /** + * Set up for tests. + * @throws CoderException if error in json parsing + */ + @Before + public void setUp() throws CoderException { + standardCoder = new StandardCoder(); + policyServiceTemplate = + yamlJsonTranslator.fromYaml(ResourceUtils.getResourceAsString(POLICY_WITH_METADATA_SET_REF), + ToscaServiceTemplate.class); + updatedToscaServiceTemplate = + standardCoder.decode(ResourceUtils.getResourceAsString(UPDATED_NODE_TEMPLATE_JSON), + ToscaServiceTemplate.class); + } + + @Test + public void testVerifyNodeType() { + assertThatThrownBy(() -> { + nodeTemplateService.verifyNodeTypeInDbTemplate(new JpaToscaNodeTemplate()); + }).hasMessageMatching("^NODE_TYPE .* for toscaNodeTemplate .* does not exist$"); + + JpaToscaNodeTemplate jpaToscaNodeTemplate = new JpaToscaNodeTemplate(); + PfConceptKey nodeType = new PfConceptKey("dummyType", "1.0.0"); + jpaToscaNodeTemplate.setType(nodeType); + jpaToscaNodeTemplate.setKey(new PfConceptKey("dummyName", "1.0.0")); + Mockito.when(nodeTypeRepository.findById(nodeType)).thenReturn(Optional.of(new JpaToscaNodeType())); + assertDoesNotThrow(() -> nodeTemplateService.verifyNodeTypeInDbTemplate(jpaToscaNodeTemplate)); + } + + @Test + public void testNodeTemplateUsedInPolicy() { + assertDoesNotThrow(() -> nodeTemplateService.assertNodeTemplateNotUsedInPolicy("dummyName", "1.0.0", + new JpaToscaServiceTemplate(policyServiceTemplate))); + + assertThatThrownBy(() -> { + nodeTemplateService.assertNodeTemplateNotUsedInPolicy("apexMetadata_decisionMaker", "1.0.0", + new JpaToscaServiceTemplate(policyServiceTemplate)); + }).hasMessage("Node template is in use, it is referenced in Tosca Policy operational.apex.decisionMaker " + + "version 1.0.0"); + } + + @Test + public void testNodeTemplateUpdate() throws PfModelException { + + Mockito.when(nodeTypeRepository.findById(Mockito.any())).thenReturn(Optional.of(new JpaToscaNodeType())); + Mockito.when(nodeTemplateRepository.findById(Mockito.any())).thenReturn(Optional.of( + new JpaToscaNodeTemplate())); + assertDoesNotThrow(() -> nodeTemplateService.updateToscaNodeTemplates( + new JpaToscaServiceTemplate(updatedToscaServiceTemplate))); + } +} diff --git a/main/src/test/java/org/onap/policy/api/main/service/TestToscaServiceTemplateForNodeTemplate.java b/main/src/test/java/org/onap/policy/api/main/service/TestToscaServiceTemplateForNodeTemplate.java new file mode 100644 index 00000000..6c6f0162 --- /dev/null +++ b/main/src/test/java/org/onap/policy/api/main/service/TestToscaServiceTemplateForNodeTemplate.java @@ -0,0 +1,160 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP Policy API + * ================================================================================ + * Copyright (C) 2022 Nordix Foundation. All rights reserved. + * ================================================================================ + * 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.api.main.service; + +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.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + +import java.util.List; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.InjectMocks; +import org.mockito.junit.MockitoJUnitRunner; +import org.onap.policy.common.utils.coder.CoderException; +import org.onap.policy.common.utils.coder.StandardCoder; +import org.onap.policy.common.utils.resources.ResourceUtils; +import org.onap.policy.models.base.PfConceptKey; +import org.onap.policy.models.tosca.authorative.concepts.ToscaNodeTemplate; +import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate; +import org.onap.policy.models.tosca.simple.concepts.JpaToscaNodeTemplate; + +@RunWith(MockitoJUnitRunner.class) +public class TestToscaServiceTemplateForNodeTemplate extends TestCommonToscaServiceTemplateService { + + private static final String NODE_TEMPLATES_JSON = "nodetemplates/nodetemplates.metadatasets.input.tosca.json"; + private static final String UPDATED_NODE_TEMPLATE_JSON = "nodetemplates/nodetemplates.metadatasets.update.json"; + private static ToscaServiceTemplate toscaServiceTemplate; + private static ToscaServiceTemplate updatedToscaServiceTemplate; + private StandardCoder standardCoder; + + @InjectMocks + private ToscaServiceTemplateService toscaServiceTemplateService; + + + @Before + public void setUp() { + super.setUp(); + } + + /** + * Fetch json files required for the tests. + * + * @throws CoderException when error parsing the json + */ + @Before + public void fetchToscaNodeTemplateJson() throws CoderException { + standardCoder = new StandardCoder(); + toscaServiceTemplate = + standardCoder.decode(ResourceUtils.getResourceAsString(NODE_TEMPLATES_JSON), ToscaServiceTemplate.class); + updatedToscaServiceTemplate = + standardCoder.decode(ResourceUtils.getResourceAsString(UPDATED_NODE_TEMPLATE_JSON), + ToscaServiceTemplate.class); + } + + @Test + public void testToscaNodeTemplatesGet() throws Exception { + + assertNotNull(toscaServiceTemplate); + var createdTemplate = toscaServiceTemplateService.createToscaNodeTemplates(toscaServiceTemplate); + mockDbServiceTemplate(createdTemplate, null, null); + + //Fetch all node templates if id is null + List<ToscaNodeTemplate> gotToscaNodeTemplates = toscaServiceTemplateService + .fetchToscaNodeTemplates(null, null); + assertEquals(3, gotToscaNodeTemplates.size()); + + // Get filtered node templates + List<ToscaNodeTemplate> filteredNodeTemplates = toscaServiceTemplateService + .fetchToscaNodeTemplates("apexMetadata_adaptive", "1.0.0"); + assertEquals(1, filteredNodeTemplates.size()); + + //Get invalid node template + List<ToscaNodeTemplate> filteredNodeTemplatesInvalid = toscaServiceTemplateService + .fetchToscaNodeTemplates("invalidname", "1.0.0"); + assertThat(filteredNodeTemplatesInvalid).isEmpty(); + } + + @Test + public void testToscaNodeTemplatesCreate() throws Exception { + + assertThatThrownBy(() -> { + toscaServiceTemplateService.createToscaNodeTemplates(null); + }).hasMessageMatching("^serviceTemplate is marked .*on.*ull but is null$"); + + ToscaServiceTemplate createdNodeTemplates = + toscaServiceTemplateService.createToscaNodeTemplates(toscaServiceTemplate); + assertThat(createdNodeTemplates.getToscaTopologyTemplate().getNodeTemplates()).hasSize(3); + assertThat(createdNodeTemplates.getToscaTopologyTemplate().getNodeTemplates().get("apexMetadata_grpc") + .getMetadata()).containsKey("threshold"); + } + + @Test + public void testToscaNodeTemplateUpdate() throws Exception { + + assertThatThrownBy(() -> { + toscaServiceTemplateService.updateToscaNodeTemplates(null); + }).hasMessageMatching("^serviceTemplate is marked non-null but is null$"); + + JpaToscaNodeTemplate jpaNodeTemplate = new JpaToscaNodeTemplate(); + PfConceptKey key = new PfConceptKey("apexMetadata_grpc", "1.0.0"); + jpaNodeTemplate.setKey(key); + jpaNodeTemplate.setDescription("Updated Metadata set for GRPC"); + ToscaServiceTemplate updatedTemplate = + toscaServiceTemplateService.updateToscaNodeTemplates(updatedToscaServiceTemplate); + assertEquals("Updated Metadata set for GRPC", + updatedTemplate.getToscaTopologyTemplate().getNodeTemplates().get("apexMetadata_grpc") + .getDescription()); + } + + @Test + public void testToscaNodeTemplateDelete() throws Exception { + + assertThatThrownBy(() -> { + toscaServiceTemplateService.deleteToscaNodeTemplate(null, null); + }).hasMessageMatching("^name is marked .*on.*ull but is null$"); + + assertThatThrownBy(() -> { + toscaServiceTemplateService.deleteToscaNodeTemplate("name", null); + }).hasMessageMatching("^version is marked .*on.*ull but is null$"); + + var createdTemplate = toscaServiceTemplateService.createToscaNodeTemplates(toscaServiceTemplate); + mockDbServiceTemplate(createdTemplate, null, null); + assertThatThrownBy(() -> { + toscaServiceTemplateService.deleteToscaNodeTemplate("dummyname", "1.0.1"); + }).hasMessage("node template dummyname:1.0.1 not found"); + + ToscaServiceTemplate responseTemplate = + toscaServiceTemplateService.deleteToscaNodeTemplate("apexMetadata_decisionMaker", "1.0.0"); + + assertTrue(responseTemplate.getToscaTopologyTemplate().getNodeTemplates() + .containsKey("apexMetadata_decisionMaker")); + assertThat(responseTemplate.getToscaTopologyTemplate().getNodeTemplates()).hasSize(1); + + assertThat(toscaServiceTemplateService.fetchToscaNodeTemplates(null, null)).hasSize(2); + } + +} |