Upgrade SDC from Titan to Janus Graph
[sdc.git] / catalog-be / src / test / java / org / openecomp / sdc / be / components / impl / PolicyPropertiesBusinessLogicTest.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 static org.assertj.core.api.Assertions.assertThat;
25 import static org.mockito.ArgumentMatchers.anyString;
26 import static org.mockito.ArgumentMatchers.eq;
27 import static org.mockito.Mockito.verify;
28 import static org.mockito.Mockito.when;
29
30 import fj.data.Either;
31 import java.util.List;
32 import javax.ws.rs.core.Response;
33 import org.junit.After;
34 import org.junit.Before;
35 import org.junit.Test;
36 import org.junit.runner.RunWith;
37 import org.mockito.ArgumentCaptor;
38 import org.mockito.InjectMocks;
39 import org.mockito.Mock;
40 import org.mockito.junit.MockitoJUnitRunner;
41 import org.openecomp.sdc.be.components.impl.exceptions.ByActionStatusComponentException;
42 import org.openecomp.sdc.be.components.impl.exceptions.ComponentException;
43 import org.openecomp.sdc.be.components.utils.PolicyDefinitionBuilder;
44 import org.openecomp.sdc.be.components.utils.PropertyDataDefinitionBuilder;
45 import org.openecomp.sdc.be.components.utils.ResourceBuilder;
46 import org.openecomp.sdc.be.components.validation.UserValidations;
47 import org.openecomp.sdc.be.dao.api.ActionStatus;
48 import org.openecomp.sdc.be.dao.jsongraph.JanusGraphDao;
49 import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition;
50 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
51 import org.openecomp.sdc.be.impl.ComponentsUtils;
52 import org.openecomp.sdc.be.model.ComponentParametersView;
53 import org.openecomp.sdc.be.model.LifecycleStateEnum;
54 import org.openecomp.sdc.be.model.PolicyDefinition;
55 import org.openecomp.sdc.be.model.PropertyDefinition;
56 import org.openecomp.sdc.be.model.Resource;
57 import org.openecomp.sdc.be.model.User;
58 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ToscaOperationFacade;
59 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
60 import org.openecomp.sdc.exception.ResponseFormat;
61
62 @RunWith(MockitoJUnitRunner.class)
63 public class PolicyPropertiesBusinessLogicTest {
64
65     private static final String POLICY_ID = "policy1";
66     private static final String RESOURCE_ID = "resourceId";
67     private static final String USER_ID = "userId";
68     public static final String NO_PROPS_POLICY = "policy2";
69     @InjectMocks
70     private PolicyBusinessLogic testInstance;
71
72     @Mock
73     private UserValidations userValidations;
74
75     @Mock
76     private JanusGraphDao janusGraphDao;
77
78     @Mock
79     private ToscaOperationFacade toscaOperationFacade;
80
81     @Mock
82     private ComponentsUtils componentsUtils;
83
84     private final ComponentTypeEnum COMPONENT_TYPE = ComponentTypeEnum.RESOURCE;
85
86     private ComponentParametersView componentFilter;
87     private Resource resource;
88     private PropertyDefinition prop1, prop2;
89
90     @Before
91     public void setUp() throws Exception {
92         testInstance.setUserValidations(userValidations);
93         testInstance.setJanusGraphGenericDao(janusGraphDao);
94         testInstance.setToscaOperationFacade(toscaOperationFacade);
95         testInstance.setComponentsUtils(componentsUtils);
96
97         componentFilter = new ComponentParametersView(true);
98         componentFilter.setIgnorePolicies(false);
99         componentFilter.setIgnoreUsers(false);
100
101         prop1 = new PropertyDataDefinitionBuilder().setUniqueId("prop1").build();
102         prop2 = new PropertyDataDefinitionBuilder().setUniqueId("prop1").build();
103
104         PolicyDefinition policy1 = PolicyDefinitionBuilder.create()
105                 .setUniqueId(POLICY_ID)
106                 .setProperties(prop1, prop2)
107                 .build();
108
109         PolicyDefinition policy2 = PolicyDefinitionBuilder.create()
110                 .setUniqueId(NO_PROPS_POLICY)
111                 .build();
112         resource = new ResourceBuilder()
113                 .setUniqueId(RESOURCE_ID)
114                 .setComponentType(COMPONENT_TYPE)
115                 .setLifeCycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT)
116                 .setLastUpdaterUserId(USER_ID)
117                 .addPolicy(policy1)
118                 .addPolicy(policy2)
119                 .build();
120     }
121
122     @After
123     public void tearDown() {
124         verify(janusGraphDao).commit();
125     }
126
127     @Test
128     public void getPolicyProperties_userIdIsNull() {
129         String userId = null;
130         ComponentException forbiddenException = new ByActionStatusComponentException(ActionStatus.AUTH_FAILED);
131         when(userValidations.validateUserExists(eq(userId), anyString(), eq(false))).thenThrow(forbiddenException);
132         try{
133             testInstance.getPolicyProperties(ComponentTypeEnum.RESOURCE, RESOURCE_ID, POLICY_ID, null);
134         } catch(ByActionStatusComponentException e){
135             assertThat(e.getActionStatus()).isEqualTo(ActionStatus.AUTH_FAILED);
136         }
137     }
138
139     @Test
140     public void getPolicyProperties_componentNotFound() {
141         when(userValidations.validateUserExists(eq(USER_ID), anyString(), eq(false))).thenReturn(new User());
142         ArgumentCaptor<ComponentParametersView> filterCaptor = ArgumentCaptor.forClass(ComponentParametersView.class);
143         when(toscaOperationFacade.getToscaElement(eq(RESOURCE_ID), filterCaptor.capture())).thenReturn(Either.right(StorageOperationStatus.NOT_FOUND));
144         when(componentsUtils.convertFromStorageResponse(StorageOperationStatus.NOT_FOUND, ComponentTypeEnum.RESOURCE)).thenCallRealMethod();
145         ResponseFormat notFoundResponse = new ResponseFormat(Response.Status.NOT_FOUND.getStatusCode());
146         when(componentsUtils.getResponseFormat(eq(ActionStatus.RESOURCE_NOT_FOUND), anyString())).thenReturn(notFoundResponse);
147         Either<List<PropertyDataDefinition>, ResponseFormat> policyProperties = testInstance.getPolicyProperties(ComponentTypeEnum.RESOURCE, RESOURCE_ID, POLICY_ID, USER_ID);
148         assertThat(policyProperties.right().value()).isSameAs(notFoundResponse);
149     }
150
151     @Test
152     public void getPolicyProperties_policyNotExist() {
153         doPolicyValidations();
154         ResponseFormat notFoundResponse = new ResponseFormat(Response.Status.NOT_FOUND.getStatusCode());
155         when(componentsUtils.getResponseFormat(ActionStatus.POLICY_NOT_FOUND_ON_CONTAINER, "nonExistingPolicy", RESOURCE_ID)).thenReturn(notFoundResponse);
156         Either<List<PropertyDataDefinition>, ResponseFormat> policyProperties = testInstance.getPolicyProperties(ComponentTypeEnum.RESOURCE, RESOURCE_ID, "nonExistingPolicy", USER_ID);
157         assertThat(policyProperties.right().value()).isEqualTo(notFoundResponse);
158     }
159
160     @Test
161     public void getPolicyProperties_noPropertiesOnPolicy() {
162         doPolicyValidations();
163         Either<List<PropertyDataDefinition>, ResponseFormat> policyProperties = testInstance.getPolicyProperties(ComponentTypeEnum.RESOURCE, RESOURCE_ID, NO_PROPS_POLICY, USER_ID);
164         assertThat(policyProperties.left().value()).isNull();
165     }
166
167     @Test
168     public void getPolicyProperties() {
169         doPolicyValidations();
170         Either<List<PropertyDataDefinition>, ResponseFormat> policyProperties = testInstance.getPolicyProperties(ComponentTypeEnum.RESOURCE, RESOURCE_ID, POLICY_ID, USER_ID);
171         assertThat(policyProperties.left().value())
172                 .usingElementComparatorOnFields("uniqueId")
173                 .containsExactly(prop1, prop2);
174     }
175
176     private void doPolicyValidations() {
177         when(userValidations.validateUserExists(eq(USER_ID), anyString(), eq(false))).thenReturn(new User());
178         ArgumentCaptor<ComponentParametersView> filterCaptor = ArgumentCaptor.forClass(ComponentParametersView.class);
179         when(toscaOperationFacade.getToscaElement(eq(RESOURCE_ID), filterCaptor.capture())).thenReturn(Either.left(resource));
180     }
181 }