summaryrefslogtreecommitdiffstats
path: root/catalog-be/src/test/java/org/openecomp/sdc/be/components/impl/PolicyTypeBusinessLogicTest.java
blob: 88a16f6d9622eff03ac78ba3563491cb9b65b530 (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
/*-
 * ============LICENSE_START=======================================================
 * SDC
 * ================================================================================
 * Copyright (C) 2017 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=========================================================
 * Modifications copyright (c) 2019 Nokia
 * ================================================================================
 */
package org.openecomp.sdc.be.components.impl;

import static com.google.common.collect.Sets.newHashSet;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.when;

import com.google.common.collect.ImmutableMap;
import fj.data.Either;
import java.util.Arrays;
import java.util.List;
import java.util.Set;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import org.openecomp.sdc.be.DummyConfigurationManager;
import org.openecomp.sdc.be.components.impl.exceptions.ByResponseFormatComponentException;
import org.openecomp.sdc.be.components.utils.PolicyTypeBuilder;
import org.openecomp.sdc.be.components.validation.UserValidations;
import org.openecomp.sdc.be.config.ConfigurationManager;
import org.openecomp.sdc.be.dao.janusgraph.JanusGraphDao;
import org.openecomp.sdc.be.impl.ComponentsUtils;
import org.openecomp.sdc.be.model.PolicyTypeDefinition;
import org.openecomp.sdc.be.model.User;
import org.openecomp.sdc.be.model.operations.StorageException;
import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
import org.openecomp.sdc.be.model.operations.impl.PolicyTypeOperation;
import org.openecomp.sdc.exception.ResponseFormat;

@RunWith(MockitoJUnitRunner.class)
public class PolicyTypeBusinessLogicTest {

    private static final String USER_ID = "userId";
    private static final Set<String> EXCLUDED_POLICY_TYPES = newHashSet("type1", "type2");
    private static final String COMPONENT_TYPE = "VF";

    @InjectMocks
    private PolicyTypeBusinessLogic testInstance;
    @Mock
    private JanusGraphDao janusGraphDao;
    @Mock
    private PolicyTypeOperation policyTypeOperation;
    @Mock
    private ComponentsUtils componentsUtils;
    @Mock
    private UserValidations userValidations;

    @Before
    public void setUp() throws Exception {
        when(userValidations.validateUserExists(eq(USER_ID))).thenReturn(new User());
        when(ConfigurationManager.getConfigurationManager().getConfiguration().getExcludedPolicyTypesMapping()).thenReturn(ImmutableMap.of(COMPONENT_TYPE, EXCLUDED_POLICY_TYPES));
    }

    @BeforeClass
    public static void beforeClass() {
        new DummyConfigurationManager();
    }

    @Test
    public void getAllPolicyTypes_userNotExist() {
        ResponseFormat userNotExistResponse = new ResponseFormat();
        when(userValidations.validateUserExists(eq(USER_ID))).thenThrow(new ByResponseFormatComponentException(userNotExistResponse));
        try{
            testInstance.getAllPolicyTypes(USER_ID, COMPONENT_TYPE, null);
        }catch(ByResponseFormatComponentException e){
            assertThat(e.getResponseFormat()).isSameAs(userNotExistResponse);
        }
    }

    @Test
    public void getAllPolicyTypes_whenExcludePolicyTypesSetIsNull_passNullExcludedTypesSet() {
        when(ConfigurationManager.getConfigurationManager().getConfiguration().getExcludedPolicyTypesMapping()).thenCallRealMethod();
        List<PolicyTypeDefinition> allPolicyTypes = testInstance.getAllPolicyTypes(USER_ID, COMPONENT_TYPE, null);
        assertThat(allPolicyTypes).isEmpty();
    }

    @Test
    public void getAllPolicyTypes() {
        List<PolicyTypeDefinition> policyTypes = Arrays.asList(new PolicyTypeBuilder().setUniqueId("id1").build(),
                new PolicyTypeBuilder().setUniqueId("id2").build(),
                new PolicyTypeBuilder().setUniqueId("id3").build());
        when(policyTypeOperation.getAllPolicyTypes(EXCLUDED_POLICY_TYPES, null)).thenReturn(policyTypes);
        List<PolicyTypeDefinition> allPolicyTypes = testInstance.getAllPolicyTypes(USER_ID, COMPONENT_TYPE, null);
        assertThat(allPolicyTypes).isSameAs(policyTypes);
    }

    @Test
    public void getAllPolicyTypes_noPolicyTypes() {
        when(policyTypeOperation.getAllPolicyTypes(EXCLUDED_POLICY_TYPES, null)).thenThrow(new StorageException(StorageOperationStatus.NOT_FOUND));
        try {
            testInstance.getAllPolicyTypes(USER_ID, COMPONENT_TYPE, null);
        }catch(StorageException e){
            assertThat(e.getStorageOperationStatus()).isSameAs(StorageOperationStatus.NOT_FOUND);
        }
    }

    @Test
    public void getLatestPolicyTypeByTypeTest() {
        PolicyTypeDefinition policyTypeDefinition = new PolicyTypeBuilder().setType("org.openecomp.policies.placement.Antilocate").build();
        when(policyTypeOperation.getLatestPolicyTypeByType("org.openecomp.policies.placement.Antilocate", null)).thenReturn(
                Either.left(policyTypeDefinition));
        assertThat(policyTypeOperation.getLatestPolicyTypeByType("org.openecomp.policies.placement.Antilocate", null))
                .isEqualTo(Either.left(testInstance.getLatestPolicyTypeByType("org.openecomp.policies.placement.Antilocate", null)));
    }
}