Skip to content

Commit

Permalink
chore(td-utils): change test suite style
Browse files Browse the repository at this point in the history
Signed-off-by: Hasan Eroglu <[email protected]>
  • Loading branch information
hasanheroglu committed Jul 5, 2024
1 parent 5ef5551 commit a19787d
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 72 deletions.
14 changes: 1 addition & 13 deletions node/td-utils/test/examples/httpAndMqtt.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,5 @@
"toggle": {
"forms": [{ "href": "mqtt://mylamp.example.com:1234/toggle" }]
}
},
"protocolSchemes": [
{
"protocol": "http",
"hostname": "mylamp.example.com",
"port": 1234
},
{
"protocol": "mqtt",
"hostname": "mylamp.example.com",
"port": 1234
}
]
}
}
3 changes: 1 addition & 2 deletions node/td-utils/test/examples/noProtocol.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,5 @@
"security": ["basic_sc"],
"properties": {},
"actions": {},
"events": {},
"protocolSchemes": []
"events": {}
}
14 changes: 1 addition & 13 deletions node/td-utils/test/examples/onlyHttp.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,5 @@
}
]
}
},
"protocolSchemes": [
{
"protocol": "http",
"hostname": "mylamp.example.com",
"port": 4212
},
{
"protocol": "http",
"hostname": "mylamp.example.com",
"port": 4214
}
]
}
}
8 changes: 1 addition & 7 deletions node/td-utils/test/examples/onlyMqtt.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,5 @@
}
]
}
},
"protocolSchemes": [
{
"protocol": "mqtt",
"hostname": "mylamp.example.com"
}
]
}
}
12 changes: 1 addition & 11 deletions node/td-utils/test/examples/secureProtocols.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,5 @@
}
]
}
},
"protocolSchemes": [
{
"protocol": "https",
"hostname": "mylamp.example.com"
},
{
"protocol": "mqtts",
"hostname": "mylamp.example.com"
}
]
}
}
69 changes: 69 additions & 0 deletions node/td-utils/test/protocol-detection.test.suite.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import httpAndMqtt from "./examples/httpAndMqtt.json";
import noProtocol from "./examples/noProtocol.json";
import onlyHttp from "./examples/onlyHttp.json";
import onlyMqtt from "./examples/onlyMqtt.json";
import secureProtocols from "./examples/secureProtocols.json";

export const testSuite = [
{
name: "httpAndMqtt",
input: httpAndMqtt,
expected: [
{
protocol: "http",
hostname: "mylamp.example.com",
port: 1234,
},
{
protocol: "mqtt",
hostname: "mylamp.example.com",
port: 1234,
},
],
},
{
name: "noProtocol",
input: noProtocol,
expected: [],
},
{
name: "onlyHttp",
input: onlyHttp,
expected: [
{
protocol: "http",
hostname: "mylamp.example.com",
port: 4212,
},
{
protocol: "http",
hostname: "mylamp.example.com",
port: 4214,
},
],
},
{
name: "onlyMqtt",
input: onlyMqtt,
expected: [
{
protocol: "mqtt",
hostname: "mylamp.example.com",
},
],
},
{
name: "secureProtocols",
input: secureProtocols,
expected: [
{
protocol: "https",
hostname: "mylamp.example.com",
},
{
protocol: "mqtts",
hostname: "mylamp.example.com",
},
],
},
];
34 changes: 8 additions & 26 deletions node/td-utils/test/protocol-detection.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,41 +14,23 @@
*/
import { detectProtocolSchemes, ProtocolScheme } from "../src/detectProtocolSchemes";
import { ThingDescription } from "wot-thing-description-types";
import httpAndMqtt from "./examples/httpAndMqtt.json";
import noProtocol from "./examples/noProtocol.json";
import onlyHttp from "./examples/onlyHttp.json";
import onlyMqtt from "./examples/onlyMqtt.json";
import secureProtocols from "./examples/secureProtocols.json";
import { testSuite } from "./protocol-detection.test.suite";

export type ThingDescriptionTest = ThingDescription & { protocolSchemes: ProtocolScheme[] };

describe("test examples", () => {
it("should test httpAndMqtt", () => {
testTD(httpAndMqtt);
});

it("should test noProtocol", () => {
testTD(noProtocol);
});

it("should test onlyHttp", () => {
testTD(onlyHttp);
});

it("should test onlyMqtt", () => {
testTD(onlyMqtt);
});

it("should test secureProtocols", () => {
testTD(secureProtocols);
testSuite.forEach((t) => {
it(`should test ${t.name}`, () => {
testTD(t.input, t.expected);
});
});
});

const testTD = (td: any) => {
const testTD = (td: any, expected: ProtocolScheme[]) => {
const detectedProtocolSchemes = detectProtocolSchemes(JSON.stringify(td));

expect(detectedProtocolSchemes.length).toBe(td.protocolSchemes.length);
td.protocolSchemes.forEach((s: ProtocolScheme) => {
expect(detectedProtocolSchemes.length).toBe(expected.length);
expected.forEach((s: ProtocolScheme) => {
expect(detectedProtocolSchemes).toContainEqual(s);
});
};

0 comments on commit a19787d

Please sign in to comment.