1 | import 'reflect-metadata';
|
2 | import kernel from '../inversify.config';
|
3 | import {expect} from 'chai';
|
4 | import {} from 'mocha';
|
5 | import {RemoteCatalogGetter} from "../interfaces/remote-catalog/remote-catalog-getter";
|
6 | import path = require('path');
|
7 |
|
8 | const Url = require('url');
|
9 |
|
10 | describe('Testing RemoteCatalogGetter Creation/Force Error', () => {
|
11 | let remoteCatalogGetter: RemoteCatalogGetter;
|
12 | beforeEach(() => {
|
13 | remoteCatalogGetter = kernel.get<RemoteCatalogGetter>('RemoteCatalogGetter');
|
14 | });
|
15 | afterEach(() => {
|
16 | remoteCatalogGetter.forceError = false;
|
17 | });
|
18 | it('should be created by kernel', (done) => {
|
19 | expect(remoteCatalogGetter).to.exist;
|
20 | done();
|
21 | });
|
22 | });
|
23 |
|
24 | describe('Testing RemoteCatalogGetter.parsedUrl', () => {
|
25 | let remoteCatalogGetter: RemoteCatalogGetter;
|
26 | beforeEach(() => {
|
27 | remoteCatalogGetter = kernel.get<RemoteCatalogGetter>('RemoteCatalogGetter');
|
28 | });
|
29 | afterEach(() => {
|
30 | remoteCatalogGetter.forceError = false;
|
31 | });
|
32 | it('should have callback with error', (done) => {
|
33 | remoteCatalogGetter.forceError = true;
|
34 | remoteCatalogGetter.getParsedUrl(null, (err, parsedUrl) => {
|
35 | expect(err).to.exist;
|
36 | expect(err.message).to.equal('force error: RemoteCatalogGetterImpl.getParsedUrl');
|
37 | expect(parsedUrl).to.not.exist;
|
38 | done();
|
39 | });
|
40 | });
|
41 | it('should have callback with error when url is undefined', (done) => {
|
42 | let url;
|
43 |
|
44 | remoteCatalogGetter.getParsedUrl(url, (err, parsedUrl) => {
|
45 | expect(err).to.exist;
|
46 | expect(err.message).to.equal(`Cannot read property 'constructor' of undefined`);
|
47 | expect(parsedUrl).to.not.exist;
|
48 | done();
|
49 | });
|
50 | });
|
51 | it('should have callback with error when url is null', (done) => {
|
52 | let url = null;
|
53 | remoteCatalogGetter.getParsedUrl(url, (err, parsedUrl) => {
|
54 | expect(err).to.exist;
|
55 | expect(err.message).to.equal(`Cannot read property 'constructor' of null`);
|
56 | expect(parsedUrl).to.not.exist;
|
57 | done();
|
58 | });
|
59 | });
|
60 | it('should have callback with error when url is and empty string', (done) => {
|
61 | let url = '';
|
62 | remoteCatalogGetter.getParsedUrl(url, (err, parsedUrl) => {
|
63 | expect(err).to.exist;
|
64 | expect(err.message).to.equal('Empty string is not a url');
|
65 | expect(parsedUrl).to.not.exist;
|
66 | done();
|
67 | });
|
68 | });
|
69 | it('should return parsed web url', (done) => {
|
70 | let url = 'http://www.yahoo.com/grindel.jpg';
|
71 | remoteCatalogGetter.getParsedUrl(url, (err, parsedUrl) => {
|
72 | expect(err).to.not.exist;
|
73 | expect(parsedUrl).to.be.an.instanceof(Url.constructor);
|
74 | expect(parsedUrl.protocol).to.equal('http:');
|
75 | done();
|
76 | });
|
77 | });
|
78 | it('should return parsed file url', (done) => {
|
79 | let url = '/tmp/grindel.jpg';
|
80 | remoteCatalogGetter.getParsedUrl(url, (err, parsedUrl) => {
|
81 | expect(err).to.not.exist;
|
82 | expect(parsedUrl).to.be.an.instanceof(Url.constructor);
|
83 | expect(parsedUrl.protocol).to.not.exist;
|
84 | done();
|
85 | });
|
86 | });
|
87 | });
|
88 |
|
89 | describe('Testing RemoteCatalogGetter.resolveTextResourceFromUrl', () => {
|
90 | let remoteCatalogGetter: RemoteCatalogGetter;
|
91 | beforeEach(() => {
|
92 | remoteCatalogGetter = kernel.get<RemoteCatalogGetter>('RemoteCatalogGetter');
|
93 | });
|
94 | afterEach(() => {
|
95 | remoteCatalogGetter.forceError = false;
|
96 | });
|
97 | it('should have callback with error', (done) => {
|
98 | remoteCatalogGetter.forceError = true;
|
99 | remoteCatalogGetter.resolveTextResourceFromUrl(null, (err, text, absoluteUrl) => {
|
100 | expect(err).to.exist;
|
101 | expect(err.message).to.equal('force error: RemoteCatalogGetterImpl.resolveTextResourceFromUrl');
|
102 | expect(absoluteUrl).to.not.exist;
|
103 | done();
|
104 | });
|
105 | });
|
106 | it('should have callback with error when url is undefined', (done) => {
|
107 | let url;
|
108 |
|
109 | remoteCatalogGetter.resolveTextResourceFromUrl(url, (err, text, absoluteUrl) => {
|
110 | expect(err).to.exist;
|
111 | expect(err.message).to.equal(`Cannot read property 'constructor' of undefined`);
|
112 | expect(absoluteUrl).to.not.exist;
|
113 | done();
|
114 | });
|
115 | });
|
116 | it('should have callback with error when url is null', (done) => {
|
117 | const url = null;
|
118 | remoteCatalogGetter.resolveTextResourceFromUrl(url, (err, text, absoluteUrl) => {
|
119 | expect(err).to.exist;
|
120 | expect(err.message).to.equal(`Cannot read property 'constructor' of null`);
|
121 | expect(absoluteUrl).to.not.exist;
|
122 | done();
|
123 | });
|
124 | });
|
125 | it('should have callback with error when url is and empty string', (done) => {
|
126 | const url = '';
|
127 | remoteCatalogGetter.resolveTextResourceFromUrl(url, (err, text, absoluteUrl) => {
|
128 | expect(err).to.exist;
|
129 | expect(err.message).to.equal('Empty string is not a url');
|
130 | expect(absoluteUrl).to.not.exist;
|
131 | done();
|
132 | });
|
133 | });
|
134 | it(`should return non-null error if file doesn't exist`, (done) => {
|
135 | const cwd = process.cwd();
|
136 | const url = path.resolve(cwd, 'ts/test/json/valid__.json');
|
137 | remoteCatalogGetter.resolveTextResourceFromUrl(url, (err, text, absoluteUrl) => {
|
138 | expect(err).to.exist;
|
139 | expect(err.message).to.equal(`Url: '/home/jreeme/src/firmament-yargs/ts/test/json/valid__.json' does not exist`);
|
140 | expect(absoluteUrl).to.equal(url);
|
141 | expect(text).to.be.empty;
|
142 | done();
|
143 | });
|
144 | });
|
145 | it(`should return null error if file exists and text should be '12345'`, (done) => {
|
146 | const cwd = process.cwd();
|
147 | const url = path.resolve(cwd, 'ts/test/json/exists.json');
|
148 | remoteCatalogGetter.resolveTextResourceFromUrl(url, (err, text, absoluteUrl) => {
|
149 | expect(err).to.not.exist;
|
150 | expect(absoluteUrl).to.equal(url);
|
151 | expect(text).to.equal('12345\n');
|
152 | done();
|
153 | });
|
154 | });
|
155 | it(`should return non-null error if web resource doesn't exist`, (done) => {
|
156 | const url = 'http://www.nositeatall.com/gumdrop.jpg';
|
157 | remoteCatalogGetter.resolveTextResourceFromUrl(url, (err, text, absoluteUrl) => {
|
158 | expect(err).to.exist;
|
159 | expect(absoluteUrl).to.equal(url);
|
160 | expect(text).to.be.empty;
|
161 | done();
|
162 | });
|
163 | });
|
164 | it(`should return null error if web resource exists`, (done) => {
|
165 | const url = 'http://www.yahoo.com/';
|
166 | remoteCatalogGetter.resolveTextResourceFromUrl(url, (err, text, absoluteUrl) => {
|
167 | expect(err).to.not.exist;
|
168 | expect(absoluteUrl).to.equal(url);
|
169 | expect(text).to.be.not.empty;
|
170 | done();
|
171 | });
|
172 | });
|
173 | });
|
174 |
|
175 | describe('Testing RemoteCatalogGetter.resolveJsonObjectFromUrl', () => {
|
176 | let remoteCatalogGetter: RemoteCatalogGetter;
|
177 | beforeEach(() => {
|
178 | remoteCatalogGetter = kernel.get<RemoteCatalogGetter>('RemoteCatalogGetter');
|
179 | });
|
180 | afterEach(() => {
|
181 | remoteCatalogGetter.forceError = false;
|
182 | });
|
183 | it('should have callback with error', (done) => {
|
184 | remoteCatalogGetter.forceError = true;
|
185 | remoteCatalogGetter.resolveJsonObjectFromUrl(null, (err, jsonObject, absoluteUrl) => {
|
186 | expect(err).to.exist;
|
187 | expect(err.message).to.equal('force error: RemoteCatalogGetterImpl.resolveJsonObjectFromUrl');
|
188 | expect(absoluteUrl).to.not.exist;
|
189 | done();
|
190 | });
|
191 | });
|
192 | it('should have callback with error when url is undefined', (done) => {
|
193 | let url;
|
194 |
|
195 | remoteCatalogGetter.resolveJsonObjectFromUrl(url, (err, jsonObject, absoluteUrl) => {
|
196 | expect(err).to.exist;
|
197 | expect(err.message).to.equal(`Cannot read property 'constructor' of undefined`);
|
198 | expect(absoluteUrl).to.not.exist;
|
199 | done();
|
200 | });
|
201 | });
|
202 | it('should have callback with error when url is null', (done) => {
|
203 | const url = null;
|
204 | remoteCatalogGetter.resolveJsonObjectFromUrl(url, (err, jsonObject, absoluteUrl) => {
|
205 | expect(err).to.exist;
|
206 | expect(err.message).to.equal(`Cannot read property 'constructor' of null`);
|
207 | expect(absoluteUrl).to.not.exist;
|
208 | done();
|
209 | });
|
210 | });
|
211 | it('should return error on bad JSON', (done) => {
|
212 | const cwd = process.cwd();
|
213 | const url = path.resolve(cwd, 'ts/test/json/bad-json.json');
|
214 | remoteCatalogGetter.resolveJsonObjectFromUrl(url, (err, jsonObject, absoluteUrl) => {
|
215 | expect(err).to.exist;
|
216 | expect(err.message).to.equal('Unexpected token : in JSON at position 1');
|
217 | expect(absoluteUrl).to.equal(url);
|
218 | done();
|
219 | });
|
220 | });
|
221 | it('should return ', (done) => {
|
222 | const cwd = process.cwd();
|
223 | const url = path.resolve(cwd, 'ts/test/json/valid.json');
|
224 | remoteCatalogGetter.resolveJsonObjectFromUrl(url, (err, jsonObject, absoluteUrl) => {
|
225 | expect(err).to.not.exist;
|
226 | expect(jsonObject).to.include({description: 'Command Group 0'});
|
227 | expect(absoluteUrl).to.equal(url);
|
228 | done();
|
229 | });
|
230 | });
|
231 | });
|
232 |
|
233 | describe('Testing RemoteCatalogGetter.getRemoteResource', () => {
|
234 | let remoteCatalogGetter: RemoteCatalogGetter;
|
235 | beforeEach(() => {
|
236 | remoteCatalogGetter = kernel.get<RemoteCatalogGetter>('RemoteCatalogGetter');
|
237 | });
|
238 | afterEach(() => {
|
239 | remoteCatalogGetter.forceError = false;
|
240 | });
|
241 | it('should have callback with error', (done) => {
|
242 | remoteCatalogGetter.forceError = true;
|
243 | remoteCatalogGetter.getRemoteResource(null, null, (err, remoteCatalogResource) => {
|
244 | expect(err).to.exist;
|
245 | expect(err.message).to.equal('force error: RemoteCatalogGetterImpl.getRemoteResource');
|
246 | expect(remoteCatalogResource).to.not.exist;
|
247 | done();
|
248 | });
|
249 | });
|
250 | it('should have callback with error when url is undefined', (done) => {
|
251 | let url;
|
252 |
|
253 | remoteCatalogGetter.getRemoteResource(url, null, (err, remoteCatalogResource) => {
|
254 | expect(err).to.exist;
|
255 | expect(err.message).to.equal(`Cannot read property 'constructor' of undefined`);
|
256 | expect(remoteCatalogResource).to.not.exist;
|
257 | done();
|
258 | });
|
259 | });
|
260 | it('should have callback with error when url is null', (done) => {
|
261 | const url = null;
|
262 | remoteCatalogGetter.getRemoteResource(url, null, (err, remoteCatalogResource) => {
|
263 | expect(err).to.exist;
|
264 | expect(err.message).to.equal(`Cannot read property 'constructor' of null`);
|
265 | expect(remoteCatalogResource).to.not.exist;
|
266 | done();
|
267 | });
|
268 | });
|
269 | it('should have callback with error when json resource does not exist', (done) => {
|
270 | const cwd = process.cwd();
|
271 | const nonExistentJsonFile = 'valid__.json';
|
272 | const url = path.resolve(cwd, 'ts/test/json', nonExistentJsonFile);
|
273 | const parentCatalogEntryName = 'The parent catalog entry name!';
|
274 |
|
275 | remoteCatalogGetter.getRemoteResource(url, parentCatalogEntryName, (err, remoteCatalogResource) => {
|
276 | expect(err).to.exist;
|
277 | expect(err.message).to.equal(`Url: '/home/jreeme/src/firmament-yargs/ts/test/json/valid__.json' does not exist`);
|
278 | expect(remoteCatalogResource).to.not.exist;
|
279 | done();
|
280 | });
|
281 | });
|
282 | it('should have callback with error when resource does not contain valid json', (done) => {
|
283 | const cwd = process.cwd();
|
284 | const nonExistentJsonFile = 'bad-json.json';
|
285 | const url = path.resolve(cwd, 'ts/test/json', nonExistentJsonFile);
|
286 | const parentCatalogEntryName = 'The parent catalog entry name!';
|
287 |
|
288 | remoteCatalogGetter.getRemoteResource(url, parentCatalogEntryName, (err, remoteCatalogResource) => {
|
289 | expect(err).to.exist;
|
290 | expect(err.message).to.equal('Unexpected token : in JSON at position 1');
|
291 | expect(remoteCatalogResource).to.exist;
|
292 | done();
|
293 | });
|
294 | });
|
295 | it('should provide reasonable remoteCatalogResource when given link to valid.json', (done) => {
|
296 | const cwd = process.cwd();
|
297 | const validJson = 'valid.json';
|
298 | const url = path.resolve(cwd, 'ts/test/json', validJson);
|
299 | const parentCatalogEntryName = 'The parent catalog entry name!';
|
300 |
|
301 | remoteCatalogGetter.getRemoteResource(url, parentCatalogEntryName, (err, remoteCatalogResource) => {
|
302 | expect(err).to.not.exist;
|
303 |
|
304 | expect(remoteCatalogResource).to.exist;
|
305 | expect(remoteCatalogResource).to.include({
|
306 | name: validJson
|
307 | });
|
308 | expect(remoteCatalogResource.parentCatalogEntryName).to.equal(parentCatalogEntryName);
|
309 | done();
|
310 | });
|
311 | });
|
312 | });
|
313 |
|
314 | describe('Testing RemoteCatalogGetter.getCatalogFromUrl', () => {
|
315 | let remoteCatalogGetter: RemoteCatalogGetter;
|
316 | beforeEach(() => {
|
317 | remoteCatalogGetter = kernel.get<RemoteCatalogGetter>('RemoteCatalogGetter');
|
318 | });
|
319 | afterEach(() => {
|
320 | remoteCatalogGetter.forceError = false;
|
321 | });
|
322 | it('should have callback with error', (done) => {
|
323 | remoteCatalogGetter.forceError = true;
|
324 | remoteCatalogGetter.getCatalogFromUrl(null, (err, remoteCatalog) => {
|
325 | expect(err).to.exist;
|
326 | expect(err.message).to.equal('force error: RemoteCatalogGetterImpl.getCatalogFromUrl');
|
327 | expect(remoteCatalog).to.not.exist;
|
328 | done();
|
329 | });
|
330 | });
|
331 | it('should have callback with error when url is undefined', (done) => {
|
332 | let url;
|
333 |
|
334 | remoteCatalogGetter.getCatalogFromUrl(url, (err, remoteCatalog) => {
|
335 | expect(err).to.exist;
|
336 | expect(err.message).to.equal(`Cannot read property 'constructor' of undefined`);
|
337 | expect(remoteCatalog).to.not.exist;
|
338 | done();
|
339 | });
|
340 | });
|
341 | it('should have callback with error when url is null', (done) => {
|
342 | const url = null;
|
343 | remoteCatalogGetter.getCatalogFromUrl(url, (err, remoteCatalog) => {
|
344 | expect(err).to.exist;
|
345 | expect(err.message).to.equal(`Cannot read property 'constructor' of null`);
|
346 | expect(remoteCatalog).to.not.exist;
|
347 | done();
|
348 | });
|
349 | });
|
350 | it('should have callback with error when json resource does not exist', (done) => {
|
351 | const cwd = process.cwd();
|
352 | const nonExistentJsonFile = 'valid__.json';
|
353 | const url = path.resolve(cwd, 'ts/test/json', nonExistentJsonFile);
|
354 | remoteCatalogGetter.getCatalogFromUrl(url, (err, remoteCatalog) => {
|
355 | expect(err).to.exist;
|
356 | expect(err.message).to.equal(`Url: '[object Object]' does not exist`);
|
357 | expect(remoteCatalog).to.not.exist;
|
358 | done();
|
359 | });
|
360 | });
|
361 | it('should have callback with error when resource does not contain valid json', (done) => {
|
362 | const cwd = process.cwd();
|
363 | const nonExistentJsonFile = 'bad-json.json';
|
364 | const url = path.resolve(cwd, 'ts/test/json', nonExistentJsonFile);
|
365 | remoteCatalogGetter.getCatalogFromUrl(url, (err, remoteCatalog) => {
|
366 | expect(err).to.exist;
|
367 | expect(err.message).to.equal('Unexpected token : in JSON at position 1');
|
368 | expect(remoteCatalog).to.not.exist;
|
369 | done();
|
370 | });
|
371 | });
|
372 | it('should return error on catalog containing bad url', (done) => {
|
373 | const cwd = process.cwd();
|
374 | const nonExistentJsonFile = 'badCommandCatalog.json';
|
375 | const url = path.resolve(cwd, 'ts/test/json', nonExistentJsonFile);
|
376 | remoteCatalogGetter.getCatalogFromUrl(url, (err, remoteCatalog) => {
|
377 | expect(err).to.exist;
|
378 | expect(err.message).to.equal(`Url: '/home/jreeme/src/firmament-yargs/ts/test/json/prep-ubuntu-16.04-server---.json' does not exist`);
|
379 | expect(remoteCatalog).to.not.exist;
|
380 | done();
|
381 | });
|
382 | });
|
383 | it('should return local remoteCatalog', (done) => {
|
384 | const cwd = process.cwd();
|
385 | const nonExistentJsonFile = 'commandCatalog.json';
|
386 | const url = path.resolve(cwd, 'ts/test/json', nonExistentJsonFile);
|
387 | remoteCatalogGetter.getCatalogFromUrl(url, (err, remoteCatalog) => {
|
388 | expect(err).to.not.exist;
|
389 | expect(remoteCatalog).to.exist;
|
390 | done();
|
391 | });
|
392 | });
|
393 | it('should return local remoteCatalog', (done) => {
|
394 | const url = 'https://raw.githubusercontent.com/jreeme/firmament-bash/master/command-json/commandCatalog.json';
|
395 | remoteCatalogGetter.getCatalogFromUrl(url, (err, remoteCatalog) => {
|
396 | expect(err).to.not.exist;
|
397 | expect(remoteCatalog).to.exist;
|
398 | done();
|
399 | });
|
400 | });
|
401 | });
|