Moved modelHelpers service in the DataSources module

Change-Id: Ife4239d8f0ca0ec12ed7db17c337deb6e06e80fe
diff --git a/src/app/datasources/helpers/model.helpers.spec.ts b/src/app/datasources/helpers/model.helpers.spec.ts
new file mode 100644
index 0000000..ee23b28
--- /dev/null
+++ b/src/app/datasources/helpers/model.helpers.spec.ts
@@ -0,0 +1,28 @@
+import * as angular from 'angular';
+import 'angular-mocks';
+import 'angular-ui-router';
+
+import {IXosModelHelpersService, ModelHelpers} from './model.helpers';
+
+let service: IXosModelHelpersService;
+describe('The ModelHelpers service', () => {
+
+  beforeEach(() => {
+    angular
+      .module('test', [])
+      .service('ModelHelpers', ModelHelpers);
+
+    angular.mock.module('test');
+  });
+
+  beforeEach(angular.mock.inject((
+    ModelHelpers: IXosModelHelpersService,
+  ) => {
+    service = ModelHelpers;
+  }));
+
+  it('should convert a core model name in an URL', () => {
+    expect(service.urlFromCoreModel('Slice')).toBe('/core/slices');
+  });
+
+});
diff --git a/src/app/datasources/helpers/model.helpers.ts b/src/app/datasources/helpers/model.helpers.ts
new file mode 100644
index 0000000..81825d3
--- /dev/null
+++ b/src/app/datasources/helpers/model.helpers.ts
@@ -0,0 +1,9 @@
+export interface IXosModelHelpersService {
+  urlFromCoreModel(name: string): string;
+}
+
+export class ModelHelpers {
+  urlFromCoreModel(name: string): string {
+    return `/core/${name.toLowerCase()}s`;
+  }
+}
diff --git a/src/app/datasources/helpers/store.helpers.spec.ts b/src/app/datasources/helpers/store.helpers.spec.ts
new file mode 100644
index 0000000..b2085e3
--- /dev/null
+++ b/src/app/datasources/helpers/store.helpers.spec.ts
@@ -0,0 +1,124 @@
+import * as angular from 'angular';
+import 'angular-mocks';
+import 'angular-ui-router';
+import {StoreHelpers, IStoreHelpersService} from './store.helpers';
+import {ModelHelpers} from './model.helpers';
+import {ModelRest} from '../rest/model.rest';
+import {BehaviorSubject} from 'rxjs';
+import {IWSEvent} from '../websocket/global';
+
+let service: IStoreHelpersService;
+let subject: BehaviorSubject<any>;
+let resource: ng.resource.IResourceClass<any>;
+let $resource: ng.resource.IResourceService;
+
+describe('The StoreHelpers service', () => {
+
+  beforeEach(() => {
+    angular
+      .module('test', ['ngResource'])
+      .service('ModelHelpers', ModelHelpers) // NOTE evaluate mock
+      .service('ModelRest', ModelRest) // NOTE evaluate mock
+      .service('StoreHelpers', StoreHelpers);
+
+    angular.mock.module('test');
+  });
+
+  beforeEach(angular.mock.inject((
+    StoreHelpers: IStoreHelpersService,
+    _$resource_: ng.resource.IResourceService
+  ) => {
+    $resource = _$resource_;
+    resource = $resource('/test');
+    service = StoreHelpers;
+  }));
+
+  it('should have an update collection method', () => {
+    expect(service.updateCollection).toBeDefined();
+  });
+
+  describe('when updating a collection', () => {
+
+    beforeEach(() => {
+      subject = new BehaviorSubject([
+        new resource({id: 1, name: 'test'})
+      ]);
+    });
+
+    it('should remove a model if it has been deleted', () => {
+      const event: IWSEvent = {
+        model: 'Test',
+        msg: {
+          object: {
+            id: 1,
+            name: 'test'
+          },
+          changed_fields: ['deleted']
+        }
+      };
+      service.updateCollection(event, subject);
+      expect(subject.value.length).toBe(0);
+    });
+
+    it('should update a model if it has been updated', () => {
+      const event: IWSEvent = {
+        model: 'Test',
+        msg: {
+          object: {
+            id: 1,
+            name: 'test-updated'
+          },
+          changed_fields: ['name']
+        }
+      };
+      service.updateCollection(event, subject);
+      expect(subject.value.length).toBe(1);
+      expect(subject.value[0].name).toBe('test-updated');
+    });
+
+    it('should add a model if it has been created', () => {
+      const event: IWSEvent = {
+        model: 'Test',
+        msg: {
+          object: {
+            id: 2,
+            name: 'another-test'
+          },
+          changed_fields: ['created']
+        }
+      };
+      service.updateCollection(event, subject);
+      expect(subject.value.length).toBe(2);
+      expect(subject.value[0].name).toBe('test');
+      expect(subject.value[1].name).toBe('another-test');
+    });
+
+    describe('when adding a model', () => {
+
+      beforeEach(() => {
+        const event: IWSEvent = {
+          model: 'Test',
+          msg: {
+            object: {
+              id: 2,
+              name: 'another-test'
+            },
+            changed_fields: ['created']
+          }
+        };
+        service.updateCollection(event, subject);
+      });
+
+      it('should create a resource', () => {
+        expect(subject.value[1].$save).toBeDefined();
+        expect(subject.value[1].$delete).toBeDefined();
+      });
+
+      xit('should automatically create the appropriate resource', () => {
+        // TODO test that the url of the resource is the correct one,
+        // use httpbackend and mock a call?? any faster way??
+      });
+    });
+  });
+
+});
diff --git a/src/app/datasources/helpers/store.helpers.ts b/src/app/datasources/helpers/store.helpers.ts
index 1a2b0ab..99662da 100644
--- a/src/app/datasources/helpers/store.helpers.ts
+++ b/src/app/datasources/helpers/store.helpers.ts
@@ -1,12 +1,22 @@
 import {BehaviorSubject} from 'rxjs';
 import * as _ from 'lodash';
 import {IWSEvent} from '../websocket/global';
