|
| 1 | +/******************************************************************************** |
| 2 | + * Copyright (c) 2023 Contributors to the Eclipse Foundation |
| 3 | + * |
| 4 | + * See the NOTICE file(s) distributed with this work for additional |
| 5 | + * information regarding copyright ownership. |
| 6 | + * |
| 7 | + * This program and the accompanying materials are made available under the |
| 8 | + * terms of the Eclipse Public License v. 2.0 which is available at |
| 9 | + * http://www.eclipse.org/legal/epl-2.0, or the W3C Software Notice and |
| 10 | + * Document License (2015-05-13) which is available at |
| 11 | + * https://www.w3.org/Consortium/Legal/2015/copyright-software-and-document. |
| 12 | + * |
| 13 | + * SPDX-License-Identifier: EPL-2.0 OR W3C-20150513 |
| 14 | + ********************************************************************************/ |
| 15 | + |
| 16 | +import { Form, SecurityScheme } from "@node-wot/td-tools"; |
| 17 | +import { Subscription } from "rxjs/Subscription"; |
| 18 | +import { Content } from "../src/content"; |
| 19 | +import { createLoggers } from "../src/logger"; |
| 20 | +import { ProtocolClient, ProtocolClientFactory } from "../src/protocol-interfaces"; |
| 21 | +import Servient from "../src/servient"; |
| 22 | +import { Readable } from "stream"; |
| 23 | +import { expect } from "chai"; |
| 24 | + |
| 25 | +const { debug, error } = createLoggers("core", "DiscoveryTest"); |
| 26 | + |
| 27 | +function createDirectoryTestTd(title: string, thingsPropertyHref: string) { |
| 28 | + return { |
| 29 | + "@context": "https://www.w3.org/2022/wot/td/v1.1", |
| 30 | + title, |
| 31 | + security: "nosec_sc", |
| 32 | + securityDefinitions: { |
| 33 | + nosec_sc: { |
| 34 | + scheme: "nosec", |
| 35 | + }, |
| 36 | + }, |
| 37 | + properties: { |
| 38 | + things: { |
| 39 | + forms: [ |
| 40 | + { |
| 41 | + href: thingsPropertyHref, |
| 42 | + }, |
| 43 | + ], |
| 44 | + }, |
| 45 | + }, |
| 46 | + }; |
| 47 | +} |
| 48 | + |
| 49 | +function createDiscoveryContent(td: unknown, contentType: string) { |
| 50 | + const buffer = Buffer.from(JSON.stringify(td)); |
| 51 | + const content = new Content(contentType, Readable.from(buffer)); |
| 52 | + return content; |
| 53 | +} |
| 54 | + |
| 55 | +const directoryTdUrl1 = "test://localhost/valid-output-tds"; |
| 56 | +const directoryTdUrl2 = "test://localhost/invalid-output-tds"; |
| 57 | +const directoryTdUrl3 = "test://localhost/no-array-output"; |
| 58 | + |
| 59 | +const directoryTdTitle1 = "Directory Test TD 1"; |
| 60 | +const directoryTdTitle2 = "Directory Test TD 2"; |
| 61 | +const directoryTdTitle3 = "Directory Test TD 3"; |
| 62 | + |
| 63 | +const directoryThingsUrl1 = "test://localhost/things1"; |
| 64 | +const directoryThingsUrl2 = "test://localhost/things2"; |
| 65 | +const directoryThingsUrl3 = "test://localhost/things3"; |
| 66 | + |
| 67 | +const directoryThingDescription1 = createDirectoryTestTd(directoryTdTitle1, directoryThingsUrl1); |
| 68 | +const directoryThingDescription2 = createDirectoryTestTd(directoryTdTitle2, directoryThingsUrl2); |
| 69 | +const directoryThingDescription3 = createDirectoryTestTd(directoryTdTitle3, directoryThingsUrl3); |
| 70 | + |
| 71 | +class TestProtocolClient implements ProtocolClient { |
| 72 | + async readResource(form: Form): Promise<Content> { |
| 73 | + const href = form.href; |
| 74 | + |
| 75 | + switch (href) { |
| 76 | + case directoryThingsUrl1: |
| 77 | + return createDiscoveryContent([directoryThingDescription1], "application/ld+json"); |
| 78 | + case directoryThingsUrl2: |
| 79 | + return createDiscoveryContent(["I am an invalid TD!"], "application/ld+json"); |
| 80 | + case directoryThingsUrl3: |
| 81 | + return createDiscoveryContent("I am no array and therefore invalid!", "application/ld+json"); |
| 82 | + } |
| 83 | + |
| 84 | + throw new Error("Invalid URL"); |
| 85 | + } |
| 86 | + |
| 87 | + writeResource(form: Form, content: Content): Promise<void> { |
| 88 | + throw new Error("Method not implemented."); |
| 89 | + } |
| 90 | + |
| 91 | + invokeResource(form: Form, content?: Content | undefined): Promise<Content> { |
| 92 | + throw new Error("Method not implemented."); |
| 93 | + } |
| 94 | + |
| 95 | + unlinkResource(form: Form): Promise<void> { |
| 96 | + throw new Error("Method not implemented."); |
| 97 | + } |
| 98 | + |
| 99 | + subscribeResource( |
| 100 | + form: Form, |
| 101 | + next: (content: Content) => void, |
| 102 | + error?: ((error: Error) => void) | undefined, |
| 103 | + complete?: (() => void) | undefined |
| 104 | + ): Promise<Subscription> { |
| 105 | + throw new Error("Method not implemented."); |
| 106 | + } |
| 107 | + |
| 108 | + async requestThingDescription(uri: string): Promise<Content> { |
| 109 | + switch (uri) { |
| 110 | + case directoryTdUrl1: |
| 111 | + debug(`Found corrent URL ${uri} to fetch directory TD`); |
| 112 | + return createDiscoveryContent(directoryThingDescription1, "application/td+json"); |
| 113 | + case directoryTdUrl2: |
| 114 | + debug(`Found corrent URL ${uri} to fetch directory TD`); |
| 115 | + return createDiscoveryContent(directoryThingDescription2, "application/td+json"); |
| 116 | + case directoryTdUrl3: |
| 117 | + debug(`Found corrent URL ${uri} to fetch directory TD`); |
| 118 | + return createDiscoveryContent(directoryThingDescription3, "application/td+json"); |
| 119 | + } |
| 120 | + |
| 121 | + throw Error("Invalid URL"); |
| 122 | + } |
| 123 | + |
| 124 | + async start(): Promise<void> { |
| 125 | + // Do nothing |
| 126 | + } |
| 127 | + |
| 128 | + async stop(): Promise<void> { |
| 129 | + // Do nothing |
| 130 | + } |
| 131 | + |
| 132 | + setSecurity(metadata: SecurityScheme[], credentials?: unknown): boolean { |
| 133 | + return true; |
| 134 | + } |
| 135 | +} |
| 136 | + |
| 137 | +class TestProtocolClientFactory implements ProtocolClientFactory { |
| 138 | + public scheme = "test"; |
| 139 | + |
| 140 | + getClient(): ProtocolClient { |
| 141 | + return new TestProtocolClient(); |
| 142 | + } |
| 143 | + |
| 144 | + init(): boolean { |
| 145 | + return true; |
| 146 | + } |
| 147 | + |
| 148 | + destroy(): boolean { |
| 149 | + return true; |
| 150 | + } |
| 151 | +} |
| 152 | + |
| 153 | +describe("Discovery Tests", () => { |
| 154 | + it("should be possible to use the exploreDirectory method", async () => { |
| 155 | + const servient = new Servient(); |
| 156 | + servient.addClientFactory(new TestProtocolClientFactory()); |
| 157 | + |
| 158 | + const WoT = await servient.start(); |
| 159 | + |
| 160 | + const discoveryProcess = await WoT.exploreDirectory(directoryTdUrl1); |
| 161 | + |
| 162 | + let tdCounter = 0; |
| 163 | + for await (const thingDescription of discoveryProcess) { |
| 164 | + expect(thingDescription.title).to.eql(directoryTdTitle1); |
| 165 | + tdCounter++; |
| 166 | + } |
| 167 | + expect(tdCounter).to.eql(1); |
| 168 | + expect(discoveryProcess.error).to.eq(undefined); |
| 169 | + }); |
| 170 | + |
| 171 | + it("should receive no output and an error by the exploreDirectory method for invalid returned TDs", async () => { |
| 172 | + const servient = new Servient(); |
| 173 | + servient.addClientFactory(new TestProtocolClientFactory()); |
| 174 | + |
| 175 | + const WoT = await servient.start(); |
| 176 | + |
| 177 | + const discoveryProcess = await WoT.exploreDirectory(directoryTdUrl2); |
| 178 | + |
| 179 | + let tdCounter = 0; |
| 180 | + for await (const thingDescription of discoveryProcess) { |
| 181 | + error(`Encountered unexpected TD with title ${thingDescription.title}`); |
| 182 | + tdCounter++; |
| 183 | + } |
| 184 | + expect(tdCounter).to.eql(0); |
| 185 | + expect(discoveryProcess.error).to.not.eq(undefined); |
| 186 | + }); |
| 187 | + |
| 188 | + it("should receive no output and an error by the exploreDirectory method if no array is returned", async () => { |
| 189 | + const servient = new Servient(); |
| 190 | + servient.addClientFactory(new TestProtocolClientFactory()); |
| 191 | + |
| 192 | + const WoT = await servient.start(); |
| 193 | + |
| 194 | + const discoveryProcess = await WoT.exploreDirectory(directoryTdUrl3); |
| 195 | + |
| 196 | + let tdCounter = 0; |
| 197 | + for await (const thingDescription of discoveryProcess) { |
| 198 | + error(`Encountered unexpected TD with title ${thingDescription.title}`); |
| 199 | + tdCounter++; |
| 200 | + } |
| 201 | + expect(tdCounter).to.eql(0); |
| 202 | + expect(discoveryProcess.error).to.not.eq(undefined); |
| 203 | + }); |
| 204 | + |
| 205 | + it("should be possible to stop discovery with exploreDirectory prematurely", async () => { |
| 206 | + const servient = new Servient(); |
| 207 | + servient.addClientFactory(new TestProtocolClientFactory()); |
| 208 | + |
| 209 | + const WoT = await servient.start(); |
| 210 | + |
| 211 | + const discoveryProcess = await WoT.exploreDirectory(directoryTdUrl1); |
| 212 | + expect(discoveryProcess.done).to.not.eq(true); |
| 213 | + discoveryProcess.stop(); |
| 214 | + expect(discoveryProcess.done).to.eq(true); |
| 215 | + |
| 216 | + let tdCounter = 0; |
| 217 | + for await (const thingDescription of discoveryProcess) { |
| 218 | + error(`Encountered unexpected TD with title ${thingDescription.title}`); |
| 219 | + tdCounter++; |
| 220 | + } |
| 221 | + expect(tdCounter).to.eql(0); |
| 222 | + expect(discoveryProcess.error).to.eq(undefined); |
| 223 | + }); |
| 224 | +}); |
0 commit comments