[SDC-29] Amdocs OnBoard 1707 initial commit.
[sdc.git] / openecomp-ui / test / licenseModel / licenseKeyGroups / 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 mockRest from 'test-utils/MockRest.js';
18 import {cloneAndSet, buildListFromFactory} from 'test-utils/Util.js';
19 import {storeCreator} from 'sdc-app/AppStore.js';
20 import {LicenseKeyGroupStoreFactory, LicenseKeyGroupPostFactory} from 'test-utils/factories/licenseModel/LicenseKeyGroupFactories.js';
21
22 import LicenseKeyGroupsActionHelper from 'sdc-app/onboarding/licenseModel/licenseKeyGroups/LicenseKeyGroupsActionHelper.js';
23 import VersionControllerUtilsFactory from 'test-utils/factories/softwareProduct/VersionControllerUtilsFactory.js';
24
25 describe('License Key Groups Module Tests', function () {
26
27         const LICENSE_MODEL_ID = '555';
28         const version = VersionControllerUtilsFactory.build().version;
29
30         it('Load License Key Group', () => {
31
32                 const licenseKeyGroupsList = buildListFromFactory(LicenseKeyGroupStoreFactory);
33
34                 deepFreeze(licenseKeyGroupsList);
35                 const store = storeCreator();
36                 deepFreeze(store.getState());
37
38                 const expectedStore = cloneAndSet(store.getState(), 'licenseModel.licenseKeyGroup.licenseKeyGroupsList', licenseKeyGroupsList);
39
40                 mockRest.addHandler('fetch', ({data, options, baseUrl}) => {
41                         expect(baseUrl).toEqual(`/onboarding-api/v1.0/vendor-license-models/${LICENSE_MODEL_ID}/versions/${version.id}/license-key-groups`);
42                         expect(data).toEqual(undefined);
43                         expect(options).toEqual(undefined);
44                         return {results: licenseKeyGroupsList};
45                 });
46
47                 return LicenseKeyGroupsActionHelper.fetchLicenseKeyGroupsList(store.dispatch, {licenseModelId: LICENSE_MODEL_ID, version}).then(() => {
48                         expect(store.getState()).toEqual(expectedStore);
49                 });
50         });
51
52         it('Delete License Key Group', () => {
53
54                 const licenseKeyGroupsList = buildListFromFactory(LicenseKeyGroupStoreFactory, 1);
55
56                 deepFreeze(licenseKeyGroupsList);
57                 const store = storeCreator({
58                         licenseModel: {
59                                 licenseKeyGroup: {
60                                         licenseKeyGroupsList
61                                 }
62                         }
63                 });
64                 deepFreeze(store.getState());
65                 const toBeDeletedLicenseKeyGroupId = licenseKeyGroupsList[0].id;
66                 const expectedStore = cloneAndSet(store.getState(), 'licenseModel.licenseKeyGroup.licenseKeyGroupsList', []);
67
68                 mockRest.addHandler('destroy', ({data, options, baseUrl}) => {
69                         expect(baseUrl).toEqual(`/onboarding-api/v1.0/vendor-license-models/${LICENSE_MODEL_ID}/versions/${version.id}/license-key-groups/${toBeDeletedLicenseKeyGroupId}`);
70                         expect(data).toEqual(undefined);
71                         expect(options).toEqual(undefined);
72                 });
73
74                 return LicenseKeyGroupsActionHelper.deleteLicenseKeyGroup(store.dispatch, {
75                         licenseKeyGroupId: toBeDeletedLicenseKeyGroupId,
76                         licenseModelId: LICENSE_MODEL_ID,
77                         version
78                 }).then(() => {
79                         expect(store.getState()).toEqual(expectedStore);
80                 });
81         });
82
83         it('Add License Key Group', () => {
84
85                 const store = storeCreator();
86                 deepFreeze(store.getState());
87
88                 const LicenseKeyGroupPost = LicenseKeyGroupPostFactory.build();
89                 deepFreeze(LicenseKeyGroupPost);
90
91                 const LicenseKeyGroupStore = LicenseKeyGroupStoreFactory.build();
92                 deepFreeze(LicenseKeyGroupStore);
93
94                 const expectedStore = cloneAndSet(store.getState(), 'licenseModel.licenseKeyGroup.licenseKeyGroupsList', [LicenseKeyGroupStore]);
95
96                 mockRest.addHandler('post', ({options, data, baseUrl}) => {
97                         expect(baseUrl).toEqual(`/onboarding-api/v1.0/vendor-license-models/${LICENSE_MODEL_ID}/versions/${version.id}/license-key-groups`);
98                         expect(data).toEqual(LicenseKeyGroupPost);
99                         expect(options).toEqual(undefined);
100                         return {
101                                 value: LicenseKeyGroupStore.id
102                         };
103                 });
104
105                 return LicenseKeyGroupsActionHelper.saveLicenseKeyGroup(store.dispatch, {
106                         licenseKeyGroup: LicenseKeyGroupPost,
107                         licenseModelId: LICENSE_MODEL_ID,
108                         version
109                 }).then(() => {
110                         expect(store.getState()).toEqual(expectedStore);
111                 });
112         });
113
114         it('Update License Key Group', () => {
115                 const licenseKeyGroupsList = buildListFromFactory(LicenseKeyGroupStoreFactory, 1);
116                 deepFreeze(licenseKeyGroupsList);
117                 const store = storeCreator({
118                         licenseModel: {
119                                 licenseKeyGroup: {
120                                         licenseKeyGroupsList
121                                 }
122                         }
123                 });
124
125                 const toBeUpdatedLicenseKeyGroupId = licenseKeyGroupsList[0].id;
126                 const previousLicenseKeyGroupData = licenseKeyGroupsList[0];
127
128                 const licenseKeyGroupUpdatedData = LicenseKeyGroupPostFactory.build({
129                         name: 'lsk1_UPDATE',
130                         description: 'string_UPDATE',
131                         id: toBeUpdatedLicenseKeyGroupId
132                 });
133                 deepFreeze(licenseKeyGroupUpdatedData);
134
135                 const licenseKeyGroupPutRequest = LicenseKeyGroupPostFactory.build({
136                         name: 'lsk1_UPDATE',
137                         description: 'string_UPDATE'
138                 });
139
140                 deepFreeze(licenseKeyGroupPutRequest);
141
142                 const expectedStore = cloneAndSet(store.getState(), 'licenseModel.licenseKeyGroup.licenseKeyGroupsList', [licenseKeyGroupUpdatedData]);
143
144                 mockRest.addHandler('put', ({data, options, baseUrl}) => {
145                         expect(baseUrl).toEqual(`/onboarding-api/v1.0/vendor-license-models/${LICENSE_MODEL_ID}/versions/${version.id}/license-key-groups/${toBeUpdatedLicenseKeyGroupId}`);
146                         expect(data).toEqual(licenseKeyGroupPutRequest);
147                         expect(options).toEqual(undefined);
148                 });
149
150                 return LicenseKeyGroupsActionHelper.saveLicenseKeyGroup(store.dispatch, {
151                         previousLicenseKeyGroup: previousLicenseKeyGroupData,
152                         licenseKeyGroup: licenseKeyGroupUpdatedData,
153                         licenseModelId: LICENSE_MODEL_ID,
154                         version
155                 }).then(() => {
156                         expect(store.getState()).toEqual(expectedStore);
157                 });
158         });
159
160 });