summaryrefslogtreecommitdiffstats
path: root/catalog-ui/src/app/ng2/pages/composition/graph/utils/match-capability-requierment-utils.spec.ts
blob: dbfc3e721952d9a6806ca2fbb9f6420ecbb119a4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { Mock } from 'ts-mockery';
import {
    CapabilitiesGroup,
    Capability, ComponentInstance, CompositionCiLinkBase, CompositionCiNodeBase, CompositionCiNodeCp,
    CompositionCiNodeVf, CompositionCiNodeVl,
    Requirement, RequirementsGroup
} from '../../../../../models';
import { MatchCapabilitiesRequirementsUtils } from './match-capability-requierment-utils';

describe('match capability requirements utils service ', () => {

    const bindableReq = Mock.of<Requirement>({
        capability : 'tosca.capabilities.network.Bindable',
        name: 'virtualBinding',
        relationship: 'tosca.relationships.network.BindsTo',
        uniqueId: 'eef99154-8039-4227-ba68-62a32e6b0d98.virtualBinding',
        ownerId : 'extcp0',
        ownerName : 's'
    });

    const virtualLinkReq = Mock.of<Requirement>({
        capability: 'tosca.capabilities.network.Linkable',
        name: 'virtualLink',
        relationship: 'tosca.relationships.network.LinksTo',
        uniqueId: 'eef99154-8039-4227-ba68-62a32e6b0d98.virtualLink',
        ownerId : '',
        ownerName : 's'
    });

    const storeAttachmentReq = Mock.of<Requirement>({
        capability: 'tosca.capabilities.Attachment',
        name: 'local_storage',
        relationship: 'tosca.relationships.AttachesTo',
        uniqueId: 'eef99154-8039-4227-ba68-62a32e6b0d98.local_storage',
        node: 'tosca.nodes.BlockStorage',
        ownerId : '',
        ownerName : 's'
    });

    const vlAttachmentReq = Mock.of<Requirement>({
        capability: 'tosca.capabilities.Attachment',
        name: 'local_storage',
        relationship: 'tosca.relationships.AttachesTo',
        uniqueId: 'eef99154-8039-4227-ba68-62a32e6b0d98.local_storage',
        node: 'tosca.nodes.BlockStorage',
        ownerId : '',
        ownerName : 's'
    });

    const extVirtualLinkReq = Mock.of<Requirement>({
        capability: 'tosca.capabilities.network.Linkable',
        name: 'external_virtualLink',
        relationship: 'tosca.relationships.network.LinksTo',
        uniqueId: 'eef99154-8039-4227-ba68-62a32e6b0d98.external_virtualLink'
    });

    const dependencyReq = Mock.of<Requirement>({
        capability: 'tosca.capabilities.Node',
        name: 'dependency',
        relationship: 'tosca.relationships.DependsOn',
        uniqueId: 'eef99154-8039-4227-ba68-62a32e6b0d98.dependency'
    });

    const featureCap = Mock.of<Capability>({
        type: 'tosca.capabilities.Node',
        name: 'feature',
        uniqueId: 'capability.ddf1301e-866b-4fa3-bc4f-edbd81e532cd.feature',
        maxOccurrences: 'UNBOUNDED',
        minOccurrences: '1'
    });

    const internalConnPointCap = Mock.of<Capability>({
        type: 'tosca.capabilities.Node',
        name: 'internal_connectionPoint',
        capabilitySources : ['org.openecomp.resource.cp.extCP'],
        uniqueId: 'capability.ddf1301e-866b-4fa3-bc4f-edbd81e532cd.internal_connectionPoint',
        maxOccurrences: 'UNBOUNDED',
        minOccurrences: '1'
    });

    const blockStoreAttachmentCap = Mock.of<Capability>({
        type: 'tosca.capabilities.Attachment',
        name: 'attachment',
        capabilitySources: ['tosca.nodes.BlockStorage'],
        uniqueId: 'capability.ddf1301e-866b-4fa3-bc4f-edbd81e532cd.attachment',
        maxOccurrences: 'UNBOUNDED',
        minOccurrences: '1'
    });

    const bindingCap = Mock.of<Capability>({
        type: 'tosca.capabilities.network.Bindable',
        name: 'binding',
        capabilitySources: ['tosca.nodes.Compute'],
        uniqueId: 'capability.ddf1301e-866b-4fa3-bc4f-edbd81e532cd.binding',
        maxOccurrences: 'UNBOUNDED',
        minOccurrences: '1',
    });

    const linkableCap = Mock.of<Capability>({
        type: 'tosca.capabilities.network.Linkable',
        capabilitySources: ['org.openecomp.resource.vl.extVL'],
        uniqueId: 'capability.ddf1301e-866b-4fa3-bc4f-edbd81e532cd.virtual_linkable',
        maxOccurrences: 'UNBOUNDED',
        minOccurrences: '1'
    });

    const nodeCompute = Mock.of<CompositionCiNodeVf>({
        name: 'Compute 0',
        componentInstance: Mock.of<ComponentInstance>({
            componentName: 'Compute',
            uniqueId : 'compute0',
            requirements: Mock.of<RequirementsGroup>({
                'tosca.capabilities.Node' : [ dependencyReq ],
                'tosca.capabilities.Attachment' : [ storeAttachmentReq ]
            }),
            capabilities: Mock.of<CapabilitiesGroup>({
                'tosca.capabilities.network.Bindable' : [ bindingCap ],
                'tosca.capabilities.Node' : [ featureCap ]
            })
        })
    });

    const nodeBlockStorage = Mock.of<CompositionCiNodeVf>({
        name: 'BlockStorage 0',
        componentInstance: Mock.of<ComponentInstance>({
            componentName: 'BlockStorage',
            uniqueId : 'blockstorage0',
            requirements: Mock.of<RequirementsGroup>({
                'tosca.capabilities.Node' : [ dependencyReq ]
            }),
            capabilities: Mock.of<CapabilitiesGroup>({
                'tosca.capabilities.Attachment' : [ blockStoreAttachmentCap ],
                'tosca.capabilities.Node' : [ featureCap ]
            })
        })
    });

    const nodeVl = Mock.of<CompositionCiNodeVl>({
        name: 'ExtVL 0',
        componentInstance: Mock.of<ComponentInstance>({
            componentName: 'BlockStorage',
            uniqueId : 'extvl0',
            requirements: Mock.of<RequirementsGroup>({
                'tosca.capabilities.Node' : [ dependencyReq ]
            }),
            capabilities: Mock.of<CapabilitiesGroup>({
                'tosca.capabilities.network.Linkable' : [ linkableCap ],
                'tosca.capabilities.Node' : [ featureCap ]
            })
        })
    });

    const nodeCp = Mock.of<CompositionCiNodeCp>({
        name: 'ExtCP 0',
        componentInstance: Mock.of<ComponentInstance>({
            componentName: 'ExtCP',
            uniqueId : 'extcp0',
            requirements: Mock.of<RequirementsGroup>({
                'tosca.capabilities.network.Linkable' : [ virtualLinkReq ],
                'tosca.capabilities.network.Bindable' : [ bindableReq ]
            }),
            capabilities: Mock.of<CapabilitiesGroup>({
                'tosca.capabilities.Node' : [ featureCap ]
            })
        })
    });

    let service: MatchCapabilitiesRequirementsUtils;

    beforeEach(() => {
        TestBed.configureTestingModule({
            imports: [],
            providers: [MatchCapabilitiesRequirementsUtils]
        });

        service = TestBed.get(MatchCapabilitiesRequirementsUtils);
    });

    it('match capability requirements utils should be defined', () => {
        console.log(JSON.stringify(service));
        expect(service).toBeDefined();
    });

    describe('isMatch function ', () => {

        it('capability type not equal to requirement capability, match is false', () => {
            const requirement = Mock.of<Requirement>({capability: 'tosca.capabilities.network.Linkable11'});
            const capability = Mock.of<Capability>({type: 'tosca.capabilities.network.Linkable'});
            expect(service.isMatch(requirement, capability)).toBeFalsy();
        });

        it('capability type equal to requirement capability and requirement node not exist, match is true', () => {
            const requirement = Mock.of<Requirement>({capability: 'tosca.capabilities.network.Linkable'});
            const capability = Mock.of<Capability>({type: 'tosca.capabilities.network.Linkable'});
            expect(service.isMatch(requirement, capability)).toBeTruthy();
        });

        it('is match - capability type equal to requirement capability and requirement node exist and includes in capability sources, match is true', () => {
            const requirement = Mock.of<Requirement>({capability: 'tosca.capabilities.network.Linkable', node: 'node1'});
            const capability = Mock.of<Capability>({
                type: 'tosca.capabilities.network.Linkable',
                capabilitySources: ['node1', 'node2', 'node3']
            });
            expect(service.isMatch(requirement, capability)).toBeTruthy();
        });

        it('no match - capability type equal to requirement capability and requirement node but not includes in capability sources, match is false', () => {
            const requirement = Mock.of<Requirement>({capability: 'tosca.capabilities.network.Linkable', node: 'node4'});
            const capability = Mock.of<Capability>({
                type: 'tosca.capabilities.network.Linkable',
                capabilitySources: ['node1', 'node2', 'node3']
            });
            expect(service.isMatch(requirement, capability)).toBeFalsy();
        });
    });

    describe('hasUnfulfilledRequirementContainingMatch function ', () => {

        it('node have no componentInstance, return false', () => {
            const node = Mock.of<CompositionCiNodeVf>({componentInstance: undefined});
            expect(service.hasUnfulfilledRequirementContainingMatch(node, [], {}, [])).toBeFalsy();
        });

        it('node have componentInstance data but no unfulfilled requirements, return false', () => {
            const node = Mock.of<CompositionCiNodeVf>({componentInstance: Mock.of<ComponentInstance>()});
            jest.spyOn(service, 'getUnfulfilledRequirements').mockReturnValue([]);
            expect(service.hasUnfulfilledRequirementContainingMatch(node, [], {}, [])).toBeFalsy();
        });

        it('node have componentInstance data and unfulfilled requirements but no match found, return false', () => {
            const node = Mock.of<CompositionCiNodeVf>({componentInstance: Mock.of<ComponentInstance>()});
            jest.spyOn(service, 'getUnfulfilledRequirements').mockReturnValue([Mock.of<Requirement>(), Mock.of<Requirement>()]);
            jest.spyOn(service, 'containsMatch').mockReturnValue(false);
            expect(service.hasUnfulfilledRequirementContainingMatch(node, [], {}, [])).toBeFalsy();
        });

        it('node have componentInstance data with unfulfilled requirements and match found, return true', () => {
            const node = Mock.of<CompositionCiNodeVf>({componentInstance: Mock.of<ComponentInstance>()});
            jest.spyOn(service, 'getUnfulfilledRequirements').mockReturnValue([Mock.of<Requirement>(), Mock.of<Requirement>()]);
            jest.spyOn(service, 'containsMatch').mockReturnValue(true);
            expect(service.hasUnfulfilledRequirementContainingMatch(node, [], {}, [])).toBeTruthy();
        });
    });

    describe('getMatches function ', () => {
        let fromId: string;
        let toId: string;

        beforeEach(() => {
            fromId = 'from_id';
            toId = 'to_id';
        });

        it('node have no unfulfilled requirements, return empty match array', () => {
            jest.spyOn(service, 'getUnfulfilledRequirements').mockReturnValue([]);
            expect(service.getMatches({}, {}, [], fromId, toId, true)).toHaveLength(0);
        });

        it('node have unfulfilled requirements but no capabilities, return empty match array', () => {
            jest.spyOn(service, 'getUnfulfilledRequirements').mockReturnValue([Mock.of<Requirement>(), Mock.of<Requirement>()]);
            expect(service.getMatches({}, {}, [], fromId, toId, true)).toHaveLength(0);
        });

        it('node have unfulfilled requirements and capabilities but no match found, return empty match array', () => {
            jest.spyOn(service, 'getUnfulfilledRequirements').mockReturnValue([Mock.of<Requirement>(), Mock.of<Requirement>()]);
            jest.spyOn(service, 'isMatch').mockReturnValue(false);
            expect(service.getMatches({}, {}, [], fromId, toId, true)).toHaveLength(0);
        });

        it('node have 2 unfulfilled requirements and 2 capabilities and match found, return 4 matches', () => {
            jest.spyOn(service, 'getUnfulfilledRequirements').mockReturnValue([Mock.of<Requirement>(), Mock.of<Requirement>()]);
            const capabilities = {aaa: Mock.of<Capability>(), bbb: Mock.of<Capability>()};
            jest.spyOn(service, 'isMatch').mockReturnValue(true);
            expect(service.getMatches({}, capabilities, [], fromId, toId, true)).toHaveLength(4);
        });
    });

    describe('Find matching nodes ===>', () => {

       it('should find matching nodes with component instance', () => {
           const nodes = [ nodeBlockStorage, nodeCompute, nodeVl ];
           let matchingNodes: any;

           // Compute can connect to Block Store
           matchingNodes = service.findMatchingNodesToComponentInstance(nodeCompute.componentInstance, nodes, []);
           expect(matchingNodes).toHaveLength(1);
           expect(matchingNodes).toContain(nodeBlockStorage);

           // Block Storage can connect to Compute
           matchingNodes = service.findMatchingNodesToComponentInstance(nodeBlockStorage.componentInstance, nodes, []);
           expect(matchingNodes).toHaveLength(1);
           expect(matchingNodes).toContain(nodeCompute);

           // Vl has no matches
           matchingNodes = service.findMatchingNodesToComponentInstance(nodeVl.componentInstance, nodes, []);
           expect(matchingNodes).toHaveLength(0);

           // CP should be able to connect to VL and Compute
           matchingNodes = service.findMatchingNodesToComponentInstance(nodeCp.componentInstance, nodes, []);
           expect(matchingNodes).toHaveLength(2);
           expect(matchingNodes).toContain(nodeCompute);
           expect(matchingNodes).toContain(nodeVl);
       });

       it('try with empty list of nodes', () => {
            const nodes = [ ];
            let matchingNodes: any;

            // Compute can connect to Block Store
            matchingNodes = service.findMatchingNodesToComponentInstance(nodeCompute.componentInstance, nodes, []);
            expect(matchingNodes).toHaveLength(0);
        });

       it('should detect fulfilled connection with compute node', () => {
            const nodes = [ nodeBlockStorage, nodeCompute, nodeVl ];
            let matchingNodes: any;
            const link = {
                relation: {
                    fromNode: 'extcp0',
                    toNode: 'compute0',
                    relationships: [{
                        relation: {
                            requirementOwnerId: 'extcp0',
                            requirement: 'virtualBinding',
                            relationship: {
                                type: 'tosca.relationships.network.BindsTo'
                            }

                        }
                    }]
                }
            };

            const links = [link];
            // CP should be able to connect to VL only since it already has a link with compute
            matchingNodes = service.findMatchingNodesToComponentInstance(nodeCp.componentInstance, nodes, links as CompositionCiLinkBase[]);
            expect(matchingNodes).toHaveLength(1);
            expect(matchingNodes).toContain(nodeVl);
        });
    });
});