Policies import when import VSP
[sdc.git] / catalog-be / src / test / java / org / openecomp / sdc / be / components / impl / PolicyTypeBusinessLogicTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2017 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  * Modifications copyright (c) 2019 Nokia
20  * ================================================================================
21  */
22 package org.openecomp.sdc.be.components.impl;
23
24 import com.google.common.collect.ImmutableMap;
25 import fj.data.Either;
26 import org.junit.Before;
27 import org.junit.BeforeClass;
28 import org.junit.Test;
29 import org.junit.runner.RunWith;
30 import org.mockito.InjectMocks;
31 import org.mockito.Mock;
32 import org.mockito.junit.MockitoJUnitRunner;
33 import org.openecomp.sdc.be.DummyConfigurationManager;
34 import org.openecomp.sdc.be.components.impl.exceptions.ByResponseFormatComponentException;
35 import org.openecomp.sdc.be.components.utils.PolicyTypeBuilder;
36 import org.openecomp.sdc.be.components.validation.UserValidations;
37 import org.openecomp.sdc.be.config.ConfigurationManager;
38 import org.openecomp.sdc.be.dao.jsongraph.JanusGraphDao;
39 import org.openecomp.sdc.be.impl.ComponentsUtils;
40 import org.openecomp.sdc.be.model.PolicyTypeDefinition;
41 import org.openecomp.sdc.be.model.User;
42 import org.openecomp.sdc.be.model.operations.StorageException;
43 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
44 import org.openecomp.sdc.be.model.operations.impl.PolicyTypeOperation;
45 import org.openecomp.sdc.exception.ResponseFormat;
46
47 import java.util.Arrays;
48 import java.util.List;
49 import java.util.Set;
50
51 import static com.google.common.collect.Sets.newHashSet;
52 import static java.util.Collections.emptyList;
53 import static org.assertj.core.api.Assertions.assertThat;
54 import static org.mockito.ArgumentMatchers.anySet;
55 import static org.mockito.ArgumentMatchers.eq;
56 import static org.mockito.Mockito.when;
57
58 @RunWith(MockitoJUnitRunner.class)
59 public class PolicyTypeBusinessLogicTest {
60
61     private static final String USER_ID = "userId";
62     private static final Set<String> EXCLUDED_POLICY_TYPES = newHashSet("type1", "type2");
63     private static final String COMPONENT_TYPE = "VF";
64
65     @InjectMocks
66     private PolicyTypeBusinessLogic testInstance;
67     @Mock
68     private JanusGraphDao janusGraphDao;
69     @Mock
70     private PolicyTypeOperation policyTypeOperation;
71     @Mock
72     private ComponentsUtils componentsUtils;
73     @Mock
74     private UserValidations userValidations;
75
76     @Before
77     public void setUp() throws Exception {
78         when(userValidations.validateUserExists(eq(USER_ID))).thenReturn(new User());
79         when(ConfigurationManager.getConfigurationManager().getConfiguration().getExcludedPolicyTypesMapping()).thenReturn(ImmutableMap.of(COMPONENT_TYPE, EXCLUDED_POLICY_TYPES));
80     }
81
82     @BeforeClass
83     public static void beforeClass() {
84         new DummyConfigurationManager();
85     }
86
87     @Test
88     public void getAllPolicyTypes_userNotExist() {
89         ResponseFormat userNotExistResponse = new ResponseFormat();
90         when(userValidations.validateUserExists(eq(USER_ID))).thenThrow(new ByResponseFormatComponentException(userNotExistResponse));
91         try{
92             testInstance.getAllPolicyTypes(USER_ID, COMPONENT_TYPE);
93         }catch(ByResponseFormatComponentException e){
94             assertThat(e.getResponseFormat()).isSameAs(userNotExistResponse);
95         }
96     }
97
98     @Test
99     public void getAllPolicyTypes_whenExcludePolicyTypesSetIsNull_passNullExcludedTypesSet() {
100         when(ConfigurationManager.getConfigurationManager().getConfiguration().getExcludedPolicyTypesMapping()).thenCallRealMethod();
101         when(policyTypeOperation.getAllPolicyTypes(anySet())).thenReturn(emptyList());
102         List<PolicyTypeDefinition> allPolicyTypes = testInstance.getAllPolicyTypes(USER_ID, COMPONENT_TYPE);
103         assertThat(allPolicyTypes).isEmpty();
104     }
105
106     @Test
107     public void getAllPolicyTypes() {
108         List<PolicyTypeDefinition> policyTypes = Arrays.asList(new PolicyTypeBuilder().setUniqueId("id1").build(),
109                 new PolicyTypeBuilder().setUniqueId("id2").build(),
110                 new PolicyTypeBuilder().setUniqueId("id3").build());
111         when(policyTypeOperation.getAllPolicyTypes(EXCLUDED_POLICY_TYPES)).thenReturn(policyTypes);
112         List<PolicyTypeDefinition> allPolicyTypes = testInstance.getAllPolicyTypes(USER_ID, COMPONENT_TYPE);
113         assertThat(allPolicyTypes).isSameAs(policyTypes);
114     }
115
116     @Test
117     public void getAllPolicyTypes_noPolicyTypes() {
118         when(policyTypeOperation.getAllPolicyTypes(EXCLUDED_POLICY_TYPES)).thenThrow(new StorageException(StorageOperationStatus.NOT_FOUND));
119         try {
120             testInstance.getAllPolicyTypes(USER_ID, COMPONENT_TYPE);
121         }catch(StorageException e){
122             assertThat(e.getStorageOperationStatus()).isSameAs(StorageOperationStatus.NOT_FOUND);
123         }
124     }
125
126     @Test
127     public void getLatestPolicyTypeByTypeTest() {
128         PolicyTypeDefinition policyTypeDefinition = new PolicyTypeBuilder().setType("org.openecomp.policies.placement.Antilocate").build();
129         when(policyTypeOperation.getLatestPolicyTypeByType("org.openecomp.policies.placement.Antilocate")).thenReturn(
130                 Either.left(policyTypeDefinition));
131         assertThat(policyTypeOperation.getLatestPolicyTypeByType("org.openecomp.policies.placement.Antilocate"))
132                 .isEqualTo(Either.left(testInstance.getLatestPolicyTypeByType("org.openecomp.policies.placement.Antilocate")));
133     }
134 }