|
| 1 | +using System; |
| 2 | +using Microsoft.VisualStudio.TestTools.UnitTesting; |
| 3 | +using Contentstack.Management.Core.Exceptions; |
| 4 | + |
| 5 | +namespace Contentstack.Management.Core.Unit.Tests.OAuth |
| 6 | +{ |
| 7 | + [TestClass] |
| 8 | + public class OAuthExceptionTest |
| 9 | + { |
| 10 | + [TestMethod] |
| 11 | + public void OAuthException_DefaultConstructor_ShouldUseDefaultMessage() |
| 12 | + { |
| 13 | + // Act |
| 14 | + var exception = new OAuthException(); |
| 15 | + |
| 16 | + // Assert |
| 17 | + Assert.AreEqual("OAuth operation failed.", exception.Message); |
| 18 | + Assert.IsNull(exception.InnerException); |
| 19 | + } |
| 20 | + |
| 21 | + [TestMethod] |
| 22 | + public void OAuthException_WithMessage_ShouldUseProvidedMessage() |
| 23 | + { |
| 24 | + // Arrange |
| 25 | + var message = "Custom OAuth error message"; |
| 26 | + |
| 27 | + // Act |
| 28 | + var exception = new OAuthException(message); |
| 29 | + |
| 30 | + // Assert |
| 31 | + Assert.AreEqual(message, exception.Message); |
| 32 | + Assert.IsNull(exception.InnerException); |
| 33 | + } |
| 34 | + |
| 35 | + [TestMethod] |
| 36 | + public void OAuthException_WithMessageAndInnerException_ShouldUseBoth() |
| 37 | + { |
| 38 | + // Arrange |
| 39 | + var message = "Custom OAuth error message"; |
| 40 | + var innerException = new InvalidOperationException("Inner exception"); |
| 41 | + |
| 42 | + // Act |
| 43 | + var exception = new OAuthException(message, innerException); |
| 44 | + |
| 45 | + // Assert |
| 46 | + Assert.AreEqual(message, exception.Message); |
| 47 | + Assert.AreEqual(innerException, exception.InnerException); |
| 48 | + } |
| 49 | + |
| 50 | + [TestMethod] |
| 51 | + public void OAuthConfigurationException_DefaultConstructor_ShouldUseDefaultMessage() |
| 52 | + { |
| 53 | + // Act |
| 54 | + var exception = new OAuthConfigurationException(); |
| 55 | + |
| 56 | + // Assert |
| 57 | + Assert.AreEqual("OAuth configuration is invalid.", exception.Message); |
| 58 | + Assert.IsNull(exception.InnerException); |
| 59 | + } |
| 60 | + |
| 61 | + [TestMethod] |
| 62 | + public void OAuthConfigurationException_WithMessage_ShouldUseProvidedMessage() |
| 63 | + { |
| 64 | + // Arrange |
| 65 | + var message = "Custom configuration error message"; |
| 66 | + |
| 67 | + // Act |
| 68 | + var exception = new OAuthConfigurationException(message); |
| 69 | + |
| 70 | + // Assert |
| 71 | + Assert.AreEqual(message, exception.Message); |
| 72 | + Assert.IsNull(exception.InnerException); |
| 73 | + } |
| 74 | + |
| 75 | + [TestMethod] |
| 76 | + public void OAuthConfigurationException_WithMessageAndInnerException_ShouldUseBoth() |
| 77 | + { |
| 78 | + // Arrange |
| 79 | + var message = "Custom configuration error message"; |
| 80 | + var innerException = new ArgumentException("Inner exception"); |
| 81 | + |
| 82 | + // Act |
| 83 | + var exception = new OAuthConfigurationException(message, innerException); |
| 84 | + |
| 85 | + // Assert |
| 86 | + Assert.AreEqual(message, exception.Message); |
| 87 | + Assert.AreEqual(innerException, exception.InnerException); |
| 88 | + } |
| 89 | + |
| 90 | + [TestMethod] |
| 91 | + public void OAuthTokenException_DefaultConstructor_ShouldUseDefaultMessage() |
| 92 | + { |
| 93 | + // Act |
| 94 | + var exception = new OAuthTokenException(); |
| 95 | + |
| 96 | + // Assert |
| 97 | + Assert.AreEqual("OAuth token operation failed.", exception.Message); |
| 98 | + Assert.IsNull(exception.InnerException); |
| 99 | + } |
| 100 | + |
| 101 | + [TestMethod] |
| 102 | + public void OAuthTokenException_WithMessage_ShouldUseProvidedMessage() |
| 103 | + { |
| 104 | + // Arrange |
| 105 | + var message = "Custom token error message"; |
| 106 | + |
| 107 | + // Act |
| 108 | + var exception = new OAuthTokenException(message); |
| 109 | + |
| 110 | + // Assert |
| 111 | + Assert.AreEqual(message, exception.Message); |
| 112 | + Assert.IsNull(exception.InnerException); |
| 113 | + } |
| 114 | + |
| 115 | + [TestMethod] |
| 116 | + public void OAuthTokenException_WithMessageAndInnerException_ShouldUseBoth() |
| 117 | + { |
| 118 | + // Arrange |
| 119 | + var message = "Custom token error message"; |
| 120 | + var innerException = new InvalidOperationException("Inner exception"); |
| 121 | + |
| 122 | + // Act |
| 123 | + var exception = new OAuthTokenException(message, innerException); |
| 124 | + |
| 125 | + // Assert |
| 126 | + Assert.AreEqual(message, exception.Message); |
| 127 | + Assert.AreEqual(innerException, exception.InnerException); |
| 128 | + } |
| 129 | + |
| 130 | + [TestMethod] |
| 131 | + public void OAuthAuthorizationException_DefaultConstructor_ShouldUseDefaultMessage() |
| 132 | + { |
| 133 | + // Act |
| 134 | + var exception = new OAuthAuthorizationException(); |
| 135 | + |
| 136 | + // Assert |
| 137 | + Assert.AreEqual("OAuth authorization failed.", exception.Message); |
| 138 | + Assert.IsNull(exception.InnerException); |
| 139 | + } |
| 140 | + |
| 141 | + [TestMethod] |
| 142 | + public void OAuthAuthorizationException_WithMessage_ShouldUseProvidedMessage() |
| 143 | + { |
| 144 | + // Arrange |
| 145 | + var message = "Custom authorization error message"; |
| 146 | + |
| 147 | + // Act |
| 148 | + var exception = new OAuthAuthorizationException(message); |
| 149 | + |
| 150 | + // Assert |
| 151 | + Assert.AreEqual(message, exception.Message); |
| 152 | + Assert.IsNull(exception.InnerException); |
| 153 | + } |
| 154 | + |
| 155 | + [TestMethod] |
| 156 | + public void OAuthAuthorizationException_WithMessageAndInnerException_ShouldUseBoth() |
| 157 | + { |
| 158 | + // Arrange |
| 159 | + var message = "Custom authorization error message"; |
| 160 | + var innerException = new InvalidOperationException("Inner exception"); |
| 161 | + |
| 162 | + // Act |
| 163 | + var exception = new OAuthAuthorizationException(message, innerException); |
| 164 | + |
| 165 | + // Assert |
| 166 | + Assert.AreEqual(message, exception.Message); |
| 167 | + Assert.AreEqual(innerException, exception.InnerException); |
| 168 | + } |
| 169 | + |
| 170 | + [TestMethod] |
| 171 | + public void OAuthTokenRefreshException_DefaultConstructor_ShouldUseDefaultMessage() |
| 172 | + { |
| 173 | + // Act |
| 174 | + var exception = new OAuthTokenRefreshException(); |
| 175 | + |
| 176 | + // Assert |
| 177 | + Assert.AreEqual("OAuth token refresh failed.", exception.Message); |
| 178 | + Assert.IsNull(exception.InnerException); |
| 179 | + } |
| 180 | + |
| 181 | + [TestMethod] |
| 182 | + public void OAuthTokenRefreshException_WithMessage_ShouldUseProvidedMessage() |
| 183 | + { |
| 184 | + // Arrange |
| 185 | + var message = "Custom refresh error message"; |
| 186 | + |
| 187 | + // Act |
| 188 | + var exception = new OAuthTokenRefreshException(message); |
| 189 | + |
| 190 | + // Assert |
| 191 | + Assert.AreEqual(message, exception.Message); |
| 192 | + Assert.IsNull(exception.InnerException); |
| 193 | + } |
| 194 | + |
| 195 | + [TestMethod] |
| 196 | + public void OAuthTokenRefreshException_WithMessageAndInnerException_ShouldUseBoth() |
| 197 | + { |
| 198 | + // Arrange |
| 199 | + var message = "Custom refresh error message"; |
| 200 | + var innerException = new InvalidOperationException("Inner exception"); |
| 201 | + |
| 202 | + // Act |
| 203 | + var exception = new OAuthTokenRefreshException(message, innerException); |
| 204 | + |
| 205 | + // Assert |
| 206 | + Assert.AreEqual(message, exception.Message); |
| 207 | + Assert.AreEqual(innerException, exception.InnerException); |
| 208 | + } |
| 209 | + |
| 210 | + [TestMethod] |
| 211 | + public void OAuthException_Inheritance_ShouldBeCorrect() |
| 212 | + { |
| 213 | + // Act |
| 214 | + var oauthException = new OAuthException(); |
| 215 | + var configException = new OAuthConfigurationException(); |
| 216 | + var tokenException = new OAuthTokenException(); |
| 217 | + var authException = new OAuthAuthorizationException(); |
| 218 | + var refreshException = new OAuthTokenRefreshException(); |
| 219 | + |
| 220 | + // Assert |
| 221 | + Assert.IsInstanceOfType(oauthException, typeof(Exception)); |
| 222 | + Assert.IsInstanceOfType(configException, typeof(OAuthException)); |
| 223 | + Assert.IsInstanceOfType(tokenException, typeof(OAuthException)); |
| 224 | + Assert.IsInstanceOfType(authException, typeof(OAuthException)); |
| 225 | + Assert.IsInstanceOfType(refreshException, typeof(OAuthTokenException)); |
| 226 | + } |
| 227 | + |
| 228 | + [TestMethod] |
| 229 | + public void OAuthException_Serialization_ShouldWork() |
| 230 | + { |
| 231 | + // Arrange |
| 232 | + var originalException = new OAuthException("Test message", new InvalidOperationException("Inner")); |
| 233 | + |
| 234 | + |
| 235 | + Assert.IsNotNull(originalException); |
| 236 | + Assert.AreEqual("Test message", originalException.Message); |
| 237 | + Assert.IsNotNull(originalException.InnerException); |
| 238 | + } |
| 239 | + |
| 240 | + [TestMethod] |
| 241 | + public void OAuthException_ToString_ShouldIncludeMessage() |
| 242 | + { |
| 243 | + // Arrange |
| 244 | + var message = "Test OAuth error message"; |
| 245 | + var exception = new OAuthException(message); |
| 246 | + |
| 247 | + // Act |
| 248 | + var result = exception.ToString(); |
| 249 | + |
| 250 | + // Assert |
| 251 | + Assert.IsTrue(result.Contains(message)); |
| 252 | + Assert.IsTrue(result.Contains("OAuthException")); |
| 253 | + } |
| 254 | + |
| 255 | + [TestMethod] |
| 256 | + public void OAuthException_WithInnerException_ToString_ShouldIncludeBoth() |
| 257 | + { |
| 258 | + // Arrange |
| 259 | + var message = "Test OAuth error message"; |
| 260 | + var innerMessage = "Inner exception message"; |
| 261 | + var innerException = new InvalidOperationException(innerMessage); |
| 262 | + var exception = new OAuthException(message, innerException); |
| 263 | + |
| 264 | + // Act |
| 265 | + var result = exception.ToString(); |
| 266 | + |
| 267 | + // Assert |
| 268 | + Assert.IsTrue(result.Contains(message)); |
| 269 | + Assert.IsTrue(result.Contains(innerMessage)); |
| 270 | + Assert.IsTrue(result.Contains("OAuthException")); |
| 271 | + Assert.IsTrue(result.Contains("InvalidOperationException")); |
| 272 | + } |
| 273 | + } |
| 274 | +} |
| 275 | + |
0 commit comments