aboutsummaryrefslogtreecommitdiffstats
path: root/catalog-be/src/test/java/org/openecomp/sdc/be/tosca/PolicyExportParserImplTest.java
blob: b499830138ec4668880821a20dc34e71011e60df (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/*-
 * ============LICENSE_START=======================================================
 * SDC
 * ================================================================================
 * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */

package org.openecomp.sdc.be.tosca;

import fj.data.Either;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import org.openecomp.sdc.be.components.impl.exceptions.SdcResourceNotFoundException;
import org.openecomp.sdc.be.datatypes.elements.PolicyTargetType;
import org.openecomp.sdc.be.model.Component;
import org.openecomp.sdc.be.model.ComponentInstance;
import org.openecomp.sdc.be.model.GroupDefinition;
import org.openecomp.sdc.be.model.PolicyDefinition;
import org.openecomp.sdc.be.model.cache.ApplicationDataTypeCache;
import org.openecomp.sdc.be.tosca.model.ToscaMetadata;
import org.openecomp.sdc.be.tosca.model.ToscaPolicyTemplate;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.mockito.Mockito.when;

@RunWith(MockitoJUnitRunner.class)
public class PolicyExportParserImplTest {

	private static final String[] POLICY_KEYS = {"policy_key_1","policy_key_2"};
	private static final String[] VERSIONS = {"version_1","version_1"};
	private static final String[] POLICY_NAMES = {"name_1","name_2"};
	private static final String[] POLICY_UUIDS = {"policyUUID_1","policyUUID_2"};
	private static final String[] INVARIANT_UUIDS = {"invariantUUID_1","invariantUUID_2"};
	private static final String[] POLICY_TYPE_NAMES = {"policyTypeName_1","policyTypeName_2"};
	private static final String[] POLICY_COMPONENT_INSTANCES = {"policyComponentInstanceId"};
	private static final String POLICY_COMPONENT_INSTANCES_NAME = "policyComponentInstanceName";
	private static final String[] POLICY_GROUPS = {"policyGroupId"};
	private static final String POLICY_GROUP_NAME = "PolicyGroupName";
	
	private PolicyExportParser policiyExportParser;
    
	@Mock
	private ApplicationDataTypeCache dataTypeCache;
	@Mock
	private PropertyConvertor propertyConvertor;
	
	@Mock
	private Component component;
		
	@Test
	public void failToGetAllDataTypes() {
		
		when(dataTypeCache.getAll()).thenReturn(Either.right(null));
		assertThatExceptionOfType(SdcResourceNotFoundException.class).isThrownBy(() -> policiyExportParser = new PolicyExportParserImpl(dataTypeCache,
                propertyConvertor));
	}
	
	@Test
	public void noPoliciesInComponent() {
		
		when(dataTypeCache.getAll()).thenReturn(Either.left(null));
		when(component.getPolicies()).thenReturn(null);
		policiyExportParser = new PolicyExportParserImpl(dataTypeCache, propertyConvertor);
		Map<String, ToscaPolicyTemplate> policies = policiyExportParser.getPolicies(component);
		assertThat(policies).isEqualTo(null);
	}
	
	@Test
	public void onePoliciesInComponent() {
		
		List<Integer> constIndexes = Arrays.asList(new Integer[] {0});
	    testPoliciesInComponent(constIndexes);
	}
	
	@Test
	public void twoPoliciesInComponent() {
		
		List<Integer> constIndexes = Arrays.asList(new Integer[] {0,1});		
		testPoliciesInComponent(constIndexes);		
	}

	private void testPoliciesInComponent(List<Integer> constIndexes) {
		when(dataTypeCache.getAll()).thenReturn(Either.left(null));
		Map<String, PolicyDefinition> policiesToAdd = getPolicies(constIndexes);
		
		when(component.getPolicies()).thenReturn(policiesToAdd);
		when(component.getComponentInstances()).thenReturn(getComponentInstances());
		when(component.getGroups()).thenReturn(getGroups());
		policiyExportParser = new PolicyExportParserImpl(dataTypeCache, propertyConvertor);
		
		Map<String, ToscaPolicyTemplate> policies = policiyExportParser.getPolicies(component);
		
		for(Integer i : constIndexes) {
			
			
			ToscaPolicyTemplate toscaPolicyTemplate = policies.get(POLICY_NAMES[i]);
			ToscaMetadata metadata = (ToscaMetadata) toscaPolicyTemplate.getMetadata();
			
			assertThat(metadata.getInvariantUUID()).isEqualTo(INVARIANT_UUIDS[i]);
			assertThat(metadata.getUUID()).isEqualTo(POLICY_UUIDS[i]);
			assertThat(metadata.getName()).isEqualTo(POLICY_NAMES[i]);
			assertThat(metadata.getVersion()).isEqualTo(VERSIONS[i]);
			
			String type = toscaPolicyTemplate.getType();
			assertThat(type).isEqualTo(POLICY_TYPE_NAMES[i]);
			
			List<String> targets = toscaPolicyTemplate.getTargets();
			assertThat(targets.get(0)).isEqualTo(POLICY_COMPONENT_INSTANCES_NAME);
			assertThat(targets.get(1)).isEqualTo(POLICY_GROUP_NAME);			
		}		
	}	

	private List<GroupDefinition> getGroups() {
		List<GroupDefinition> groups = new ArrayList<>();
		GroupDefinition groupDefinition = new GroupDefinition();
		groupDefinition.setUniqueId(POLICY_GROUPS[0]);
		groupDefinition.setName(POLICY_GROUP_NAME);
		groups.add(groupDefinition);
		return groups;
	}

	private List<ComponentInstance> getComponentInstances() {
		List<ComponentInstance> componentInstances = new ArrayList<>();
		ComponentInstance componentInstance = new ComponentInstance();
		componentInstance.setUniqueId(POLICY_COMPONENT_INSTANCES[0]);
		componentInstance.setName(POLICY_COMPONENT_INSTANCES_NAME);
		componentInstances.add(componentInstance);
		return componentInstances;
	}

	private Map<String, PolicyDefinition> getPolicies(List<Integer> indexes) {
		Map<String, PolicyDefinition> policies = new HashMap<>();
		
		for (int index : indexes) {
			
			PolicyDefinition policyDefinition = new PolicyDefinition();
			
			// Set type
			policyDefinition.setPolicyTypeName(POLICY_TYPE_NAMES[index]);
			
			// Set Metadata
			policyDefinition.setInvariantUUID(INVARIANT_UUIDS[index]);
			policyDefinition.setPolicyUUID(POLICY_UUIDS[index]);
			policyDefinition.setName(POLICY_NAMES[index]);
			policyDefinition.setVersion(VERSIONS[index]);
			
			// Set targets
			policyDefinition.setTargets(getTargers());			
			
			policies.put(POLICY_KEYS[index],policyDefinition);
		}		
		return policies;
	}

	private Map<PolicyTargetType, List<String>> getTargers() {
		Map<PolicyTargetType, List<String>> targets = new HashMap<>();
		targets.put(PolicyTargetType.COMPONENT_INSTANCES, Arrays.asList(POLICY_COMPONENT_INSTANCES));
		targets.put(PolicyTargetType.GROUPS, Arrays.asList(POLICY_GROUPS));
		return targets;
	}	
}