2 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
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';
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';
28 describe('License Agreement Module Tests', () => {
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};
35 it('Load License Agreement List', () => {
36 const licenseAgreementList = buildListFromFactory(LicenseAgreementStoreFactory);
38 const store = storeCreator();
39 deepFreeze(store.getState());
41 const expectedStore = cloneAndSet(store.getState(), 'licenseModel.licenseAgreement.licenseAgreementList', licenseAgreementList);
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};
49 return LicenseAgreementActionHelper.fetchLicenseAgreementList(store.dispatch, {licenseModelId: LICENSE_MODEL_ID, version}).then(() => {
50 expect(store.getState()).toEqual(expectedStore);
54 it('Delete License Agreement', () => {
55 const licenseAgreementList = buildListFromFactory(LicenseAgreementStoreFactory, 1);
56 const store = storeCreator({
57 currentScreen: {...itemPermissionAndProps},
64 deepFreeze(store.getState());
65 const expectedCurrentScreenProps = {
66 ...itemPermissionAndProps,
68 ...itemPermissionAndProps.itemPermission,
72 const toBeDeletedLicenseAgreementId = licenseAgreementList[0].id;
73 let expectedStore = cloneAndSet(store.getState(), 'licenseModel.licenseAgreement.licenseAgreementList', []);
74 expectedStore = cloneAndSet(expectedStore, 'currentScreen.itemPermission', expectedCurrentScreenProps.itemPermission);
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);
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}};
88 return LicenseAgreementActionHelper.deleteLicenseAgreement(store.dispatch, {
89 licenseAgreementId: toBeDeletedLicenseAgreementId,
90 licenseModelId: LICENSE_MODEL_ID,
93 expect(store.getState()).toEqual(expectedStore);
97 it('Add License Agreement', () => {
98 const store = storeCreator({
99 currentScreen: {...itemPermissionAndProps}
101 deepFreeze(store.getState());
103 const licenseAgreementToAdd = LicenseAgreementDispatchFactory.build();
105 const LicenseAgreementPostRequest = LicenseAgreementPostFactory.build(
106 pickBy(licenseAgreementToAdd, (val, key) => key !== 'featureGroupsIds')
109 deepFreeze(LicenseAgreementPostRequest);
111 const licenseAgreementIdFromResponse = 'ADDED_ID';
112 const licenseAgreementAfterAdd = LicenseAgreementStoreFactory.build({
113 ...licenseAgreementToAdd,
114 id: licenseAgreementIdFromResponse
116 deepFreeze(licenseAgreementAfterAdd);
117 const licenseAgreementList = [licenseAgreementAfterAdd];
118 const expectedCurrentScreenProps = {
119 ...itemPermissionAndProps,
121 ...itemPermissionAndProps.itemPermission,
125 const featureGroupsList = licenseAgreementList.featureGroupsIds;
126 let expectedStore = cloneAndSet(store.getState(), 'licenseModel.licenseAgreement.licenseAgreementList', [licenseAgreementAfterAdd]);
127 expectedStore = cloneAndSet(expectedStore, 'currentScreen.itemPermission', expectedCurrentScreenProps.itemPermission);
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);
134 value: licenseAgreementIdFromResponse
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};
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};
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}};
156 return LicenseAgreementActionHelper.saveLicenseAgreement(store.dispatch, {
157 licenseAgreement: licenseAgreementToAdd,
158 licenseModelId: LICENSE_MODEL_ID,
161 expect(store.getState()).toEqual(expectedStore);
165 it('Update License Agreement', () => {
166 const licenseAgreementList = buildListFromFactory(LicenseAgreementStoreFactory, 1, {featureGroupsIds: ['77']});
167 const store = storeCreator({
168 currentScreen: {...itemPermissionAndProps},
175 deepFreeze(store.getState());
177 const previousLicenseAgreementData = licenseAgreementList[0];
178 const toBeUpdatedLicenseAgreementId = previousLicenseAgreementData.id;
179 const oldFeatureGroupIds = previousLicenseAgreementData.featureGroupsIds;
181 const newFeatureGroupsIds = ['update_id_1', 'update_id_2'];
183 const licenseAgreementUpdateData = LicenseAgreementStoreFactory.build({
184 id: toBeUpdatedLicenseAgreementId,
185 featureGroupsIds: newFeatureGroupsIds
187 deepFreeze(licenseAgreementUpdateData);
189 const LicenseAgreementPutFactoryRequest = LicenseAgreementPutFactory.build({
190 addedFeatureGroupsIds: newFeatureGroupsIds,
191 removedFeatureGroupsIds: oldFeatureGroupIds
194 deepFreeze(LicenseAgreementPutFactoryRequest);
196 const expectedCurrentScreenProps = {
197 ...itemPermissionAndProps,
199 ...itemPermissionAndProps.itemPermission,
203 let expectedStore = cloneAndSet(store.getState(), 'licenseModel.licenseAgreement.licenseAgreementList', [licenseAgreementUpdateData]);
204 expectedStore = cloneAndSet(expectedStore, 'currentScreen.itemPermission', expectedCurrentScreenProps.itemPermission);
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);
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]};
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}};
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};
230 return LicenseAgreementActionHelper.saveLicenseAgreement(store.dispatch, {
231 licenseModelId: LICENSE_MODEL_ID,
233 previousLicenseAgreement: previousLicenseAgreementData,
234 licenseAgreement: licenseAgreementUpdateData
236 expect(store.getState()).toEqual(expectedStore);