react 16 upgrade
[sdc.git] / openecomp-ui / test / flows / test.js
1 /*
2  * Copyright © 2016-2018 European Support Limited
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 or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 import deepFreeze from 'deep-freeze';
18 import mockRest from 'test-utils/MockRest.js';
19 import store from 'sdc-app/AppStore.js';
20 import FlowsActions from 'sdc-app/flows/FlowsActions.js';
21 import { enums } from 'sdc-app/flows/FlowsConstants.js';
22
23 import {
24     FlowCreateFactory,
25     FlowPostRequestFactory,
26     FlowPostResponseFactory,
27     FlowFetchRequestFactory,
28     FlowFetchResponseFactory,
29     FlowDeleteRequestFactory,
30     FlowUpdateRequestFactory
31 } from 'test-utils/factories/flows/FlowsFactories.js';
32
33 import { buildFromExistingObject } from 'test-utils/Util.js';
34
35 const NEW_FLOW = true;
36
37 let assertFlowDataAfterCreateFetchAndUpdate = data => {
38     let { flowList, serviceID, diagramType } = store.getState().flows;
39     expect(serviceID).toBe(data.serviceID);
40     expect(diagramType).toBe(data.artifactType);
41     let uniqueId = data.uniqueId || `${data.serviceID}.${data.artifactName}`;
42     let index = flowList.findIndex(flow => flow.uniqueId === uniqueId);
43     expect(index).not.toBe(-1);
44 };
45
46 describe('Workflows and Management Flows Module Tests:', function() {
47     it('empty artifact should open flow creation modal', () => {
48         const artifacts = {};
49
50         deepFreeze(store.getState());
51         deepFreeze(artifacts);
52         FlowsActions.fetchFlowArtifacts(store.dispatch, {
53             artifacts,
54             diagramType: enums.WORKFLOW,
55             participants: [],
56             serviceID: '1234'
57         });
58         let state = store.getState();
59         expect(state.modal).toBeDefined();
60     });
61
62     it('Close flow details editor modal', () => {
63         deepFreeze(store.getState());
64         FlowsActions.closeEditCreateWFModal(store.dispatch);
65         let state = store.getState();
66         expect(state.modal).toBeFalsy();
67     });
68
69     it('Get Flows List from loaded artifact', () => {
70         deepFreeze(store.getState());
71
72         const artifacts = {
73             test1: FlowPostResponseFactory.build({ artifactName: 'test1' }),
74             kukuriku: FlowPostResponseFactory.build({
75                 artifactType: 'PUPPET',
76                 artifactName: 'kukuriku'
77             }),
78             test3: FlowPostResponseFactory.build({ artifactName: 'test3' })
79         };
80
81         const artifactsArray = Object.keys(artifacts).map(artifact => artifact);
82
83         deepFreeze(artifacts);
84
85         deepFreeze(store.getState());
86
87         let actionData = {
88             artifacts,
89             diagramType: enums.WORKFLOW,
90             participants: [],
91             serviceID: '1234'
92         };
93         FlowsActions.fetchFlowArtifacts(store.dispatch, actionData);
94
95         let state = store.getState();
96         expect(state.flows.flowList.length).toEqual(artifactsArray.length);
97         expect(state.flows.flowParticipants).toEqual(actionData.participants);
98         expect(state.flows.serviceID).toBe(actionData.serviceID);
99         expect(state.flows.diagramType).toBe(actionData.diagramType);
100     });
101
102     it('Add New Flow', () => {
103         deepFreeze(store.getState());
104
105         const flowCreateData = FlowCreateFactory.build();
106         let expectedDataToBeSentInTheRequest = buildFromExistingObject(
107             FlowPostRequestFactory,
108             flowCreateData
109         );
110
111         mockRest.addHandler('post', ({ data, baseUrl, options }) => {
112             expect(baseUrl).toBe(
113                 `/sdc1/feProxy/rest/v1/catalog/services/${
114                     flowCreateData.serviceID
115                 }/artifacts/`
116             );
117             expect(data.artifactLabel).toBe(
118                 expectedDataToBeSentInTheRequest.artifactLabel
119             );
120             expect(data.artifactName).toBe(
121                 expectedDataToBeSentInTheRequest.artifactName
122             );
123             expect(data.artifactType).toBe(
124                 expectedDataToBeSentInTheRequest.artifactType
125             );
126             expect(data.description).toBe(
127                 expectedDataToBeSentInTheRequest.description
128             );
129             expect(data.payloadData).toBe(
130                 expectedDataToBeSentInTheRequest.payloadData
131             );
132             expect(options.md5).toBe(true);
133             return buildFromExistingObject(
134                 FlowPostResponseFactory,
135                 expectedDataToBeSentInTheRequest
136             );
137         });
138
139         return FlowsActions.createOrUpdateFlow(
140             store.dispatch,
141             { flow: flowCreateData },
142             NEW_FLOW
143         ).then(() => {
144             assertFlowDataAfterCreateFetchAndUpdate(flowCreateData);
145         });
146     });
147
148     it('Fetch Flow', () => {
149         deepFreeze(store.getState());
150
151         const flowFetchData = FlowFetchRequestFactory.build();
152
153         mockRest.addHandler('fetch', ({ baseUrl }) => {
154             //sdc1/feProxy/rest/v1/catalog/services/338d75f0-aec8-4eb4-89c9-8733fcd9bf3b/artifacts/338d75f0-aec8-4eb4-89c9-8733fcd9bf3b.zizizi
155             expect(baseUrl).toBe(
156                 `/sdc1/feProxy/rest/v1/catalog/services/${
157                     flowFetchData.serviceID
158                 }/artifacts/${flowFetchData.uniqueId}`
159             );
160             return buildFromExistingObject(
161                 FlowFetchResponseFactory,
162                 flowFetchData
163             );
164         });
165
166         return FlowsActions.fetchArtifact(store.dispatch, {
167             flow: flowFetchData
168         }).then(() => {
169             assertFlowDataAfterCreateFetchAndUpdate(flowFetchData);
170         });
171     });
172
173     it('Update Existing Flow', () => {
174         deepFreeze(store.getState());
175         const flowUpdateData = FlowUpdateRequestFactory.build();
176
177         mockRest.addHandler('post', ({ baseUrl }) => {
178             expect(baseUrl).toBe(
179                 `/sdc1/feProxy/rest/v1/catalog/services/${
180                     flowUpdateData.serviceID
181                 }/artifacts/${flowUpdateData.uniqueId}`
182             );
183
184             return buildFromExistingObject(
185                 FlowPostResponseFactory,
186                 flowUpdateData
187             );
188         });
189
190         return FlowsActions.createOrUpdateFlow(
191             store.dispatch,
192             { flow: flowUpdateData },
193             !NEW_FLOW
194         ).then(() => {
195             assertFlowDataAfterCreateFetchAndUpdate(flowUpdateData);
196         });
197     });
198
199     it('Delete Flow', () => {
200         deepFreeze(store.getState());
201
202         const flowDeleteData = FlowDeleteRequestFactory.build();
203
204         mockRest.addHandler('destroy', ({ baseUrl }) => {
205             expect(baseUrl).toBe(
206                 `/sdc1/feProxy/rest/v1/catalog/services/${
207                     flowDeleteData.serviceID
208                 }/artifacts/${flowDeleteData.uniqueId}`
209             );
210             return {};
211         });
212
213         return FlowsActions.deleteFlow(store.dispatch, {
214             flow: flowDeleteData
215         }).then(() => {
216             let { flowList } = store.getState().flows;
217             let index = flowList.findIndex(
218                 flow => flow.uniqueId === flowDeleteData.uniqueId
219             );
220             expect(index).toBe(-1);
221         });
222     });
223 });