Skip to content

Commit

Permalink
Twin e2e tests
Browse files Browse the repository at this point in the history
  • Loading branch information
BertKleewein committed Dec 15, 2016
1 parent c8c1edb commit 9d4b28d
Show file tree
Hide file tree
Showing 8 changed files with 600 additions and 0 deletions.
13 changes: 13 additions & 0 deletions e2e/Local.testsettings
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<TestSettings name="Local" id="51d48d4a-c4d1-415f-b3bb-bd0ce1e57a18" xmlns="http://microsoft.com/schemas/VisualStudio/TeamTest/2010">
<Description>These are default test settings for a local test run.</Description>
<Deployment enabled="false" />
<Execution parallelTestCount="1">
<TestTypeSpecific />
<AgentRule name="Execution Agents">
</AgentRule>
</Execution>
<Properties>
<Property name="TestSettingsUIType" value="LoadTest" />
</Properties>
</TestSettings>
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{E22726E9-6526-493F-8D31-0C8011D1B8A2}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>Microsoft.Azure.Devices.E2ETests</RootNamespace>
<AssemblyName>Microsoft.Azure.Devices.E2ETests</AssemblyName>
<TargetFrameworkVersion>v4.5.1</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<ProjectTypeGuids>{3AC096D0-A1C2-E12C-1390-A8335801FDAB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">10.0</VisualStudioVersion>
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
<ReferencePath>$(ProgramFiles)\Common Files\microsoft shared\VSTT\$(VisualStudioVersion)\UITestExtensionPackages</ReferencePath>
<IsCodedUITest>False</IsCodedUITest>
<TestProjectType>UnitTest</TestProjectType>
</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="Microsoft.CSharp" />
<Reference Include="System" />
</ItemGroup>
<Choose>
<When Condition="('$(VisualStudioVersion)' == '10.0' or '$(VisualStudioVersion)' == '') and '$(TargetFrameworkVersion)' == 'v3.5'">
<ItemGroup>
<Reference Include="Microsoft.VisualStudio.QualityTools.UnitTestFramework, Version=10.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL" />
</ItemGroup>
</When>
<Otherwise>
<ItemGroup>
<Reference Include="Microsoft.VisualStudio.QualityTools.UnitTestFramework">
<Private>False</Private>
</Reference>
</ItemGroup>
</Otherwise>
</Choose>
<ItemGroup>
<Compile Include="TwinE2ETests.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\device\Microsoft.Azure.Devices.Client\Microsoft.Azure.Devices.Client.csproj">
<Project>{836B1FB6-70F0-4C7E-AA35-519C2F83AA81}</Project>
<Name>Microsoft.Azure.Devices.Client</Name>
</ProjectReference>
<ProjectReference Include="..\..\service\Microsoft.Azure.Devices\Microsoft.Azure.Devices.csproj">
<Project>{7fc2a03f-36fe-44c5-a1ef-5b1f11458350}</Project>
<Name>Microsoft.Azure.Devices</Name>
</ProjectReference>
<ProjectReference Include="..\..\shared\Microsoft.Azure.Devices.Shared\Microsoft.Azure.Devices.Shared.csproj">
<Project>{835230fc-8d03-4cd1-8845-5a9d1b0b837d}</Project>
<Name>Microsoft.Azure.Devices.Shared</Name>
</ProjectReference>
</ItemGroup>
<Choose>
<When Condition="'$(VisualStudioVersion)' == '10.0' And '$(IsCodedUITest)' == 'True'">
<ItemGroup>
<Reference Include="Microsoft.VisualStudio.QualityTools.CodedUITestFramework, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<Private>False</Private>
</Reference>
<Reference Include="Microsoft.VisualStudio.TestTools.UITest.Common, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<Private>False</Private>
</Reference>
<Reference Include="Microsoft.VisualStudio.TestTools.UITest.Extension, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<Private>False</Private>
</Reference>
<Reference Include="Microsoft.VisualStudio.TestTools.UITesting, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<Private>False</Private>
</Reference>
</ItemGroup>
</When>
</Choose>
<Import Project="$(VSToolsPath)\TeamTest\Microsoft.TestTools.targets" Condition="Exists('$(VSToolsPath)\TeamTest\Microsoft.TestTools.targets')" />
<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>
36 changes: 36 additions & 0 deletions e2e/Microsoft.Azure.Devices.E2ETests/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("Microsoft.Azure.Devices.E2ETests")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("Microsoft.Azure.Devices.E2ETests")]
[assembly: AssemblyCopyright("Copyright © 2016")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]

// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("e22726e9-6526-493f-8d31-0c8011d1b8a2")]

// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
157 changes: 157 additions & 0 deletions e2e/Microsoft.Azure.Devices.E2ETests/TwinE2ETests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Microsoft.Azure.Devices;
using Microsoft.Azure.Devices.Client;
using Microsoft.Azure.Devices.Shared;
using System.Text.RegularExpressions;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;

