-
Notifications
You must be signed in to change notification settings - Fork 39
/
angular-spring-data-rest-provider.fetch.spec.js
334 lines (261 loc) · 15.3 KB
/
angular-spring-data-rest-provider.fetch.spec.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
describe("the fetch function", function () {
beforeEach(beforeEachFunction);
it("must not call any href url of a link if no fetch link names are given", function () {
var fetchFunctionConfiguration = {
'fetchFunction': function () {
throw new Error("The method must not be called.")
}
};
// set the new fetch function which throws an error if it is called
springDataRestAdapterProvider.config(fetchFunctionConfiguration);
this.processedDataPromise = SpringDataRestAdapter.process(this.rawResponse);
});
it("must call the correct href url if an existing fetch link name is given", function () {
var embeddedNewKey = this.config.embeddedNewKey;
var fetchLinkName = 'parentCategory';
// the correct link href url
var firstParentCategoryHref = 'http://localhost:8080/categories/f974f5ef-a951-43b4-9027-4d2163216e54/parentCategory';
var secondParentCategoryHref = 'http://localhost:8080/categories/b5ba38d5-98d3-4579-8709-a28549406697/parentCategory';
// check if the underlying fetch function is called with the correct href url
var firstExpectedResult = {parentCategory: '1'};
var secondExpectedResult = {parentCategory: '2'};
this.httpBackend.whenGET(firstParentCategoryHref).
respond(200, firstExpectedResult);
this.httpBackend.expectGET(firstParentCategoryHref);
this.httpBackend.whenGET(secondParentCategoryHref).
respond(200, secondExpectedResult);
this.httpBackend.expectGET(secondParentCategoryHref);
SpringDataRestAdapter.process(this.rawResponse, fetchLinkName).then(function (processedData) {
// expect the fetched objects
expect(processedData[embeddedNewKey][0][fetchLinkName]).toEqual(firstExpectedResult);
expect(processedData[embeddedNewKey][1][fetchLinkName]).toEqual(secondExpectedResult);
});
this.httpBackend.flush();
this.httpBackend.verifyNoOutstandingRequest();
this.httpBackend.verifyNoOutstandingExpectation();
});
it("must call all links if the fetch all link names key", function () {
var embeddedNewKey = this.config.embeddedNewKey;
// the correct link href url
var testLinkHref = 'http://localhost:8080/categories/testLink';
var firstParentCategoryHref = 'http://localhost:8080/categories/f974f5ef-a951-43b4-9027-4d2163216e54/parentCategory';
var secondParentCategoryHref = 'http://localhost:8080/categories/b5ba38d5-98d3-4579-8709-a28549406697/parentCategory';
var testParentCategoryHref = 'http://localhost:8080/categories/b5ba38d5-98d3-4579-8709-a28549406697/testCategory';
// check if the underlying fetch function is called with the correct href
var testLinkExpectedResult = {testLink: 'test'};
var firstParentCategoryExpectedResult = {parentCategory: '1'};
var secondParentCategoryExpectedResult = {parentCategory: '2'};
var testParentCategoryExpectedResult = {testCategory: 'test'};
this.httpBackend.whenGET(testLinkHref).
respond(200, testLinkExpectedResult);
this.httpBackend.expectGET(testLinkHref);
this.httpBackend.whenGET(firstParentCategoryHref).
respond(200, firstParentCategoryExpectedResult);
this.httpBackend.expectGET(firstParentCategoryHref);
this.httpBackend.whenGET(secondParentCategoryHref).
respond(200, secondParentCategoryExpectedResult);
this.httpBackend.expectGET(secondParentCategoryHref);
this.httpBackend.whenGET(testParentCategoryHref).
respond(200, testParentCategoryExpectedResult);
this.httpBackend.expectGET(testParentCategoryHref);
SpringDataRestAdapter.process(this.rawResponse, this.config.fetchAllKey).then(function (processedData) {
// expect the fetched objects
expect(processedData['testLink']).toEqual(testLinkExpectedResult);
expect(processedData[embeddedNewKey][0]['parentCategory']).toEqual(firstParentCategoryExpectedResult);
expect(processedData[embeddedNewKey][1]['parentCategory']).toEqual(secondParentCategoryExpectedResult);
expect(processedData[embeddedNewKey][1]['testCategory']).toEqual(testParentCategoryExpectedResult);
}, function (error) {
fail(error)
});
this.httpBackend.flush();
this.httpBackend.verifyNoOutstandingRequest();
this.httpBackend.verifyNoOutstandingExpectation();
});
it("must call all links of the given fetch link names array", function () {
var embeddedNewKey = this.config.embeddedNewKey;
var fetchLinkNames = ['testLink', 'testCategory'];
// the correct link href url
var testLinkHref = 'http://localhost:8080/categories/testLink';
var testParentCategoryHref = 'http://localhost:8080/categories/b5ba38d5-98d3-4579-8709-a28549406697/testCategory';
// check if the underlying fetch function is called with the correct href
var testLinkExpectedResult = {testLink: 'test'};
var testParentCategoryExpectedResult = {testCategory: 'test'};
this.httpBackend.whenGET(testLinkHref).
respond(200, testLinkExpectedResult);
this.httpBackend.expectGET(testLinkHref);
this.httpBackend.whenGET(testParentCategoryHref).
respond(200, testParentCategoryExpectedResult);
this.httpBackend.expectGET(testParentCategoryHref);
SpringDataRestAdapter.process(this.rawResponse, fetchLinkNames).then(function (processedData) {
// expect the fetched objects
expect(processedData[fetchLinkNames[0]]).toEqual(testLinkExpectedResult);
expect(processedData[embeddedNewKey][1][fetchLinkNames[1]]).toEqual(testParentCategoryExpectedResult);
}, function (error) {
fail(error)
});
this.httpBackend.flush();
this.httpBackend.verifyNoOutstandingRequest();
this.httpBackend.verifyNoOutstandingExpectation();
});
it("must call all links with the same name of the given fetch link names array", function () {
var embeddedNewKey = this.config.embeddedNewKey;
var fetchLinkNames = ['testCategory'];
// the correct link href url
var testTestCategoryHref = 'http://localhost:8080/categories/b5ba38d5-98d3-4579-8709-a28549406697/testCategory';
var testTestCategory2Href = 'http://localhost:8080/categories/b5ba38d5-98d3-4579-8709-a28549406697/testCategory2';
// check if the underlying fetch function is called with the correct href
var testTestCategoryExpectedResult = {
"version": 0,
"creationDate": 1406219870650,
"modificationDate": 1406219870650,
"name": "Test category 1",
"_links": {
"self": {
"href": "http://localhost:8080/categories/f974f5ef-a951-43b4-9027-4d2163216e54"
},
"testCategory": {
"href": "http://localhost:8080/categories/b5ba38d5-98d3-4579-8709-a28549406697/testCategory2"
}
}
};
var testTestCategory2ExpectedResult = {testCategory: 'test'};
this.httpBackend.whenGET(testTestCategoryHref).
respond(200, testTestCategoryExpectedResult);
this.httpBackend.expectGET(testTestCategoryHref);
this.httpBackend.whenGET(testTestCategory2Href).
respond(200, testTestCategory2ExpectedResult);
this.httpBackend.expectGET(testTestCategory2Href);
SpringDataRestAdapter.process(mockDataWithMultipleEmbeddedItemsAndSameLinks(), fetchLinkNames, true, true).then(function (processedData) {
// expect the fetched objects
expect(processedData[embeddedNewKey][0][fetchLinkNames[0]].name).toEqual("Test category 1");
expect(processedData[embeddedNewKey][0][fetchLinkNames[0]][fetchLinkNames[0]].testCategory).toEqual("test");
}, function (error) {
fail(error)
});
this.httpBackend.flush();
this.httpBackend.verifyNoOutstandingRequest();
this.httpBackend.verifyNoOutstandingExpectation();
});
it("must process the fetched response recursively if the flag is set", function () {
var embeddedNewKey = this.config.embeddedNewKey;
var resourcesKey = this.config.resourcesKey;
var fetchLinkName = 'parentCategory';
// the correct link href url
var firstParentCategoryHref = 'http://localhost:8080/categories/f974f5ef-a951-43b4-9027-4d2163216e54/parentCategory';
var secondParentCategoryHref = 'http://localhost:8080/categories/b5ba38d5-98d3-4579-8709-a28549406697/parentCategory';
// check if the underlying fetch function is called with the correct href url
var firstExpectedResult = mockDataWithoutEmbeddedKey();
var secondExpectedResult = mockDataWithoutEmbeddedKey();
this.httpBackend.whenGET(firstParentCategoryHref).
respond(200, firstExpectedResult);
this.httpBackend.expectGET(firstParentCategoryHref);
this.httpBackend.whenGET(secondParentCategoryHref).
respond(200, secondExpectedResult);
this.httpBackend.expectGET(secondParentCategoryHref);
SpringDataRestAdapter.process(this.rawResponse, fetchLinkName, true).then(function (processedData) {
// expect the recursively fetched objects
expect(typeof processedData[embeddedNewKey][0][fetchLinkName][resourcesKey] == 'function').
toEqual(true);
expect(typeof processedData[embeddedNewKey][1][fetchLinkName][resourcesKey] == 'function').
toEqual(true);
}, function (error) {
fail(error)
});
this.httpBackend.flush();
this.httpBackend.verifyNoOutstandingRequest();
this.httpBackend.verifyNoOutstandingExpectation();
});
it("it must call the overridden fetch function with the given resource name", function () {
// define the fetch link name
var fetchLinkName = 'parentCategory';
// define the new fetch function
var i = 0, localConfig = this.config;
var fetchFunctionConfiguration = {
'fetchFunction': function (inUrl, inKey, inData, inFetchLinkNames, inRecursive) {
if (i == 0) {
expect(inUrl).toEqual("http://localhost:8080/categories/f974f5ef-a951-43b4-9027-4d2163216e54/parentCategory");
expect(inKey).toEqual(fetchLinkName);
expect(inData[localConfig.linksKey][localConfig.linksSelfLinkName][localConfig.linksHrefKey]).
toEqual("http://localhost:8080/categories/f974f5ef-a951-43b4-9027-4d2163216e54");
expect(inFetchLinkNames).toEqual(fetchLinkName);
expect(inRecursive).toEqual(undefined);
i++;
} else if (i == 1) {
expect(inUrl).toEqual("http://localhost:8080/categories/b5ba38d5-98d3-4579-8709-a28549406697/parentCategory");
expect(inKey).toEqual(fetchLinkName);
expect(inData[localConfig.linksKey][localConfig.linksSelfLinkName][localConfig.linksHrefKey]).
toEqual("http://localhost:8080/categories/b5ba38d5-98d3-4579-8709-a28549406697");
expect(inFetchLinkNames).toEqual(fetchLinkName);
expect(inRecursive).toEqual(undefined);
i++;
} else if (i > 1) {
throw new Error("The method must not be called three times");
}
}
};
// set the new fetch function and create a new spring data rest adapter
springDataRestAdapterProvider.config(fetchFunctionConfiguration);
this.response = SpringDataRestAdapter.process(this.rawResponse, fetchLinkName);
this.rootScope.$apply();
});
it("must reject the promise if the status code is not between 200 - 299 and not a 404", function () {
var fetchLinkName = 'testCategory';
// the correct link href url
var testCategoryHref = 'http://localhost:8080/categories/b5ba38d5-98d3-4579-8709-a28549406697/testCategory';
this.httpBackend.whenGET(testCategoryHref).
respond(300, "error");
this.httpBackend.expectGET(testCategoryHref);
SpringDataRestAdapter.process(this.rawResponse, fetchLinkName).then(function () {
throw new Error("Should not be called when the promise is rejected")
}, function (error) {
expect(error.status).toBe(300);
});
this.httpBackend.flush();
this.httpBackend.verifyNoOutstandingRequest();
this.httpBackend.verifyNoOutstandingExpectation();
});
it("must not reject the promise if the status code is a 404", function () {
var embeddedNewKey = this.config.embeddedNewKey;
var fetchLinkName = 'testCategory';
// the correct link href url
var testCategoryHref = 'http://localhost:8080/categories/b5ba38d5-98d3-4579-8709-a28549406697/testCategory';
var firstExpectedResult = {parentCategory: '1'};
this.httpBackend.whenGET(testCategoryHref).
respond(200, firstExpectedResult);
this.httpBackend.expectGET(testCategoryHref);
SpringDataRestAdapter.process(this.rawResponse, fetchLinkName).then(function (processedData) {
expect(processedData[embeddedNewKey][1][fetchLinkName]).toEqual(firstExpectedResult);
}, function () {
fail("Should not be called when the promise is rejected")
});
this.httpBackend.flush();
this.httpBackend.verifyNoOutstandingRequest();
this.httpBackend.verifyNoOutstandingExpectation();
});
it("must process the fetched responses as all the other responses", function () {
var embeddedNewKey = this.config.embeddedNewKey;
var fetchLinkName = 'parentCategory';
// the correct link href url
var firstParentCategoryHref = 'http://localhost:8080/categories/f974f5ef-a951-43b4-9027-4d2163216e54/parentCategory';
var secondParentCategoryHref = 'http://localhost:8080/categories/b5ba38d5-98d3-4579-8709-a28549406697/parentCategory';
// check if the underlying fetch function is called with the correct href url
var firstExpectedResult = mockData();
var secondExpectedResult = mockData();
this.httpBackend.whenGET(firstParentCategoryHref).
respond(200, firstExpectedResult);
this.httpBackend.expectGET(firstParentCategoryHref);
this.httpBackend.whenGET(secondParentCategoryHref).
respond(200, secondExpectedResult);
this.httpBackend.expectGET(secondParentCategoryHref);
SpringDataRestAdapter.process(this.rawResponse, fetchLinkName).then(function (processedData) {
// expect the fetched objects
expect(processedData[embeddedNewKey][0][fetchLinkName][embeddedNewKey][0]['name']).toEqual('Test category 1');
expect(processedData[embeddedNewKey][1][fetchLinkName][embeddedNewKey][1]['name']).toEqual('Test category 2');
}, function (error) {
fail(error)
});
this.httpBackend.flush();
this.httpBackend.verifyNoOutstandingRequest();
this.httpBackend.verifyNoOutstandingExpectation();
});
});