Fix 'Changing VFC version on template wipes previously assigned property values based...
[sdc.git] / openecomp-ui / test-utils / MockRest.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 const queue = {
17         fetch: [],
18         put: [],
19         post: [],
20         destroy: []
21 };
22
23 const initQueue = () => {
24         queue['fetch'] = [];
25         queue['put'] = [];
26         queue['post'] = [];
27         queue['destroy'] = [];
28 };
29
30 const handleOperation = (handler, options) => {
31         if(typeof handler === 'function') {
32                 return Promise.resolve(handler(options));
33         }
34         else {
35                 return Promise.resolve(handler);
36         }
37 };
38
39 export default {
40
41         fetch(baseUrl, options) {
42                 const {fetch} = queue;
43                 if(!fetch.length) {
44                         throw new Error(`Fetch operation was called without proper handler. baseUrl: '${baseUrl}' options: '${options}'`);
45                 }
46                 return handleOperation(fetch.shift(), {options, baseUrl});
47         },
48
49         put(baseUrl, data, options) {
50                 const {put} = queue;
51                 if(!put.length) {
52                         throw new Error(`put operation was called without proper handler. baseUrl: '${baseUrl}' options: '${options}'`);
53                 }
54                 return handleOperation(put.shift(), {data, options, baseUrl});
55         },
56
57         post(baseUrl, data, options) {
58                 const {post} = queue;
59                 if(!post.length) {
60                         throw new Error(`post operation was called without proper handler. baseUrl: '${baseUrl}' options: '${options}'`);
61                 }
62                 return handleOperation(post.shift(), {data, options, baseUrl});
63         },
64
65         destroy(baseUrl, options) {
66                 const {destroy} = queue;
67                 if(!destroy.length) {
68                         throw new Error(`Destroy operation was called without proper handler. baseUrl: '${baseUrl}' options: '${options}'`);
69                 }
70                 return handleOperation(destroy.shift(), {options, baseUrl});
71         },
72
73         addHandler(operation, handler) {
74                 queue[operation].push(handler);
75         },
76
77         resetQueue() {
78                 initQueue();
79         },
80
81         checkEmptyQueue() {
82                 let isEmpty = true;
83                 let message = 'Check following calls: ';
84                 for (let operationType in queue) {
85                         if (queue[operationType].length > 0) {
86                                 isEmpty = false;
87                                 message += operationType;
88                         }
89                 }
90                 if (!isEmpty) {
91                         throw new Error('Queue is not empty, ' + message);
92                 }
93         }
94 };