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=========================================================
21 package org.openecomp.sdc.be.components;
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertFalse;
25 import static org.junit.Assert.assertTrue;
26 import static org.mockito.Mockito.when;
28 import java.util.ArrayList;
29 import java.util.HashMap;
30 import java.util.List;
33 import javax.servlet.ServletContext;
35 import org.junit.Before;
36 import org.junit.Test;
37 import org.mockito.Mockito;
38 import org.mockito.invocation.InvocationOnMock;
39 import org.mockito.stubbing.Answer;
40 import org.openecomp.sdc.ElementOperationMock;
41 import org.openecomp.sdc.be.auditing.api.IAuditingManager;
42 import org.openecomp.sdc.be.auditing.impl.AuditingManager;
43 import org.openecomp.sdc.be.components.impl.ArtifactsBusinessLogic;
44 import org.openecomp.sdc.be.components.impl.ResponseFormatManager;
45 import org.openecomp.sdc.be.components.impl.ServiceBusinessLogic;
46 import org.openecomp.sdc.be.config.ConfigurationManager;
47 import org.openecomp.sdc.be.dao.api.ActionStatus;
48 import org.openecomp.sdc.be.dao.cassandra.AuditCassandraDao;
49 import org.openecomp.sdc.be.dao.impl.AuditingDao;
50 import org.openecomp.sdc.be.dao.titan.TitanGenericDao;
51 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
52 import org.openecomp.sdc.be.datatypes.enums.NodeTypeEnum;
53 import org.openecomp.sdc.be.impl.ComponentsUtils;
54 import org.openecomp.sdc.be.impl.WebAppContextWrapper;
55 import org.openecomp.sdc.be.model.ArtifactDefinition;
56 import org.openecomp.sdc.be.model.Component;
57 import org.openecomp.sdc.be.model.Resource;
58 import org.openecomp.sdc.be.model.Service;
59 import org.openecomp.sdc.be.model.User;
60 import org.openecomp.sdc.be.model.category.CategoryDefinition;
61 import org.openecomp.sdc.be.model.operations.api.IElementOperation;
62 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
63 import org.openecomp.sdc.be.model.operations.impl.CacheMangerOperation;
64 import org.openecomp.sdc.be.model.operations.impl.GraphLockOperation;
65 import org.openecomp.sdc.be.model.operations.impl.ServiceOperation;
66 import org.openecomp.sdc.be.resources.data.auditing.DistributionDeployEvent;
67 import org.openecomp.sdc.be.resources.data.auditing.DistributionNotificationEvent;
68 import org.openecomp.sdc.be.resources.data.auditing.ResourceAdminEvent;
69 import org.openecomp.sdc.be.user.Role;
70 import org.openecomp.sdc.be.user.UserBusinessLogic;
71 import org.openecomp.sdc.common.api.ArtifactGroupTypeEnum;
72 import org.openecomp.sdc.common.api.ConfigurationSource;
73 import org.openecomp.sdc.common.api.Constants;
74 import org.openecomp.sdc.common.datastructure.AuditingFieldsKeysEnum;
75 import org.openecomp.sdc.common.datastructure.ESTimeBasedEvent;
76 import org.openecomp.sdc.common.impl.ExternalConfiguration;
77 import org.openecomp.sdc.common.impl.FSConfigurationSource;
78 import org.openecomp.sdc.common.util.ValidationUtils;
79 import org.openecomp.sdc.exception.ResponseFormat;
80 import org.slf4j.Logger;
81 import org.slf4j.LoggerFactory;
82 import org.springframework.web.context.WebApplicationContext;
84 import com.google.gson.Gson;
85 import com.google.gson.GsonBuilder;
87 import fj.data.Either;
89 public class ServiceBusinessLogicTest {
91 private static Logger log = LoggerFactory.getLogger(ServiceBusinessLogicTest.class.getName());
92 private static final String SERVICE_CATEGORY = "Mobility";
93 final ServletContext servletContext = Mockito.mock(ServletContext.class);
94 IAuditingManager iAuditingManager = null;
95 UserBusinessLogic mockUserAdmin = Mockito.mock(UserBusinessLogic.class);
96 final ServiceOperation serviceOperation = Mockito.mock(ServiceOperation.class);
97 WebAppContextWrapper webAppContextWrapper = Mockito.mock(WebAppContextWrapper.class);
98 WebApplicationContext webAppContext = Mockito.mock(WebApplicationContext.class);
99 ServiceBusinessLogic bl = new ServiceBusinessLogic();
100 ResponseFormatManager responseManager = null;
101 IElementOperation mockElementDao;
102 AuditingManager auditingManager = Mockito.mock(AuditingManager.class);
103 ComponentsUtils componentsUtils = new ComponentsUtils();
104 AuditCassandraDao auditingDao = Mockito.mock(AuditCassandraDao.class);
105 ArtifactsBusinessLogic artifactBl = Mockito.mock(ArtifactsBusinessLogic.class);
106 GraphLockOperation graphLockOperation = Mockito.mock(GraphLockOperation.class);
107 TitanGenericDao mockTitanDao = Mockito.mock(TitanGenericDao.class);
109 CacheMangerOperation cacheManager = Mockito.mock(CacheMangerOperation.class);
112 Service serviceResponse = null;
113 private static final String CERTIFIED_VERSION = "1.0";
114 private static final String UNCERTIFIED_VERSION = "0.2";
115 private static final String COMPONNET_ID = "myUniqueId";
116 private static Map<AuditingFieldsKeysEnum, Object> FILTER_MAP_CERTIFIED_VERSION = new HashMap<AuditingFieldsKeysEnum, Object>();
117 private static Map<AuditingFieldsKeysEnum, Object> FILTER_MAP_UNCERTIFIED_VERSION_CURR = new HashMap<AuditingFieldsKeysEnum, Object>();
118 private static Map<AuditingFieldsKeysEnum, Object> FILTER_MAP_UNCERTIFIED_VERSION_PREV = new HashMap<AuditingFieldsKeysEnum, Object>();
120 public ServiceBusinessLogicTest() {
125 public void setup() {
127 ExternalConfiguration.setAppName("catalog-be");
129 // Init Configuration
130 String appConfigDir = "src/test/resources/config/catalog-be";
131 ConfigurationSource configurationSource = new FSConfigurationSource(ExternalConfiguration.getChangeListener(), appConfigDir);
132 ConfigurationManager configurationManager = new ConfigurationManager(configurationSource);
135 mockElementDao = new ElementOperationMock();
137 // User data and management
139 user.setUserId("jh0003");
140 user.setFirstName("Jimmi");
141 user.setLastName("Hendrix");
142 user.setRole(Role.ADMIN.name());
144 Either<User, ActionStatus> eitherGetUser = Either.left(user);
145 when(mockUserAdmin.getUser("jh0003", false)).thenReturn(eitherGetUser);
147 // Servlet Context attributes
148 when(servletContext.getAttribute(Constants.CONFIGURATION_MANAGER_ATTR)).thenReturn(configurationManager);
149 when(servletContext.getAttribute(Constants.SERVICE_OPERATION_MANAGER)).thenReturn(serviceOperation);
150 when(servletContext.getAttribute(Constants.WEB_APPLICATION_CONTEXT_WRAPPER_ATTR)).thenReturn(webAppContextWrapper);
151 when(webAppContextWrapper.getWebAppContext(servletContext)).thenReturn(webAppContext);
152 when(webAppContext.getBean(IElementOperation.class)).thenReturn(mockElementDao);
153 when(graphLockOperation.lockComponent(Mockito.anyString(), Mockito.eq(NodeTypeEnum.Service))).thenReturn(StorageOperationStatus.OK);
154 when(graphLockOperation.lockComponentByName(Mockito.anyString(), Mockito.eq(NodeTypeEnum.Service))).thenReturn(StorageOperationStatus.OK);
156 // artifact bussinesslogic
157 ArtifactDefinition artifactDef = new ArtifactDefinition();
158 when(artifactBl.createArtifactPlaceHolderInfo(Mockito.anyString(), Mockito.anyString(), Mockito.anyMap(), Mockito.any(User.class), Mockito.any(ArtifactGroupTypeEnum.class))).thenReturn(artifactDef);
161 serviceResponse = createServiceObject(true);
162 Either<Service, StorageOperationStatus> eitherCreate = Either.left(serviceResponse);
163 when(serviceOperation.createService(Mockito.any(Service.class))).thenReturn(eitherCreate);
164 Either<Boolean, StorageOperationStatus> eitherCount = Either.left(true);
165 when(serviceOperation.validateServiceNameExists("Service")).thenReturn(eitherCount);
166 Either<Boolean, StorageOperationStatus> eitherCountExist = Either.left(false);
167 when(serviceOperation.validateServiceNameExists("alreadyExist")).thenReturn(eitherCountExist);
169 when(serviceOperation.validateComponentNameExists("alreadyExist")).thenReturn(eitherCountExist);
170 when(serviceOperation.validateComponentNameExists("Service")).thenReturn(eitherCount);
172 bl = new ServiceBusinessLogic();
173 bl.setElementDao(mockElementDao);
174 bl.setUserAdmin(mockUserAdmin);
175 bl.setServiceOperation(serviceOperation);
176 bl.setArtifactBl(artifactBl);
177 bl.setGraphLockOperation(graphLockOperation);
178 bl.setTitanGenericDao(mockTitanDao);
180 componentsUtils.Init();
181 componentsUtils.setAuditingManager(auditingManager);
182 bl.setComponentsUtils(componentsUtils);
183 bl.setCassandraAuditingDao(auditingDao);
184 bl.setCacheManagerOperation(cacheManager);
186 mockAuditingDaoLogic();
188 responseManager = ResponseFormatManager.getInstance();
193 public void testGetComponentAuditRecordsCertifiedVersion() {
194 Either<List<Map<String, Object>>, ResponseFormat> componentAuditRecords = bl.getComponentAuditRecords(CERTIFIED_VERSION, COMPONNET_ID, user.getUserId());
195 assertTrue(componentAuditRecords.isLeft());
196 int size = componentAuditRecords.left().value().size();
197 assertTrue(size == 3);
201 public void testGetComponentAuditRecordsUnCertifiedVersion() {
202 Either<List<Map<String, Object>>, ResponseFormat> componentAuditRecords = bl.getComponentAuditRecords(UNCERTIFIED_VERSION, COMPONNET_ID, user.getUserId());
203 assertTrue(componentAuditRecords.isLeft());
204 int size = componentAuditRecords.left().value().size();
205 assertTrue(size == 1);
209 public void testHappyScenario() {
210 Service service = createServiceObject(false);
211 Either<Service, ResponseFormat> createResponse = bl.createService(service, user);
213 if (createResponse.isRight()) {
214 assertEquals(new Integer(200), createResponse.right().value().getStatus());
216 assertEqualsServiceObject(createServiceObject(true), createResponse.left().value());
219 private void assertEqualsServiceObject(Service origService, Service newService) {
220 assertEquals(origService.getContactId(), newService.getContactId());
221 assertEquals(origService.getCategories(), newService.getCategories());
222 assertEquals(origService.getCreatorUserId(), newService.getCreatorUserId());
223 assertEquals(origService.getCreatorFullName(), newService.getCreatorFullName());
224 assertEquals(origService.getDescription(), newService.getDescription());
225 assertEquals(origService.getIcon(), newService.getIcon());
226 assertEquals(origService.getLastUpdaterUserId(), newService.getLastUpdaterUserId());
227 assertEquals(origService.getLastUpdaterFullName(), newService.getLastUpdaterFullName());
228 assertEquals(origService.getName(), newService.getName());
229 assertEquals(origService.getName(), newService.getName());
230 assertEquals(origService.getUniqueId(), newService.getUniqueId());
231 assertEquals(origService.getVersion(), newService.getVersion());
232 assertEquals(origService.getArtifacts(), newService.getArtifacts());
233 assertEquals(origService.getCreationDate(), newService.getCreationDate());
234 assertEquals(origService.getLastUpdateDate(), newService.getLastUpdateDate());
235 assertEquals(origService.getLifecycleState(), newService.getLifecycleState());
236 assertEquals(origService.getTags(), newService.getTags());
239 private void assertResponse(Either<Service, ResponseFormat> createResponse, ActionStatus expectedStatus, String... variables) {
240 ResponseFormat expectedResponse = responseManager.getResponseFormat(expectedStatus, variables);
241 ResponseFormat actualResponse = createResponse.right().value();
242 assertEquals(expectedResponse.getStatus(), actualResponse.getStatus());
243 assertEquals("assert error description", expectedResponse.getFormattedMessage(), actualResponse.getFormattedMessage());
246 /* CREATE validations - start ***********************/
247 // Service name - start
250 public void testFailedServiceValidations() {
251 testServiceNameAlreadyExists();
252 testServiceNameEmpty();
253 // testServiceNameExceedsLimit();
254 testServiceNameWrongFormat();
255 testServiceDescriptionEmpty();
256 testServiceDescriptionMissing();
257 testServiceDescExceedsLimitCreate();
258 testServiceDescNotEnglish();
259 testServiceIconEmpty();
260 testServiceIconMissing();
261 testResourceIconInvalid();
262 testResourceIconExceedsLimit();
263 // testTagsExceedsLimitCreate();
264 // testTagsSingleExcessLimit();
265 testTagsNoServiceName();
267 testServiceTagNotExist();
268 testServiceTagEmpty();
269 // 1610OS Support - Because of changes in the validation in the ui these tests will fail. need to fix them
270 //testContactIdTooLong();
271 //testContactIdWrongFormatCreate();
272 testResourceContactIdMissing();
273 testServiceCategoryExist();
274 testServiceBadCategoryCreate();
275 // 1610OS Support - Because of changes in the validation in the ui these tests will fail. need to fix them
276 //testInvalidProjectCode();
277 //testProjectCodeTooLong();
278 //testProjectCodeTooShort();
279 testMissingProjectCode();
282 private void testServiceNameAlreadyExists() {
283 String serviceName = "alreadyExist";
284 Service serviceExccedsNameLimit = createServiceObject(false);
285 // 51 chars, the limit is 50
286 serviceExccedsNameLimit.setName(serviceName);
287 List<String> tgs = new ArrayList<String>();
288 tgs.add(serviceName);
289 serviceExccedsNameLimit.setTags(tgs);
291 Either<Service, ResponseFormat> createResponse = bl.createService(serviceExccedsNameLimit, user);
292 assertTrue(createResponse.isRight());
293 assertResponse(createResponse, ActionStatus.COMPONENT_NAME_ALREADY_EXIST, ComponentTypeEnum.SERVICE.getValue(), serviceName);
296 private void testServiceNameEmpty() {
297 Service serviceExccedsNameLimit = createServiceObject(false);
298 serviceExccedsNameLimit.setName(null);
300 Either<Service, ResponseFormat> createResponse = bl.createService(serviceExccedsNameLimit, user);
301 assertTrue(createResponse.isRight());
302 assertResponse(createResponse, ActionStatus.MISSING_COMPONENT_NAME, ComponentTypeEnum.SERVICE.getValue());
305 private void testServiceNameExceedsLimit() {
306 Service serviceExccedsNameLimit = createServiceObject(false);
307 // 51 chars, the limit is 50
308 String tooLongServiceName = "h1KSyJh9EspI8SPwAGu4VETfqWejeanuB1PCJBxdsafefegesse";
309 serviceExccedsNameLimit.setName(tooLongServiceName);
311 Either<Service, ResponseFormat> createResponse = bl.createService(serviceExccedsNameLimit, user);
312 assertTrue(createResponse.isRight());
313 assertResponse(createResponse, ActionStatus.COMPONENT_NAME_EXCEEDS_LIMIT, ComponentTypeEnum.SERVICE.getValue(), "" + ValidationUtils.COMPONENT_NAME_MAX_LENGTH);
316 private void testServiceNameWrongFormat() {
317 Service service = createServiceObject(false);
319 String nameWrongFormat = "ljg\fd";
320 service.setName(nameWrongFormat);
322 Either<Service, ResponseFormat> createResponse = bl.createService(service, user);
323 assertTrue(createResponse.isRight());
324 assertResponse(createResponse, ActionStatus.INVALID_COMPONENT_NAME, ComponentTypeEnum.SERVICE.getValue());
327 // Service name - end
328 // Service description - start
329 private void testServiceDescriptionEmpty() {
330 Service serviceExist = createServiceObject(false);
331 serviceExist.setDescription("");
333 Either<Service, ResponseFormat> createResponse = bl.createService(serviceExist, user);
334 assertTrue(createResponse.isRight());
336 assertResponse(createResponse, ActionStatus.COMPONENT_MISSING_DESCRIPTION, ComponentTypeEnum.SERVICE.getValue());
339 private void testServiceDescriptionMissing() {
340 Service serviceExist = createServiceObject(false);
341 serviceExist.setDescription(null);
343 Either<Service, ResponseFormat> createResponse = bl.createService(serviceExist, user);
344 assertTrue(createResponse.isRight());
346 assertResponse(createResponse, ActionStatus.COMPONENT_MISSING_DESCRIPTION, ComponentTypeEnum.SERVICE.getValue());
349 private void testServiceDescExceedsLimitCreate() {
350 Service serviceExccedsDescLimit = createServiceObject(false);
351 // 1025 chars, the limit is 1024
352 String tooLongServiceDesc = "1GUODojQ0sGzKR4NP7e5j82ADQ3KHTVOaezL95qcbuaqDtjZhAQGQ3iFwKAy580K4WiiXs3u3zq7RzXcSASl5fm0RsWtCMOIDP"
353 + "AOf9Tf2xtXxPCuCIMCR5wOGnNTaFxgnJEHAGxilBhZDgeMNHmCN1rMK5B5IRJOnZxcpcL1NeG3APTCIMP1lNAxngYulDm9heFSBc8TfXAADq7703AvkJT0QPpGq2z2P"
354 + "tlikcAnIjmWgfC5Tm7UH462BAlTyHg4ExnPPL4AO8c92VrD7kZSgSqiy73cN3gLT8uigkKrUgXQFGVUFrXVyyQXYtVM6bLBeuCGQf4C2j8lkNg6M0J3PC0PzMRoinOxk"
355 + "Ae2teeCtVcIj4A1KQo3210j8q2v7qQU69Mabsa6DT9FgE4rcrbiFWrg0Zto4SXWD3o1eJA9o29lTg6kxtklH3TuZTmpi5KVp1NFhS1RpnqF83tzv4mZLKsx7Zh1fEgYvRFwx1"
356 + "ar3RolyDfNoZiGBGTMsZzz7RPFBf2hTnLmNqVGQnHKhhGj0Y5s8t2cbqbO2nmHiJb9uaUVrCGypgbAcJL3KPOBfAVW8PcpmNj4yVjI3L4x5zHjmGZbp9vKshEQODcrmcgsYAoKqe"
357 + "uu5u7jk8XVxEfQ0m5qL8UOErXPlJovSmKUmP5B5T0w299zIWDYCzSoNasHpHjOMDLAiDDeHbozUOn9t3Qou00e9POq4RMM0VnIx1H38nJoJZz2XH8CI5YMQe7oTagaxgQTF2aa0qaq2"
358 + "V6nJsfRGRklGjNhFFYP2cS4Xv2IJO9DSX6LTXOmENrGVJJvMOZcvnBaZPfoAHN0LU4i1SoepLzulIxnZBfkUWFJgZ5wQ0Bco2GC1HMqzW21rwy4XHRxXpXbmW8LVyoA1KbnmVmROycU4"
359 + "scTZ62IxIcIWCVeMjBIcTviXULbPUyqlfEPXWr8IMJtpAaELWgyquPClAREMDs2b9ztKmUeXlMccFES1XWbFTrhBHhmmDyVReEgCwfokrUFR13LTUK1k8I6OEHOs";
361 serviceExccedsDescLimit.setDescription(tooLongServiceDesc);
363 Either<Service, ResponseFormat> createResponse = bl.createService(serviceExccedsDescLimit, user);
364 assertTrue(createResponse.isRight());
365 assertResponse(createResponse, ActionStatus.COMPONENT_DESCRIPTION_EXCEEDS_LIMIT, ComponentTypeEnum.SERVICE.getValue(), "" + ValidationUtils.COMPONENT_DESCRIPTION_MAX_LENGTH);
368 private void testServiceDescNotEnglish() {
369 Service notEnglish = createServiceObject(false);
371 String tooLongServiceDesc = "\uC2B5";
372 notEnglish.setDescription(tooLongServiceDesc);
374 Either<Service, ResponseFormat> createResponse = bl.createService(notEnglish, user);
375 assertTrue(createResponse.isRight());
376 assertResponse(createResponse, ActionStatus.COMPONENT_INVALID_DESCRIPTION, ComponentTypeEnum.SERVICE.getValue());
379 // Service description - stop
380 // Service icon - start
381 private void testServiceIconEmpty() {
382 Service serviceExist = createServiceObject(false);
383 serviceExist.setIcon("");
385 Either<Service, ResponseFormat> createResponse = bl.createService(serviceExist, user);
386 assertTrue(createResponse.isRight());
388 assertResponse(createResponse, ActionStatus.COMPONENT_MISSING_ICON, ComponentTypeEnum.SERVICE.getValue());
391 private void testServiceIconMissing() {
392 Service serviceExist = createServiceObject(false);
393 serviceExist.setIcon(null);
395 Either<Service, ResponseFormat> createResponse = bl.createService(serviceExist, user);
396 assertTrue(createResponse.isRight());
398 assertResponse(createResponse, ActionStatus.COMPONENT_MISSING_ICON, ComponentTypeEnum.SERVICE.getValue());
401 private void testResourceIconInvalid() {
402 Service resourceExist = createServiceObject(false);
403 resourceExist.setIcon("kjk3453^&");
405 Either<Service, ResponseFormat> createResponse = bl.createService(resourceExist, user);
406 assertTrue(createResponse.isRight());
408 assertResponse(createResponse, ActionStatus.COMPONENT_INVALID_ICON, ComponentTypeEnum.SERVICE.getValue());
411 private void testResourceIconExceedsLimit() {
412 Service resourceExist = createServiceObject(false);
413 resourceExist.setIcon("dsjfhskdfhskjdhfskjdhkjdhfkshdfksjsdkfhsdfsdfsdfsfsdfsf");
415 Either<Service, ResponseFormat> createResponse = bl.createService(resourceExist, user);
416 assertTrue(createResponse.isRight());
418 assertResponse(createResponse, ActionStatus.COMPONENT_ICON_EXCEEDS_LIMIT, ComponentTypeEnum.SERVICE.getValue(), "" + ValidationUtils.ICON_MAX_LENGTH);
421 // Service icon - stop
422 // Service tags - start
423 private void testTagsExceedsLimitCreate() {
424 Service serviceExccedsNameLimit = createServiceObject(false);
425 String tag1 = "I63llMSEF12FntTwpMt64JhopkjQZzv5KS7mBoRku42PYLrBjQ";
426 String tag2 = "I63llMSEF12FntTwpMt64JhopkjQZzv5KS7mBoRku42PYLrBjW";
427 String tag3 = "I63llMSEF12FntTwpMt64JhopkjQZzv5KS7mBoRku42PYLrBjE";
428 String tag4 = "I63llMSEF12FntTwpMt64JhopkjQZzv5KS7mBoRku42PYLrBjb";
429 String tag5 = "I63llMSEF12FntTwpMt64JhopkjQZzv5KS7mBoRku42PYLrBjr";
430 String tag6 = "I63llMSEF12FntTwpMt64JhopkjQZzv5KS7mBoRku42PYLrBjf";
431 String tag7 = "I63llMSEF12FntTwpMt64JhopkjQZzv5KS7mBoRku42PYLrBjg";
432 String tag8 = "I63llMSEF12FntTwpMt64JhopkjQZzv5KS7mBoRku42PYLrBjd";
433 String tag9 = "I63llMSEF12FntTwpMt64JhopkjQZzv5KS7mBoRku42PYLrBjf";
434 String tag10 = "I63llMSEF12FntTwpMt64JhopkjQZzv5KS7mBoRku42PYLrBjg";
435 String tag11 = "I63llMSEF12FntTwpMt64JhopkjQZzv5KS7mBoRku42PYLrBjh";
436 String tag12 = "I63llMSEF12FntTwpMt64JhopkjQZzv5KS7mBoRku42PYLrBjj";
437 String tag13 = "I63llMSEF12FntTwpMt64JhopkjQZzv5KS7mBoRku42PYLrBjk";
438 String tag14 = "I63llMSEF12FntTwpMt64JhopkjQZzv5KS7mBoRku42PYLrBjs";
439 String tag15 = "I63llMSEF12FntTwpMt64JhopkjQZzv5KS7mBoRku42PYLrBjz";
440 String tag16 = "I63llMSEF12FntTwpMt64JhopkjQZzv5KS7mBoRku42PYLrBjx";
441 String tag17 = "I63llMSEF12FntTwpMt64JhopkjQZzv5KS7mBoRku42PYLrBj2";
442 String tag18 = "I63llMSEF12FntTwpMt64JhopkjQZzv5KS7mBoRku42PYLrBj3";
443 String tag19 = "I63llMSEF12FntTwpMt64JhopkjQZzv5KS7mBoRku42PYLrBj4";
444 String tag20 = "I63llMSEF12FntTwpMt64JhopkjQZzv5KS7mBoRku42PYLrBj5";
445 String tag21 = "I63llMSEF12FntTwpMt64JhopkjQZzv5KS7mBoRku42PYLrBj0";
447 List<String> tagsList = new ArrayList<String>();
469 tagsList.add(serviceExccedsNameLimit.getName());
471 serviceExccedsNameLimit.setTags(tagsList);
473 Either<Service, ResponseFormat> createResponse = bl.createService(serviceExccedsNameLimit, user);
474 assertTrue(createResponse.isRight());
475 assertResponse(createResponse, ActionStatus.COMPONENT_TAGS_EXCEED_LIMIT, "" + ValidationUtils.TAG_LIST_MAX_LENGTH);
479 private void testTagsSingleExcessLimit() {
480 Service serviceExccedsNameLimit = createServiceObject(false);
481 String tag1 = "afzs2qLBb5X6tZhiunkcEwiFX1qRQY8YZl3y3Du5M5xeQY5Nq9a";
482 String tag2 = serviceExccedsNameLimit.getName();
483 List<String> tagsList = new ArrayList<String>();
486 serviceExccedsNameLimit.setTags(tagsList);
488 Either<Service, ResponseFormat> createResponse = bl.createService(serviceExccedsNameLimit, user);
489 assertTrue(createResponse.isRight());
490 assertResponse(createResponse, ActionStatus.COMPONENT_SINGLE_TAG_EXCEED_LIMIT, "" + ValidationUtils.TAG_MAX_LENGTH);
494 private void testTagsNoServiceName() {
495 Service serviceExccedsNameLimit = createServiceObject(false);
496 String tag1 = "afzs2qLBb";
497 List<String> tagsList = new ArrayList<String>();
499 serviceExccedsNameLimit.setTags(tagsList);
501 Either<Service, ResponseFormat> createResponse = bl.createService(serviceExccedsNameLimit, user);
502 assertTrue(createResponse.isRight());
503 assertResponse(createResponse, ActionStatus.COMPONENT_INVALID_TAGS_NO_COMP_NAME);
507 private void testInvalidTag() {
508 Service serviceExccedsNameLimit = createServiceObject(false);
509 String tag1 = "afzs2qLBb%#%";
510 List<String> tagsList = new ArrayList<String>();
512 serviceExccedsNameLimit.setTags(tagsList);
514 Either<Service, ResponseFormat> createResponse = bl.createService(serviceExccedsNameLimit, user);
515 assertTrue(createResponse.isRight());
516 assertResponse(createResponse, ActionStatus.INVALID_FIELD_FORMAT, new String[] { "Service", "tag" });
520 private void testServiceTagNotExist() {
521 Service serviceExist = createServiceObject(false);
522 serviceExist.setTags(null);
524 Either<Service, ResponseFormat> createResponse = bl.createService(serviceExist, user);
525 assertTrue(createResponse.isRight());
527 assertResponse(createResponse, ActionStatus.COMPONENT_MISSING_TAGS);
530 private void testServiceTagEmpty() {
531 Service serviceExist = createServiceObject(false);
532 serviceExist.setTags(new ArrayList<String>());
534 Either<Service, ResponseFormat> createResponse = bl.createService(serviceExist, user);
535 assertTrue(createResponse.isRight());
537 assertResponse(createResponse, ActionStatus.COMPONENT_MISSING_TAGS);
540 // Service tags - stop
541 // Service contactId - start
542 private void testContactIdTooLong() {
543 Service serviceContactId = createServiceObject(false);
544 // 7 chars instead of 6
545 String contactIdTooLong = "yrt1234";
546 serviceContactId.setContactId(contactIdTooLong);
548 Either<Service, ResponseFormat> createResponse = bl.createService(serviceContactId, user);
549 assertTrue(createResponse.isRight());
550 assertResponse(createResponse, ActionStatus.COMPONENT_INVALID_CONTACT, ComponentTypeEnum.SERVICE.getValue());
553 private void testContactIdWrongFormatCreate() {
554 Service serviceContactId = createServiceObject(false);
555 // 3 letters and 3 digits
556 String contactIdTooLong = "yrt134";
557 serviceContactId.setContactId(contactIdTooLong);
559 Either<Service, ResponseFormat> createResponse = bl.createService(serviceContactId, user);
560 assertTrue(createResponse.isRight());
561 assertResponse(createResponse, ActionStatus.COMPONENT_INVALID_CONTACT, ComponentTypeEnum.SERVICE.getValue());
564 private void testResourceContactIdMissing() {
565 Service resourceExist = createServiceObject(false);
566 resourceExist.setContactId(null);
568 Either<Service, ResponseFormat> createResponse = bl.createService(resourceExist, user);
569 assertTrue(createResponse.isRight());
570 assertResponse(createResponse, ActionStatus.COMPONENT_MISSING_CONTACT, ComponentTypeEnum.SERVICE.getValue());
573 // Service contactId - stop
574 // Service category - start
575 private void testServiceCategoryExist() {
576 Service serviceExist = createServiceObject(false);
577 serviceExist.setCategories(null);
579 Either<Service, ResponseFormat> createResponse = bl.createService(serviceExist, user);
580 assertTrue(createResponse.isRight());
582 assertResponse(createResponse, ActionStatus.COMPONENT_MISSING_CATEGORY, ComponentTypeEnum.SERVICE.getValue());
585 public void markDistributionAsDeployedTestAlreadyDeployed() {
586 String notifyAction = "DNotify";
587 String requestAction = "DRequest";
588 String resultAction = "DResult";
589 String did = "123456";
591 setupBeforeDeploy(notifyAction, requestAction, did);
592 List<DistributionDeployEvent> resultList = new ArrayList<DistributionDeployEvent>();
593 Map<String, Object> params = new HashMap<String, Object>();
594 DistributionDeployEvent event = new DistributionDeployEvent();
596 event.setAction(resultAction);
598 event.setStatus("200");
599 // ESTimeBasedEvent deployEvent = new ESTimeBasedEvent();
600 // deployEvent.setFields(params);
601 resultList.add(event);
602 Either<List<DistributionDeployEvent>, ActionStatus> eventList = Either.left(resultList);
604 Mockito.when(auditingDao.getDistributionDeployByStatus(Mockito.anyString(), Mockito.eq(resultAction), Mockito.anyString())).thenReturn(eventList);
606 Either<Service, ResponseFormat> markDeployed = bl.markDistributionAsDeployed(did, did, user);
607 assertTrue(markDeployed.isLeft());
609 Mockito.verify(auditingDao, Mockito.times(0)).getDistributionRequest(did, requestAction);
614 public void markDistributionAsDeployedTestSuccess() {
615 String notifyAction = "DNotify";
616 String requestAction = "DRequest";
617 String did = "123456";
619 setupBeforeDeploy(notifyAction, requestAction, did);
621 Either<Service, ResponseFormat> markDeployed = bl.markDistributionAsDeployed(did, did, user);
622 assertTrue(markDeployed.isLeft());
627 public void markDistributionAsDeployedTestNotDistributed() {
628 String notifyAction = "DNotify";
629 String requestAction = "DRequest";
630 String did = "123456";
632 setupBeforeDeploy(notifyAction, requestAction, did);
633 List<ResourceAdminEvent> emptyList = new ArrayList<ResourceAdminEvent>();
634 Either<List<ResourceAdminEvent>, ActionStatus> emptyEventList = Either.left(emptyList);
635 Mockito.when(auditingDao.getDistributionRequest(Mockito.anyString(), Mockito.eq(requestAction))).thenReturn(emptyEventList);
637 Either<Service, StorageOperationStatus> notFound = Either.right(StorageOperationStatus.NOT_FOUND);
638 Mockito.when(serviceOperation.getService(did)).thenReturn(notFound);
640 Either<Service, ResponseFormat> markDeployed = bl.markDistributionAsDeployed(did, did, user);
641 assertTrue(markDeployed.isRight());
642 assertEquals(404, markDeployed.right().value().getStatus().intValue());
646 private void testServiceBadCategoryCreate() {
648 Service serviceExist = createServiceObject(false);
649 CategoryDefinition category = new CategoryDefinition();
650 category.setName("koko");
651 List<CategoryDefinition> categories = new ArrayList<>();
652 categories.add(category);
653 serviceExist.setCategories(categories);
655 Either<Service, ResponseFormat> createResponse = bl.createService(serviceExist, user);
656 assertTrue(createResponse.isRight());
658 assertResponse(createResponse, ActionStatus.COMPONENT_INVALID_CATEGORY, ComponentTypeEnum.SERVICE.getValue());
661 // Service category - stop
662 private void testInvalidProjectCode() {
664 Service serviceExist = createServiceObject(false);
665 serviceExist.setProjectCode("koko");
667 Either<Service, ResponseFormat> createResponse = bl.createService(serviceExist, user);
668 assertTrue(createResponse.isRight());
670 assertResponse(createResponse, ActionStatus.INVALID_PROJECT_CODE);
673 private void testProjectCodeTooLong() {
675 Service serviceExist = createServiceObject(false);
676 serviceExist.setProjectCode("5555555555555");
678 Either<Service, ResponseFormat> createResponse = bl.createService(serviceExist, user);
679 assertTrue(createResponse.isRight());
681 assertResponse(createResponse, ActionStatus.INVALID_PROJECT_CODE);
684 private void testProjectCodeTooShort() {
686 Service serviceExist = createServiceObject(false);
687 serviceExist.setProjectCode("555");
689 Either<Service, ResponseFormat> createResponse = bl.createService(serviceExist, user);
690 assertTrue(createResponse.isRight());
692 assertResponse(createResponse, ActionStatus.INVALID_PROJECT_CODE);
695 private void testMissingProjectCode() {
697 Service serviceExist = createServiceObject(false);
698 serviceExist.setProjectCode(null);
700 Either<Service, ResponseFormat> createResponse = bl.createService(serviceExist, user);
701 assertTrue(createResponse.isRight());
703 assertResponse(createResponse, ActionStatus.MISSING_PROJECT_CODE);
707 public void testDeleteMarkedServicesNoServices() {
708 List<String> ids = new ArrayList<String>();
709 Either<List<String>, StorageOperationStatus> eitherNoResources = Either.left(ids);
710 when(serviceOperation.getAllComponentsMarkedForDeletion()).thenReturn(eitherNoResources);
712 Either<List<String>, ResponseFormat> deleteMarkedResources = bl.deleteMarkedComponents();
713 assertTrue(deleteMarkedResources.isLeft());
714 assertTrue(deleteMarkedResources.left().value().isEmpty());
716 Mockito.verify(artifactBl, Mockito.times(0)).deleteAllComponentArtifactsIfNotOnGraph(Mockito.anyList());
721 public void testDeleteMarkedServices() {
722 List<String> ids = new ArrayList<String>();
723 String resourceInUse = "123";
724 ids.add(resourceInUse);
725 String resourceFree = "456";
726 ids.add(resourceFree);
727 Either<List<String>, StorageOperationStatus> eitherNoResources = Either.left(ids);
728 when(serviceOperation.getAllComponentsMarkedForDeletion()).thenReturn(eitherNoResources);
730 Either<Boolean, StorageOperationStatus> resourceInUseResponse = Either.left(true);
731 Either<Boolean, StorageOperationStatus> resourceFreeResponse = Either.left(false);
733 List<ArtifactDefinition> artifacts = new ArrayList<ArtifactDefinition>();
734 Either<List<ArtifactDefinition>, StorageOperationStatus> getArtifactsResponse = Either.left(artifacts);
735 when(serviceOperation.getComponentArtifactsForDelete(resourceFree, NodeTypeEnum.Service, true)).thenReturn(getArtifactsResponse);
737 when(serviceOperation.isComponentInUse(resourceFree)).thenReturn(resourceFreeResponse);
738 when(serviceOperation.isComponentInUse(resourceInUse)).thenReturn(resourceInUseResponse);
740 Either<Component, StorageOperationStatus> eitherDelete = Either.left(new Resource());
741 when(serviceOperation.deleteComponent(resourceFree, true)).thenReturn(eitherDelete);
743 when(artifactBl.deleteAllComponentArtifactsIfNotOnGraph(artifacts)).thenReturn(StorageOperationStatus.OK);
745 Either<List<String>, ResponseFormat> deleteMarkedResources = bl.deleteMarkedComponents();
746 assertTrue(deleteMarkedResources.isLeft());
747 List<String> resourceIdList = deleteMarkedResources.left().value();
748 assertFalse(resourceIdList.isEmpty());
749 assertTrue(resourceIdList.contains(resourceFree));
750 assertFalse(resourceIdList.contains(resourceInUse));
752 Mockito.verify(artifactBl, Mockito.times(1)).deleteAllComponentArtifactsIfNotOnGraph(artifacts);
755 private Service createServiceObject(boolean afterCreate) {
756 Service service = new Service();
757 service.setName("Service");
758 CategoryDefinition category = new CategoryDefinition();
759 category.setName(SERVICE_CATEGORY);
760 List<CategoryDefinition> categories = new ArrayList<>();
761 categories.add(category);
762 service.setCategories(categories);
764 service.setDescription("description");
765 List<String> tgs = new ArrayList<String>();
766 tgs.add(service.getName());
767 service.setTags(tgs);
768 // service.setVendorName("Motorola");
769 // service.setVendorRelease("1.0.0");
770 // service.setContactId("ya5467");
771 service.setIcon("MyIcon");
772 // service.setState(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT);
773 service.setContactId("aa1234");
774 service.setProjectCode("12345");
777 service.setVersion("0.1");
778 service.setUniqueId(service.getName() + ":" + service.getVersion());
779 service.setCreatorUserId(user.getUserId());
780 service.setCreatorFullName(user.getFirstName() + " " + user.getLastName());
782 Gson gson = new GsonBuilder().setPrettyPrinting().create();
783 log.debug("{}", gson.toJson(service));
787 private void mockAuditingDaoLogic() {
788 FILTER_MAP_CERTIFIED_VERSION.put(AuditingFieldsKeysEnum.AUDIT_SERVICE_INSTANCE_ID, COMPONNET_ID);
789 FILTER_MAP_UNCERTIFIED_VERSION_CURR.put(AuditingFieldsKeysEnum.AUDIT_SERVICE_INSTANCE_ID, COMPONNET_ID);
790 FILTER_MAP_UNCERTIFIED_VERSION_PREV.put(AuditingFieldsKeysEnum.AUDIT_SERVICE_INSTANCE_ID, COMPONNET_ID);
792 FILTER_MAP_UNCERTIFIED_VERSION_CURR.put(AuditingFieldsKeysEnum.AUDIT_RESOURCE_CURR_VERSION, UNCERTIFIED_VERSION);
793 FILTER_MAP_UNCERTIFIED_VERSION_PREV.put(AuditingFieldsKeysEnum.AUDIT_RESOURCE_PREV_VERSION, UNCERTIFIED_VERSION);
795 final ResourceAdminEvent createResourceAudit = new ResourceAdminEvent();
796 createResourceAudit.setModifier("Carlos Santana(cs0008)");
797 createResourceAudit.setCurrState("NOT_CERTIFIED_CHECKOUT");
798 createResourceAudit.setCurrVersion("0.1");
799 createResourceAudit.setServiceInstanceId("82eddd99-0bd9-4742-ab0a-1bdb5e262a05");
800 createResourceAudit.setRequestId("3e65cea1-7403-4bc7-b461-e2544d83799f");
801 createResourceAudit.setDesc("OK");
802 createResourceAudit.setResourceType("Resource");
803 createResourceAudit.setStatus("201");
804 createResourceAudit.setPrevVersion("");
805 createResourceAudit.setAction("Create");
806 // fields.put("TIMESTAMP", "2015-11-22 09:19:12.977");
807 createResourceAudit.setPrevState("");
808 createResourceAudit.setResourceName("MyTestResource");
809 // createResourceAudit.setFields(fields);
811 final ResourceAdminEvent checkInResourceAudit = new ResourceAdminEvent();
812 checkInResourceAudit.setModifier("Carlos Santana(cs0008)");
813 checkInResourceAudit.setCurrState("NOT_CERTIFIED_CHECKIN");
814 checkInResourceAudit.setCurrVersion("0.1");
815 checkInResourceAudit.setServiceInstanceId("82eddd99-0bd9-4742-ab0a-1bdb5e262a05");
816 checkInResourceAudit.setRequestId("ffacbf5d-eeb1-43c6-a310-37fe7e1cc091");
817 checkInResourceAudit.setDesc("OK");
818 checkInResourceAudit.setComment("Stam");
819 checkInResourceAudit.setResourceType("Resource");
820 checkInResourceAudit.setStatus("200");
821 checkInResourceAudit.setPrevVersion("0.1");
822 checkInResourceAudit.setAction("Checkin");
823 // fields.put("TIMESTAMP", "2015-11-22 09:25:03.797");
824 checkInResourceAudit.setPrevState("NOT_CERTIFIED_CHECKOUT");
825 checkInResourceAudit.setResourceName("MyTestResource");
827 final ResourceAdminEvent checkOutResourceAudit = new ResourceAdminEvent();
828 checkOutResourceAudit.setModifier("Carlos Santana(cs0008)");
829 checkOutResourceAudit.setCurrState("NOT_CERTIFIED_CHECKOUT");
830 checkOutResourceAudit.setCurrVersion("0.2");
831 checkOutResourceAudit.setServiceInstanceId("82eddd99-0bd9-4742-ab0a-1bdb5e262a05");
832 checkOutResourceAudit.setRequestId("7add5078-4c16-4d74-9691-cc150e3c96b8");
833 checkOutResourceAudit.setDesc("OK");
834 checkOutResourceAudit.setComment("");
835 checkOutResourceAudit.setResourceType("Resource");
836 checkOutResourceAudit.setStatus("200");
837 checkOutResourceAudit.setPrevVersion("0.1");
838 checkOutResourceAudit.setAction("Checkout");
839 // fields.put("TIMESTAMP", "2015-11-22 09:39:41.024");
840 checkOutResourceAudit.setPrevState("NOT_CERTIFIED_CHECKIN");
841 checkOutResourceAudit.setResourceName("MyTestResource");
842 // checkOutResourceAudit.setFields(fields);
844 // Mockito.doAnswer(new Answer<Either<List<ESTimeBasedEvent>,
845 // ActionStatus> >() {
846 // public Either<List<ESTimeBasedEvent>, ActionStatus>
847 // answer(InvocationOnMock invocation) {
848 // final Either<List<ESTimeBasedEvent>, ActionStatus> either;
849 // final List<ESTimeBasedEvent> list;
850 // Object[] args = invocation.getArguments();
851 // Map<AuditingFieldsKeysEnum, Object> filterMap =
852 // (Map<AuditingFieldsKeysEnum, Object>) args[0];
853 // if( filterMap.equals(FILTER_MAP_CERTIFIED_VERSION) ){
855 // ArrayList<ESTimeBasedEvent>(){{add(createResourceAudit);add(checkInResourceAudit);add(checkOutResourceAudit);}};
856 // either = Either.left(list);
859 // else if( filterMap.equals(FILTER_MAP_UNCERTIFIED_VERSION_PREV) ){
860 // list = new ArrayList<ESTimeBasedEvent>();
861 // either = Either.left(list);
863 // else if( filterMap.equals(FILTER_MAP_UNCERTIFIED_VERSION_CURR) ){
865 // ArrayList<ESTimeBasedEvent>(){{/*add(createResourceAudit);add(checkInResourceAudit);*/add(checkOutResourceAudit);}};
866 // either = Either.left(list);
873 // }).when(auditingDao).getFilteredResourceAdminAuditingEvents(Mockito.anyMap());
876 List<ResourceAdminEvent> list = new ArrayList<ResourceAdminEvent>() {
878 add(createResourceAudit);
879 add(checkInResourceAudit);
880 add(checkOutResourceAudit);
883 Either<List<ResourceAdminEvent>, ActionStatus> result = Either.left(list);
884 Mockito.when(auditingDao.getByServiceInstanceId(Mockito.anyString())).thenReturn(result);
886 List<ResourceAdminEvent> listPrev = new ArrayList<ResourceAdminEvent>();
887 Either<List<ResourceAdminEvent>, ActionStatus> resultPrev = Either.left(listPrev);
888 Mockito.when(auditingDao.getAuditByServiceIdAndPrevVersion(Mockito.anyString(), Mockito.anyString())).thenReturn(resultPrev);
890 List<ResourceAdminEvent> listCurr = new ArrayList<ResourceAdminEvent>() {
892 add(checkOutResourceAudit);
895 Either<List<ResourceAdminEvent>, ActionStatus> resultCurr = Either.left(listCurr);
896 Mockito.when(auditingDao.getAuditByServiceIdAndCurrVersion(Mockito.anyString(), Mockito.anyString())).thenReturn(resultCurr);
900 private void setupBeforeDeploy(String notifyAction, String requestAction, String did) {
902 DistributionNotificationEvent notifyEvent = new DistributionNotificationEvent();
903 notifyEvent.setAction(notifyAction);
904 notifyEvent.setDid(did);
905 notifyEvent.setStatus("200");
907 ResourceAdminEvent requestEvent = new ResourceAdminEvent();
908 requestEvent.setAction(requestAction);
909 requestEvent.setDid(did);
910 requestEvent.setStatus("200");
912 ArrayList<DistributionNotificationEvent> arrayList = new ArrayList<DistributionNotificationEvent>();
913 List<DistributionNotificationEvent> notifyResults = arrayList;
914 notifyResults.add(notifyEvent);
915 Either<List<DistributionNotificationEvent>, ActionStatus> eitherNotify = Either.left(notifyResults);
917 Mockito.when(auditingDao.getDistributionNotify(Mockito.anyString(), Mockito.eq(notifyAction))).thenReturn(eitherNotify);
919 List<ResourceAdminEvent> requestResults = new ArrayList<ResourceAdminEvent>();
920 requestResults.add(requestEvent);
921 Either<List<ResourceAdminEvent>, ActionStatus> eitherRequest = Either.left(requestResults);
922 Mockito.when(auditingDao.getDistributionRequest(Mockito.anyString(), Mockito.eq(requestAction))).thenReturn(eitherRequest);
924 Either<Service, StorageOperationStatus> eitherService = Either.left(createServiceObject(true));
925 Mockito.when(serviceOperation.getService(Mockito.anyString())).thenReturn(eitherService);
927 List<DistributionDeployEvent> emptyList = new ArrayList<DistributionDeployEvent>();
928 Either<List<DistributionDeployEvent>, ActionStatus> emptyEventList = Either.left(emptyList);
929 Mockito.when(auditingDao.getDistributionDeployByStatus(Mockito.anyString(), Mockito.eq("DResult"), Mockito.anyString())).thenReturn(emptyEventList);