|
| 1 | +using System; |
| 2 | +using System.Linq; |
| 3 | +using System.Net; |
| 4 | +using System.Threading.Tasks; |
| 5 | +using Contentstack.Management.Core.Exceptions; |
| 6 | +using Microsoft.VisualStudio.TestTools.UnitTesting; |
| 7 | + |
| 8 | +namespace Contentstack.Management.Core.Tests.Helpers |
| 9 | +{ |
| 10 | + public static class AssertLogger |
| 11 | + { |
| 12 | + public static void IsNotNull(object value, string name = "") |
| 13 | + { |
| 14 | + bool passed = value != null; |
| 15 | + TestOutputLogger.LogAssertion($"IsNotNull({name})", "NotNull", value?.ToString() ?? "null", passed); |
| 16 | + Assert.IsNotNull(value); |
| 17 | + } |
| 18 | + |
| 19 | + public static void IsNull(object value, string name = "") |
| 20 | + { |
| 21 | + bool passed = value == null; |
| 22 | + TestOutputLogger.LogAssertion($"IsNull({name})", "null", value?.ToString() ?? "null", passed); |
| 23 | + Assert.IsNull(value); |
| 24 | + } |
| 25 | + |
| 26 | + public static void AreEqual<T>(T expected, T actual, string name = "") |
| 27 | + { |
| 28 | + bool passed = Equals(expected, actual); |
| 29 | + TestOutputLogger.LogAssertion($"AreEqual({name})", expected?.ToString() ?? "null", actual?.ToString() ?? "null", passed); |
| 30 | + Assert.AreEqual(expected, actual); |
| 31 | + } |
| 32 | + |
| 33 | + public static void AreEqual<T>(T expected, T actual, string message, string name) |
| 34 | + { |
| 35 | + bool passed = Equals(expected, actual); |
| 36 | + TestOutputLogger.LogAssertion($"AreEqual({name})", expected?.ToString() ?? "null", actual?.ToString() ?? "null", passed); |
| 37 | + Assert.AreEqual(expected, actual, message); |
| 38 | + } |
| 39 | + |
| 40 | + public static void IsTrue(bool condition, string name = "") |
| 41 | + { |
| 42 | + TestOutputLogger.LogAssertion($"IsTrue({name})", "True", condition.ToString(), condition); |
| 43 | + Assert.IsTrue(condition); |
| 44 | + } |
| 45 | + |
| 46 | + public static void IsTrue(bool condition, string message, string name) |
| 47 | + { |
| 48 | + TestOutputLogger.LogAssertion($"IsTrue({name})", "True", condition.ToString(), condition); |
| 49 | + Assert.IsTrue(condition, message); |
| 50 | + } |
| 51 | + |
| 52 | + public static void IsFalse(bool condition, string name = "") |
| 53 | + { |
| 54 | + TestOutputLogger.LogAssertion($"IsFalse({name})", "False", condition.ToString(), !condition); |
| 55 | + Assert.IsFalse(condition); |
| 56 | + } |
| 57 | + |
| 58 | + public static void IsFalse(bool condition, string message, string name) |
| 59 | + { |
| 60 | + TestOutputLogger.LogAssertion($"IsFalse({name})", "False", condition.ToString(), !condition); |
| 61 | + Assert.IsFalse(condition, message); |
| 62 | + } |
| 63 | + |
| 64 | + public static void IsInstanceOfType(object value, Type expectedType, string name = "") |
| 65 | + { |
| 66 | + bool passed = value != null && expectedType.IsInstanceOfType(value); |
| 67 | + TestOutputLogger.LogAssertion( |
| 68 | + $"IsInstanceOfType({name})", |
| 69 | + expectedType?.Name ?? "null", |
| 70 | + value?.GetType()?.Name ?? "null", |
| 71 | + passed); |
| 72 | + Assert.IsInstanceOfType(value, expectedType); |
| 73 | + } |
| 74 | + |
| 75 | + public static T ThrowsException<T>(Action action, string name = "") where T : Exception |
| 76 | + { |
| 77 | + try |
| 78 | + { |
| 79 | + action(); |
| 80 | + TestOutputLogger.LogAssertion($"ThrowsException<{typeof(T).Name}>({name})", typeof(T).Name, "NoException", false); |
| 81 | + throw new AssertFailedException($"Expected exception {typeof(T).Name} was not thrown."); |
| 82 | + } |
| 83 | + catch (T ex) |
| 84 | + { |
| 85 | + TestOutputLogger.LogAssertion($"ThrowsException<{typeof(T).Name}>({name})", typeof(T).Name, typeof(T).Name, true); |
| 86 | + return ex; |
| 87 | + } |
| 88 | + catch (AssertFailedException) |
| 89 | + { |
| 90 | + throw; |
| 91 | + } |
| 92 | + catch (Exception ex) |
| 93 | + { |
| 94 | + TestOutputLogger.LogAssertion($"ThrowsException<{typeof(T).Name}>({name})", typeof(T).Name, ex.GetType().Name, false); |
| 95 | + throw new AssertFailedException( |
| 96 | + $"Expected exception {typeof(T).Name} but got {ex.GetType().Name}: {ex.Message}", ex); |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + public static void Fail(string message) |
| 101 | + { |
| 102 | + TestOutputLogger.LogAssertion("Fail", "N/A", message ?? "", false); |
| 103 | + Assert.Fail(message); |
| 104 | + } |
| 105 | + |
| 106 | + public static void Fail(string message, params object[] parameters) |
| 107 | + { |
| 108 | + TestOutputLogger.LogAssertion("Fail", "N/A", message ?? "", false); |
| 109 | + Assert.Fail(message, parameters); |
| 110 | + } |
| 111 | + |
| 112 | + public static void Inconclusive(string message) |
| 113 | + { |
| 114 | + TestOutputLogger.LogAssertion("Inconclusive", "N/A", message ?? "", false); |
| 115 | + Assert.Inconclusive(message); |
| 116 | + } |
| 117 | + |
| 118 | + /// <summary> |
| 119 | + /// Asserts a Contentstack API error with an HTTP status in the allowed set. |
| 120 | + /// </summary> |
| 121 | + public static ContentstackErrorException ThrowsContentstackError(Action action, string name, params HttpStatusCode[] acceptableStatuses) |
| 122 | + { |
| 123 | + var ex = ThrowsException<ContentstackErrorException>(action, name); |
| 124 | + IsTrue( |
| 125 | + acceptableStatuses.Contains(ex.StatusCode), |
| 126 | + $"Expected one of [{string.Join(", ", acceptableStatuses)}] but was {ex.StatusCode}", |
| 127 | + "statusCode"); |
| 128 | + return ex; |
| 129 | + } |
| 130 | + |
| 131 | + /// <summary> |
| 132 | + /// Async variant: runs the task and expects <see cref="ContentstackErrorException"/> with an allowed status. |
| 133 | + /// </summary> |
| 134 | + public static async Task<ContentstackErrorException> ThrowsContentstackErrorAsync(Func<Task> action, string name, params HttpStatusCode[] acceptableStatuses) |
| 135 | + { |
| 136 | + try |
| 137 | + { |
| 138 | + await action(); |
| 139 | + TestOutputLogger.LogAssertion($"ThrowsContentstackErrorAsync({name})", "ContentstackErrorException", "NoException", false); |
| 140 | + throw new AssertFailedException($"Expected exception ContentstackErrorException was not thrown."); |
| 141 | + } |
| 142 | + catch (ContentstackErrorException ex) |
| 143 | + { |
| 144 | + IsTrue( |
| 145 | + acceptableStatuses.Contains(ex.StatusCode), |
| 146 | + $"Expected one of [{string.Join(", ", acceptableStatuses)}] but was {ex.StatusCode}", |
| 147 | + "statusCode"); |
| 148 | + TestOutputLogger.LogAssertion($"ThrowsContentstackErrorAsync({name})", nameof(ContentstackErrorException), ex.StatusCode.ToString(), true); |
| 149 | + return ex; |
| 150 | + } |
| 151 | + catch (AssertFailedException) |
| 152 | + { |
| 153 | + throw; |
| 154 | + } |
| 155 | + catch (Exception ex) |
| 156 | + { |
| 157 | + TestOutputLogger.LogAssertion($"ThrowsContentstackErrorAsync({name})", nameof(ContentstackErrorException), ex.GetType().Name, false); |
| 158 | + throw new AssertFailedException( |
| 159 | + $"Expected exception ContentstackErrorException but got {ex.GetType().Name}: {ex.Message}", ex); |
| 160 | + } |
| 161 | + } |
| 162 | + } |
| 163 | +} |
0 commit comments