Permalink
Browse files

Added Testing Framework for RestTemplate based code (example) [SPRNET…

…REST-20]
  • Loading branch information...
1 parent ef7fb2c commit 7d76a6481090b060b2bbe6ccb767d463c73bd228 @bbaia bbaia committed Jan 12, 2012
@@ -0,0 +1,28 @@
+Microsoft Visual Studio Solution File, Format Version 11.00
+# Visual Studio 2010
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Spring.RestBucksClient", "src\Spring.RestBucksClient\Spring.RestBucksClient.2010-NET40.csproj", "{CC3E13DE-FF16-4659-A7D8-0BE9F5246A24}"
+EndProject
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Spring.RestBucksClient.Tests", "test\Spring.RestBucksClient.Tests\Spring.RestBucksClient.Tests.2010-NET40.csproj", "{985A302C-4F1B-4D40-948E-3160C6B39C30}"
+EndProject
+Global
+ GlobalSection(SolutionConfigurationPlatforms) = preSolution
+ Debug|Any CPU = Debug|Any CPU
+ Release|Any CPU = Release|Any CPU
+ EndGlobalSection
+ GlobalSection(ProjectConfigurationPlatforms) = postSolution
+ {CC3E13DE-FF16-4659-A7D8-0BE9F5246A24}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+ {CC3E13DE-FF16-4659-A7D8-0BE9F5246A24}.Debug|Any CPU.Build.0 = Debug|Any CPU
+ {CC3E13DE-FF16-4659-A7D8-0BE9F5246A24}.Release|Any CPU.ActiveCfg = Release|Any CPU
+ {CC3E13DE-FF16-4659-A7D8-0BE9F5246A24}.Release|Any CPU.Build.0 = Release|Any CPU
+ {985A302C-4F1B-4D40-948E-3160C6B39C30}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+ {985A302C-4F1B-4D40-948E-3160C6B39C30}.Debug|Any CPU.Build.0 = Debug|Any CPU
+ {985A302C-4F1B-4D40-948E-3160C6B39C30}.Release|Any CPU.ActiveCfg = Release|Any CPU
+ {985A302C-4F1B-4D40-948E-3160C6B39C30}.Release|Any CPU.Build.0 = Release|Any CPU
+ EndGlobalSection
+ GlobalSection(SolutionProperties) = preSolution
+ HideSolutionNode = FALSE
+ EndGlobalSection
+ GlobalSection(ExtensibilityGlobals) = postSolution
+ NAntAddinLastFileName = Spring.build
+ EndGlobalSection
+EndGlobal
@@ -0,0 +1,21 @@
+using System.Collections.Generic;
+using System.Runtime.Serialization;
+
+namespace Spring.RestBucksClient.Api
+{
+ [DataContract(Name="order", Namespace="http://restbucks.com")]
+ public class Order
+ {
+ [DataMember(Name = "location", Order = 0)]
+ public string Location { get; set; }
+
+ [DataMember(Name = "cost", EmitDefaultValue = false, Order = 1)]
+ public decimal Cost { get; set; }
+
+ [DataMember(Name = "items", Order = 2)]
+ public List<OrderItem> Items { get; set; }
+
+ [DataMember(Name = "status", EmitDefaultValue = false, Order = 3)]
+ public string Status { get; set; }
+ }
+}
@@ -0,0 +1,14 @@
+using System.Runtime.Serialization;
+
+namespace Spring.RestBucksClient.Api
+{
+ [DataContract(Name = "item", Namespace = "http://restbucks.com")]
+ public class OrderItem
+ {
+ [DataMember(Name = "name", Order = 0)]
+ public string Name { get; set; }
+
+ [DataMember(Name = "quantity", Order = 1)]
+ public int Quantity { get; set; }
+ }
+}
@@ -0,0 +1,49 @@
+using System;
+
+using Spring.Http;
+using Spring.Http.Converters.Xml;
+using Spring.Rest.Client;
+
+namespace Spring.RestBucksClient.Api
+{
+ /// <summary>
+ /// Basic REST Client for the example of the book "REST in practice": http://restinpractice.com/.
+ /// Uses http://restbuckson.net/ for the server side.
+ /// </summary>
+ public class RestBucksClient
+ {
+ private const string BaseUrl = "http://restbuckson.net/";
+
+ private RestTemplate restTemplate;
+
+ /// <summary>
+ /// Gets a reference to the REST client backing this API binding and used to perform API calls.
+ /// </summary>
+ public RestTemplate RestTemplate
+ {
+ get { return this.restTemplate; }
+ }
+
+ public RestBucksClient()
+ {
+ // Create and configure RestTemplate
+ restTemplate = new RestTemplate(BaseUrl);
+ DataContractHttpMessageConverter xmlConverter = new DataContractHttpMessageConverter();
+ xmlConverter.SupportedMediaTypes = new MediaType[]{ new MediaType("application", "vnd.restbucks+xml") };
+ restTemplate.MessageConverters.Add(xmlConverter);
+ }
+
+
+ // Methods
+
+ public Uri CreateOrder(Order order)
+ {
+ return restTemplate.PostForLocation("orders", order);
+ }
+
+ public Order GetOrder(Uri orderUri)
+ {
+ return restTemplate.GetForObject<Order>(orderUri);
+ }
+ }
+}
@@ -0,0 +1,54 @@
+<?xml version="1.0" encoding="utf-8"?>
+<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
+ <PropertyGroup>
+ <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
+ <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
+ <ProductVersion>8.0.30703</ProductVersion>
+ <SchemaVersion>2.0</SchemaVersion>
+ <ProjectGuid>{CC3E13DE-FF16-4659-A7D8-0BE9F5246A24}</ProjectGuid>
+ <OutputType>Library</OutputType>
+ <AppDesignerFolder>Properties</AppDesignerFolder>
+ <RootNamespace>Spring.RestBucksClient</RootNamespace>
+ <AssemblyName>Spring.RestBucksClient</AssemblyName>
+ <TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
+ <FileAlignment>512</FileAlignment>
+ <TargetFrameworkProfile>Client</TargetFrameworkProfile>
+ </PropertyGroup>
+ <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
+ <DebugSymbols>true</DebugSymbols>
+ <DebugType>full</DebugType>
+ <Optimize>false</Optimize>
+ <OutputPath>bin\Debug\</OutputPath>
+ <DefineConstants>DEBUG;TRACE</DefineConstants>
+ <ErrorReport>prompt</ErrorReport>
+ <WarningLevel>4</WarningLevel>
+ </PropertyGroup>
+ <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
+ <DebugType>pdbonly</DebugType>
+ <Optimize>true</Optimize>
+ <OutputPath>bin\Release\</OutputPath>
+ <DefineConstants>TRACE</DefineConstants>
+ <ErrorReport>prompt</ErrorReport>
+ <WarningLevel>4</WarningLevel>
+ </PropertyGroup>
+ <ItemGroup>
+ <Reference Include="Spring.Rest">
+ <HintPath>..\..\..\..\bin\net\4.0\Debug\Spring.Rest.dll</HintPath>
+ </Reference>
+ <Reference Include="System" />
+ <Reference Include="System.Runtime.Serialization" />
+ </ItemGroup>
+ <ItemGroup>
+ <Compile Include="Api\OrderItem.cs" />
+ <Compile Include="Api\Order.cs" />
+ <Compile Include="Api\RestBucksClient.cs" />
+ </ItemGroup>
+ <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
+ <!-- To modify your build process, add your task inside one of the targets below and uncomment it.
+ Other similar extension points exist, see Microsoft.Common.targets.
+ <Target Name="BeforeBuild">
+ </Target>
+ <Target Name="AfterBuild">
+ </Target>
+ -->
+</Project>
@@ -0,0 +1,17 @@
+<order xmlns="http://restbucks.com">
+ <links>
+ <link uri="http://restbuckson.net/order/2818050" rel="http://restbuckson.net/docs/order-get.htm" mediaType="application/vnd.restbucks+xml"/>
+ <link uri="http://restbuckson.net/order/2818050" rel="http://restbuckson.net/docs/order-update.htm" mediaType="application/vnd.restbucks+xml"/>
+ <link uri="http://restbuckson.net/order/2818050" rel="http://restbuckson.net/docs/order-cancel.htm" mediaType="application/vnd.restbucks+xml"/>
+ <link uri="http://restbuckson.net/order/2818050/payment" rel="http://restbuckson.net/docs/order-pay.htm" mediaType="application/vnd.restbucks+xml"/>
+ </links>
+ <location>inShop</location>
+ <cost>7.60000</cost>
+ <items>
+ <item>
+ <name>Latte</name>
+ <quantity>1</quantity>
+ </item>
+ </items>
+ <status>unpaid</status>
+</order>
@@ -0,0 +1,95 @@
+using System;
+using System.Collections.Generic;
+
+using NUnit.Framework; // Here we are using NUnit, but any other unit test framework will work as well.
+
+using Spring.IO;
+using Spring.Http;
+using Spring.Rest.Client.Testing;
+
+namespace Spring.RestBucksClient.Api
+{
+ /// <summary>
+ /// Unit tests for the RestBucksClient class.
+ /// </summary>
+ [TestFixture]
+ public sealed class RestBucksClientTests
+ {
+ private RestBucksClient restBucksClient;
+ private MockRestServiceServer mockServer;
+ private HttpHeaders responseHeaders;
+
+ [SetUp]
+ public void Setup()
+ {
+ restBucksClient = new RestBucksClient();
+ mockServer = MockRestServiceServer.CreateServer(this.restBucksClient.RestTemplate);
+
+ responseHeaders = new HttpHeaders();
+ responseHeaders.ContentType = new MediaType("application", "vnd.restbucks+xml");
+ }
+
+ [TearDown]
+ public void TearDown()
+ {
+ this.mockServer.Verify();
+ }
+
+
+ // Test methods
+
+ [Test]
+ public void CreateOrder()
+ {
+ responseHeaders.Location = new Uri("http://restbucks.com/order/123456");
+ mockServer.ExpectNewRequest()
+ .AndExpectUri("http://restbuckson.net/orders")
+ .AndExpectMethod(HttpMethod.POST)
+ .AndExpectBody("<order xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns=\"http://restbucks.com\"><location>inShop</location><items><item><name>Latte</name><quantity>1</quantity></item></items></order>")
+ .AndRespondWith("", responseHeaders);
+
+ Order order = new Order()
+ {
+ Location = "inShop",
+ Items = new List<OrderItem>()
+ {
+ new OrderItem()
+ {
+ Name = "Latte",
+ Quantity = 1
+ }
+ }
+ };
+ Uri uri = restBucksClient.CreateOrder(order);
+ Assert.AreEqual(responseHeaders.Location, uri);
+ }
+
+ [Test]
+ public void GetOrder()
+ {
+ Uri orderUri = new Uri("http://restbuckson.net/order/123456");
+ mockServer.ExpectNewRequest()
+ .AndExpectUri(orderUri)
+ .AndExpectMethod(HttpMethod.GET)
+ .AndRespondWith(XmlResource("Order"), responseHeaders);
+
+ Order order = restBucksClient.GetOrder(orderUri);
+ Assert.IsNotNull(order);
+ Assert.AreEqual("inShop", order.Location);
+ Assert.AreEqual(7.6, order.Cost);
+ Assert.AreEqual(1, order.Items.Count);
+ Assert.AreEqual("Latte", order.Items[0].Name);
+ Assert.AreEqual(1, order.Items[0].Quantity);
+ Assert.AreEqual("unpaid", order.Status);
+ }
+
+
+ // Helper methods
+
+ private static IResource XmlResource(string filename)
+ {
+ // Note: Default namespace is 'Spring.RestBucksClient'
+ return new AssemblyResource("assembly://Spring.RestBucksClient.Tests/Spring.RestBucksClient.Api/" + filename + ".xml");
+ }
+ }
+}
@@ -0,0 +1,66 @@
+<?xml version="1.0" encoding="utf-8"?>
+<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
+ <PropertyGroup>
+ <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
+ <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
+ <ProductVersion>8.0.30703</ProductVersion>
+ <SchemaVersion>2.0</SchemaVersion>
+ <ProjectGuid>{985A302C-4F1B-4D40-948E-3160C6B39C30}</ProjectGuid>
+ <OutputType>Library</OutputType>
+ <AppDesignerFolder>Properties</AppDesignerFolder>
+ <RootNamespace>Spring.RestBucksClient</RootNamespace>
+ <AssemblyName>Spring.RestBucksClient.Tests</AssemblyName>
+ <TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
+ <FileAlignment>512</FileAlignment>
+ </PropertyGroup>
+ <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
+ <DebugSymbols>true</DebugSymbols>
+ <DebugType>full</DebugType>
+ <Optimize>false</Optimize>
+ <OutputPath>bin\Debug\</OutputPath>
+ <DefineConstants>DEBUG;TRACE</DefineConstants>
+ <ErrorReport>prompt</ErrorReport>
+ <WarningLevel>4</WarningLevel>
+ </PropertyGroup>
+ <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
+ <DebugType>pdbonly</DebugType>
+ <Optimize>true</Optimize>
+ <OutputPath>bin\Release\</OutputPath>
+ <DefineConstants>TRACE</DefineConstants>
+ <ErrorReport>prompt</ErrorReport>
+ <WarningLevel>4</WarningLevel>
+ </PropertyGroup>
+ <ItemGroup>
+ <Reference Include="nunit.framework, Version=2.5.4.10098, Culture=neutral, PublicKeyToken=96d09a1eb7f44a77, processorArchitecture=MSIL">
+ <SpecificVersion>False</SpecificVersion>
+ <HintPath>..\..\..\..\lib\net\2.0\nunit.framework.dll</HintPath>
+ </Reference>
+ <Reference Include="Spring.Rest">
+ <HintPath>..\..\..\..\bin\net\4.0\Debug\Spring.Rest.dll</HintPath>
+ </Reference>
+ <Reference Include="Spring.Rest.Testing">
+ <HintPath>..\..\..\..\bin\net\4.0\Debug\Spring.Rest.Testing.dll</HintPath>
+ </Reference>
+ <Reference Include="System" />
+ </ItemGroup>
+ <ItemGroup>
+ <Compile Include="Api\RestBucksClientTests.cs" />
+ </ItemGroup>
+ <ItemGroup>
+ <ProjectReference Include="..\..\src\Spring.RestBucksClient\Spring.RestBucksClient.2010-NET40.csproj">
+ <Project>{CC3E13DE-FF16-4659-A7D8-0BE9F5246A24}</Project>
+ <Name>Spring.RestBucksClient</Name>
+ </ProjectReference>
+ </ItemGroup>
+ <ItemGroup>
+ <EmbeddedResource Include="Api\Order.xml" />
+ </ItemGroup>
+ <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
+ <!-- To modify your build process, add your task inside one of the targets below and uncomment it.
+ Other similar extension points exist, see Microsoft.Common.targets.
+ <Target Name="BeforeBuild">
+ </Target>
+ <Target Name="AfterBuild">
+ </Target>
+ -->
+</Project>

0 comments on commit 7d76a64

Please sign in to comment.