|
| 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