-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathContentstack016_VariantsTest.cs
More file actions
258 lines (227 loc) · 10.1 KB
/
Contentstack016_VariantsTest.cs
File metadata and controls
258 lines (227 loc) · 10.1 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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
using System;
using System.Threading.Tasks;
using Contentstack.Management.Core.Models;
using Contentstack.Management.Core.Tests.Model;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace Contentstack.Management.Core.Tests.IntegrationTest
{
[TestClass]
public class Contentstack016_VariantsTest
{
private Stack _stack;
[TestInitialize]
public void Initialize()
{
StackResponse response = StackResponse.getStack(Contentstack.Client.serializer);
_stack = Contentstack.Client.Stack(response.Stack.APIKey);
}
[TestMethod]
[DoNotParallelize]
public async Task Test001_Should_Create_Variants()
{
try
{
VariantsModel variantsModel = new VariantsModel();
ContentstackResponse response = await _stack.Variants().CreateAsync(variantsModel);
Assert.AreEqual(System.Net.HttpStatusCode.Created, response.StatusCode);
}
catch (Exception e)
{
Assert.Fail(e.Message);
}
}
[TestMethod]
[DoNotParallelize]
public async Task Test002_Should_Fetch_Variants_By_Uid()
{
try
{
// First create a variant to ensure we have something to fetch
VariantsModel variantsModel = new VariantsModel();
ContentstackResponse createResponse = await _stack.Variants().CreateAsync(variantsModel);
Assert.AreEqual(System.Net.HttpStatusCode.Created, createResponse.StatusCode);
// Extract UID from created variant
var createdVariant = createResponse.OpenJObjectResponse();
string variantUid = createdVariant["variant"]["uid"].ToString();
// Test fetching by UID
ContentstackResponse fetchResponse = await _stack.Variants(variantUid).FetchAsync();
Assert.IsTrue(fetchResponse.StatusCode == System.Net.HttpStatusCode.OK ||
fetchResponse.StatusCode == System.Net.HttpStatusCode.NotFound);
}
catch (Exception e)
{
Assert.Fail(e.Message);
}
}
[TestMethod]
[DoNotParallelize]
public async Task Test003_Should_FetchByUid_Multiple_Variants()
{
try
{
// Create multiple variants first
VariantsModel variantsModel1 = new VariantsModel();
VariantsModel variantsModel2 = new VariantsModel();
ContentstackResponse createResponse1 = await _stack.Variants().CreateAsync(variantsModel1);
ContentstackResponse createResponse2 = await _stack.Variants().CreateAsync(variantsModel2);
Assert.AreEqual(System.Net.HttpStatusCode.Created, createResponse1.StatusCode);
Assert.AreEqual(System.Net.HttpStatusCode.Created, createResponse2.StatusCode);
// Extract UIDs from created variants
var createdVariant1 = createResponse1.OpenJObjectResponse();
var createdVariant2 = createResponse2.OpenJObjectResponse();
string variantUid1 = createdVariant1["variant"]["uid"].ToString();
string variantUid2 = createdVariant2["variant"]["uid"].ToString();
// Test fetching multiple variants by UIDs
string[] uids = { variantUid1, variantUid2 };
ContentstackResponse fetchResponse = await _stack.Variants().FetchByUidAsync(uids);
Assert.IsTrue(fetchResponse.StatusCode == System.Net.HttpStatusCode.OK ||
fetchResponse.StatusCode == System.Net.HttpStatusCode.NotFound);
}
catch (Exception e)
{
Assert.Fail(e.Message);
}
}
[TestMethod]
[DoNotParallelize]
public async Task Test004_Should_FetchByUid_Single_Variant()
{
try
{
// Create a variant first
VariantsModel variantsModel = new VariantsModel();
ContentstackResponse createResponse = await _stack.Variants().CreateAsync(variantsModel);
Assert.AreEqual(System.Net.HttpStatusCode.Created, createResponse.StatusCode);
// Extract UID from created variant
var createdVariant = createResponse.OpenJObjectResponse();
string variantUid = createdVariant["variant"]["uid"].ToString();
// Test fetching single variant using FetchByUid
string[] uids = { variantUid };
ContentstackResponse fetchResponse = await _stack.Variants().FetchByUidAsync(uids);
Assert.IsTrue(fetchResponse.StatusCode == System.Net.HttpStatusCode.OK ||
fetchResponse.StatusCode == System.Net.HttpStatusCode.NotFound);
}
catch (Exception e)
{
Assert.Fail(e.Message);
}
}
[TestMethod]
[DoNotParallelize]
public async Task Test005_Should_Handle_FetchByUid_With_Nonexistent_Uids()
{
try
{
// Test fetching with non-existent UIDs
string[] nonExistentUids = { "nonexistent_uid_1", "nonexistent_uid_2" };
ContentstackResponse fetchResponse = await _stack.Variants().FetchByUidAsync(nonExistentUids);
// Should return 404 or empty result, not crash
Assert.IsTrue(fetchResponse.StatusCode == System.Net.HttpStatusCode.NotFound ||
fetchResponse.StatusCode == System.Net.HttpStatusCode.OK);
}
catch (Exception e)
{
Assert.Fail(e.Message);
}
}
[TestMethod]
[DoNotParallelize]
public async Task Test006_Should_FetchByUid_Sync_Method()
{
try
{
// Create a variant first
VariantsModel variantsModel = new VariantsModel();
ContentstackResponse createResponse = _stack.Variants().Create(variantsModel);
Assert.AreEqual(System.Net.HttpStatusCode.Created, createResponse.StatusCode);
// Extract UID from created variant
var createdVariant = createResponse.OpenJObjectResponse();
string variantUid = createdVariant["variant"]["uid"].ToString();
// Test synchronous FetchByUid
string[] uids = { variantUid };
ContentstackResponse fetchResponse = _stack.Variants().FetchByUid(uids);
Assert.IsTrue(fetchResponse.StatusCode == System.Net.HttpStatusCode.OK ||
fetchResponse.StatusCode == System.Net.HttpStatusCode.NotFound);
}
catch (Exception e)
{
Assert.Fail(e.Message);
}
}
[TestMethod]
[DoNotParallelize]
public async Task Test007_Should_Delete_Variant()
{
try
{
// Create a variant first
VariantsModel variantsModel = new VariantsModel();
ContentstackResponse createResponse = await _stack.Variants().CreateAsync(variantsModel);
Assert.AreEqual(System.Net.HttpStatusCode.Created, createResponse.StatusCode);
// Extract UID from created variant
var createdVariant = createResponse.OpenJObjectResponse();
string variantUid = createdVariant["variant"]["uid"].ToString();
// Test deleting the variant
ContentstackResponse deleteResponse = await _stack.Variants(variantUid).DeleteAsync();
Assert.IsTrue(deleteResponse.StatusCode == System.Net.HttpStatusCode.OK ||
deleteResponse.StatusCode == System.Net.HttpStatusCode.NoContent ||
deleteResponse.StatusCode == System.Net.HttpStatusCode.NotFound);
}
catch (Exception e)
{
Assert.Fail(e.Message);
}
}
[TestMethod]
[DoNotParallelize]
public void Test008_Should_Validate_FetchByUid_Parameters()
{
try
{
// Test with null UIDs
Assert.ThrowsException<ArgumentException>(() => _stack.Variants().FetchByUid(null));
// Test with empty UIDs array
string[] emptyUids = new string[0];
Assert.ThrowsException<ArgumentException>(() => _stack.Variants().FetchByUid(emptyUids));
}
catch (Exception e)
{
Assert.Fail(e.Message);
}
}
[TestMethod]
[DoNotParallelize]
public async Task Test009_Should_Validate_FetchByUidAsync_Parameters()
{
try
{
// Test with null UIDs
await Assert.ThrowsExceptionAsync<ArgumentException>(() => _stack.Variants().FetchByUidAsync(null));
// Test with empty UIDs array
string[] emptyUids = new string[0];
await Assert.ThrowsExceptionAsync<ArgumentException>(() => _stack.Variants().FetchByUidAsync(emptyUids));
}
catch (Exception e)
{
Assert.Fail(e.Message);
}
}
[TestMethod]
[DoNotParallelize]
public void Test010_Should_Validate_Instance_With_Uid_Cannot_Use_FetchByUid()
{
try
{
// Test that an instance with UID cannot call FetchByUid
string instanceUid = "some_uid";
string[] uids = { "uid1", "uid2" };
Assert.ThrowsException<InvalidOperationException>(() =>
_stack.Variants(instanceUid).FetchByUid(uids));
}
catch (Exception e)
{
Assert.Fail(e.Message);
}
}
}
}