Add new code new version
[sdc.git] / openecomp-ui / test / licenseModel / licenseKeyGroups / 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
27 import LicenseKeyGroupsActionHelper from 'sdc-app/onboarding/licenseModel/licenseKeyGroups/LicenseKeyGroupsActionHelper.js';
28
29 describe('License Key Groups Module Tests', function () {
30
31         const LICENSE_MODEL_ID = '555';
32         it('Load License Key Group', () => {
33                 const licenseKeyGroupsList = [
34                         {
35                                 name: 'lsk1',
36                                 description: 'string',
37                                 type: 'Unique',
38                                 operationalScope: {'choices': ['Data_Center'], 'other': ''},
39                                 id: '0'
40                         }
41                 ];
42                 deepFreeze(licenseKeyGroupsList);
43                 const store = storeCreator();
44                 deepFreeze(store.getState());
45
46                 const expectedStore = cloneAndSet(store.getState(), 'licenseModel.licenseKeyGroup.licenseKeyGroupsList', licenseKeyGroupsList);
47
48                 mockRest.addHandler('fetch', ({data, options, baseUrl}) => {
49                         expect(baseUrl).to.equal(`/onboarding-api/v1.0/vendor-license-models/${LICENSE_MODEL_ID}/license-key-groups`);
50                         expect(data).to.equal(undefined);
51                         expect(options).to.equal(undefined);
52                         return {results: licenseKeyGroupsList};
53                 });
54
55                 return LicenseKeyGroupsActionHelper.fetchLicenseKeyGroupsList(store.dispatch, {licenseModelId: LICENSE_MODEL_ID}).then(() => {
56                         expect(store.getState()).to.deep.equal(expectedStore);
57                 });
58         });
59
60         it('Delete License Key Group', () => {
61                 const licenseKeyGroupsList = [
62                         {
63                                 name: 'lsk1',
64                                 description: 'string',
65                                 type: 'Unique',
66                                 operationalScope: {'choices': ['Data_Center'], 'other': ''},
67                                 id: '0'
68                         }
69                 ];
70                 deepFreeze(licenseKeyGroupsList);
71                 const store = storeCreator({
72                         licenseModel: {
73                                 licenseKeyGroup: {
74                                         licenseKeyGroupsList
75                                 }
76                         }
77                 });
78                 deepFreeze(store.getState());
79                 const toBeDeletedLicenseKeyGroupId = '0';
80                 const expectedStore = cloneAndSet(store.getState(), 'licenseModel.licenseKeyGroup.licenseKeyGroupsList', []);
81
82                 mockRest.addHandler('destroy', ({data, options, baseUrl}) => {
83                         expect(baseUrl).to.equal(`/onboarding-api/v1.0/vendor-license-models/${LICENSE_MODEL_ID}/license-key-groups/${toBeDeletedLicenseKeyGroupId}`);
84                         expect(data).to.equal(undefined);
85                         expect(options).to.equal(undefined);
86                 });
87
88                 return LicenseKeyGroupsActionHelper.deleteLicenseKeyGroup(store.dispatch, {
89                         licenseKeyGroupId: toBeDeletedLicenseKeyGroupId,
90                         licenseModelId: LICENSE_MODEL_ID
91                 }).then(() => {
92                         expect(store.getState()).to.deep.equal(expectedStore);
93                 });
94         });
95
96         it('Add License Key Group', () => {
97
98                 const store = storeCreator();
99                 deepFreeze(store.getState());
100
101                 const licenseKeyGroupPostRequest = {
102                         name: 'lsk1_ADDED',
103                         description: 'string_ADDED',
104                         type: 'Unique_ADDED',
105                         operationalScope: {'choices': ['Data_Center'], 'other': ''}
106                 };
107                 deepFreeze(licenseKeyGroupPostRequest);
108
109                 const licenseKeyGroupToAdd = {
110                         ...licenseKeyGroupPostRequest
111                 };
112
113                 deepFreeze(licenseKeyGroupToAdd);
114
115                 const licenseKeyGroupIdFromResponse = 'ADDED_ID';
116                 const licenseKeyGroupAfterAdd = {
117                         ...licenseKeyGroupToAdd,
118                         id: licenseKeyGroupIdFromResponse
119                 };
120                 deepFreeze(licenseKeyGroupAfterAdd);
121
122                 const expectedStore = cloneAndSet(store.getState(), 'licenseModel.licenseKeyGroup.licenseKeyGroupsList', [licenseKeyGroupAfterAdd]);
123
124                 mockRest.addHandler('create', ({options, data, baseUrl}) => {
125                         expect(baseUrl).to.equal(`/onboarding-api/v1.0/vendor-license-models/${LICENSE_MODEL_ID}/license-key-groups`);
126                         expect(data).to.deep.equal(licenseKeyGroupPostRequest);
127                         expect(options).to.equal(undefined);
128                         return {
129                                 value: licenseKeyGroupIdFromResponse
130                         };
131                 });
132
133                 return LicenseKeyGroupsActionHelper.saveLicenseKeyGroup(store.dispatch, {
134                         licenseKeyGroup: licenseKeyGroupToAdd,
135                         licenseModelId: LICENSE_MODEL_ID
136                 }).then(() => {
137                         expect(store.getState()).to.deep.equal(expectedStore);
138                 });
139         });
140
141         it('Update License Key Group', () => {
142                 const licenseKeyGroupsList = [
143                         {
144                                 name: 'lsk1',
145                                 description: 'string',
146                                 type: 'Unique',
147                                 operationalScope: {'choices': ['Data_Center'], 'other': ''},
148                                 id: '0'
149                         }
150                 ];
151                 deepFreeze(licenseKeyGroupsList);
152                 const store = storeCreator({
153                         licenseModel: {
154                                 licenseKeyGroup: {
155                                         licenseKeyGroupsList
156                                 }
157                         }
158                 });
159
160                 const toBeUpdatedLicenseKeyGroupId = licenseKeyGroupsList[0].id;
161                 const previousLicenseKeyGroupData = licenseKeyGroupsList[0];
162
163                 const licenseKeyGroupUpdateData = {
164                         ...licenseKeyGroupsList[0],
165                         name: 'lsk1_UPDATE',
166                         description: 'string_UPDATE',
167                         type: 'Unique',
168                         operationalScope: {'choices': ['Data_Center'], 'other': ''}
169                 };
170                 deepFreeze(licenseKeyGroupUpdateData);
171
172                 const licenseKeyGroupPutRequest = {
173                         name: 'lsk1_UPDATE',
174                         description: 'string_UPDATE',
175                         type: 'Unique',
176                         operationalScope: {'choices': ['Data_Center'], 'other': ''}
177                 };
178                 deepFreeze(licenseKeyGroupPutRequest);
179
180                 const expectedStore = cloneAndSet(store.getState(), 'licenseModel.licenseKeyGroup.licenseKeyGroupsList', [licenseKeyGroupUpdateData]);
181
182                 mockRest.addHandler('save', ({data, options, baseUrl}) => {
183                         expect(baseUrl).to.equal(`/onboarding-api/v1.0/vendor-license-models/${LICENSE_MODEL_ID}/license-key-groups/${toBeUpdatedLicenseKeyGroupId}`);
184                         expect(data).to.deep.equal(licenseKeyGroupPutRequest);
185                         expect(options).to.equal(undefined);
186                 });
187
188                 return LicenseKeyGroupsActionHelper.saveLicenseKeyGroup(store.dispatch, {
189                         previousLicenseKeyGroup: previousLicenseKeyGroupData,
190                         licenseKeyGroup: licenseKeyGroupUpdateData,
191                         licenseModelId: LICENSE_MODEL_ID
192                 }).then(() => {
193                         expect(store.getState()).to.deep.equal(expectedStore);
194                 });
195         });
196
197 });