Simon Hunt | f817338 | 2015-01-13 14:12:09 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 Open Networking Laboratory |
| 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 | ONOS GUI -- SVG -- GeoData Service - Unit Tests |
Simon Hunt | f817338 | 2015-01-13 14:12:09 -0800 | [diff] [blame] | 19 | */ |
| 20 | describe('factory: fw/svg/geodata.js', function() { |
| 21 | var $log, $httpBackend, fs, gds, promise; |
| 22 | |
| 23 | beforeEach(module('onosUtil', 'onosSvg')); |
| 24 | |
| 25 | beforeEach(inject(function (_$log_, _$httpBackend_, FnService, GeoDataService) { |
| 26 | $log = _$log_; |
| 27 | $httpBackend = _$httpBackend_; |
| 28 | fs = FnService; |
| 29 | gds = GeoDataService; |
| 30 | gds.clearCache(); |
| 31 | })); |
| 32 | |
| 33 | |
| 34 | it('should define GeoDataService', function () { |
| 35 | expect(gds).toBeDefined(); |
| 36 | }); |
| 37 | |
| 38 | it('should define api functions', function () { |
| 39 | expect(fs.areFunctions(gds, [ |
Simon Hunt | a7b6a6b | 2015-01-13 19:53:09 -0800 | [diff] [blame] | 40 | 'clearCache', 'fetchTopoData', 'createPathGenerator' |
Simon Hunt | f817338 | 2015-01-13 14:12:09 -0800 | [diff] [blame] | 41 | ])).toBeTruthy(); |
| 42 | }); |
| 43 | |
| 44 | it('should return null when no parameters given', function () { |
Simon Hunt | a7b6a6b | 2015-01-13 19:53:09 -0800 | [diff] [blame] | 45 | promise = gds.fetchTopoData(); |
Simon Hunt | f817338 | 2015-01-13 14:12:09 -0800 | [diff] [blame] | 46 | expect(promise).toBeNull(); |
| 47 | }); |
| 48 | |
| 49 | it('should augment the id of a bundled map', function () { |
| 50 | var id = '*foo'; |
Simon Hunt | a7b6a6b | 2015-01-13 19:53:09 -0800 | [diff] [blame] | 51 | promise = gds.fetchTopoData(id); |
Simon Hunt | f817338 | 2015-01-13 14:12:09 -0800 | [diff] [blame] | 52 | expect(promise.meta).toBeDefined(); |
| 53 | expect(promise.meta.id).toBe(id); |
Thomas Vachuska | e95da77 | 2015-02-23 15:50:11 -0800 | [diff] [blame] | 54 | expect(promise.meta.url).toBe('data/map/foo.json'); |
Simon Hunt | f817338 | 2015-01-13 14:12:09 -0800 | [diff] [blame] | 55 | }); |
| 56 | |
| 57 | it('should treat an external id as the url itself', function () { |
| 58 | var id = 'some/path/to/foo'; |
Simon Hunt | a7b6a6b | 2015-01-13 19:53:09 -0800 | [diff] [blame] | 59 | promise = gds.fetchTopoData(id); |
Simon Hunt | f817338 | 2015-01-13 14:12:09 -0800 | [diff] [blame] | 60 | expect(promise.meta).toBeDefined(); |
| 61 | expect(promise.meta.id).toBe(id); |
| 62 | expect(promise.meta.url).toBe(id + '.json'); |
| 63 | }); |
| 64 | |
| 65 | it('should cache the returned objects', function () { |
| 66 | var id = 'foo'; |
Simon Hunt | a7b6a6b | 2015-01-13 19:53:09 -0800 | [diff] [blame] | 67 | promise = gds.fetchTopoData(id); |
Simon Hunt | f817338 | 2015-01-13 14:12:09 -0800 | [diff] [blame] | 68 | expect(promise).toBeDefined(); |
| 69 | expect(promise.meta.wasCached).toBeFalsy(); |
| 70 | expect(promise.tagged).toBeUndefined(); |
| 71 | |
| 72 | promise.tagged = 'I woz here'; |
| 73 | |
Simon Hunt | a7b6a6b | 2015-01-13 19:53:09 -0800 | [diff] [blame] | 74 | promise = gds.fetchTopoData(id); |
Simon Hunt | f817338 | 2015-01-13 14:12:09 -0800 | [diff] [blame] | 75 | expect(promise).toBeDefined(); |
| 76 | expect(promise.meta.wasCached).toBeTruthy(); |
| 77 | expect(promise.tagged).toEqual('I woz here'); |
| 78 | }); |
| 79 | |
| 80 | it('should clear the cache when asked', function () { |
| 81 | var id = 'foo'; |
Simon Hunt | a7b6a6b | 2015-01-13 19:53:09 -0800 | [diff] [blame] | 82 | promise = gds.fetchTopoData(id); |
Simon Hunt | f817338 | 2015-01-13 14:12:09 -0800 | [diff] [blame] | 83 | expect(promise.meta.wasCached).toBeFalsy(); |
| 84 | |
Simon Hunt | a7b6a6b | 2015-01-13 19:53:09 -0800 | [diff] [blame] | 85 | promise = gds.fetchTopoData(id); |
Simon Hunt | f817338 | 2015-01-13 14:12:09 -0800 | [diff] [blame] | 86 | expect(promise.meta.wasCached).toBeTruthy(); |
| 87 | |
| 88 | gds.clearCache(); |
Simon Hunt | a7b6a6b | 2015-01-13 19:53:09 -0800 | [diff] [blame] | 89 | promise = gds.fetchTopoData(id); |
Simon Hunt | f817338 | 2015-01-13 14:12:09 -0800 | [diff] [blame] | 90 | expect(promise.meta.wasCached).toBeFalsy(); |
| 91 | }); |
| 92 | |
| 93 | |
| 94 | it('should log a warning if data fails to load', function () { |
| 95 | var id = 'foo'; |
| 96 | $httpBackend.expectGET('foo.json').respond(404, 'Not found'); |
| 97 | spyOn($log, 'warn'); |
| 98 | |
Simon Hunt | a7b6a6b | 2015-01-13 19:53:09 -0800 | [diff] [blame] | 99 | promise = gds.fetchTopoData(id); |
Simon Hunt | f817338 | 2015-01-13 14:12:09 -0800 | [diff] [blame] | 100 | $httpBackend.flush(); |
Simon Hunt | a7b6a6b | 2015-01-13 19:53:09 -0800 | [diff] [blame] | 101 | expect(promise.topodata).toBeUndefined(); |
Simon Hunt | f817338 | 2015-01-13 14:12:09 -0800 | [diff] [blame] | 102 | expect($log.warn) |
Simon Hunt | a7b6a6b | 2015-01-13 19:53:09 -0800 | [diff] [blame] | 103 | .toHaveBeenCalledWith('Failed to retrieve map TopoJSON data: foo.json', |
Simon Hunt | f817338 | 2015-01-13 14:12:09 -0800 | [diff] [blame] | 104 | 404, 'Not found'); |
| 105 | }); |
| 106 | |
Simon Hunt | a7b6a6b | 2015-01-13 19:53:09 -0800 | [diff] [blame] | 107 | // --- path generator tests |
| 108 | |
| 109 | function simpleTopology(object) { |
| 110 | return { |
| 111 | type: "Topology", |
| 112 | transform: {scale: [1, 1], translate: [0, 0]}, |
| 113 | objects: {states: object}, |
| 114 | arcs: [ |
| 115 | [[0, 0], [1, 0], [0, 1], [-1, 0], [0, -1]], |
| 116 | [[0, 0], [1, 0], [0, 1]], |
| 117 | [[1, 1], [-1, 0], [0, -1]], |
| 118 | [[1, 1]], |
| 119 | [[0, 0]] |
| 120 | ] |
| 121 | }; |
| 122 | } |
| 123 | |
| 124 | function simpleLineStringTopo() { |
| 125 | return simpleTopology({type: "LineString", arcs: [1, 2]}); |
| 126 | } |
| 127 | |
| 128 | it('should use default settings if none are supplied', function () { |
| 129 | var gen = gds.createPathGenerator(simpleLineStringTopo()); |
| 130 | expect(gen.settings.objectTag).toBe('states'); |
| 131 | expect(gen.settings.logicalSize).toBe(1000); |
| 132 | expect(gen.settings.mapFillScale).toBe(.95); |
| 133 | // best we can do for now is test that projection is a function ... |
| 134 | expect(fs.isF(gen.settings.projection)).toBeTruthy(); |
| 135 | }); |
| 136 | |
| 137 | it('should allow us to override default settings', function () { |
| 138 | var gen = gds.createPathGenerator(simpleLineStringTopo(), { |
| 139 | mapFillScale: .80 |
| 140 | }); |
| 141 | expect(gen.settings.objectTag).toBe('states'); |
| 142 | expect(gen.settings.logicalSize).toBe(1000); |
| 143 | expect(gen.settings.mapFillScale).toBe(.80); |
| 144 | }); |
| 145 | |
| 146 | it('should create transformed geodata, and a path generator', function () { |
| 147 | var gen = gds.createPathGenerator(simpleLineStringTopo()); |
| 148 | expect(fs.isO(gen.settings)).toBeTruthy(); |
| 149 | expect(fs.isO(gen.geodata)).toBeTruthy(); |
| 150 | expect(fs.isF(gen.pathgen)).toBeTruthy(); |
| 151 | }); |
| 152 | // NOTE: we probably should have more unit tests that assert stuff about |
| 153 | // the transformed data (geo data) -- though perhaps we can rely on |
| 154 | // the unit testing of TopoJSON? See... |
| 155 | // https://github.com/mbostock/topojson/blob/master/test/feature-test.js |
| 156 | // and, what about the path generator?, and the computed bounds? |
| 157 | // In summary, more work should be done here.. |
| 158 | |
Simon Hunt | f817338 | 2015-01-13 14:12:09 -0800 | [diff] [blame] | 159 | }); |