From 64f6bf630729ed001ac0c5277a7d3550a9096fc8 Mon Sep 17 00:00:00 2001 From: talio Date: Tue, 14 May 2019 11:28:43 +0300 Subject: Fix test coverage Fix test coverage for PolicyUtils.java Change-Id: I7fc4fd36ced11706a867f9b8b5e29f9c0f75b368 Issue-ID: SDC-2298 Signed-off-by: talio --- .../be/components/validation/PolicyUtilsTest.java | 105 +++++++++++++++++++++ 1 file changed, 105 insertions(+) (limited to 'catalog-be/src/test/java/org') diff --git a/catalog-be/src/test/java/org/openecomp/sdc/be/components/validation/PolicyUtilsTest.java b/catalog-be/src/test/java/org/openecomp/sdc/be/components/validation/PolicyUtilsTest.java index 5b5718f688..42b2f6f50b 100644 --- a/catalog-be/src/test/java/org/openecomp/sdc/be/components/validation/PolicyUtilsTest.java +++ b/catalog-be/src/test/java/org/openecomp/sdc/be/components/validation/PolicyUtilsTest.java @@ -1,21 +1,56 @@ +/* + * ============LICENSE_START============================================================================================================= + * Copyright (c) 2019 AT&T Intellectual Property. 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. + * ============LICENSE_END=============================================================================================================== + * + */ package org.openecomp.sdc.be.components.validation; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + import fj.data.Either; +import java.util.Objects; import mockit.Deencapsulation; import org.junit.Test; import org.openecomp.sdc.be.components.BeConfDependentTest; +import org.openecomp.sdc.be.components.utils.ComponentInstancePropertyBuilder; +import org.openecomp.sdc.be.components.utils.ResourceBuilder; +import org.openecomp.sdc.be.components.utils.ServiceBuilder; +import org.openecomp.sdc.be.config.ConfigurationManager; import org.openecomp.sdc.be.dao.api.ActionStatus; import org.openecomp.sdc.be.datatypes.enums.JsonPresentationFields; import org.openecomp.sdc.be.model.Component; +import org.openecomp.sdc.be.model.ComponentInstanceProperty; import org.openecomp.sdc.be.model.PolicyDefinition; import org.openecomp.sdc.be.model.Resource; import org.openecomp.sdc.be.model.Service; import java.util.Map; import java.util.Set; +import org.openecomp.sdc.be.model.operations.impl.UniqueIdBuilder; +import org.openecomp.sdc.common.api.ConfigurationSource; +import org.openecomp.sdc.common.impl.ExternalConfiguration; +import org.openecomp.sdc.common.impl.FSConfigurationSource; public class PolicyUtilsTest extends BeConfDependentTest{ + private static final String PROP_NAME = "propertyName"; + private static final String COMP_ID = "compId"; + private static final String appConfigDir = "src/test/resources/config/catalog-be"; + private static final String EXPECTED_SERVICE_POLICY_TYPE = "a.b.c"; + private static final String EXPECTED_RESOURCE_POLICY_TYPE = "c.d.e"; + private static final String POLICY_ID_1 = "policyId1"; + private static final String POLICY_ID_2 = "policyId2"; + private static final String POLICY_NAME = "policyName"; + @Test public void testGetNextPolicyCounter() throws Exception { Map policies = null; @@ -36,6 +71,38 @@ public class PolicyUtilsTest extends BeConfDependentTest{ result = PolicyUtils.validatePolicyFields(recievedPolicy, validPolicy, policies); } + @Test + public void testValidatePolicyFieldsOneEmptyField() { + PolicyDefinition receivedPolicy = new PolicyDefinition(); + PolicyDefinition validPolicy = new PolicyDefinition(); + + receivedPolicy.setName(POLICY_NAME); + receivedPolicy.setUniqueId(null); + validPolicy.setUniqueId(POLICY_ID_2); + + Either policyEither = + PolicyUtils.validatePolicyFields(receivedPolicy, validPolicy, null); + + assertTrue(policyEither.isLeft()); + assertEquals(validPolicy, policyEither.left().value()); + } + + @Test + public void testValidatePolicyFieldsUpdateUniqueId() { + PolicyDefinition receivedPolicy = new PolicyDefinition(); + PolicyDefinition validPolicy = new PolicyDefinition(); + + receivedPolicy.setName(POLICY_NAME); + receivedPolicy.setUniqueId(POLICY_ID_1); + validPolicy.setUniqueId(POLICY_ID_2); + + Either policyEither = + PolicyUtils.validatePolicyFields(receivedPolicy, validPolicy, null); + + assertTrue(policyEither.isLeft()); + assertEquals(validPolicy, policyEither.left().value()); + } + @Test public void testGetExcludedPolicyTypesByComponent() throws Exception { Component component = new Resource(); @@ -47,6 +114,28 @@ public class PolicyUtilsTest extends BeConfDependentTest{ result = PolicyUtils.getExcludedPolicyTypesByComponent(component); } + @Test + public void testGetExcludedPoliciesWithServiceComponent() { + ConfigurationSource configurationSource = new FSConfigurationSource(ExternalConfiguration.getChangeListener(), appConfigDir); + ConfigurationManager configurationManager = new ConfigurationManager(configurationSource); + + Service service = new ServiceBuilder().setUniqueId(COMP_ID).build(); + + Set policyTypes = PolicyUtils.getExcludedPolicyTypesByComponent(service); + validateExtractedPolicies(policyTypes, EXPECTED_SERVICE_POLICY_TYPE); + } + + @Test + public void testGetExcludedPoliciesWithResourceComponent() { + ConfigurationSource configurationSource = new FSConfigurationSource(ExternalConfiguration.getChangeListener(), appConfigDir); + ConfigurationManager configurationManager = new ConfigurationManager(configurationSource); + + Resource resource = new ResourceBuilder().setUniqueId(COMP_ID).build(); + + Set policyTypes = PolicyUtils.getExcludedPolicyTypesByComponent(resource); + validateExtractedPolicies(policyTypes, EXPECTED_RESOURCE_POLICY_TYPE); + } + @Test public void testExtractNextPolicyCounterFromUniqueId() throws Exception { String uniqueId = ""; @@ -108,4 +197,20 @@ public class PolicyUtilsTest extends BeConfDependentTest{ Deencapsulation.invoke(PolicyUtils.class, "logImmutableFieldUpdateWarning", new Object[] { oldValue, newValue, JsonPresentationFields.class }); } + + @Test + public void testGetDeclaredPolicyDefinition() { + ComponentInstanceProperty property = new ComponentInstancePropertyBuilder().setName(PROP_NAME).build(); + PolicyDefinition policy = PolicyUtils.getDeclaredPolicyDefinition(COMP_ID, property); + + assertTrue(Objects.nonNull(policy)); + assertEquals(UniqueIdBuilder.buildPolicyUniqueId(COMP_ID, PROP_NAME), policy.getUniqueId()); + assertEquals(COMP_ID, policy.getInstanceUniqueId()); + } + + private void validateExtractedPolicies(Set policyTypes, String expectedType) { + assertTrue(org.apache.commons.collections.CollectionUtils.isNotEmpty(policyTypes)); + assertEquals(1, policyTypes.size()); + assertEquals(expectedType, policyTypes.iterator().next()); + } } \ No newline at end of file -- cgit 1.2.3-korg