d7f70ae8a7bc4459d9bc3a8234cdcd924f3be024
[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 static org.assertj.core.api.Assertions.assertThat;
24 import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
25 import static org.mockito.Mockito.when;
26
27 import fj.data.Either;
28 import java.util.ArrayList;
29 import java.util.Arrays;
30 import java.util.HashMap;
31 import java.util.List;
32 import java.util.Map;
33 import org.junit.Test;
34 import org.junit.runner.RunWith;
35 import org.mockito.Mock;
36 import org.mockito.junit.MockitoJUnitRunner;
37 import org.openecomp.sdc.be.components.impl.exceptions.SdcResourceNotFoundException;
38 import org.openecomp.sdc.be.datatypes.elements.PolicyTargetType;
39 import org.openecomp.sdc.be.model.Component;
40 import org.openecomp.sdc.be.model.ComponentInstance;
41 import org.openecomp.sdc.be.model.GroupDefinition;
42 import org.openecomp.sdc.be.model.PolicyDefinition;
43 import org.openecomp.sdc.be.model.cache.ApplicationDataTypeCache;
44 import org.openecomp.sdc.be.tosca.model.ToscaMetadata;
45 import org.openecomp.sdc.be.tosca.model.ToscaPolicyTemplate;
46
47 @RunWith(MockitoJUnitRunner.class)
48 public class PolicyExportParserImplTest {
49
50         private static final String[] POLICY_KEYS = {"policy_key_1","policy_key_2"};
51         private static final String[] VERSIONS = {"version_1","version_1"};
52         private static final String[] POLICY_NAMES = {"name_1","name_2"};
53         private static final String[] POLICY_UUIDS = {"policyUUID_1","policyUUID_2"};
54         private static final String[] INVARIANT_UUIDS = {"invariantUUID_1","invariantUUID_2"};
55         private static final String[] POLICY_TYPE_NAMES = {"policyTypeName_1","policyTypeName_2"};
56         private static final String[] POLICY_COMPONENT_INSTANCES = {"policyComponentInstanceId"};
57         private static final String POLICY_COMPONENT_INSTANCES_NAME = "policyComponentInstanceName";
58         private static final String[] POLICY_GROUPS = {"policyGroupId"};
59         private static final String POLICY_GROUP_NAME = "PolicyGroupName";
60         
61         private PolicyExportParser policiyExportParser;
62     
63         @Mock
64         private ApplicationDataTypeCache applicationDataTypeCache;
65         @Mock
66         private PropertyConvertor propertyConvertor;
67         
68         @Mock
69         private Component component;
70                 
71         @Test
72         public void failToGetAllDataTypes() {
73                 
74                 when(applicationDataTypeCache.getAll(null)).thenReturn(Either.right(null));
75                 assertThatExceptionOfType(SdcResourceNotFoundException.class).isThrownBy(() -> policiyExportParser = new PolicyExportParserImpl(
76                         applicationDataTypeCache,
77                 propertyConvertor));
78         }
79         
80         @Test
81         public void noPoliciesInComponent() {
82                 
83                 when(applicationDataTypeCache.getAll(null)).thenReturn(Either.left(null));
84                 when(component.getPolicies()).thenReturn(null);
85                 policiyExportParser = new PolicyExportParserImpl(applicationDataTypeCache, propertyConvertor);
86                 Map<String, ToscaPolicyTemplate> policies = policiyExportParser.getPolicies(component);
87                 assertThat(policies).isEqualTo(null);
88         }
89         
90         @Test
91         public void onePoliciesInComponent() {
92                 
93                 List<Integer> constIndexes = Arrays.asList(new Integer[] {0});
94             testPoliciesInComponent(constIndexes);
95         }
96         
97         @Test
98         public void twoPoliciesInComponent() {
99                 
100                 List<Integer> constIndexes = Arrays.asList(new Integer[] {0,1});                
101                 testPoliciesInComponent(constIndexes);          
102         }
103
104         private void testPoliciesInComponent(List<Integer> constIndexes) {
105                 when(applicationDataTypeCache.getAll(null)).thenReturn(Either.left(null));
106                 Map<String, PolicyDefinition> policiesToAdd = getPolicies(constIndexes);
107                 
108                 when(component.getPolicies()).thenReturn(policiesToAdd);
109                 when(component.getComponentInstances()).thenReturn(getComponentInstances());
110                 when(component.getGroups()).thenReturn(getGroups());
111                 policiyExportParser = new PolicyExportParserImpl(applicationDataTypeCache, propertyConvertor);
112                 
113                 Map<String, ToscaPolicyTemplate> policies = policiyExportParser.getPolicies(component);
114                 
115                 for(Integer i : constIndexes) {
116                         
117                         
118                         ToscaPolicyTemplate toscaPolicyTemplate = policies.get(POLICY_NAMES[i]);
119                         ToscaMetadata metadata = (ToscaMetadata) toscaPolicyTemplate.getMetadata();
120                         
121                         assertThat(metadata.getInvariantUUID()).isEqualTo(INVARIANT_UUIDS[i]);
122                         assertThat(metadata.getUUID()).isEqualTo(POLICY_UUIDS[i]);
123                         assertThat(metadata.getName()).isEqualTo(POLICY_NAMES[i]);
124                         assertThat(metadata.getVersion()).isEqualTo(VERSIONS[i]);
125                         
126                         String type = toscaPolicyTemplate.getType();
127                         assertThat(type).isEqualTo(POLICY_TYPE_NAMES[i]);
128                         
129                         List<String> targets = toscaPolicyTemplate.getTargets();
130                         assertThat(targets.get(0)).isEqualTo(POLICY_COMPONENT_INSTANCES_NAME);
131                         assertThat(targets.get(1)).isEqualTo(POLICY_GROUP_NAME);                        
132                 }               
133         }       
134
135         private List<GroupDefinition> getGroups() {
136                 List<GroupDefinition> groups = new ArrayList<>();
137                 GroupDefinition groupDefinition = new GroupDefinition();
138                 groupDefinition.setUniqueId(POLICY_GROUPS[0]);
139                 groupDefinition.setName(POLICY_GROUP_NAME);
140                 groups.add(groupDefinition);
141                 return groups;
142         }
143
144         private List<ComponentInstance> getComponentInstances() {
145                 List<ComponentInstance> componentInstances = new ArrayList<>();
146                 ComponentInstance componentInstance = new ComponentInstance();
147                 componentInstance.setUniqueId(POLICY_COMPONENT_INSTANCES[0]);
148                 componentInstance.setName(POLICY_COMPONENT_INSTANCES_NAME);
149                 componentInstances.add(componentInstance);
150                 return componentInstances;
151         }
152
153         private Map<String, PolicyDefinition> getPolicies(List<Integer> indexes) {
154                 Map<String, PolicyDefinition> policies = new HashMap<>();
155                 
156                 for (int index : indexes) {
157                         
158                         PolicyDefinition policyDefinition = new PolicyDefinition();
159                         
160                         // Set type
161                         policyDefinition.setPolicyTypeName(POLICY_TYPE_NAMES[index]);
162                         
163                         // Set Metadata
164                         policyDefinition.setInvariantUUID(INVARIANT_UUIDS[index]);
165                         policyDefinition.setPolicyUUID(POLICY_UUIDS[index]);
166                         policyDefinition.setName(POLICY_NAMES[index]);
167                         policyDefinition.setVersion(VERSIONS[index]);
168                         
169                         // Set targets
170                         policyDefinition.setTargets(getTargers());                      
171                         
172                         policies.put(POLICY_KEYS[index],policyDefinition);
173                 }               
174                 return policies;
175         }
176
177         private Map<PolicyTargetType, List<String>> getTargers() {
178                 Map<PolicyTargetType, List<String>> targets = new HashMap<>();
179                 targets.put(PolicyTargetType.COMPONENT_INSTANCES, Arrays.asList(POLICY_COMPONENT_INSTANCES));
180                 targets.put(PolicyTargetType.GROUPS, Arrays.asList(POLICY_GROUPS));
181                 return targets;
182         }       
183 }