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.impl;
23 import static org.assertj.core.api.Assertions.assertThat;
24 import static org.hamcrest.Matchers.is;
25 import static org.hamcrest.Matchers.notNullValue;
26 import static org.hamcrest.Matchers.nullValue;
27 import static org.junit.jupiter.api.Assertions.assertEquals;
28 import static org.junit.jupiter.api.Assertions.assertFalse;
29 import static org.junit.jupiter.api.Assertions.assertNotNull;
30 import static org.junit.jupiter.api.Assertions.assertNull;
31 import static org.junit.jupiter.api.Assertions.assertTrue;
32 import static org.junit.jupiter.api.Assertions.fail;
33 import static org.mockito.Mockito.when;
35 import com.google.common.collect.Lists;
36 import com.google.common.collect.Maps;
37 import fj.data.Either;
38 import java.lang.reflect.Method;
39 import java.util.ArrayList;
40 import java.util.Arrays;
41 import java.util.HashMap;
42 import java.util.List;
44 import java.util.UUID;
45 import org.apache.commons.lang3.tuple.ImmutablePair;
46 import org.junit.jupiter.api.Test;
47 import org.mockito.Mockito;
48 import org.openecomp.sdc.be.components.impl.exceptions.ComponentException;
49 import org.openecomp.sdc.be.dao.api.ActionStatus;
50 import org.openecomp.sdc.be.datatypes.elements.ArtifactDataDefinition;
51 import org.openecomp.sdc.be.datatypes.elements.InterfaceInstanceDataDefinition;
52 import org.openecomp.sdc.be.datatypes.elements.ListDataDefinition;
53 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
54 import org.openecomp.sdc.be.model.ArtifactDefinition;
55 import org.openecomp.sdc.be.model.Component;
56 import org.openecomp.sdc.be.model.ComponentInstance;
57 import org.openecomp.sdc.be.model.ComponentInstanceInterface;
58 import org.openecomp.sdc.be.model.GroupInstance;
59 import org.openecomp.sdc.be.model.Operation;
60 import org.openecomp.sdc.be.model.PropertyDefinition;
61 import org.openecomp.sdc.be.model.Resource;
62 import org.openecomp.sdc.be.model.Service;
63 import org.openecomp.sdc.be.model.category.CategoryDefinition;
64 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
65 import org.openecomp.sdc.be.plugins.ServiceCreationPlugin;
66 import org.openecomp.sdc.be.resources.data.auditing.AuditingActionEnum;
67 import org.openecomp.sdc.be.resources.data.auditing.DistributionDeployEvent;
68 import org.openecomp.sdc.be.resources.data.auditing.ResourceAdminEvent;
69 import org.openecomp.sdc.be.types.ServiceConsumptionData;
70 import org.openecomp.sdc.be.user.Role;
71 import org.openecomp.sdc.common.util.ValidationUtils;
72 import org.openecomp.sdc.exception.ResponseFormat;
73 import org.springframework.http.HttpStatus;
75 class ServiceBusinessLogicTest extends ServiceBusinessLogicBaseTestSetup {
77 private final static String DEFAULT_ICON = "defaulticon";
78 private static final String ALREADY_EXIST = "alreadyExist";
81 void testGetComponentAuditRecordsCertifiedVersion() {
82 Either<List<Map<String, Object>>, ResponseFormat> componentAuditRecords = bl.getComponentAuditRecords(CERTIFIED_VERSION, COMPONNET_ID, user.getUserId());
83 assertTrue(componentAuditRecords.isLeft());
84 assertEquals(3, componentAuditRecords.left().value().size());
88 void testGetComponentAuditRecordsUnCertifiedVersion() {
89 Either<List<Map<String, Object>>, ResponseFormat> componentAuditRecords = bl.getComponentAuditRecords(UNCERTIFIED_VERSION, COMPONNET_ID, user.getUserId());
90 assertTrue(componentAuditRecords.isLeft());
91 assertEquals(4, componentAuditRecords.left().value().size());
95 void testHappyScenario() {
96 Service service = createServiceObject(false);
97 when(genericTypeBusinessLogic.fetchDerivedFromGenericType(service, null)).thenReturn(Either.left(genericService));
98 Either<Service, ResponseFormat> createResponse = bl.createService(service, user);
100 if (createResponse.isRight()) {
101 assertEquals(new Integer(200), createResponse.right().value().getStatus());
103 assertEqualsServiceObject(createServiceObject(true), createResponse.left().value());
107 void testServiceCreationPluginCall() {
108 final Service service = createServiceObject(false);
109 when(genericTypeBusinessLogic.fetchDerivedFromGenericType(service, null)).thenReturn(Either.left(genericService));
110 final List<ServiceCreationPlugin> serviceCreationPlugins = new ArrayList<>();
111 serviceCreationPlugins.add(new ServiceCreationPlugin() {
113 public void beforeCreate(Service service) {
118 public int getOrder() {
122 serviceCreationPlugins.add(new ServiceCreationPlugin() {
124 public void beforeCreate(Service service) {
125 throw new RuntimeException();
129 public int getOrder() {
133 bl.setServiceCreationPluginList(serviceCreationPlugins);
134 final Either<Service, ResponseFormat> createResponse = bl.createService(service, user);
135 assertTrue(createResponse.isLeft());
140 void testCreateServiceWhenGenericTypeHasProperties() {
141 final Service service = createServiceObject(false);
143 final Resource genericTypeResource = mockGenericTypeResource();
145 when(genericTypeBusinessLogic.fetchDerivedFromGenericType(service, null)).thenReturn(Either.left(genericTypeResource));
146 final Service expectedService = createServiceObject(true);
147 expectedService.setProperties(mockPropertyList());
148 when(toscaOperationFacade.createToscaComponent(service)).thenReturn(Either.left(expectedService));
149 Either<Service, ResponseFormat> createResponse = bl.createService(service, user);
151 org.hamcrest.MatcherAssert.assertThat("Service creation should be successful",
152 createResponse.isLeft(), is(true));
153 final Service actualService = createResponse.left().value();
154 org.hamcrest.MatcherAssert.assertThat("Service should not be null", service, is(notNullValue()));
156 assertEqualsServiceObject(expectedService, actualService);
160 void testCreateServiceWhenGenericTypeAndServiceHasProperties() {
161 final Service service = createServiceObject(false);
162 service.setProperties(mockPropertyList());
163 service.getProperties().remove(0);
164 final PropertyDefinition serviceProperty = new PropertyDefinition();
165 serviceProperty.setName("aServiceProperty");
166 service.getProperties().add(serviceProperty);
168 final Resource genericTypeResource = mockGenericTypeResource();
170 when(genericTypeBusinessLogic.fetchDerivedFromGenericType(service, null)).thenReturn(Either.left(genericTypeResource));
171 final Service expectedService = createServiceObject(true);
172 expectedService.setProperties(mockPropertyList());
173 expectedService.getProperties().add(serviceProperty);
174 when(toscaOperationFacade.createToscaComponent(service)).thenReturn(Either.left(expectedService));
175 Either<Service, ResponseFormat> createResponse = bl.createService(service, user);
177 org.hamcrest.MatcherAssert.assertThat("Service creation should be successful",
178 createResponse.isLeft(), is(true));
179 final Service actualService = createResponse.left().value();
180 org.hamcrest.MatcherAssert.assertThat("Service should not be null", service, is(notNullValue()));
183 assertEqualsServiceObject(expectedService, actualService);
187 void testHappyScenarioCRNullProjectCode() {
188 Service service = createServiceObject(false);
189 service.setProjectCode(null);
190 when(genericTypeBusinessLogic.fetchDerivedFromGenericType(service, null)).thenReturn(Either.left(genericService));
191 Either<Service, ResponseFormat> createResponse = bl.createService(service, user);
193 if (createResponse.isRight()) {
194 assertEquals(new Integer(200), createResponse.right().value().getStatus());
196 assertEqualsServiceObject(createServiceObject(true), createResponse.left().value());
200 void testHappyScenarioCREmptyStringProjectCode() {
201 createServiceValidator();
202 Service service = createServiceObject(false);
203 service.setProjectCode("");
204 when(genericTypeBusinessLogic.fetchDerivedFromGenericType(service, null)).thenReturn(Either.left(genericService));
205 Either<Service, ResponseFormat> createResponse = bl.createService(service, user);
207 if (createResponse.isRight()) {
208 assertEquals(new Integer(200), createResponse.right().value().getStatus());
210 assertEqualsServiceObject(createServiceObject(true), createResponse.left().value());
212 private void assertEqualsServiceObject(final Service expectedService, final Service actualService) {
213 assertEquals(expectedService.getContactId(), actualService.getContactId());
214 assertEquals(expectedService.getCategories(), actualService.getCategories());
215 assertEquals(expectedService.getCreatorUserId(), actualService.getCreatorUserId());
216 assertEquals(expectedService.getCreatorFullName(), actualService.getCreatorFullName());
217 assertEquals(expectedService.getDescription(), actualService.getDescription());
218 assertEquals(expectedService.getIcon(), actualService.getIcon());
219 assertEquals(expectedService.getLastUpdaterUserId(), actualService.getLastUpdaterUserId());
220 assertEquals(expectedService.getLastUpdaterFullName(), actualService.getLastUpdaterFullName());
221 assertEquals(expectedService.getName(), actualService.getName());
222 assertEquals(expectedService.getUniqueId(), actualService.getUniqueId());
223 assertEquals(expectedService.getVersion(), actualService.getVersion());
224 assertEquals(expectedService.getArtifacts(), actualService.getArtifacts());
225 assertEquals(expectedService.getCreationDate(), actualService.getCreationDate());
226 assertEquals(expectedService.getLastUpdateDate(), actualService.getLastUpdateDate());
227 assertEquals(expectedService.getLifecycleState(), actualService.getLifecycleState());
228 assertEquals(expectedService.getTags(), actualService.getTags());
229 if (expectedService.getProperties() == null) {
230 org.hamcrest.MatcherAssert.assertThat("Service properties should be null",
231 actualService.getProperties(), is(nullValue()));
234 org.hamcrest.MatcherAssert.assertThat("Service properties should be as expected",
235 actualService.getProperties(), is(expectedService.getProperties()));
240 /* CREATE validations - start ***********************/
241 // Service name - start
244 void testFailedServiceValidations() {
246 testServiceNameAlreadyExists();
247 testServiceNameEmpty();
248 testServiceNameWrongFormat();
249 testServiceDescriptionEmpty();
250 testServiceDescriptionMissing();
251 testServiceDescExceedsLimitCreate();
252 testServiceDescNotEnglish();
253 testServiceIconEmpty();
254 testServiceIconMissing();
255 testResourceIconInvalid();
256 testTagsNoServiceName();
258 testServiceTagNotExist();
259 testServiceTagEmpty();
261 testContactIdTooLong();
262 testContactIdWrongFormatCreate();
263 testInvalidProjectCode();
264 testProjectCodeTooLong();
265 testProjectCodeTooShort();
267 testResourceContactIdMissing();
268 testServiceCategoryExist();
269 testServiceBadCategoryCreate();
272 private void testServiceNameAlreadyExists() {
273 String serviceName = ALREADY_EXIST;
274 Service serviceExccedsNameLimit = createServiceObject(false);
275 // 51 chars, the limit is 50
276 serviceExccedsNameLimit.setName(serviceName);
277 List<String> tgs = new ArrayList<>();
278 tgs.add(serviceName);
279 serviceExccedsNameLimit.setTags(tgs);
281 bl.createService(serviceExccedsNameLimit, user);
282 } catch (ComponentException exp) {
283 assertResponse(exp.getResponseFormat(), ActionStatus.COMPONENT_NAME_ALREADY_EXIST, ComponentTypeEnum.SERVICE.getValue(), serviceName);
289 private void testServiceNameEmpty() {
290 Service serviceExccedsNameLimit = createServiceObject(false);
291 serviceExccedsNameLimit.setName(null);
293 bl.createService(serviceExccedsNameLimit, user);
294 } catch(ComponentException e){
295 assertComponentException(e, ActionStatus.MISSING_COMPONENT_NAME, ComponentTypeEnum.SERVICE.getValue());
301 private void testServiceNameWrongFormat() {
302 Service service = createServiceObject(false);
304 String nameWrongFormat = "ljg\\fd";
305 service.setName(nameWrongFormat);
307 bl.createService(service, user);
308 } catch(ComponentException e){
309 assertComponentException(e, ActionStatus.INVALID_COMPONENT_NAME, ComponentTypeEnum.SERVICE.getValue());
315 // Service name - end
316 // Service description - start
318 private void testServiceDescriptionEmpty() {
319 Service serviceExist = createServiceObject(false);
320 serviceExist.setDescription("");
322 bl.createService(serviceExist, user);
323 } catch(ComponentException e){
324 assertComponentException(e, ActionStatus.COMPONENT_MISSING_DESCRIPTION, ComponentTypeEnum.SERVICE.getValue());
329 private void testServiceDescriptionMissing() {
330 Service serviceExist = createServiceObject(false);
331 serviceExist.setDescription(null);
333 bl.createService(serviceExist, user);
334 } catch(ComponentException e){
335 assertComponentException(e, ActionStatus.COMPONENT_MISSING_DESCRIPTION, ComponentTypeEnum.SERVICE.getValue());
341 private void testServiceDescExceedsLimitCreate() {
342 Service serviceExccedsDescLimit = createServiceObject(false);
343 // 1025 chars, the limit is 1024
344 String tooLongServiceDesc = "1GUODojQ0sGzKR4NP7e5j82ADQ3KHTVOaezL95qcbuaqDtjZhAQGQ3iFwKAy580K4WiiXs3u3zq7RzXcSASl5fm0RsWtCMOIDP"
345 + "AOf9Tf2xtXxPCuCIMCR5wOGnNTaFxgnJEHAGxilBhZDgeMNHmCN1rMK5B5IRJOnZxcpcL1NeG3APTCIMP1lNAxngYulDm9heFSBc8TfXAADq7703AvkJT0QPpGq2z2P"
346 + "tlikcAnIjmWgfC5Tm7UH462BAlTyHg4ExnPPL4AO8c92VrD7kZSgSqiy73cN3gLT8uigkKrUgXQFGVUFrXVyyQXYtVM6bLBeuCGQf4C2j8lkNg6M0J3PC0PzMRoinOxk"
347 + "Ae2teeCtVcIj4A1KQo3210j8q2v7qQU69Mabsa6DT9FgE4rcrbiFWrg0Zto4SXWD3o1eJA9o29lTg6kxtklH3TuZTmpi5KVp1NFhS1RpnqF83tzv4mZLKsx7Zh1fEgYvRFwx1"
348 + "ar3RolyDfNoZiGBGTMsZzz7RPFBf2hTnLmNqVGQnHKhhGj0Y5s8t2cbqbO2nmHiJb9uaUVrCGypgbAcJL3KPOBfAVW8PcpmNj4yVjI3L4x5zHjmGZbp9vKshEQODcrmcgsYAoKqe"
349 + "uu5u7jk8XVxEfQ0m5qL8UOErXPlJovSmKUmP5B5T0w299zIWDYCzSoNasHpHjOMDLAiDDeHbozUOn9t3Qou00e9POq4RMM0VnIx1H38nJoJZz2XH8CI5YMQe7oTagaxgQTF2aa0qaq2"
350 + "V6nJsfRGRklGjNhFFYP2cS4Xv2IJO9DSX6LTXOmENrGVJJvMOZcvnBaZPfoAHN0LU4i1SoepLzulIxnZBfkUWFJgZ5wQ0Bco2GC1HMqzW21rwy4XHRxXpXbmW8LVyoA1KbnmVmROycU4"
351 + "scTZ62IxIcIWCVeMjBIcTviXULbPUyqlfEPXWr8IMJtpAaELWgyquPClAREMDs2b9ztKmUeXlMccFES1XWbFTrhBHhmmDyVReEgCwfokrUFR13LTUK1k8I6OEHOs";
353 serviceExccedsDescLimit.setDescription(tooLongServiceDesc);
355 bl.createService(serviceExccedsDescLimit, user);
356 } catch(ComponentException e){
357 assertComponentException(e, ActionStatus.COMPONENT_DESCRIPTION_EXCEEDS_LIMIT, ComponentTypeEnum.SERVICE.getValue(), "" + ValidationUtils.COMPONENT_DESCRIPTION_MAX_LENGTH);
363 private void testServiceDescNotEnglish() {
364 Service notEnglish = createServiceObject(false);
366 String tooLongServiceDesc = "\uC2B5";
367 notEnglish.setDescription(tooLongServiceDesc);
369 bl.createService(notEnglish, user);
370 } catch(ComponentException e){
371 assertComponentException(e, ActionStatus.COMPONENT_INVALID_DESCRIPTION, ComponentTypeEnum.SERVICE.getValue());
377 // Service description - stop
378 // Service icon - start
380 private void testServiceIconEmpty() {
381 Service serviceExist = createServiceObject(false);
382 serviceExist.setIcon("");
383 Either<Service, ResponseFormat> service = bl.validateServiceBeforeCreate(serviceExist,user,AuditingActionEnum.CREATE_SERVICE);
384 assertThat(service.left().value().getIcon()).isEqualTo(DEFAULT_ICON);
387 private void testServiceIconMissing() {
388 Service serviceExist = createServiceObject(false);
389 serviceExist.setIcon(null);
390 Either<Service, ResponseFormat> service = bl.validateServiceBeforeCreate(serviceExist,user,AuditingActionEnum.CREATE_SERVICE);
391 assertThat(service.left().value().getIcon()).isEqualTo(DEFAULT_ICON);
394 private void testResourceIconInvalid() {
395 Service resourceExist = createServiceObject(false);
396 resourceExist.setIcon("kjk3453^&");
398 Either<Service, ResponseFormat> service = bl.validateServiceBeforeCreate(resourceExist, user, AuditingActionEnum.CREATE_RESOURCE);
399 assertThat(service.left().value().getIcon()).isEqualTo(DEFAULT_ICON);
403 private void testTagsNoServiceName() {
404 Service serviceExccedsNameLimit = createServiceObject(false);
405 String tag1 = "afzs2qLBb";
406 List<String> tagsList = new ArrayList<>();
408 serviceExccedsNameLimit.setTags(tagsList);
410 bl.createService(serviceExccedsNameLimit, user);
411 } catch(ComponentException e) {
412 assertComponentException(e, ActionStatus.COMPONENT_INVALID_TAGS_NO_COMP_NAME);
418 private void testInvalidTag() {
419 Service serviceExccedsNameLimit = createServiceObject(false);
420 String tag1 = "afzs2qLBb%#%";
421 List<String> tagsList = new ArrayList<>();
423 serviceExccedsNameLimit.setTags(tagsList);
425 bl.createService(serviceExccedsNameLimit, user);
426 } catch(ComponentException e) {
427 assertComponentException(e, ActionStatus.INVALID_FIELD_FORMAT, "Service", "tag");
433 private void testServiceTagNotExist() {
434 Service serviceExist = createServiceObject(false);
435 serviceExist.setTags(null);
437 Either<Service, ResponseFormat> service = bl.validateServiceBeforeCreate(serviceExist, user, AuditingActionEnum.CREATE_RESOURCE);
438 assertThat(service.left().value().getTags().get(0)).isEqualTo(serviceExist.getName());
441 private void testServiceTagEmpty() {
442 Service serviceExist = createServiceObject(false);
443 serviceExist.setTags(new ArrayList<>());
445 Either<Service, ResponseFormat> service = bl.validateServiceBeforeCreate(serviceExist, user, AuditingActionEnum.CREATE_RESOURCE);
446 assertThat(service.left().value().getTags().get(0)).isEqualTo(serviceExist.getName());
449 // Service tags - stop
450 // Service contactId - start
452 private void testContactIdTooLong() {
453 Service serviceContactId = createServiceObject(false);
454 // 59 chars instead of 50
455 String contactIdTooLong = "thisNameIsVeryLongAndExeccedsTheNormalLengthForContactId";
456 serviceContactId.setContactId(contactIdTooLong);
458 bl.createService(serviceContactId, user);
459 } catch(ComponentException e) {
460 assertComponentException(e, ActionStatus.COMPONENT_INVALID_CONTACT, ComponentTypeEnum.SERVICE.getValue());
465 private void testContactIdWrongFormatCreate() {
466 Service serviceContactId = createServiceObject(false);
467 // 3 letters and 3 digits and special characters
468 String contactIdTooLong = "yrt134!!!";
469 serviceContactId.setContactId(contactIdTooLong);
471 bl.createService(serviceContactId, user);
472 } catch(ComponentException e) {
473 assertComponentException(e, ActionStatus.COMPONENT_INVALID_CONTACT, ComponentTypeEnum.SERVICE.getValue());
479 private void testResourceContactIdMissing() {
480 Service resourceExist = createServiceObject(false);
481 resourceExist.setContactId(null);
483 bl.createService(resourceExist, user);
484 } catch(ComponentException e) {
485 assertComponentException(e, ActionStatus.COMPONENT_MISSING_CONTACT, ComponentTypeEnum.SERVICE.getValue());
491 // Service contactId - stop
492 // Service category - start
494 private void testServiceCategoryExist() {
495 Service serviceExist = createServiceObject(false);
496 serviceExist.setCategories(null);
498 bl.createService(serviceExist, user);
499 } catch(ComponentException e) {
500 assertComponentException(e, ActionStatus.COMPONENT_MISSING_CATEGORY, ComponentTypeEnum.SERVICE.getValue());
506 void markDistributionAsDeployedTestAlreadyDeployed() {
507 String notifyAction = "DNotify";
508 String requestAction = "DRequest";
509 String resultAction = "DResult";
510 String did = "123456";
512 setupBeforeDeploy(notifyAction, requestAction, did);
513 List<DistributionDeployEvent> resultList = new ArrayList<>();
514 Map<String, Object> params = new HashMap<>();
515 DistributionDeployEvent event = new DistributionDeployEvent();
517 event.setAction(resultAction);
519 event.setStatus("200");
520 // ESTimeBasedEvent deployEvent = new ESTimeBasedEvent();
521 // deployEvent.setFields(params);
522 resultList.add(event);
523 Either<List<DistributionDeployEvent>, ActionStatus> eventList = Either.left(resultList);
525 Mockito.when(auditingDao.getDistributionDeployByStatus(Mockito.anyString(), Mockito.eq(resultAction), Mockito.anyString())).thenReturn(eventList);
527 Either<Service, ResponseFormat> markDeployed = bl.markDistributionAsDeployed(did, did, user);
528 assertTrue(markDeployed.isLeft());
530 Mockito.verify(auditingDao, Mockito.times(0)).getDistributionRequest(did, requestAction);
535 void markDistributionAsDeployedTestSuccess() {
536 String notifyAction = "DNotify";
537 String requestAction = "DRequest";
538 String did = "123456";
540 setupBeforeDeploy(notifyAction, requestAction, did);
541 List<Role> roles = new ArrayList<>();
542 roles.add(Role.ADMIN);
543 roles.add(Role.DESIGNER);
544 Either<Service, ResponseFormat> markDeployed = bl.markDistributionAsDeployed(did, did, user);
545 assertTrue(markDeployed.isLeft());
549 void markDistributionAsDeployedTestNotDistributed() {
550 String notifyAction = "DNotify";
551 String requestAction = "DRequest";
552 String did = "123456";
554 setupBeforeDeploy(notifyAction, requestAction, did);
555 List<ResourceAdminEvent> emptyList = new ArrayList<>();
556 Either<List<ResourceAdminEvent>, ActionStatus> emptyEventList = Either.left(emptyList);
557 Mockito.when(auditingDao.getDistributionRequest(Mockito.anyString(), Mockito.eq(requestAction))).thenReturn(emptyEventList);
559 Either<Component, StorageOperationStatus> notFound = Either.right(StorageOperationStatus.NOT_FOUND);
560 Mockito.when(toscaOperationFacade.getToscaElement(did)).thenReturn(notFound);
562 Either<Service, ResponseFormat> markDeployed = bl.markDistributionAsDeployed(did, did, user);
563 assertTrue(markDeployed.isRight());
564 assertEquals(404, markDeployed.right().value().getStatus().intValue());
568 private void testServiceBadCategoryCreate() {
570 Service serviceExist = createServiceObject(false);
571 CategoryDefinition category = new CategoryDefinition();
572 category.setName("koko");
573 category.setIcons(Arrays.asList(DEFAULT_ICON));
574 List<CategoryDefinition> categories = new ArrayList<>();
575 categories.add(category);
576 serviceExist.setCategories(categories);
578 bl.createService(serviceExist, user);
579 } catch(ComponentException e) {
580 assertComponentException(e, ActionStatus.COMPONENT_INVALID_CATEGORY, ComponentTypeEnum.SERVICE.getValue());
586 // Service category - stop
587 // Service projectCode - start
589 private void testInvalidProjectCode() {
591 Service serviceExist = createServiceObject(false);
592 serviceExist.setProjectCode("koko!!");
595 bl.createService(serviceExist, user);
596 } catch(ComponentException exp) {
597 assertComponentException(exp, ActionStatus.INVALID_PROJECT_CODE);
603 private void testProjectCodeTooLong() {
605 Service serviceExist = createServiceObject(false);
606 String tooLongProjectCode = "thisNameIsVeryLongAndExeccedsTheNormalLengthForProjectCode";
607 serviceExist.setProjectCode(tooLongProjectCode);
610 bl.createService(serviceExist, user);
611 } catch(ComponentException exp) {
612 assertComponentException(exp, ActionStatus.INVALID_PROJECT_CODE);
619 private void testProjectCodeTooShort() {
621 Service serviceExist = createServiceObject(false);
622 serviceExist.setProjectCode("333");
625 bl.createService(serviceExist, user);
626 } catch(ComponentException exp) {
627 assertComponentException(exp, ActionStatus.INVALID_PROJECT_CODE);
634 void testDeleteMarkedServices() {
635 List<String> ids = new ArrayList<>();
636 List<String> responseIds = new ArrayList<>();
637 String resourceInUse = "123";
638 ids.add(resourceInUse);
639 String resourceFree = "456";
640 ids.add(resourceFree);
641 responseIds.add(resourceFree);
642 Either<List<String>, StorageOperationStatus> eitherNoResources = Either.left(ids);
643 when(toscaOperationFacade.getAllComponentsMarkedForDeletion(ComponentTypeEnum.RESOURCE)).thenReturn(eitherNoResources);
645 Either<Boolean, StorageOperationStatus> resourceInUseResponse = Either.left(true);
646 Either<Boolean, StorageOperationStatus> resourceFreeResponse = Either.left(false);
648 List<ArtifactDefinition> artifacts = new ArrayList<>();
649 Either<List<ArtifactDefinition>, StorageOperationStatus> getArtifactsResponse = Either.left(artifacts);
651 Either<Component, StorageOperationStatus> eitherDelete = Either.left(new Resource());
652 when(toscaOperationFacade.deleteToscaComponent(resourceFree)).thenReturn(eitherDelete);
653 when(toscaOperationFacade.deleteMarkedElements(ComponentTypeEnum.SERVICE)).thenReturn(Either.left(responseIds));
654 Either<List<String>, ResponseFormat> deleteMarkedResources = bl.deleteMarkedComponents();
655 assertTrue(deleteMarkedResources.isLeft());
656 List<String> resourceIdList = deleteMarkedResources.left().value();
657 assertFalse(resourceIdList.isEmpty());
658 assertTrue(resourceIdList.contains(resourceFree));
659 assertFalse(resourceIdList.contains(resourceInUse));
663 @SuppressWarnings({ "unchecked", "rawtypes" })
665 void testFindGroupInstanceOnRelatedComponentInstance() {
667 Class<ServiceBusinessLogic> targetClass = ServiceBusinessLogic.class;
668 String methodName = "findGroupInstanceOnRelatedComponentInstance";
669 Object invalidId = "invalidId";
671 Component service = createNewService();
672 List<ComponentInstance> componentInstances = service.getComponentInstances();
674 Either<ImmutablePair<ComponentInstance, GroupInstance>, ResponseFormat> findGroupInstanceRes;
675 Object[] argObjects = {service, componentInstances.get(1).getUniqueId(), componentInstances.get(1).getGroupInstances().get(1).getUniqueId()};
676 Class[] argClasses = {Component.class, String.class,String.class};
678 Method method = targetClass.getDeclaredMethod(methodName, argClasses);
679 method.setAccessible(true);
681 findGroupInstanceRes = (Either<ImmutablePair<ComponentInstance, GroupInstance>, ResponseFormat>) method.invoke(bl, argObjects);
682 assertNotNull(findGroupInstanceRes);
683 assertEquals(findGroupInstanceRes.left().value().getKey().getUniqueId(), componentInstances.get(1)
685 assertEquals(findGroupInstanceRes.left().value().getValue().getUniqueId(), componentInstances.get(1)
690 Object[] argObjectsInvalidCiId = {service, invalidId , componentInstances.get(1).getGroupInstances().get(1).getUniqueId()};
692 findGroupInstanceRes = (Either<ImmutablePair<ComponentInstance, GroupInstance>, ResponseFormat>) method.invoke(bl, argObjectsInvalidCiId);
693 assertNotNull(findGroupInstanceRes);
694 assertTrue(findGroupInstanceRes.isRight());
695 assertEquals("SVC4593", findGroupInstanceRes.right().value().getMessageId());
697 Object[] argObjectsInvalidGiId = {service, componentInstances.get(1).getUniqueId() , invalidId};
699 findGroupInstanceRes = (Either<ImmutablePair<ComponentInstance, GroupInstance>, ResponseFormat>) method.invoke(bl, argObjectsInvalidGiId);
700 assertNotNull(findGroupInstanceRes);
701 assertTrue(findGroupInstanceRes.isRight());
702 assertEquals("SVC4653", findGroupInstanceRes.right().value().getMessageId());
704 catch (Exception e) {
709 private Component createNewComponent() {
711 Service service = new Service();
713 service.setName("serviceName");
714 service.setUniqueId("serviceUniqueId");
715 List<ComponentInstance> componentInstances = new ArrayList<>();
716 ComponentInstance ci;
717 for(int i= 0; i<listSize; ++i){
718 ci = new ComponentInstance();
719 ci.setName("ciName" + i);
720 ci.setUniqueId("ciId" + i);
721 List<GroupInstance> groupInstances= new ArrayList<>();
723 for(int j = 0; j<listSize; ++j){
724 gi = new GroupInstance();
725 gi.setName(ci.getName( )+ "giName" + j);
726 gi.setUniqueId(ci.getName() + "giId" + j);
727 groupInstances.add(gi);
729 ci.setGroupInstances(groupInstances);
730 componentInstances.add(ci);
732 service.setComponentInstances(componentInstances);
736 protected Service createNewService() {
737 return (Service)createNewComponent();
741 void testDerivedFromGeneric() {
742 Service service = createServiceObject(true);
743 service.setDerivedFromGenericInfo(genericService);
744 when(toscaOperationFacade.createToscaComponent(service)).thenReturn(Either.left(service));
745 when(genericTypeBusinessLogic.fetchDerivedFromGenericType(service, null)).thenReturn(Either.left(genericService));
746 Either<Service, ResponseFormat> createResponse = bl.createService(service, user);
747 assertTrue(createResponse.isLeft());
748 service = createResponse.left().value();
749 assertEquals(genericService.getToscaResourceName(), service.getDerivedFromGenericType());
750 assertEquals(genericService.getVersion(), service.getDerivedFromGenericVersion());
754 void testServiceWithoutDerivedFromGeneric() {
755 final Service service = createServiceObject(true);
756 when(toscaOperationFacade.createToscaComponent(service)).thenReturn(Either.left(service));
757 when(genericTypeBusinessLogic.fetchDerivedFromGenericType(service, null)).thenReturn(Either.left(genericService));
758 final Either<Service, ResponseFormat> createResponse = bl.createService(service, user);
759 assertTrue(createResponse.isLeft());
760 final Service actualService = createResponse.left().value();
761 assertNull(actualService.getDerivedFromGenericType());
762 assertNull(actualService.getDerivedFromGenericVersion());
766 void testUpdateMetadataNamingPolicy() {
767 Service currentService = createServiceObject(true);
768 Service newService = createServiceObject(false);
769 currentService.setEcompGeneratedNaming(false);
770 newService.setEcompGeneratedNaming(true);
771 newService.setNamingPolicy("policy");
772 Either<Service, ResponseFormat> resultOfUpdate = bl.validateAndUpdateServiceMetadata(user, currentService, newService);
773 assertThat(resultOfUpdate.isLeft()).isTrue();
774 Service updatedService = resultOfUpdate.left().value();
775 assertThat(updatedService.isEcompGeneratedNaming()).isTrue();
776 assertThat(updatedService.getNamingPolicy()).isEqualToIgnoringCase("policy");
780 void testUpdateMetadataToEmptyProjectCode() {
781 Service currentService = createServiceObject(true);
782 Service newService = createServiceObject(false);
783 currentService.setProjectCode("12345");
784 newService.setProjectCode("");
785 Either<Service, ResponseFormat> resultOfUpdate = bl.validateAndUpdateServiceMetadata(user, currentService, newService);
786 assertThat(resultOfUpdate.isLeft()).isTrue();
787 Service updatedService = resultOfUpdate.left().value();
788 assertThat(updatedService.getProjectCode()).isEmpty();
792 void testUpdateMetadataFromEmptyProjectCode() {
793 Service currentService = createServiceObject(true);
794 Service newService = createServiceObject(false);
795 currentService.setProjectCode("");
796 newService.setProjectCode("12345");
797 Either<Service, ResponseFormat> resultOfUpdate = bl.validateAndUpdateServiceMetadata(user, currentService, newService);
798 assertThat(resultOfUpdate.isLeft()).isTrue();
799 Service updatedService = resultOfUpdate.left().value();
800 assertThat(updatedService.getProjectCode()).isEqualTo("12345");
804 void testUpdateMetadataProjectCode() {
805 Service currentService = createServiceObject(true);
806 Service newService = createServiceObject(false);
807 currentService.setProjectCode("33333");
808 newService.setProjectCode("12345");
809 Either<Service, ResponseFormat> resultOfUpdate = bl.validateAndUpdateServiceMetadata(user, currentService, newService);
810 assertThat(resultOfUpdate.isLeft()).isTrue();
811 Service updatedService = resultOfUpdate.left().value();
812 assertThat(updatedService.getProjectCode()).isEqualTo("12345");
816 void testUpdateMetadataServiceType() {
817 Service currentService = createServiceObject(true);
818 Service newService = createServiceObject(false);
819 currentService.setServiceType("alice");
821 newService.setServiceType("bob");
822 Either<Service, ResponseFormat> resultOfUpdate = bl.validateAndUpdateServiceMetadata(user, currentService, newService);
823 assertThat(resultOfUpdate.isLeft()).isTrue();
824 Service updatedService = resultOfUpdate.left().value();
825 assertThat(updatedService.getServiceType()).isEqualToIgnoringCase("bob");
826 //empty string is invalid
827 newService.setServiceType("");
828 resultOfUpdate = bl.validateAndUpdateServiceMetadata(user, currentService, newService);
829 assertThat(resultOfUpdate.isLeft()).isTrue();
831 newService.setServiceType(null);
832 resultOfUpdate = bl.validateAndUpdateServiceMetadata(user, currentService, newService);
833 assertThat(resultOfUpdate.isRight()).isTrue();
837 void testCreateDefaultMetadataServiceFunction() {
838 Service currentService = createServiceObject(true);
839 assertThat(currentService.getServiceFunction()).isEqualTo("");
843 void testCreateCustomMetadataServiceFunction() {
844 String customServiceFunctionName = "customName";
845 Service currentService = createServiceObject(true);
846 currentService.setServiceFunction(customServiceFunctionName);
847 assertThat(currentService.getServiceFunction()).isEqualTo(customServiceFunctionName);
851 void testUpdateMetadataServiceFunction() {
852 Service currentService = createServiceObject(true);
853 Service newService = createServiceObject(false);
854 currentService.setServiceFunction("alice");
856 newService.setServiceFunction("bob");
857 Either<Service, ResponseFormat> resultOfUpdate = bl.validateAndUpdateServiceMetadata(user, currentService, newService);
858 assertThat(resultOfUpdate.isLeft()).isTrue();
859 Service updatedService = resultOfUpdate.left().value();
860 assertThat(updatedService.getServiceFunction()).isEqualToIgnoringCase("bob");
861 //empty string is valid
862 newService.setServiceFunction("");
863 resultOfUpdate = bl.validateAndUpdateServiceMetadata(user, currentService, newService);
864 assertThat(resultOfUpdate.isLeft()).isTrue();
865 //null is valid and assigner to ""
866 newService.setServiceFunction(null);
867 resultOfUpdate = bl.validateAndUpdateServiceMetadata(user, currentService, newService);
868 assertThat(resultOfUpdate.isLeft()).isTrue();
869 assertThat(updatedService.getServiceFunction()).isEqualTo("");
875 void testServiceFunctionExceedLength() {
876 String serviceName = "Service";
877 String serviceFunction = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
878 Service serviceFunctionExceedLength = createServiceObject(false);
879 serviceFunctionExceedLength.setName(serviceName);
880 serviceFunctionExceedLength.setServiceFunction(serviceFunction);
881 List<String> tgs = new ArrayList<>();
882 tgs.add(serviceName);
883 serviceFunctionExceedLength.setTags(tgs);
885 serviceFunctionValidator.validateAndCorrectField(user, serviceFunctionExceedLength, AuditingActionEnum.CREATE_SERVICE);
886 } catch (ComponentException exp) {
887 assertResponse(exp.getResponseFormat(), ActionStatus.PROPERTY_EXCEEDS_LIMIT, SERVICE_FUNCTION);
892 void testServiceFunctionInvalidCharacter(){
893 String serviceName = "Service";
894 String serviceFunction = "a?";
895 Service serviceFunctionExceedLength = createServiceObject(false);
896 serviceFunctionExceedLength.setName(serviceName);
897 serviceFunctionExceedLength.setServiceFunction(serviceFunction);
898 List<String> tgs = new ArrayList<>();
899 tgs.add(serviceName);
900 serviceFunctionExceedLength.setTags(tgs);
902 serviceFunctionValidator.validateAndCorrectField(user, serviceFunctionExceedLength, AuditingActionEnum.CREATE_SERVICE);
903 } catch (ComponentException exp) {
904 assertResponse(exp.getResponseFormat(), ActionStatus.INVALID_PROPERY, SERVICE_FUNCTION);
909 void testAddPropertyServiceConsumptionServiceNotFound() {
910 Mockito.when(toscaOperationFacade.getToscaElement(Mockito.anyString())).thenReturn(Either.right(StorageOperationStatus.NOT_FOUND));
912 Either<Operation, ResponseFormat> operationEither =
913 bl.addPropertyServiceConsumption("1", "2", "3",
914 user.getUserId(), new ServiceConsumptionData());
915 assertTrue(operationEither.isRight());
916 assertEquals(HttpStatus.NOT_FOUND.value(), operationEither.right().value().getStatus().intValue());
920 void testAddPropertyServiceConsumptionParentServiceIsEmpty() {
921 Either<Component, StorageOperationStatus> eitherService = Either.left(createNewComponent());
922 Mockito.when(toscaOperationFacade.getToscaElement(Mockito.anyString())).thenReturn(eitherService);
924 Either<Operation, ResponseFormat> operationEither =
925 bl.addPropertyServiceConsumption("1", "2", "3",
926 user.getUserId(), new ServiceConsumptionData());
927 assertTrue(operationEither.isRight());
928 assertEquals(HttpStatus.NOT_FOUND.value(), operationEither.right().value().getStatus().intValue());
932 void testAddPropertyServiceConsumptionNoMatchingComponent() {
933 Service aService = createNewService();
934 Either<Component, StorageOperationStatus> eitherService = Either.left(aService);
935 Mockito.when(toscaOperationFacade.getToscaElement(Mockito.anyString())).thenReturn(eitherService);
937 String weirdUniqueServiceInstanceId = UUID.randomUUID().toString();
939 Either<Operation, ResponseFormat> operationEither =
940 bl.addPropertyServiceConsumption("1", weirdUniqueServiceInstanceId, "3",
941 user.getUserId(), new ServiceConsumptionData());
942 assertTrue(operationEither.isRight());
943 assertEquals(HttpStatus.NOT_FOUND.value(), operationEither.right().value().getStatus().intValue());
947 void testAddPropertyServiceConsumptionNotComponentInstancesInterfacesOnParentService() {
948 Service aService = createNewService();
949 aService.getComponentInstances().get(0).setUniqueId(aService.getUniqueId());
950 Either<Component, StorageOperationStatus> eitherService = Either.left(aService);
951 Mockito.when(toscaOperationFacade.getToscaElement(Mockito.anyString())).thenReturn(eitherService);
953 Either<Operation, ResponseFormat> operationEither =
954 bl.addPropertyServiceConsumption("1", aService.getUniqueId(), "3",
955 user.getUserId(), new ServiceConsumptionData());
956 assertTrue(operationEither.isRight());
957 assertEquals(HttpStatus.NOT_FOUND.value(), operationEither.right().value().getStatus().intValue());
961 void testAddPropertyServiceConsumptionInterfaceCandidateNotPresent() {
962 Service aService = createNewService();
963 aService.getComponentInstances().get(0).setUniqueId(aService.getUniqueId());
964 Either<Component, StorageOperationStatus> eitherService = Either.left(aService);
965 Mockito.when(toscaOperationFacade.getToscaElement(Mockito.anyString())).thenReturn(eitherService);
967 Map<String, List<ComponentInstanceInterface>> componentInstancesInterfacesMap =
969 componentInstancesInterfacesMap.put(aService.getUniqueId(),
970 Lists.newArrayList(new ComponentInstanceInterface("1", new InterfaceInstanceDataDefinition())));
972 aService.setComponentInstancesInterfaces(componentInstancesInterfacesMap);
974 Either<Operation, ResponseFormat> operationEither =
975 bl.addPropertyServiceConsumption("1", aService.getUniqueId(), "3",
976 user.getUserId(), new ServiceConsumptionData());
977 assertTrue(operationEither.isRight());
978 assertEquals(HttpStatus.NOT_FOUND.value(), operationEither.right().value().getStatus().intValue());
982 void testAddPropertyServiceConsumptionNoInputsCandidate() {
983 Service aService = createNewService();
984 aService.getComponentInstances().get(0).setUniqueId(aService.getUniqueId());
985 Either<Component, StorageOperationStatus> eitherService = Either.left(aService);
986 Mockito.when(toscaOperationFacade.getToscaElement(Mockito.anyString())).thenReturn(eitherService);
988 String operationId = "operationId";
989 ComponentInstanceInterface componentInstanceInterface =
990 new ComponentInstanceInterface("interfaceId", new InterfaceInstanceDataDefinition());
991 Map<String, Operation> operationsMap = Maps.newHashMap();
992 operationsMap.put(operationId, new Operation(new ArtifactDataDefinition(), "1",
993 new ListDataDefinition<>(), new ListDataDefinition<>()));
994 componentInstanceInterface.setOperationsMap(operationsMap);
996 Map<String, List<ComponentInstanceInterface>> componentInstancesInterfacesMap = Maps.newHashMap();
997 componentInstancesInterfacesMap.put(aService.getUniqueId(), Lists.newArrayList(componentInstanceInterface));
998 aService.setComponentInstancesInterfaces(componentInstancesInterfacesMap);
1000 Either<Operation, ResponseFormat> operationEither =
1001 bl.addPropertyServiceConsumption("1", aService.getUniqueId(), operationId,
1002 user.getUserId(), new ServiceConsumptionData());
1003 assertTrue(operationEither.isRight());
1004 assertEquals(HttpStatus.NOT_FOUND.value(), operationEither.right().value().getStatus().intValue());
1007 private Resource mockGenericTypeResource() {
1008 final Resource genericTypeResource = new Resource();
1009 genericTypeResource.setProperties(mockPropertyList());
1010 return genericTypeResource;
1013 private List<PropertyDefinition> mockPropertyList() {
1014 final List<PropertyDefinition> propertyList = new ArrayList<>();
1015 final PropertyDefinition propertyDefinition1 = new PropertyDefinition();
1016 propertyDefinition1.setName("property1");
1017 propertyDefinition1.setType("string");
1018 propertyList.add(propertyDefinition1);
1020 final PropertyDefinition propertyDefinition2 = new PropertyDefinition();
1021 propertyDefinition2.setName("property2");
1022 propertyDefinition2.setType("boolean");
1023 propertyList.add(propertyDefinition2);
1025 final PropertyDefinition propertyDefinition3 = new PropertyDefinition();
1026 propertyDefinition3.setName("property3");
1027 propertyDefinition3.setType("string");
1028 propertyList.add(propertyDefinition3);
1029 return propertyList;