Skip to content

Commit

Permalink
test(coap-server): add test for cov:observe subprotocol
Browse files Browse the repository at this point in the history
  • Loading branch information
JKRhb committed Jan 12, 2024
1 parent 55e197c commit 5a58d39
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 2 deletions.
14 changes: 13 additions & 1 deletion packages/binding-coap/src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { PropertyElement } from "wot-thing-description-types";

const observeOpFilter = ["observeproperty", "unobserveproperty"];
const readWriteOpFilter = ["readproperty", "writeproperty"];
const eventOpFilter = ["subscribeevent", "unsubscribeevent"];

function filterOpValues(opValues: string[], filterValues: string[]) {
return opValues.filter((opValue) => filterValues.includes(opValue));
Expand All @@ -41,10 +42,21 @@ export function filterPropertyObserveOperations(opValues: string[]) {
* @param opValues The `op` values to be filtered.
* @returns A filtered array that might be empty.
*/
function filterPropertyReadWriteOperations(opValues: string[]) {
export function filterPropertyReadWriteOperations(opValues: string[]) {
return filterOpValues(opValues, readWriteOpFilter);
}

/**
* Convenience function to filter out the `op` values "subscribeevent" and
* "unsubscribeevent" from a string array.
*
* @param opValues The `op` values to be filtered.
* @returns A filtered array that might be empty.
*/
export function filterEventOperations(opValues: string[]) {
return filterOpValues(opValues, eventOpFilter);
}

/**
* Function to (potentially) generate two arrays of `op` values: One with the
* values "readproperty" and "writeproperty", and one with
Expand Down
90 changes: 89 additions & 1 deletion packages/binding-coap/test/coap-server-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,13 @@ import Servient, { ExposedThing, Content } from "@node-wot/core";

import { suite, test } from "@testdeck/mocha";
import { expect, should } from "chai";
import { DataSchemaValue, InteractionInput, InteractionOptions } from "wot-typescript-definitions";
import { DataSchemaValue, InteractionInput, InteractionOptions, ThingDescription } from "wot-typescript-definitions";
import * as TD from "@node-wot/td-tools";
import CoapServer from "../src/coap-server";
import { CoapClient } from "../src/coap";
import { Readable } from "stream";
import { IncomingMessage, registerFormat, request } from "coap";
import { filterEventOperations, filterPropertyObserveOperations, filterPropertyReadWriteOperations } from "../src/util";

// should must be called to augment all variables
should();
Expand Down Expand Up @@ -634,4 +635,91 @@ class CoapServerTest {

await coapServer.stop();
}

@test async "should add the cov:observe subprotocol value to obervable properties and events "() {
const coapServer = new CoapServer({ port: 5683 });
const servient = new Servient();
servient.addServer(coapServer);

await coapServer.start(servient);

const covObserveThing = new ExposedThing(servient, {
title: "Test",
properties: {
observableProperty: {
observable: true,
},
nonObservableProperty: {},
},
events: {
testEvent: {},
},
});

await coapServer.expose(covObserveThing);

await new Promise<void>((resolve) => {
const req = request({
host: "localhost",
pathname: "test",
port: 5683,
method: "GET",
});
req.on("response", (res: IncomingMessage) => {
const payload = res.payload.toString();
const td = JSON.parse(payload) as ThingDescription;

for (const property of Object.values(td.properties!)) {
let observeOpValueFormCount = 0;
for (const form of property.forms) {
const opValues = form.op!;
expect(opValues.length).to.be.greaterThan(0);

const observeOpValueCount = filterPropertyObserveOperations(opValues as Array<string>).length;
const observeOpValuePresent = observeOpValueCount > 0;

if (observeOpValuePresent) {
observeOpValueFormCount++;
expect(form.subprotocol).to.eql("cov:observe");
}

const readWriteOpValueCount = filterPropertyReadWriteOperations(
opValues as Array<string>
).length;
const readWriteOpValuePresent = readWriteOpValueCount > 0;

// eslint-disable-next-line no-unused-expressions
expect(observeOpValuePresent && readWriteOpValuePresent).to.not.be.true;

if (property.observable !== true) {
expect(observeOpValueCount).to.eql(0);
}
}

if (property.observable === true) {
expect(observeOpValueFormCount).to.be.greaterThan(0);
}
}

for (const event of Object.values(td.events!)) {
for (const form of event.forms) {
const opValues = form.op!;
expect(opValues.length > 0);

const eventOpValueCount = filterEventOperations(opValues as Array<string>).length;
const eventOpValueCountPresent = eventOpValueCount > 0;

expect(eventOpValueCountPresent);

expect(form.subprotocol === "cov:observe");
}
}

resolve();
});
req.end();
});

await coapServer.stop();
}
}

0 comments on commit 5a58d39

Please sign in to comment.