Add collaboration feature
[sdc.git] / openecomp-ui / test / licenseModel / licenseAgreement / 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 pickBy from 'lodash/pickBy';
18 import mockRest from 'test-utils/MockRest.js';
19 import {cloneAndSet, buildListFromFactory} from 'test-utils/Util.js';
20 import {storeCreator} from 'sdc-app/AppStore.js';
21 import LicenseAgreementActionHelper from 'sdc-app/onboarding/licenseModel/licenseAgreement/LicenseAgreementActionHelper.js';
22
23 import { LicenseAgreementStoreFactory, LicenseAgreementDispatchFactory, LicenseAgreementPostFactory, LicenseAgreementPutFactory } from 'test-utils/factories/licenseModel/LicenseAgreementFactories.js';
24 import VersionFactory from 'test-utils/factories/common/VersionFactory.js';
25 import {SyncStates} from 'sdc-app/common/merge/MergeEditorConstants.js';
26 import CurrentScreenFactory from 'test-utils/factories/common/CurrentScreenFactory.js';
27
28 describe('License Agreement Module Tests', () => {
29
30         const LICENSE_MODEL_ID = '777';
31         const version = VersionFactory.build();
32         const itemPermissionAndProps = CurrentScreenFactory.build({}, {version});
33         const returnedVersionFields = {baseId: version.baseId, description: version.description, id: version.id, name: version.name, status: version.status};
34
35         it('Load License Agreement List', () => {
36                 const licenseAgreementList = buildListFromFactory(LicenseAgreementStoreFactory);
37
38                 const store = storeCreator();
39                 deepFreeze(store.getState());
40
41                 const expectedStore = cloneAndSet(store.getState(), 'licenseModel.licenseAgreement.licenseAgreementList', licenseAgreementList);
42
43                 mockRest.addHandler('fetch', ({options, data, baseUrl}) => {
44                         expect(baseUrl).toEqual(`/onboarding-api/v1.0/vendor-license-models/${LICENSE_MODEL_ID}/versions/${version.id}/license-agreements`);
45                         expect(data).toEqual(undefined);
46                         expect(options).toEqual(undefined);
47                         return {results: licenseAgreementList};
48                 });
49                 return LicenseAgreementActionHelper.fetchLicenseAgreementList(store.dispatch, {licenseModelId: LICENSE_MODEL_ID, version}).then(() => {
50                         expect(store.getState()).toEqual(expectedStore);
51                 });
52         });
53
54         it('Delete License Agreement', () => {
55                 const licenseAgreementList = buildListFromFactory(LicenseAgreementStoreFactory, 1);
56                 const store = storeCreator({
57                         currentScreen: {...itemPermissionAndProps},
58                         licenseModel: {
59                                 licenseAgreement: {
60                                         licenseAgreementList
61                                 }
62                         }
63                 });
64                 deepFreeze(store.getState());
65                 const expectedCurrentScreenProps = {
66                         ...itemPermissionAndProps,
67                         itemPermission: {
68                                 ...itemPermissionAndProps.itemPermission,
69                                 isDirty: true
70                         }
71                 };
72                 const toBeDeletedLicenseAgreementId = licenseAgreementList[0].id;
73                 let  expectedStore = cloneAndSet(store.getState(), 'licenseModel.licenseAgreement.licenseAgreementList', []);
74                 expectedStore = cloneAndSet(expectedStore, 'currentScreen.itemPermission', expectedCurrentScreenProps.itemPermission);
75
76                 mockRest.addHandler('destroy', ({data, options, baseUrl}) => {
77                         expect(baseUrl).toEqual(`/onboarding-api/v1.0/vendor-license-models/${LICENSE_MODEL_ID}/versions/${version.id}/license-agreements/${toBeDeletedLicenseAgreementId}`);
78                         expect(data).toEqual(undefined);
79                         expect(options).toEqual(undefined);
80                 });
81                 mockRest.addHandler('fetch', ({data, options, baseUrl}) => {
82                         expect(baseUrl).toEqual(`/onboarding-api/v1.0/items/${LICENSE_MODEL_ID}/versions/${version.id}`);
83                         expect(data).toEqual(undefined);
84                         expect(options).toEqual(undefined);
85                         return {...returnedVersionFields, state: {synchronizationState: SyncStates.UP_TO_DATE, dirty: true}};
86                 });
87
88                 return LicenseAgreementActionHelper.deleteLicenseAgreement(store.dispatch, {
89                         licenseAgreementId: toBeDeletedLicenseAgreementId,
90                         licenseModelId: LICENSE_MODEL_ID,
91                         version
92                 }).then(() => {
93                         expect(store.getState()).toEqual(expectedStore);
94                 });
95         });
96
97         it('Add License Agreement', () => {
98                 const store = storeCreator({
99                         currentScreen: {...itemPermissionAndProps}
100                 });
101                 deepFreeze(store.getState());
102
103                 const licenseAgreementToAdd = LicenseAgreementDispatchFactory.build();
104
105                 const LicenseAgreementPostRequest = LicenseAgreementPostFactory.build(
106                         pickBy(licenseAgreementToAdd, (val, key) => key !== 'featureGroupsIds')
107                 );
108
109                 deepFreeze(LicenseAgreementPostRequest);
110
111                 const licenseAgreementIdFromResponse = 'ADDED_ID';
112                 const licenseAgreementAfterAdd = LicenseAgreementStoreFactory.build({
113                         ...licenseAgreementToAdd,
114                         id: licenseAgreementIdFromResponse
115                 });
116                 deepFreeze(licenseAgreementAfterAdd);
117                 const licenseAgreementList = [licenseAgreementAfterAdd];
118                 const expectedCurrentScreenProps = {
119                         ...itemPermissionAndProps,
120                         itemPermission: {
121                                 ...itemPermissionAndProps.itemPermission,
122                                 isDirty: true
123                         }
124                 };
125                 const featureGroupsList = licenseAgreementList.featureGroupsIds;
126                 let expectedStore = cloneAndSet(store.getState(), 'licenseModel.licenseAgreement.licenseAgreementList', [licenseAgreementAfterAdd]);
127                 expectedStore = cloneAndSet(expectedStore, 'currentScreen.itemPermission', expectedCurrentScreenProps.itemPermission);
128
129                 mockRest.addHandler('post', ({options, data, baseUrl}) => {
130                         expect(baseUrl).toEqual(`/onboarding-api/v1.0/vendor-license-models/${LICENSE_MODEL_ID}/versions/${version.id}/license-agreements`);
131                         expect(data).toEqual(LicenseAgreementPostRequest);
132                         expect(options).toEqual(undefined);
133                         return {
134                                 value: licenseAgreementIdFromResponse
135                         };
136                 });
137                 mockRest.addHandler('fetch', ({options, data, baseUrl}) => {
138                         expect(baseUrl).toEqual(`/onboarding-api/v1.0/vendor-license-models/${LICENSE_MODEL_ID}/versions/${version.id}/license-agreements`);
139                         expect(data).toEqual(undefined);
140                         expect(options).toEqual(undefined);
141                         return {results: licenseAgreementList};
142                 });
143                 mockRest.addHandler('fetch', ({options, data, baseUrl}) => {
144                         expect(baseUrl).toEqual(`/onboarding-api/v1.0/vendor-license-models/${LICENSE_MODEL_ID}/versions/${version.id}/feature-groups`);
145                         expect(data).toEqual(undefined);
146                         expect(options).toEqual(undefined);
147                         return {results: featureGroupsList};
148                 });
149                 mockRest.addHandler('fetch', ({data, options, baseUrl}) => {
150                         expect(baseUrl).toEqual(`/onboarding-api/v1.0/items/${LICENSE_MODEL_ID}/versions/${version.id}`);
151                         expect(data).toEqual(undefined);
152                         expect(options).toEqual(undefined);
153                         return {...returnedVersionFields, state: {synchronizationState: SyncStates.UP_TO_DATE, dirty: true}};
154
155                 });
156                 return LicenseAgreementActionHelper.saveLicenseAgreement(store.dispatch, {
157                         licenseAgreement: licenseAgreementToAdd,
158                         licenseModelId: LICENSE_MODEL_ID,
159                         version
160                 }).then(() => {
161                         expect(store.getState()).toEqual(expectedStore);
162                 });
163         });
164
165         it('Update License Agreement', () => {
166                 const licenseAgreementList = buildListFromFactory(LicenseAgreementStoreFactory, 1, {featureGroupsIds: ['77']});
167                 const store = storeCreator({
168                         currentScreen: {...itemPermissionAndProps},
169                         licenseModel: {
170                                 licenseAgreement: {
171                                         licenseAgreementList
172                                 }
173                         }
174                 });
175                 deepFreeze(store.getState());
176
177                 const previousLicenseAgreementData = licenseAgreementList[0];
178                 const toBeUpdatedLicenseAgreementId = previousLicenseAgreementData.id;
179                 const oldFeatureGroupIds = previousLicenseAgreementData.featureGroupsIds;
180
181                 const newFeatureGroupsIds = ['update_id_1', 'update_id_2'];
182
183                 const licenseAgreementUpdateData = LicenseAgreementStoreFactory.build({
184                         id: toBeUpdatedLicenseAgreementId,
185                         featureGroupsIds: newFeatureGroupsIds
186                 });
187                 deepFreeze(licenseAgreementUpdateData);
188
189                 const LicenseAgreementPutFactoryRequest = LicenseAgreementPutFactory.build({
190                         addedFeatureGroupsIds: newFeatureGroupsIds,
191                         removedFeatureGroupsIds: oldFeatureGroupIds
192                 });
193
194                 deepFreeze(LicenseAgreementPutFactoryRequest);
195
196                 const expectedCurrentScreenProps = {
197                         ...itemPermissionAndProps,
198                         itemPermission: {
199                                 ...itemPermissionAndProps.itemPermission,
200                                 isDirty: true
201                         }
202                 };
203                 let expectedStore = cloneAndSet(store.getState(), 'licenseModel.licenseAgreement.licenseAgreementList', [licenseAgreementUpdateData]);
204                 expectedStore = cloneAndSet(expectedStore, 'currentScreen.itemPermission', expectedCurrentScreenProps.itemPermission);
205
206                 mockRest.addHandler('put', ({data, options, baseUrl}) => {
207                         expect(baseUrl).toEqual(`/onboarding-api/v1.0/vendor-license-models/${LICENSE_MODEL_ID}/versions/${version.id}/license-agreements/${toBeUpdatedLicenseAgreementId}`);
208                         expect(data).toEqual(LicenseAgreementPutFactoryRequest);
209                         expect(options).toEqual(undefined);
210                 });
211                 mockRest.addHandler('fetch', ({options, data, baseUrl}) => {
212                         expect(baseUrl).toEqual(`/onboarding-api/v1.0/vendor-license-models/${LICENSE_MODEL_ID}/versions/${version.id}/license-agreements`);
213                         expect(data).toEqual(undefined);
214                         expect(options).toEqual(undefined);
215                         return {results: [licenseAgreementUpdateData]};
216                 });
217                 mockRest.addHandler('fetch', ({data, options, baseUrl}) => {
218                         expect(baseUrl).toEqual(`/onboarding-api/v1.0/items/${LICENSE_MODEL_ID}/versions/${version.id}`);
219                         expect(data).toEqual(undefined);
220                         expect(options).toEqual(undefined);
221                         return {...returnedVersionFields, state: {synchronizationState: SyncStates.UP_TO_DATE, dirty: true}};
222                 });
223                 mockRest.addHandler('fetch', ({options, data, baseUrl}) => {
224                         expect(baseUrl).toEqual(`/onboarding-api/v1.0/vendor-license-models/${LICENSE_MODEL_ID}/versions/${version.id}/feature-groups`);
225                         expect(data).toEqual(undefined);
226                         expect(options).toEqual(undefined);
227                         return {results: newFeatureGroupsIds};
228                 });
229
230                 return LicenseAgreementActionHelper.saveLicenseAgreement(store.dispatch, {
231                         licenseModelId: LICENSE_MODEL_ID,
232                         version,
233                         previousLicenseAgreement: previousLicenseAgreementData,
234                         licenseAgreement: licenseAgreementUpdateData
235                 }).then(() => {
236                         expect(store.getState()).toEqual(expectedStore);
237                 });
238         });
239
240 });