2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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 * ================================================================================
22 package org.openecomp.sdc.be.components.impl;
24 import fj.data.Either;
25 import org.apache.commons.lang3.RandomStringUtils;
26 import org.junit.Before;
27 import org.junit.Test;
28 import org.mockito.InjectMocks;
29 import org.mockito.Mock;
30 import org.mockito.Mockito;
31 import org.mockito.MockitoAnnotations;
32 import org.openecomp.sdc.be.components.impl.exceptions.ByActionStatusComponentException;
33 import org.openecomp.sdc.be.config.ConfigurationManager;
34 import org.openecomp.sdc.be.dao.api.ActionStatus;
35 import org.openecomp.sdc.be.datatypes.elements.ConsumerDataDefinition;
36 import org.openecomp.sdc.be.datatypes.enums.NodeTypeEnum;
37 import org.openecomp.sdc.be.impl.ComponentsUtils;
38 import org.openecomp.sdc.be.model.ConsumerDefinition;
39 import org.openecomp.sdc.be.model.User;
40 import org.openecomp.sdc.be.model.operations.api.IGraphLockOperation;
41 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
42 import org.openecomp.sdc.be.model.operations.impl.ConsumerOperation;
43 import org.openecomp.sdc.be.resources.data.ConsumerData;
44 import org.openecomp.sdc.be.user.UserBusinessLogic;
45 import org.openecomp.sdc.common.impl.ExternalConfiguration;
46 import org.openecomp.sdc.common.impl.FSConfigurationSource;
47 import org.openecomp.sdc.exception.ResponseFormat;
49 import java.util.HashMap;
52 import static org.junit.Assert.assertEquals;
53 import static org.junit.Assert.assertTrue;
54 import static org.mockito.ArgumentMatchers.any;
55 import static org.mockito.ArgumentMatchers.anyString;
57 public class ConsumerBusinessLogicTest extends BaseBusinessLogicMock {
60 private ConsumerDefinition consumer;
61 private ConsumerDataDefinition consumerDataDefinition;
64 private ConsumerBusinessLogic consumerBusinessLogic;
67 private ComponentsUtils componentsUtils;
70 private UserBusinessLogic UserBusinessLogic;
73 private IGraphLockOperation iGraphLockOperation;
76 private ConsumerOperation consumerOperation;
79 private ConsumerData consumerData;
83 consumerBusinessLogic = new ConsumerBusinessLogic(elementDao, groupOperation, groupInstanceOperation, groupTypeOperation,
84 interfaceOperation, interfaceLifecycleTypeOperation, artifactToscaOperation);
85 consumerDataDefinition = new ConsumerDataDefinition();
86 consumer = new ConsumerDefinition();
87 MockitoAnnotations.initMocks(this);
88 user = new User("Stan", "Lee", "stan.lee",
89 "stan.lee@marvel.com", "ADMIN", 1542024000L);
93 public void testCreateConsumer_givenMissingUser_thenReturnsError() {
94 User user = new User();
95 ConsumerDefinition consumerDefinition = new ConsumerDefinition();
96 Mockito.when(componentsUtils.getResponseFormat(ActionStatus.MISSING_INFORMATION))
97 .thenReturn(new ResponseFormat());
98 assertTrue(consumerBusinessLogic.createConsumer(user, consumerDefinition).isRight());
102 public void testCreateConsumer_givenNonListedUser_thenReturnsError() {
103 ConsumerDefinition consumerDefinition = new ConsumerDefinition();
104 Mockito.when(componentsUtils.getResponseFormat(ActionStatus.RESTRICTED_ACCESS))
105 .thenReturn(new ResponseFormat());
106 Mockito.when(UserBusinessLogic.getUser(user.getUserId(), false)).thenThrow(new ByActionStatusComponentException(ActionStatus.RESTRICTED_OPERATION));
107 assertTrue(consumerBusinessLogic.createConsumer(user, consumerDefinition).isRight());
111 public void testCreateConsumer_givenNonAdminUser_thenReturnsError() {
112 user.setRole("DESIGNER");
113 Mockito.when(componentsUtils.getResponseFormat(ActionStatus.RESTRICTED_OPERATION))
114 .thenReturn(new ResponseFormat());
115 Mockito.when(UserBusinessLogic.getUser(user.getUserId(), false)).thenReturn(user);
116 assertTrue(consumerBusinessLogic.createConsumer(user, consumer).isRight());
120 public void testCreateConsumer_givenInvalidConsumerNames_thenReturnsError() {
121 Map<String, ActionStatus> invalidConsumerNames = new HashMap<>();
122 invalidConsumerNames.put(null, ActionStatus.MISSING_DATA);
123 invalidConsumerNames.put(".#()", ActionStatus.INVALID_CONTENT);
124 invalidConsumerNames.put(RandomStringUtils.random(256, true, false), ActionStatus.EXCEEDS_LIMIT);
125 for(Map.Entry<String, ActionStatus> e: invalidConsumerNames.entrySet()){
126 consumer.setConsumerName(e.getKey());
127 Mockito.when(UserBusinessLogic.getUser(user.getUserId(), false)).thenReturn(user);
128 Mockito.when(componentsUtils.getResponseFormat(e.getValue(), "Consumer name"))
129 .thenReturn(new ResponseFormat());
130 assertTrue(consumerBusinessLogic.createConsumer(user, consumer).isRight());
135 public void testCreateConsumer_givenInvalidConsumerPasswords_thenReturnsError() {
136 Map<String, ActionStatus> invalidPasswordResults = new HashMap<>();
137 invalidPasswordResults.put(null, ActionStatus.MISSING_DATA);
138 invalidPasswordResults.put(RandomStringUtils.random(64, '*' ), ActionStatus.INVALID_CONTENT_PARAM);
139 invalidPasswordResults.put("password", ActionStatus.INVALID_LENGTH);
140 for(Map.Entry<String, ActionStatus> e: invalidPasswordResults.entrySet()){
141 consumer.setConsumerName("_marvel");
142 consumer.setConsumerPassword(e.getKey());
143 Mockito.when(UserBusinessLogic.getUser(user.getUserId(), false)).thenReturn(user);
144 Mockito.when(componentsUtils.getResponseFormat(e.getValue(), "Consumer password"))
145 .thenReturn(new ResponseFormat());
146 assertTrue(consumerBusinessLogic.createConsumer(user, consumer).isRight());
151 public void testCreateConsumer_givenInvalidConsumerSalts_thenReturnsError() {
152 consumer.setConsumerPassword(RandomStringUtils.random(64, true,true));
153 Map<String, ActionStatus> invalidPasswordSalts = new HashMap<>();
154 invalidPasswordSalts.put(null, ActionStatus.MISSING_DATA);
155 invalidPasswordSalts.put(RandomStringUtils.random(32, "*" ), ActionStatus.INVALID_CONTENT_PARAM);
156 invalidPasswordSalts.put("password", ActionStatus.INVALID_LENGTH);
157 for(Map.Entry<String, ActionStatus> e: invalidPasswordSalts.entrySet()){
158 consumer.setConsumerName("_marvel");
159 consumer.setConsumerSalt(e.getKey());
160 Mockito.when(UserBusinessLogic.getUser(user.getUserId(), false)).thenReturn(user);
161 Mockito.when(componentsUtils.getResponseFormat(e.getValue(), "Consumer salt"))
162 .thenReturn(new ResponseFormat());
163 assertTrue(consumerBusinessLogic.createConsumer(user, consumer).isRight());
168 public void testCreateConsumer_givenConsumerNotLocked_thenReturnsError() {
169 consumer.setConsumerName("_marvel");
170 consumer.setConsumerPassword(RandomStringUtils.random(64, true,true));
171 consumer.setConsumerSalt(RandomStringUtils.random(32, 'a'));
172 Mockito.when(UserBusinessLogic.getUser(user.getUserId(), false)).thenReturn(user);
173 Mockito.when(iGraphLockOperation.lockComponent(anyString(), any(NodeTypeEnum.class)))
174 .thenReturn(StorageOperationStatus.GENERAL_ERROR);
175 assertTrue(consumerBusinessLogic.createConsumer(user, consumer).isRight());
179 public void testCreateConsumer_givenUnableToCreateCredentials_thenReturnsError() {
180 ConsumerDataDefinition consumerDataDefinition = new ConsumerDataDefinition();
181 consumerDataDefinition.setConsumerName("_marvel");
182 consumerDataDefinition.setConsumerPassword(RandomStringUtils.random(64, true,true));
183 consumerDataDefinition.setConsumerSalt(RandomStringUtils.random(32, 'a'));
184 ConsumerDefinition consumer = new ConsumerDefinition(consumerDataDefinition);
185 Mockito.when(UserBusinessLogic.getUser(user.getUserId(), false)).thenReturn(user);
186 Mockito.when(iGraphLockOperation.lockComponent(anyString(), any(NodeTypeEnum.class)))
187 .thenReturn(StorageOperationStatus.OK);
188 Mockito.when(consumerOperation.getCredentials(anyString()))
189 .thenReturn(Either.right(StorageOperationStatus.OK));
190 Mockito.when(consumerOperation.createCredentials(any(ConsumerData.class)))
191 .thenReturn(Either.right(StorageOperationStatus.USER_NOT_FOUND));
192 assertTrue(consumerBusinessLogic.createConsumer(user, consumer).isRight());
196 public void testCreateConsumer_givenValidUserAndConsumer_thenReturnsConsumer() {
197 ConsumerDataDefinition consumerDataDefinition = new ConsumerDataDefinition();
198 consumerDataDefinition.setConsumerName("_marvel");
199 consumerDataDefinition.setConsumerPassword(RandomStringUtils.random(64, true,true));
200 consumerDataDefinition.setConsumerSalt(RandomStringUtils.random(32, 'a'));
201 ConsumerDefinition consumer = new ConsumerDefinition(consumerDataDefinition);
202 Mockito.when(UserBusinessLogic.getUser(user.getUserId(), false)).thenReturn(user);
203 Mockito.when(iGraphLockOperation.lockComponent(anyString(), any(NodeTypeEnum.class)))
204 .thenReturn(StorageOperationStatus.OK);
205 Mockito.when(consumerOperation.getCredentials(anyString()))
206 .thenReturn(Either.right(StorageOperationStatus.OK));
207 Mockito.when(consumerOperation.createCredentials(any(ConsumerData.class)))
208 .thenReturn(Either.left(new ConsumerData(consumerDataDefinition)));
209 assertTrue(consumerBusinessLogic.createConsumer(user, consumer).isLeft());
213 public void testGetConsumer_givenNullUser_thenReturnsError() {
214 Mockito.when(consumerOperation.getCredentials("marvel123"))
215 .thenReturn(Either.right(StorageOperationStatus.USER_NOT_FOUND));
216 assertTrue(consumerBusinessLogic.getConsumer("marvel123", null).isRight());
220 public void testGetConsumer_givenValidUserAndConsumerId_thenReturnsConsumer() {
221 Mockito.when(consumerOperation.getCredentials("marvel123"))
222 .thenReturn(Either.left(new ConsumerData()));
223 Mockito.when(consumerData.getConsumerDataDefinition())
224 .thenReturn(consumerDataDefinition);
225 Mockito.when(UserBusinessLogic.getUser(user.getUserId(), false)).thenReturn(user);
226 assertTrue(consumerBusinessLogic.getConsumer("marvel123", user).isLeft());
230 public void testUpdateConsumer_givenValidConsumer_thenReturnsUpdatedConsumer() {
231 ConsumerDefinition updatedConsumer = new ConsumerDefinition(consumerDataDefinition);
232 updatedConsumer.setConsumerName("marvel2");
233 ConsumerDataDefinition updatedConsumerDataDef = new ConsumerDataDefinition();
234 updatedConsumerDataDef.setConsumerName("marvel2");
235 ConsumerData consumerData = new ConsumerData(updatedConsumerDataDef);
236 Mockito.when(consumerOperation.updateCredentials(any(ConsumerData.class)))
237 .thenReturn(Either.left(consumerData));
238 assertEquals(updatedConsumer.getConsumerName(), consumerBusinessLogic.updateConsumer(consumer).left().value().getConsumerName());
242 public void testUpdateConsumer_givenUpdateFailure_thenReturnsError() {
243 Mockito.when(consumerOperation.updateCredentials(any(ConsumerData.class)))
244 .thenReturn(Either.right(StorageOperationStatus.GENERAL_ERROR));
245 assertTrue(consumerBusinessLogic.updateConsumer(consumer).isRight());
249 public void testDeleteConsumer_givenValidUserAndConsumerId_thenReturnsSuccessful() {
250 ConsumerData consumerData = new ConsumerData(new ConsumerDataDefinition());
251 Mockito.when(UserBusinessLogic.getUser(user.getUserId(), false)).thenReturn(user);
252 Mockito.when(consumerOperation.deleteCredentials("marvel123"))
253 .thenReturn(Either.left(consumerData));
254 assertTrue(consumerBusinessLogic.deleteConsumer("marvel123", user).isLeft());
258 public void testDeleteConsumer_givenInvalidUser_thenReturnsError() {
259 Mockito.when(UserBusinessLogic.getUser(user.getUserId(), false)).thenReturn(user);
260 Mockito.when(consumerOperation.deleteCredentials("marvel123"))
261 .thenReturn(Either.right(StorageOperationStatus.USER_NOT_FOUND));
262 assertTrue(consumerBusinessLogic.deleteConsumer("marvel123", user).isRight());