Merge pull request #3913 from omwok/master
[mono.git] / mcs / class / System.ServiceModel / Test / MetadataTests / ExportTests.cs
1 //
2 // TestExport.cs
3 //
4 // Author:
5 //       Martin Baulig <martin.baulig@xamarin.com>
6 //
7 // Copyright (c) 2012 Xamarin Inc. (http://www.xamarin.com)
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining a copy
10 // of this software and associated documentation files (the "Software"), to deal
11 // in the Software without restriction, including without limitation the rights
12 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 // copies of the Software, and to permit persons to whom the Software is
14 // furnished to do so, subject to the following conditions:
15 //
16 // The above copyright notice and this permission notice shall be included in
17 // all copies or substantial portions of the Software.
18 //
19 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 // THE SOFTWARE.
26 using System;
27 using System.Net;
28 using System.Xml;
29 using System.Collections.Generic;
30 using System.ServiceModel;
31 using System.ServiceModel.Channels;
32 using System.ServiceModel.Description;
33
34 using QName = System.Xml.XmlQualifiedName;
35 using WS = System.Web.Services.Description;
36
37 using NUnit.Framework;
38 using NUnit.Framework.Constraints;
39
40 namespace MonoTests.System.ServiceModel.MetadataTests {
41
42         [TestFixture]
43         public class TestExport {
44                 internal const string HttpUri = "http://tempuri.org/TestHttp/";
45
46                 [Test]
47                 public void SimpleExport ()
48                 {
49                         var label = new TestLabel ("DuplicateContract");
50                         
51                         var cd = new ContractDescription ("MyContract");
52                         var endpoint = new ServiceEndpoint (
53                                 cd, new BasicHttpBinding (), new EndpointAddress (HttpUri));
54                         
55                         var exporter = new WsdlExporter ();
56                         exporter.ExportContract (cd);
57                         exporter.ExportEndpoint (endpoint);
58                         
59                         CheckExport (
60                                 exporter, new QName ("MyContract", "http://tempuri.org/"),
61                                 "BasicHttpBinding", 1, label);
62                 }
63
64                 [Test]
65                 public void DuplicateContract ()
66                 {
67                         var label = new TestLabel ("DuplicateContract");
68
69                         var cd = new ContractDescription ("MyContract");
70                         var endpoint = new ServiceEndpoint (
71                                 cd, new BasicHttpBinding (), new EndpointAddress (HttpUri));
72
73                         var exporter = new WsdlExporter ();
74                         exporter.ExportContract (cd);
75                         exporter.ExportContract (cd);
76                         exporter.ExportEndpoint (endpoint);
77
78                         CheckExport (
79                                 exporter, new QName ("MyContract", "http://tempuri.org/"),
80                                 "BasicHttpBinding", 1, label);
81                 }
82
83                 [Test]
84                 public void DuplicateEndpoint ()
85                 {
86                         var label = new TestLabel ("DuplicateEndpoint");
87
88                         var cd = new ContractDescription ("MyContract");
89                         var endpoint = new ServiceEndpoint (
90                                 cd, new BasicHttpBinding (), new EndpointAddress (HttpUri));
91
92                         var exporter = new WsdlExporter ();
93                         exporter.ExportEndpoint (endpoint);
94                         exporter.ExportEndpoint (endpoint);
95
96                         CheckExport (
97                                 exporter, new QName ("MyContract", "http://tempuri.org/"),
98                                 "BasicHttpBinding", 1, label);
99                 }
100
101                 [Test]
102                 public void DuplicateEndpoint2 ()
103                 {
104                         var label = new TestLabel ("DuplicateEndpoint2");
105                         
106                         var cd = new ContractDescription ("MyContract");
107                         var endpoint = new ServiceEndpoint (
108                                 cd, new BasicHttpBinding (), new EndpointAddress (HttpUri));
109                         var endpoint2 = new ServiceEndpoint (
110                                 cd, new BasicHttpBinding (), new EndpointAddress (HttpUri));
111                         
112                         var exporter = new WsdlExporter ();
113                         exporter.ExportEndpoint (endpoint);
114                         exporter.ExportEndpoint (endpoint);
115                         exporter.ExportEndpoint (endpoint2);
116                         
117                         CheckExport (
118                                 exporter, new QName ("MyContract", "http://tempuri.org/"),
119                                 "BasicHttpBinding", 2, label);
120                 }
121
122                 public static void CheckExport (
123                         WsdlExporter exporter, QName contractName, string bindingName,
124                         int countEndpoints, TestLabel label)
125                 {
126                         Assert.That (exporter.GeneratedWsdlDocuments, Is.Not.Null, label.Get ());
127                         Assert.That (exporter.GeneratedWsdlDocuments.Count, Is.EqualTo (1), label.Get ());
128                         
129                         var wsdl = exporter.GeneratedWsdlDocuments [0];
130                         CheckExport (wsdl, contractName, bindingName, countEndpoints, label);
131                 }
132
133                 public static void CheckExport (
134                         WS.ServiceDescription wsdl, QName contractName, string bindingName,
135                         int countEndpoints, TestLabel label)
136                 {
137                         label.EnterScope ("ServiceDescription");
138                         Assert.That (wsdl.TargetNamespace, Is.EqualTo (contractName.Namespace), label.Get ());
139                         Assert.That (wsdl.Name, Is.EqualTo ("service"), label.Get ());
140                         label.LeaveScope ();
141
142                         label.EnterScope ("Bindings");
143                         Assert.That (wsdl.Bindings, Is.Not.Null, label.Get ());
144                         Assert.That (wsdl.Bindings.Count, Is.EqualTo (countEndpoints), label.Get ());
145                         
146                         for (int i = 0; i < countEndpoints; i++) {
147                                 label.EnterScope (string.Format ("#{0}", i+1));
148                                 var binding = wsdl.Bindings [i];
149                                 var expectedName = string.Format (
150                                         "{0}_{1}{2}", bindingName, contractName.Name,
151                                         i > 0 ? i.ToString () : "");
152                                 Assert.That (binding.Name, Is.EqualTo (expectedName), label.Get ());
153                                 Assert.That (binding.Type, Is.EqualTo (contractName), label.Get ());
154                                 label.LeaveScope ();
155                         }
156                         label.LeaveScope ();
157                         
158                         label.EnterScope ("PortTypes");
159                         Assert.That (wsdl.PortTypes, Is.Not.Null, label.Get ());
160                         Assert.That (wsdl.PortTypes.Count, Is.EqualTo (1), label.Get ());
161                         var portType = wsdl.PortTypes [0];
162                         Assert.That (portType.Name, Is.EqualTo (contractName.Name), label.Get ());
163                         label.LeaveScope ();
164                         
165                         label.EnterScope ("Services");
166                         Assert.That (wsdl.Services, Is.Not.Null, label.Get ());
167                         Assert.That (wsdl.Services.Count, Is.EqualTo (1), label.Get ());
168                         var service = wsdl.Services [0];
169                         Assert.That (service.Name, Is.EqualTo ("service"), label.Get ());
170                         label.LeaveScope ();
171                         
172                         label.EnterScope ("Ports");
173                         Assert.That (service.Ports, Is.Not.Null, label.Get ());
174                         Assert.That (service.Ports.Count, Is.EqualTo (countEndpoints), label.Get ());
175                         for (int i = 0; i < countEndpoints; i++) {
176                                 label.EnterScope (string.Format ("#{0}", i+1));
177                                 var port = service.Ports [i];
178                                 var expectedName = string.Format (
179                                         "{0}_{1}{2}", bindingName, contractName.Name,
180                                         i > 0 ? i.ToString () : "");
181                                 var qname = new QName (expectedName, contractName.Namespace);
182                                 Assert.That (port.Name, Is.EqualTo (qname.Name), label.Get ());
183                                 Assert.That (port.Binding, Is.EqualTo (qname), label.Get ());
184                                 label.LeaveScope ();
185                         }
186                         label.LeaveScope ();
187                 }
188
189                 [Test]
190                 public void Mtom_Policy ()
191                 {
192                         var label = new TestLabel ("Mtom_Policy");
193                         var contract = new ContractDescription ("MyContract");
194                         var binding = new BasicHttpBinding ();
195                         binding.MessageEncoding = WSMessageEncoding.Mtom;
196
197                         var endpoint = new ServiceEndpoint (
198                                 contract, binding, new EndpointAddress (HttpUri));
199
200                         var exporter = new WsdlExporter ();
201                         exporter.ExportEndpoint (endpoint);
202
203                         Assert.That (exporter.GeneratedWsdlDocuments, Is.Not.Null, label.Get ());
204                         Assert.That (exporter.GeneratedWsdlDocuments.Count, Is.EqualTo (1), label.Get ());
205                         var wsdl = exporter.GeneratedWsdlDocuments [0];
206
207                         Assert.That (wsdl.Bindings, Is.Not.Null, label.Get ());
208                         Assert.That (wsdl.Bindings.Count, Is.EqualTo (1), label.Get ());
209
210                         var wsb = wsdl.Bindings [0];
211                         label.EnterScope ("Binding");
212                         Assert.That (wsb.Extensions, Is.Not.Null, label.Get ());
213                         Assert.That (wsb.Extensions.Count, Is.EqualTo (2), label.Get ());
214                         label.LeaveScope ();
215
216                         label.EnterScope ("Extensions");
217                         WS.SoapBinding soap = null;
218                         XmlElement xml = null;
219                         foreach (var extension in wsb.Extensions) {
220                                 if (extension is WS.SoapBinding)
221                                         soap = (WS.SoapBinding)extension;
222                                 else if (extension is XmlElement)
223                                         xml = (XmlElement)extension;
224                                 else
225                                         Assert.Fail ("Unknown extension.", label);
226                         }
227
228                         Assert.That (soap, Is.Not.Null, label.Get ());
229                         Assert.That (xml, Is.Not.Null, label.Get ());
230                         label.LeaveScope ();
231
232                         label.EnterScope ("Policy");
233                         var assertions = BindingTestAssertions.AssertPolicy (wsdl, xml, label);
234                         Assert.That (assertions.Count, Is.EqualTo (1), label.Get ());
235                         var assertion = assertions [0];
236                         Assert.That (assertion.NamespaceURI, Is.EqualTo ("http://schemas.xmlsoap.org/ws/2004/09/policy/optimizedmimeserialization"), label.Get ());
237                         Assert.That (assertion.LocalName, Is.EqualTo ("OptimizedMimeSerialization"), label.Get ());
238                         label.LeaveScope ();
239                 }
240         }
241 }
242