namespace Microsoft.Azure.Devices.E2ETests
{
[TestClass]
public class TwinE2ETests
{

static string hubConnectionString;
static string deviceName;
static string deviceConnectionString;
static string hostName;

public TestContext TestContext { get; set; }

static string GetHostName(string connectionString)
{
Regex regex = new Regex("HostName=([^;]+)", RegexOptions.None);
return regex.Match(connectionString).Groups[1].Value;
}

static string GetDeviceConnectionString(Device device)
{
var deviceConnectionString = new StringBuilder();
deviceConnectionString.AppendFormat("HostName={0}", hostName);
deviceConnectionString.AppendFormat(";DeviceId={0}", device.Id);
deviceConnectionString.AppendFormat(";SharedAccessKey={0}", device.Authentication.SymmetricKey.PrimaryKey);
return deviceConnectionString.ToString();
}

[ClassInitialize]
static public void ClassInitialize(TestContext testContext)
{
Task.Run(async () =>
{
hubConnectionString = Environment.GetEnvironmentVariable("IOTHUB_CONNECTION_STRING");
deviceName = "E2E_Twin_CSharp_" + Guid.NewGuid().ToString();
deviceConnectionString = null;
hostName = GetHostName(hubConnectionString);

var registryManager = RegistryManager.CreateFromConnectionString(hubConnectionString);
Debug.WriteLine("Creating device " + deviceName);
var device = await registryManager.AddDeviceAsync(new Device(deviceName));
deviceConnectionString = GetDeviceConnectionString(device);
Debug.WriteLine("Device successfully created");
await registryManager.CloseAsync();
}).Wait();
}

[ClassCleanup]
static public void ClassCleanup()
{
Task.Run(async () =>
{
var registryManager = RegistryManager.CreateFromConnectionString(hubConnectionString);

Debug.WriteLine("Removing device " + deviceName);
await registryManager.RemoveDeviceAsync(deviceName);
Debug.WriteLine("Device successfully removed");
await registryManager.CloseAsync();
}).Wait();
}

[TestMethod]
public async Task Twin_Device_Connects_And_Gets_Twin()
{
var deviceClient = DeviceClient.CreateFromConnectionString(deviceConnectionString, Client.TransportType.Mqtt);
var deviceTwin = await deviceClient.GetTwinAsync();
await deviceClient.CloseAsync();
}

[TestMethod]
public async Task Twin_Service_Connects_And_Gets_Twin()
{
var registryManager = RegistryManager.CreateFromConnectionString(hubConnectionString);
var serviceTwin = await registryManager.GetTwinAsync(deviceName);
await registryManager.CloseAsync();

}

[TestMethod]
public async Task Twin_Service_Sets_Desired_Property_And_Device_Receives_Event()
{
var tcs = new TaskCompletionSource<bool>();
var propName = Guid.NewGuid().ToString();
var propValue = Guid.NewGuid().ToString();

var deviceClient = DeviceClient.CreateFromConnectionString(deviceConnectionString, Client.TransportType.Mqtt);
await deviceClient.OpenAsync();
await deviceClient.SetDesiredPropertyUpdateCallback((patch, context) =>
{
return Task.Run(() =>
{
Assert.AreEqual(patch[propName].ToString(), propValue);
tcs.SetResult(true);
});

}, null);

var registryManager = RegistryManager.CreateFromConnectionString(hubConnectionString);
var twinPatch = new Twin();
twinPatch.Properties.Desired[propName] = propValue;
await registryManager.UpdateTwinAsync(deviceName, twinPatch, "*");
await registryManager.CloseAsync();

await tcs.Task;
await deviceClient.CloseAsync();
}

[TestMethod]
public async Task Twin_Service_Sets_Desired_Property_And_Device_Receives_It_On_Next_Get()
{
var propName = Guid.NewGuid().ToString();
var propValue = Guid.NewGuid().ToString();

var registryManager = RegistryManager.CreateFromConnectionString(hubConnectionString);
var twinPatch = new Twin();
twinPatch.Properties.Desired[propName] = propValue;
await registryManager.UpdateTwinAsync(deviceName, twinPatch, "*");
await registryManager.CloseAsync();

var deviceClient = DeviceClient.CreateFromConnectionString(deviceConnectionString, Client.TransportType.Mqtt);
var deviceTwin = await deviceClient.GetTwinAsync();
Assert.AreEqual<string>(deviceTwin.Properties.Desired[propName].ToString(), propValue);
await deviceClient.CloseAsync();
}

[TestMethod]
public async Task Twin_Device_Sets_Reported_Property_And_Service_Receives_It()
{
var propName = Guid.NewGuid().ToString();
var propValue = Guid.NewGuid().ToString();

var deviceClient = DeviceClient.CreateFromConnectionString(deviceConnectionString, Client.TransportType.Mqtt);
var patch = new TwinCollection();
patch[propName] = propValue;
await deviceClient.UpdateReportedPropertiesAsync(patch);
await deviceClient.CloseAsync();

var registryManager = RegistryManager.CreateFromConnectionString(hubConnectionString);
var serviceTwin = await registryManager.GetTwinAsync(deviceName);
Assert.AreEqual<string>(serviceTwin.Properties.Reported[propName].ToString(), propValue);

TestContext.WriteLine("verified " + serviceTwin.Properties.Reported[propName].ToString() + "=" + propValue);
await registryManager.CloseAsync();

}
}
}
15 changes: 15 additions & 0 deletions e2e/build/NuGet.Config
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<packageSources>
<add key="nuget.org" value="https://api.nuget.org/v3/index.json" protocolVersion="3" />
<add key="nuget.org" value="https://www.nuget.org/api/v2/" />
<add key="Local NuGet package source" value="..\..\..\LocalNuget" />
</packageSources>
<packageRestore>
<add key="enabled" value="True" />
<add key="automatic" value="True" />
</packageRestore>
<activePackageSource>
<add key="All" value="(Aggregate source)" />
</activePackageSource>
</configuration>
Loading

0 comments on commit 9d4b28d

Please sign in to comment.