-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathContentstackExceptionUnitTests.cs
More file actions
151 lines (123 loc) · 4.35 KB
/
ContentstackExceptionUnitTests.cs
File metadata and controls
151 lines (123 loc) · 4.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
using System;
using System.Collections.Generic;
using System.Net;
using AutoFixture;
using Contentstack.Core.Internals;
using Xunit;
namespace Contentstack.Core.Unit.Tests
{
/// <summary>
/// Unit tests for ContentstackException class - uses mocks and AutoFixture, no real API calls
/// </summary>
public class ContentstackExceptionUnitTests
{
private readonly IFixture _fixture = new Fixture();
#region Initialization Tests
[Fact]
public void ContentstackException_DefaultConstructor_InitializesCorrectly()
{
// Act
var exception = new ContentstackException();
// Assert
Assert.NotNull(exception);
// Message property from base Exception class is set to a default value
Assert.NotNull(exception.Message);
Assert.Equal(0, exception.ErrorCode);
Assert.Null(exception.Errors);
}
[Fact]
public void ContentstackException_WithErrorMessage_InitializesCorrectly()
{
// Arrange
var errorMessage = _fixture.Create<string>();
// Act
var exception = new ContentstackException(errorMessage);
// Assert
Assert.NotNull(exception);
Assert.Equal(errorMessage, exception.Message);
}
[Fact]
public void ContentstackException_WithException_InitializesCorrectly()
{
// Arrange
var sourceException = new Exception("Source exception message");
// Act
var exception = new ContentstackException(sourceException);
// Assert
Assert.NotNull(exception);
Assert.Equal(sourceException.Message, exception.Message);
// The constructor sets the sourceException as InnerException
Assert.Equal(sourceException, exception.InnerException);
}
#endregion
#region Property Tests
[Fact]
public void ErrorCode_SetAndGet_ReturnsCorrectValue()
{
// Arrange
var exception = new ContentstackException();
var errorCode = _fixture.Create<int>();
// Act
exception.ErrorCode = errorCode;
// Assert
Assert.Equal(errorCode, exception.ErrorCode);
}
[Fact]
public void StatusCode_SetAndGet_ReturnsCorrectValue()
{
// Arrange
var exception = new ContentstackException();
var statusCode = HttpStatusCode.BadRequest;
// Act
exception.StatusCode = statusCode;
// Assert
Assert.Equal(statusCode, exception.StatusCode);
}
[Fact]
public void Errors_SetAndGet_ReturnsCorrectValue()
{
// Arrange
var exception = new ContentstackException();
var errors = new Dictionary<string, object>
{
{ "field1", "Error message 1" },
{ "field2", "Error message 2" }
};
// Act
exception.Errors = errors;
// Assert
Assert.NotNull(exception.Errors);
Assert.Equal(2, exception.Errors.Count);
Assert.Equal("Error message 1", exception.Errors["field1"]);
Assert.Equal("Error message 2", exception.Errors["field2"]);
}
#endregion
#region Complete Exception Tests
[Fact]
public void ContentstackException_WithAllPropertiesSet_ReturnsAllValues()
{
// Arrange
var errorMessage = _fixture.Create<string>();
var errorCode = 404;
var statusCode = HttpStatusCode.NotFound;
var errors = new Dictionary<string, object>
{
{ "error", "Not found" }
};
// Act
var exception = new ContentstackException(errorMessage)
{
ErrorCode = errorCode,
StatusCode = statusCode,
Errors = errors
};
// Assert
Assert.Equal(errorMessage, exception.Message);
Assert.Equal(errorCode, exception.ErrorCode);
Assert.Equal(statusCode, exception.StatusCode);
Assert.NotNull(exception.Errors);
Assert.Single(exception.Errors);
}
#endregion
}
}