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 all 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
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ The Test Thing is a total toy device that users can try different types of prope

### Smart Home Mashup

See the mashup's [readme](./mashups//smart-home/README.md).
See the mashup's [readme](./mashups/smart-home/README.md).

## How to Run

Expand Down Expand Up @@ -139,10 +139,10 @@ For Node.js-based devices, we use npm workspaces and running `npm install` at th

### Saving Grafana Dashboards

Grafana dashboard json files are stored in [./conf/grafana/dashboards](./conf//grafana//dashboards/).
Grafana dashboard json files are stored in [./conf/grafana/dashboards](./conf/grafana/dashboards/).
To save your newly created dashboard locally and push it into the remote repository:
- Export the dashboard as JSON file using Share > Export.
- Save the exported JSON file to [./conf/grafana/dashboards](./conf//grafana//dashboards/).
- Save the exported JSON file to [./conf/grafana/dashboards](./conf/grafana/dashboards/).

If your dashboard uses another datasource than our default `prometheus-datasource`, new datasource also must be provisioned in [./conf/grafana/datasources](./conf/grafana/provisioning/datasources/).
For more information check Grafana's provisioning [documentation](https://grafana.com/docs/grafana/latest/administration/provisioning/).
2 changes: 1 addition & 1 deletion mashups/smart-home/things/presence-sensor.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/********************************************************************************
* Copyright (c) 2023 Contributors to the Eclipse Foundation
* Copyright (c) 2024 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
Expand Down
2 changes: 1 addition & 1 deletion mashups/smart-home/things/simple-coffee-machine.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/********************************************************************************
* Copyright (c) 2023 Contributors to the Eclipse Foundation
* Copyright (c) 2024 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
Expand Down
2 changes: 1 addition & 1 deletion mashups/smart-home/things/smart-clock.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/********************************************************************************
* Copyright (c) 2023 Contributors to the Eclipse Foundation
* Copyright (c) 2024 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
Expand Down
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 @@ -37,17 +37,18 @@ for tmd in things/* ; do
npm run build
fi

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
21 changes: 7 additions & 14 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())
const port = 3000

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()
})
})
})
})

Loading
Loading