aboutsummaryrefslogtreecommitdiffstats
path: root/catalog-be/src/test/java/org/openecomp/sdc/be/components/impl/PolicyPropertiesBusinessLogicTest.java
blob: 3b9a43af28cc502c84f6db0bb0026374780549af (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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/*-
 * ============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 org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import fj.data.Either;
import java.util.List;
import javax.ws.rs.core.Response;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import org.openecomp.sdc.be.components.impl.exceptions.ByActionStatusComponentException;
import org.openecomp.sdc.be.components.impl.exceptions.ComponentException;
import org.openecomp.sdc.be.components.utils.PolicyDefinitionBuilder;
import org.openecomp.sdc.be.components.utils.PropertyDataDefinitionBuilder;
import org.openecomp.sdc.be.components.utils.ResourceBuilder;
import org.openecomp.sdc.be.components.validation.UserValidations;
import org.openecomp.sdc.be.dao.api.ActionStatus;
import org.openecomp.sdc.be.dao.jsongraph.JanusGraphDao;
import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition;
import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
import org.openecomp.sdc.be.impl.ComponentsUtils;
import org.openecomp.sdc.be.model.ComponentParametersView;
import org.openecomp.sdc.be.model.LifecycleStateEnum;
import org.openecomp.sdc.be.model.PolicyDefinition;
import org.openecomp.sdc.be.model.PropertyDefinition;
import org.openecomp.sdc.be.model.Resource;
import org.openecomp.sdc.be.model.User;
import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ToscaOperationFacade;
import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
import org.openecomp.sdc.exception.ResponseFormat;

@RunWith(MockitoJUnitRunner.class)
public class PolicyPropertiesBusinessLogicTest {

    private static final String POLICY_ID = "policy1";
    private static final String RESOURCE_ID = "resourceId";
    private static final String USER_ID = "userId";
    public static final String NO_PROPS_POLICY = "policy2";
    @InjectMocks
    private PolicyBusinessLogic testInstance;

    @Mock
    private UserValidations userValidations;

    @Mock
    private JanusGraphDao janusGraphDao;

    @Mock
    private ToscaOperationFacade toscaOperationFacade;

    @Mock
    private ComponentsUtils componentsUtils;

    private final ComponentTypeEnum COMPONENT_TYPE = ComponentTypeEnum.RESOURCE;

    private ComponentParametersView componentFilter;
    private Resource resource;
    private PropertyDefinition prop1, prop2;

    @Before
    public void setUp() throws Exception {
        testInstance.setUserValidations(userValidations);
        testInstance.setJanusGraphGenericDao(janusGraphDao);
        testInstance.setToscaOperationFacade(toscaOperationFacade);
        testInstance.setComponentsUtils(componentsUtils);

        componentFilter = new ComponentParametersView(true);
        componentFilter.setIgnorePolicies(false);
        componentFilter.setIgnoreUsers(false);

        prop1 = new PropertyDataDefinitionBuilder().setUniqueId("prop1").build();
        prop2 = new PropertyDataDefinitionBuilder().setUniqueId("prop1").build();

        PolicyDefinition policy1 = PolicyDefinitionBuilder.create()
                .setUniqueId(POLICY_ID)
                .setProperties(prop1, prop2)
                .build();

        PolicyDefinition policy2 = PolicyDefinitionBuilder.create()
                .setUniqueId(NO_PROPS_POLICY)
                .build();
        resource = new ResourceBuilder()
                .setUniqueId(RESOURCE_ID)
                .setComponentType(COMPONENT_TYPE)
                .setLifeCycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT)
                .setLastUpdaterUserId(USER_ID)
                .addPolicy(policy1)
                .addPolicy(policy2)
                .build();
    }

    @After
    public void tearDown() {
        verify(janusGraphDao).commit();
    }

    @Test
    public void getPolicyProperties_userIdIsNull() {
        String userId = null;
        ComponentException forbiddenException = new ByActionStatusComponentException(ActionStatus.AUTH_FAILED);
        when(userValidations.validateUserExists(eq(userId), anyString(), eq(false))).thenThrow(forbiddenException);
        try{
            testInstance.getPolicyProperties(ComponentTypeEnum.RESOURCE, RESOURCE_ID, POLICY_ID, null);
        } catch(ByActionStatusComponentException e){
            assertThat(e.getActionStatus()).isEqualTo(ActionStatus.AUTH_FAILED);
        }
    }

    @Test
    public void getPolicyProperties_componentNotFound() {
        when(userValidations.validateUserExists(eq(USER_ID), anyString(), eq(false))).thenReturn(new User());
        ArgumentCaptor<ComponentParametersView> filterCaptor = ArgumentCaptor.forClass(ComponentParametersView.class);
        when(toscaOperationFacade.getToscaElement(eq(RESOURCE_ID), filterCaptor.capture())).thenReturn(Either.right(StorageOperationStatus.NOT_FOUND));
        when(componentsUtils.convertFromStorageResponse(StorageOperationStatus.NOT_FOUND, ComponentTypeEnum.RESOURCE)).thenCallRealMethod();
        ResponseFormat notFoundResponse = new ResponseFormat(Response.Status.NOT_FOUND.getStatusCode());
        when(componentsUtils.getResponseFormat(eq(ActionStatus.RESOURCE_NOT_FOUND), anyString())).thenReturn(notFoundResponse);
        Either<List<PropertyDataDefinition>, ResponseFormat> policyProperties = testInstance.getPolicyProperties(ComponentTypeEnum.RESOURCE, RESOURCE_ID, POLICY_ID, USER_ID);
        assertThat(policyProperties.right().value()).isSameAs(notFoundResponse);
    }

    @Test
    public void getPolicyProperties_policyNotExist() {
        doPolicyValidations();
        ResponseFormat notFoundResponse = new ResponseFormat(Response.Status.NOT_FOUND.getStatusCode());
        when(componentsUtils.getResponseFormat(ActionStatus.POLICY_NOT_FOUND_ON_CONTAINER, "nonExistingPolicy", RESOURCE_ID)).thenReturn(notFoundResponse);
        Either<List<PropertyDataDefinition>, ResponseFormat> policyProperties = testInstance.getPolicyProperties(ComponentTypeEnum.RESOURCE, RESOURCE_ID, "nonExistingPolicy", USER_ID);
        assertThat(policyProperties.right().value()).isEqualTo(notFoundResponse);
    }

    @Test
    public void getPolicyProperties_noPropertiesOnPolicy() {
        doPolicyValidations();
        Either<List<PropertyDataDefinition>, ResponseFormat> policyProperties = testInstance.getPolicyProperties(ComponentTypeEnum.RESOURCE, RESOURCE_ID, NO_PROPS_POLICY, USER_ID);
        assertThat(policyProperties.left().value()).isNull();
    }

    @Test
    public void getPolicyProperties() {
        doPolicyValidations();
        Either<List<PropertyDataDefinition>, ResponseFormat> policyProperties = testInstance.getPolicyProperties(ComponentTypeEnum.RESOURCE, RESOURCE_ID, POLICY_ID, USER_ID);
        assertThat(policyProperties.left().value())
                .usingElementComparatorOnFields("uniqueId")
                .containsExactly(prop1, prop2);
    }

    private void doPolicyValidations() {
        when(userValidations.validateUserExists(eq(USER_ID), anyString(), eq(false))).thenReturn(new User());
        ArgumentCaptor<ComponentParametersView> filterCaptor = ArgumentCaptor.forClass(ComponentParametersView.class);
        when(toscaOperationFacade.getToscaElement(eq(RESOURCE_ID), filterCaptor.capture())).thenReturn(Either.left(resource));
    }
}