b6840e25a0eae82a9d5d638641bc3ab261a37e93
[aai/sparky-fe.git] / test / utils / MockRest.js
1 /*
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * Copyright © 2017-2018 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 const queue = {
22         fetch: [],
23         put: [],
24         post: [],
25         destroy: []
26 };
27
28 const initQueue = () => {
29         queue['fetch'] = [];
30         queue['put'] = [];
31         queue['post'] = [];
32         queue['destroy'] = [];
33 };
34
35 const handleOperation = (handler, options) => {
36         if(typeof handler === 'function') {
37                 return Promise.resolve(handler(options));
38         }
39         else {
40                 return Promise.resolve(handler);
41         }
42 };
43
44 export default {
45
46         fetch(baseUrl, options) {
47                 const {fetch} = queue;
48                 if(!fetch.length) {
49                         throw new Error(`Fetch operation was called without proper handler. baseUrl: '${baseUrl}' options: '${options}'`);
50                 }
51                 return handleOperation(fetch.shift(), {options, baseUrl});
52         },
53
54         put(baseUrl, data, options) {
55                 const {put} = queue;
56                 if(!put.length) {
57                         throw new Error(`put operation was called without proper handler. baseUrl: '${baseUrl}' options: '${options}'`);
58                 }
59                 return handleOperation(put.shift(), {data, options, baseUrl});
60         },
61
62         post(baseUrl, data, options) {
63                 const {post} = queue;
64                 if(!post.length) {
65                         throw new Error(`post operation was called without proper handler. baseUrl: '${baseUrl}' options: '${options}'`);
66                 }
67                 return handleOperation(post.shift(), {data, options, baseUrl});
68         },
69
70         destroy(baseUrl, options) {
71                 const {destroy} = queue;
72                 if(!destroy.length) {
73                         throw new Error(`Destroy operation was called without proper handler. baseUrl: '${baseUrl}' options: '${options}'`);
74                 }
75                 return handleOperation(destroy.shift(), {options, baseUrl});
76         },
77
78         addHandler(operation, handler) {
79                 queue[operation].push(handler);
80         },
81
82         resetQueue() {
83                 initQueue();
84         }
85 };