Catalog alignment
[sdc.git] / catalog-be / src / test / java / org / openecomp / sdc / be / tosca / PolicyExportParserImplTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.openecomp.sdc.be.tosca;
22
23 import fj.data.Either;
24 import org.junit.Test;
25 import org.junit.runner.RunWith;
26 import org.mockito.Mock;
27 import org.mockito.junit.MockitoJUnitRunner;
28 import org.openecomp.sdc.be.components.impl.exceptions.SdcResourceNotFoundException;
29 import org.openecomp.sdc.be.datatypes.elements.PolicyTargetType;
30 import org.openecomp.sdc.be.model.Component;
31 import org.openecomp.sdc.be.model.ComponentInstance;
32 import org.openecomp.sdc.be.model.GroupDefinition;
33 import org.openecomp.sdc.be.model.PolicyDefinition;
34 import org.openecomp.sdc.be.model.cache.ApplicationDataTypeCache;
35 import org.openecomp.sdc.be.tosca.model.ToscaMetadata;
36 import org.openecomp.sdc.be.tosca.model.ToscaPolicyTemplate;
37
38 import java.util.ArrayList;
39 import java.util.Arrays;
40 import java.util.HashMap;
41 import java.util.List;
42 import java.util.Map;
43
44 import static org.assertj.core.api.Assertions.assertThat;
45 import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
46 import static org.mockito.Mockito.when;
47
48 @RunWith(MockitoJUnitRunner.class)
49 public class PolicyExportParserImplTest {
50
51         private static final String[] POLICY_KEYS = {"policy_key_1","policy_key_2"};
52         private static final String[] VERSIONS = {"version_1","version_1"};
53         private static final String[] POLICY_NAMES = {"name_1","name_2"};
54         private static final String[] POLICY_UUIDS = {"policyUUID_1","policyUUID_2"};
55         private static final String[] INVARIANT_UUIDS = {"invariantUUID_1","invariantUUID_2"};
56         private static final String[] POLICY_TYPE_NAMES = {"policyTypeName_1","policyTypeName_2"};
57         private static final String[] POLICY_COMPONENT_INSTANCES = {"policyComponentInstanceId"};
58         private static final String POLICY_COMPONENT_INSTANCES_NAME = "policyComponentInstanceName";
59         private static final String[] POLICY_GROUPS = {"policyGroupId"};
60         private static final String POLICY_GROUP_NAME = "PolicyGroupName";
61         
62         private PolicyExportParser policiyExportParser;
63     
64         @Mock
65         private ApplicationDataTypeCache dataTypeCache;
66         
67         @Mock
68         private Component component;
69                 
70         @Test
71         public void failToGetAllDataTypes() {
72                 
73                 when(dataTypeCache.getAll()).thenReturn(Either.right(null));
74                 assertThatExceptionOfType(SdcResourceNotFoundException.class).isThrownBy(() -> policiyExportParser = new PolicyExportParserImpl(dataTypeCache));
75         }
76         
77         @Test
78         public void noPoliciesInComponent() {
79                 
80                 when(dataTypeCache.getAll()).thenReturn(Either.left(null));
81                 when(component.getPolicies()).thenReturn(null);
82                 policiyExportParser = new PolicyExportParserImpl(dataTypeCache);
83                 Map<String, ToscaPolicyTemplate> policies = policiyExportParser.getPolicies(component);
84                 assertThat(policies).isEqualTo(null);
85         }
86         
87         @Test
88         public void onePoliciesInComponent() {
89                 
90                 List<Integer> constIndexes = Arrays.asList(new Integer[] {0});
91             testPoliciesInComponent(constIndexes);
92         }
93         
94         @Test
95         public void twoPoliciesInComponent() {
96                 
97                 List<Integer> constIndexes = Arrays.asList(new Integer[] {0,1});                
98                 testPoliciesInComponent(constIndexes);          
99         }
100
101         private void testPoliciesInComponent(List<Integer> constIndexes) {
102                 when(dataTypeCache.getAll()).thenReturn(Either.left(null));
103                 Map<String, PolicyDefinition> policiesToAdd = getPolicies(constIndexes);
104                 
105                 when(component.getPolicies()).thenReturn(policiesToAdd);
106                 when(component.getComponentInstances()).thenReturn(getComponentInstances());
107                 when(component.getGroups()).thenReturn(getGroups());
108                 policiyExportParser = new PolicyExportParserImpl(dataTypeCache);
109                 
110                 Map<String, ToscaPolicyTemplate> policies = policiyExportParser.getPolicies(component);
111                 
112                 for(Integer i : constIndexes) {
113                         
114                         
115                         ToscaPolicyTemplate toscaPolicyTemplate = policies.get(POLICY_NAMES[i]);
116                         ToscaMetadata metadata = (ToscaMetadata) toscaPolicyTemplate.getMetadata();
117                         
118                         assertThat(metadata.getInvariantUUID()).isEqualTo(INVARIANT_UUIDS[i]);
119                         assertThat(metadata.getUUID()).isEqualTo(POLICY_UUIDS[i]);
120                         assertThat(metadata.getName()).isEqualTo(POLICY_NAMES[i]);
121                         assertThat(metadata.getVersion()).isEqualTo(VERSIONS[i]);
122                         
123                         String type = toscaPolicyTemplate.getType();
124                         assertThat(type).isEqualTo(POLICY_TYPE_NAMES[i]);
125                         
126                         List<String> targets = toscaPolicyTemplate.getTargets();
127                         assertThat(targets.get(0)).isEqualTo(POLICY_COMPONENT_INSTANCES_NAME);
128                         assertThat(targets.get(1)).isEqualTo(POLICY_GROUP_NAME);                        
129                 }               
130         }       
131
132         private List<GroupDefinition> getGroups() {
133                 List<GroupDefinition> groups = new ArrayList<>();
134                 GroupDefinition groupDefinition = new GroupDefinition();
135                 groupDefinition.setUniqueId(POLICY_GROUPS[0]);
136                 groupDefinition.setName(POLICY_GROUP_NAME);
137                 groups.add(groupDefinition);
138                 return groups;
139         }
140
141         private List<ComponentInstance> getComponentInstances() {
142                 List<ComponentInstance> componentInstances = new ArrayList<>();
143                 ComponentInstance componentInstance = new ComponentInstance();
144                 componentInstance.setUniqueId(POLICY_COMPONENT_INSTANCES[0]);
145                 componentInstance.setName(POLICY_COMPONENT_INSTANCES_NAME);
146                 componentInstances.add(componentInstance);
147                 return componentInstances;
148         }
149
150         private Map<String, PolicyDefinition> getPolicies(List<Integer> indexes) {
151                 Map<String, PolicyDefinition> policies = new HashMap<>();
152                 
153                 for (int index : indexes) {
154                         
155                         PolicyDefinition policyDefinition = new PolicyDefinition();
156                         
157                         // Set type
158                         policyDefinition.setPolicyTypeName(POLICY_TYPE_NAMES[index]);
159                         
160                         // Set Metadata
161                         policyDefinition.setInvariantUUID(INVARIANT_UUIDS[index]);
162                         policyDefinition.setPolicyUUID(POLICY_UUIDS[index]);
163                         policyDefinition.setName(POLICY_NAMES[index]);
164                         policyDefinition.setVersion(VERSIONS[index]);
165                         
166                         // Set targets
167                         policyDefinition.setTargets(getTargers());                      
168                         
169                         policies.put(POLICY_KEYS[index],policyDefinition);
170                 }               
171                 return policies;
172         }
173
174         private Map<PolicyTargetType, List<String>> getTargers() {
175                 Map<PolicyTargetType, List<String>> targets = new HashMap<>();
176                 targets.put(PolicyTargetType.COMPONENT_INSTANCES, Arrays.asList(POLICY_COMPONENT_INSTANCES));
177                 targets.put(PolicyTargetType.GROUPS, Arrays.asList(POLICY_GROUPS));
178                 return targets;
179         }       
180 }