[SDC-29] Amdocs OnBoard 1707 initial commit.
[sdc.git] / openecomp-ui / test / flows / 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
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 } from 'test-utils/factories/flows/FlowsFactories.js';
31
32 import {buildFromExistingObject} from 'test-utils/Util.js';
33
34 const NEW_FLOW = true;
35
36 let assertFlowDataAfterCreateFetchAndUpdate = (data) => {
37         let {flowList, serviceID, diagramType} = store.getState().flows;
38         expect(serviceID).toBe(data.serviceID);
39         expect(diagramType).toBe(data.artifactType);
40         let uniqueId = data.uniqueId || `${data.serviceID}.${data.artifactName}`;
41         let index = flowList.findIndex(flow => flow.uniqueId === uniqueId);
42         expect(index).not.toBe(-1);
43 };
44
45 describe('Workflows and Management Flows Module Tests:', function () {
46
47
48         it('empty artifact should open flow creation modal', () => {
49
50                 const artifacts = {};
51
52                 deepFreeze(store.getState());
53                 deepFreeze(artifacts);
54                 FlowsActions.fetchFlowArtifacts(store.dispatch, {
55                         artifacts,
56                         diagramType: enums.WORKFLOW,
57                         participants: [],
58                         serviceID: '1234'
59                 });
60                 let state = store.getState();
61                 expect(state.flows.isDisplayModal).toBe(true);
62                 expect(state.flows.isModalInEditMode).toBe(false);
63         });
64
65         it('Close flow details editor modal', () => {
66                 deepFreeze(store.getState());
67                 FlowsActions.closeFlowDetailsEditor(store.dispatch);
68                 let state = store.getState();
69                 expect(state.flows.isDisplayModal).toBe(false);
70                 expect(state.flows.isModalInEditMode).toBe(false);
71         });
72
73         it('Get Flows List from loaded artifact', () => {
74
75                 deepFreeze(store.getState());
76
77                 const artifacts = {
78                         'test1': FlowPostResponseFactory.build({artifactName: 'test1'}),
79                         'kukuriku': FlowPostResponseFactory.build({
80                                 'artifactType': 'PUPPET',
81                                 'artifactName': 'kukuriku',
82                         }),
83                         'test3': FlowPostResponseFactory.build({artifactName: 'test3'})
84                 };
85
86                 const artifactsArray = Object.keys(artifacts).map(artifact => artifact);
87
88                 deepFreeze(artifacts);
89
90                 deepFreeze(store.getState());
91
92                 let actionData = {
93                         artifacts,
94                         diagramType: enums.WORKFLOW,
95                         participants: [],
96                         serviceID: '1234'
97                 };
98                 FlowsActions.fetchFlowArtifacts(store.dispatch, actionData);
99
100                 let state = store.getState();
101                 expect(state.flows.isDisplayModal).toBe(false);
102                 expect(state.flows.isModalInEditMode).toBe(false);
103                 expect(state.flows.flowList.length).toEqual(artifactsArray.length);
104                 expect(state.flows.flowParticipants).toEqual(actionData.participants);
105                 expect(state.flows.serviceID).toBe(actionData.serviceID);
106                 expect(state.flows.diagramType).toBe(actionData.diagramType);
107
108         });
109
110
111         it('Add New Flow', () => {
112
113                 deepFreeze(store.getState());
114
115                 const flowCreateData = FlowCreateFactory.build();
116                 let expectedDataToBeSentInTheRequest = buildFromExistingObject(FlowPostRequestFactory, flowCreateData);
117
118                 mockRest.addHandler('post', ({data, baseUrl, options}) => {
119                         expect(baseUrl).toBe(`/sdc1/feProxy/rest/v1/catalog/services/${flowCreateData.serviceID}/artifacts/`);
120                         expect(data.artifactLabel).toBe(expectedDataToBeSentInTheRequest.artifactLabel);
121                         expect(data.artifactName).toBe(expectedDataToBeSentInTheRequest.artifactName);
122                         expect(data.artifactType).toBe(expectedDataToBeSentInTheRequest.artifactType);
123                         expect(data.description).toBe(expectedDataToBeSentInTheRequest.description);
124                         expect(data.payloadData).toBe(expectedDataToBeSentInTheRequest.payloadData);
125                         expect(options.md5).toBe(true);
126                         return buildFromExistingObject(FlowPostResponseFactory, expectedDataToBeSentInTheRequest);
127                 });
128
129                 return FlowsActions.createOrUpdateFlow(store.dispatch, {flow: flowCreateData}, NEW_FLOW).then(() => {
130                         assertFlowDataAfterCreateFetchAndUpdate(flowCreateData);
131                 });
132
133                 
134         });
135
136         it('Fetch Flow', () => {
137
138                 deepFreeze(store.getState());
139
140                 const flowFetchData = FlowFetchRequestFactory.build();
141
142                 mockRest.addHandler('fetch', ({baseUrl}) => {
143                         //sdc1/feProxy/rest/v1/catalog/services/338d75f0-aec8-4eb4-89c9-8733fcd9bf3b/artifacts/338d75f0-aec8-4eb4-89c9-8733fcd9bf3b.zizizi
144                         expect(baseUrl).toBe(`/sdc1/feProxy/rest/v1/catalog/services/${flowFetchData.serviceID}/artifacts/${flowFetchData.uniqueId}`);
145                         return buildFromExistingObject(FlowFetchResponseFactory, flowFetchData);
146                 });
147
148                 return FlowsActions.fetchArtifact(store.dispatch, {flow: flowFetchData}).then(() => {
149                         assertFlowDataAfterCreateFetchAndUpdate(flowFetchData);
150                 });
151         });
152
153         it('Update Existing Flow', () => {
154
155                 deepFreeze(store.getState());
156                 const flowUpdateData = FlowUpdateRequestFactory.build();
157
158                 mockRest.addHandler('post', ({baseUrl}) => {
159                         expect(baseUrl).toBe(`/sdc1/feProxy/rest/v1/catalog/services/${flowUpdateData.serviceID}/artifacts/${flowUpdateData.uniqueId}`);
160
161                         return buildFromExistingObject(FlowPostResponseFactory, flowUpdateData);
162                 });
163
164                 return FlowsActions.createOrUpdateFlow(store.dispatch, {flow: flowUpdateData}, !NEW_FLOW).then(() => {
165                         assertFlowDataAfterCreateFetchAndUpdate(flowUpdateData);
166                 });
167
168         });
169
170         it('Delete Flow', () => {
171
172                 deepFreeze(store.getState());
173
174                 const flowDeleteData = FlowDeleteRequestFactory.build();
175
176                 mockRest.addHandler('destroy', ({baseUrl}) => {
177                         expect(baseUrl).toBe(`/sdc1/feProxy/rest/v1/catalog/services/${flowDeleteData.serviceID}/artifacts/${flowDeleteData.uniqueId}`);
178                         return {};
179                 });
180
181                 return FlowsActions.deleteFlow(store.dispatch, {flow: flowDeleteData}).then(() => {
182                         let {flowList} = store.getState().flows;
183                         let index = flowList.findIndex(flow => flow.uniqueId === flowDeleteData.uniqueId);
184                         expect(index).toBe(-1);
185                 });
186         });
187 });