Merge pull request #496 from nicolas-raoul/unit-test-for-issue2907
[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 using NUnit.Framework.SyntaxHelpers;
40
41 namespace MonoTests.System.ServiceModel.MetadataTests {
42
43         [TestFixture]
44         public class TestExport {
45                 internal const string HttpUri = "http://tempuri.org/TestHttp/";
46
47                 [Test]
48                 public void SimpleExport ()
49                 {
50                         var label = new TestLabel ("DuplicateContract");
51                         
52                         var cd = new ContractDescription ("MyContract");
53                         var endpoint = new ServiceEndpoint (
54                                 cd, new BasicHttpBinding (), new EndpointAddress (HttpUri));
55                         
56                         var exporter = new WsdlExporter ();
57                         exporter.ExportContract (cd);
58                         exporter.ExportEndpoint (endpoint);
59                         
60                         CheckExport (
61                                 exporter, new QName ("MyContract", "http://tempuri.org/"),
62                                 "BasicHttpBinding", 1, label);
63                 }
64
65                 [Test]
66                 public void DuplicateContract ()
67                 {
68                         var label = new TestLabel ("DuplicateContract");
69
70                         var cd = new ContractDescription ("MyContract");
71                         var endpoint = new ServiceEndpoint (
72                                 cd, new BasicHttpBinding (), new EndpointAddress (HttpUri));
73
74                         var exporter = new WsdlExporter ();
75                         exporter.ExportContract (cd);
76                         exporter.ExportContract (cd);
77                         exporter.ExportEndpoint (endpoint);
78
79                         CheckExport (
80                                 exporter, new QName ("MyContract", "http://tempuri.org/"),
81                                 "BasicHttpBinding", 1, label);
82                 }
83
84                 [Test]
85                 public void DuplicateEndpoint ()
86                 {
87                         var label = new TestLabel ("DuplicateEndpoint");
88
89                         var cd = new ContractDescription ("MyContract");
90                         var endpoint = new ServiceEndpoint (
91                                 cd, new BasicHttpBinding (), new EndpointAddress (HttpUri));
92
93                         var exporter = new WsdlExporter ();
94                         exporter.ExportEndpoint (endpoint);
95                         exporter.ExportEndpoint (endpoint);
96
97                         CheckExport (
98                                 exporter, new QName ("MyContract", "http://tempuri.org/"),
99                                 "BasicHttpBinding", 1, label);
100                 }
101
102                 [Test]
103                 public void DuplicateEndpoint2 ()
104                 {
105                         var label = new TestLabel ("DuplicateEndpoint2");
106                         
107                         var cd = new ContractDescription ("MyContract");
108                         var endpoint = new ServiceEndpoint (
109                                 cd, new BasicHttpBinding (), new EndpointAddress (HttpUri));
110                         var endpoint2 = new ServiceEndpoint (
111                                 cd, new BasicHttpBinding (), new EndpointAddress (HttpUri));
112                         
113                         var exporter = new WsdlExporter ();
114                         exporter.ExportEndpoint (endpoint);
115                         exporter.ExportEndpoint (endpoint);
116                         exporter.ExportEndpoint (endpoint2);
117                         
118                         CheckExport (
119                                 exporter, new QName ("MyContract", "http://tempuri.org/"),
120                                 "BasicHttpBinding", 2, label);
121                 }
122
123                 public static void CheckExport (
124                         WsdlExporter exporter, QName contractName, string bindingName,
125                         int countEndpoints, TestLabel label)
126                 {
127                         Assert.That (exporter.GeneratedWsdlDocuments, Is.Not.Null, label.Get ());
128                         Assert.That (exporter.GeneratedWsdlDocuments.Count, Is.EqualTo (1), label.Get ());
129                         
130                         var wsdl = exporter.GeneratedWsdlDocuments [0];
131                         CheckExport (wsdl, contractName, bindingName, countEndpoints, label);
132                 }
133
134                 public static void CheckExport (
135                         WS.ServiceDescription wsdl, QName contractName, string bindingName,
136                         int countEndpoints, TestLabel label)
137                 {
138                         label.EnterScope ("ServiceDescription");
139                         Assert.That (wsdl.TargetNamespace, Is.EqualTo (contractName.Namespace), label.Get ());
140                         Assert.That (wsdl.Name, Is.EqualTo ("service"), label.Get ());
141                         label.LeaveScope ();
142
143                         label.EnterScope ("Bindings");
144                         Assert.That (wsdl.Bindings, Is.Not.Null, label.Get ());
145                         Assert.That (wsdl.Bindings.Count, Is.EqualTo (countEndpoints), label.Get ());
146                         
147                         for (int i = 0; i < countEndpoints; i++) {
148                                 label.EnterScope (string.Format ("#{0}", i+1));
149                                 var binding = wsdl.Bindings [i];
150                                 var expectedName = string.Format (
151                                         "{0}_{1}{2}", bindingName, contractName.Name,
152                                         i > 0 ? i.ToString () : "");
153                                 Assert.That (binding.Name, Is.EqualTo (expectedName), label.Get ());
154                                 Assert.That (binding.Type, Is.EqualTo (contractName), label.Get ());
155                                 label.LeaveScope ();
156                         }
157                         label.LeaveScope ();
158                         
159                         label.EnterScope ("PortTypes");
160                         Assert.That (wsdl.PortTypes, Is.Not.Null, label.Get ());
161                         Assert.That (wsdl.PortTypes.Count, Is.EqualTo (1), label.Get ());
162                         var portType = wsdl.PortTypes [0];
163                         Assert.That (portType.Name, Is.EqualTo (contractName.Name), label.Get ());
164                         label.LeaveScope ();
165                         
166                         label.EnterScope ("Services");
167                         Assert.That (wsdl.Services, Is.Not.Null, label.Get ());
168                         Assert.That (wsdl.Services.Count, Is.EqualTo (1), label.Get ());
169                         var service = wsdl.Services [0];
170                         Assert.That (service.Name, Is.EqualTo ("service"), label.Get ());
171                         label.LeaveScope ();
172                         
173                         label.EnterScope ("Ports");
174                         Assert.That (service.Ports, Is.Not.Null, label.Get ());
175                         Assert.That (service.Ports.Count, Is.EqualTo (countEndpoints), label.Get ());
176                         for (int i = 0; i < countEndpoints; i++) {
177                                 label.EnterScope (string.Format ("#{0}", i+1));
178                                 var port = service.Ports [i];
179                                 var expectedName = string.Format (
180                                         "{0}_{1}{2}", bindingName, contractName.Name,
181                                         i > 0 ? i.ToString () : "");
182                                 var qname = new QName (expectedName, contractName.Namespace);
183                                 Assert.That (port.Name, Is.EqualTo (qname.Name), label.Get ());
184                                 Assert.That (port.Binding, Is.EqualTo (qname), label.Get ());
185                                 label.LeaveScope ();
186                         }
187                         label.LeaveScope ();
188                 }
189
190                 [Test]
191                 public void Mtom_Policy ()
192                 {
193                         var label = new TestLabel ("Mtom_Policy");
194                         var contract = new ContractDescription ("MyContract");
195                         var binding = new BasicHttpBinding ();
196                         binding.MessageEncoding = WSMessageEncoding.Mtom;
197
198                         var endpoint = new ServiceEndpoint (
199                                 contract, binding, new EndpointAddress (HttpUri));
200
201                         var exporter = new WsdlExporter ();
202                         exporter.ExportEndpoint (endpoint);
203
204                         Assert.That (exporter.GeneratedWsdlDocuments, Is.Not.Null, label.Get ());
205                         Assert.That (exporter.GeneratedWsdlDocuments.Count, Is.EqualTo (1), label.Get ());
206                         var wsdl = exporter.GeneratedWsdlDocuments [0];
207
208                         Assert.That (wsdl.Bindings, Is.Not.Null, label.Get ());
209                         Assert.That (wsdl.Bindings.Count, Is.EqualTo (1), label.Get ());
210
211                         var wsb = wsdl.Bindings [0];
212                         label.EnterScope ("Binding");
213                         Assert.That (wsb.Extensions, Is.Not.Null, label.Get ());
214                         Assert.That (wsb.Extensions.Count, Is.EqualTo (2), label.Get ());
215                         label.LeaveScope ();
216
217                         label.EnterScope ("Extensions");
218                         WS.SoapBinding soap = null;
219                         XmlElement xml = null;
220                         foreach (var extension in wsb.Extensions) {
221                                 if (extension is WS.SoapBinding)
222                                         soap = (WS.SoapBinding)extension;
223                                 else if (extension is XmlElement)
224                                         xml = (XmlElement)extension;
225                                 else
226                                         Assert.Fail ("Unknown extension.", label);
227                         }
228
229                         Assert.That (soap, Is.Not.Null, label.Get ());
230                         Assert.That (xml, Is.Not.Null, label.Get ());
231                         label.LeaveScope ();
232
233                         label.EnterScope ("Policy");
234                         var assertions = BindingTestAssertions.AssertPolicy (wsdl, xml, label);
235                         Assert.That (assertions.Count, Is.EqualTo (1), label.Get ());
236                         var assertion = assertions [0];
237                         Assert.That (assertion.NamespaceURI, Is.EqualTo ("http://schemas.xmlsoap.org/ws/2004/09/policy/optimizedmimeserialization"), label.Get ());
238                         Assert.That (assertion.LocalName, Is.EqualTo ("OptimizedMimeSerialization"), label.Get ());
239                         label.LeaveScope ();
240                 }
241         }
242 }
243