|
| 1 | +// Copyright 2023 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 'dart:convert'; |
| 8 | + |
| 9 | +import 'package:dart_wot/dart_wot.dart'; |
| 10 | +import 'package:dart_wot/src/core/content.dart'; |
| 11 | +import 'package:dart_wot/src/core/thing_discovery.dart'; |
| 12 | +import 'package:test/test.dart'; |
| 13 | + |
| 14 | +const testUriScheme = 'test'; |
| 15 | +final validTestDiscoveryUri = |
| 16 | + Uri.parse('$testUriScheme://[::1]/.well-known/wot'); |
| 17 | +final invalidTestDiscoveryUri = |
| 18 | + Uri.parse('$testUriScheme://[::2]/.well-known/wot'); |
| 19 | + |
| 20 | +const validTestTitle = 'Test TD'; |
| 21 | +const validTestThingDescription = ''' |
| 22 | + { |
| 23 | + "@context": "https://www.w3.org/2022/wot/td/v1.1", |
| 24 | + "title": "$validTestTitle", |
| 25 | + "security": "nosec_sc", |
| 26 | + "securityDefinitions": { |
| 27 | + "nosec_sc": {"scheme": "nosec"} |
| 28 | + } |
| 29 | + } |
| 30 | +'''; |
| 31 | + |
| 32 | +const invalidTestThingDescription = '"Hi there!"'; |
| 33 | + |
| 34 | +void main() { |
| 35 | + group('Discovery Tests', () { |
| 36 | + test('Should be able to use the requestThingDescription method', () async { |
| 37 | + final servient = Servient( |
| 38 | + clientFactories: [ |
| 39 | + _MockedProtocolClientFactory(), |
| 40 | + ], |
| 41 | + ); |
| 42 | + |
| 43 | + final wot = await servient.start(); |
| 44 | + final thingDescription = |
| 45 | + await wot.requestThingDescription(validTestDiscoveryUri); |
| 46 | + |
| 47 | + expect(thingDescription.title, validTestTitle); |
| 48 | + }); |
| 49 | + |
| 50 | + test( |
| 51 | + 'Should throw an exception if an invalid TD results from the ' |
| 52 | + 'requestThingDescription method', |
| 53 | + () async { |
| 54 | + final servient = Servient( |
| 55 | + clientFactories: [ |
| 56 | + _MockedProtocolClientFactory(), |
| 57 | + ], |
| 58 | + ); |
| 59 | + |
| 60 | + final wot = await servient.start(); |
| 61 | + await expectLater( |
| 62 | + wot.requestThingDescription(invalidTestDiscoveryUri), |
| 63 | + // TODO: Refine error handling |
| 64 | + throwsA(isA<DiscoveryException>()), |
| 65 | + ); |
| 66 | + }, |
| 67 | + ); |
| 68 | + }); |
| 69 | +} |
| 70 | + |
| 71 | +class _MockedProtocolClient implements ProtocolClient { |
| 72 | + @override |
| 73 | + Stream<DiscoveryContent> discoverWithCoreLinkFormat(Uri uri) { |
| 74 | + // TODO: implement discoverWithCoreLinkFormat |
| 75 | + throw UnimplementedError(); |
| 76 | + } |
| 77 | + |
| 78 | + @override |
| 79 | + Future<Content> invokeResource(Form form, Content content) { |
| 80 | + // TODO: implement invokeResource |
| 81 | + throw UnimplementedError(); |
| 82 | + } |
| 83 | + |
| 84 | + @override |
| 85 | + Future<Content> readResource(Form form) { |
| 86 | + // TODO: implement readResource |
| 87 | + throw UnimplementedError(); |
| 88 | + } |
| 89 | + |
| 90 | + @override |
| 91 | + Future<Content> requestThingDescription(Uri url) async { |
| 92 | + if (url == validTestDiscoveryUri) { |
| 93 | + return validTestThingDescription.toDiscoveryContent(url); |
| 94 | + } |
| 95 | + |
| 96 | + if (url == invalidTestDiscoveryUri) { |
| 97 | + return invalidTestThingDescription.toDiscoveryContent(url); |
| 98 | + } |
| 99 | + |
| 100 | + throw StateError('Encountered invalid URL.'); |
| 101 | + } |
| 102 | + |
| 103 | + @override |
| 104 | + Future<void> start() async { |
| 105 | + // Do nothing |
| 106 | + } |
| 107 | + |
| 108 | + @override |
| 109 | + Future<void> stop() async { |
| 110 | + // Do nothing |
| 111 | + } |
| 112 | + |
| 113 | + @override |
| 114 | + Future<Subscription> subscribeResource( |
| 115 | + Form form, { |
| 116 | + required void Function(Content content) next, |
| 117 | + void Function(Exception error)? error, |
| 118 | + required void Function() complete, |
| 119 | + }) { |
| 120 | + // TODO: implement subscribeResource |
| 121 | + throw UnimplementedError(); |
| 122 | + } |
| 123 | + |
| 124 | + @override |
| 125 | + Future<void> writeResource(Form form, Content content) { |
| 126 | + // TODO: implement writeResource |
| 127 | + throw UnimplementedError(); |
| 128 | + } |
| 129 | + |
| 130 | + @override |
| 131 | + Stream<DiscoveryContent> discoverDirectly( |
| 132 | + Uri uri, { |
| 133 | + bool disableMulticast = false, |
| 134 | + }) { |
| 135 | + // TODO: implement discoverDirectly |
| 136 | + throw UnimplementedError(); |
| 137 | + } |
| 138 | +} |
| 139 | + |
| 140 | +class _MockedProtocolClientFactory implements ProtocolClientFactory { |
| 141 | + @override |
| 142 | + ProtocolClient createClient() { |
| 143 | + return _MockedProtocolClient(); |
| 144 | + } |
| 145 | + |
| 146 | + @override |
| 147 | + bool destroy() { |
| 148 | + return true; |
| 149 | + } |
| 150 | + |
| 151 | + @override |
| 152 | + bool init() { |
| 153 | + return true; |
| 154 | + } |
| 155 | + |
| 156 | + @override |
| 157 | + Set<String> get schemes => {testUriScheme}; |
| 158 | +} |
| 159 | + |
| 160 | +extension _DiscoveryContentCreationExtension on String { |
| 161 | + DiscoveryContent toDiscoveryContent(Uri url) { |
| 162 | + final body = Stream.fromIterable([utf8.encode(this)]); |
| 163 | + return DiscoveryContent('application/td+json', body, url); |
| 164 | + } |
| 165 | +} |
0 commit comments