1 import { TestBed } from '@angular/core/testing';
2 import { SdcUiServices } from 'onap-ui-angular';
3 import { Observable } from 'rxjs/Rx';
4 import CollectionNodes = Cy.CollectionNodes;
5 import { Mock } from 'ts-mockery';
6 import { ComponentInstance } from '../../../../../models';
7 import { ComponentMetadata } from '../../../../../models/component-metadata';
8 import { Resource } from '../../../../../models/components/resource';
9 import { CompositionCiNodeCp } from '../../../../../models/graph/nodes/composition-graph-nodes/composition-ci-node-cp';
10 import { CompositionCiNodeVl } from '../../../../../models/graph/nodes/composition-graph-nodes/composition-ci-node-vl';
11 import { EventListenerService } from '../../../../../services';
12 import CollectionEdges = Cy.CollectionEdges;
13 import { GRAPH_EVENTS } from '../../../../../utils/constants';
14 import { ServiceServiceNg2 } from '../../../../services/component-services/service.service';
15 import { TopologyTemplateService } from '../../../../services/component-services/topology-template.service';
16 import { ComponentGenericResponse } from '../../../../services/responses/component-generic-response';
17 import { QueueServiceUtils } from '../../../../utils/queue-service-utils';
18 import { WorkspaceService } from '../../../workspace/workspace.service';
19 import { CompositionService } from '../../composition.service';
20 import { CommonGraphUtils } from '../common/common-graph-utils';
21 import { CompositionGraphGeneralUtils } from './composition-graph-general-utils';
22 import { CompositionGraphNodesUtils } from './composition-graph-nodes-utils';
24 describe('composition graph nodes utils', () => {
26 const CP_TO_DELETE_ID = 'cp1';
27 const VL_TO_DELETE_ID = 'vl';
30 let loaderServiceMock: Partial<SdcUiServices.LoaderService>;
31 let service: CompositionGraphNodesUtils;
32 let topologyServiceMock: TopologyTemplateService;
33 let queueServiceMock: QueueServiceUtils;
34 let workspaceServiceMock: WorkspaceService;
35 let compositionServiceMock: CompositionService;
36 let eventListenerServiceMock: EventListenerService;
37 const cpInstanceMock: ComponentInstance = Mock.of<ComponentInstance>({
38 uniqueId: CP_TO_DELETE_ID,
41 const vlInstanceMock: ComponentInstance = Mock.of<ComponentInstance>({
42 uniqueId: VL_TO_DELETE_ID,
45 const cp2InstanceMock: ComponentInstance = Mock.of<ComponentInstance>({
50 const cyMock = Mock.of<Cy.Instance>({
55 const serviceServiceMock = Mock.of<ServiceServiceNg2>({
56 getComponentCompositionData : () => Observable.of(Mock.of<ComponentGenericResponse>())
59 // Instances on the graph cp, vl, cp2
60 const cp = Mock.from<CompositionCiNodeCp>({ id: CP_TO_DELETE_ID, componentInstance: cpInstanceMock });
61 const vl = Mock.from<CompositionCiNodeVl>({ id: VL_TO_DELETE_ID, componentInstance: vlInstanceMock });
62 const cp2 = Mock.from<CompositionCiNodeCp>({ id: CP2_ID, componentInstance: cp2InstanceMock });
71 topologyServiceMock = Mock.of<TopologyTemplateService>({
72 deleteComponentInstance : () => Observable.of(cpInstanceMock)
75 queueServiceMock = Mock.of<QueueServiceUtils>({
76 addBlockingUIAction : ( (f) => f() )
79 workspaceServiceMock = Mock.of<WorkspaceService>({
80 metadata: Mock.of<ComponentMetadata>( { uniqueId: 'topologyTemplateUniqueId' } )
83 compositionServiceMock = Mock.of<CompositionService>({
84 deleteComponentInstance : jest.fn()
87 eventListenerServiceMock = Mock.of<EventListenerService>({
88 notifyObservers : jest.fn()
91 TestBed.configureTestingModule({
94 CompositionGraphNodesUtils,
95 {provide: WorkspaceService, useValue: workspaceServiceMock},
96 {provide: TopologyTemplateService, useValue: topologyServiceMock},
97 {provide: CompositionService, useValue: compositionServiceMock},
98 {provide: CompositionGraphGeneralUtils, useValue: {}},
99 {provide: CommonGraphUtils, useValue: {}},
100 {provide: EventListenerService, useValue: eventListenerServiceMock},
101 {provide: QueueServiceUtils, useValue: queueServiceMock},
102 {provide: ServiceServiceNg2, useValue: serviceServiceMock},
103 {provide: SdcUiServices.LoaderService, useValue: loaderServiceMock}
106 service = TestBed.get(CompositionGraphNodesUtils);
109 it('When a CP is deleted which is connected to a VL that has another leg to another CP, the VL is deleted as well', () => {
110 // Prepare a VL that is connected to both CP and CP2
111 const vlToDelete = Mock.of<CollectionNodes>({
113 connectedEdges: () => Mock.of<CollectionEdges>({
115 connectedNodes: () => [cp, cp2] as CollectionNodes
119 // Prepare a CP which is connected to a VL
120 const cpToDelete = Mock.of<CollectionNodes>({
122 connectedEdges: () => Mock.of<CollectionEdges>({
124 connectedNodes: () => [vlToDelete] as CollectionNodes
127 service.deleteNode(cyMock, Mock.of<Resource>(), cpToDelete);
128 expect(compositionServiceMock.deleteComponentInstance).toHaveBeenCalledWith(CP_TO_DELETE_ID);
129 expect(eventListenerServiceMock.notifyObservers).toHaveBeenCalledWith(GRAPH_EVENTS.ON_DELETE_COMPONENT_INSTANCE, VL_TO_DELETE_ID);
130 expect(eventListenerServiceMock.notifyObservers).toHaveBeenLastCalledWith(GRAPH_EVENTS.ON_DELETE_COMPONENT_INSTANCE_SUCCESS, CP_TO_DELETE_ID);
131 expect(cyMock.remove).toHaveBeenCalled();
134 it('When a CP is deleted which is solely connected to another VL the VL is not deleted', () => {
135 // Prepare a VL that is connected only to 1 CP
136 const vlToDelete = Mock.of<CollectionNodes>({
138 connectedEdges: () => Mock.of<CollectionEdges>({
140 connectedNodes: () => [cp] as CollectionNodes
144 // Prepare a CP which is connected to a VL
145 const cpToDelete = Mock.of<CollectionNodes>({
147 connectedEdges: () => Mock.of<CollectionEdges>({
149 connectedNodes: () => [vlToDelete] as CollectionNodes
152 service.deleteNode(cyMock, Mock.of<Resource>(), cpToDelete);
153 expect(compositionServiceMock.deleteComponentInstance).toHaveBeenCalledWith(CP_TO_DELETE_ID);
154 expect(eventListenerServiceMock.notifyObservers).toHaveBeenLastCalledWith(GRAPH_EVENTS.ON_DELETE_COMPONENT_INSTANCE_SUCCESS, CP_TO_DELETE_ID);
155 expect(eventListenerServiceMock.notifyObservers).toHaveBeenCalledTimes(1);
156 expect(cyMock.remove).toHaveBeenCalled();