public void BuildWithEvent()
        {
            var thing = new EventThing();

            _builder
            .SetThing(thing)
            .SetThingOption(_option);

            Visit(thing.GetType());

            var response = _builder.Build();

            response.Should().NotBeNull();

            var message = JsonSerializer.Serialize(response, response.GetType(), _option.ToJsonSerializerOptions());

            FluentAssertions.Json.JsonAssertionExtensions.Should(JToken.Parse(message))
            .BeEquivalentTo(JToken.Parse(@"
{
    ""events"": {
        ""int"": {
            ""type"": ""integer"",
            ""links"": [
                {
                    ""href"": ""/things/event-thing/events/int"",
                    ""rel"": ""event""
                }
            ]
        },
        ""test"": {
            ""title"": ""Bar"",
            ""description"": ""Foo"",
            ""unit"": ""milli"",
            ""type"": ""string"",
            ""links"": [
                {
                    ""href"": ""/things/event-thing/events/test"",
                    ""rel"": ""event""
                }
            ]
        }
    },
    ""@context"": ""https://iot.mozilla.org/schemas"",
    ""security"": ""nosec_sc"",
    ""securityDefinitions"": {
        ""nosec_sc"": {
            ""scheme"": ""nosec""
        }
    },
    ""links"": [{
        ""rel"": ""properties"",
        ""href"": ""/things/event-thing/properties""
      },{
        ""rel"": ""actions"",
        ""href"": ""/things/event-thing/actions""
      },{
        ""rel"": ""events"",
        ""href"": ""/things/event-thing/events""
    }]
}
"));

            void Visit(Type thingType)
            {
                var events = thingType.GetEvents(BindingFlags.Public | BindingFlags.Instance);

                foreach (var @event in events)
                {
                    var args = @event.EventHandlerType !.GetGenericArguments();
                    if (args.Length > 1)
                    {
                        continue;
                    }

                    if ((args.Length == 0 && @event.EventHandlerType != typeof(EventHandler)) ||
                        (args.Length == 1 && @event.EventHandlerType != typeof(EventHandler <>).MakeGenericType(args[0])))
                    {
                        continue;
                    }

                    _builder.Add(@event, @event.GetCustomAttribute <ThingEventAttribute>());
                }
            }
        }
Пример #2
0
 /// <inheritdoc/>
 public T Deserialize <T>(ReadOnlySpan <byte> values)
 {
     return(JsonSerializer.Deserialize <T>(values, _options.ToJsonSerializerOptions()));
 }