Skip to content

Commit 5d8ebaa

Browse files
committed
Added the negative test flow in both integration and unit testcases.
1 parent 130fbdd commit 5d8ebaa

File tree

4 files changed

+153
-0
lines changed

4 files changed

+153
-0
lines changed

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Threading.Tasks;
3+
using Contentstack.Management.Core.Exceptions;
34
using Contentstack.Management.Core.Models;
45
using Contentstack.Management.Core.Tests.Model;
56
using Microsoft.VisualStudio.TestTools.UnitTesting;
@@ -104,5 +105,21 @@ public void Test006_Should_Delete_Taxonomy()
104105
ContentstackResponse response = _stack.Taxonomy(_taxonomyUid).Delete();
105106
Assert.IsTrue(response.IsSuccessStatusCode, $"Delete failed: {response.OpenResponse()}");
106107
}
108+
109+
[TestMethod]
110+
[DoNotParallelize]
111+
public void Test007_Should_Throw_When_Fetch_NonExistent_Taxonomy()
112+
{
113+
Assert.ThrowsException<ContentstackErrorException>(() =>
114+
_stack.Taxonomy("non_existent_taxonomy_uid_12345").Fetch());
115+
}
116+
117+
[TestMethod]
118+
[DoNotParallelize]
119+
public void Test008_Should_Throw_When_Delete_NonExistent_Taxonomy()
120+
{
121+
Assert.ThrowsException<ContentstackErrorException>(() =>
122+
_stack.Taxonomy("non_existent_taxonomy_uid_12345").Delete());
123+
}
107124
}
108125
}
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+
}

Contentstack.Management.Core.Unit.Tests/Models/TaxonomyTest.cs

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
using System;
2+
using System.Net;
23
using AutoFixture;
34
using Contentstack.Management.Core.Models;
45
using Contentstack.Management.Core.Queryable;
56
using Contentstack.Management.Core.Unit.Tests.Mokes;
67
using Microsoft.VisualStudio.TestTools.UnitTesting;
8+
using Newtonsoft.Json;
79

810
namespace Contentstack.Management.Core.Unit.Tests.Models
911
{
@@ -130,5 +132,67 @@ public void Should_Get_Single_Term_From_Taxonomy()
130132
Assert.AreEqual(termUid, term.Uid);
131133
Assert.AreEqual($"/taxonomies/{taxonomyUid}/terms/{termUid}", term.resourcePath);
132134
}
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+
}
133197
}
134198
}

Contentstack.Management.Core.Unit.Tests/Models/TermTest.cs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,5 +134,45 @@ public async System.Threading.Tasks.Task Should_Search_Terms_Async()
134134
Assert.AreEqual(_contentstackResponse.OpenResponse(), response.OpenResponse());
135135
Assert.AreEqual(_contentstackResponse.OpenJObjectResponse().ToString(), response.OpenJObjectResponse().ToString());
136136
}
137+
138+
[TestMethod]
139+
public void Ancestors_Throws_When_Term_Uid_Is_Empty()
140+
{
141+
string taxonomyUid = _fixture.Create<string>();
142+
Assert.ThrowsException<InvalidOperationException>(() => _stack.Taxonomy(taxonomyUid).Terms().Ancestors());
143+
Assert.ThrowsExceptionAsync<InvalidOperationException>(() => _stack.Taxonomy(taxonomyUid).Terms().AncestorsAsync());
144+
}
145+
146+
[TestMethod]
147+
public void Descendants_Throws_When_Term_Uid_Is_Empty()
148+
{
149+
string taxonomyUid = _fixture.Create<string>();
150+
Assert.ThrowsException<InvalidOperationException>(() => _stack.Taxonomy(taxonomyUid).Terms().Descendants());
151+
Assert.ThrowsExceptionAsync<InvalidOperationException>(() => _stack.Taxonomy(taxonomyUid).Terms().DescendantsAsync());
152+
}
153+
154+
[TestMethod]
155+
public void Move_Throws_When_Term_Uid_Is_Empty()
156+
{
157+
string taxonomyUid = _fixture.Create<string>();
158+
Assert.ThrowsException<InvalidOperationException>(() => _stack.Taxonomy(taxonomyUid).Terms().Move(_fixture.Create<TermMoveModel>()));
159+
Assert.ThrowsExceptionAsync<InvalidOperationException>(() => _stack.Taxonomy(taxonomyUid).Terms().MoveAsync(_fixture.Create<TermMoveModel>()));
160+
}
161+
162+
[TestMethod]
163+
public void Locales_Throws_When_Term_Uid_Is_Empty()
164+
{
165+
string taxonomyUid = _fixture.Create<string>();
166+
Assert.ThrowsException<InvalidOperationException>(() => _stack.Taxonomy(taxonomyUid).Terms().Locales());
167+
Assert.ThrowsExceptionAsync<InvalidOperationException>(() => _stack.Taxonomy(taxonomyUid).Terms().LocalesAsync());
168+
}
169+
170+
[TestMethod]
171+
public void Localize_Throws_When_Term_Uid_Is_Empty()
172+
{
173+
string taxonomyUid = _fixture.Create<string>();
174+
Assert.ThrowsException<InvalidOperationException>(() => _stack.Taxonomy(taxonomyUid).Terms().Localize(_fixture.Create<TermModel>()));
175+
Assert.ThrowsExceptionAsync<InvalidOperationException>(() => _stack.Taxonomy(taxonomyUid).Terms().LocalizeAsync(_fixture.Create<TermModel>()));
176+
}
137177
}
138178
}

0 commit comments

Comments
 (0)