commit 8d8f39908faf264873aa60a22b273b566d96f200 Author: ?ngel ?lvarez Date: Thu Nov 2 12:42:38 2017 +0100 initial commit diff --git a/encode-json-read.spec.ts b/encode-json-read.spec.ts new file mode 100644 index 0000000..a2422f8 --- /dev/null +++ b/encode-json-read.spec.ts @@ -0,0 +1,41 @@ +import {} from 'jasmine'; + +import { EncodeJSONRead } from './encode-json-read' + +let json_constructor = null; + +describe('Encode JSON read unit test', () => { + beforeEach(() => { + json_constructor = new EncodeJSONRead(); + }); + + it('should create the inventory page', () => { + let result = json_constructor.createDomain('id', '=', 2); + expect(typeof result).toBe('string'); + expect(result).toEqual("('id', '=', 2)"); + }); + it('should add a new node', () => { + let domain = "[" + json_constructor.createDomain('id', '=', 2) + "]"; + json_constructor.addNode('party.party', domain, + ["name", "address"]); + + expect(json_constructor.root['party.party']).toEqual( + ['[(\'id\', \'=\', 2)]', ["name", "address"], 0, '']); + + expect(typeof json_constructor.root['party.party']).toBe('object'); + + expect(json_constructor.root['party.party'].length).toEqual(4); + + expect(json_constructor.root['party.party'][1]).toEqual(["name", "address"]) + + expect(json_constructor.root['party.party'][2]).toEqual(0); + }) + it('should create a JSON', () => { + let domain = "[" + json_constructor.createDomain('id', '=', 2) + "]"; + json_constructor.addNode('party.party', domain, ["name", "address"]); + let result = json_constructor.createJson(); + + expect(typeof result).toBe('string'); + expect(result).toEqual("[{\"party.party\":[\"[(\'id\', \'=\', 2)]\",[\"name\",\"address\"],0,\"\"]}]") + }) +}); diff --git a/encode-json-read.ts b/encode-json-read.ts new file mode 100644 index 0000000..a9829fa --- /dev/null +++ b/encode-json-read.ts @@ -0,0 +1,58 @@ +/** + * Encodes a JSON + * EncodeJSONRead will encode a JSON to make a search request + * + * DO NOT MODIFIY THIS FILE, EXTEND THE CLASS INSTEAD + */ + +import { RootSend, Send } from './json-interface'; + +export class EncodeJSONRead { + + root: Send; + root_send: RootSend; + + constructor(){ + console.log("Initialaizing JSON constructor"); + this.root_send = { + root: [] + }; + } + /** + * Adds a new node to the JSON object + * @param {string} method Name of the target method + * @param {string} domain Domain of the method + * @param {Array} fields Fields to get + * @param {number} offset Sets where to start gathering data after getting it, the default value no offset + * @param {any} limit Limits the number of data returned, default None + */ + addNode(method: string, domain: any, fields: Array, + offset = 0, limit:any = ''){ + console.log("Adding node for method, domain, fields", + method, domain, fields) + this.root = {} + this.root[method] = [domain, fields, offset, limit]; + this.root_send.root.push(this.root); + } + /** + * Creates a new domain + * @param {string} field Name of the field + * @param {string} operator Operator: =, >, <, in, like... + * @param {any} value Value to compare + * @param {string} related Used for reference fields search + * @returns {string} String contaning the domain withour brackets + */ + createDomain(field: string, operator: string, value: any, related?: string){ + if (related) + return [field, operator, value, related] + return [field, operator, value] + } + /** + * Creates the JSON object and returns it + * @returns {JSON} JSON created with the nodes + */ + createJson(){ + console.log("Generating JSON") + return JSON.stringify(this.root_send.root) + } +} diff --git a/encode-json-write.ts b/encode-json-write.ts new file mode 100644 index 0000000..9a1630c --- /dev/null +++ b/encode-json-write.ts @@ -0,0 +1,42 @@ +/** + * Encodes a JSON + * EncodeJSONWrite will encode a JSON to make a save or write request + * + * DO NOT MODIFIY THIS FILE, EXTEND THE CLASS INSTEAD + */ + +import { Write } from './json-interface'; + +export class EncodeJSONWrite { + + write: Write; + constructor(){ + console.log("Initialaizing JSON constructor"); + this.write = {}; + } + + /** + * Adds a node to the JSON object + * @param {string} method Name of the target method + * @param {Object} values Values to create or update + */ + public addNode(method: string, values: any ){ + console.log("Adding node for method, id, values", + method, values); + + if (this.write[method] !== undefined) + this.write[method].push(values); + else + this.write[method] = [values] + console.log("Current state of the node", this.write) + } + /** + * Creates the JSON object and returns it + * @returns JSON object + */ + public createJSON(){ + console.log("Generating JSON"); + console.log(JSON.stringify(this.write)) + return JSON.stringify(this.write); + } +} diff --git a/encode-json-wrtie.spec.ts b/encode-json-wrtie.spec.ts new file mode 100644 index 0000000..c7b87e8 --- /dev/null +++ b/encode-json-wrtie.spec.ts @@ -0,0 +1,40 @@ +import {} from 'jasmine'; + +import { EncodeJSONWrite } from './encode-json-write' + +let json_constructor = null; +let method = "party.party"; + +describe('Encode JSON read unit test', () => { + beforeEach(() => { + json_constructor = new EncodeJSONWrite(); + }); + + it('Should add a new node to write', () => { + let id = -1; + let val = { + name: 'Test write' + }; + let values = [id, val] + json_constructor.addNode(method, values); + expect(typeof json_constructor.write).toBe('object'); + }) + it('Should create a JSON', () => { + let id = -1; + let val = { + name: 'Test write' + }; + let values = [id, val] + json_constructor.addNode(method, values); + id = 1; + val = { + name: 'Test write w' + }; + values = [id, val] + json_constructor.addNode(method, values); + + let result = json_constructor.createJSON(); + expect(typeof result).toBe('string') + expect(result).toEqual('{"party.party":[[-1,{"name":"Test write"}],[1,{"name":"Test write w"}]]}') + }) +}); diff --git a/json-interface.ts b/json-interface.ts new file mode 100644 index 0000000..f0b4658 --- /dev/null +++ b/json-interface.ts @@ -0,0 +1,25 @@ +/** + * Creates the interfaces to create the JSON objects + * + * DO NOT MODIFY THIS FILE, EXTEND THE INTERFACES INSTEAD + */ + +/* + Storage of the JSON + */ +export interface RootSend { + root: Array; +} +/* + Temportary storage of a node + */ +export interface Send { + [method: string]: any[]; +} + +/* + Storage for the write JSON + */ +export interface Write { + [method:string]: any[]; +} \ No newline at end of file