From 60c406bcea73403df8c1899db382e73974969be8 Mon Sep 17 00:00:00 2001 From: Ahmed Ali Date: Wed, 8 Jul 2020 12:54:10 +0200 Subject: [PATCH] add provider compute mock --- package.json | 2 +- test/integration/__mocks__/Provider.Mock.ts | 116 ++++++++ test/unit/__fixtures__/ddo.json | 290 +------------------- 3 files changed, 122 insertions(+), 286 deletions(-) create mode 100644 test/integration/__mocks__/Provider.Mock.ts diff --git a/package.json b/package.json index e33233df..784b4b52 100644 --- a/package.json +++ b/package.json @@ -19,7 +19,7 @@ "changelog": "auto-changelog -p", "prepublishOnly": "npm run build", "test:unit": "mocha --config=test/unit/.mocharc.json --node-env=test --exit test/unit/**/*.ts", - "test:integration": "mocha --config=test/integration/.mocharc.json --node-env=test --exit test/integration/**/*.ts", + "test:integration": "mocha --config=test/integration/.mocharc.json --node-env=test --exit test/integration/*.ts", "test:cover": "nyc --report-dir coverage/unit npm run test:unit" }, "repository": { diff --git a/test/integration/__mocks__/Provider.Mock.ts b/test/integration/__mocks__/Provider.Mock.ts new file mode 100644 index 00000000..a3d6b510 --- /dev/null +++ b/test/integration/__mocks__/Provider.Mock.ts @@ -0,0 +1,116 @@ +import Account from '../../../src/ocean/Account' +import { Provider } from '../../../src/provider/Provider' +import { noZeroX } from '../../../src/utils' +import { ComputeJob } from '../../../src/ocean/interfaces/ComputeJob' +import { Output } from '../../../src/ocean/interfaces/ComputeOutput' +import { MetadataAlgorithm } from '../../../src/ddo/interfaces/MetadataAlgorithm' + +export default class ProviderMock extends Provider { + public async compute( + method: string, + did: string, + consumerAccount: Account, + algorithmDid?: string, + algorithmMeta?: MetadataAlgorithm, + jobId?: string, + output?: Output, + txId?: string, + serviceIndex?: string, + serviceType?: string, + tokenAddress?: string + ): Promise { + const address = consumerAccount.getId() + + let signatureMessage = address + signatureMessage += jobId || '' + signatureMessage += (did && `${noZeroX(did)}`) || '' + const signature = await this.createHashSignature( + consumerAccount, + signatureMessage + ) + + // construct Brizo URL + let url = this.getComputeEndpoint() + url += `?signature=${signature}` + url += `&documentId=${noZeroX(did)}` + url += (output && `&output=${JSON.stringify(output)}`) || '' + url += (algorithmDid && `&algorithmDid=${algorithmDid}`) || '' + url += + (algorithmMeta && + `&algorithmMeta=${encodeURIComponent(JSON.stringify(algorithmMeta))}`) || + '' + url += (jobId && `&jobId=${jobId}`) || '' + url += `&consumerAddress=${address}` + url += `&transferTxId=${txId}` || '' + url += `&serviceId=${serviceIndex}` || '' + url += `&serviceType=${serviceType}` || '' + url += `&dataToken=${tokenAddress}` || '' + url += `&consumerAddress=${consumerAccount.getId()}` || '' + + // switch fetch method + let fetch + + switch (method) { + case 'post': // start + fetch = Promise.resolve({ + jobId: '0x1111:001', + status: 1, + statusText: 'Job started' + }) + break + case 'put': // stop + fetch = Promise.resolve([ + { + status: 7, + statusText: 'Job stopped' + } + ]) + break + case 'delete': + fetch = Promise.resolve([ + { + status: 8, + statusText: 'Job deleted successfully' + } + ]) + break + default: + // status + fetch = Promise.resolve([ + { + owner: '0x1111', + documentId: 'did:op:2222', + jobId: '3333', + dateCreated: '2020-10-01T01:00:00Z', + dateFinished: '2020-10-01T01:00:00Z', + status: 5, + statusText: 'Job finished', + algorithmLogUrl: 'http://example.net/logs/algo.log', + resultsUrls: [ + 'http://example.net/logs/output/0', + 'http://example.net/logs/output/1' + ], + resultsDid: + 'did:op:87bdaabb33354d2eb014af5091c604fb4b0f67dc6cca4d18a96547bffdc27bcf' + }, + { + owner: '0x1111', + documentId: 'did:op:2222', + jobId: '3334', + dateCreated: '2020-10-01T01:00:00Z', + dateFinished: '2020-10-01T01:00:00Z', + status: 5, + statusText: 'Job finished', + algorithmLogUrl: 'http://example.net/logs2/algo.log', + resultsUrls: [ + 'http://example.net/logs2/output/0', + 'http://example.net/logs2/output/1' + ], + resultsDid: '' + } + ]) + break + } + return await fetch + } +} diff --git a/test/unit/__fixtures__/ddo.json b/test/unit/__fixtures__/ddo.json index 73ddd2f6..fe6233a2 100644 --- a/test/unit/__fixtures__/ddo.json +++ b/test/unit/__fixtures__/ddo.json @@ -41,166 +41,26 @@ { "type": "access", "index": 0, - "serviceEndpoint": "http://mybrizo.org/api/v1/brizo/services/consume?pubKey=${pubKey}&serviceId={serviceId}&url={url}", + "serviceEndpoint": "http://myprovider.org/api/v1/services/consume?pubKey=${pubKey}&serviceId={serviceId}&url={url}", "templateId": "044852b2a670ade5407e78fb2863c51000000000000000000000000000000000", "attributes": { "main": { - "name": "dataAssetAccessServiceAgreement", + "name": "dataAssetAccess", "creator": "0x36A7f3383A63279cDaF4DfC0F3ABc07d90252C6b", "datePublished": "2019-11-15T14:11:23Z", "price": "", "timeout": 36000 - }, - "serviceAgreementTemplate": { - "contractName": "EscrowAccessSecretStoreTemplate", - "events": [ - { - "name": "AgreementCreated", - "actorType": "consumer", - "handler": { - "moduleName": "escrowAccessSecretStoreTemplate", - "functionName": "fulfillLockRewardCondition", - "version": "0.1" - } - } - ], - "fulfillmentOrder": [ - "lockReward.fulfill", - "accessSecretStore.fulfill", - "escrowReward.fulfill" - ], - "conditionDependency": { - "lockReward": [], - "accessSecretStore": [], - "escrowReward": ["lockReward", "accessSecretStore"] - }, - "conditions": [ - { - "name": "lockReward", - "timelock": 0, - "timeout": 0, - "contractName": "LockRewardCondition", - "functionName": "fulfill", - "parameters": [ - { - "name": "_rewardAddress", - "type": "address", - "value": "0x36A7f3383A63279cDaF4DfC0F3ABc07d90252C6b" - }, - { - "name": "_amount", - "type": "uint256", - "value": "0" - } - ], - "events": [ - { - "name": "Fulfilled", - "actorType": "publisher", - "handler": { - "moduleName": "lockRewardCondition", - "functionName": "fulfillAccessSecretStoreCondition", - "version": "0.1" - } - } - ] - }, - { - "name": "accessSecretStore", - "timelock": 0, - "timeout": 0, - "contractName": "AccessSecretStoreCondition", - "functionName": "fulfill", - "parameters": [ - { - "name": "_documentId", - "type": "bytes32", - "value": "c678e7e5963d4fdc99afea49ac221d4d4177790f30204417823319d4d35f851f" - }, - { - "name": "_grantee", - "type": "address", - "value": "" - } - ], - "events": [ - { - "name": "Fulfilled", - "actorType": "publisher", - "handler": { - "moduleName": "accessSecretStore", - "functionName": "fulfillEscrowRewardCondition", - "version": "0.1" - } - }, - { - "name": "TimedOut", - "actorType": "consumer", - "handler": { - "moduleName": "accessSecretStore", - "functionName": "refundReward", - "version": "0.1" - } - } - ] - }, - { - "name": "escrowReward", - "timelock": 0, - "timeout": 0, - "contractName": "EscrowReward", - "functionName": "fulfill", - "parameters": [ - { - "name": "_amount", - "type": "uint256", - "value": "0" - }, - { - "name": "_receiver", - "type": "address", - "value": "" - }, - { - "name": "_sender", - "type": "address", - "value": "" - }, - { - "name": "_lockCondition", - "type": "bytes32", - "value": "" - }, - { - "name": "_releaseCondition", - "type": "bytes32", - "value": "" - } - ], - "events": [ - { - "name": "Fulfilled", - "actorType": "publisher", - "handler": { - "moduleName": "escrowRewardCondition", - "functionName": "verifyRewardTokens", - "version": "0.1" - } - } - ] - } - ] } } }, { "type": "compute", "index": 1, - "serviceEndpoint": "http://localhost:8030/api/v1/brizo/services/compute", + "serviceEndpoint": "http://localhost:8030/api/v1/services/compute", "templateId": "", "attributes": { "main": { - "name": "dataAssetComputingServiceAgreement", + "name": "dataAssetComputing", "creator": "0x36A7f3383A63279cDaF4DfC0F3ABc07d90252C6b", "datePublished": "2019-04-09T19:02:11Z", "price": "10", @@ -250,147 +110,7 @@ } } }, - "additionalInformation": {}, - "serviceAgreementTemplate": { - "contractName": "EscrowComputeExecutionTemplate", - "events": [ - { - "name": "AgreementActorAdded", - "actorType": "provider", - "handler": { - "moduleName": "", - "functionName": "fulfillLockRewardCondition", - "version": "0.1" - } - } - ], - "fulfillmentOrder": [ - "lockReward.fulfill", - "computeExecution.fulfill", - "escrowReward.fulfill" - ], - "conditionDependency": { - "lockReward": [], - "computeExecution": [], - "releaseReward": ["lockReward", "computeExecution"] - }, - "conditions": [ - { - "name": "lockReward", - "timelock": 0, - "timeout": 0, - "contractName": "LockRewardCondition", - "functionName": "fulfill", - "parameters": [ - { - "name": "_rewardAddress", - "type": "address", - "value": "" - }, - { - "name": "_amount", - "type": "uint256", - "value": "" - } - ], - "events": [ - { - "name": "Fulfilled", - "actorType": "provider", - "handler": { - "moduleName": "lockRewardExecutionCondition", - "functionName": "fulfillComputeExecutionCondition", - "version": "0.1" - } - } - ] - }, - { - "name": "computeExecution", - "timelock": 0, - "timeout": 0, - "contractName": "ComputeExecutionCondition", - "functionName": "fulfill", - "parameters": [ - { - "name": "_documentId", - "type": "bytes32", - "value": "" - }, - { - "name": "_grantee", - "type": "address", - "value": "" - } - ], - "events": [ - { - "name": "Fulfilled", - "actorType": "provider", - "handler": { - "moduleName": "accessSecretStore", - "functionName": "fulfillEscrowRewardCondition", - "version": "0.1" - } - }, - { - "name": "TimedOut", - "actorType": "consumer", - "handler": { - "moduleName": "accessSecretStore", - "functionName": "refundReward", - "version": "0.1" - } - } - ] - }, - { - "name": "escrowReward", - "timelock": 0, - "timeout": 0, - "contractName": "EscrowReward", - "functionName": "fulfill", - "parameters": [ - { - "name": "_amount", - "type": "uint256", - "value": "" - }, - { - "name": "_receiver", - "type": "address", - "value": "" - }, - { - "name": "_sender", - "type": "address", - "value": "" - }, - { - "name": "_lockCondition", - "type": "bytes32", - "value": "" - }, - { - "name": "_releaseCondition", - "type": "bytes32", - "value": "" - } - ], - "events": [ - { - "name": "Fulfilled", - "actorType": "provider", - "handler": { - "moduleName": "escrowRewardCondition", - "functionName": "verifyRewardTokens", - "version": "0.1" - } - } - ] - } - ] - } + "additionalInformation": {} } }, {