|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.IO; |
| 4 | +using Newtonsoft.Json.Linq; |
| 5 | +using Xunit; |
| 6 | + |
| 7 | +namespace Contentstack.Utils.Tests |
| 8 | +{ |
| 9 | + public class VariantAliasesTest |
| 10 | + { |
| 11 | + private static JObject ReadJsonRoot(string fileName) |
| 12 | + { |
| 13 | + var path = Path.Combine(AppContext.BaseDirectory, "Resources", fileName); |
| 14 | + return JObject.Parse(File.ReadAllText(path)); |
| 15 | + } |
| 16 | + |
| 17 | + private static HashSet<string> JsonArrayToStringSet(JArray arr) |
| 18 | + { |
| 19 | + var set = new HashSet<string>(); |
| 20 | + foreach (var t in arr) |
| 21 | + { |
| 22 | + set.Add(t.ToString()); |
| 23 | + } |
| 24 | + return set; |
| 25 | + } |
| 26 | + |
| 27 | + [Fact] |
| 28 | + public void GetVariantAliases_SingleEntry_ReturnsAliases() |
| 29 | + { |
| 30 | + JObject full = ReadJsonRoot("variantsSingleEntry.json"); |
| 31 | + JObject entry = (JObject)full["entry"]; |
| 32 | + const string contentTypeUid = "movie"; |
| 33 | + |
| 34 | + JObject result = Utils.GetVariantAliases(entry, contentTypeUid); |
| 35 | + |
| 36 | + Assert.True(result["entry_uid"] != null && !string.IsNullOrEmpty(result["entry_uid"].ToString())); |
| 37 | + Assert.Equal(contentTypeUid, result["contenttype_uid"].ToString()); |
| 38 | + JArray variants = (JArray)result["variants"]; |
| 39 | + Assert.NotNull(variants); |
| 40 | + var aliasSet = JsonArrayToStringSet(variants); |
| 41 | + Assert.Equal( |
| 42 | + new HashSet<string> { "cs_personalize_0_0", "cs_personalize_0_3" }, |
| 43 | + aliasSet); |
| 44 | + } |
| 45 | + |
| 46 | + [Fact] |
| 47 | + public void GetDataCsvariantsAttribute_SingleEntry_ReturnsJsonArrayString() |
| 48 | + { |
| 49 | + JObject full = ReadJsonRoot("variantsSingleEntry.json"); |
| 50 | + JObject entry = (JObject)full["entry"]; |
| 51 | + const string contentTypeUid = "movie"; |
| 52 | + |
| 53 | + JObject result = Utils.GetDataCsvariantsAttribute(entry, contentTypeUid); |
| 54 | + |
| 55 | + Assert.True(result["data-csvariants"] != null); |
| 56 | + string dataCsvariantsStr = result["data-csvariants"].ToString(); |
| 57 | + JArray arr = JArray.Parse(dataCsvariantsStr); |
| 58 | + Assert.Single(arr); |
| 59 | + JObject first = (JObject)arr[0]; |
| 60 | + Assert.True(first["entry_uid"] != null && !string.IsNullOrEmpty(first["entry_uid"].ToString())); |
| 61 | + Assert.Equal(contentTypeUid, first["contenttype_uid"].ToString()); |
| 62 | + var aliasSet = JsonArrayToStringSet((JArray)first["variants"]); |
| 63 | + Assert.Equal( |
| 64 | + new HashSet<string> { "cs_personalize_0_0", "cs_personalize_0_3" }, |
| 65 | + aliasSet); |
| 66 | + } |
| 67 | + |
| 68 | + [Fact] |
| 69 | + public void GetVariantAliases_MultipleEntries_ReturnsOneResultPerEntryWithUid() |
| 70 | + { |
| 71 | + JObject full = ReadJsonRoot("variantsEntries.json"); |
| 72 | + JArray entries = (JArray)full["entries"]; |
| 73 | + const string contentTypeUid = "movie"; |
| 74 | + |
| 75 | + JArray result = Utils.GetVariantAliases(entries, contentTypeUid); |
| 76 | + |
| 77 | + Assert.NotNull(result); |
| 78 | + Assert.Equal(3, result.Count); |
| 79 | + |
| 80 | + JObject first = (JObject)result[0]; |
| 81 | + Assert.True(first["entry_uid"] != null && !string.IsNullOrEmpty(first["entry_uid"].ToString())); |
| 82 | + Assert.Equal(contentTypeUid, first["contenttype_uid"].ToString()); |
| 83 | + var firstSet = JsonArrayToStringSet((JArray)first["variants"]); |
| 84 | + Assert.Equal( |
| 85 | + new HashSet<string> { "cs_personalize_0_0", "cs_personalize_0_3" }, |
| 86 | + firstSet); |
| 87 | + |
| 88 | + JObject second = (JObject)result[1]; |
| 89 | + Assert.True(second["entry_uid"] != null && !string.IsNullOrEmpty(second["entry_uid"].ToString())); |
| 90 | + Assert.Single((JArray)second["variants"]); |
| 91 | + Assert.Equal("cs_personalize_0_0", ((JArray)second["variants"])[0].ToString()); |
| 92 | + |
| 93 | + JObject third = (JObject)result[2]; |
| 94 | + Assert.True(third["entry_uid"] != null && !string.IsNullOrEmpty(third["entry_uid"].ToString())); |
| 95 | + Assert.Empty((JArray)third["variants"]); |
| 96 | + } |
| 97 | + |
| 98 | + [Fact] |
| 99 | + public void GetDataCsvariantsAttribute_MultipleEntries_ReturnsJsonArrayString() |
| 100 | + { |
| 101 | + JObject full = ReadJsonRoot("variantsEntries.json"); |
| 102 | + JArray entries = (JArray)full["entries"]; |
| 103 | + const string contentTypeUid = "movie"; |
| 104 | + |
| 105 | + JObject result = Utils.GetDataCsvariantsAttribute(entries, contentTypeUid); |
| 106 | + |
| 107 | + Assert.True(result["data-csvariants"] != null); |
| 108 | + string dataCsvariantsStr = result["data-csvariants"].ToString(); |
| 109 | + JArray arr = JArray.Parse(dataCsvariantsStr); |
| 110 | + Assert.Equal(3, arr.Count); |
| 111 | + Assert.True(((JObject)arr[0])["entry_uid"] != null && !string.IsNullOrEmpty(((JObject)arr[0])["entry_uid"].ToString())); |
| 112 | + Assert.Equal(2, ((JArray)((JObject)arr[0])["variants"]).Count); |
| 113 | + Assert.True(((JObject)arr[1])["entry_uid"] != null && !string.IsNullOrEmpty(((JObject)arr[1])["entry_uid"].ToString())); |
| 114 | + Assert.Single((JArray)((JObject)arr[1])["variants"]); |
| 115 | + Assert.True(((JObject)arr[2])["entry_uid"] != null && !string.IsNullOrEmpty(((JObject)arr[2])["entry_uid"].ToString())); |
| 116 | + Assert.Empty((JArray)((JObject)arr[2])["variants"]); |
| 117 | + } |
| 118 | + |
| 119 | + [Fact] |
| 120 | + public void GetVariantAliases_ThrowsWhenEntryNull() |
| 121 | + { |
| 122 | + Assert.Throws<ArgumentException>(() => Utils.GetVariantAliases((JObject)null, "landing_page")); |
| 123 | + } |
| 124 | + |
| 125 | + [Fact] |
| 126 | + public void GetVariantAliases_ThrowsWhenContentTypeUidNull() |
| 127 | + { |
| 128 | + JObject full = ReadJsonRoot("variantsSingleEntry.json"); |
| 129 | + JObject entry = (JObject)full["entry"]; |
| 130 | + Assert.Throws<ArgumentException>(() => Utils.GetVariantAliases(entry, null)); |
| 131 | + } |
| 132 | + |
| 133 | + [Fact] |
| 134 | + public void GetVariantAliases_ThrowsWhenContentTypeUidEmpty() |
| 135 | + { |
| 136 | + JObject full = ReadJsonRoot("variantsSingleEntry.json"); |
| 137 | + JObject entry = (JObject)full["entry"]; |
| 138 | + Assert.Throws<ArgumentException>(() => Utils.GetVariantAliases(entry, "")); |
| 139 | + } |
| 140 | + |
| 141 | + [Fact] |
| 142 | + public void GetDataCsvariantsAttribute_WhenEntryNull_ReturnsEmptyArrayString() |
| 143 | + { |
| 144 | + JObject result = Utils.GetDataCsvariantsAttribute((JObject)null, "landing_page"); |
| 145 | + Assert.True(result["data-csvariants"] != null); |
| 146 | + Assert.Equal("[]", result["data-csvariants"].ToString()); |
| 147 | + } |
| 148 | + } |
| 149 | +} |
0 commit comments