Skip to content

Commit ab9e2ab

Browse files
committed
test: add additional tests for exceptions
1 parent c9ea575 commit ab9e2ab

File tree

4 files changed

+115
-0
lines changed

4 files changed

+115
-0
lines changed

test/core/exceptions_test.dart

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Copyright 2024 Contributors to the Eclipse Foundation. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
//
5+
// SPDX-License-Identifier: BSD-3-Clause
6+
7+
import "package:dart_wot/core.dart";
8+
import "package:test/test.dart";
9+
10+
void main() {
11+
group("DartWotException should", () {
12+
test("be indicate the respective name in its toString() method", () {
13+
expect(
14+
DartWotException("test").toString(),
15+
"DartWotException: test",
16+
);
17+
18+
expect(
19+
ValidationException("test").toString(),
20+
"ValidationException: test",
21+
);
22+
23+
expect(
24+
ValidationException("test", ["test", "test"]).toString(),
25+
"ValidationException: test\n\nErrors:\n\ntest\ntest",
26+
);
27+
28+
expect(
29+
DiscoveryException("test").toString(),
30+
"DiscoveryException: test",
31+
);
32+
});
33+
});
34+
}

test/core/servient_test.dart

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,4 +61,17 @@ void main() {
6161
},
6262
);
6363
});
64+
65+
test(
66+
"should throw a DartWotException when a "
67+
"ProtocolClientFactory is not registered",
68+
() {
69+
final servient = Servient();
70+
71+
expect(
72+
() => servient.clientFor(testUriScheme),
73+
throwsA(isA<DartWotException>()),
74+
);
75+
},
76+
);
6477
}

test/core/thing_description_test.dart

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,22 @@ void main() {
4343
throwsUnimplementedError,
4444
);
4545
});
46+
47+
test("should throw a ValidationException when it is invalid during parsing",
48+
() {
49+
const thingDescriptionJson = {
50+
"@context": [
51+
"https://www.w3.org/2022/wot/td/v1.1",
52+
],
53+
"title": "Invalid TD with missing security field.",
54+
"securityDefinitions": {
55+
"nosec_sc": {"scheme": "nosec"},
56+
},
57+
};
58+
59+
expect(
60+
() => ThingDescription.fromJson(thingDescriptionJson),
61+
throwsA(isA<ValidationException>()),
62+
);
63+
});
4664
}

test/core/wot_test.dart

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// Copyright 2024 Contributors to the Eclipse Foundation. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
//
5+
// SPDX-License-Identifier: BSD-3-Clause
6+
7+
import "package:dart_wot/core.dart";
8+
import "package:test/test.dart";
9+
10+
void main() {
11+
group("WoT should", () {
12+
test("throw an execption when consuming the same TD twice", () async {
13+
const thingDescriptionJson = {
14+
"@context": "https://www.w3.org/2022/wot/td/v1.1",
15+
"title": "Test Thing",
16+
"securityDefinitions": {
17+
"nosec_sc": {"scheme": "nosec"},
18+
},
19+
"security": ["nosec_sc"],
20+
};
21+
final thingDescription = thingDescriptionJson.toThingDescription();
22+
23+
final wot = await Servient().start();
24+
25+
await wot.consume(thingDescription);
26+
final result = wot.consume(thingDescription);
27+
await expectLater(result, throwsA(isA<DartWotException>()));
28+
});
29+
30+
test(
31+
"throw an execption when producing an ExposedThing "
32+
"from the same TD twice", () async {
33+
const exposedThingInit = {
34+
"@context": "https://www.w3.org/2022/wot/td/v1.1",
35+
"title": "Test Thing",
36+
"securityDefinitions": {
37+
"nosec_sc": {"scheme": "nosec"},
38+
},
39+
"security": ["nosec_sc"],
40+
"id": "urn:test"
41+
};
42+
43+
final wot = await Servient().start();
44+
45+
await wot.produce(exposedThingInit);
46+
final result = wot.produce(exposedThingInit);
47+
await expectLater(result, throwsA(isA<DartWotException>()));
48+
});
49+
});
50+
}

0 commit comments

Comments
 (0)