Fix locally failing TCs in catalog-be
[sdc.git] / catalog-be / src / test / java / org / openecomp / sdc / be / components / impl / ConsumerBusinessLogicTest.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.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertTrue;
26 import static org.mockito.ArgumentMatchers.any;
27 import static org.mockito.ArgumentMatchers.anyString;
28
29 import fj.data.Either;
30 import java.util.HashMap;
31 import java.util.Map;
32 import org.apache.commons.lang3.RandomStringUtils;
33 import org.junit.jupiter.api.BeforeEach;
34 import org.junit.jupiter.api.Test;
35 import org.mockito.InjectMocks;
36 import org.mockito.Mock;
37 import org.mockito.Mockito;
38 import org.mockito.MockitoAnnotations;
39 import org.openecomp.sdc.be.components.impl.exceptions.ByActionStatusComponentException;
40 import org.openecomp.sdc.be.dao.api.ActionStatus;
41 import org.openecomp.sdc.be.datatypes.elements.ConsumerDataDefinition;
42 import org.openecomp.sdc.be.datatypes.enums.NodeTypeEnum;
43 import org.openecomp.sdc.be.impl.ComponentsUtils;
44 import org.openecomp.sdc.be.model.ConsumerDefinition;
45 import org.openecomp.sdc.be.model.User;
46 import org.openecomp.sdc.be.model.operations.api.IGraphLockOperation;
47 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
48 import org.openecomp.sdc.be.model.operations.impl.ConsumerOperation;
49 import org.openecomp.sdc.be.resources.data.ConsumerData;
50 import org.openecomp.sdc.be.user.UserBusinessLogic;
51 import org.openecomp.sdc.exception.ResponseFormat;
52
53 class ConsumerBusinessLogicTest extends BaseBusinessLogicMock {
54
55     private User user;
56     private ConsumerDefinition consumer;
57     private ConsumerDataDefinition consumerDataDefinition;
58
59     @InjectMocks
60     private ConsumerBusinessLogic consumerBusinessLogic;
61
62     @Mock
63     private ComponentsUtils componentsUtils;
64
65     @Mock
66     private UserBusinessLogic UserBusinessLogic;
67
68     @Mock
69     private IGraphLockOperation iGraphLockOperation;
70
71     @Mock
72     private ConsumerOperation consumerOperation;
73
74     @Mock
75     private ConsumerData consumerData;
76
77     @BeforeEach
78     public void setUp() {
79         consumerBusinessLogic = new ConsumerBusinessLogic(elementDao, groupOperation, groupInstanceOperation, groupTypeOperation,
80             interfaceOperation, interfaceLifecycleTypeOperation, artifactToscaOperation);
81         consumerDataDefinition = new ConsumerDataDefinition();
82         consumer = new ConsumerDefinition();
83         MockitoAnnotations.initMocks(this);
84         user = new User("Stan", "Lee", "stan.lee",
85             "stan.lee@marvel.com", "ADMIN", 1542024000L);
86     }
87
88     @Test
89     void testCreateConsumer_givenMissingUser_thenReturnsError() {
90         User user = new User();
91         ConsumerDefinition consumerDefinition = new ConsumerDefinition();
92         Mockito.when(componentsUtils.getResponseFormat(ActionStatus.MISSING_INFORMATION))
93             .thenReturn(new ResponseFormat());
94         assertTrue(consumerBusinessLogic.createConsumer(user, consumerDefinition).isRight());
95     }
96
97     @Test
98     void testCreateConsumer_givenNonListedUser_thenReturnsError() {
99         ConsumerDefinition consumerDefinition = new ConsumerDefinition();
100         Mockito.when(componentsUtils.getResponseFormat(ActionStatus.RESTRICTED_ACCESS))
101             .thenReturn(new ResponseFormat());
102         Mockito.when(UserBusinessLogic.getUser(user.getUserId(), false))
103             .thenThrow(new ByActionStatusComponentException(ActionStatus.RESTRICTED_OPERATION));
104         assertTrue(consumerBusinessLogic.createConsumer(user, consumerDefinition).isRight());
105     }
106
107     @Test
108     void testCreateConsumer_givenNonAdminUser_thenReturnsError() {
109         user.setRole("DESIGNER");
110         Mockito.when(componentsUtils.getResponseFormat(ActionStatus.RESTRICTED_OPERATION))
111             .thenReturn(new ResponseFormat());
112         Mockito.when(UserBusinessLogic.getUser(user.getUserId(), false)).thenReturn(user);
113         assertTrue(consumerBusinessLogic.createConsumer(user, consumer).isRight());
114     }
115
116     @Test
117     void testCreateConsumer_givenInvalidConsumerNames_thenReturnsError() {
118         Map<String, ActionStatus> invalidConsumerNames = new HashMap<>();
119         invalidConsumerNames.put(null, ActionStatus.MISSING_DATA);
120         invalidConsumerNames.put(".#()", ActionStatus.INVALID_CONTENT);
121         invalidConsumerNames.put(RandomStringUtils.random(256, true, false), ActionStatus.EXCEEDS_LIMIT);
122         for (Map.Entry<String, ActionStatus> e : invalidConsumerNames.entrySet()) {
123             consumer.setConsumerName(e.getKey());
124             Mockito.when(UserBusinessLogic.getUser(user.getUserId(), false)).thenReturn(user);
125             Mockito.when(componentsUtils.getResponseFormat(e.getValue(), "Consumer name"))
126                 .thenReturn(new ResponseFormat());
127             assertTrue(consumerBusinessLogic.createConsumer(user, consumer).isRight());
128         }
129     }
130
131     @Test
132     void testCreateConsumer_givenInvalidConsumerPasswords_thenReturnsError() {
133         Map<String, ActionStatus> invalidPasswordResults = new HashMap<>();
134         invalidPasswordResults.put(null, ActionStatus.MISSING_DATA);
135         invalidPasswordResults.put(RandomStringUtils.random(64, '*'), ActionStatus.INVALID_CONTENT_PARAM);
136         invalidPasswordResults.put("password", ActionStatus.INVALID_LENGTH);
137         for (Map.Entry<String, ActionStatus> e : invalidPasswordResults.entrySet()) {
138             consumer.setConsumerName("_marvel");
139             consumer.setConsumerPassword(e.getKey());
140             Mockito.when(UserBusinessLogic.getUser(user.getUserId(), false)).thenReturn(user);
141             Mockito.when(componentsUtils.getResponseFormat(e.getValue(), "Consumer password"))
142                 .thenReturn(new ResponseFormat());
143             assertTrue(consumerBusinessLogic.createConsumer(user, consumer).isRight());
144         }
145     }
146
147     @Test
148     void testCreateConsumer_givenInvalidConsumerSalts_thenReturnsError() {
149         consumer.setConsumerPassword(RandomStringUtils.random(64, true, true));
150         Map<String, ActionStatus> invalidPasswordSalts = new HashMap<>();
151         invalidPasswordSalts.put(null, ActionStatus.MISSING_DATA);
152         invalidPasswordSalts.put(RandomStringUtils.random(32, "*"), ActionStatus.INVALID_CONTENT_PARAM);
153         invalidPasswordSalts.put("password", ActionStatus.INVALID_LENGTH);
154         for (Map.Entry<String, ActionStatus> e : invalidPasswordSalts.entrySet()) {
155             consumer.setConsumerName("_marvel");
156             consumer.setConsumerSalt(e.getKey());
157             Mockito.when(UserBusinessLogic.getUser(user.getUserId(), false)).thenReturn(user);
158             Mockito.when(componentsUtils.getResponseFormat(e.getValue(), "Consumer salt"))
159                 .thenReturn(new ResponseFormat());
160             assertTrue(consumerBusinessLogic.createConsumer(user, consumer).isRight());
161         }
162     }
163
164     @Test
165     void testCreateConsumer_givenConsumerNotLocked_thenReturnsError() {
166         consumer.setConsumerName("_marvel");
167         consumer.setConsumerPassword(RandomStringUtils.random(64, true, true));
168         consumer.setConsumerSalt(RandomStringUtils.random(32, 'a'));
169         Mockito.when(UserBusinessLogic.getUser(user.getUserId(), false)).thenReturn(user);
170         Mockito.when(iGraphLockOperation.lockComponent(anyString(), any(NodeTypeEnum.class)))
171             .thenReturn(StorageOperationStatus.GENERAL_ERROR);
172         assertTrue(consumerBusinessLogic.createConsumer(user, consumer).isRight());
173     }
174
175     @Test
176     void testCreateConsumer_givenUnableToCreateCredentials_thenReturnsError() {
177         ConsumerDataDefinition consumerDataDefinition = new ConsumerDataDefinition();
178         consumerDataDefinition.setConsumerName("_marvel");
179         consumerDataDefinition.setConsumerPassword(RandomStringUtils.random(64, true, true));
180         consumerDataDefinition.setConsumerSalt(RandomStringUtils.random(32, 'a'));
181         ConsumerDefinition consumer = new ConsumerDefinition(consumerDataDefinition);
182         Mockito.when(UserBusinessLogic.getUser(user.getUserId(), false)).thenReturn(user);
183         Mockito.when(iGraphLockOperation.lockComponent(anyString(), any(NodeTypeEnum.class)))
184             .thenReturn(StorageOperationStatus.OK);
185         Mockito.when(consumerOperation.getCredentials(anyString()))
186             .thenReturn(Either.right(StorageOperationStatus.OK));
187         Mockito.when(consumerOperation.createCredentials(any(ConsumerData.class)))
188             .thenReturn(Either.right(StorageOperationStatus.USER_NOT_FOUND));
189         assertTrue(consumerBusinessLogic.createConsumer(user, consumer).isRight());
190     }
191
192     @Test
193     void testCreateConsumer_givenValidUserAndConsumer_thenReturnsConsumer() {
194         ConsumerDataDefinition consumerDataDefinition = new ConsumerDataDefinition();
195         consumerDataDefinition.setConsumerName("_marvel");
196         consumerDataDefinition.setConsumerPassword(RandomStringUtils.random(64, true, true));
197         consumerDataDefinition.setConsumerSalt(RandomStringUtils.random(32, 'a'));
198         ConsumerDefinition consumer = new ConsumerDefinition(consumerDataDefinition);
199         Mockito.when(UserBusinessLogic.getUser(user.getUserId(), false)).thenReturn(user);
200         Mockito.when(iGraphLockOperation.lockComponent(anyString(), any(NodeTypeEnum.class)))
201             .thenReturn(StorageOperationStatus.OK);
202         Mockito.when(consumerOperation.getCredentials(anyString()))
203             .thenReturn(Either.right(StorageOperationStatus.OK));
204         Mockito.when(consumerOperation.createCredentials(any(ConsumerData.class)))
205             .thenReturn(Either.left(new ConsumerData(consumerDataDefinition)));
206         assertTrue(consumerBusinessLogic.createConsumer(user, consumer).isLeft());
207     }
208
209     @Test
210     void testGetConsumer_givenNullUser_thenReturnsError() {
211         Mockito.when(consumerOperation.getCredentials("marvel123"))
212             .thenReturn(Either.right(StorageOperationStatus.USER_NOT_FOUND));
213         assertTrue(consumerBusinessLogic.getConsumer("marvel123", null).isRight());
214     }
215
216     @Test
217     void testGetConsumer_givenValidUserAndConsumerId_thenReturnsConsumer() {
218         Mockito.when(consumerOperation.getCredentials("marvel123"))
219             .thenReturn(Either.left(new ConsumerData()));
220         Mockito.when(consumerData.getConsumerDataDefinition())
221             .thenReturn(consumerDataDefinition);
222         Mockito.when(UserBusinessLogic.getUser(user.getUserId(), false)).thenReturn(user);
223         assertTrue(consumerBusinessLogic.getConsumer("marvel123", user).isLeft());
224     }
225
226     @Test
227     void testUpdateConsumer_givenValidConsumer_thenReturnsUpdatedConsumer() {
228         ConsumerDefinition updatedConsumer = new ConsumerDefinition(consumerDataDefinition);
229         updatedConsumer.setConsumerName("marvel2");
230         ConsumerDataDefinition updatedConsumerDataDef = new ConsumerDataDefinition();
231         updatedConsumerDataDef.setConsumerName("marvel2");
232         ConsumerData consumerData = new ConsumerData(updatedConsumerDataDef);
233         Mockito.when(consumerOperation.updateCredentials(any(ConsumerData.class)))
234             .thenReturn(Either.left(consumerData));
235         assertEquals(updatedConsumer.getConsumerName(), consumerBusinessLogic.updateConsumer(consumer).left().value().getConsumerName());
236     }
237
238     @Test
239     void testUpdateConsumer_givenUpdateFailure_thenReturnsError() {
240         Mockito.when(consumerOperation.updateCredentials(any(ConsumerData.class)))
241             .thenReturn(Either.right(StorageOperationStatus.GENERAL_ERROR));
242         assertTrue(consumerBusinessLogic.updateConsumer(consumer).isRight());
243     }
244
245     @Test
246     void testDeleteConsumer_givenValidUserAndConsumerId_thenReturnsSuccessful() {
247         ConsumerData consumerData = new ConsumerData(new ConsumerDataDefinition());
248         Mockito.when(UserBusinessLogic.getUser(user.getUserId(), false)).thenReturn(user);
249         Mockito.when(consumerOperation.deleteCredentials("marvel123"))
250             .thenReturn(Either.left(consumerData));
251         assertTrue(consumerBusinessLogic.deleteConsumer("marvel123", user).isLeft());
252     }
253
254     @Test
255     void testDeleteConsumer_givenInvalidUser_thenReturnsError() {
256         Mockito.when(UserBusinessLogic.getUser(user.getUserId(), false)).thenReturn(user);
257         Mockito.when(consumerOperation.deleteCredentials("marvel123"))
258             .thenReturn(Either.right(StorageOperationStatus.USER_NOT_FOUND));
259         assertTrue(consumerBusinessLogic.deleteConsumer("marvel123", user).isRight());
260     }
261 }