Browse Source

Created test suite for the AbstractService

This patch creates a test suite for the AbstractService.

Change-Id: I23c61d3f9b77e911e24a740ad489e25d0ac08c86
Michael Krotscheck 2 years ago
parent
commit
b60cb86755
No account linked to committer's email address
2 changed files with 262 additions and 0 deletions
  1. 116
    0
      test/unit/helpers/data/versions.js
  2. 146
    0
      test/unit/util/abstract_serviceTest.js

+ 116
- 0
test/unit/helpers/data/versions.js View File

@@ -0,0 +1,116 @@
1
+/*
2
+ * Copyright (c) 2016 Michael Krotscheck.
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ *     http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+
17
+/**
18
+ * This file contains test data for fetchMock, to simplify bootstrapping of unit tests for
19
+ * cross-service version detection.
20
+ */
21
+
22
+/**
23
+ * URLs to match the test data below.
24
+ */
25
+const rootUrl = "http://example.com/";
26
+
27
+/**
28
+ * A mock list of supported versions for the below requests.
29
+ *
30
+ * @type {Array}
31
+ */
32
+const versions = [
33
+  'v2.3'
34
+];
35
+
36
+/**
37
+ * Build a new FetchMock configuration for the versions (root) endpoint.
38
+ *
39
+ * @returns {{}} A full FetchMock configuration for Glance's Root Resource.
40
+ */
41
+function rootResponse() {
42
+  return {
43
+    method: 'GET',
44
+    matcher: rootUrl,
45
+    response: {
46
+      versions: [
47
+        {
48
+          status: "CURRENT",
49
+          id: "v2.3",
50
+          links: [
51
+            {
52
+              href: `${rootUrl}/v2/`,
53
+              rel: "self"
54
+            }
55
+          ]
56
+        },
57
+        {
58
+          status: "SUPPORTED",
59
+          id: "v2.2",
60
+          links: [
61
+            {
62
+              href: `${rootUrl}/v2/`,
63
+              rel: "self"
64
+            }
65
+          ]
66
+        },
67
+        {
68
+          status: "SUPPORTED",
69
+          id: "v2.1",
70
+          links: [
71
+            {
72
+              href: `${rootUrl}/v2/`,
73
+              rel: "self"
74
+            }
75
+          ]
76
+        },
77
+        {
78
+          status: "SUPPORTED",
79
+          id: "v2.0",
80
+          links: [
81
+            {
82
+              href: `${rootUrl}/v2/`,
83
+              rel: "self"
84
+            }
85
+          ]
86
+        },
87
+        {
88
+          status: "SUPPORTED",
89
+          id: "v1.1",
90
+          links: [
91
+            {
92
+              href: `${rootUrl}/v1/`,
93
+              rel: "self"
94
+            }
95
+          ]
96
+        },
97
+        {
98
+          status: "SUPPORTED",
99
+          id: "v1.0",
100
+          links: [
101
+            {
102
+              href: `${rootUrl}/v1/`,
103
+              rel: "self"
104
+            }
105
+          ]
106
+        }
107
+      ]
108
+    }
109
+  };
110
+}
111
+
112
+export {
113
+  rootUrl,
114
+  versions,
115
+  rootResponse
116
+};

+ 146
- 0
test/unit/util/abstract_serviceTest.js View File

