6d996334911d9a7decc019358d1ea6c3c6c4a07a
[aai/sparky-fe.git] / test / utils / MockRest.js
1 /*
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6  * Copyright © 2017 Amdocs
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *       http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  *
21  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  */
23 const queue = {
24         fetch: [],
25         put: [],
26         post: [],
27         destroy: []
28 };
29
30 const initQueue = () => {
31         queue['fetch'] = [];
32         queue['put'] = [];
33         queue['post'] = [];
34         queue['destroy'] = [];
35 };
36
37 const handleOperation = (handler, options) => {
38         if(typeof handler === 'function') {
39                 return Promise.resolve(handler(options));
40         }
41         else {
42                 return Promise.resolve(handler);
43         }
44 };
45
46 export default {
47
48         fetch(baseUrl, options) {
49                 const {fetch} = queue;
50                 if(!fetch.length) {
51                         throw new Error(`Fetch operation was called without proper handler. baseUrl: '${baseUrl}' options: '${options}'`);
52                 }
53                 return handleOperation(fetch.shift(), {options, baseUrl});
54         },
55
56         put(baseUrl, data, options) {
57                 const {put} = queue;
58                 if(!put.length) {
59                         throw new Error(`put operation was called without proper handler. baseUrl: '${baseUrl}' options: '${options}'`);
60                 }
61                 return handleOperation(put.shift(), {data, options, baseUrl});
62         },
63
64         post(baseUrl, data, options) {
65                 const {post} = queue;
66                 if(!post.length) {
67                         throw new Error(`post operation was called without proper handler. baseUrl: '${baseUrl}' options: '${options}'`);
68                 }
69                 return handleOperation(post.shift(), {data, options, baseUrl});
70         },
71
72         destroy(baseUrl, options) {
73                 const {destroy} = queue;
74                 if(!destroy.length) {
75                         throw new Error(`Destroy operation was called without proper handler. baseUrl: '${baseUrl}' options: '${options}'`);
76                 }
77                 return handleOperation(destroy.shift(), {options, baseUrl});
78         },
79
80         addHandler(operation, handler) {
81                 queue[operation].push(handler);
82         },
83
84         resetQueue() {
85                 initQueue();
86         }
87 };