<#@ template language="C#" inherits="Generators.BaseGenerator" #> <#@ assembly name="System.Core" #> <#@ import namespace="System.IO" #> <#@ import namespace="System.Linq" #> <#@ import namespace="System.Text" #> <#@ import namespace="System.Collections.Generic" #> <# AddLicenseHeader(); #> using System; using System.Globalization; using System.IO; using System.Linq; using System.Text; using Microsoft.VisualStudio.TestTools.UnitTesting; using <#=this.Config.Namespace#>; using <#=this.Config.Namespace#>.Model; using <#=this.Config.Namespace#>.Model.Internal.MarshallTransformations; using Amazon.Runtime.Internal.Transform; using Amazon.Util; using ServiceClientGenerator; using AWSSDK_DotNet35.UnitTests.TestTools; namespace AWSSDK_DotNet35.UnitTests.Marshalling { [TestClass] public partial class <#=this.Config.ClassName#>MarshallingTests { static readonly ServiceModel service_model = Utils.LoadServiceModel("<#= this.Config.ServiceDirectoryName #>"); <# foreach(var operation in this.Config.ServiceModel.Operations) { #> [TestMethod] [TestCategory("UnitTest")] [TestCategory("<#=this.Config.ServiceModel.Type#>")] [TestCategory("<#=this.Config.TestCategory#>")] public void <#=operation.Name#>MarshallTest() { var operation = service_model.FindOperation("<#=operation.Name#>"); var request = InstantiateClassGenerator.Execute<<#=operation.Name#>Request>(); var marshaller = new <#=operation.Name#>RequestMarshaller(); var internalRequest = marshaller.Marshall(request); TestTools.RequestValidator.Validate("<#=operation.Name#>", request, internalRequest, service_model); <# if (operation.RequestHeaderMembers.Count(m => m.Shape.IsMap) > 0) { foreach (var member in operation.RequestHeaderMembers) { if (member.Shape.IsMap) { #> // Validate request headers in <#=member.PropertyName#> (prefix '<#=member.MarshallLocationName#>') foreach (var kvp<#=member.PropertyName#> in request.<#=member.PropertyName#>) { <# // Dictionary if (member.ModelShape.ValueShape.IsString) { #> Assert.AreEqual(kvp<#=member.PropertyName#>.Value, internalRequest.Headers[$"<#=member.MarshallLocationName#>{kvp<#=member.PropertyName#>.Key}"]); <# } // Dictionary> else if (member.ModelShape.ValueShape?.ListShape?.IsString == true) { #> var flattenedHeader = string.Join(",", kvp<#=member.PropertyName#>.Value); Assert.AreEqual(flattenedHeader, internalRequest.Headers[$"<#=member.MarshallLocationName#>{kvp<#=member.PropertyName#>.Key}"]); <# } #> } <# } } } #> <# if(operation.ResponseStructure != null) { #> var webResponse = new WebResponseData { Headers = { <# if (operation.ResponseHeaderMembers.Count() > 0 ) { foreach (var member in operation.ResponseHeaderMembers) { if (member.Shape.IsString) { if (member.IsJsonValue) { #> {"<#=member.MarshallLocationName#>",Convert.ToBase64String(Encoding.UTF8.GetBytes("<#=member.MarshallLocationName#>_Value"))}, <# } else { #> {"<#=member.MarshallLocationName#>","<#=member.MarshallLocationName#>_Value"}, <# } } else if (member.Shape.IsMap) { //Defer processing of the map until after the other types because //we cannot write the code to write out the dynamic map statically //inline. continue; } else if (member.Shape.IsInt) { #> {"<#=member.MarshallLocationName#>",int.MaxValue.ToString(CultureInfo.InvariantCulture)}, <# } else if (member.Shape.IsLong) { #> {"<#=member.MarshallLocationName#>",long.MaxValue.ToString(CultureInfo.InvariantCulture)}, <# } else if (member.Shape.IsDateTime) { #> {"<#=member.MarshallLocationName#>",ValidatorUtils.GetTestDate(TimestampFormat.<#=member.TimestampFormat#>)}, <# } else { throw new Exception("Member " + member.Shape.Name + " cannot be handled because it's type, " + member.Shape.Type + ", is not handled. Please Update RestMarshallingTests.tt."); } } } #> {"x-amzn-RequestId", Guid.NewGuid().ToString()}, {"x-amz-crc32","0"} } }; <# if (operation.ResponseHeaderMembers.Count(m => m.Shape.IsMap) > 0) { #> // populate a dummy response object so we can copy the headers var dummyResponse = InstantiateClassGenerator.Execute<<#=operation.Name#>Response>(); <# foreach (var member in operation.ResponseHeaderMembers) { if (member.Shape.IsMap) { #> // Populate map of response headers for <#=member.PropertyName#> with prefix "<#=member.MarshallLocationName#>". foreach (var kvp<#=member.PropertyName#> in dummyResponse.<#=member.PropertyName#>) { <# // Dictionary if (member.ModelShape.ValueShape.IsString) { #> webResponse.Headers.Add($"<#=member.MarshallLocationName#>{kvp<#=member.PropertyName#>.Key}", kvp<#=member.PropertyName#>.Value); <# } // Dictionary> else if (member.ModelShape.ValueShape?.ListShape?.IsString == true) { #> webResponse.Headers.Add($"<#=member.MarshallLocationName#>{kvp<#=member.PropertyName#>.Key}", string.Join(",", kvp<#=member.PropertyName#>.Value.ToArray())); <# } #> } <# } } } if(this.Config.ServiceModel.Type == ServiceType.Rest_Xml) { #> var payloadResponse = new XmlSampleGenerator(service_model, operation).Execute(); webResponse.ContentLength = payloadResponse.Length; var context = new XmlUnmarshallerContext(Utils.CreateStreamFromString(payloadResponse), false, webResponse); ResponseUnmarshaller unmarshaller = <#=operation.Name#>ResponseUnmarshaller.Instance; var response = unmarshaller.Unmarshall(context) as <#=operation.Name#>Response; InstantiateClassGenerator.ValidateObjectFullyInstantiated(response); <# } else { #> var payloadResponse = new JsonSampleGenerator(service_model, operation.ResponseStructure).Execute(); webResponse.Headers["Content-Length"] = UTF8Encoding.UTF8.GetBytes(payloadResponse).Length.ToString(); var context = new JsonUnmarshallerContext(Utils.CreateStreamFromString(payloadResponse), false, webResponse); ResponseUnmarshaller unmarshaller = <#=operation.Name#>ResponseUnmarshaller.Instance; var response = unmarshaller.Unmarshall(context) as <#=operation.Name#>Response; InstantiateClassGenerator.ValidateObjectFullyInstantiated(response); <# if (operation.ResponseHeaderMembers.Count(m => m.Shape.IsMap) > 0) { foreach (var member in operation.ResponseHeaderMembers) { if (member.Shape.IsMap) { #> // Validate response headers for <#=member.PropertyName#> with prefix "<#=member.MarshallLocationName#>". foreach (var kvp<#=member.PropertyName#> in dummyResponse.<#=member.PropertyName#>) { <# // Dictionary if (member.ModelShape.ValueShape.IsString) { #> Assert.AreEqual(response.ResponseHeaders[kvp<#=member.PropertyName#>.Key], kvp<#=member.PropertyName#>.Value); <# } // Dictionary> else if (member.ModelShape.ValueShape?.ListShape?.IsString == true) { #> Assert.AreEqual(response.ResponseHeaders[kvp<#=member.PropertyName#>.Key].Count(), kvp<#=member.PropertyName#>.Value.Count()); for (var i = 0; i < response.ResponseHeaders[kvp<#=member.PropertyName#>.Key].Count(); i++) Assert.AreEqual(response.ResponseHeaders[kvp<#=member.PropertyName#>.Key][i], kvp<#=member.PropertyName#>.Value[i]); <# } #> } <# } } } } } #> } <# foreach (var exception in operation.Exceptions) { #> [TestMethod] [TestCategory("UnitTest")] [TestCategory("<#=this.Config.ServiceModel.Type#>")] [TestCategory("<#=this.Config.TestCategory#>")] public void <#=operation.Name#>_<#=exception.Name#>MarshallTest() { var operation = service_model.FindOperation("<#=operation.Name#>"); var request = InstantiateClassGenerator.Execute<<#=operation.Name#>Request>(); var marshaller = new <#=operation.Name#>RequestMarshaller(); var internalRequest = marshaller.Marshall(request); TestTools.RequestValidator.Validate("<#=operation.Name#>", request, internalRequest, service_model); var exception = operation.Exceptions.First(e => e.Name.Equals("<#=exception.Name#>")); var webResponse = new WebResponseData { Headers = { <# if( operation.ResponseHeaderMembers.Count() > 0 ) { foreach (var member in operation.ResponseHeaderMembers) { if (member.Shape.IsString) { if (member.IsJsonValue) { #> {"<#=member.MarshallLocationName#>",Convert.ToBase64String(Encoding.UTF8.GetBytes("<#=member.MarshallLocationName#>_Value"))}, <# } else { #> {"<#=member.MarshallLocationName#>","<#=member.MarshallLocationName#>_Value"}, <# } } else if (member.Shape.IsMap) { //Defer processing of the map until after the other types because //we cannot write the code to write out the dynamic map statically //inline. continue; } else if (member.Shape.IsInt) { #> {"<#=member.MarshallLocationName#>",int.MaxValue.ToString(CultureInfo.InvariantCulture)}, <# } else if (member.Shape.IsLong) { #> {"<#=member.MarshallLocationName#>",long.MaxValue.ToString(CultureInfo.InvariantCulture)}, <# } else if (member.Shape.IsDateTime) { #> {"<#=member.MarshallLocationName#>",ValidatorUtils.GetTestDate(TimestampFormat.<#=member.TimestampFormat#>)}, <# } else { throw new Exception("Member " + member.Shape.Name + " cannot be handled because it's type, " + member.Shape.Type + ", is not handled. Please Update RestMarshallingTests.tt."); } } } #> {"x-amzn-RequestId", Guid.NewGuid().ToString()}, {"x-amz-crc32","0"}, <# if(this.Config.ServiceModel.Type != ServiceType.Rest_Xml) { #> {"x-amzn-ErrorType","<#=exception.Name#>"}, <# } #> } }; <# if (operation.ResponseHeaderMembers.Count(m => m.Shape.IsMap) > 0) { #> // populate a dummy response object so we can copy the headers var dummyResponse = InstantiateClassGenerator.Execute<<#=operation.Name#>Response>(); <# foreach (var member in operation.ResponseHeaderMembers) { if (member.Shape.IsMap) { #> // Populate map of response headers for <#=member.PropertyName#> with prefix "<#=member.MarshallLocationName#>". foreach (var kvp<#=member.PropertyName#> in dummyResponse.<#=member.PropertyName#>) { <# // Dictionary if (member.ModelShape.ValueShape.IsString) { #> webResponse.Headers.Add($"<#=member.MarshallLocationName#>{kvp<#=member.PropertyName#>.Key}", kvp<#=member.PropertyName#>.Value); <# } // Dictionary> else if (member.ModelShape.ValueShape?.ListShape?.IsString == true) { #> webResponse.Headers.Add($"<#=member.MarshallLocationName#>{kvp<#=member.PropertyName#>.Key}", string.Join(",", kvp<#=member.PropertyName#>.Value.ToArray())); <# } #> } <# } } } if(this.Config.ServiceModel.Type == ServiceType.Rest_Xml) { #> var payloadResponse = new XmlSampleGenerator(service_model, operation).Execute(exception); webResponse.Headers["Content-Length"] = UTF8Encoding.UTF8.GetBytes(payloadResponse).Length.ToString(); var context = new XmlUnmarshallerContext(Utils.CreateStreamFromString(payloadResponse), true, webResponse, true); var response = <#=operation.Name#>ResponseUnmarshaller.Instance.UnmarshallException(context, null, System.Net.HttpStatusCode.OK); InstantiateClassGenerator.ValidateObjectFullyInstantiated(response); <# } else { #> var payloadResponse = new JsonSampleGenerator(service_model, exception).Execute(); webResponse.Headers["Content-Length"] = UTF8Encoding.UTF8.GetBytes(payloadResponse).Length.ToString(); var context = new JsonUnmarshallerContext(Utils.CreateStreamFromString(payloadResponse), true, webResponse, true); var response = <#=operation.Name#>ResponseUnmarshaller.Instance.UnmarshallException(context, null, System.Net.HttpStatusCode.OK); InstantiateClassGenerator.ValidateObjectFullyInstantiated(response); <# } #> } <# } } #> } }