Skip to content

Commit 5a58d39

Browse files
committed
test(coap-server): add test for cov:observe subprotocol
1 parent 55e197c commit 5a58d39

2 files changed

Lines changed: 102 additions & 2 deletions

File tree

packages/binding-coap/src/util.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import { PropertyElement } from "wot-thing-description-types";
1818

1919
const observeOpFilter = ["observeproperty", "unobserveproperty"];
2020
const readWriteOpFilter = ["readproperty", "writeproperty"];
21+
const eventOpFilter = ["subscribeevent", "unsubscribeevent"];
2122

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

49+
/**
50+
* Convenience function to filter out the `op` values "subscribeevent" and
51+
* "unsubscribeevent" from a string array.
52+
*
53+
* @param opValues The `op` values to be filtered.
54+
* @returns A filtered array that might be empty.
55+
*/
56+
export function filterEventOperations(opValues: string[]) {
57+
return filterOpValues(opValues, eventOpFilter);
58+
}
59+
4860
/**
4961
* Function to (potentially) generate two arrays of `op` values: One with the
5062
* values "readproperty" and "writeproperty", and one with

packages/binding-coap/test/coap-server-test.ts

Lines changed: 89 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,13 @@ import Servient, { ExposedThing, Content } from "@node-wot/core";
2020

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

3031
// should must be called to augment all variables
3132
should();
@@ -634,4 +635,91 @@ class CoapServerTest {
634635

635636
await coapServer.stop();
636637
}
638+
639+
@test async "should add the cov:observe subprotocol value to obervable properties and events "() {
640+
const coapServer = new CoapServer({ port: 5683 });
641+
const servient = new Servient();
642+
servient.addServer(coapServer);
643+
644+
await coapServer.start(servient);
645+
646+
const covObserveThing = new ExposedThing(servient, {
647+
title: "Test",
648+
properties: {
649+
observableProperty: {
650+
observable: true,
651+
},
652+
nonObservableProperty: {},
653+
},
654+
events: {
655+
testEvent: {},
656+
},
657+
});
658+
659+
await coapServer.expose(covObserveThing);
660+
661+
await new Promise<void>((resolve) => {
662+
const req = request({
663+
host: "localhost",
664+
pathname: "test",
665+
port: 5683,
666+
method: "GET",
667+
});
668+
req.on("response", (res: IncomingMessage) => {
669+
const payload = res.payload.toString();
670+
const td = JSON.parse(payload) as ThingDescription;
671+
672+
for (const property of Object.values(td.properties!)) {
673+
let observeOpValueFormCount = 0;
674+
for (const form of property.forms) {
675+
const opValues = form.op!;
676+
expect(opValues.length).to.be.greaterThan(0);
677+
678+
const observeOpValueCount = filterPropertyObserveOperations(opValues as Array<string>).length;
679+
const observeOpValuePresent = observeOpValueCount > 0;
680+
681+
if (observeOpValuePresent) {
682+
observeOpValueFormCount++;
683+
expect(form.subprotocol).to.eql("cov:observe");
684+
}
685+
686+
const readWriteOpValueCount = filterPropertyReadWriteOperations(
687+
opValues as Array<string>
688+
).length;
689+
const readWriteOpValuePresent = readWriteOpValueCount > 0;
690+
691+
// eslint-disable-next-line no-unused-expressions
692+
expect(observeOpValuePresent && readWriteOpValuePresent).to.not.be.true;
693+
694+
if (property.observable !== true) {
695+
expect(observeOpValueCount).to.eql(0);
696+
}
697+
}
698+
699+
if (property.observable === true) {
700+
expect(observeOpValueFormCount).to.be.greaterThan(0);
701+
}
702+
}
703+
704+
for (const event of Object.values(td.events!)) {
705+
for (const form of event.forms) {
706+
const opValues = form.op!;
707+
expect(opValues.length > 0);
708+
709+
const eventOpValueCount = filterEventOperations(opValues as Array<string>).length;
710+
const eventOpValueCountPresent = eventOpValueCount > 0;
711+
712+
expect(eventOpValueCountPresent);
713+
714+
expect(form.subprotocol === "cov:observe");
715+
}
716+
}
717+
718+
resolve();
719+
});
720+
req.end();
721+
});
722+
723+
await coapServer.stop();
724+
}
637725
}

0 commit comments

Comments
 (0)