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