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 VersionControllerUtilsFactory from 'test-utils/factories/softwareProduct/VersionControllerUtilsFactory.js';
26 describe('License Agreement Module Tests', () => {
28 const LICENSE_MODEL_ID = '777';
29 const version = VersionControllerUtilsFactory.build().version;
31 it('Load License Agreement List', () => {
32 const licenseAgreementList = buildListFromFactory(LicenseAgreementStoreFactory);
34 const store = storeCreator();
35 deepFreeze(store.getState());
37 const expectedStore = cloneAndSet(store.getState(), 'licenseModel.licenseAgreement.licenseAgreementList', licenseAgreementList);
39 mockRest.addHandler('fetch', ({options, data, baseUrl}) => {
40 expect(baseUrl).toEqual(`/onboarding-api/v1.0/vendor-license-models/${LICENSE_MODEL_ID}/versions/${version.id}/license-agreements`);
41 expect(data).toEqual(undefined);
42 expect(options).toEqual(undefined);
43 return {results: licenseAgreementList};
45 return LicenseAgreementActionHelper.fetchLicenseAgreementList(store.dispatch, {licenseModelId: LICENSE_MODEL_ID, version}).then(() => {
46 expect(store.getState()).toEqual(expectedStore);
50 it('Delete License Agreement', () => {
51 const licenseAgreementList = buildListFromFactory(LicenseAgreementStoreFactory, 1);
52 const store = storeCreator({
59 deepFreeze(store.getState());
60 const toBeDeletedLicenseAgreementId = licenseAgreementList[0].id;
61 const expectedStore = cloneAndSet(store.getState(), 'licenseModel.licenseAgreement.licenseAgreementList', []);
63 mockRest.addHandler('destroy', ({data, options, baseUrl}) => {
64 expect(baseUrl).toEqual(`/onboarding-api/v1.0/vendor-license-models/${LICENSE_MODEL_ID}/versions/${version.id}/license-agreements/${toBeDeletedLicenseAgreementId}`);
65 expect(data).toEqual(undefined);
66 expect(options).toEqual(undefined);
69 return LicenseAgreementActionHelper.deleteLicenseAgreement(store.dispatch, {
70 licenseAgreementId: toBeDeletedLicenseAgreementId,
71 licenseModelId: LICENSE_MODEL_ID,
74 expect(store.getState()).toEqual(expectedStore);
78 it('Add License Agreement', () => {
79 const store = storeCreator();
80 deepFreeze(store.getState());
82 const licenseAgreementToAdd = LicenseAgreementDispatchFactory.build();
84 const LicenseAgreementPostRequest = LicenseAgreementPostFactory.build(
85 pickBy(licenseAgreementToAdd, (val, key) => key !== 'featureGroupsIds')
88 deepFreeze(LicenseAgreementPostRequest);
90 const licenseAgreementIdFromResponse = 'ADDED_ID';
91 const licenseAgreementAfterAdd = LicenseAgreementStoreFactory.build({
92 ...licenseAgreementToAdd,
93 id: licenseAgreementIdFromResponse
95 deepFreeze(licenseAgreementAfterAdd);
97 const expectedStore = cloneAndSet(store.getState(), 'licenseModel.licenseAgreement.licenseAgreementList', [licenseAgreementAfterAdd]);
99 mockRest.addHandler('post', ({options, data, baseUrl}) => {
100 expect(baseUrl).toEqual(`/onboarding-api/v1.0/vendor-license-models/${LICENSE_MODEL_ID}/versions/${version.id}/license-agreements`);
101 expect(data).toEqual(LicenseAgreementPostRequest);
102 expect(options).toEqual(undefined);
104 value: licenseAgreementIdFromResponse
108 return LicenseAgreementActionHelper.saveLicenseAgreement(store.dispatch, {
109 licenseAgreement: licenseAgreementToAdd,
110 licenseModelId: LICENSE_MODEL_ID,
113 expect(store.getState()).toEqual(expectedStore);
117 it('Update License Agreement', () => {
118 const licenseAgreementList = buildListFromFactory(LicenseAgreementStoreFactory, 1, {featureGroupsIds: ['77']});
119 const store = storeCreator({
126 deepFreeze(store.getState());
128 const previousLicenseAgreementData = licenseAgreementList[0];
129 const toBeUpdatedLicenseAgreementId = previousLicenseAgreementData.id;
130 const oldFeatureGroupIds = previousLicenseAgreementData.featureGroupsIds;
132 const newFeatureGroupsIds = ['update_id_1', 'update_id_2'];
134 const licenseAgreementUpdateData = LicenseAgreementStoreFactory.build({
135 id: toBeUpdatedLicenseAgreementId,
136 featureGroupsIds: newFeatureGroupsIds
138 deepFreeze(licenseAgreementUpdateData);
140 const LicenseAgreementPutFactoryRequest = LicenseAgreementPutFactory.build({
141 addedFeatureGroupsIds: newFeatureGroupsIds,
142 removedFeatureGroupsIds: oldFeatureGroupIds
145 deepFreeze(LicenseAgreementPutFactoryRequest);
147 const expectedStore = cloneAndSet(store.getState(), 'licenseModel.licenseAgreement.licenseAgreementList', [licenseAgreementUpdateData]);
149 mockRest.addHandler('put', ({data, options, baseUrl}) => {
150 expect(baseUrl).toEqual(`/onboarding-api/v1.0/vendor-license-models/${LICENSE_MODEL_ID}/versions/${version.id}/license-agreements/${toBeUpdatedLicenseAgreementId}`);
151 expect(data).toEqual(LicenseAgreementPutFactoryRequest);
152 expect(options).toEqual(undefined);
155 return LicenseAgreementActionHelper.saveLicenseAgreement(store.dispatch, {
156 licenseModelId: LICENSE_MODEL_ID,
158 previousLicenseAgreement: previousLicenseAgreementData,
159 licenseAgreement: licenseAgreementUpdateData
161 expect(store.getState()).toEqual(expectedStore);