Add collaboration feature
[sdc.git] / openecomp-ui / test / licenseModel / featureGroups / 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 FeatureGroupsActionHelper from 'sdc-app/onboarding/licenseModel/featureGroups/FeatureGroupsActionHelper.js';
21 import { FeatureGroupStoreFactory, FeatureGroupPostFactory, FeatureGroupDispatchFactory, FeatureGroupPutFactory } from 'test-utils/factories/licenseModel/FeatureGroupFactories.js';
22 import VersionFactory from 'test-utils/factories/common/VersionFactory.js';
23 import CurrentScreenFactory from 'test-utils/factories/common/CurrentScreenFactory.js';
24 import {SyncStates} from 'sdc-app/common/merge/MergeEditorConstants.js';
25
26
27 describe('Feature Groups Module Tests', function () {
28
29         const LICENSE_MODEL_ID = '555';
30         const version = VersionFactory.build();
31         const itemPermissionAndProps = CurrentScreenFactory.build({}, {version});
32         const returnedVersionFields = {baseId: version.baseId, description: version.description, id: version.id, name: version.name, status: version.status};
33
34         it('Load Feature Groups List', () => {
35
36                 const featureGroupsList = buildListFromFactory(FeatureGroupStoreFactory);
37                 deepFreeze(featureGroupsList);
38
39                 const store = storeCreator();
40                 deepFreeze(store.getState());
41
42                 const expectedStore = cloneAndSet(store.getState(), 'licenseModel.featureGroup.featureGroupsList', featureGroupsList);
43
44                 mockRest.addHandler('fetch', ({data, options, baseUrl}) => {
45                         expect(baseUrl).toEqual(`/onboarding-api/v1.0/vendor-license-models/${LICENSE_MODEL_ID}/versions/${version.id}/feature-groups`);
46                         expect(data).toEqual(undefined);
47                         expect(options).toEqual(undefined);
48                         return {results: featureGroupsList};
49                 });
50
51                 return FeatureGroupsActionHelper.fetchFeatureGroupsList(store.dispatch, {licenseModelId: LICENSE_MODEL_ID, version}).then(() => {
52                         expect(store.getState()).toEqual(expectedStore);
53                 });
54         });
55
56         it('Delete Feature Group', () => {
57                 const featureGroupsList = buildListFromFactory(FeatureGroupStoreFactory, 1);
58                 deepFreeze(featureGroupsList);
59                 const store = storeCreator({
60                         currentScreen: {...itemPermissionAndProps},
61                         licenseModel: {
62                                 featureGroup: {
63                                         featureGroupsList
64                                 }
65                         }
66                 });
67                 deepFreeze(store.getState());
68
69                 const expectedCurrentScreenProps = {
70                         ...itemPermissionAndProps,
71                         itemPermission: {
72                                 ...itemPermissionAndProps.itemPermission,
73                                 isDirty: true
74                         }
75                 };
76
77                 let expectedStore = cloneAndSet(store.getState(), 'licenseModel.featureGroup.featureGroupsList', []);
78                 expectedStore = cloneAndSet(expectedStore, 'currentScreen.itemPermission', expectedCurrentScreenProps.itemPermission);
79
80                 const idToDelete = featureGroupsList[0].id;
81
82                 mockRest.addHandler('destroy', ({data, options, baseUrl}) => {
83                         expect(baseUrl).toEqual(`/onboarding-api/v1.0/vendor-license-models/${LICENSE_MODEL_ID}/versions/${version.id}/feature-groups/${idToDelete}`);
84                         expect(data).toEqual(undefined);
85                         expect(options).toEqual(undefined);
86                         return {
87                                 results: {
88                                         returnCode: 'OK'
89                                 }
90                         };
91                 });
92
93                 mockRest.addHandler('fetch', ({data, options, baseUrl}) => {
94                         expect(baseUrl).toEqual(`/onboarding-api/v1.0/items/${LICENSE_MODEL_ID}/versions/${version.id}`);
95                         expect(data).toEqual(undefined);
96                         expect(options).toEqual(undefined);
97                         return {...returnedVersionFields, state: {synchronizationState: SyncStates.UP_TO_DATE, dirty: true}};
98                 });
99
100                 return FeatureGroupsActionHelper.deleteFeatureGroup(store.dispatch, {
101                         licenseModelId: LICENSE_MODEL_ID,
102                         version,
103                         featureGroupId: idToDelete
104                 }).then(() => {
105                         expect(store.getState()).toEqual(expectedStore);
106                 });
107         });
108
109         it('Add Feature Group', () => {
110
111                 const store = storeCreator({
112                         currentScreen: {...itemPermissionAndProps},
113                         licenseModel: {
114                                 featureGroup: {
115                                         featureGroupsList: []
116                                 }
117                         }
118                 });
119                 deepFreeze(store.getState());
120
121                 const FeatureGroupPostRequest = FeatureGroupPostFactory.build({
122                         addedLicenseKeyGroupsIds: [1],
123                         addedEntitlementPoolsIds: [1]
124                 });
125                 const featureGroupToAdd = FeatureGroupDispatchFactory.build({
126                         licenseKeyGroupsIds: [1],
127                         entitlementPoolsIds: [1]
128                 });
129
130                 const featureGroupIdFromResponse = 'ADDED_ID';
131                 const featureGroupAfterAdd = FeatureGroupStoreFactory.build({
132                         ...featureGroupToAdd,
133                         id: featureGroupIdFromResponse
134                 });
135
136                 const expectedCurrentScreenProps = {
137                         ...itemPermissionAndProps,
138                         itemPermission: {
139                                 ...itemPermissionAndProps.itemPermission,
140                                 isDirty: true
141                         }
142                 };
143
144                 let expectedStore = cloneAndSet(store.getState(), 'licenseModel.featureGroup.featureGroupsList', [featureGroupAfterAdd]);
145                 expectedStore = cloneAndSet(expectedStore, 'currentScreen.itemPermission', expectedCurrentScreenProps.itemPermission);
146
147                 mockRest.addHandler('post', ({data, options, baseUrl}) => {
148                         expect(baseUrl).toEqual(`/onboarding-api/v1.0/vendor-license-models/${LICENSE_MODEL_ID}/versions/${version.id}/feature-groups`);
149                         expect(data).toEqual(FeatureGroupPostRequest);
150                         expect(options).toEqual(undefined);
151                         return {
152                                 returnCode: 'OK',
153                                 value: featureGroupIdFromResponse
154                         };
155                 });
156
157                 mockRest.addHandler('fetch', ({data, options, baseUrl}) => {
158                         expect(baseUrl).toEqual(`/onboarding-api/v1.0/vendor-license-models/${LICENSE_MODEL_ID}/versions/${version.id}/entitlement-pools`);
159                         expect(data).toEqual(undefined);
160                         expect(options).toEqual(undefined);
161                         return {results: []};
162                 });
163
164                 mockRest.addHandler('fetch', ({data, options, baseUrl}) => {
165                         expect(baseUrl).toEqual(`/onboarding-api/v1.0/vendor-license-models/${LICENSE_MODEL_ID}/versions/${version.id}/license-key-groups`);
166                         expect(data).toEqual(undefined);
167                         expect(options).toEqual(undefined);
168                         return {results: []};
169                 });
170
171                 mockRest.addHandler('fetch', ({data, options, baseUrl}) => {
172                         expect(baseUrl).toEqual(`/onboarding-api/v1.0/items/${LICENSE_MODEL_ID}/versions/${version.id}`);
173                         expect(data).toEqual(undefined);
174                         expect(options).toEqual(undefined);
175                         return {...returnedVersionFields, state: {synchronizationState: SyncStates.UP_TO_DATE, dirty: true}};
176                 });
177
178
179                 return FeatureGroupsActionHelper.saveFeatureGroup(store.dispatch, {
180                         licenseModelId: LICENSE_MODEL_ID,
181                         version,
182                         featureGroup: featureGroupToAdd
183                 }).then(() => {
184                         expect(store.getState()).toEqual(expectedStore);
185                 });
186         });
187
188         it('Update Feature Group', () => {
189                 const featureGroupsList = buildListFromFactory(FeatureGroupStoreFactory, 1, {
190                         licenseKeyGroupsIds: [1],
191                         entitlementPoolsIds: [1]
192                 });
193                 deepFreeze(featureGroupsList);
194
195                 const store = storeCreator({
196                         currentScreen: {...itemPermissionAndProps},
197                         licenseModel: {
198                                 featureGroup: {
199                                         featureGroupsList
200                                 }
201                         }
202                 });
203                 deepFreeze(store.getState());
204
205                 const toBeUpdatedFeatureGroupId = featureGroupsList[0].id;
206                 const previousFeatureGroupData = featureGroupsList[0];
207
208                 const featureGroupUpdateData = FeatureGroupStoreFactory.build({
209                         ...previousFeatureGroupData,
210                         licenseKeyGroupsIds: [7],
211                         entitlementPoolsIds: [7]
212                 });
213                 deepFreeze(featureGroupUpdateData);
214
215                 const FeatureGroupPutFactoryRequest = FeatureGroupPutFactory.build({
216                         name: featureGroupUpdateData.name,
217                         description: featureGroupUpdateData.description,
218                         partNumber: featureGroupUpdateData.partNumber,
219                         addedLicenseKeyGroupsIds: [7],
220                         addedEntitlementPoolsIds: [7],
221                         removedLicenseKeyGroupsIds: [1],
222                         removedEntitlementPoolsIds: [1]
223                 });
224                 deepFreeze(FeatureGroupPutFactoryRequest);
225
226                 const expectedCurrentScreenProps = {
227                         ...itemPermissionAndProps,
228                         itemPermission: {
229                                 ...itemPermissionAndProps.itemPermission,
230                                 isDirty: true
231                         }
232                 };
233
234                 let expectedStore = cloneAndSet(store.getState(), 'licenseModel.featureGroup.featureGroupsList', [featureGroupUpdateData]);
235                 expectedStore = cloneAndSet(expectedStore, 'currentScreen.itemPermission', expectedCurrentScreenProps.itemPermission);
236
237
238                 mockRest.addHandler('put', ({data, options, baseUrl}) => {
239                         expect(baseUrl).toEqual(`/onboarding-api/v1.0/vendor-license-models/${LICENSE_MODEL_ID}/versions/${version.id}/feature-groups/${toBeUpdatedFeatureGroupId}`);
240                         expect(data).toEqual(FeatureGroupPutFactoryRequest);
241                         expect(options).toEqual(undefined);
242                         return {returnCode: 'OK'};
243                 });
244
245                 mockRest.addHandler('fetch', ({data, options, baseUrl}) => {
246                         expect(baseUrl).toEqual(`/onboarding-api/v1.0/vendor-license-models/${LICENSE_MODEL_ID}/versions/${version.id}/entitlement-pools`);
247                         expect(data).toEqual(undefined);
248                         expect(options).toEqual(undefined);
249                         return {results: []};
250                 });
251
252                 mockRest.addHandler('fetch', ({data, options, baseUrl}) => {
253                         expect(baseUrl).toEqual(`/onboarding-api/v1.0/vendor-license-models/${LICENSE_MODEL_ID}/versions/${version.id}/license-key-groups`);
254                         expect(data).toEqual(undefined);
255                         expect(options).toEqual(undefined);
256                         return {results: []};
257                 });
258
259                 mockRest.addHandler('fetch', ({data, options, baseUrl}) => {
260                         expect(baseUrl).toEqual(`/onboarding-api/v1.0/items/${LICENSE_MODEL_ID}/versions/${version.id}`);
261                         expect(data).toEqual(undefined);
262                         expect(options).toEqual(undefined);
263                         return {...returnedVersionFields, state: {synchronizationState: SyncStates.UP_TO_DATE, dirty: true}};
264                 });
265
266                 return FeatureGroupsActionHelper.saveFeatureGroup(store.dispatch, {
267                         licenseModelId: LICENSE_MODEL_ID,
268                         version,
269                         previousFeatureGroup: previousFeatureGroupData,
270                         featureGroup: featureGroupUpdateData
271                 }).then(() => {
272                         expect(store.getState()).toEqual(expectedStore);
273                 });
274
275         });
276
277         it('Open Editor', () => {
278
279                 const store = storeCreator();
280                 deepFreeze(store.getState());
281
282                 const editorData = FeatureGroupStoreFactory.build();
283                 deepFreeze(editorData);
284                 const LICENSE_MODEL_ID = '123';
285
286                 mockRest.addHandler('fetch', ({data, options, baseUrl}) => {
287                         expect(baseUrl).toEqual(`/onboarding-api/v1.0/vendor-license-models/${LICENSE_MODEL_ID}/versions/${version.id}/entitlement-pools`);
288                         expect(data).toEqual(undefined);
289                         expect(options).toEqual(undefined);
290                         return {results: []};
291                 });
292
293                 mockRest.addHandler('fetch', ({data, options, baseUrl}) => {
294                         expect(baseUrl).toEqual(`/onboarding-api/v1.0/vendor-license-models/${LICENSE_MODEL_ID}/versions/${version.id}/license-key-groups`);
295                         expect(data).toEqual(undefined);
296                         expect(options).toEqual(undefined);
297                         return {results: []};
298                 });
299
300
301                 return FeatureGroupsActionHelper.openFeatureGroupsEditor(store.dispatch, {featureGroup: editorData, licenseModelId: '123', version}).then(() => {
302                         expect(store.getState().licenseModel.featureGroup.featureGroupEditor.data).toEqual(editorData);
303                 });
304         });
305
306 });