Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

8 communication tests #39

Merged
merged 14 commits into from
Sep 20, 2024
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions scripts/runTests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,18 @@ for tmd in things/* ; do

current_path=$(pwd)
cd $tdd
td_result="$(../../../../node_modules/mocha/bin/mocha.js --exit --timeout 5000)"
td_result=$(../../../../node_modules/mocha/bin/mocha.js)
td_exit_code=$?
cd $current_path

if [ $td_exit_code -ne 0 ]; then
echo -e "\033[0;31m** TD test failed for the thing $tdd.\033[0m"
echo $td_result
echo "$td_result"
return_value=1
continue
else
echo -e "\033[0;32m** TD test successful for $tdd.\033[0m"
echo "$td_result"
fi
done
echo "-----"
Expand Down
23 changes: 8 additions & 15 deletions things/advanced-coffee-machine/http/ts/test/client.test.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,19 @@

import chai from 'chai'
import chaiAsPromised from 'chai-as-promised';
import chaiAsPromised from 'chai-as-promised'

import { Servient } from "@node-wot/core";
import { Servient } from "@node-wot/core"
import { HttpClientFactory } from "@node-wot/binding-http"
import { port } from './fixtures'

chai.use(chaiAsPromised)
const expect = chai.expect

let servient = new Servient()
servient.addClientFactory(new HttpClientFactory({ baseUri: 'localhost:3000' }))
const port = 3000
servient.addClientFactory(new HttpClientFactory())

let thing: WoT.ConsumedThing

const readProperty = async (thing: WoT.ConsumedThing, name: string): Promise<any> => {
try {
const res = await thing.readProperty(name)
const value = await res.value()
return value
}
catch (error) {
console.error(`Error: ${error}`)
}
}

describe("Client Tests", () => {
before(async () => {
try {
Expand All @@ -35,6 +24,10 @@ describe("Client Tests", () => {
console.error(error)
}
})

after(async () => {
await servient.shutdown()
})

it("should read allAvailableResources property", async () => {
const response = await thing.readProperty("allAvailableResources")
Expand Down
4 changes: 2 additions & 2 deletions things/advanced-coffee-machine/http/ts/test/fixtures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ import path from "path"

let thingProcess: ChildProcess | undefined
let response: ThingStartResponse
const port = 3000
export const port = 3000

export async function mochaGlobalSetup() {
try {
response = await getInitiateMain(path.join(__dirname, '..', 'dist', 'main.js'), port)
response = await getInitiateMain('node', [path.join(__dirname, '..', 'dist', 'main.js'), '-p', `${port}`])
thingProcess = response.process
}
catch(error: any) {
Expand Down
2 changes: 1 addition & 1 deletion things/advanced-coffee-machine/http/ts/test/td.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import * as chai from 'chai'
import * as http from 'http'
import { getTDValidate } from '../../../../../util/util'
import { ValidateFunction } from 'ajv'
import { port } from './fixtures'

const expect = chai.expect

const port = 3000
let validate: ValidateFunction | undefined

describe("TD Test", () => {
Expand Down
5 changes: 5 additions & 0 deletions things/calculator/coap/js/.mocharc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"exit": true,
"spec": "./test/**.test.js",
"require": ["./test/fixtures.js"]
}
218 changes: 218 additions & 0 deletions things/calculator/coap/js/test/client.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,218 @@
const chai = require("chai")
const chaiAsPromised = require("chai-as-promised")

const { Servient } = require("@node-wot/core")
const { CoapClientFactory } = require("@node-wot/binding-coap")
const { simplePort, contentNegotiationPort } = require('./fixtures')

chai.use(chaiAsPromised)
const expect = chai.expect

const servient = new Servient()
servient.addClientFactory(new CoapClientFactory())
let WoT

const readProperty = async (thing, propertyName) => {
try {
const response = await thing.readProperty(propertyName)
return await response.value()
} catch(error) {
console.error(`Error: ${error}`)
}
}

describe("Client Tests", () => {
before(async () => {
try {
WoT = await servient.start()
} catch(error) {
console.error(error)
}
})
after(async () => {
await servient.shutdown()
})

describe("Simple Calculator", () => {
let thing

before(async () => {
try {
const td = await WoT.requestThingDescription(`coap://localhost:${simplePort}/coap-calculator-simple`)
thing = await WoT.consume(td)
} catch(error) {
console.error(error)
}
})

describe("result property", () => {
it("should return initial value", async () => {
const value = await readProperty(thing, "result")
expect(value).to.be.equal(0)
})

it("should return sum when adding value to the existing result", async () => {
const resultValue = await readProperty(thing, "result")
const valueToAdd = 12
await thing.invokeAction("add", valueToAdd)
const newResultValue = await readProperty(thing, "result")
expect(newResultValue).to.be.equal(resultValue + valueToAdd)
})

it("should return sum when subtracting value from the existing result", async() => {
const resultValue = await readProperty(thing, "result")
const valueToSubtract = 3
await thing.invokeAction("subtract", valueToSubtract)
const newResultValue = await readProperty(thing, "result")
expect(newResultValue).to.be.equal(resultValue - valueToSubtract)
})
})

describe("lastChange property", () => {
it("should observe a change when the result is changed", async () => {
setTimeout(async () => {
await thing.invokeAction('add', 1)
}, 200)

let value
const subscription = thing.observeProperty('lastChange', async (response) => {
value = await response.value()
})

setTimeout(async () => {
expect(value).to.be.not.undefined
await subscription.stop()
})
})
})

describe("add action", () => {
it("should return sum when adding value to the existing result", async () => {
const resultValue = await readProperty(thing, "result")
const valueToAdd = 12
const response = await thing.invokeAction("add", valueToAdd)
const actionResultValue = await response.value()
expect(actionResultValue).to.be.equal(resultValue + valueToAdd)
})
})

describe("subtract action", () => {
it("should return sum when subtracting value from the existing result", async() => {
const resultValue = await readProperty(thing, "result")
const valueToSubtract = 3
const response = await thing.invokeAction("subtract", valueToSubtract)
const actionResultValue = await response.value()
expect(actionResultValue).to.be.equal(resultValue - valueToSubtract)
})
})

describe("update event", () => {
it("should return the update message when subscribed", async () => {
const resultValue = await readProperty(thing, "result")
const valueToAdd = 13

const actionTimeout = setInterval(async () => {
await thing.invokeAction('add', valueToAdd)
}, 200)

const subscription = await thing.subscribeEvent("update", async (response) => {
await expect(response.value).to.have.eventually.be.equal(resultValue + valueToAdd)
})

await subscription.stop()
})
})
})

describe("Content Negotiation Calculator", () => {
let thing

before(async () => {
try {
const td = await WoT.requestThingDescription(`coap://localhost:${contentNegotiationPort}/coap-calculator-content-negotiation`)
thing = await WoT.consume(td)
} catch(error) {
console.error(error)
}
})

describe("result property", () => {
it("should return initial value", async () => {
const value = await readProperty(thing, "result")
expect(value).to.be.equal(0)
})

it("should return sum when adding value to the existing result", async () => {
const resultValue = await readProperty(thing, "result")
const valueToAdd = 12
await thing.invokeAction("add", valueToAdd)
const newResultValue = await readProperty(thing, "result")
expect(newResultValue).to.be.equal(resultValue + valueToAdd)
})

it("should return sum when subtracting value from the existing result", async () => {
const resultValue = await readProperty(thing, "result")
const valueToSubtract = 3
await thing.invokeAction("subtract", valueToSubtract)
const newResultValue = await readProperty(thing, "result")
expect(newResultValue).to.be.equal(resultValue - valueToSubtract)
})
})

describe("lastChange property", () => {
it("should observe a change when the result is changed", async () => {
setTimeout(async () => {
await thing.invokeAction('add', 1)
}, 200)

let value
const subscription = thing.observeProperty('lastChange', async (response) => {
value = await response.value()
})

setTimeout(async () => {
expect(value).to.be.not.undefined
await subscription.stop()
})
})
})

describe("add action", () => {
it("should return sum when adding value to the existing result", async () => {
const resultValue = await readProperty(thing, "result")
const valueToAdd = 12
const response = await thing.invokeAction("add", valueToAdd)
const actionResultValue = await response.value()
expect(actionResultValue).to.be.equal(resultValue + valueToAdd)
})
})

describe("subtract action", () => {
it("should return sum when subtracting value from the existing result", async() => {
const resultValue = await readProperty(thing, "result")
const valueToSubtract = 3
const response = await thing.invokeAction("subtract", valueToSubtract)
const actionResultValue = await response.value()
expect(actionResultValue).to.be.equal(resultValue - valueToSubtract)
})
})

describe("update event", () => {
it("should return the update message when subscribed", async () => {
const resultValue = await readProperty(thing, "result")
const valueToAdd = 13

const actionTimeout = setInterval(async () => {
await thing.invokeAction('add', valueToAdd)
}, 200)

const subscription = await thing.subscribeEvent("update", async (response) => {
await expect(response.value).to.have.eventually.be.equal(resultValue + valueToAdd)
})

await subscription.stop()
})
})
})
})

45 changes: 45 additions & 0 deletions things/calculator/coap/js/test/fixtures.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
const { getInitiateMain } = require("../../../../../util/dist/util")
const path = require("node:path")

let simpleThingProcess
let contentNegotiationThingProcess
let response
const simplePort = 5683
const contentNegotiationPort = 5684

const mochaGlobalSetup = async function() {
try {
response = await getInitiateMain('node', [path.join(__dirname, '..', 'coap-simple-calculator.js'), '-p', `${simplePort}`])
simpleThingProcess = response.process
}
catch(error) {
console.error(error)
simpleThingProcess = error.process
}

try {
response = await getInitiateMain('node', [path.join(__dirname, '..', 'coap-content-negotiation-calculator.js'), '-p', `${contentNegotiationPort}`])
contentNegotiationThingProcess = response.process
}
catch (error) {
console.error(error)
contentNegotiationThingProcess = error.process
}
}

const mochaGlobalTeardown = function() {
if(simpleThingProcess) {
simpleThingProcess.kill()
}

if(contentNegotiationThingProcess) {
contentNegotiationThingProcess.kill()
}
}

module.exports = {
simplePort,
contentNegotiationPort,
mochaGlobalSetup,
mochaGlobalTeardown
}
Loading
Loading