[SDC] Full OnBoard health-check and NFoD support
[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 VersionControllerUtilsFactory from 'test-utils/factories/softwareProduct/VersionControllerUtilsFactory.js';
24 import {LimitItemFactory, LimitPostFactory} from 'test-utils/factories/licenseModel/LimitFactories.js';
25
26 describe('License Key Groups Module Tests', function () {
27
28         const LICENSE_MODEL_ID = '555';
29         const version = VersionControllerUtilsFactory.build().version;
30
31         it('Load License Key Group', () => {
32
33                 const licenseKeyGroupsList = buildListFromFactory(LicenseKeyGroupStoreFactory);
34
35                 deepFreeze(licenseKeyGroupsList);
36                 const store = storeCreator();
37                 deepFreeze(store.getState());
38
39                 const expectedStore = cloneAndSet(store.getState(), 'licenseModel.licenseKeyGroup.licenseKeyGroupsList', licenseKeyGroupsList);
40
41                 mockRest.addHandler('fetch', ({data, options, baseUrl}) => {
42                         expect(baseUrl).toEqual(`/onboarding-api/v1.0/vendor-license-models/${LICENSE_MODEL_ID}/versions/${version.id}/license-key-groups`);
43                         expect(data).toEqual(undefined);
44                         expect(options).toEqual(undefined);
45                         return {results: licenseKeyGroupsList};
46                 });
47
48                 return LicenseKeyGroupsActionHelper.fetchLicenseKeyGroupsList(store.dispatch, {licenseModelId: LICENSE_MODEL_ID, version}).then(() => {
49                         expect(store.getState()).toEqual(expectedStore);
50                 });
51         });
52
53         it('Delete License Key Group', () => {
54
55                 const licenseKeyGroupsList = buildListFromFactory(LicenseKeyGroupStoreFactory, 1);
56
57                 deepFreeze(licenseKeyGroupsList);
58                 const store = storeCreator({
59                         licenseModel: {
60                                 licenseKeyGroup: {
61                                         licenseKeyGroupsList
62                                 }
63                         }
64                 });
65                 deepFreeze(store.getState());
66                 const toBeDeletedLicenseKeyGroupId = licenseKeyGroupsList[0].id;
67                 const expectedStore = cloneAndSet(store.getState(), 'licenseModel.licenseKeyGroup.licenseKeyGroupsList', []);
68
69                 mockRest.addHandler('destroy', ({data, options, baseUrl}) => {
70                         expect(baseUrl).toEqual(`/onboarding-api/v1.0/vendor-license-models/${LICENSE_MODEL_ID}/versions/${version.id}/license-key-groups/${toBeDeletedLicenseKeyGroupId}`);
71                         expect(data).toEqual(undefined);
72                         expect(options).toEqual(undefined);
73                 });
74
75                 return LicenseKeyGroupsActionHelper.deleteLicenseKeyGroup(store.dispatch, {
76                         licenseKeyGroupId: toBeDeletedLicenseKeyGroupId,
77                         licenseModelId: LICENSE_MODEL_ID,
78                         version
79                 }).then(() => {
80                         expect(store.getState()).toEqual(expectedStore);
81                 });
82         });
83
84         it('Add License Key Group', () => {
85
86                 const store = storeCreator();
87                 deepFreeze(store.getState());
88
89                 const LicenseKeyGroupPost = LicenseKeyGroupPostFactory.build();
90                 deepFreeze(LicenseKeyGroupPost);
91
92                 const LicenseKeyGroupStore = LicenseKeyGroupStoreFactory.build();
93                 deepFreeze(LicenseKeyGroupStore);
94
95                 const expectedStore = cloneAndSet(store.getState(), 'licenseModel.licenseKeyGroup.licenseKeyGroupsList', [LicenseKeyGroupStore]);
96
97                 mockRest.addHandler('post', ({options, data, baseUrl}) => {
98                         expect(baseUrl).toEqual(`/onboarding-api/v1.0/vendor-license-models/${LICENSE_MODEL_ID}/versions/${version.id}/license-key-groups`);
99                         expect(data).toEqual(LicenseKeyGroupPost);
100                         expect(options).toEqual(undefined);
101                         return {
102                                 value: LicenseKeyGroupStore.id
103                         };
104                 });
105
106                 return LicenseKeyGroupsActionHelper.saveLicenseKeyGroup(store.dispatch, {
107                         licenseKeyGroup: LicenseKeyGroupPost,
108                         licenseModelId: LICENSE_MODEL_ID,
109                         version
110                 }).then(() => {
111                         expect(store.getState()).toEqual(expectedStore);
112                 });
113         });
114
115         it('Update License Key Group', () => {
116                 const licenseKeyGroupsList = buildListFromFactory(LicenseKeyGroupStoreFactory, 1);
117                 deepFreeze(licenseKeyGroupsList);
118                 const store = storeCreator({
119                         licenseModel: {
120                                 licenseKeyGroup: {
121                                         licenseKeyGroupsList
122                                 }
123                         }
124                 });
125
126                 const toBeUpdatedLicenseKeyGroupId = licenseKeyGroupsList[0].id;
127                 const previousLicenseKeyGroupData = licenseKeyGroupsList[0];
128
129                 const licenseKeyGroupUpdatedData = LicenseKeyGroupPostFactory.build({
130                         name: 'lsk1_UPDATE',
131                         description: 'string_UPDATE',
132                         id: toBeUpdatedLicenseKeyGroupId
133                 });
134                 deepFreeze(licenseKeyGroupUpdatedData);
135
136                 const licenseKeyGroupPutRequest = LicenseKeyGroupPostFactory.build({
137                         name: 'lsk1_UPDATE',
138                         description: 'string_UPDATE'
139                 });
140
141                 deepFreeze(licenseKeyGroupPutRequest);
142
143                 const expectedStore = cloneAndSet(store.getState(), 'licenseModel.licenseKeyGroup.licenseKeyGroupsList', [licenseKeyGroupUpdatedData]);
144
145                 mockRest.addHandler('put', ({data, options, baseUrl}) => {
146                         expect(baseUrl).toEqual(`/onboarding-api/v1.0/vendor-license-models/${LICENSE_MODEL_ID}/versions/${version.id}/license-key-groups/${toBeUpdatedLicenseKeyGroupId}`);
147                         expect(data).toEqual(licenseKeyGroupPutRequest);
148                         expect(options).toEqual(undefined);
149                 });
150
151                 return LicenseKeyGroupsActionHelper.saveLicenseKeyGroup(store.dispatch, {
152                         previousLicenseKeyGroup: previousLicenseKeyGroupData,
153                         licenseKeyGroup: licenseKeyGroupUpdatedData,
154                         licenseModelId: LICENSE_MODEL_ID,
155                         version
156                 }).then(() => {
157                         expect(store.getState()).toEqual(expectedStore);
158                 });
159         });
160
161         it('Load Limits List', () => {
162
163                 const limitsList = LimitItemFactory.buildList(3);               
164                 deepFreeze(limitsList);
165                 const store = storeCreator();
166                 deepFreeze(store.getState());
167
168                 const expectedStore = cloneAndSet(store.getState(), 'licenseModel.licenseKeyGroup.licenseKeyGroupsEditor.limitsList', limitsList);
169                 const licenseKeyGroup = LicenseKeyGroupStoreFactory.build();
170                 mockRest.addHandler('fetch', ({data, options, baseUrl}) => {
171                         expect(baseUrl).toEqual(`/onboarding-api/v1.0/vendor-license-models/${LICENSE_MODEL_ID}/versions/${version.id}/license-key-groups/${licenseKeyGroup.id}/limits`);
172                         expect(data).toEqual(undefined);
173                         expect(options).toEqual(undefined);
174                         return {results: limitsList};
175                  });
176
177                 return LicenseKeyGroupsActionHelper.fetchLimits(store.dispatch, {licenseModelId: LICENSE_MODEL_ID, version, licenseKeyGroup}).then(() => {
178                         expect(store.getState()).toEqual(expectedStore);
179                  });
180         });
181
182         it('Add Limit', () => {
183
184                 const store = storeCreator();
185                 deepFreeze(store.getState());
186
187                 const limitToAdd = LimitPostFactory.build();
188
189                 deepFreeze(limitToAdd);
190
191                 const LimitIdFromResponse = 'ADDED_ID';
192                 const limitAddedItem = {...limitToAdd, id: LimitIdFromResponse};
193                 deepFreeze(limitAddedItem);
194                 const licenseKeyGroup = LicenseKeyGroupStoreFactory.build();
195
196                 const expectedStore = cloneAndSet(store.getState(), 'licenseModel.licenseKeyGroup.licenseKeyGroupsEditor.limitsList', [limitAddedItem]);
197
198                 mockRest.addHandler('post', ({data, options, baseUrl}) => {
199                         expect(baseUrl).toEqual(`/onboarding-api/v1.0/vendor-license-models/${LICENSE_MODEL_ID}/versions/${version.id}/license-key-groups/${licenseKeyGroup.id}/limits`);
200                         expect(data).toEqual(limitToAdd);
201                         expect(options).toEqual(undefined);
202                         return {
203                                 returnCode: 'OK',
204                                 value: LimitIdFromResponse
205                         };
206                 });
207
208                 mockRest.addHandler('fetch', ({data, options, baseUrl}) => {
209                         expect(baseUrl).toEqual(`/onboarding-api/v1.0/vendor-license-models/${LICENSE_MODEL_ID}/versions/${version.id}/license-key-groups/${licenseKeyGroup.id}/limits`);
210                         expect(data).toEqual(undefined);
211                         expect(options).toEqual(undefined);
212                         return {results: [limitAddedItem]};
213                  });
214
215                 return LicenseKeyGroupsActionHelper.submitLimit(store.dispatch,
216                         {
217                                 licenseModelId: LICENSE_MODEL_ID,
218                                 version,                                
219                                 licenseKeyGroup,
220                                 limit: limitToAdd
221                         }
222                 ).then(() => {
223                         expect(store.getState()).toEqual(expectedStore);
224                 });
225         });
226
227         it('Delete Limit', () => {
228
229                 const limitsList = LimitItemFactory.buildList(1);               
230                 deepFreeze(limitsList);
231                                         
232                 const store = storeCreator({
233                         licenseModel: {
234                                 entitlementPool: {
235                                         entitlementPoolEditor: {
236                                                 limitsList
237                                         }
238                                 }
239                         }
240                 });
241                 deepFreeze(store.getState());
242
243                 const licenseKeyGroup = LicenseKeyGroupStoreFactory.build();
244                 const expectedStore = cloneAndSet(store.getState(), 'licenseModel.licenseKeyGroup.licenseKeyGroupsEditor.limitsList', []);
245
246                 mockRest.addHandler('destroy', ({data, options, baseUrl}) => {
247                         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}`);
248                         expect(data).toEqual(undefined);
249                         expect(options).toEqual(undefined);
250                         return {
251                                 results: {
252                                         returnCode: 'OK'
253                                 }
254                         };
255                 });
256
257                 mockRest.addHandler('fetch', ({data, options, baseUrl}) => {
258                         expect(baseUrl).toEqual(`/onboarding-api/v1.0/vendor-license-models/${LICENSE_MODEL_ID}/versions/${version.id}/license-key-groups/${licenseKeyGroup.id}/limits`);
259                         expect(data).toEqual(undefined);
260                         expect(options).toEqual(undefined);
261                         return {results: []};
262                  });
263
264                 return LicenseKeyGroupsActionHelper.deleteLimit(store.dispatch, {
265                         licenseModelId: LICENSE_MODEL_ID,
266                         version,
267                         licenseKeyGroup,
268                         limit: limitsList[0]
269                 }).then(() => {
270                         expect(store.getState()).toEqual(expectedStore);
271                 });
272         });
273
274         it('Update Limit', () => {
275
276                 const limitsList = LimitItemFactory.buildList(1);               
277                 deepFreeze(limitsList);
278                 const licenseKeyGroup = LicenseKeyGroupStoreFactory.build();
279                 const store = storeCreator({
280                         licenseModel: {
281                                 licenseKeyGroup: {
282                                         licenseKeyGroupsEditor: {
283                                                 limitsList
284                                         }
285                                 }
286                         }
287                 });
288
289                 deepFreeze(store.getState());
290
291                 
292                 const previousData = limitsList[0];
293                 deepFreeze(previousData);
294                 const limitId = limitsList[0].id;
295                 
296                 const updatedLimit = {...previousData, name: 'updatedLimit'};
297                 deepFreeze(updatedLimit);
298                 const updatedLimitForPut = {...updatedLimit, id: undefined};
299
300                 const expectedStore = cloneAndSet(store.getState(), 'licenseModel.licenseKeyGroup.licenseKeyGroupsEditor.limitsList', [updatedLimit]);
301
302
303                 mockRest.addHandler('put', ({data, options, baseUrl}) => {
304                         expect(baseUrl).toEqual(`/onboarding-api/v1.0/vendor-license-models/${LICENSE_MODEL_ID}/versions/${version.id}/license-key-groups/${licenseKeyGroup.id}/limits/${limitId}`);
305                         expect(data).toEqual(updatedLimitForPut);
306                         expect(options).toEqual(undefined);
307                         return {returnCode: 'OK'};
308                 });
309
310                 mockRest.addHandler('fetch', ({data, options, baseUrl}) => {
311                         expect(baseUrl).toEqual(`/onboarding-api/v1.0/vendor-license-models/${LICENSE_MODEL_ID}/versions/${version.id}/license-key-groups/${licenseKeyGroup.id}/limits`);
312                         expect(data).toEqual(undefined);
313                         expect(options).toEqual(undefined);
314                         return {results: [updatedLimit]};
315                  });
316
317                 return LicenseKeyGroupsActionHelper.submitLimit(store.dispatch,
318                         {
319                                 licenseModelId: LICENSE_MODEL_ID,
320                                 version,                                
321                                 licenseKeyGroup,
322                                 limit: updatedLimit
323                         }
324                 ).then(() => {
325                         expect(store.getState()).toEqual(expectedStore);
326                 });
327         });
328
329 });