35a8c00ee969cf4293bb87f949abadf91f87bf68
[sdc.git] /
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 static org.junit.jupiter.api.Assertions.assertEquals;
19 import static org.junit.jupiter.api.Assertions.assertTrue;
20
21 import org.apache.commons.collections4.MapUtils;
22 import org.junit.jupiter.api.Test;
23 import org.openecomp.core.validation.types.MessageContainer;
24 import org.openecomp.sdc.datatypes.configuration.ImplementationConfiguration;
25 import org.openecomp.sdc.validation.type.ConfigConstants;
26 import org.openecomp.sdc.validation.util.ValidationTestUtil;
27
28 import java.util.Collections;
29 import java.util.HashMap;
30 import java.util.Map;
31
32 public class ResourceBaseValidatorTest {
33 private String testValidator = "testValidator";
34
35   @Test
36   public void testInvalidResourceType(){
37     ResourceBaseValidator resourceBaseValidator = new ResourceBaseValidator();
38     Map<String, MessageContainer> messages = ValidationTestUtil.testValidator(
39         resourceBaseValidator, "/InvalidResourceType");
40     assertEquals(messages.get("first.yaml").getErrorMessageList().get(0).getMessage(),
41         "WARNING: [RBV1]: A resource has an invalid or unsupported type - null, " +
42             "Resource ID [FSB2]");
43   }
44
45   @Test
46   public void testInvalidHeatStructure(){
47     ResourceBaseValidator resourceBaseValidator = new ResourceBaseValidator();
48     Map<String, MessageContainer> messages = ValidationTestUtil.testValidator(resourceBaseValidator,
49         "/InvalidHeatStructure");
50     assertEquals(messages.get("first.yaml").getErrorMessageList().get(0).getMessage(),
51         "ERROR: [RBV2]: Invalid HEAT format problem - [while scanning for the next " +
52         "token\n" + "found character '\\t(TAB)' that cannot start any token. " +
53         "(Do not use \\t(TAB) for indentation)\n" +
54         " in 'reader', line 10, column 1:\n" +
55         "    \t\t\tresources:\n" +
56         "    ^\n" +
57         "]");
58   }
59
60   @Test
61   public void testInitWithEmptyPropertiesMap() {
62     ResourceBaseValidator resourceBaseValidator = new ResourceBaseValidator();
63     Map<String, Object> properties = new HashMap<>();
64     resourceBaseValidator.init(properties);
65
66     assertTrue(MapUtils.isEmpty(resourceBaseValidator.getResourceTypeToImpl()));
67   }
68
69   @Test
70   public void testInitPropertiesMap() {
71     ResourceBaseValidator resourceBaseValidator = new ResourceBaseValidator();
72     initProperties(resourceBaseValidator, getValidImplementationConfiguration());
73
74     Map<String, ImplementationConfiguration> resourceTypeToImpl =
75         resourceBaseValidator.getResourceTypeToImpl();
76     assertTrue(MapUtils.isNotEmpty(resourceTypeToImpl));
77     assertTrue(resourceTypeToImpl.containsKey(testValidator));
78   }
79
80   @Test
81   public void testInitPropertiesWithString() {
82     ResourceBaseValidator resourceBaseValidator = new ResourceBaseValidator();
83     Map<String, Object> properties = new HashMap<>();
84     properties.put(testValidator, "invalidValue");
85
86     resourceBaseValidator.init(properties);
87
88     assertTrue(MapUtils.isEmpty(resourceBaseValidator.getResourceTypeToImpl()));
89   }
90
91   @Test
92   public void testInitPropertiesWithoutImplClass() {
93     ResourceBaseValidator resourceBaseValidator = new ResourceBaseValidator();
94     initProperties(resourceBaseValidator, new HashMap<>());
95
96     assertTrue(MapUtils.isEmpty(resourceBaseValidator.getResourceTypeToImpl()));
97   }
98
99   public Map<String, Object> getValidImplementationConfiguration() {
100     Map<String, Object> implConfiguration = new HashMap<>();
101     implConfiguration.put(
102         ConfigConstants.Impl_Class, "org.openecomp.sdc.validation.impl.validators.ForbiddenResourceGuideLineValidator");
103     implConfiguration.put(ConfigConstants.Enable, true);
104
105     return implConfiguration;
106   }
107
108   private void initProperties(ResourceBaseValidator resourceBaseValidator,
109                               Map<String, Object> implementationConfiguration) {
110     Map<String, Object> properties =
111         Collections.singletonMap(testValidator, implementationConfiguration);
112
113     resourceBaseValidator.init(properties);
114   }
115 }