Upgrade SDC from Titan to Janus Graph
[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.*;
54 import static org.mockito.Mockito.when;
55
56 @RunWith(MockitoJUnitRunner.class)
57 public class PolicyTypeBusinessLogicTest {
58
59     private static final String USER_ID = "userId";
60     private static final Set<String> EXCLUDED_POLICY_TYPES = newHashSet("type1", "type2");
61     private static final String COMPONENT_TYPE = "VF";
62
63     @InjectMocks
64     private PolicyTypeBusinessLogic testInstance;
65     @Mock
66     private JanusGraphDao janusGraphDao;
67     @Mock
68     private PolicyTypeOperation policyTypeOperation;
69     @Mock
70     private ComponentsUtils componentsUtils;
71     @Mock
72     private UserValidations userValidations;
73
74     @Before
75     public void setUp() throws Exception {
76         when(userValidations.validateUserExists(eq(USER_ID), anyString(), eq(true))).thenReturn(new User());
77         when(ConfigurationManager.getConfigurationManager().getConfiguration().getExcludedPolicyTypesMapping()).thenReturn(ImmutableMap.of(COMPONENT_TYPE, EXCLUDED_POLICY_TYPES));
78     }
79
80     @BeforeClass
81     public static void beforeClass() {
82         new DummyConfigurationManager();
83     }
84
85     @Test
86     public void getAllPolicyTypes_userNotExist() {
87         ResponseFormat userNotExistResponse = new ResponseFormat();
88         when(userValidations.validateUserExists(eq(USER_ID), anyString(), eq(true))).thenThrow(new ByResponseFormatComponentException(userNotExistResponse));
89         try{
90             testInstance.getAllPolicyTypes(USER_ID, COMPONENT_TYPE);
91         }catch(ByResponseFormatComponentException e){
92             assertThat(e.getResponseFormat()).isSameAs(userNotExistResponse);
93         }
94     }
95
96     @Test
97     public void getAllPolicyTypes_whenExcludePolicyTypesSetIsNull_passNullExcludedTypesSet() {
98         when(ConfigurationManager.getConfigurationManager().getConfiguration().getExcludedPolicyTypesMapping()).thenCallRealMethod();
99         when(policyTypeOperation.getAllPolicyTypes(anySet())).thenReturn(emptyList());
100         List<PolicyTypeDefinition> allPolicyTypes = testInstance.getAllPolicyTypes(USER_ID, COMPONENT_TYPE);
101         assertThat(allPolicyTypes).isEmpty();
102     }
103
104     @Test
105     public void getAllPolicyTypes() {
106         List<PolicyTypeDefinition> policyTypes = Arrays.asList(new PolicyTypeBuilder().setUniqueId("id1").build(),
107                 new PolicyTypeBuilder().setUniqueId("id2").build(),
108                 new PolicyTypeBuilder().setUniqueId("id3").build());
109         when(policyTypeOperation.getAllPolicyTypes(EXCLUDED_POLICY_TYPES)).thenReturn(policyTypes);
110         List<PolicyTypeDefinition> allPolicyTypes = testInstance.getAllPolicyTypes(USER_ID, COMPONENT_TYPE);
111         assertThat(allPolicyTypes).isSameAs(policyTypes);
112     }
113
114     @Test
115     public void getAllPolicyTypes_noPolicyTypes() {
116         when(policyTypeOperation.getAllPolicyTypes(EXCLUDED_POLICY_TYPES)).thenThrow(new StorageException(StorageOperationStatus.NOT_FOUND));
117         try {
118             testInstance.getAllPolicyTypes(USER_ID, COMPONENT_TYPE);
119         }catch(StorageException e){
120             assertThat(e.getStorageOperationStatus()).isSameAs(StorageOperationStatus.NOT_FOUND);
121         }
122     }
123
124 }