@@ -0,0 +1,146 @@
1
+/*
2
+ * Copyright (c) 2016 Michael Krotscheck.
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ *     http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+
17
+import AbstractService from "../../../src/util/abstract_service";
18
+import * as mockData from '../helpers/data/versions'; // Might as well use keystone
19
+import fetchMock from "fetch-mock";
20
+
21
+describe('AbstractService', () => {
22
+
23
+  afterEach(fetchMock.restore);
24
+
25
+  it('should provide a singleton HTTP instance', () => {
26
+    let service = new AbstractService(mockData.rootUrl, mockData.versions);
27
+
28
+    expect(service._http).toBeUndefined();
29
+    let http1 = service.http;
30
+    expect(service._http).toBeDefined();
31
+    expect(http1).not.toBeNull();
32
+    let http2 = service.http;
33
+    expect(http1).toBe(http2);
34
+  });
35
+
36
+  it('should return supported versions', () => {
37
+    let service = new AbstractService(mockData.rootUrl, mockData.versions);
38
+    expect(service.supportedVersions).toEqual(mockData.versions);
39
+  });
40
+
41
+  it('should return an empty array if no versions are configured', () => {
42
+    let service = new AbstractService(mockData.rootUrl, null);
43
+    expect(service.supportedVersions).toEqual([]);
44
+  });
45
+
46
+  describe("versions()", () => {
47
+
48
+    it("Should return a list of all versions available from this resource", (done) => {
49
+      const service = new AbstractService(mockData.rootUrl, mockData.versions);
50
+
51
+      fetchMock.mock(mockData.rootResponse());
52
+
53
+      service.versions()
54
+        .then((versions) => {
55
+          // Quick sanity check.
56
+          expect(versions.length).toBe(6);
57
+          done();
58
+        })
59
+        .catch((error) => done.fail(error));
60
+    });
61
+
62
+    it("Should return a list of all versions available from this resource", (done) => {
63
+      const service = new AbstractService(mockData.rootUrl, mockData.versions);
64
+
65
+      fetchMock.mock(mockData.rootResponse());
66
+
67
+      service.versions()
68
+        .then((versions) => {
69
+          // Quick sanity check.
70
+          expect(versions.length).toBe(6);
71
+          done();
72
+        })
73
+        .catch((error) => done.fail(error));
74
+    });
75
+
76
+    it("Should NOT cache its results", (done) => {
77
+      const service = new AbstractService(mockData.rootUrl, mockData.versions);
78
+      const mockOptions = mockData.rootResponse();
79
+
80
+      fetchMock.mock(mockOptions);
81
+
82
+      service.versions()
83
+        .then(() => {
84
+          // Validate that the mock has only been invoked once
85
+          expect(fetchMock.calls(mockOptions.name).length).toEqual(1);
86
+          return service.versions();
87
+        })
88
+        .then(() => {
89
+          expect(fetchMock.calls(mockOptions.name).length).toEqual(2);
90
+          done();
91
+        })
92
+        .catch((error) => done.fail(error));
93
+    });
94
+  });
95
+
96
+  describe("version()", () => {
97
+
98
+    it("Should return a supported version of the service API.", (done) => {
99
+      const service = new AbstractService(mockData.rootUrl, mockData.versions);
100
+
101
+      fetchMock.mock(mockData.rootResponse());
102
+
103
+      service.version()
104
+        .then((version) => {
105
+          expect(version.id).toEqual('v2.3');
106
+          done();
107
+        })
108
+        .catch((error) => done.fail(error));
109
+    });
110
+
111
+    it("Should throw an exception if no supported version is found.", (done) => {
112
+      const service = new AbstractService(mockData.rootUrl, mockData.versions);
113
+
114
+      // Build an invalid mock object.
115
+      const mockOptions = mockData.rootResponse();
116
+      mockOptions.response.versions.shift();
117
+
118
+      fetchMock.mock(mockOptions);
119
+
120
+      service.version()
121
+        .then((response) => done.fail(response))
122
+        .catch((error) => {
123
+          expect(error).not.toBeNull();
124
+          done();
125
+        });
126
+    });
127
+
128
+    it("Should NOT cache its results", (done) => {
129
+      const service = new AbstractService(mockData.rootUrl, mockData.versions);
130
+      const mockOptions = mockData.rootResponse();
131
+      fetchMock.mock(mockOptions);
132
+
133
+      service.version()
134
+        .then(() => {
135
+          // Validate that the mock has only been invoked once
136
+          expect(fetchMock.calls(mockOptions.name).length).toEqual(1);
137
+          return service.version();
138
+        })
139
+        .then(() => {
140
+          expect(fetchMock.calls(mockOptions.name).length).toEqual(2);
141
+          done();
142
+        })
143
+        .catch((error) => done.fail(error));
144
+    });
145
+  });
146
+});

Loading…
Cancel
Save