Config. allowed instances in component composition
[sdc.git] / catalog-model / src / test / java / org / openecomp / sdc / be / model / jsonjanusgraph / config / ContainerInstanceTypesDataTest.java
1 /*
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2020 Nordix Foundation
4  *  ================================================================================
5  *  Licensed under the Apache License, Version 2.0 (the "License");
6  *  you may not use this file except in compliance with the License.
7  *  You may obtain a copy of the License at
8  *
9  *        http://www.apache.org/licenses/LICENSE-2.0
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  *  SPDX-License-Identifier: Apache-2.0
17  *  ============LICENSE_END=========================================================
18  */
19
20 package org.openecomp.sdc.be.model.jsonjanusgraph.config;
21
22 import static java.util.Collections.emptyList;
23 import static org.hamcrest.MatcherAssert.assertThat;
24 import static org.hamcrest.Matchers.containsInAnyOrder;
25 import static org.hamcrest.Matchers.empty;
26 import static org.hamcrest.Matchers.is;
27
28 import java.util.EnumMap;
29 import java.util.EnumSet;
30 import java.util.List;
31 import org.apache.commons.collections4.CollectionUtils;
32 import org.junit.jupiter.api.BeforeAll;
33 import org.junit.jupiter.api.BeforeEach;
34 import org.junit.jupiter.api.Test;
35 import org.openecomp.sdc.be.config.ConfigurationManager;
36 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
37 import org.openecomp.sdc.be.datatypes.enums.ResourceTypeEnum;
38 import org.openecomp.sdc.common.impl.ExternalConfiguration;
39 import org.openecomp.sdc.common.impl.FSConfigurationSource;
40
41 public class ContainerInstanceTypesDataTest {
42
43     private ContainerInstanceTypesData containerInstanceTypesData;
44     private EnumSet<ResourceTypeEnum> serviceAllowedTypes;
45     private EnumMap<ResourceTypeEnum, EnumSet<ResourceTypeEnum>> resourceAllowedTypeConfig;
46     private EnumSet<ResourceTypeEnum> allResourceTypes;
47
48     @BeforeAll
49     public static void beforeClass() {
50         new ConfigurationManager(new FSConfigurationSource(ExternalConfiguration.getChangeListener(),
51             "src/test/resources/config"));
52     }
53
54     @BeforeEach
55     public void setUp() {
56         containerInstanceTypesData = new ContainerInstanceTypesData();
57
58         allResourceTypes = EnumSet.allOf(ResourceTypeEnum.class);
59
60         serviceAllowedTypes = EnumSet.of(ResourceTypeEnum.VF,
61             ResourceTypeEnum.CR,
62             ResourceTypeEnum.CP,
63             ResourceTypeEnum.PNF,
64             ResourceTypeEnum.CVFC,
65             ResourceTypeEnum.VL,
66             ResourceTypeEnum.Configuration,
67             ResourceTypeEnum.ServiceProxy,
68             ResourceTypeEnum.ABSTRACT
69         );
70
71         resourceAllowedTypeConfig =
72             new EnumMap<>(ResourceTypeEnum.class);
73
74         resourceAllowedTypeConfig.put(ResourceTypeEnum.VF,
75             EnumSet.of(ResourceTypeEnum.VFC,
76                 ResourceTypeEnum.VF,
77                 ResourceTypeEnum.CR,
78                 ResourceTypeEnum.CP,
79                 ResourceTypeEnum.PNF,
80                 ResourceTypeEnum.CVFC,
81                 ResourceTypeEnum.VL,
82                 ResourceTypeEnum.Configuration,
83                 ResourceTypeEnum.ServiceProxy,
84                 ResourceTypeEnum.ABSTRACT));
85
86         resourceAllowedTypeConfig.put(ResourceTypeEnum.CVFC,
87             EnumSet.of(ResourceTypeEnum.VFC,
88                 ResourceTypeEnum.VF,
89                 ResourceTypeEnum.CR,
90                 ResourceTypeEnum.CP,
91                 ResourceTypeEnum.PNF,
92                 ResourceTypeEnum.CVFC,
93                 ResourceTypeEnum.VL,
94                 ResourceTypeEnum.ServiceProxy,
95                 ResourceTypeEnum.ABSTRACT));
96
97         resourceAllowedTypeConfig.put(ResourceTypeEnum.PNF,
98             EnumSet.of(ResourceTypeEnum.VF,
99                 ResourceTypeEnum.CR,
100                 ResourceTypeEnum.CP,
101                 ResourceTypeEnum.PNF,
102                 ResourceTypeEnum.CVFC,
103                 ResourceTypeEnum.VL,
104                 ResourceTypeEnum.Configuration,
105                 ResourceTypeEnum.ServiceProxy,
106                 ResourceTypeEnum.ABSTRACT));
107
108         resourceAllowedTypeConfig.put(ResourceTypeEnum.CR,
109             EnumSet.of(ResourceTypeEnum.VF,
110                 ResourceTypeEnum.CR,
111                 ResourceTypeEnum.CP,
112                 ResourceTypeEnum.PNF,
113                 ResourceTypeEnum.CVFC,
114                 ResourceTypeEnum.VL,
115                 ResourceTypeEnum.Configuration,
116                 ResourceTypeEnum.ServiceProxy,
117                 ResourceTypeEnum.ABSTRACT));
118
119         resourceAllowedTypeConfig.put(ResourceTypeEnum.VL,
120             EnumSet.of(ResourceTypeEnum.VL));
121     }
122
123     @Test
124     public void isAllowedForServiceComponent() {
125         for (final ResourceTypeEnum allowedType : serviceAllowedTypes) {
126             assertThat(String.format("%s should be allowed", allowedType.getValue()),
127                 containerInstanceTypesData.isAllowedForServiceComponent(allowedType), is(true));
128         }
129     }
130
131     @Test
132     public void isAllowedForResourceComponent() {
133         for (final ResourceTypeEnum componentResourceType : allResourceTypes) {
134             final EnumSet<ResourceTypeEnum> allowedResourceType = resourceAllowedTypeConfig.get(componentResourceType);
135             for (final ResourceTypeEnum resourceType : allResourceTypes) {
136                 if (allowedResourceType == null) {
137                     final String msg = String
138                         .format("'%s' resource type should not be allowed", resourceType.getValue());
139                     assertThat(msg, containerInstanceTypesData
140                         .isAllowedForResourceComponent(componentResourceType, resourceType), is(false));
141                     continue;
142                 }
143                 final boolean isAllowed = allowedResourceType.contains(resourceType);
144                 final String msg = String
145                     .format("'%s' resource type should %s be allowed", resourceType.getValue(), isAllowed ? "" : "not");
146                 assertThat(msg, containerInstanceTypesData
147                     .isAllowedForResourceComponent(componentResourceType, resourceType), is(
148                     isAllowed));
149             }
150         }
151     }
152
153     @Test
154     public void getComponentAllowedListTest() {
155         List<String> actualAllowedList = containerInstanceTypesData
156             .getComponentAllowedList(ComponentTypeEnum.SERVICE, null);
157         assertThat("Allowed Instance Resource Type List should be as expected",
158             actualAllowedList, containsInAnyOrder(actualAllowedList.toArray()));
159
160         for (final ResourceTypeEnum resourceType : allResourceTypes) {
161             actualAllowedList = containerInstanceTypesData
162                 .getComponentAllowedList(ComponentTypeEnum.RESOURCE, resourceType);
163             final EnumSet<ResourceTypeEnum> expectedAllowedSet = resourceAllowedTypeConfig.get(resourceType);
164             if (CollectionUtils.isEmpty(expectedAllowedSet)) {
165                 assertThat("Allowed Instance Resource Type List should be as expected",
166                     actualAllowedList, is(empty()));
167                 continue;
168             }
169             assertThat("Allowed Instance Resource Type List should be as expected",
170                 actualAllowedList,
171                 containsInAnyOrder(expectedAllowedSet.stream().map(ResourceTypeEnum::getValue).toArray()));
172         }
173     }
174
175     @Test
176     public void getComponentAllowedListTestEmptyList() {
177         List<String> actualAllowedList = containerInstanceTypesData
178             .getComponentAllowedList(ComponentTypeEnum.PRODUCT, null);
179         final String msg = "Allowed Instance Resource Type List should be empty";
180         assertThat(msg, actualAllowedList, is(emptyList()));
181
182         actualAllowedList = containerInstanceTypesData
183             .getComponentAllowedList(ComponentTypeEnum.SERVICE, ResourceTypeEnum.VF);
184         assertThat(msg, actualAllowedList, is(emptyList()));
185
186         actualAllowedList = containerInstanceTypesData
187             .getComponentAllowedList(ComponentTypeEnum.SERVICE, ResourceTypeEnum.ServiceProxy);
188         assertThat(msg, actualAllowedList, is(emptyList()));
189
190         actualAllowedList = containerInstanceTypesData
191             .getComponentAllowedList(ComponentTypeEnum.RESOURCE, null);
192         assertThat(msg, actualAllowedList, is(emptyList()));
193     }
194
195 }