// Copyright (c) .NET Foundation. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System; using Newtonsoft.Json.Linq; using Xunit; namespace Microsoft.DotNet.ProjectModel.Server.Tests { public static class JObjectExtensions { public static JObject AsJObject(this JToken token) { DthMessageExtension.AssertType(token, nameof(JToken)); return (JObject)token; } public static JObject RetrieveDependencyDiagnosticsErrorAt(this JObject payload, int index) { Assert.NotNull(payload); return payload.RetrievePropertyAs("Errors") .RetrieveArraryElementAs(index); } public static T RetrieveDependencyDiagnosticsErrorAt(this JObject payload, int index) where T : JToken { Assert.NotNull(payload); return payload.RetrievePropertyAs("Errors") .RetrieveArraryElementAs(index); } public static T RetrievePropertyAs(this JObject json, string propertyName) where T : JToken { Assert.NotNull(json); var property = json[propertyName]; Assert.NotNull(property); DthMessageExtension.AssertType(property, $"Property {propertyName}"); return (T)property; } public static JObject AssertProperty(this JObject json, string propertyName, T expectedPropertyValue) { Assert.NotNull(json); var property = json[propertyName]; Assert.NotNull(property); Assert.Equal(expectedPropertyValue, property.Value()); return json; } public static JObject AssertProperty(this JObject json, string propertyName, Func assertion) { return AssertProperty(json, propertyName, assertion, value => $"Assert failed on {propertyName}."); } public static JObject AssertProperty(this JObject json, string propertyName, Func assertion, Func errorMessage) { Assert.NotNull(json); var property = json[propertyName]; Assert.False(property == null, $"Property {propertyName} doesn't exist."); var propertyValue = property.Value(); Assert.False(propertyValue == null, $"Property {propertyName} of type {typeof(T).Name} doesn't exist."); Assert.True(assertion(propertyValue), errorMessage(propertyValue)); return json; } } }