From 093a6ea4af9f9315049f8c66d52d618db3d13b32 Mon Sep 17 00:00:00 2001 From: Sebastian Gerske Date: Thu, 1 Nov 2018 12:47:48 +0100 Subject: [PATCH] added ddo handling --- src/aquarius/Aquarius.ts | 58 +++++++++++++--- src/aquarius/AquariusConnector.ts | 28 +++++--- src/aquarius/AquariusConnectorProvider.ts | 3 - src/aquarius/query/Query.ts | 3 + src/aquarius/query/SearchQuery.ts | 14 ++++ src/aquarius/query/Sort.ts | 3 + src/ddo/DDO.ts | 2 + test/aquarius/Aquarius.test.ts | 85 +++++++++++++++++++++-- test/mocks/AquariusConnector.mock.ts | 25 ++++--- 9 files changed, 182 insertions(+), 39 deletions(-) create mode 100644 src/aquarius/query/Query.ts create mode 100644 src/aquarius/query/SearchQuery.ts create mode 100644 src/aquarius/query/Sort.ts diff --git a/src/aquarius/Aquarius.ts b/src/aquarius/Aquarius.ts index 2e14c4b..331187c 100644 --- a/src/aquarius/Aquarius.ts +++ b/src/aquarius/Aquarius.ts @@ -1,6 +1,9 @@ +import {URL} from "url" +import DDO from "../ddo/DDO" import Config from "../models/Config" import Logger from "../utils/Logger" import AquariusConnectorProvider from "./AquariusConnectorProvider" +import SearchQuery from "./query/SearchQuery" export default class Aquarius { @@ -11,7 +14,7 @@ export default class Aquarius { this.url = config.aquariusUri } - public async getAccessUrl(accessToken: any, payload: any): Promise { + public async getAccessUrl(accessToken: any, payload: any): Promise { const accessUrl = await AquariusConnectorProvider.getConnector().post( `${accessToken.service_endpoint}/${accessToken.resource_id}`, @@ -33,11 +36,10 @@ export default class Aquarius { return accessUrl } - public async queryMetadata(query): Promise { + public async queryMetadata(query: SearchQuery): Promise { - const result = await AquariusConnectorProvider.getConnector().post( - this.url + "/api/v1/aquarius/assets/ddo/query", - JSON.stringify(query)) + const result = await AquariusConnectorProvider.getConnector() + .post(this.url + "/api/v1/aquarius/assets/ddo/query", JSON.stringify(query)) .then((response: any) => { if (response.ok) { return response.json() @@ -51,11 +53,15 @@ export default class Aquarius { return result } - public async queryMetadataByText(query): Promise { + public async queryMetadataByText(query: SearchQuery): Promise { - const result = await AquariusConnectorProvider.getConnector().get( - this.url + "/api/v1/aquarius/assets/ddo/query", - JSON.stringify(query)) + const fullUrl = new URL(this.url + "/api/v1/aquarius/assets/ddo/query") + fullUrl.searchParams.append("text", query.text) + fullUrl.searchParams.append("sort", JSON.stringify(query.sort)) + fullUrl.searchParams.append("offset", query.offset.toString()) + fullUrl.searchParams.append("page", query.page.toString()) + const result = await AquariusConnectorProvider.getConnector() + .get(fullUrl) .then((response: any) => { if (response.ok) { return response.json() @@ -68,4 +74,38 @@ export default class Aquarius { return result } + + public async storeDDO(ddo: DDO): Promise { + const fullUrl = this.url + `/api/v1/aquarius/assets/ddo` + const result = await AquariusConnectorProvider.getConnector() + .post(fullUrl, DDO.serialize(ddo)) + .then((response: any) => { + if (response.ok) { + return response.json() + } + Logger.log("Failed:", response.status, response.statusText) + }) + .catch((error) => { + Logger.error("Error fetching querying metadata: ", error) + }) + + return result + } + + public async retrieveDDO(did: string): Promise { + const fullUrl = this.url + `/api/v1/aquarius/assets/ddo/${did}` + const result = await AquariusConnectorProvider.getConnector() + .get(fullUrl) + .then((response: any) => { + if (response.ok) { + return response.json() + } + Logger.log("Failed:", response.status, response.statusText) + }) + .catch((error) => { + Logger.error("Error fetching querying metadata: ", error) + }) + + return result + } } diff --git a/src/aquarius/AquariusConnector.ts b/src/aquarius/AquariusConnector.ts index 243bd23..d4e0ad3 100644 --- a/src/aquarius/AquariusConnector.ts +++ b/src/aquarius/AquariusConnector.ts @@ -1,10 +1,9 @@ import fetch from "node-fetch" -import { URL } from "url" export default class AquariusConnector { - public post(url, payload) { - return fetch(url, { + public async post(url, payload): Promise { + return this.fetch(url, { method: "POST", body: payload, headers: { @@ -13,17 +12,26 @@ export default class AquariusConnector { }) } - public get(url, payload) { - const fullUrl = new URL(url) - for (const key of Object.keys(payload)) { - fullUrl.searchParams.append(key, payload[key]) - } - return fetch(fullUrl, { + public async get(url): Promise { + return this.fetch(url, { method: "GET", - body: null, headers: { "Content-type": "application/json", }, }) } + + public async put(url, payload): Promise { + return this.fetch(url, { + method: "PUT", + body: payload, + headers: { + "Content-type": "application/json", + }, + }) + } + + private async fetch(url, opts): Promise { + return fetch(url, opts) + } } diff --git a/src/aquarius/AquariusConnectorProvider.ts b/src/aquarius/AquariusConnectorProvider.ts index 997e659..5c2bbd5 100644 --- a/src/aquarius/AquariusConnectorProvider.ts +++ b/src/aquarius/AquariusConnectorProvider.ts @@ -1,12 +1,9 @@ -import Logger from "../utils/Logger" import AquariusConnector from "./AquariusConnector" export default class AquariusConnectorProvider { public static setConnector(connector: AquariusConnector) { - Logger.log("setting", typeof connector.constructor.name) - AquariusConnectorProvider.connector = connector } diff --git a/src/aquarius/query/Query.ts b/src/aquarius/query/Query.ts new file mode 100644 index 0000000..2bf1b66 --- /dev/null +++ b/src/aquarius/query/Query.ts @@ -0,0 +1,3 @@ +export default class Query { + public value: number = 1 +} \ No newline at end of file diff --git a/src/aquarius/query/SearchQuery.ts b/src/aquarius/query/SearchQuery.ts new file mode 100644 index 0000000..16eea31 --- /dev/null +++ b/src/aquarius/query/SearchQuery.ts @@ -0,0 +1,14 @@ +import Query from "./Query" +import Sort from "./Sort" + +export default class SearchQuery { + public offset: number = 100 + public page: number = 0 + public query: Query = { + value: 1, + } as Query + public sort: Sort = { + value: 1, + } as Sort + public text: string = "Office" +} diff --git a/src/aquarius/query/Sort.ts b/src/aquarius/query/Sort.ts new file mode 100644 index 0000000..3aa3fd9 --- /dev/null +++ b/src/aquarius/query/Sort.ts @@ -0,0 +1,3 @@ +export default class Sort { + public value: number = 1 +} diff --git a/src/ddo/DDO.ts b/src/ddo/DDO.ts index 1cca7f8..4682f28 100644 --- a/src/ddo/DDO.ts +++ b/src/ddo/DDO.ts @@ -24,10 +24,12 @@ export default class DDO { private assa: string public constructor(ddo?: { + id?: string, publicKey?: PublicKey[], authentication?: Authentication[], service?: Service[], }) { + this.id = ddo ? ddo.id ? ddo.id : null : null this.publicKey = ddo ? ddo.publicKey ? ddo.publicKey : [] : [] this.authentication = ddo ? ddo.authentication ? ddo.authentication : [] : [] this.service = ddo ? ddo.service ? ddo.service : [] : [] diff --git a/test/aquarius/Aquarius.test.ts b/test/aquarius/Aquarius.test.ts index d5ed2aa..421a081 100644 --- a/test/aquarius/Aquarius.test.ts +++ b/test/aquarius/Aquarius.test.ts @@ -1,21 +1,20 @@ import * as assert from "assert" import Aquarius from "../../src/aquarius/Aquarius" import AquariusConnectorProvider from "../../src/aquarius/AquariusConnectorProvider" +import SearchQuery from "../../src/aquarius/query/SearchQuery" +import DDO from "../../src/ddo/DDO" +import IdGenerator from "../../src/ocean/IdGenerator" import config from "../config" import AquariusConnectorMock from "../mocks/AquariusConnector.mock" +// import * as jsonDDO from "../testdata/ddo.json" describe("Aquarius", () => { - before(() => { - AquariusConnectorProvider.setConnector(new AquariusConnectorMock()) - }) - + const aquarius: Aquarius = new Aquarius(config) describe("#queryMetadata()", () => { it("should query metadata", async () => { - const aquarius: Aquarius = new Aquarius(config) - const query = { offset: 100, page: 0, @@ -26,12 +25,84 @@ describe("Aquarius", () => { value: 1, }, text: "Office", - } + } as SearchQuery + + // @ts-ignore + AquariusConnectorProvider.setConnector(new AquariusConnectorMock()) const result: any[] = await aquarius.queryMetadata(query) assert(result) + assert(result.length !== null) }) }) + describe("#queryMetadataByText()", () => { + + it("should query metadata by text", async () => { + + const query = { + offset: 100, + page: 0, + query: { + value: 1, + }, + sort: { + value: 1, + }, + text: "Office", + } as SearchQuery + + // @ts-ignore + AquariusConnectorProvider.setConnector(new AquariusConnectorMock()) + + const result: any[] = await aquarius.queryMetadataByText(query) + assert(result) + assert(result.length !== null) + }) + + }) + + describe("#storeDDO()", () => { + + it("should store a ddo", async () => { + + const did: string = `did:op:${IdGenerator.generateId()}` + const ddo: DDO = new DDO({ + id: did, + }) + + // @ts-ignore + AquariusConnectorProvider.setConnector(new AquariusConnectorMock(ddo)) + + const result: DDO = await aquarius.storeDDO(ddo) + assert(result) + assert(result.id === ddo.id) + }) + }) + + describe("#retrieveDDO()", () => { + + it("should store a ddo", async () => { + + const did: string = `did:op:${IdGenerator.generateId()}` + const ddo: DDO = new DDO({ + id: did, + }) + + // @ts-ignore + AquariusConnectorProvider.setConnector(new AquariusConnectorMock(ddo)) + + const storageResult: DDO = await aquarius.storeDDO(ddo) + assert(storageResult) + + assert(storageResult.id === did) + + const restrieveResult: DDO = await aquarius.retrieveDDO(did) + assert(restrieveResult) + + assert(restrieveResult.id === did) + assert(restrieveResult.id === storageResult.id) + }) + }) }) diff --git a/test/mocks/AquariusConnector.mock.ts b/test/mocks/AquariusConnector.mock.ts index 5e0db38..3326ece 100644 --- a/test/mocks/AquariusConnector.mock.ts +++ b/test/mocks/AquariusConnector.mock.ts @@ -1,17 +1,22 @@ import AquariusConnector from "../../src/aquarius/AquariusConnector" +// @ts-ignore export default class AquariusConnectorMock extends AquariusConnector { - public async post(url: string, payload: any) { + constructor(private returnData: any) { + super() + } - return { - ok: true, - json: () => { - return [] - }, - text: () => { - return "" - }, - } + // @ts-ignore + private async fetch(url, opts): Promise { + + return new Promise((resolve, reject) => { + resolve({ + ok: true, + json: () => { + return this.returnData ? this.returnData : [] + }, + }) + }) } }