-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathTaxonomyTest.cs
More file actions
198 lines (167 loc) · 8.97 KB
/
TaxonomyTest.cs
File metadata and controls
198 lines (167 loc) · 8.97 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
using System;
using System.Net;
using AutoFixture;
using Contentstack.Management.Core.Models;
using Contentstack.Management.Core.Queryable;
using Contentstack.Management.Core.Unit.Tests.Mokes;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Newtonsoft.Json;
namespace Contentstack.Management.Core.Unit.Tests.Models
{
[TestClass]
public class TaxonomyTest
{
private Stack _stack;
private readonly IFixture _fixture = new Fixture();
private ContentstackResponse _contentstackResponse;
[TestInitialize]
public void Initialize()
{
var client = new ContentstackClient();
_contentstackResponse = MockResponse.CreateContentstackResponse("MockResponse.txt");
client.ContentstackPipeline.ReplaceHandler(new MockHttpHandler(_contentstackResponse));
client.contentstackOptions.Authtoken = _fixture.Create<string>();
_stack = new Stack(client, _fixture.Create<string>());
}
[TestMethod]
public void Initialize_Taxonomy()
{
Taxonomy taxonomy = _stack.Taxonomy();
Assert.IsNull(taxonomy.Uid);
Assert.AreEqual("/taxonomies", taxonomy.resourcePath);
Assert.ThrowsException<InvalidOperationException>(() => taxonomy.Fetch());
Assert.ThrowsExceptionAsync<InvalidOperationException>(() => taxonomy.FetchAsync());
Assert.ThrowsException<InvalidOperationException>(() => taxonomy.Update(_fixture.Create<TaxonomyModel>()));
Assert.ThrowsExceptionAsync<InvalidOperationException>(() => taxonomy.UpdateAsync(_fixture.Create<TaxonomyModel>()));
Assert.ThrowsException<InvalidOperationException>(() => taxonomy.Delete());
Assert.ThrowsExceptionAsync<InvalidOperationException>(() => taxonomy.DeleteAsync());
Assert.ThrowsException<InvalidOperationException>(() => taxonomy.Terms());
Assert.AreEqual(typeof(Query), taxonomy.Query().GetType());
}
[TestMethod]
public void Initialize_Taxonomy_With_Uid()
{
string uid = _fixture.Create<string>();
Taxonomy taxonomy = _stack.Taxonomy(uid);
Assert.AreEqual(uid, taxonomy.Uid);
Assert.AreEqual($"/taxonomies/{uid}", taxonomy.resourcePath);
Assert.ThrowsException<InvalidOperationException>(() => taxonomy.Create(_fixture.Create<TaxonomyModel>()));
Assert.ThrowsExceptionAsync<InvalidOperationException>(() => taxonomy.CreateAsync(_fixture.Create<TaxonomyModel>()));
Assert.ThrowsException<InvalidOperationException>(() => taxonomy.Query());
}
[TestMethod]
public void Should_Create_Taxonomy()
{
ContentstackResponse response = _stack.Taxonomy().Create(_fixture.Create<TaxonomyModel>());
Assert.AreEqual(_contentstackResponse.OpenResponse(), response.OpenResponse());
Assert.AreEqual(_contentstackResponse.OpenJObjectResponse().ToString(), response.OpenJObjectResponse().ToString());
}
[TestMethod]
public async System.Threading.Tasks.Task Should_Create_Taxonomy_Async()
{
ContentstackResponse response = await _stack.Taxonomy().CreateAsync(_fixture.Create<TaxonomyModel>());
Assert.AreEqual(_contentstackResponse.OpenResponse(), response.OpenResponse());
Assert.AreEqual(_contentstackResponse.OpenJObjectResponse().ToString(), response.OpenJObjectResponse().ToString());
}
[TestMethod]
public void Should_Query_Taxonomy()
{
ContentstackResponse response = _stack.Taxonomy().Query().Find();
Assert.AreEqual(_contentstackResponse.OpenResponse(), response.OpenResponse());
Assert.AreEqual(_contentstackResponse.OpenJObjectResponse().ToString(), response.OpenJObjectResponse().ToString());
}
[TestMethod]
public async System.Threading.Tasks.Task Should_Query_Taxonomy_Async()
{
ContentstackResponse response = await _stack.Taxonomy().Query().FindAsync();
Assert.AreEqual(_contentstackResponse.OpenResponse(), response.OpenResponse());
Assert.AreEqual(_contentstackResponse.OpenJObjectResponse().ToString(), response.OpenJObjectResponse().ToString());
}
[TestMethod]
public void Should_Fetch_Taxonomy()
{
ContentstackResponse response = _stack.Taxonomy(_fixture.Create<string>()).Fetch();
Assert.AreEqual(_contentstackResponse.OpenResponse(), response.OpenResponse());
Assert.AreEqual(_contentstackResponse.OpenJObjectResponse().ToString(), response.OpenJObjectResponse().ToString());
}
[TestMethod]
public async System.Threading.Tasks.Task Should_Fetch_Taxonomy_Async()
{
ContentstackResponse response = await _stack.Taxonomy(_fixture.Create<string>()).FetchAsync();
Assert.AreEqual(_contentstackResponse.OpenResponse(), response.OpenResponse());
Assert.AreEqual(_contentstackResponse.OpenJObjectResponse().ToString(), response.OpenJObjectResponse().ToString());
}
[TestMethod]
public void Should_Get_Terms_From_Taxonomy()
{
string taxonomyUid = _fixture.Create<string>();
Term terms = _stack.Taxonomy(taxonomyUid).Terms();
Assert.IsNotNull(terms);
Assert.IsNull(terms.Uid);
Assert.AreEqual($"/taxonomies/{taxonomyUid}/terms", terms.resourcePath);
}
[TestMethod]
public void Should_Get_Single_Term_From_Taxonomy()
{
string taxonomyUid = _fixture.Create<string>();
string termUid = _fixture.Create<string>();
Term term = _stack.Taxonomy(taxonomyUid).Terms(termUid);
Assert.IsNotNull(term);
Assert.AreEqual(termUid, term.Uid);
Assert.AreEqual($"/taxonomies/{taxonomyUid}/terms/{termUid}", term.resourcePath);
}
[TestMethod]
public void Export_Throws_When_Uid_Is_Empty()
{
Assert.ThrowsException<InvalidOperationException>(() => _stack.Taxonomy().Export());
Assert.ThrowsExceptionAsync<InvalidOperationException>(() => _stack.Taxonomy().ExportAsync());
}
[TestMethod]
public void Locales_Throws_When_Uid_Is_Empty()
{
Assert.ThrowsException<InvalidOperationException>(() => _stack.Taxonomy().Locales());
Assert.ThrowsExceptionAsync<InvalidOperationException>(() => _stack.Taxonomy().LocalesAsync());
}
[TestMethod]
public void Localize_Throws_When_Uid_Is_Empty()
{
Assert.ThrowsException<InvalidOperationException>(() => _stack.Taxonomy().Localize(_fixture.Create<TaxonomyModel>()));
Assert.ThrowsExceptionAsync<InvalidOperationException>(() => _stack.Taxonomy().LocalizeAsync(_fixture.Create<TaxonomyModel>()));
}
[TestMethod]
public void Import_Throws_When_Uid_Is_Set()
{
using (var stream = new System.IO.MemoryStream(System.Text.Encoding.UTF8.GetBytes("{}")))
{
var model = new TaxonomyImportModel(stream, "taxonomy.json");
Assert.ThrowsException<InvalidOperationException>(() => _stack.Taxonomy("some_uid").Import(model));
Assert.ThrowsExceptionAsync<InvalidOperationException>(() => _stack.Taxonomy("some_uid").ImportAsync(model));
}
}
[TestMethod]
public void Create_Throws_When_Uid_Is_Set()
{
Assert.ThrowsException<InvalidOperationException>(() => _stack.Taxonomy(_fixture.Create<string>()).Create(_fixture.Create<TaxonomyModel>()));
Assert.ThrowsExceptionAsync<InvalidOperationException>(() => _stack.Taxonomy(_fixture.Create<string>()).CreateAsync(_fixture.Create<TaxonomyModel>()));
}
[TestMethod]
public void Query_Throws_When_Uid_Is_Set()
{
Assert.ThrowsException<InvalidOperationException>(() => _stack.Taxonomy(_fixture.Create<string>()).Query());
}
[TestMethod]
public void Localize_When_Api_Returns_400_Returns_Unsuccessful_Response()
{
var httpMsg = MockResponse.Create(HttpStatusCode.BadRequest, null, "{\"error_message\":\"Invalid locale\",\"error_code\":400}");
var badResponse = new ContentstackResponse(httpMsg, JsonSerializer.Create(new JsonSerializerSettings()));
var client = new ContentstackClient();
client.ContentstackPipeline.ReplaceHandler(new MockHttpHandler(badResponse));
client.contentstackOptions.Authtoken = _fixture.Create<string>();
var stack = new Stack(client, _fixture.Create<string>());
string uid = _fixture.Create<string>();
ContentstackResponse response = stack.Taxonomy(uid).Localize(_fixture.Create<TaxonomyModel>());
Assert.IsFalse(response.IsSuccessStatusCode);
Assert.AreEqual(HttpStatusCode.BadRequest, response.StatusCode);
}
}
}