public PropertyBuilderTest() { _fixture = new Fixture(); _thing = new PropertyThing(); _factory = new FakePropertyFactory(); _builder = new PropertyBuilder(_factory); }
public void BuildWithProperties() { var thing = new PropertyThing(); _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(@" { ""properties"": { ""bool"": { ""type"": ""boolean"", ""links"": [ { ""href"": ""/things/property-thing/properties/bool"", ""rel"": ""property"" } ] }, ""guid"": { ""type"": ""string"", ""links"": [ { ""href"": ""/things/property-thing/properties/guid"", ""rel"": ""property"" } ] }, ""timeSpan"": { ""type"": ""string"", ""links"": [ { ""href"": ""/things/property-thing/properties/timeSpan"", ""rel"": ""property"" } ] }, ""dateTime"": { ""type"": ""string"", ""links"": [ { ""href"": ""/things/property-thing/properties/dateTime"", ""rel"": ""property"" } ] }, ""dateTimeOffset"": { ""type"": ""string"", ""links"": [ { ""href"": ""/things/property-thing/properties/dateTimeOffset"", ""rel"": ""property"" } ] }, ""enum"": { ""type"": ""string"", ""links"": [ { ""href"": ""/things/property-thing/properties/enum"", ""rel"": ""property"" } ] }, ""string"": { ""type"": ""string"", ""links"": [ { ""href"": ""/things/property-thing/properties/string"", ""rel"": ""property"" } ] }, ""byte"": { ""type"": ""integer"", ""links"": [ { ""href"": ""/things/property-thing/properties/byte"", ""rel"": ""property"" } ] }, ""sbyte"": { ""type"": ""integer"", ""links"": [ { ""href"": ""/things/property-thing/properties/sbyte"", ""rel"": ""property"" } ] }, ""short"": { ""type"": ""integer"", ""links"": [ { ""href"": ""/things/property-thing/properties/short"", ""rel"": ""property"" } ] }, ""ushort"": { ""type"": ""integer"", ""links"": [ { ""href"": ""/things/property-thing/properties/ushort"", ""rel"": ""property"" } ] }, ""int"": { ""type"": ""integer"", ""links"": [ { ""href"": ""/things/property-thing/properties/int"", ""rel"": ""property"" } ] }, ""uint"": { ""type"": ""integer"", ""links"": [ { ""href"": ""/things/property-thing/properties/uint"", ""rel"": ""property"" } ] }, ""long"": { ""type"": ""integer"", ""links"": [ { ""href"": ""/things/property-thing/properties/long"", ""rel"": ""property"" } ] }, ""ulong"": { ""type"": ""integer"", ""links"": [ { ""href"": ""/things/property-thing/properties/ulong"", ""rel"": ""property"" } ] }, ""float"": { ""type"": ""number"", ""links"": [ { ""href"": ""/things/property-thing/properties/float"", ""rel"": ""property"" } ] }, ""double"": { ""type"": ""number"", ""links"": [ { ""href"": ""/things/property-thing/properties/double"", ""rel"": ""property"" } ] }, ""decimal"": { ""type"": ""number"", ""links"": [ { ""href"": ""/things/property-thing/properties/decimal"", ""rel"": ""property"" } ] } }, ""@context"": ""https://iot.mozilla.org/schemas"", ""security"": ""nosec_sc"", ""securityDefinitions"": { ""nosec_sc"": { ""scheme"": ""nosec"" } }, ""links"": [{ ""rel"": ""properties"", ""href"": ""/things/property-thing/properties"" },{ ""rel"": ""actions"", ""href"": ""/things/property-thing/actions"" },{ ""rel"": ""events"", ""href"": ""/things/property-thing/events"" }] } ")); void Visit(Type thingType) { var properties = thingType.GetProperties(BindingFlags.Public | BindingFlags.Instance) .Where(x => !IsThingProperty(x.Name)); var schema = Substitute.For <IJsonSchema>(); schema.Pattern.Returns((string)null); foreach (var property in properties) { _builder.Add(property, null, new JsonSchema(schema, null, property.PropertyType.ToJsonType(), property.Name !, _fixture.Create <bool>())); } }