Skip to content

Commit 52dc968

Browse files
authored
Merge pull request #138 from contentstack/enhc/DX-4914
feat: Add full Taxonomy and Terms API to .NET CMA SDK
2 parents c186af5 + 0c4c9c8 commit 52dc968

File tree

13 files changed

+1826
-6
lines changed

13 files changed

+1826
-6
lines changed

Contentstack.Management.Core.Tests/IntegrationTest/Contentstack017_TaxonomyTest.cs

Lines changed: 744 additions & 0 deletions
Large diffs are not rendered by default.

Contentstack.Management.Core.Tests/Model/Models.cs

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using Newtonsoft.Json;
1+
using Newtonsoft.Json;
22
using Contentstack.Management.Core.Models;
33
using System.Collections.Generic;
44

@@ -25,5 +25,32 @@ public class ContentTypesModel
2525
[JsonProperty("content_types")]
2626
public List<ContentModelling> Modellings { get; set; }
2727
}
28+
29+
public class TaxonomyResponseModel
30+
{
31+
[JsonProperty("taxonomy")]
32+
public TaxonomyModel Taxonomy { get; set; }
33+
}
34+
35+
public class TaxonomiesResponseModel
36+
{
37+
[JsonProperty("taxonomies")]
38+
public List<TaxonomyModel> Taxonomies { get; set; }
39+
}
40+
41+
public class TermResponseModel
42+
{
43+
[JsonProperty("term")]
44+
public TermModel Term { get; set; }
45+
}
46+
47+
public class TermsResponseModel
48+
{
49+
[JsonProperty("terms")]
50+
public List<TermModel> Terms { get; set; }
51+
52+
[JsonProperty("count")]
53+
public int? Count { get; set; }
54+
}
2855
}
2956

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
using System;
2+
using System.IO;
3+
using Contentstack.Management.Core.Models;
4+
using Microsoft.VisualStudio.TestTools.UnitTesting;
5+
6+
namespace Contentstack.Management.Core.Unit.Tests.Models
7+
{
8+
[TestClass]
9+
public class TaxonomyImportModelTest
10+
{
11+
[TestMethod]
12+
public void Throws_When_FilePath_Is_Null()
13+
{
14+
var ex = Assert.ThrowsException<ArgumentNullException>(() => new TaxonomyImportModel((string)null));
15+
Assert.AreEqual("filePath", ex.ParamName);
16+
}
17+
18+
[TestMethod]
19+
public void Throws_When_FilePath_Is_Empty()
20+
{
21+
var ex = Assert.ThrowsException<ArgumentNullException>(() => new TaxonomyImportModel(""));
22+
Assert.AreEqual("filePath", ex.ParamName);
23+
}
24+
25+
[TestMethod]
26+
public void Throws_When_Stream_Is_Null()
27+
{
28+
var ex = Assert.ThrowsException<ArgumentNullException>(() => new TaxonomyImportModel((Stream)null, "taxonomy.json"));
29+
Assert.AreEqual("stream", ex.ParamName);
30+
}
31+
}
32+
}
Lines changed: 198 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,198 @@
1+
using System;
2+
using System.Net;
3+
using AutoFixture;
4+
using Contentstack.Management.Core.Models;
5+
using Contentstack.Management.Core.Queryable;
6+
using Contentstack.Management.Core.Unit.Tests.Mokes;
7+
using Microsoft.VisualStudio.TestTools.UnitTesting;
8+
using Newtonsoft.Json;
9+
10+
namespace Contentstack.Management.Core.Unit.Tests.Models
11+
{
12+
[TestClass]
13+
public class TaxonomyTest
14+
{
15+
private Stack _stack;
16+
private readonly IFixture _fixture = new Fixture();
17+
private ContentstackResponse _contentstackResponse;
18+
19+
[TestInitialize]
20+
public void Initialize()
21+
{
22+
var client = new ContentstackClient();
23+
_contentstackResponse = MockResponse.CreateContentstackResponse("MockResponse.txt");
24+
client.ContentstackPipeline.ReplaceHandler(new MockHttpHandler(_contentstackResponse));
25+
client.contentstackOptions.Authtoken = _fixture.Create<string>();
26+
_stack = new Stack(client, _fixture.Create<string>());
27+
}
28+
29+
[TestMethod]
30+
public void Initialize_Taxonomy()
31+
{
32+
Taxonomy taxonomy = _stack.Taxonomy();
33+
34+
Assert.IsNull(taxonomy.Uid);
35+
Assert.AreEqual("/taxonomies", taxonomy.resourcePath);
36+
Assert.ThrowsException<InvalidOperationException>(() => taxonomy.Fetch());
37+
Assert.ThrowsExceptionAsync<InvalidOperationException>(() => taxonomy.FetchAsync());
38+
Assert.ThrowsException<InvalidOperationException>(() => taxonomy.Update(_fixture.Create<TaxonomyModel>()));
39+
Assert.ThrowsExceptionAsync<InvalidOperationException>(() => taxonomy.UpdateAsync(_fixture.Create<TaxonomyModel>()));
40+
Assert.ThrowsException<InvalidOperationException>(() => taxonomy.Delete());
41+
Assert.ThrowsExceptionAsync<InvalidOperationException>(() => taxonomy.DeleteAsync());
42+
Assert.ThrowsException<InvalidOperationException>(() => taxonomy.Terms());
43+
Assert.AreEqual(typeof(Query), taxonomy.Query().GetType());
44+
}
45+
46+
[TestMethod]
47+
public void Initialize_Taxonomy_With_Uid()
48+
{
49+
string uid = _fixture.Create<string>();
50+
Taxonomy taxonomy = _stack.Taxonomy(uid);
51+
52+
Assert.AreEqual(uid, taxonomy.Uid);
53+
Assert.AreEqual($"/taxonomies/{uid}", taxonomy.resourcePath);
54+
Assert.ThrowsException<InvalidOperationException>(() => taxonomy.Create(_fixture.Create<TaxonomyModel>()));
55+
Assert.ThrowsExceptionAsync<InvalidOperationException>(() => taxonomy.CreateAsync(_fixture.Create<TaxonomyModel>()));
56+
Assert.ThrowsException<InvalidOperationException>(() => taxonomy.Query());
57+
}
58+
59+
[TestMethod]
60+
public void Should_Create_Taxonomy()
61+
{
62+
ContentstackResponse response = _stack.Taxonomy().Create(_fixture.Create<TaxonomyModel>());
63+
64+
Assert.AreEqual(_contentstackResponse.OpenResponse(), response.OpenResponse());
65+
Assert.AreEqual(_contentstackResponse.OpenJObjectResponse().ToString(), response.OpenJObjectResponse().ToString());
66+
}
67+
68+
[TestMethod]
69+
public async System.Threading.Tasks.Task Should_Create_Taxonomy_Async()
70+
{
71+
ContentstackResponse response = await _stack.Taxonomy().CreateAsync(_fixture.Create<TaxonomyModel>());
72+
73+
Assert.AreEqual(_contentstackResponse.OpenResponse(), response.OpenResponse());
74+
Assert.AreEqual(_contentstackResponse.OpenJObjectResponse().ToString(), response.OpenJObjectResponse().ToString());
75+
}
76+
77+
[TestMethod]
78+
public void Should_Query_Taxonomy()
79+
{
80+
ContentstackResponse response = _stack.Taxonomy().Query().Find();
81+
82+
Assert.AreEqual(_contentstackResponse.OpenResponse(), response.OpenResponse());
83+
Assert.AreEqual(_contentstackResponse.OpenJObjectResponse().ToString(), response.OpenJObjectResponse().ToString());
84+
}
85+
86+
[TestMethod]
87+
public async System.Threading.Tasks.Task Should_Query_Taxonomy_Async()
88+
{
89+
ContentstackResponse response = await _stack.Taxonomy().Query().FindAsync();
90+
91+
Assert.AreEqual(_contentstackResponse.OpenResponse(), response.OpenResponse());
92+
Assert.AreEqual(_contentstackResponse.OpenJObjectResponse().ToString(), response.OpenJObjectResponse().ToString());
93+
}
94+
95+
[TestMethod]
96+
public void Should_Fetch_Taxonomy()
97+
{
98+
ContentstackResponse response = _stack.Taxonomy(_fixture.Create<string>()).Fetch();
99+
100+
Assert.AreEqual(_contentstackResponse.OpenResponse(), response.OpenResponse());
101+
Assert.AreEqual(_contentstackResponse.OpenJObjectResponse().ToString(), response.OpenJObjectResponse().ToString());
102+
}
103+
104+
[TestMethod]
105+
public async System.Threading.Tasks.Task Should_Fetch_Taxonomy_Async()
106+
{
107+
ContentstackResponse response = await _stack.Taxonomy(_fixture.Create<string>()).FetchAsync();
108+
109+
Assert.AreEqual(_contentstackResponse.OpenResponse(), response.OpenResponse());
110+
Assert.AreEqual(_contentstackResponse.OpenJObjectResponse().ToString(), response.OpenJObjectResponse().ToString());
111+
}
112+
113+
[TestMethod]
114+
public void Should_Get_Terms_From_Taxonomy()
115+
{
116+
string taxonomyUid = _fixture.Create<string>();
117+
Term terms = _stack.Taxonomy(taxonomyUid).Terms();
118+
119+
Assert.IsNotNull(terms);
120+
Assert.IsNull(terms.Uid);
121+
Assert.AreEqual($"/taxonomies/{taxonomyUid}/terms", terms.resourcePath);
122+
}
123+
124+
[TestMethod]
125+
public void Should_Get_Single_Term_From_Taxonomy()
126+
{
127+
string taxonomyUid = _fixture.Create<string>();
128+
string termUid = _fixture.Create<string>();
129+
Term term = _stack.Taxonomy(taxonomyUid).Terms(termUid);
130+
131+
Assert.IsNotNull(term);
132+
Assert.AreEqual(termUid, term.Uid);
133+
Assert.AreEqual($"/taxonomies/{taxonomyUid}/terms/{termUid}", term.resourcePath);
134+
}
135+
136+
[TestMethod]
137+
public void Export_Throws_When_Uid_Is_Empty()
138+
{
139+
Assert.ThrowsException<InvalidOperationException>(() => _stack.Taxonomy().Export());
140+
Assert.ThrowsExceptionAsync<InvalidOperationException>(() => _stack.Taxonomy().ExportAsync());
141+
}
142+
143+
[TestMethod]
144+
public void Locales_Throws_When_Uid_Is_Empty()
145+
{
146+
Assert.ThrowsException<InvalidOperationException>(() => _stack.Taxonomy().Locales());
147+
Assert.ThrowsExceptionAsync<InvalidOperationException>(() => _stack.Taxonomy().LocalesAsync());
148+
}
149+
150+
[TestMethod]
151+
public void Localize_Throws_When_Uid_Is_Empty()
152+
{
153+
Assert.ThrowsException<InvalidOperationException>(() => _stack.Taxonomy().Localize(_fixture.Create<TaxonomyModel>()));
154+
Assert.ThrowsExceptionAsync<InvalidOperationException>(() => _stack.Taxonomy().LocalizeAsync(_fixture.Create<TaxonomyModel>()));
155+
}
156+
157+
[TestMethod]
158+
public void Import_Throws_When_Uid_Is_Set()
159+
{
160+
using (var stream = new System.IO.MemoryStream(System.Text.Encoding.UTF8.GetBytes("{}")))
161+
{
162+
var model = new TaxonomyImportModel(stream, "taxonomy.json");
163+
Assert.ThrowsException<InvalidOperationException>(() => _stack.Taxonomy("some_uid").Import(model));
164+
Assert.ThrowsExceptionAsync<InvalidOperationException>(() => _stack.Taxonomy("some_uid").ImportAsync(model));
165+
}
166+
}
167+
168+
[TestMethod]
169+
public void Create_Throws_When_Uid_Is_Set()
170+
{
171+
Assert.ThrowsException<InvalidOperationException>(() => _stack.Taxonomy(_fixture.Create<string>()).Create(_fixture.Create<TaxonomyModel>()));
172+
Assert.ThrowsExceptionAsync<InvalidOperationException>(() => _stack.Taxonomy(_fixture.Create<string>()).CreateAsync(_fixture.Create<TaxonomyModel>()));
173+
}
174+
175+
[TestMethod]
176+
public void Query_Throws_When_Uid_Is_Set()
177+
{
178+
Assert.ThrowsException<InvalidOperationException>(() => _stack.Taxonomy(_fixture.Create<string>()).Query());
179+
}
180+
181+
[TestMethod]
182+
public void Localize_When_Api_Returns_400_Returns_Unsuccessful_Response()
183+
{
184+
var httpMsg = MockResponse.Create(HttpStatusCode.BadRequest, null, "{\"error_message\":\"Invalid locale\",\"error_code\":400}");
185+
var badResponse = new ContentstackResponse(httpMsg, JsonSerializer.Create(new JsonSerializerSettings()));
186+
var client = new ContentstackClient();
187+
client.ContentstackPipeline.ReplaceHandler(new MockHttpHandler(badResponse));
188+
client.contentstackOptions.Authtoken = _fixture.Create<string>();
189+
var stack = new Stack(client, _fixture.Create<string>());
190+
string uid = _fixture.Create<string>();
191+
192+
ContentstackResponse response = stack.Taxonomy(uid).Localize(_fixture.Create<TaxonomyModel>());
193+
194+
Assert.IsFalse(response.IsSuccessStatusCode);
195+
Assert.AreEqual(HttpStatusCode.BadRequest, response.StatusCode);
196+
}
197+
}
198+
}

0 commit comments

Comments
 (0)