-
Notifications
You must be signed in to change notification settings - Fork 4
feat: Add full Taxonomy and Terms API to .NET CMA SDK #138
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
125 changes: 125 additions & 0 deletions
125
Contentstack.Management.Core.Tests/IntegrationTest/Contentstack017_TaxonomyTest.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,125 @@ | ||
| using System; | ||
| using System.Threading.Tasks; | ||
| using Contentstack.Management.Core.Exceptions; | ||
| 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 Contentstack017_TaxonomyTest | ||
| { | ||
| private static string _taxonomyUid; | ||
| private Stack _stack; | ||
| private TaxonomyModel _createModel; | ||
|
|
||
| [TestInitialize] | ||
| public void Initialize() | ||
| { | ||
| StackResponse response = StackResponse.getStack(Contentstack.Client.serializer); | ||
| _stack = Contentstack.Client.Stack(response.Stack.APIKey); | ||
| if (_taxonomyUid == null) | ||
| _taxonomyUid = "taxonomy_integration_test_" + Guid.NewGuid().ToString("N").Substring(0, 8); | ||
| _createModel = new TaxonomyModel | ||
| { | ||
| Uid = _taxonomyUid, | ||
| Name = "Taxonomy Integration Test", | ||
| Description = "Description for taxonomy integration test" | ||
| }; | ||
| } | ||
|
|
||
| [TestMethod] | ||
| [DoNotParallelize] | ||
| public void Test001_Should_Create_Taxonomy() | ||
| { | ||
| ContentstackResponse response = _stack.Taxonomy().Create(_createModel); | ||
| Assert.IsTrue(response.IsSuccessStatusCode, $"Create failed: {response.OpenResponse()}"); | ||
|
|
||
| var wrapper = response.OpenTResponse<TaxonomyResponseModel>(); | ||
| Assert.IsNotNull(wrapper?.Taxonomy); | ||
| Assert.AreEqual(_createModel.Uid, wrapper.Taxonomy.Uid); | ||
| Assert.AreEqual(_createModel.Name, wrapper.Taxonomy.Name); | ||
| Assert.AreEqual(_createModel.Description, wrapper.Taxonomy.Description); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| [DoNotParallelize] | ||
| public void Test002_Should_Fetch_Taxonomy() | ||
| { | ||
| ContentstackResponse response = _stack.Taxonomy(_taxonomyUid).Fetch(); | ||
| Assert.IsTrue(response.IsSuccessStatusCode, $"Fetch failed: {response.OpenResponse()}"); | ||
|
|
||
| var wrapper = response.OpenTResponse<TaxonomyResponseModel>(); | ||
| Assert.IsNotNull(wrapper?.Taxonomy); | ||
| Assert.AreEqual(_taxonomyUid, wrapper.Taxonomy.Uid); | ||
| Assert.IsNotNull(wrapper.Taxonomy.Name); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| [DoNotParallelize] | ||
| public void Test003_Should_Query_Taxonomies() | ||
| { | ||
| ContentstackResponse response = _stack.Taxonomy().Query().Find(); | ||
| Assert.IsTrue(response.IsSuccessStatusCode, $"Query failed: {response.OpenResponse()}"); | ||
|
|
||
| var wrapper = response.OpenTResponse<TaxonomiesResponseModel>(); | ||
| Assert.IsNotNull(wrapper?.Taxonomies); | ||
| Assert.IsTrue(wrapper.Taxonomies.Count >= 0); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| [DoNotParallelize] | ||
| public void Test004_Should_Update_Taxonomy() | ||
| { | ||
| var updateModel = new TaxonomyModel | ||
| { | ||
| Name = "Taxonomy Integration Test Updated", | ||
| Description = "Updated description" | ||
| }; | ||
| ContentstackResponse response = _stack.Taxonomy(_taxonomyUid).Update(updateModel); | ||
| Assert.IsTrue(response.IsSuccessStatusCode, $"Update failed: {response.OpenResponse()}"); | ||
|
|
||
| var wrapper = response.OpenTResponse<TaxonomyResponseModel>(); | ||
| Assert.IsNotNull(wrapper?.Taxonomy); | ||
| Assert.AreEqual("Taxonomy Integration Test Updated", wrapper.Taxonomy.Name); | ||
| Assert.AreEqual("Updated description", wrapper.Taxonomy.Description); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| [DoNotParallelize] | ||
| public async Task Test005_Should_Fetch_Taxonomy_Async() | ||
| { | ||
| ContentstackResponse response = await _stack.Taxonomy(_taxonomyUid).FetchAsync(); | ||
| Assert.IsTrue(response.IsSuccessStatusCode, $"FetchAsync failed: {response.OpenResponse()}"); | ||
|
|
||
| var wrapper = response.OpenTResponse<TaxonomyResponseModel>(); | ||
| Assert.IsNotNull(wrapper?.Taxonomy); | ||
| Assert.AreEqual(_taxonomyUid, wrapper.Taxonomy.Uid); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| [DoNotParallelize] | ||
| public void Test006_Should_Delete_Taxonomy() | ||
| { | ||
| ContentstackResponse response = _stack.Taxonomy(_taxonomyUid).Delete(); | ||
| Assert.IsTrue(response.IsSuccessStatusCode, $"Delete failed: {response.OpenResponse()}"); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| [DoNotParallelize] | ||
| public void Test007_Should_Throw_When_Fetch_NonExistent_Taxonomy() | ||
| { | ||
| Assert.ThrowsException<ContentstackErrorException>(() => | ||
| _stack.Taxonomy("non_existent_taxonomy_uid_12345").Fetch()); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| [DoNotParallelize] | ||
| public void Test008_Should_Throw_When_Delete_NonExistent_Taxonomy() | ||
| { | ||
| Assert.ThrowsException<ContentstackErrorException>(() => | ||
| _stack.Taxonomy("non_existent_taxonomy_uid_12345").Delete()); | ||
| } | ||
| } | ||
|
OMpawar-21 marked this conversation as resolved.
Outdated
|
||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
Contentstack.Management.Core.Unit.Tests/Models/TaxonomyImportModelTest.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| using System; | ||
| using System.IO; | ||
| using Contentstack.Management.Core.Models; | ||
| using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
|
|
||
| namespace Contentstack.Management.Core.Unit.Tests.Models | ||
| { | ||
| [TestClass] | ||
| public class TaxonomyImportModelTest | ||
| { | ||
| [TestMethod] | ||
| public void Throws_When_FilePath_Is_Null() | ||
| { | ||
| var ex = Assert.ThrowsException<ArgumentNullException>(() => new TaxonomyImportModel((string)null)); | ||
| Assert.AreEqual("filePath", ex.ParamName); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void Throws_When_FilePath_Is_Empty() | ||
| { | ||
| var ex = Assert.ThrowsException<ArgumentNullException>(() => new TaxonomyImportModel("")); | ||
| Assert.AreEqual("filePath", ex.ParamName); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void Throws_When_Stream_Is_Null() | ||
| { | ||
| var ex = Assert.ThrowsException<ArgumentNullException>(() => new TaxonomyImportModel((Stream)null, "taxonomy.json")); | ||
| Assert.AreEqual("stream", ex.ParamName); | ||
| } | ||
| } | ||
| } |
198 changes: 198 additions & 0 deletions
198
Contentstack.Management.Core.Unit.Tests/Models/TaxonomyTest.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,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); | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.