GUI -- Sketched out MapService.
- implemented ID to URL adjustment, and object caching behaviors.

Change-Id: I5a30b59be01761d9a6475f7707db947dd2304dc5
diff --git a/web/gui/src/main/webapp/app/fw/svg/map.js b/web/gui/src/main/webapp/app/fw/svg/map.js
index 0b3088d..c736149 100644
--- a/web/gui/src/main/webapp/app/fw/svg/map.js
+++ b/web/gui/src/main/webapp/app/fw/svg/map.js
@@ -19,17 +19,81 @@
 
  @author Simon Hunt
  */
+
+/*
+    The Map Service caches GeoJSON maps, which can be loaded into the map
+    layer of the Topology View.
+
+    A GeoMap object can be fetched by ID. IDs that start with an asterisk
+    identify maps bundled with the GUI. IDs that do not start with an
+    asterisk are assumed to be URLs to externally provided data (exact
+    format to be decided).
+
+    e.g.  var geomap = MapService.fetchGeoMap('*continental-us');
+
+    The GeoMap object encapsulates topology data (features), and the
+    D3 projection object.
+
+    Note that, since the GeoMap instance is cached / shared, it should
+    contain no state.
+ */
+
 (function () {
     'use strict';
 
-    var $log;
+    // injected references
+    var $log, fs;
+
+    // internal state
+    var maps = d3.map(),
+        msgMs = 'MapService.',
+        bundledUrlPrefix = 'data/map/';
+
+    function getUrl(id) {
+        if (id[0] === '*') {
+            return bundledUrlPrefix + id.slice(1) + '.json';
+        }
+        return id + '.json';
+    }
 
     angular.module('onosSvg')
-        .factory('MapService', ['$log', function (_$log_) {
+        .factory('MapService', ['$log', 'FnService',
+
+        function (_$log_, _fs_) {
             $log = _$log_;
+            fs = _fs_;
+
+            function clearCache() {
+                maps = d3.map();
+            }
+
+            function fetchGeoMap(id) {
+                if (!fs.isS(id)) {
+                    return null;
+                }
+
+                var geomap = maps.get(id);
+
+                if (!geomap) {
+                    // need to fetch the data and build the object...
+                    geomap = {
+                        id: id,
+                        url: getUrl(id),
+                        wasCached: false
+                    };
+                    // TODO: use $http service to load map data asynchronously
+
+                    maps.set(id, geomap);
+                } else {
+                    geomap.wasCached = true;
+                }
+
+                return geomap;
+            }
 
             return {
-                tbd: function () {}
+                clearCache: clearCache,
+                fetchGeoMap: fetchGeoMap
             };
         }]);
 
diff --git a/web/gui/src/main/webapp/app/fw/svg/svg.js b/web/gui/src/main/webapp/app/fw/svg/svg.js
index 9f8351b..ba400bd 100644
--- a/web/gui/src/main/webapp/app/fw/svg/svg.js
+++ b/web/gui/src/main/webapp/app/fw/svg/svg.js
@@ -22,6 +22,6 @@
 (function () {
     'use strict';
 
-    angular.module('onosSvg', []);
+    angular.module('onosSvg', ['onosUtil']);
 
 }());
diff --git a/web/gui/src/main/webapp/tests/app/fw/svg/map-spec.js b/web/gui/src/main/webapp/tests/app/fw/svg/map-spec.js
index 2822bb9..159f36b 100644
--- a/web/gui/src/main/webapp/tests/app/fw/svg/map-spec.js
+++ b/web/gui/src/main/webapp/tests/app/fw/svg/map-spec.js
@@ -20,17 +20,80 @@
  @author Simon Hunt
  */
 describe('factory: fw/svg/map.js', function() {
-    var ms;
+    var $log, fs, ms, d3Elem, geomap;
 
-    beforeEach(module('onosSvg'));
+    var urlPrefix = 'data/map/';
 
-    beforeEach(inject(function (MapService) {
+    beforeEach(module('onosUtil', 'onosSvg'));
+
+    beforeEach(inject(function (_$log_, FnService, MapService) {
+        $log = _$log_;
+        fs = FnService;
         ms = MapService;
+        ms.clearCache();
+        // TODO: d3Elem = d3.select('body').append('...').attr('id', 'myFoo');
     }));
 
+    afterEach(function () {
+        // TODO d3.select('#myFoo').remove();
+    });
+
     it('should define MapService', function () {
         expect(ms).toBeDefined();
     });
 
-    // TODO: unit tests for map functions
+    it('should define api functions', function () {
+        expect(fs.areFunctions(ms, [
+            'clearCache', 'fetchGeoMap'
+        ])).toBeTruthy();
+    });
+
+    it('should return null when no parameters given', function () {
+        geomap = ms.fetchGeoMap();
+        expect(geomap).toBeNull();
+    });
+
+    it('should augment the id of a bundled map', function () {
+        var id = '*foo';
+        geomap = ms.fetchGeoMap(id);
+        expect(geomap).toBeDefined();
+        expect(geomap.id).toBe(id);
+        expect(geomap.url).toBe('data/map/foo.json');
+    });
+
+    it('should treat an external id as the url itself', function () {
+        var id = 'some/path/to/foo';
+        geomap = ms.fetchGeoMap(id);
+        expect(geomap).toBeDefined();
+        expect(geomap.id).toBe(id);
+        expect(geomap.url).toBe(id + '.json');
+    });
+
+    it('should cache the returned objects', function () {
+        var id = 'foo';
+        geomap = ms.fetchGeoMap(id);
+        expect(geomap).toBeDefined();
+        expect(geomap.wasCached).toBeFalsy();
+        expect(geomap.tagged).toBeUndefined();
+
+        geomap.tagged = 'I woz here';
+
+        geomap = ms.fetchGeoMap(id);
+        expect(geomap).toBeDefined();
+        expect(geomap.wasCached).toBeTruthy();
+        expect(geomap.tagged).toEqual('I woz here');
+    });
+
+    it('should clear the cache when asked', function () {
+        var id = 'foo';
+        geomap = ms.fetchGeoMap(id);
+        expect(geomap.wasCached).toBeFalsy();
+
+        geomap = ms.fetchGeoMap(id);
+        expect(geomap.wasCached).toBeTruthy();
+
+        ms.clearCache();
+        geomap = ms.fetchGeoMap(id);
+        expect(geomap.wasCached).toBeFalsy();
+    });
 });