Add new code new version
[sdc.git] / openecomp-ui / test / licenseModel / licenseAgreement / test.js
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 import {expect} from 'chai';
22 import deepFreeze from 'deep-freeze';
23 import mockRest from 'test-utils/MockRest.js';
24 import {cloneAndSet} from 'test-utils/Util.js';
25 import {storeCreator} from 'sdc-app/AppStore.js';
26 import LicenseAgreementActionHelper from 'sdc-app/onboarding/licenseModel/licenseAgreement/LicenseAgreementActionHelper.js';
27
28
29 describe('License Agreement Module Tests', () => {
30
31         const LICENSE_MODEL_ID = '777';
32
33         it('Load License Agreement List', () => {
34                 const licenseAgreementList = [
35                         {
36                                 id: '0',
37                                 name: 'name0',
38                                 description: 'description0',
39                                 licenseTerm: 'licenseTerm0',
40                                 requirementsAndConstrains: 'req_and_constraints0',
41                                 featureGroupsIds: ['77']
42                         }
43                 ];
44                 deepFreeze(licenseAgreementList);
45                 const store = storeCreator();
46                 deepFreeze(store.getState());
47
48                 const expectedStore = cloneAndSet(store.getState(), 'licenseModel.licenseAgreement.licenseAgreementList', licenseAgreementList);
49
50                 mockRest.addHandler('fetch', ({options, data, baseUrl}) => {
51                         expect(baseUrl).to.equal(`/onboarding-api/v1.0/vendor-license-models/${LICENSE_MODEL_ID}/license-agreements`);
52                         expect(data).to.equal(undefined);
53                         expect(options).to.equal(undefined);
54                         return {results: licenseAgreementList};
55                 });
56                 return LicenseAgreementActionHelper.fetchLicenseAgreementList(store.dispatch, {licenseModelId: LICENSE_MODEL_ID}).then(() => {
57                         expect(store.getState()).to.deep.equal(expectedStore);
58                 });
59         });
60
61         it('Delete License Agreement', () => {
62                 const licenseAgreementList = [
63                         {
64                                 id: '0',
65                                 name: 'name0',
66                                 description: 'description0',
67                                 licenseTerm: 'licenseTerm0',
68                                 requirementsAndConstrains: 'req_and_constraints0',
69                                 featureGroupsIds: ['77']
70                         }
71                 ];
72                 deepFreeze(licenseAgreementList);
73                 const store = storeCreator({
74                         licenseModel: {
75                                 licenseAgreement: {
76                                         licenseAgreementList
77                                 }
78                         }
79                 });
80                 deepFreeze(store.getState());
81                 const toBeDeletedLicenseAgreementId = '0';
82                 const expectedStore = cloneAndSet(store.getState(), 'licenseModel.licenseAgreement.licenseAgreementList', []);
83
84                 mockRest.addHandler('destroy', ({data, options, baseUrl}) => {
85                         expect(baseUrl).to.equal(`/onboarding-api/v1.0/vendor-license-models/${LICENSE_MODEL_ID}/license-agreements/${toBeDeletedLicenseAgreementId}`);
86                         expect(data).to.equal(undefined);
87                         expect(options).to.equal(undefined);
88                 });
89
90                 return LicenseAgreementActionHelper.deleteLicenseAgreement(store.dispatch, {
91                         licenseAgreementId: toBeDeletedLicenseAgreementId,
92                         licenseModelId: LICENSE_MODEL_ID
93                 }).then(() => {
94                         expect(store.getState()).to.deep.equal(expectedStore);
95                 });
96         });
97
98         it('Add License Agreement', () => {
99                 const store = storeCreator();
100                 deepFreeze(store.getState());
101                 const licenseAgreementPostRequest = {
102                         name: 'name_ADDED_LA',
103                         description: 'description_ADDED_LA',
104                         licenseTerm: 'licenseTerm_ADDED_LA',
105                         requirementsAndConstrains: 'req_and_constraints_ADDED_LA',
106                         addedFeatureGroupsIds: []
107                 };
108                 deepFreeze(licenseAgreementPostRequest);
109
110                 const licenseAgreementToAdd = {
111                         name: 'name_ADDED_LA',
112                         description: 'description_ADDED_LA',
113                         licenseTerm: 'licenseTerm_ADDED_LA',
114                         requirementsAndConstrains: 'req_and_constraints_ADDED_LA',
115                         featureGroupsIds: []
116                 };
117                 deepFreeze(licenseAgreementToAdd);
118
119                 const licenseAgreementIdFromResponse = 'ADDED_ID';
120                 const licenseAgreementAfterAdd = {
121                         ...licenseAgreementToAdd,
122                         id: licenseAgreementIdFromResponse
123                 };
124                 deepFreeze(licenseAgreementAfterAdd);
125
126                 const expectedStore = cloneAndSet(store.getState(), 'licenseModel.licenseAgreement.licenseAgreementList', [licenseAgreementAfterAdd]);
127
128                 mockRest.addHandler('create', ({options, data, baseUrl}) => {
129                         expect(baseUrl).to.equal(`/onboarding-api/v1.0/vendor-license-models/${LICENSE_MODEL_ID}/license-agreements`);
130                         expect(data).to.deep.equal(licenseAgreementPostRequest);
131                         expect(options).to.equal(undefined);
132                         return {
133                                 value: licenseAgreementIdFromResponse
134                         };
135                 });
136
137                 return LicenseAgreementActionHelper.saveLicenseAgreement(store.dispatch, {
138                         licenseAgreement: licenseAgreementToAdd,
139                         licenseModelId: LICENSE_MODEL_ID
140                 }).then(() => {
141                         expect(store.getState()).to.deep.equal(expectedStore);
142                 });
143         });
144
145         it('Update License Agreement', () => {
146                 const licenseAgreementList = [
147                         {
148                                 id: '0',
149                                 name: 'name0',
150                                 description: 'description0',
151                                 licenseTerm: 'licenseTerm0',
152                                 requirementsAndConstrains: 'req_and_constraints0',
153                                 featureGroupsIds: ['77']
154                         }
155                 ];
156                 const store = storeCreator({
157                         licenseModel: {
158                                 licenseAgreement: {
159                                         licenseAgreementList
160                                 }
161                         }
162                 });
163                 deepFreeze(store.getState());
164
165                 const toBeUpdatedLicenseAgreementId = licenseAgreementList[0].id;
166                 const previousLicenseAgreementData = licenseAgreementList[0];
167
168                 const licenseAgreementUpdateData = {
169                         ...licenseAgreementList[0],
170                         name: 'name_UPDATED',
171                         description: 'description_UPDATED',
172                         licenseTerm: 'licenseTerm_UPDATED_LA',
173                         requirementsAndConstrains: 'req_and_constraints_UPDATED_LA',
174                         featureGroupsIds: ['update_id_1', 'update_id_2']
175                 };
176                 deepFreeze(licenseAgreementUpdateData);
177
178                 const licenseAgreementPutRequest = {
179                         name: 'name_UPDATED',
180                         description: 'description_UPDATED',
181                         licenseTerm: 'licenseTerm_UPDATED_LA',
182                         requirementsAndConstrains: 'req_and_constraints_UPDATED_LA',
183                         addedFeatureGroupsIds: ['update_id_1', 'update_id_2'],
184                         removedFeatureGroupsIds: ['77']
185                 };
186                 deepFreeze(licenseAgreementPutRequest);
187
188                 const expectedStore = cloneAndSet(store.getState(), 'licenseModel.licenseAgreement.licenseAgreementList', [licenseAgreementUpdateData]);
189
190                 mockRest.addHandler('save', ({data, options, baseUrl}) => {
191                         expect(baseUrl).to.equal(`/onboarding-api/v1.0/vendor-license-models/${LICENSE_MODEL_ID}/license-agreements/${toBeUpdatedLicenseAgreementId}`);
192                         expect(data).to.deep.equal(licenseAgreementPutRequest);
193                         expect(options).to.equal(undefined);
194                 });
195
196                 return LicenseAgreementActionHelper.saveLicenseAgreement(store.dispatch, {
197                         licenseModelId: LICENSE_MODEL_ID,
198                         previousLicenseAgreement: previousLicenseAgreementData,
199                         licenseAgreement: licenseAgreementUpdateData
200                 }).then(() => {
201                         expect(store.getState()).to.deep.equal(expectedStore);
202                 });
203         });
204
205 });