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