Add collaboration feature
[sdc.git] / openecomp-ui / test / licenseModel / licenseKeyGroups / test.js
1 /*!
2  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
13  * or implied. See the License for the specific language governing
14  * permissions and limitations under the License.
15  */
16 import deepFreeze from 'deep-freeze';
17 import mockRest from 'test-utils/MockRest.js';
18 import {cloneAndSet, buildListFromFactory} from 'test-utils/Util.js';
19 import {storeCreator} from 'sdc-app/AppStore.js';
20 import {LicenseKeyGroupStoreFactory, LicenseKeyGroupPostFactory} from 'test-utils/factories/licenseModel/LicenseKeyGroupFactories.js';
21
22 import LicenseKeyGroupsActionHelper from 'sdc-app/onboarding/licenseModel/licenseKeyGroups/LicenseKeyGroupsActionHelper.js';
23 import VersionFactory from 'test-utils/factories/common/VersionFactory.js';
24 import CurrentScreenFactory from 'test-utils/factories/common/CurrentScreenFactory.js';
25 import {LimitItemFactory, LimitPostFactory} from 'test-utils/factories/licenseModel/LimitFactories.js';
26 import {getStrValue} from 'nfvo-utils/getValue.js';
27 import {SyncStates} from 'sdc-app/common/merge/MergeEditorConstants.js';
28
29 describe('License Key Groups Module Tests', function () {
30
31         const LICENSE_MODEL_ID = '555';
32         const version = VersionFactory.build();
33         const itemPermissionAndProps = CurrentScreenFactory.build({}, {version});
34         const returnedVersionFields = {baseId: version.baseId, description: version.description, id: version.id, name: version.name, status: version.status};
35
36         it('Load License Key Group', () => {
37
38                 const licenseKeyGroupsList = buildListFromFactory(LicenseKeyGroupStoreFactory);
39
40                 deepFreeze(licenseKeyGroupsList);
41                 const store = storeCreator();
42                 deepFreeze(store.getState());
43
44                 const expectedStore = cloneAndSet(store.getState(), 'licenseModel.licenseKeyGroup.licenseKeyGroupsList', licenseKeyGroupsList);
45
46                 mockRest.addHandler('fetch', ({data, options, baseUrl}) => {
47                         expect(baseUrl).toEqual(`/onboarding-api/v1.0/vendor-license-models/${LICENSE_MODEL_ID}/versions/${version.id}/license-key-groups`);
48                         expect(data).toEqual(undefined);
49                         expect(options).toEqual(undefined);
50                         return {results: licenseKeyGroupsList};
51                 });
52
53                 return LicenseKeyGroupsActionHelper.fetchLicenseKeyGroupsList(store.dispatch, {licenseModelId: LICENSE_MODEL_ID, version}).then(() => {
54                         expect(store.getState()).toEqual(expectedStore);
55                 });
56         });
57
58         it('Delete License Key Group', () => {
59
60                 const licenseKeyGroupsList = buildListFromFactory(LicenseKeyGroupStoreFactory, 1);
61
62                 deepFreeze(licenseKeyGroupsList);
63                 const store = storeCreator({
64                         currentScreen: {...itemPermissionAndProps},
65                         licenseModel: {
66                                 licenseKeyGroup: {
67                                         licenseKeyGroupsList
68                                 }
69                         }
70                 });
71                 deepFreeze(store.getState());
72                 const toBeDeletedLicenseKeyGroupId = licenseKeyGroupsList[0].id;
73
74                 const expectedCurrentScreenProps = {
75                         ...itemPermissionAndProps,
76                         itemPermission: {
77                                 ...itemPermissionAndProps.itemPermission,
78                                 isDirty: true
79                         }
80                 };
81
82                 let expectedStore = cloneAndSet(store.getState(), 'licenseModel.licenseKeyGroup.licenseKeyGroupsList', []);
83                 expectedStore = cloneAndSet(expectedStore, 'currentScreen.itemPermission', expectedCurrentScreenProps.itemPermission);
84
85                 mockRest.addHandler('destroy', ({data, options, baseUrl}) => {
86                         expect(baseUrl).toEqual(`/onboarding-api/v1.0/vendor-license-models/${LICENSE_MODEL_ID}/versions/${version.id}/license-key-groups/${toBeDeletedLicenseKeyGroupId}`);
87                         expect(data).toEqual(undefined);
88                         expect(options).toEqual(undefined);
89                 });
90
91                 mockRest.addHandler('fetch', ({data, options, baseUrl}) => {
92                         expect(baseUrl).toEqual(`/onboarding-api/v1.0/items/${LICENSE_MODEL_ID}/versions/${version.id}`);
93                         expect(data).toEqual(undefined);
94                         expect(options).toEqual(undefined);
95                         return {...returnedVersionFields, state: {synchronizationState: SyncStates.UP_TO_DATE, dirty: true}};
96                 });
97
98                 return LicenseKeyGroupsActionHelper.deleteLicenseKeyGroup(store.dispatch, {
99                         licenseKeyGroupId: toBeDeletedLicenseKeyGroupId,
100                         licenseModelId: LICENSE_MODEL_ID,
101                         version
102                 }).then(() => {
103                         expect(store.getState()).toEqual(expectedStore);
104                 });
105         });
106
107         it('Add License Key Group', () => {
108
109                 const store = storeCreator({
110                         currentScreen: {...itemPermissionAndProps}
111                 });
112                 deepFreeze(store.getState());
113
114                 const LicenseKeyGroupPost = LicenseKeyGroupPostFactory.build();
115                 deepFreeze(LicenseKeyGroupPost);
116
117                 const LicenseKeyGroupStore = LicenseKeyGroupStoreFactory.build();
118                 deepFreeze(LicenseKeyGroupStore);
119
120                 const expectedCurrentScreenProps = {
121                         ...itemPermissionAndProps,
122                         itemPermission: {
123                                 ...itemPermissionAndProps.itemPermission,
124                                 isDirty: true
125                         }
126                 };
127
128                 let expectedStore = cloneAndSet(store.getState(), 'licenseModel.licenseKeyGroup.licenseKeyGroupsList', [LicenseKeyGroupStore]);
129                 expectedStore = cloneAndSet(expectedStore, 'currentScreen.itemPermission', expectedCurrentScreenProps.itemPermission);
130
131                 mockRest.addHandler('post', ({options, data, baseUrl}) => {
132                         expect(baseUrl).toEqual(`/onboarding-api/v1.0/vendor-license-models/${LICENSE_MODEL_ID}/versions/${version.id}/license-key-groups`);
133                         expect(data).toEqual(LicenseKeyGroupPost);
134                         expect(options).toEqual(undefined);
135                         return {
136                                 value: LicenseKeyGroupStore.id
137                         };
138                 });
139
140                 mockRest.addHandler('fetch', ({data, options, baseUrl}) => {
141                         expect(baseUrl).toEqual(`/onboarding-api/v1.0/items/${LICENSE_MODEL_ID}/versions/${version.id}`);
142                         expect(data).toEqual(undefined);
143                         expect(options).toEqual(undefined);
144                         return {...returnedVersionFields, state: {synchronizationState: SyncStates.UP_TO_DATE, dirty: true}};
145                 });
146
147                 return LicenseKeyGroupsActionHelper.saveLicenseKeyGroup(store.dispatch, {
148                         licenseKeyGroup: LicenseKeyGroupPost,
149                         licenseModelId: LICENSE_MODEL_ID,
150                         version
151                 }).then(() => {
152                         expect(store.getState()).toEqual(expectedStore);
153                 });
154         });
155
156         it('Update License Key Group', () => {
157                 const licenseKeyGroupsList = buildListFromFactory(LicenseKeyGroupStoreFactory, 1);
158                 deepFreeze(licenseKeyGroupsList);
159                 const store = storeCreator({
160                         currentScreen: {...itemPermissionAndProps},
161                         licenseModel: {
162                                 licenseKeyGroup: {
163                                         licenseKeyGroupsList
164                                 }
165                         }
166                 });
167
168                 const toBeUpdatedLicenseKeyGroupId = licenseKeyGroupsList[0].id;
169                 const previousLicenseKeyGroupData = licenseKeyGroupsList[0];
170
171                 const licenseKeyGroupUpdatedData = LicenseKeyGroupPostFactory.build({
172                         name: 'lsk1_UPDATE',
173                         description: 'string_UPDATE',
174                         id: toBeUpdatedLicenseKeyGroupId
175                 });
176                 deepFreeze(licenseKeyGroupUpdatedData);
177
178                 const licenseKeyGroupPutRequest = LicenseKeyGroupPostFactory.build({
179                         name: 'lsk1_UPDATE',
180                         description: 'string_UPDATE'
181                 });
182
183                 deepFreeze(licenseKeyGroupPutRequest);
184
185                 const expectedCurrentScreenProps = {
186                         ...itemPermissionAndProps,
187                         itemPermission: {
188                                 ...itemPermissionAndProps.itemPermission,
189                                 isDirty: true
190                         }
191                 };
192
193                 let expectedStore = cloneAndSet(store.getState(), 'licenseModel.licenseKeyGroup.licenseKeyGroupsList', [licenseKeyGroupUpdatedData]);
194                 expectedStore = cloneAndSet(expectedStore, 'currentScreen.itemPermission', expectedCurrentScreenProps.itemPermission);
195
196                 mockRest.addHandler('put', ({data, options, baseUrl}) => {
197                         expect(baseUrl).toEqual(`/onboarding-api/v1.0/vendor-license-models/${LICENSE_MODEL_ID}/versions/${version.id}/license-key-groups/${toBeUpdatedLicenseKeyGroupId}`);
198                         expect(data).toEqual(licenseKeyGroupPutRequest);
199                         expect(options).toEqual(undefined);
200                 });
201
202                 mockRest.addHandler('fetch', ({data, options, baseUrl}) => {
203                         expect(baseUrl).toEqual(`/onboarding-api/v1.0/items/${LICENSE_MODEL_ID}/versions/${version.id}`);
204                         expect(data).toEqual(undefined);
205                         expect(options).toEqual(undefined);
206                         return {...returnedVersionFields, state: {synchronizationState: SyncStates.UP_TO_DATE, dirty: true}};
207                 });
208
209                 return LicenseKeyGroupsActionHelper.saveLicenseKeyGroup(store.dispatch, {
210                         previousLicenseKeyGroup: previousLicenseKeyGroupData,
211                         licenseKeyGroup: licenseKeyGroupUpdatedData,
212                         licenseModelId: LICENSE_MODEL_ID,
213                         version
214                 }).then(() => {
215                         expect(store.getState()).toEqual(expectedStore);
216                 });
217         });
218
219         it('Load Limits List', () => {
220
221                 const limitsList = LimitItemFactory.buildList(3);
222                 deepFreeze(limitsList);
223                 const store = storeCreator();
224                 deepFreeze(store.getState());
225
226                 const expectedStore = cloneAndSet(store.getState(), 'licenseModel.licenseKeyGroup.licenseKeyGroupsEditor.limitsList', limitsList);
227                 const licenseKeyGroup = LicenseKeyGroupStoreFactory.build();
228                 mockRest.addHandler('fetch', ({data, options, baseUrl}) => {
229                         expect(baseUrl).toEqual(`/onboarding-api/v1.0/vendor-license-models/${LICENSE_MODEL_ID}/versions/${version.id}/license-key-groups/${licenseKeyGroup.id}/limits`);
230                         expect(data).toEqual(undefined);
231                         expect(options).toEqual(undefined);
232                         return {results: limitsList};
233                  });
234
235                 return LicenseKeyGroupsActionHelper.fetchLimits(store.dispatch, {licenseModelId: LICENSE_MODEL_ID, version, licenseKeyGroup}).then(() => {
236                         expect(store.getState()).toEqual(expectedStore);
237                  });
238         });
239
240         it('Add Limit', () => {
241
242                 const store = storeCreator({
243                         currentScreen: {...itemPermissionAndProps}
244                 });
245                 deepFreeze(store.getState());
246
247                 const limitToAdd = LimitPostFactory.build();
248                 let limitFromBE = {...limitToAdd};
249                 limitFromBE.metric = getStrValue(limitFromBE.metric);
250                 limitFromBE.unit = getStrValue(limitFromBE.unit);
251
252                 deepFreeze(limitToAdd);
253                 deepFreeze(limitFromBE);
254
255                 const LimitIdFromResponse = 'ADDED_ID';
256                 const limitAddedItem = {...limitToAdd, id: LimitIdFromResponse};
257                 deepFreeze(limitAddedItem);
258                 const licenseKeyGroup = LicenseKeyGroupStoreFactory.build();
259
260                 const expectedCurrentScreenProps = {
261                         ...itemPermissionAndProps,
262                         itemPermission: {
263                                 ...itemPermissionAndProps.itemPermission,
264                                 isDirty: true
265                         }
266                 };
267
268                 let expectedStore = cloneAndSet(store.getState(), 'licenseModel.licenseKeyGroup.licenseKeyGroupsEditor.limitsList', [limitAddedItem]);
269                 expectedStore = cloneAndSet(expectedStore, 'currentScreen.itemPermission', expectedCurrentScreenProps.itemPermission);
270
271                 mockRest.addHandler('post', ({data, options, baseUrl}) => {
272                         expect(baseUrl).toEqual(`/onboarding-api/v1.0/vendor-license-models/${LICENSE_MODEL_ID}/versions/${version.id}/license-key-groups/${licenseKeyGroup.id}/limits`);
273                         expect(data).toEqual(limitFromBE);
274                         expect(options).toEqual(undefined);
275                         return {
276                                 returnCode: 'OK',
277                                 value: LimitIdFromResponse
278                         };
279                 });
280
281                 mockRest.addHandler('fetch', ({data, options, baseUrl}) => {
282                         expect(baseUrl).toEqual(`/onboarding-api/v1.0/vendor-license-models/${LICENSE_MODEL_ID}/versions/${version.id}/license-key-groups/${licenseKeyGroup.id}/limits`);
283                         expect(data).toEqual(undefined);
284                         expect(options).toEqual(undefined);
285                         return {results: [limitAddedItem]};
286                  });
287
288                 mockRest.addHandler('fetch', ({data, options, baseUrl}) => {
289                         expect(baseUrl).toEqual(`/onboarding-api/v1.0/items/${LICENSE_MODEL_ID}/versions/${version.id}`);
290                         expect(data).toEqual(undefined);
291                         expect(options).toEqual(undefined);
292                         return {...returnedVersionFields, state: {synchronizationState: SyncStates.UP_TO_DATE, dirty: true}};
293                 });
294
295                 return LicenseKeyGroupsActionHelper.submitLimit(store.dispatch,
296                         {
297                                 licenseModelId: LICENSE_MODEL_ID,
298                                 version,
299                                 licenseKeyGroup,
300                                 limit: limitToAdd
301                         }
302                 ).then(() => {
303                         expect(store.getState()).toEqual(expectedStore);
304                 });
305         });
306
307         it('Delete Limit', () => {
308
309                 const limitsList = LimitItemFactory.buildList(1);
310                 deepFreeze(limitsList);
311
312                 const store = storeCreator({
313                         currentScreen: {...itemPermissionAndProps},
314                         licenseModel: {
315                                 entitlementPool: {
316                                         entitlementPoolEditor: {
317                                                 limitsList
318                                         }
319                                 }
320                         }
321                 });
322                 deepFreeze(store.getState());
323
324                 const licenseKeyGroup = LicenseKeyGroupStoreFactory.build();
325
326                 const expectedCurrentScreenProps = {
327                         ...itemPermissionAndProps,
328                         itemPermission: {
329                                 ...itemPermissionAndProps.itemPermission,
330                                 isDirty: true
331                         }
332                 };
333
334                 let expectedStore = cloneAndSet(store.getState(), 'licenseModel.licenseKeyGroup.licenseKeyGroupsEditor.limitsList', []);
335                 expectedStore = cloneAndSet(expectedStore, 'currentScreen.itemPermission', expectedCurrentScreenProps.itemPermission);
336
337                 mockRest.addHandler('destroy', ({data, options, baseUrl}) => {
338                         expect(baseUrl).toEqual(`/onboarding-api/v1.0/vendor-license-models/${LICENSE_MODEL_ID}/versions/${version.id}/license-key-groups/${licenseKeyGroup.id}/limits/${limitsList[0].id}`);
339                         expect(data).toEqual(undefined);
340                         expect(options).toEqual(undefined);
341                         return {
342                                 results: {
343                                         returnCode: 'OK'
344                                 }
345                         };
346                 });
347
348                 mockRest.addHandler('fetch', ({data, options, baseUrl}) => {
349                         expect(baseUrl).toEqual(`/onboarding-api/v1.0/vendor-license-models/${LICENSE_MODEL_ID}/versions/${version.id}/license-key-groups/${licenseKeyGroup.id}/limits`);
350                         expect(data).toEqual(undefined);
351                         expect(options).toEqual(undefined);
352                         return {results: []};
353                  });
354
355                 mockRest.addHandler('fetch', ({data, options, baseUrl}) => {
356                         expect(baseUrl).toEqual(`/onboarding-api/v1.0/items/${LICENSE_MODEL_ID}/versions/${version.id}`);
357                         expect(data).toEqual(undefined);
358                         expect(options).toEqual(undefined);
359                         return {...returnedVersionFields, state: {synchronizationState: SyncStates.UP_TO_DATE, dirty: true}};
360                 });
361
362                 return LicenseKeyGroupsActionHelper.deleteLimit(store.dispatch, {
363                         licenseModelId: LICENSE_MODEL_ID,
364                         version,
365                         licenseKeyGroup,
366                         limit: limitsList[0]
367                 }).then(() => {
368                         expect(store.getState()).toEqual(expectedStore);
369                 });
370         });
371
372         it('Update Limit', () => {
373
374                 const limitsList = LimitItemFactory.buildList(1);
375                 deepFreeze(limitsList);
376                 const licenseKeyGroup = LicenseKeyGroupStoreFactory.build();
377                 const store = storeCreator({
378                         currentScreen: {...itemPermissionAndProps},
379                         licenseModel: {
380                                 licenseKeyGroup: {
381                                         licenseKeyGroupsEditor: {
382                                                 limitsList
383                                         }
384                                 }
385                         }
386                 });
387
388                 deepFreeze(store.getState());
389
390
391                 const previousData = limitsList[0];
392                 deepFreeze(previousData);
393                 const limitId = limitsList[0].id;
394
395                 let updatedLimit = {...previousData, name: 'updatedLimit'};
396                 const updatedLimitForPut = {...updatedLimit, id: undefined};
397                 updatedLimit.metric = {choice: updatedLimit.metric, other: ''};
398                 updatedLimit.unit = {choice: updatedLimit.unit, other: ''};
399                 deepFreeze(updatedLimit);
400
401                 const expectedCurrentScreenProps = {
402                         ...itemPermissionAndProps,
403                         itemPermission: {
404                                 ...itemPermissionAndProps.itemPermission,
405                                 isDirty: true
406                         }
407                 };
408
409                 let expectedStore = cloneAndSet(store.getState(), 'licenseModel.licenseKeyGroup.licenseKeyGroupsEditor.limitsList', [updatedLimitForPut]);
410                 expectedStore = cloneAndSet(expectedStore, 'currentScreen.itemPermission', expectedCurrentScreenProps.itemPermission);
411
412
413                 mockRest.addHandler('put', ({data, options, baseUrl}) => {
414                         expect(baseUrl).toEqual(`/onboarding-api/v1.0/vendor-license-models/${LICENSE_MODEL_ID}/versions/${version.id}/license-key-groups/${licenseKeyGroup.id}/limits/${limitId}`);
415                         expect(data).toEqual(updatedLimitForPut);
416                         expect(options).toEqual(undefined);
417                         return {returnCode: 'OK'};
418                 });
419
420                 mockRest.addHandler('fetch', ({data, options, baseUrl}) => {
421                         expect(baseUrl).toEqual(`/onboarding-api/v1.0/vendor-license-models/${LICENSE_MODEL_ID}/versions/${version.id}/license-key-groups/${licenseKeyGroup.id}/limits`);
422                         expect(data).toEqual(undefined);
423                         expect(options).toEqual(undefined);
424                         return {results: [updatedLimitForPut]};
425                  });
426
427                 mockRest.addHandler('fetch', ({data, options, baseUrl}) => {
428                         expect(baseUrl).toEqual(`/onboarding-api/v1.0/items/${LICENSE_MODEL_ID}/versions/${version.id}`);
429                         expect(data).toEqual(undefined);
430                         expect(options).toEqual(undefined);
431                         return {...returnedVersionFields, state: {synchronizationState: SyncStates.UP_TO_DATE, dirty: true}};
432                 });
433
434                 return LicenseKeyGroupsActionHelper.submitLimit(store.dispatch,
435                         {
436                                 licenseModelId: LICENSE_MODEL_ID,
437                                 version,
438                                 licenseKeyGroup,
439                                 limit: updatedLimit
440                         }
441                 ).then(() => {
442                         expect(store.getState()).toEqual(expectedStore);
443                 });
444         });
445
446 });