Catalog alignment
[sdc.git] / catalog-ui / src / app / ng2 / pages / composition / graph / utils / composition-graph-nodes-utils.spec.ts
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';
23
24 describe('composition graph nodes utils', () => {
25
26     const CP_TO_DELETE_ID = 'cp1';
27     const VL_TO_DELETE_ID = 'vl';
28     const CP2_ID = 'cp2';
29
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,
39         isVl: () => false
40     });
41     const vlInstanceMock: ComponentInstance = Mock.of<ComponentInstance>({
42         uniqueId: VL_TO_DELETE_ID,
43         isVl: () => true
44     });
45     const cp2InstanceMock: ComponentInstance = Mock.of<ComponentInstance>({
46         uniqueId: CP2_ID,
47         isVl: () => false
48     });
49
50     const cyMock = Mock.of<Cy.Instance>({
51         remove: jest.fn(),
52         collection: jest.fn()
53     });
54
55     const serviceServiceMock = Mock.of<ServiceServiceNg2>({
56         getComponentCompositionData : () => Observable.of(Mock.of<ComponentGenericResponse>())
57     });
58
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 });
63
64     beforeEach(() => {
65
66         loaderServiceMock = {
67             activate: jest.fn(),
68             deactivate: jest.fn()
69         };
70
71         topologyServiceMock = Mock.of<TopologyTemplateService>({
72             deleteComponentInstance : () => Observable.of(cpInstanceMock)
73         });
74
75         queueServiceMock = Mock.of<QueueServiceUtils>({
76             addBlockingUIAction : ( (f) => f() )
77         });
78
79         workspaceServiceMock = Mock.of<WorkspaceService>({
80             metadata: Mock.of<ComponentMetadata>( { uniqueId: 'topologyTemplateUniqueId' } )
81         });
82
83         compositionServiceMock = Mock.of<CompositionService>({
84             deleteComponentInstance : jest.fn()
85         });
86
87         eventListenerServiceMock = Mock.of<EventListenerService>({
88             notifyObservers : jest.fn()
89         });
90
91         TestBed.configureTestingModule({
92             imports: [],
93             providers: [
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}
104             ]
105         });
106         service = TestBed.get(CompositionGraphNodesUtils);
107     });
108
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>({
112             data:  () => vl,
113             connectedEdges: () => Mock.of<CollectionEdges>({
114                 length: 2,
115                 connectedNodes: () => [cp, cp2] as CollectionNodes
116             })
117         });
118
119         // Prepare a CP which is connected to a VL
120         const cpToDelete = Mock.of<CollectionNodes>({
121             data:  () => cp,
122             connectedEdges: () => Mock.of<CollectionEdges>({
123                 length: 1,
124                 connectedNodes: () => [vlToDelete] as CollectionNodes
125             })
126         });
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();
132     });
133
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>({
137             data:  () => vl,
138             connectedEdges: () => Mock.of<CollectionEdges>({
139                 length: 1,
140                 connectedNodes: () => [cp] as CollectionNodes
141             })
142         });
143
144         // Prepare a CP which is connected to a VL
145         const cpToDelete = Mock.of<CollectionNodes>({
146             data:  () => cp,
147             connectedEdges: () => Mock.of<CollectionEdges>({
148                 length: 1,
149                 connectedNodes: () => [vlToDelete] as CollectionNodes
150             })
151         });
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();
157     });
158 });