Project WS Handler refactor

This commit is contained in:
ziajka 2018-03-19 15:01:18 +01:00
parent 2cbe216f76
commit 8432fb9d24
5 changed files with 208 additions and 9 deletions

View File

@ -0,0 +1,33 @@
import {NodesDataSource} from "./nodes-datasource";
import {Node} from "../models/node.model";
describe('NodesDataSource', () => {
let dataSource: NodesDataSource;
let data: Node[];
beforeEach(() => {
dataSource = new NodesDataSource();
dataSource.connect().subscribe((nodes: Node[]) => {
data = nodes;
});
});
describe('Node can be updated', () => {
beforeEach(() => {
const node = new Node();
node.node_id = "1";
node.name = "Node 1";
dataSource.add(node);
node.name = "Node 2";
dataSource.update(node);
});
it('name should change', () => {
expect(data[0].node_id).toEqual("1");
expect(data[0].name).toEqual("Node 2");
});
});
});

View File

@ -0,0 +1,11 @@
import {Node} from "../models/node.model";
import {DataSource} from "./datasource";
import {Injectable} from "@angular/core";
@Injectable()
export class NodesDataSource extends DataSource<Node> {
protected findIndex(node: Node) {
return this.data.findIndex((n: Node) => n.node_id === node.node_id);
}
}

View File

@ -1,9 +0,0 @@
import {Node} from "../../cartography/shared/models/node.model";
export class Database<T> {
}
export class NodeDatabase extends Database<Node> {
}

View File

@ -0,0 +1,123 @@
import {ProjectWebServiceHandler, WebServiceMessage} from "./project-web-service-handler";
import {Subject} from "rxjs/Subject";
import {inject, TestBed} from "@angular/core/testing";
import {NodesDataSource} from "../../cartography/shared/datasources/nodes-datasource";
import {LinksDataSource} from "../../cartography/shared/datasources/links-datasource";
import {Node} from "../../cartography/shared/models/node.model";
import {Link} from "../../cartography/shared/models/link.model";
describe('ProjectWebServiceHandler', () => {
let ws: Subject<WebServiceMessage>;
beforeEach(() => {
TestBed.configureTestingModule({
providers: [ProjectWebServiceHandler, NodesDataSource, LinksDataSource]
});
ws = new Subject<WebServiceMessage>();
});
it('should be created', inject([ProjectWebServiceHandler], (service: ProjectWebServiceHandler) => {
expect(service).toBeTruthy();
}));
it('node should be added', inject([ProjectWebServiceHandler, NodesDataSource],
(service: ProjectWebServiceHandler, nodesDataSource: NodesDataSource) => {
spyOn(nodesDataSource, 'add');
service.connect(ws);
const message = new WebServiceMessage();
message.action = "node.created";
message.event = new Node();
ws.next(message);
expect(service).toBeTruthy();
expect(nodesDataSource.add).toHaveBeenCalledWith(message.event);
}));
it('node should be updated', inject([ProjectWebServiceHandler, NodesDataSource],
(service: ProjectWebServiceHandler, nodesDataSource: NodesDataSource) => {
spyOn(nodesDataSource, 'update');
service.connect(ws);
const message = new WebServiceMessage();
message.action = "node.updated";
message.event = new Node();
ws.next(message);
expect(service).toBeTruthy();
expect(nodesDataSource.update).toHaveBeenCalledWith(message.event);
}));
it('node should be removed', inject([ProjectWebServiceHandler, NodesDataSource],
(service: ProjectWebServiceHandler, nodesDataSource: NodesDataSource) => {
spyOn(nodesDataSource, 'remove');
service.connect(ws);
const message = new WebServiceMessage();
message.action = "node.deleted";
message.event = new Node();
ws.next(message);
expect(service).toBeTruthy();
expect(nodesDataSource.remove).toHaveBeenCalledWith(message.event);
}));
it('link should be added', inject([ProjectWebServiceHandler, LinksDataSource],
(service: ProjectWebServiceHandler, linksDataSource: LinksDataSource) => {
spyOn(linksDataSource, 'add');
service.connect(ws);
const message = new WebServiceMessage();
message.action = "link.created";
message.event = new Link();
ws.next(message);
expect(service).toBeTruthy();
expect(linksDataSource.add).toHaveBeenCalledWith(message.event);
}));
it('link should be updated', inject([ProjectWebServiceHandler, LinksDataSource],
(service: ProjectWebServiceHandler, linksDataSource: LinksDataSource) => {
spyOn(linksDataSource, 'update');
service.connect(ws);
const message = new WebServiceMessage();
message.action = "link.updated";
message.event = new Link();
ws.next(message);
expect(service).toBeTruthy();
expect(linksDataSource.update).toHaveBeenCalledWith(message.event);
}));
it('link should be removed', inject([ProjectWebServiceHandler, LinksDataSource],
(service: ProjectWebServiceHandler, linksDataSource: LinksDataSource) => {
spyOn(linksDataSource, 'remove');
service.connect(ws);
const message = new WebServiceMessage();
message.action = "link.deleted";
message.event = new Link();
ws.next(message);
expect(service).toBeTruthy();
expect(linksDataSource.remove).toHaveBeenCalledWith(message.event);
}));
});

View File

@ -0,0 +1,41 @@
import {Injectable} from "@angular/core";
import {NodesDataSource} from "../../cartography/shared/datasources/nodes-datasource";
import {LinksDataSource} from "../../cartography/shared/datasources/links-datasource";
import {Subject} from "rxjs/Subject";
import {Link} from "../../cartography/shared/models/link.model";
import {Node} from "../../cartography/shared/models/node.model";
export class WebServiceMessage {
action: string;
event: Node | Link;
}
@Injectable()
export class ProjectWebServiceHandler {
constructor(private nodesDataSource: NodesDataSource,
private linksDataSource: LinksDataSource) {}
public connect(ws: Subject<WebServiceMessage>) {
ws.subscribe((message: WebServiceMessage) => {
if (message.action === 'node.updated') {
this.nodesDataSource.update(message.event as Node);
}
if (message.action === 'node.created') {
this.nodesDataSource.add(message.event as Node);
}
if (message.action === 'node.deleted') {
this.nodesDataSource.remove(message.event as Node);
}
if (message.action === 'link.created') {
this.linksDataSource.add(message.event as Link);
}
if (message.action === 'link.updated') {
this.linksDataSource.update(message.event as Link);
}
if (message.action === 'link.deleted') {
this.linksDataSource.remove(message.event as Link);
}
});
}
}