|
1 | | -using Microsoft.VisualStudio.TestTools.UnitTesting; |
| 1 | +// Copyright (c) Microsoft Corporation. All Rights Reserved. Licensed under the MIT License. See License in the project root for license information. |
| 2 | + |
| 3 | +using Microsoft.VisualStudio.TestTools.UnitTesting; |
| 4 | +using System.Collections.Generic; |
2 | 5 | using System.Linq; |
3 | 6 | using System.Xml.Linq; |
4 | 7 |
|
@@ -50,7 +53,8 @@ public void It_adds_the_ContainsTarget_attribute() |
50 | 53 |
|
51 | 54 | bool doesntContainTargetBefore = MetadataPreprocessor.GetXMetadata().Descendants() |
52 | 55 | .Where(x => x.Name.LocalName == "NavigationProperty") |
53 | | - .Where(x => x.Attribute("ContainsTarget") == null || x.Attribute("ContainsTarget").Value.Equals("false")) |
| 56 | + .Where(x => x.Attribute("ContainsTarget") == null || |
| 57 | + x.Attribute("ContainsTarget").Value.Equals("false")) |
54 | 58 | .Where(x => x.Attribute("Type").Value == $"Collection(microsoft.graph.{navPropTypeToProcess})") |
55 | 59 | .Any(); |
56 | 60 |
|
@@ -110,5 +114,159 @@ public void It_adds_long_description_to_thumbnail() |
110 | 114 |
|
111 | 115 | Assert.IsTrue(foundAnnotationAfter, "Expected: thumbnailComplexType set with an annotation. Actual: annotation wasn't found."); |
112 | 116 | } |
| 117 | + |
| 118 | + /// <summary> |
| 119 | + /// Tests that we reorder parameters according to an input listof parameters. |
| 120 | + /// </summary> |
| 121 | + [TestMethod] |
| 122 | + public void It_reorders_parameters_in_an_action() |
| 123 | + { |
| 124 | + /* The element to reorder from the resources/dirtymetadata.xml file. |
| 125 | + <Action Name="forward" IsBound="true"> |
| 126 | + <Parameter Name="bindingParameter" Type="microsoft.graph.onenotePage" /> |
| 127 | + <Parameter Name="ToRecipients" Type="Collection(microsoft.graph.recipient)" Nullable="false" /> |
| 128 | + <Parameter Name="Comment" Type="Edm.String" Unicode="false" /> |
| 129 | + </Action> |
| 130 | + */ |
| 131 | + |
| 132 | + // Specify the metadata definition to reorder and the new element order specification. |
| 133 | + var targetMetadataDefType = MetadataDefinitionType.Action; |
| 134 | + var targetMetadataDefName = "forward"; |
| 135 | + var newParameterOrder = new List<string>() { "bindingParameter", |
| 136 | + "Comment", |
| 137 | + "ToRecipients" }; |
| 138 | + var bindingParameterType = "microsoft.graph.onenotePage"; |
| 139 | + |
| 140 | + // Check whether an element exists in the metadata that matches our reordered element list before we reorder. |
| 141 | + var isTargetDefinitionInMetadataBefore = MetadataPreprocessor.GetXMetadata().Descendants() |
| 142 | + .Where(x => x.Name.LocalName == targetMetadataDefType.ToString()) |
| 143 | + .Where(x => x.Attribute("Name").Value == targetMetadataDefName) // Returns all Action elements named forward. |
| 144 | + .Where(el => el.Descendants().FirstOrDefault(x => x.Attribute("Type").Value == bindingParameterType) != null) |
| 145 | + .Where(el => el.Elements().Select(a => a.Attribute("Name").Value) |
| 146 | + .SequenceEqual(newParameterOrder)).Any(); |
| 147 | + |
| 148 | + // Make a call to reorder the parameters for the target action in the metadata loaded into memory. |
| 149 | + MetadataPreprocessor.ReorderElements(targetMetadataDefType, |
| 150 | + targetMetadataDefName, |
| 151 | + newParameterOrder, |
| 152 | + bindingParameterType); |
| 153 | + |
| 154 | + // Query the updated metadata for the results that match the reordered element. |
| 155 | + var results = MetadataPreprocessor.GetXMetadata().Descendants() |
| 156 | + .Where(x => x.Name.LocalName == targetMetadataDefType.ToString()) |
| 157 | + .Where(x => x.Attribute("Name").Value == targetMetadataDefName) // Returns all Action elements named forward. |
| 158 | + .Where(el => el.Descendants().FirstOrDefault(x => x.Attribute("Type").Value == bindingParameterType) != null) |
| 159 | + .Where(el => el.Elements().Select(a => a.Attribute("Name").Value) |
| 160 | + .SequenceEqual(newParameterOrder)); |
| 161 | + |
| 162 | + Assert.IsFalse(isTargetDefinitionInMetadataBefore); |
| 163 | + // Added multiple elements with the same binding parameter - we want to make sure there is only one in the results. |
| 164 | + Assert.IsTrue(results.Count() == 1, $"Expected: A single result item. Actual: found {results.Count()} items."); |
| 165 | + Assert.AreEqual(newParameterOrder.Count(), |
| 166 | + results.FirstOrDefault().Elements().Count(), |
| 167 | + "The reordered element list doesn't match the count of elements in the input new order list."); |
| 168 | + Assert.IsTrue(results.FirstOrDefault() |
| 169 | + .Elements() |
| 170 | + .Select(a => a.Attribute("Name").Value) |
| 171 | + .SequenceEqual(newParameterOrder), |
| 172 | + "The element list was not reordered as expected."); |
| 173 | + } |
| 174 | + |
| 175 | + /// <summary> |
| 176 | + /// Tests that we reorder parameters according to an input element name list. |
| 177 | + /// </summary> |
| 178 | + [TestMethod] |
| 179 | + public void It_reorders_elements_in_a_complextype() |
| 180 | + { |
| 181 | + /* The element to reorder from the resources/dirtymetadata.xml file. |
| 182 | + <ComplexType Name="thumbnail"> |
| 183 | + <Property Name="content" Type="Edm.Stream" /> |
| 184 | + <Property Name="height" Type="Edm.Int32" /> |
| 185 | + <Property Name="sourceItemId" Type="Edm.String" /> |
| 186 | + <Property Name="url" Type="Edm.String" /> |
| 187 | + <Property Name="width" Type="Edm.Int32" /> |
| 188 | + </ComplexType> |
| 189 | + */ |
| 190 | + |
| 191 | + // Specify the metadata definition to reorder and the new element order specification. |
| 192 | + var targetMetadataDefType = MetadataDefinitionType.ComplexType; |
| 193 | + var targetMetadataDefName = "thumbnail"; |
| 194 | + var newParameterOrder = new List<string>() { "width", |
| 195 | + "url", |
| 196 | + "sourceItemId", |
| 197 | + "height", |
| 198 | + "content" }; |
| 199 | + |
| 200 | + // Check whether an element exists in the metadata that |
| 201 | + // matches our reordered element list before we reorder. |
| 202 | + var isTargetDefinitionInMetadataBefore = MetadataPreprocessor.GetXMetadata().Descendants() |
| 203 | + .Where(x => x.Name.LocalName == targetMetadataDefType.ToString()) |
| 204 | + .Where(x => x.Attribute("Name").Value == targetMetadataDefName) |
| 205 | + .Where(el => el.Elements().Select(a => a.Attribute("Name").Value) |
| 206 | + .SequenceEqual(newParameterOrder)).Any(); |
| 207 | + |
| 208 | + // Make a call to reorder the parameters for the target |
| 209 | + // complex type in the metadata loaded into memory. |
| 210 | + MetadataPreprocessor.ReorderElements(targetMetadataDefType, |
| 211 | + targetMetadataDefName, |
| 212 | + newParameterOrder); |
| 213 | + |
| 214 | + // Query the updated metadata for the results that match the reordered element. |
| 215 | + var results = MetadataPreprocessor.GetXMetadata().Descendants() |
| 216 | + .Where(x => x.Name.LocalName == targetMetadataDefType.ToString()) |
| 217 | + .Where(x => x.Attribute("Name").Value == targetMetadataDefName) |
| 218 | + .Where(el => el.Elements().Select(a => a.Attribute("Name").Value) |
| 219 | + .SequenceEqual(newParameterOrder)); |
| 220 | + |
| 221 | + Assert.IsFalse(isTargetDefinitionInMetadataBefore); |
| 222 | + Assert.IsTrue(results.Count() == 1, $"Expected: A single result item. Actual: found {results.Count()} items."); |
| 223 | + Assert.AreEqual(newParameterOrder.Count(), |
| 224 | + results.FirstOrDefault().Elements().Count(), |
| 225 | + "The reordered element list doesn't match the count of elements in the input new order list."); |
| 226 | + Assert.IsTrue(results.FirstOrDefault().Elements().Select(a => a.Attribute("Name").Value).SequenceEqual(newParameterOrder), |
| 227 | + "The element list was not reordered as expected."); |
| 228 | + } |
| 229 | + |
| 230 | + [TestMethod] |
| 231 | + public void It_does_not_reorder_when_element_list_does_not_match_in_a_complextype() |
| 232 | + { |
| 233 | + /* The element to attempt to reorder from the resources/dirtymetadata.xml file. |
| 234 | + * The element list, newParameterOrder does not match the thumbnail type |
| 235 | + * in the metadata (missing 'content' element) so we expect that the |
| 236 | + * reorder attempt fails. |
| 237 | + <ComplexType Name="thumbnail"> |
| 238 | + <Property Name="content" Type="Edm.Stream" /> |
| 239 | + <Property Name="height" Type="Edm.Int32" /> |
| 240 | + <Property Name="sourceItemId" Type="Edm.String" /> |
| 241 | + <Property Name="url" Type="Edm.String" /> |
| 242 | + <Property Name="width" Type="Edm.Int32" /> |
| 243 | + </ComplexType> |
| 244 | + */ |
| 245 | + |
| 246 | + // Specify the metadata definition to reorder and the new |
| 247 | + // element order specification. |
| 248 | + var targetMetadataDefType = MetadataDefinitionType.ComplexType; |
| 249 | + var targetMetadataDefName = "thumbnail"; |
| 250 | + var newParameterOrder = new List<string>() { "width", |
| 251 | + "url", |
| 252 | + "sourceItemId", |
| 253 | + "height" }; |
| 254 | + |
| 255 | + // Make a call to reorder the parameters for the target |
| 256 | + // complex type in the metadata loaded into memory. |
| 257 | + MetadataPreprocessor.ReorderElements(targetMetadataDefType, |
| 258 | + targetMetadataDefName, |
| 259 | + newParameterOrder); |
| 260 | + |
| 261 | + // Query the updated metadata for the results that match the reordered element. |
| 262 | + var results = MetadataPreprocessor.GetXMetadata().Descendants() |
| 263 | + .Where(x => x.Name.LocalName == targetMetadataDefType.ToString()) |
| 264 | + .Where(x => x.Attribute("Name").Value == targetMetadataDefName) |
| 265 | + .Where(el => el.Elements().Select(a => a.Attribute("Name").Value) |
| 266 | + .SequenceEqual(newParameterOrder)); |
| 267 | + |
| 268 | + Assert.IsTrue(results.Count() == 0, |
| 269 | + $"Expected: Zero results. Actual: found {results.Count()} items."); |
| 270 | + } |
113 | 271 | } |
114 | 272 | } |
0 commit comments