+import {IXosModelHelpersService} from './model.helpers';
+import {IXosResourceService} from '../rest/model.rest';
 
 export interface IStoreHelpersService {
   updateCollection(event: IWSEvent, subject: BehaviorSubject<any>): BehaviorSubject<any>;
 }
 
 export class StoreHelpers {
+  static $inject = ['ModelHelpers', 'ModelRest'];
+
+  constructor (
+    private modelHelpers: IXosModelHelpersService,
+    private modelRest: IXosResourceService
+  ) {
+  }
+
   public updateCollection(event: IWSEvent, subject: BehaviorSubject<any>): BehaviorSubject<any> {
     const collection: any[] = subject.value;
     const index: number = _.findIndex(collection, (i) => {
@@ -15,17 +25,23 @@
     });
     const exist: boolean = index > -1;
     const isDeleted: boolean = _.includes(event.msg.changed_fields, 'deleted');
-       // remove
+
+    // generate a resource for the model
+    const endpoint = this.modelHelpers.urlFromCoreModel(event.model);
+    const resource = this.modelRest.getResource(endpoint);
+    const model = new resource(event.msg.object);
+
+    // remove
     if (exist && isDeleted) {
        _.remove(collection, {id: event.msg.object.id});
      }
     // Replace item at index using native splice
     else if (exist && !isDeleted) {
-       collection.splice(index, 1, event.msg.object);
+       collection.splice(index, 1, model);
      }
     // if the element is not deleted add it
     else if (!exist && !isDeleted) {
-      collection.push(event.msg.object);
+      collection.push(model);
      }
 
     subject.next(collection);
diff --git a/src/app/datasources/index.ts b/src/app/datasources/index.ts
index 7a14a9a..b6897ed 100644
--- a/src/app/datasources/index.ts
+++ b/src/app/datasources/index.ts
@@ -5,12 +5,12 @@
 import {StoreHelpers} from './helpers/store.helpers';
 import {SynchronizerStore} from './stores/synchronizer.store';
 import {ModeldefsService} from './rest/modeldefs.rest';
-import {xosCore} from '../core/index';
+import {ModelHelpers} from './helpers/model.helpers';
 
 export const xosDataSources = 'xosDataSources';
 
 angular
-  .module('xosDataSources', ['ngCookies', 'ngResource', xosCore])
+  .module('xosDataSources', ['ngCookies', 'ngResource'])
   .service('ModelRest', ModelRest)
   .service('AuthService', AuthService)
   .service('WebSocket', WebSocketEvent);
@@ -18,6 +18,7 @@
 angular
   .module('xosDataSources')
   .service('StoreHelpers', StoreHelpers)
+  .service('ModelHelpers', ModelHelpers)
   .service('SynchronizerStore', SynchronizerStore)
   .service('ModelStore', ModelStore)
   .service('ModelDefs', ModeldefsService);
diff --git a/src/app/datasources/stores/model.store.spec.ts b/src/app/datasources/stores/model.store.spec.ts
index a6edcb9..49b9338 100644
--- a/src/app/datasources/stores/model.store.spec.ts
+++ b/src/app/datasources/stores/model.store.spec.ts
@@ -7,7 +7,7 @@
 import {StoreHelpers} from '../helpers/store.helpers';
 import {ModelRest} from '../rest/model.rest';
 import {AppConfig} from '../../config/app.config';
-import {ModelHelpers} from '../../core/services/helpers/model.helper';
+import {ModelHelpers} from '../helpers/model.helpers';
 
 let service: IModelStoreService;
 let httpBackend: ng.IHttpBackendService;
diff --git a/src/app/datasources/stores/model.store.ts b/src/app/datasources/stores/model.store.ts
index 015d729..91cdf24 100644
--- a/src/app/datasources/stores/model.store.ts
+++ b/src/app/datasources/stores/model.store.ts
@@ -4,7 +4,7 @@
 import {IWSEvent, IWSEventService} from '../websocket/global';
 import {IXosResourceService} from '../rest/model.rest';
 import {IStoreHelpersService} from '../helpers/store.helpers';
-import {IXosModelHelpersService} from '../../core/services/helpers/model.helper';
+import {IXosModelHelpersService} from '../helpers/model.helpers';
 
 export interface  IModelStoreService {
   query(model: string): Observable<any>;