// Copyright (c) .NET Foundation and contributors. All rights reserved. // Licensed under the MIT license. See LICENSE file in the project root for full license information. using System; using System.Collections.Generic; using System.Linq; using System.Runtime.Versioning; using Newtonsoft.Json.Linq; using Xunit; namespace Microsoft.DotNet.ProjectModel.Server.Tests { public static class DthMessageCollectionExtension { public static IList GetMessagesByFramework(this IEnumerable messages, FrameworkName targetFramework) { return messages.Where(msg => MatchesFramework(targetFramework, msg)).ToList(); } public static IList GetMessagesByType(this IEnumerable messages, string typename) { return messages.Where(msg => string.Equals(msg.MessageType, typename)).ToList(); } public static DthMessage RetrieveSingleMessage(this IEnumerable messages, string typename) { var result = messages.SingleOrDefault(msg => string.Equals(msg.MessageType, typename, StringComparison.Ordinal)); if (result == null) { if (messages.FirstOrDefault(msg => string.Equals(msg.MessageType, typename, StringComparison.Ordinal)) != null) { Assert.False(true, $"More than one {typename} messages exist."); } else { Assert.False(true, $"{typename} message doesn't exists."); } } return result; } public static IEnumerable ContainsMessage(this IEnumerable messages, string typename) { var contain = messages.FirstOrDefault(msg => string.Equals(msg.MessageType, typename, StringComparison.Ordinal)) != null; Assert.True(contain, $"Messages collection doesn't contain message of type {typename}."); return messages; } public static IEnumerable AssertDoesNotContain(this IEnumerable messages, string typename) { var notContain = messages.FirstOrDefault(msg => string.Equals(msg.MessageType, typename, StringComparison.Ordinal)) == null; Assert.True(notContain, $"Message collection contains message of type {typename}."); return messages; } private static bool MatchesFramework(FrameworkName targetFramework, DthMessage msg) { if (msg.Payload.Type != JTokenType.Object) { return false; } var frameworkObj = msg.Payload["Framework"]; if (frameworkObj == null || !frameworkObj.HasValues) { return false; } return string.Equals(frameworkObj.Value("FrameworkName"), targetFramework.FullName, StringComparison.OrdinalIgnoreCase); } } }