aboutsummaryrefslogtreecommitdiffstats
path: root/src/app/services/tileservice/tiles.service.spec.ts
blob: f7f4369783dab3e682a8257daafca0fbe140e7c5 (plain)
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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
/*
 * Copyright (c) 2022. Deutsche Telekom AG
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * SPDX-License-Identifier: Apache-2.0
 */


import { TestBed, waitForAsync } from '@angular/core/testing';
import { Tile } from '../../model/tile';
import { TilesService } from './tiles.service';
import { HttpErrorResponse } from '@angular/common/http';
import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing';
import { environment } from '../../../environments/environment';
import { OAuthLogger, OAuthService, UrlHelperService } from 'angular-oauth2-oidc';

// https://dev.to/coly010/unit-testing-angular-services-1anm

/**
 * describe sets up the Test Suite for the TileService
 */
describe('TilesService', () => {
  /**
   * let service declares a Test Suite-scoped variable where we will store a reference to our service
   */
  let service: TilesService;
  let mockTile: Tile;
  let httpmock: HttpTestingController;
  let errmsg: string;

  const backendServerUrlTest = environment.backendServerUrl + '/tiles';
  /**
   * beforeEach tells the test runner to run this code before every test in the Test Suite
   * It is using Angular's TestBed to create the testing environment. Finally it is injecting the TilesService
   * and placing a reference to it in the service variable defined earlier.
   */
  beforeEach(() => {
    TestBed.configureTestingModule({
      imports: [HttpClientTestingModule],
      providers: [TilesService, OAuthService, UrlHelperService, OAuthLogger],
    });
    service = TestBed.inject(TilesService);
    httpmock = TestBed.inject(HttpTestingController);
    mockTile = {
      id: 1,
      title: 'NewTile1',
      description: 'New Tile for frontend test',
      imageUrl: 'https://www.onap.org/wp-content/uploads/sites/20/2017/02/logo_onap_2017.png',
      imageAltText: 'Onap Image',
      redirectUrl: 'www.onap.org',
      headers: 'This is a header',
      groups: [],
      roles: [],
    };
    // responseTile = {
    //   id: 2,
    //   title: 'NewTile1',
    //   description: 'New Tile for frontend test',
    //   imageUrl: 'https://www.onap.org/wp-content/uploads/sites/20/2017/02/logo_onap_2017.png',
    //   imageAltText: 'Onap Image',
    //   redirectUrl: 'www.onap.org',
    //   headers: 'This is a header',
    //   groups: [],
    //   roles: [],
    // };
  });

  /**
   * After every test, assert that there are no more pending requests.
   */
  afterEach(() => {
    httpmock.verify();
  });

  /**
   * the it() function creates a new test with the title 'should be created'
   * This test is expecting the service varibale to truthy, in otherwords,
   * it should have been instantiated correctly by the Angular TestBed.
   */
  it('should be created', () => {
    expect(service).toBeTruthy();
  });

  /**
   * TileService method tests begin
   * Testing getTiles
   */
  describe('#getTiles', () => {
    // let expectedTiles: Tile[];

    beforeEach(() => {
      // expectedTiles = [mockTile, responseTile];
    });

    /**
     * testing method getTiles() to get all existing tiles
     */
    /*
    it('should return expected tiles (called once)', (done: DoneFn) => {
      service.getTiles().subscribe(response => {
        expect(response).toEqual(expectedTiles, 'should return expected tiles');
        // done() to be called after asynchronous calls (https://angular.io/guide/testing-services)
        done();
      });

      // TileService should have made one request to GET tiles from expected URL
      const req = httpmock.expectOne(backendServerUrlTest);
      expect(req.request.method).toEqual('GET');

      // Respond with the expected mock tiles
      req.flush(expectedTiles);
    });
*/
    /**
     * TODO: Maybe it makes sense to inform the user that no tiles are displayed
     * testing method getTiles() in case there are no tiles in the database
     */
    /*
    it('should be OK returning no tiles', (done: DoneFn) => {
      service.getTiles().subscribe(response => {
        expect(response.length).toEqual(0, 'should have empty tiles array');
        done();
      });

      const req = httpmock.expectOne(backendServerUrlTest);
      expect(req.request.method).toEqual('GET');

      req.flush([]); // Respond with no tile
    });
*/
    /**
     * testing method getTiles() in case the backend responds with 404 Not Found
     * This service reports the error but finds a way to let the app keep going.
     */

    /*
    it('should handle 404 error', (done: DoneFn) => {
      errmsg = '404 error';

      service.getTiles().subscribe(
        response => fail('should fail with the 404 error'),
        (err: HttpErrorResponse) => {
          expect(err.status).toEqual(404);
          expect(err.error).toEqual(errmsg);
        },
      );

      // Make an HTTP Get Request
      // service.getTiles().then(
      //     response => fail('should have failed with the 404 error'),
      //       (err: HttpErrorResponse) => {
      //         expect(err.status).toEqual(404);
      //         expect(err.error).toEqual(errmsg);
      //       }
      // );

      const req = httpmock.expectOne(backendServerUrlTest);
      expect(req.request.method).toEqual('GET');

      // respond with a 404 and the error message in the body --> TODO Frontend GUI must react correctly
      req.flush(errmsg, { status: 404, statusText: 'Not Found' });
    });
     */

    /**
     * testing getTiles() when method is called multiple times
     * TODO: expect cached results
     */
    /*
    it('should return expected tiles (called multiple times)', () => {
      service.getTiles().subscribe();
      service.getTiles().subscribe();
      service.getTiles().subscribe(response => {
        expect(response).toEqual(expectedTiles, 'should return expected tiles');
      });

      const req = httpmock.match(backendServerUrlTest);
      expect(req.length).toEqual(3, 'calls to getTiles()');

      // Respond to each request with different mock tile results
      req[0].flush([]);
      req[1].flush([mockTile]);
      req[2].flush(expectedTiles);
    });*/
  });

  /**
   * Tests for getTileByID()
   */
  describe('#getTileByID', () => {
    /**
     * testing method getTilesById() to return the specific tile with right id
     */
    it('should return expected tile by id', () => {
      service.getTileById(mockTile.id).then(response => {
        expect(response).toEqual(mockTile, 'should return expected tile');
      });

      const req = httpmock.expectOne(backendServerUrlTest + '/' + mockTile.id);
      expect(req.request.method).toEqual('GET');

      // Respond with the mock tiles
      req.flush(mockTile);
    });

    /**
     * testing method getTileByID() in case the backend responds with 404 Not Found and the tile does not exist
     */
    it(
      'getTileById(): should handle 404 error',
      waitForAsync(() => {
        errmsg = '404 error';
        // Make an HTTP Get Request
        service.getTileById(mockTile.id).then(
          () => fail('should have failed with the 404 error'),
          (err: HttpErrorResponse) => {
            expect(err.status).toEqual(404);
            expect(err.error).toEqual(errmsg);
          },
        );

        const req = httpmock.expectOne(backendServerUrlTest + '/' + mockTile.id);
        expect(req.request.method).toEqual('GET');

        req.flush(errmsg, { status: 404, statusText: 'Not Found' });
      }),
    );
  });
  /**
   * Tests for update an existing tile
   */
  describe('#updateTiles', () => {
    /**
     * testing method updateTiles()
     */
    it('should update a tile and return it', () => {
      mockTile.title = 'Update title';

      service.updateTiles(mockTile).then(response => {
        expect(response.title).toEqual('Update title', 'should return tile');
      });
      // TileService should have made one request to PUT
      const req = httpmock.expectOne(backendServerUrlTest + '/' + mockTile.id);
      expect(req.request.method).toEqual('PUT');
      expect(req.request.body).toEqual(mockTile);

      req.flush(mockTile);
    });

    /**
     * testing method updateTiles() in case the backend responds with 404 Not Found and the tile does not exist
     */
    it(
      'updateTiles(): should handle 404 error',
      waitForAsync(() => {
        errmsg = '404 error';
        // Make an HTTP Get Request
        service.updateTiles(mockTile).then(
          () => fail('should have failed with the 404 error'),
          (err: HttpErrorResponse) => {
            expect(err.status).toEqual(404);
            expect(err.error).toEqual(errmsg);
          },
        );

        const req = httpmock.expectOne(backendServerUrlTest + '/' + mockTile.id);
        expect(req.request.method).toEqual('PUT');

        req.flush(errmsg, { status: 404, statusText: 'Not Found' });
      }),
    );

    /**
     * testing method updateTiles() in case the backend responds with 401 Unauthorized
     */
    it(
      'updateTiles(): should handle 401 error',
      waitForAsync(() => {
        errmsg = '401 error';
        // Make an HTTP Get Request
        service.updateTiles(mockTile).then(
          () => fail('should have failed with the 401 error'),
          (err: HttpErrorResponse) => {
            expect(err.status).toEqual(401);
            expect(err.error).toEqual(errmsg);
          },
        );

        const req = httpmock.expectOne(backendServerUrlTest + '/' + mockTile.id);
        expect(req.request.method).toEqual('PUT');

        req.flush(errmsg, { status: 401, statusText: 'Not Found' });
      }),
    );

    /**
     * testing method updateTiles() in case the backend responds with 403 Forbidden
     */
    it(
      'updateTiles(): should handle 403 error',
      waitForAsync(() => {
        errmsg = '403 error';
        // Make an HTTP Get Request
        service.updateTiles(mockTile).then(
          () => fail('should have failed with the 404 error'),
          (err: HttpErrorResponse) => {
            expect(err.status).toEqual(403);
          },
        );

        const req = httpmock.expectOne(backendServerUrlTest + '/' + mockTile.id);
        expect(req.request.method).toEqual('PUT');

        req.flush(errmsg, { status: 403, statusText: 'Not Found' });
      }),
    );
  });
  /*
   *  Test save a new tile
   */
  describe('#saveTiles', () => {
    /*
     * testing saveTiles() to save a new tile
     */
    it(
      'should save a tile correctly (mocked http post request)',
      waitForAsync(() => {
        service.saveTiles(mockTile).then(response => {
          expect(response.id).toBe(1);
          expect(response.title).toBe('NewTile1');
          expect(response.redirectUrl).toBe('www.onap.org');
          expect(response.imageAltText).toBe('Onap Image');
          expect(response.imageUrl).toBe('https://www.onap.org/wp-content/uploads/sites/20/2017/02/logo_onap_2017.png');
          expect(response.description).toBe('New Tile for frontend test');
          expect(response.headers).toBe('This is a header');
        });
        /*
         * Checking that there ist just one request and check the type of request
         * 'flush'/ respond with mock data, run then-block in line 64 and check the except commands
         */
        const req = httpmock.expectOne(backendServerUrlTest);
        expect(req.request.method).toEqual('POST');
        req.flush(mockTile);
      }),
    );
    /**
     * testing method saveTiles() in case the backend answers with an 401 responds
     */
    it(
      'saveTiles(): should handle 401 error',
      waitForAsync(() => {
        errmsg = '401 error';
        // Make an HTTP Get Request
        service.saveTiles(mockTile).then(
          () => fail('should have failed with the 401 error'),
          (err: HttpErrorResponse) => {
            expect(err.status).toEqual(401);
            expect(err.error).toEqual(errmsg);
          },
        );

        const req = httpmock.expectOne(backendServerUrlTest);
        expect(req.request.method).toEqual('POST');

        req.flush(errmsg, { status: 401, statusText: 'Not Found' });
      }),
    );

    it(
      'saveTiles(): should handle 403 error',
      waitForAsync(() => {
        errmsg = '403 error';
        // Make an HTTP Get Request
        service.saveTiles(mockTile).then(
          () => fail('should have failed with the 401 error'),
          (err: HttpErrorResponse) => {
            expect(err.status).toEqual(403);
            expect(err.error).toEqual(errmsg);
          },
        );

        const req = httpmock.expectOne(backendServerUrlTest);
        expect(req.request.method).toEqual('POST');

        req.flush(errmsg, { status: 403, statusText: 'Forbidden' });
      }),
    );
  });
  /**
   * testing delete a tile
   */
  describe('#deleteTiles', () => {
    /**
     * testing method deleteTile()
     */
    it(
      'should delete a tile correctly (mocked http delete request)',
      waitForAsync(() => {
        service.deleteTile(mockTile).then(response => {
          expect(response).toBeDefined();
        });
        const req = httpmock.expectOne(environment.backendServerUrl + '/tiles/' + mockTile.id);
        expect(req.request.method).toEqual('DELETE');
        req.flush({});
      }),
    );

    /**
     * testing method deleteTiles() in case the backend responds with 404 Not Found and the tile does not exist
     */
    it(
      'deleteTiles(): should handle 404 error',
      waitForAsync(() => {
        errmsg = '404 error';
        // Make an HTTP Get Request
        service.deleteTile(mockTile).then(
          () => fail('should have failed with the 404 error'),
          (err: HttpErrorResponse) => {
            expect(err.status).toEqual(404);
            expect(err.error).toEqual(errmsg);
          },
        );

        const req = httpmock.expectOne(backendServerUrlTest + '/' + mockTile.id);
        expect(req.request.method).toEqual('DELETE');

        req.flush(errmsg, { status: 404, statusText: 'Not Found' });
      }),
    );

    /**
     * testing method deleteTiles() in case the backend responds with 401 Unauthorized
     */
    it(
      'deleteTiles(): should handle 401 error',
      waitForAsync(() => {
        errmsg = '401 error';
        // Make an HTTP Get Request
        service.deleteTile(mockTile).then(
          () => fail('should have failed with the 401 error'),
          (err: HttpErrorResponse) => {
            expect(err.status).toEqual(401);
            expect(err.error).toEqual(errmsg);
          },
        );

        const req = httpmock.expectOne(backendServerUrlTest + '/' + mockTile.id);
        expect(req.request.method).toEqual('DELETE');

        req.flush(errmsg, { status: 401, statusText: 'Unauthorized' });
      }),
    );

    /**
     * testing method deleteTiles() in case the backend responds with 403 Forbidden
     */
    it(
      'deleteTiles(): should handle 403 error',
      waitForAsync(() => {
        errmsg = '403 error';
        // Make an HTTP Get Request
        service.deleteTile(mockTile).then(
          () => fail('should have failed with the 404 error'),
          (err: HttpErrorResponse) => {
            expect(err.status).toEqual(403);
            expect(err.error).toEqual(errmsg);
          },
        );

        const req = httpmock.expectOne(backendServerUrlTest + '/' + mockTile.id);
        expect(req.request.method).toEqual('DELETE');

        req.flush(errmsg, { status: 403, statusText: 'Forbidden' });
      }),
    );
  });
});