test coverage
[sdc.git] / openecomp-be / lib / openecomp-sdc-validation-lib / openecomp-sdc-validation-sdk / src / test / java / org / openecomp / sdc / validation / base / ResourceBaseValidatorTest.java
1 /*
2  * Copyright © 2018 European Support Limited
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package org.openecomp.sdc.validation.base;
17
18 import org.apache.commons.collections4.MapUtils;
19 import org.openecomp.core.validation.types.MessageContainer;
20 import org.openecomp.sdc.datatypes.configuration.ImplementationConfiguration;
21 import org.openecomp.sdc.validation.type.ConfigConstants;
22 import org.openecomp.sdc.validation.util.ValidationTestUtil;
23 import org.testng.Assert;
24 import org.testng.annotations.Test;
25
26 import java.util.Collections;
27 import java.util.HashMap;
28 import java.util.Map;
29
30 @Test
31 public class ResourceBaseValidatorTest {
32 private String testValidator = "testValidator";
33
34   @Test
35   public void testInvalidResourceType(){
36     ResourceBaseValidator resourceBaseValidator = new ResourceBaseValidator();
37     Map<String, MessageContainer> messages = ValidationTestUtil.testValidator(
38         resourceBaseValidator, "/InvalidResourceType");
39     Assert.assertEquals(messages.get("first.yaml").getErrorMessageList().get(0).getMessage(),
40         "WARNING: [RBV1]: A resource has an invalid or unsupported type - null, " +
41             "Resource ID [FSB2]");
42   }
43
44   @Test
45   public void testInvalidHeatStructure(){
46     ResourceBaseValidator resourceBaseValidator = new ResourceBaseValidator();
47     Map<String, MessageContainer> messages = ValidationTestUtil.testValidator(resourceBaseValidator,
48         "/InvalidHeatStructure");
49     Assert.assertEquals(messages.get("first.yaml").getErrorMessageList().get(0).getMessage(),
50         "ERROR: [RBV2]: Invalid HEAT format problem - [while scanning for the next " +
51         "token\n" + "found character '\\t(TAB)' that cannot start any token. " +
52         "(Do not use \\t(TAB) for indentation)\n" +
53         " in 'reader', line 10, column 1:\n" +
54         "    \t\t\tresources:\n" +
55         "    ^\n" +
56         "]");
57   }
58
59   @Test
60   public void testInitWithEmptyPropertiesMap() {
61     ResourceBaseValidator resourceBaseValidator = new ResourceBaseValidator();
62     Map<String, Object> properties = new HashMap<>();
63     resourceBaseValidator.init(properties);
64
65     Assert.assertTrue(MapUtils.isEmpty(resourceBaseValidator.getResourceTypeToImpl()));
66   }
67
68   @Test
69   public void testInitPropertiesMap() {
70     ResourceBaseValidator resourceBaseValidator = new ResourceBaseValidator();
71     initProperties(resourceBaseValidator, getValidImplementationConfiguration());
72
73     Map<String, ImplementationConfiguration> resourceTypeToImpl =
74         resourceBaseValidator.getResourceTypeToImpl();
75     Assert.assertTrue(MapUtils.isNotEmpty(resourceTypeToImpl));
76     Assert.assertTrue(resourceTypeToImpl.containsKey(testValidator));
77   }
78
79   @Test
80   public void testInitPropertiesWithString() {
81     ResourceBaseValidator resourceBaseValidator = new ResourceBaseValidator();
82     Map<String, Object> properties = new HashMap<>();
83     properties.put(testValidator, "invalidValue");
84
85     resourceBaseValidator.init(properties);
86
87     Assert.assertTrue(MapUtils.isEmpty(resourceBaseValidator.getResourceTypeToImpl()));
88   }
89
90   @Test
91   public void testInitPropertiesWithoutImplClass() {
92     ResourceBaseValidator resourceBaseValidator = new ResourceBaseValidator();
93     initProperties(resourceBaseValidator, new HashMap<>());
94
95     Assert.assertTrue(MapUtils.isEmpty(resourceBaseValidator.getResourceTypeToImpl()));
96   }
97
98   public Map<String, Object> getValidImplementationConfiguration() {
99     Map<String, Object> implConfiguration = new HashMap<>();
100     implConfiguration.put(
101         ConfigConstants.Impl_Class, "org.openecomp.sdc.validation.impl.validators.ForbiddenResourceGuideLineValidator");
102     implConfiguration.put(ConfigConstants.Enable, true);
103
104     return implConfiguration;
105   }
106
107   private void initProperties(ResourceBaseValidator resourceBaseValidator,
108                               Map<String, Object> implementationConfiguration) {
109     Map<String, Object> properties =
110         Collections.singletonMap(testValidator, implementationConfiguration);
111
112     resourceBaseValidator.init(properties);
113   }
114 }