diff --git a/integration/ocean/ConsumeAsset.test.ts b/integration/ocean/ConsumeAsset.test.ts index 7812fec..00fef52 100644 --- a/integration/ocean/ConsumeAsset.test.ts +++ b/integration/ocean/ConsumeAsset.test.ts @@ -1,5 +1,4 @@ import { assert } from "chai" -import * as Web3 from "web3" import * as fs from "fs" import { config } from "../config" @@ -94,7 +93,7 @@ describe("Consume Asset", () => { it("should consume and store the assets", async () => { const accessService = ddo.findServiceByType("Access") - const folder = "/tmp/ocean/squid-js" + const folder = "/tmp/ocean/squid-js-1" const path = await ocean.assets.consume( serviceAgreementSignatureResult.agreementId, ddo.id, @@ -114,4 +113,29 @@ describe("Consume Asset", () => { assert.deepEqual(files, ["file-0", "file-1"], "Stored files are not correct.") // assert.deepEqual(files, ["README.md", "package.json"], "Stored files are not correct.") }) + + it("should consume and store one assets", async () => { + const accessService = ddo.findServiceByType("Access") + + const folder = "/tmp/ocean/squid-js-2" + const path = await ocean.assets.consume( + serviceAgreementSignatureResult.agreementId, + ddo.id, + accessService.serviceDefinitionId, + consumer, + folder, + 1, + ) + + assert.include(path, folder, "The storage path is not correct.") + + const files = await new Promise((resolve) => { + fs.readdir(path, (err, fileList) => { + resolve(fileList) + }) + }) + + assert.deepEqual(files, ["file-1"], "Stored files are not correct.") + // assert.deepEqual(files, ["package.json"], "Stored files are not correct.") + }) }) diff --git a/scripts/unlock-accounts.sh b/scripts/unlock-accounts.sh index cd52e33..85d3a6a 100755 --- a/scripts/unlock-accounts.sh +++ b/scripts/unlock-accounts.sh @@ -1,3 +1,5 @@ +#!/bin/sh + count=0 echo "Starting..." while sleep 0.1; diff --git a/src/brizo/Brizo.ts b/src/brizo/Brizo.ts index ef964fb..e894c91 100644 --- a/src/brizo/Brizo.ts +++ b/src/brizo/Brizo.ts @@ -72,12 +72,14 @@ export class Brizo extends Instantiable { account: Account, files: File[], destination: string, + index: number = -1, ): Promise { const agreementIdSignature = await this.ocean.utils.signature.signText(agreementId, account.getId()) const filesPromises = files - .map(async ({}, i) => { + .filter(({}, i) => index === -1 || i === index) + .map(async ({index}) => { let consumeUrl = serviceEndpoint - consumeUrl += `?index=${i}` + consumeUrl += `?index=${index}` consumeUrl += `&serviceAgreementId=${agreementId}` consumeUrl += `&consumerAddress=${account.getId()}` consumeUrl += `&signature=${agreementIdSignature}` @@ -85,7 +87,7 @@ export class Brizo extends Instantiable { try { await this.downloadFile( consumeUrl, - `file-${i}`, + `file-${index}`, destination, ) } catch (e) { diff --git a/src/ocean/OceanAssets.ts b/src/ocean/OceanAssets.ts index 5d2363e..a642166 100644 --- a/src/ocean/OceanAssets.ts +++ b/src/ocean/OceanAssets.ts @@ -147,14 +147,16 @@ export class OceanAssets extends Instantiable { } // tslint:disable-next-line - public async consume(agreementId: string, did: string, serviceDefinitionId: string, consumerAccount: Account, resultPath: string): Promise - public async consume(agreementId: string, did: string, serviceDefinitionId: string, consumerAccount: Account): Promise + public async consume(agreementId: string, did: string, serviceDefinitionId: string, consumerAccount: Account, resultPath: string, index?: number): Promise + // tslint:disable-next-line + public async consume(agreementId: string, did: string, serviceDefinitionId: string, consumerAccount: Account, resultPath?: undefined | null, index?: number): Promise public async consume( agreementId: string, did: string, serviceDefinitionId: string, consumerAccount: Account, resultPath?: string, + index: number = -1, ): Promise { const ddo = await this.resolve(did) @@ -179,6 +181,7 @@ export class OceanAssets extends Instantiable { consumerAccount, files, resultPath, + index, ) this.logger.log("Files consumed")