/*- * ============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.*; 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 Component component; @Test public void failToGetAllDataTypes() { when(dataTypeCache.getAll()).thenReturn(Either.right(null)); assertThatExceptionOfType(SdcResourceNotFoundException.class).isThrownBy(() -> policiyExportParser = new PolicyExportParserImpl(dataTypeCache)); } @Test public void noPoliciesInComponent() { when(dataTypeCache.getAll()).thenReturn(Either.left(null)); when(component.getPolicies()).thenReturn(null); policiyExportParser = new PolicyExportParserImpl(dataTypeCache); Map policies = policiyExportParser.getPolicies(component); assertThat(policies).isEqualTo(null); } @Test public void onePoliciesInComponent() { List constIndexes = Arrays.asList(new Integer[] {0}); testPoliciesInComponent(constIndexes); } @Test public void twoPoliciesInComponent() { List constIndexes = Arrays.asList(new Integer[] {0,1}); testPoliciesInComponent(constIndexes); } private void testPoliciesInComponent(List constIndexes) { when(dataTypeCache.getAll()).thenReturn(Either.left(null)); Map policiesToAdd = getPolicies(constIndexes); when(component.getPolicies()).thenReturn(policiesToAdd); when(component.getComponentInstances()).thenReturn(getComponentInstances()); when(component.getGroups()).thenReturn(getGroups()); policiyExportParser = new PolicyExportParserImpl(dataTypeCache); Map 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 targets = toscaPolicyTemplate.getTargets(); assertThat(targets.get(0)).isEqualTo(POLICY_COMPONENT_INSTANCES_NAME); assertThat(targets.get(1)).isEqualTo(POLICY_GROUP_NAME); } } private List getGroups() { List groups = new ArrayList<>(); GroupDefinition groupDefinition = new GroupDefinition(); groupDefinition.setUniqueId(POLICY_GROUPS[0]); groupDefinition.setName(POLICY_GROUP_NAME); groups.add(groupDefinition); return groups; } private List getComponentInstances() { List 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 getPolicies(List indexes) { Map 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> getTargers() { Map> targets = new HashMap<>(); targets.put(PolicyTargetType.COMPONENT_INSTANCES, Arrays.asList(POLICY_COMPONENT_INSTANCES)); targets.put(PolicyTargetType.GROUPS, Arrays.asList(POLICY_GROUPS)); return targets; } }