Skip to content

Commit

Permalink
feat: add Edrs controller (#215)
Browse files Browse the repository at this point in the history
  • Loading branch information
ndr-brt authored May 2, 2024
1 parent a519d14 commit 7822f78
Show file tree
Hide file tree
Showing 11 changed files with 3,795 additions and 429 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ developers by providing an HTTP Client which is thoroughly tested and fully type
## Compatibility matrix
|Client |EDC |
|--------|-------|
|0.4.0 |0.6.x |
|0.4.x |0.6.x |
|0.3.0 |0.5.0 |
|0.2.1 |0.4.1 |
|0.2.0 |0.2.0 |
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
"jest": "^28.1.3",
"lint-staged": "^15.0.2",
"prettier": "^3.0.1",
"testcontainers": "^10.9.0",
"ts-jest": "^28.0.7",
"ts-node": "^10.9.1",
"typescript": "^5.1.6"
Expand Down
61 changes: 61 additions & 0 deletions src/controllers/management-controllers/edr-controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import { EdcConnectorClientContext } from "../../context";
import {
expand,
expandArray,
QuerySpec,
JsonLdObject,
JSON_LD_DEFAULT_CONTEXT,
Edr
} from "../../entities";
import { Inner } from "../../inner";

export class EdrController {
#inner: Inner;
#context?: EdcConnectorClientContext;

constructor(inner: Inner, context?: EdcConnectorClientContext) {
this.#inner = inner;
this.#context = context;
}

async request(query: QuerySpec = {}, context?: EdcConnectorClientContext): Promise<Edr[]> {
const actualContext = context || this.#context!;

return this.#inner
.request(actualContext.management, {
path: "/v1/edrs/request",
method: "POST",
apiToken: actualContext.apiToken,
body:
Object.keys(query).length === 0
? null
: {
...query,
"@context": JSON_LD_DEFAULT_CONTEXT,
},
})
.then((body) => expandArray(body, () => new Edr()));
}

async delete(edrId: string, context?: EdcConnectorClientContext): Promise<void> {
const actualContext = context || this.#context!;

return this.#inner.request(actualContext.management, {
path: `/v1/edrs/${edrId}`,
method: "DELETE",
apiToken: actualContext.apiToken,
});
}

async dataAddress(edrId: string, context?: EdcConnectorClientContext): Promise<JsonLdObject> {
const actualContext = context || this.#context!;

return this.#inner.request(actualContext.management, {
path: `/v1/edrs/${edrId}/dataaddress`,
method: "GET",
apiToken: actualContext.apiToken,
})
.then((body) => expand(body, () => new JsonLdObject()));
}

}
3 changes: 2 additions & 1 deletion src/controllers/management-controllers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ export * from "./catalog-controller";
export * from "./contract-agreement-controller";
export * from "./contract-defintion-controller";
export * from "./contract-negotiation-controller";
export * from "./dataplane-controller";
export * from "./edr-controller";
export * from "./policy-defintion-controller";
export * from "./transfer-process-controller";
export * from "./dataplane-controller";
29 changes: 29 additions & 0 deletions src/entities/edr.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { JsonLdId } from "./jsonld";

export class Edr extends JsonLdId {

get transferProcessId(): string {
return this.mandatoryValue('edc', 'transferProcessId');
}

get agreementId(): string {
return this.mandatoryValue('edc', 'agreementId');
}

get contractNegotiationId(): string {
return this.mandatoryValue('edc', 'contractNegotiationId');
}

get assetId(): string {
return this.mandatoryValue('edc', 'assetId');
}

get providerId(): string {
return this.mandatoryValue('edc', 'providerId');
}

get createdAt(): string {
return this.mandatoryValue('edc', 'createdAt');
}

}
7 changes: 4 additions & 3 deletions src/entities/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,20 @@ import { CriterionInput } from "./criterion";

export * from "./addresses";
export * from "./asset";
export * from "./context";
export * from "./catalog";
export * from "./context";
export * from "./contract-agreement";
export * from "./contract-definition";
export * from "./contract-negotiation";
export * from "./criterion";
export * from "./data-address";
export * from "./dataplane";
export * from "./edr";
export * from "./health";
export * from "./id-response";
export * from "./jsonld";
export * from "./health";
export * from "./policy";
export * from "./policy-definition";
export * from "./policy";
export * from "./transfer-process";

export const EDC_NAMESPACE = "edc";
Expand Down
4 changes: 4 additions & 0 deletions src/entities/jsonld.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ export class JsonLdObject {
return this.arrayOf(() => new JsonLdObject(), prefix, name);
}

types(): string[] {
return this["@type"] as string[];
}

private getNamespaceUrl(prefix: string): string {
switch (prefix) {
case 'edc': return EDC_CONTEXT;
Expand Down
4 changes: 4 additions & 0 deletions src/facades/management.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
ContractDefinitionController,
ContractNegotiationController,
DataplaneController,
EdrController,
PolicyDefinitionController,
TransferProcessController,
} from "../controllers/management-controllers";
Expand Down Expand Up @@ -35,6 +36,9 @@ export class ManagementController {
get contractNegotiations() {
return new ContractNegotiationController(this.#inner, this.#context);
}
get edrs(): EdrController {
return new EdrController(this.#inner, this.#context);
}
get dataplanes() {
return new DataplaneController(this.#inner, this.#context);
}
Expand Down
48 changes: 48 additions & 0 deletions tests/controllers/management-tests/edrs.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { GenericContainer, StartedTestContainer } from "testcontainers";
import { EdcConnectorClient } from "../../../src";

describe("edrs", () => {
let startedContainer: StartedTestContainer;
let edcClient: EdcConnectorClient;

beforeAll(async () => {
startedContainer = await new GenericContainer("stoplight/prism:5.8.1")
.withCopyFilesToContainer([{ source: "tests/management-api.yaml", target: "/management-api.yml" }])
.withCommand(["mock", "-h", "0.0.0.0", "/management-api.yml"])
.withExposedPorts(4010)
.start();

edcClient = new EdcConnectorClient.Builder()
.managementUrl("http://localhost:" + startedContainer.getFirstMappedPort())
.build();
});

afterAll(async () => {
await startedContainer.stop();
});

it("should request edrs", async () => {
const result = await edcClient.management.edrs.request();

expect(result.length).toBeGreaterThan(0);
expect(result[0].transferProcessId).not.toBeNull();
expect(result[0].contractNegotiationId).not.toBeNull();
expect(result[0].assetId).not.toBeNull();
expect(result[0].providerId).not.toBeNull();
expect(result[0].agreementId).not.toBeNull();
})

it("should delete edrs", async () => {
await expect(edcClient.management.edrs.delete("edrId")).resolves
.not.toThrowError();
});

it("should get data address", async () => {
const dataAddress = await edcClient.management.edrs.dataAddress("edrId");

console.log(dataAddress)

expect(dataAddress.types().length).toBeGreaterThan(0);
});

});
Loading

0 comments on commit 7822f78

Please sign in to comment.