-
Notifications
You must be signed in to change notification settings - Fork 0
/
mockWsApi.js
114 lines (101 loc) · 3.88 KB
/
mockWsApi.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
angular.module("mock.wsApi", []).service("WsApi", function ($q) {
var service = mockService($q);
var mapping = apiMapping;
var fetchResponse;
service.mockFetchResponse = function (data) {
if (data === null || data === undefined) {
fetchResponse = undefined;
} else {
// using hasOwnProperty() to support special test cases (such as checks to see if payload is undefined).
fetchResponse = {
type: data.hasOwnProperty("type") ? data.type : null,
payload: data.hasOwnProperty("payload") ? data.payload : {},
messageStatus: data.hasOwnProperty("messageStatus") ? data.messageStatus : null,
httpStatus: data.hasOwnProperty("httpStatus") ? data.httpStatus : null,
valueType: data.hasOwnProperty("valueType") ? data.valueType : null
};
}
};
service.mockMapping = function (toMock) {
mapping = {};
for (var key in toMock) {
mapping[key] = toMock[key];
}
};
service.fetch = function (apiReq, options) {
var payload = {};
if (angular.isUndefined(apiReq)) {
return rejectPromise($q.defer());
}
if (fetchResponse) {
switch (fetchResponse.type) {
case "message":
return messagePromise($q.defer(), fetchResponse.payload, fetchResponse.messageStatus, fetchResponse.httpStatus);
case "value":
return valuePromise($q.defer(), fetchResponse.payload, fetchResponse.valueType);
case "payload":
return payloadPromise($q.defer(), fetchResponse.payload, fetchResponse.messageStatus, fetchResponse.httpStatus);
case "data":
return dataPromise($q.defer(), fetchResponse.payload, fetchResponse.messageStatus, fetchResponse.httpStatus);
case "reject":
return rejectPromise($q.defer(), fetchResponse.payload, fetchResponse.messageStatus, fetchResponse.httpStatus);
case "failure":
return failurePromise($q.defer(), fetchResponse.payload, fetchResponse.messageStatus, fetchResponse.httpStatus);
}
} else {
if (apiReq === mapping.ActiveSprints.all) {
payload = {
"ArrayList<Sprint>": mockActiveSprints
};
} else if (apiReq === mapping.ProductsStats.all) {
payload = {
"ArrayList<ProductStats>": dataProductsStats
};
} else if (apiReq === mapping.RemoteProducts.byProduct) {
payload = {
"HashMap": dataRemoteProducts
};
} else if (apiReq === mapping.RemoteProducts.all) {
payload = {
"HashMap": dataRemoteProducts
};
} else if (apiReq === mapping.Product.all) {
payload = {
"ArrayList<Product>": dataProductRepo1
};
}
if (apiReq.controller === mapping.FeatureRequest.push.controller) {
if (apiReq.method === 'push/:requestId/:productId/:rpmId') {
var requestId = Number(options.pathValues.requestId);
var productId = Number(options.pathValues.productId);
var rpmId = Number(options.pathValues.rpmId);
var scopeId = typeof options.data === "string" ? options.data : "";
if (requestId > 0 && productId > 0 && rpmId > 0 && scopeId !== "") {
for (var key in dataInternalRequestRepo1) {
if (dataInternalRequestRepo1[key].id == requestId) {
payload = {
"FeatureRequest": {
id: requestId,
projectId: productId,
rpmId: rpmId,
scopeId: options.data
}
};
return payloadPromise($q.defer(), payload);
}
}
}
}
return rejectPromise($q.defer());
}
}
return payloadPromise($q.defer(), payload);
};
service.getMapping = function () {
return mapping;
};
service.listen = function (apiReq) {
return payloadPromise($q.defer());
};
return service;
});