From 10252b81abde9cb2daac9ec2b619c818b2450d8b Mon Sep 17 00:00:00 2001 From: talio Date: Mon, 22 Oct 2018 16:51:53 +0300 Subject: test coverage add unit tests for ResourceBaseValidator Change-Id: Ib81a2f51d935156597eb055028d7e6da6e8d5834 Issue-ID: SDC-1864 Signed-off-by: talio --- .../openecomp-sdc-validation-sdk/pom.xml | 4 ++ .../sdc/validation/base/ResourceBaseValidator.java | 4 ++ .../type/HeatResourceValidationContext.java | 32 +++++---- .../type/NamingConventionValidationContext.java | 28 +++++--- .../validation/base/ResourceBaseValidatorTest.java | 81 +++++++++++++++++++++- 5 files changed, 126 insertions(+), 23 deletions(-) (limited to 'openecomp-be/lib/openecomp-sdc-validation-lib') diff --git a/openecomp-be/lib/openecomp-sdc-validation-lib/openecomp-sdc-validation-sdk/pom.xml b/openecomp-be/lib/openecomp-sdc-validation-lib/openecomp-sdc-validation-sdk/pom.xml index c0d0d1f7ea..08fc684535 100644 --- a/openecomp-be/lib/openecomp-sdc-validation-lib/openecomp-sdc-validation-sdk/pom.xml +++ b/openecomp-be/lib/openecomp-sdc-validation-lib/openecomp-sdc-validation-sdk/pom.xml @@ -40,6 +40,10 @@ commons-io ${commons.io.version} + + org.projectlombok + lombok + diff --git a/openecomp-be/lib/openecomp-sdc-validation-lib/openecomp-sdc-validation-sdk/src/main/java/org/openecomp/sdc/validation/base/ResourceBaseValidator.java b/openecomp-be/lib/openecomp-sdc-validation-lib/openecomp-sdc-validation-sdk/src/main/java/org/openecomp/sdc/validation/base/ResourceBaseValidator.java index 1972f6ed63..ba23c8b649 100644 --- a/openecomp-be/lib/openecomp-sdc-validation-lib/openecomp-sdc-validation-sdk/src/main/java/org/openecomp/sdc/validation/base/ResourceBaseValidator.java +++ b/openecomp-be/lib/openecomp-sdc-validation-lib/openecomp-sdc-validation-sdk/src/main/java/org/openecomp/sdc/validation/base/ResourceBaseValidator.java @@ -52,6 +52,10 @@ public class ResourceBaseValidator implements Validator { private static final ErrorMessageCode ERROR_CODE_RBV_1 = new ErrorMessageCode("RBV1"); private static final ErrorMessageCode ERROR_CODE_RBV_2 = new ErrorMessageCode("RBV2"); + Map getResourceTypeToImpl() { + return MapUtils.unmodifiableMap(this.resourceTypeToImpl); + } + @Override public void init(Map properties) { if (MapUtils.isEmpty(properties)) { diff --git a/openecomp-be/lib/openecomp-sdc-validation-lib/openecomp-sdc-validation-sdk/src/main/java/org/openecomp/sdc/validation/type/HeatResourceValidationContext.java b/openecomp-be/lib/openecomp-sdc-validation-lib/openecomp-sdc-validation-sdk/src/main/java/org/openecomp/sdc/validation/type/HeatResourceValidationContext.java index 6067876532..97703b1de0 100644 --- a/openecomp-be/lib/openecomp-sdc-validation-lib/openecomp-sdc-validation-sdk/src/main/java/org/openecomp/sdc/validation/type/HeatResourceValidationContext.java +++ b/openecomp-be/lib/openecomp-sdc-validation-lib/openecomp-sdc-validation-sdk/src/main/java/org/openecomp/sdc/validation/type/HeatResourceValidationContext.java @@ -1,3 +1,18 @@ +/* + * Copyright © 2018 European Support Limited + * + * 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. + */ package org.openecomp.sdc.validation.type; import org.openecomp.sdc.heat.datatypes.model.HeatOrchestrationTemplate; @@ -5,10 +20,14 @@ import org.openecomp.sdc.validation.ValidationContext; import java.util.List; import java.util.Map; +import lombok.Getter; +import lombok.EqualsAndHashCode; /** * Created by TALIO on 2/23/2017. */ +@Getter +@EqualsAndHashCode public class HeatResourceValidationContext implements ValidationContext { private HeatOrchestrationTemplate heatOrchestrationTemplate; @@ -26,19 +45,6 @@ public class HeatResourceValidationContext implements ValidationContext { this.heatOrchestrationTemplate = heatOrchestrationTemplate; this.fileLevelResourceDependencies = fileLevelResourceDependencies; -// this.zipLevelResourceDependencies = zipLevelResourceDependencies; this.envFileName = envFileName; } - - public HeatOrchestrationTemplate getHeatOrchestrationTemplate() { - return heatOrchestrationTemplate; - } - - public Map>>> getFileLevelResourceDependencies() { - return fileLevelResourceDependencies; - } - - public String getEnvFileName() { - return envFileName; - } } diff --git a/openecomp-be/lib/openecomp-sdc-validation-lib/openecomp-sdc-validation-sdk/src/main/java/org/openecomp/sdc/validation/type/NamingConventionValidationContext.java b/openecomp-be/lib/openecomp-sdc-validation-lib/openecomp-sdc-validation-sdk/src/main/java/org/openecomp/sdc/validation/type/NamingConventionValidationContext.java index 273cdedfa5..25b846e42b 100644 --- a/openecomp-be/lib/openecomp-sdc-validation-lib/openecomp-sdc-validation-sdk/src/main/java/org/openecomp/sdc/validation/type/NamingConventionValidationContext.java +++ b/openecomp-be/lib/openecomp-sdc-validation-lib/openecomp-sdc-validation-sdk/src/main/java/org/openecomp/sdc/validation/type/NamingConventionValidationContext.java @@ -1,11 +1,31 @@ +/* + * Copyright © 2018 European Support Limited + * + * 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. + */ package org.openecomp.sdc.validation.type; import org.openecomp.sdc.heat.datatypes.model.HeatOrchestrationTemplate; import org.openecomp.sdc.validation.ValidationContext; +import lombok.Getter; +import lombok.EqualsAndHashCode; + /** * Created by TALIO on 2/23/2017. */ +@Getter +@EqualsAndHashCode public class NamingConventionValidationContext implements ValidationContext { private HeatOrchestrationTemplate heatOrchestrationTemplate; @@ -17,12 +37,4 @@ public class NamingConventionValidationContext implements ValidationContext { this.heatOrchestrationTemplate = heatOrchestrationTemplate; this.envFileName = envFileName; } - - public HeatOrchestrationTemplate getHeatOrchestrationTemplate() { - return heatOrchestrationTemplate; - } - - public String getEnvFileName() { - return envFileName; - } } diff --git a/openecomp-be/lib/openecomp-sdc-validation-lib/openecomp-sdc-validation-sdk/src/test/java/org/openecomp/sdc/validation/base/ResourceBaseValidatorTest.java b/openecomp-be/lib/openecomp-sdc-validation-lib/openecomp-sdc-validation-sdk/src/test/java/org/openecomp/sdc/validation/base/ResourceBaseValidatorTest.java index e2d685da70..ec39ead030 100644 --- a/openecomp-be/lib/openecomp-sdc-validation-lib/openecomp-sdc-validation-sdk/src/test/java/org/openecomp/sdc/validation/base/ResourceBaseValidatorTest.java +++ b/openecomp-be/lib/openecomp-sdc-validation-lib/openecomp-sdc-validation-sdk/src/test/java/org/openecomp/sdc/validation/base/ResourceBaseValidatorTest.java @@ -1,19 +1,39 @@ +/* + * Copyright © 2018 European Support Limited + * + * 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. + */ package org.openecomp.sdc.validation.base; +import org.apache.commons.collections4.MapUtils; import org.openecomp.core.validation.types.MessageContainer; +import org.openecomp.sdc.datatypes.configuration.ImplementationConfiguration; +import org.openecomp.sdc.validation.type.ConfigConstants; import org.openecomp.sdc.validation.util.ValidationTestUtil; import org.testng.Assert; import org.testng.annotations.Test; +import java.util.Collections; +import java.util.HashMap; import java.util.Map; @Test public class ResourceBaseValidatorTest { - -ResourceBaseValidator resourceBaseValidator=new ResourceBaseValidator(); +private String testValidator = "testValidator"; @Test public void testInvalidResourceType(){ + ResourceBaseValidator resourceBaseValidator = new ResourceBaseValidator(); Map messages = ValidationTestUtil.testValidator( resourceBaseValidator, "/InvalidResourceType"); Assert.assertEquals(messages.get("first.yaml").getErrorMessageList().get(0).getMessage(), @@ -23,6 +43,7 @@ ResourceBaseValidator resourceBaseValidator=new ResourceBaseValidator(); @Test public void testInvalidHeatStructure(){ + ResourceBaseValidator resourceBaseValidator = new ResourceBaseValidator(); Map messages = ValidationTestUtil.testValidator(resourceBaseValidator, "/InvalidHeatStructure"); Assert.assertEquals(messages.get("first.yaml").getErrorMessageList().get(0).getMessage(), @@ -34,4 +55,60 @@ ResourceBaseValidator resourceBaseValidator=new ResourceBaseValidator(); " ^\n" + "]"); } + + @Test + public void testInitWithEmptyPropertiesMap() { + ResourceBaseValidator resourceBaseValidator = new ResourceBaseValidator(); + Map properties = new HashMap<>(); + resourceBaseValidator.init(properties); + + Assert.assertTrue(MapUtils.isEmpty(resourceBaseValidator.getResourceTypeToImpl())); + } + + @Test + public void testInitPropertiesMap() { + ResourceBaseValidator resourceBaseValidator = new ResourceBaseValidator(); + initProperties(resourceBaseValidator, getValidImplementationConfiguration()); + + Map resourceTypeToImpl = + resourceBaseValidator.getResourceTypeToImpl(); + Assert.assertTrue(MapUtils.isNotEmpty(resourceTypeToImpl)); + Assert.assertTrue(resourceTypeToImpl.containsKey(testValidator)); + } + + @Test + public void testInitPropertiesWithString() { + ResourceBaseValidator resourceBaseValidator = new ResourceBaseValidator(); + Map properties = new HashMap<>(); + properties.put(testValidator, "invalidValue"); + + resourceBaseValidator.init(properties); + + Assert.assertTrue(MapUtils.isEmpty(resourceBaseValidator.getResourceTypeToImpl())); + } + + @Test + public void testInitPropertiesWithoutImplClass() { + ResourceBaseValidator resourceBaseValidator = new ResourceBaseValidator(); + initProperties(resourceBaseValidator, new HashMap<>()); + + Assert.assertTrue(MapUtils.isEmpty(resourceBaseValidator.getResourceTypeToImpl())); + } + + public Map getValidImplementationConfiguration() { + Map implConfiguration = new HashMap<>(); + implConfiguration.put( + ConfigConstants.Impl_Class, "org.openecomp.sdc.validation.impl.validators.ForbiddenResourceGuideLineValidator"); + implConfiguration.put(ConfigConstants.Enable, true); + + return implConfiguration; + } + + private void initProperties(ResourceBaseValidator resourceBaseValidator, + Map implementationConfiguration) { + Map properties = + Collections.singletonMap(testValidator, implementationConfiguration); + + resourceBaseValidator.init(properties); + } } -- cgit 1.2.3-korg