public static async Task RegisteredType_AdditionalLink( Guid id, Guid parentId, UnequalNonNullPair <NonEmptyString> routes, JsonSerializerSettings serializerSettings) { if (serializerSettings is null) { throw new ArgumentNullException(nameof(serializerSettings)); } var dto = new Registered { Id = id, ParentId = parentId, }; var(route, parentRoute) = routes; var builder = new TransformationMap.Builder <Registered>(r => Const(new Uri($"https://example.invalid/registered/{r.Id}"))); ITransformationMap <Registered> transformationMap = builder; _ = transformationMap.Link("up", r => Const(new Uri($"https://example.invalid/parent/{r.ParentId}"))); var map = new Dictionary <Type, ITransformationInstructions> { [typeof(Registered)] = builder, }; await using var serviceProvider = new ServiceCollection() .AddScoped <ILinkBuilder <Constant>, LinkBuilder.Constant>() .BuildServiceProvider(); var repo = new HalRepository(map, serviceProvider); serializerSettings.Converters.Add(new LinkCollection.Converter()); var sut = new HalJsonOutputFormatter(serializerSettings, ArrayPool <char> .Shared, new MvcOptions(), repo); using var writer = new StringWriter(); var context = new OutputFormatterWriteContext( new DefaultHttpContext(), (_, _) => writer, typeof(Registered), dto); await sut.WriteResponseBodyAsync(context, new UTF8Encoding(encoderShouldEmitUTF8Identifier : false)).ConfigureAwait(false); var actual = JsonConvert.DeserializeObject <HollowHal>(writer.ToString(), serializerSettings); Assert.NotNull(actual); Assert.Empty(actual.Embedded); Assert.NotNull(actual.Links); Assert.Equal(2, actual.Links.Count); var self = Assert.Contains("self", actual.Links); Assert.NotNull(self); Assert.Equal($"https://example.invalid/registered/{id}", self.Href); var parent = Assert.Contains("up", actual.Links); Assert.NotNull(parent); Assert.Equal($"https://example.invalid/parent/{parentId}", parent.Href); }
public static void SimpleProperty_LinkAndIgnored(NonEmptyString relation) { var builder = new TransformationMap.Builder <Linker>(l => Const(l.Id)); ITransformationMap <Linker> transformationMap = builder; transformationMap.LinkAndIgnore(relation.Get, l => l.Link); ITransformationInstructions transformationInstructions = builder; Assert.Single(transformationInstructions.IgnoreInstructions); }
public static void SimpleProperty_Ignored() { var builder = new TransformationMap.Builder <Linker>(l => Const(l.Id)); ITransformationMap <Linker> transformationMap = builder; transformationMap.Ignore(l => l.Link); ITransformationInstructions transformationInstructions = builder; Assert.Single(transformationInstructions.IgnoreInstructions); }
public static async Task RegisteredType_NullLink( Guid id, Guid parentId, UnequalNonNullPair <NonEmptyString> routes, JsonSerializerSettings serializerSettings) { // arrange var dto = new Registered { Id = id, ParentId = parentId }; var(route, parentRoute) = routes; var builder = new TransformationMap.Builder <Registered>(r => Const(new Uri($"https://example.invalid/registered/{r.Id}"))); ITransformationMap <Registered> transformationMap = builder; transformationMap.Link("up", _ => (ILinkData)null); var map = new Dictionary <Type, ITransformationInstructions> { [typeof(Registered)] = builder }; var serviceProvider = new ServiceCollection() .AddScoped <ILinkBuilder <Constant>, LinkBuilder.Constant>() .BuildServiceProvider(); var repo = new HalRepository(map, serviceProvider); serializerSettings.Converters.Add(new LinkCollection.Converter()); var sut = new HalJsonOutputFormatter(serializerSettings, ArrayPool <char> .Shared, repo); var writer = new StringWriter(); var context = new OutputFormatterWriteContext( new DefaultHttpContext(), (_, __) => writer, typeof(Registered), dto); // act await sut.WriteResponseBodyAsync(context, new UTF8Encoding(encoderShouldEmitUTF8Identifier : false)).ConfigureAwait(false); var actual = JsonConvert.DeserializeObject <HollowHal>(writer.ToString(), serializerSettings); // assert Assert.NotNull(actual); Assert.Empty(actual.Embedded); Assert.NotNull(actual.Links); var(rel, link) = Assert.Single(actual.Links); Assert.Equal("self", rel); Assert.Equal($"https://example.invalid/registered/{id}", link.Href); }
public static void SimpleProperty_LinkAndIgnored(NonEmptyString relation) { if (relation is null) { throw new ArgumentNullException(nameof(relation)); } var builder = new TransformationMap.Builder <Linker>(l => Const(l.Id)); ITransformationMap <Linker> transformationMap = builder; _ = transformationMap.LinkAndIgnore(relation.Get, l => l.Link); ITransformationInstructions transformationInstructions = builder; _ = Assert.Single(transformationInstructions.IgnoreInstructions); }