Skip to content

Commit

Permalink
Add support for message with content type and user properties (#263)
Browse files Browse the repository at this point in the history
  • Loading branch information
josesimoes authored Nov 23, 2022
1 parent ed01470 commit 87e5996
Show file tree
Hide file tree
Showing 10 changed files with 448 additions and 4 deletions.
15 changes: 15 additions & 0 deletions .runsettings
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="utf-8"?>
<RunSettings>
<!-- Configurations that affect the Test Framework -->
<RunConfiguration>
<MaxCpuCount>1</MaxCpuCount>
<ResultsDirectory>.\TestResults</ResultsDirectory><!-- Path relative to solution directory -->
<TestSessionTimeout>120000</TestSessionTimeout><!-- Milliseconds -->
<TargetFrameworkVersion>net48</TargetFrameworkVersion>
<TargetPlatform>x64</TargetPlatform>
</RunConfiguration>
<nanoFrameworkAdapter>
<Logging>None</Logging>
<IsRealHardware>False</IsRealHardware>
</nanoFrameworkAdapter>
</RunSettings>
87 changes: 87 additions & 0 deletions Tests/DeviceClientTests/DeviceClientTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
//
// Copyright (c) .NET Foundation and Contributors
// See LICENSE file in the project root for full license information.
//

using nanoFramework.Azure.Devices.Client;
using nanoFramework.M2Mqtt.Messages;
using nanoFramework.TestFramework;
using System;
using System.Collections;

namespace DeviceClientTests
{
[TestClass]
public class DeviceClientTests
{
private static string _propertyOneName = "prop1";
private static string _propertyTwoName = "prop2";
private static string _propertyThreeName = "prop3";
private static string _propertyOneValue = "iAmValue1";
private static float _propertyTwoValue = 33.44f;
private static string _propertyThreeValue = "string with $/#% chars";

private static UserProperty _userProperty1 = new(_propertyOneName, _propertyOneValue);
private static UserProperty _userProperty2 = new(_propertyTwoName, _propertyTwoValue.ToString());
private static UserProperty _userProperty3 = new(_propertyThreeName, _propertyThreeValue);

private static UserProperty _userPropertyBad1 = new(null, _propertyOneValue);
private static UserProperty _userPropertyBad2 = new(_propertyTwoName, null);

[TestMethod]
public void EncodeUserPropertiesTest_00()
{
DeviceClient client = new();

var encodedProperties = client.EncodeUserProperties(new ArrayList() { _userProperty1, _userProperty2, _userProperty3 });

Assert.Equal(encodedProperties, "prop1=iAmValue1&prop2=33.44&prop3=string+with+%24%2F%23%25+chars");
}

[TestMethod]
public void EncodeUserPropertiesTest_01()
{
DeviceClient client = new();

Assert.Throws(typeof(ArgumentException), () =>
{
client.EncodeUserProperties(new ArrayList() { _userProperty3, _userPropertyBad1 });
},
"Expecting ArgumentException with invalid user property 01."
);

Assert.Throws(typeof(ArgumentException), () =>
{
client.EncodeUserProperties(new ArrayList() { _userPropertyBad2, _userProperty3 });
},
"Expecting ArgumentException with invalid user property 02."
);

Assert.Throws(typeof(InvalidCastException), () =>
{
client.EncodeUserProperties(new ArrayList() { _userProperty1, "Invalid property" });
},
"Expecting ArgumentException with invalid user property 03."
);

Assert.Throws(typeof(InvalidCastException), () =>
{
client.EncodeUserProperties(new ArrayList() { 8888888, "Invalid property" });
},
"Expecting ArgumentException with invalid user property 04."
);
}

[DataRow("application/json", "$.ct=application%2Fjson&$.ce=utf-8")]
[DataRow("application/mime", "$.ct=application%2Fmime&$.ce=utf-8")]
[TestMethod]
public void EncodeContentType_00(string contentType, string encodedContentType)
{
DeviceClient client = new();

Assert.Equal(
client.EncodeContentType(contentType),
encodedContentType);
}
}
}
94 changes: 94 additions & 0 deletions Tests/DeviceClientTests/DeviceClientTests.nfproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="Current" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup Label="Globals">
<NanoFrameworkProjectSystemPath>$(MSBuildExtensionsPath)\nanoFramework\v1.0\</NanoFrameworkProjectSystemPath>
</PropertyGroup>
<Import Project="$(NanoFrameworkProjectSystemPath)NFProjectSystem.Default.props" Condition="Exists('$(NanoFrameworkProjectSystemPath)NFProjectSystem.Default.props')" />
<ItemGroup>
<ProjectCapability Include="TestContainer" />
</ItemGroup>
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectTypeGuids>{11A8DD76-328B-46DF-9F39-F559912D0360};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
<ProjectGuid>557ef898-6cf2-4cbc-ae29-72f387ff0ee0</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<FileAlignment>512</FileAlignment>
<RootNamespace>DeviceClientTests</RootNamespace>
<AssemblyName>NFUnitTest</AssemblyName>
<IsCodedUITest>False</IsCodedUITest>
<IsTestProject>true</IsTestProject>
<TestProjectType>UnitTest</TestProjectType>
<TargetFrameworkVersion>v1.0</TargetFrameworkVersion>
</PropertyGroup>
<PropertyGroup>
<SignAssembly>true</SignAssembly>
</PropertyGroup>
<PropertyGroup>
<AssemblyOriginatorKeyFile>..\..\nanoFramework.Azure.Devices.Client\key.snk</AssemblyOriginatorKeyFile>
</PropertyGroup>
<PropertyGroup>
<DelaySign>false</DelaySign>
</PropertyGroup>
<Import Project="$(NanoFrameworkProjectSystemPath)NFProjectSystem.props" Condition="Exists('$(NanoFrameworkProjectSystemPath)NFProjectSystem.props')" />
<ItemGroup>
<Compile Include="DeviceClientTests.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<Reference Include="mscorlib">
<HintPath>..\..\packages\nanoFramework.CoreLibrary.1.12.0\lib\mscorlib.dll</HintPath>
</Reference>
<Reference Include="nanoFramework.M2Mqtt">
<HintPath>..\..\packages\nanoFramework.M2Mqtt.5.1.59\lib\nanoFramework.M2Mqtt.dll</HintPath>
</Reference>
<Reference Include="nanoFramework.Runtime.Events">
<HintPath>..\..\packages\nanoFramework.Runtime.Events.1.11.1\lib\nanoFramework.Runtime.Events.dll</HintPath>
</Reference>
<Reference Include="nanoFramework.Runtime.Native">
<HintPath>..\..\packages\nanoFramework.Runtime.Native.1.5.4\lib\nanoFramework.Runtime.Native.dll</HintPath>
</Reference>
<Reference Include="nanoFramework.System.Collections">
<HintPath>..\..\packages\nanoFramework.System.Collections.1.4.0\lib\nanoFramework.System.Collections.dll</HintPath>
</Reference>
<Reference Include="nanoFramework.System.Text">
<HintPath>..\..\packages\nanoFramework.System.Text.1.2.22\lib\nanoFramework.System.Text.dll</HintPath>
</Reference>
<Reference Include="nanoFramework.TestFramework">
<HintPath>..\..\packages\nanoFramework.TestFramework.2.0.60\lib\nanoFramework.TestFramework.dll</HintPath>
</Reference>
<Reference Include="nanoFramework.UnitTestLauncher">
<HintPath>..\..\packages\nanoFramework.TestFramework.2.0.60\lib\nanoFramework.UnitTestLauncher.exe</HintPath>
</Reference>
<Reference Include="System.IO.Streams">
<HintPath>..\..\packages\nanoFramework.System.IO.Streams.1.1.27\lib\System.IO.Streams.dll</HintPath>
</Reference>
<Reference Include="System.Net">
<HintPath>..\..\packages\nanoFramework.System.Net.1.10.38\lib\System.Net.dll</HintPath>
</Reference>
<Reference Include="System.Threading">
<HintPath>..\..\packages\nanoFramework.System.Threading.1.1.8\lib\System.Threading.dll</HintPath>
</Reference>
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\nanoFramework.Azure.Devices.Client\Azure.Devices.Client.nfproj" />
</ItemGroup>
<Import Project="$(NanoFrameworkProjectSystemPath)NFProjectSystem.CSharp.targets" Condition="Exists('$(NanoFrameworkProjectSystemPath)NFProjectSystem.CSharp.targets')" />
<!-- MANUAL UPDATE HERE -->
<Import Project="..\..\packages\nanoFramework.TestFramework.2.0.60\build\nanoFramework.TestFramework.targets" Condition="Exists('..\..\packages\nanoFramework.TestFramework.2.0.60\build\nanoFramework.TestFramework.targets')" />
<ProjectExtensions>
<ProjectCapabilities>
<ProjectConfigurationsDeclaredAsItems />
</ProjectCapabilities>
</ProjectExtensions>
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
<PropertyGroup>
<WarningText>Update the Import path in nfproj to the correct nanoFramework.TestFramework NuGet package folder.</WarningText>
</PropertyGroup>
<Warning Condition="!Exists('..\..\packages\nanoFramework.TestFramework.2.0.60\build\nanoFramework.TestFramework.targets')" Text="'$(WarningText)'" />
</Target>
</Project>
31 changes: 31 additions & 0 deletions Tests/DeviceClientTests/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
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: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyCopyright("Copyright (c) 2021 nanoFramework contributors")]
[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)]

// 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")]
13 changes: 13 additions & 0 deletions Tests/DeviceClientTests/packages.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="nanoFramework.CoreLibrary" version="1.12.0" targetFramework="netnano1.0" />
<package id="nanoFramework.M2Mqtt" version="5.1.59" targetFramework="netnano1.0" />
<package id="nanoFramework.Runtime.Events" version="1.11.1" targetFramework="netnano1.0" />
<package id="nanoFramework.Runtime.Native" version="1.5.4" targetFramework="netnano1.0" />
<package id="nanoFramework.System.Collections" version="1.4.0" targetFramework="netnano1.0" />
<package id="nanoFramework.System.IO.Streams" version="1.1.27" targetFramework="netnano1.0" />
<package id="nanoFramework.System.Net" version="1.10.38" targetFramework="netnano1.0" />
<package id="nanoFramework.System.Text" version="1.2.22" targetFramework="netnano1.0" />
<package id="nanoFramework.System.Threading" version="1.1.8" targetFramework="netnano1.0" />
<package id="nanoFramework.TestFramework" version="2.0.60" targetFramework="netnano1.0" developmentDependency="true" />
</packages>
67 changes: 67 additions & 0 deletions Tests/DeviceClientTests/packages.lock.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
{
"version": 1,
"dependencies": {
".NETnanoFramework,Version=v1.0": {
"nanoFramework.CoreLibrary": {
"type": "Direct",
"requested": "[1.12.0, 1.12.0]",
"resolved": "1.12.0",
"contentHash": "qQrFNXmJiStMC4VXk5cVMOJp23/qlT9FW5i9i+igwQVwraQTtvpkam8yK1hj992jqrbjoCIFZP4Hw9E8H0pB7w=="
},
"nanoFramework.M2Mqtt": {
"type": "Direct",
"requested": "[5.1.59, 5.1.59]",
"resolved": "5.1.59",
"contentHash": "/q/+sKyASsut6vz9f8V1/BQqdBt8H9WVurabW/WLvxq5I14+Ov8wDt7SnBOv4X4fwys88gfTHsCsX7ZX7Z0BcA=="
},
"nanoFramework.Runtime.Events": {
"type": "Direct",
"requested": "[1.11.1, 1.11.1]",
"resolved": "1.11.1",
"contentHash": "hHRPhNagq1T0oN9QJfPOqreuWUV42DJamT0f7GrPQvrOYcr9ne4YTZq8MIpudvKtSALs50iYqxJ+raOF2CiNsA=="
},
"nanoFramework.Runtime.Native": {
"type": "Direct",
"requested": "[1.5.4, 1.5.4]",
"resolved": "1.5.4",
"contentHash": "qfaOY1O5TOEw//MO4AOWRq5CdZXTfd3KnsGTNU2yw+IEYKiQLPkJhtF3ufF/S04xfXL556S2kOFG3/RZrwQ4Qw=="
},
"nanoFramework.System.Collections": {
"type": "Direct",
"requested": "[1.4.0, 1.4.0]",
"resolved": "1.4.0",
"contentHash": "/yFwxtCFzi+24NuyxcwlH1YyBGOxRX4oHGLwVmFbgbvOyx3ny/Mwyk2YjHTzmTSgUg9C2XxPF+EkXWwCOAkytw=="
},
"nanoFramework.System.IO.Streams": {
"type": "Direct",
"requested": "[1.1.27, 1.1.27]",
"resolved": "1.1.27",
"contentHash": "9ho/C/ZIQrBw51UnzLVbSc//kE1mDcgykGEZH8p+A5Q4R7JMVwdEKlNQXWr1VHEkGbv2wbyos6nRLqu2rdjpwA=="
},
"nanoFramework.System.Net": {
"type": "Direct",
"requested": "[1.10.38, 1.10.38]",
"resolved": "1.10.38",
"contentHash": "vMGSqPoJvvXmpJXir/QH7vgj3n3M/705IQtmWRo0a99HdVaXnc0iZmmRtLi8JKNGngwEDZ0ge4+ZBBqqtSsyrw=="
},
"nanoFramework.System.Text": {
"type": "Direct",
"requested": "[1.2.22, 1.2.22]",
"resolved": "1.2.22",
"contentHash": "vLvU0II3oJfajQ8MgNm8aCkaQ2JhjznzruwksOorbMJf86zLRbA5NUeg9X/KjbAE5pIalitUOqtNLKorYTbYGg=="
},
"nanoFramework.System.Threading": {
"type": "Direct",
"requested": "[1.1.8, 1.1.8]",
"resolved": "1.1.8",
"contentHash": "oES5GN3KHoDzifRNr06WM7P9NaQf+kDmIYkr1ETR2awmERHz4sRpECduGEatwyo1vMhMvZY/KoBcEpAyKNbDgQ=="
},
"nanoFramework.TestFramework": {
"type": "Direct",
"requested": "[2.0.60, 2.0.60]",
"resolved": "2.0.60",
"contentHash": "Sl+jB89CV3bAX4lmVoJYF/75a4EOjyfQSgaa6sPlUerUEI24y8KK4+s3EOv9UJFz6MGgJWJkQIas6D0Zv1jELw=="
}
}
}
}
2 changes: 2 additions & 0 deletions azure-pipelines.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ steps:
- template: azure-pipelines-templates/class-lib-build.yml@templates
parameters:
sonarCloudProject: 'nanoframework_Azure.Devices'
runUnitTests: true
unitTestRunsettings: '$(System.DefaultWorkingDirectory)\.runsettings'

# step from template @ nf-tools repo
# report error
Expand Down
14 changes: 14 additions & 0 deletions nanoFramework.Azure.Devices.Client.sln
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@ Project("{11A8DD76-328B-46DF-9F39-F559912D0360}") = "TwinTests", "Tests\TwinTest
EndProject
Project("{11A8DD76-328B-46DF-9F39-F559912D0360}") = "CryptoTests", "Tests\CryptoTests\CryptoTests.nfproj", "{18856152-0A46-4B7F-BF38-82313AEB61F8}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{29CB2BEC-F0F1-4B2B-A2A5-DCC9063F4BF0}"
ProjectSection(SolutionItems) = preProject
.runsettings = .runsettings
EndProjectSection
EndProject
Project("{11A8DD76-328B-46DF-9F39-F559912D0360}") = "DeviceClientTests", "Tests\DeviceClientTests\DeviceClientTests.nfproj", "{557EF898-6CF2-4CBC-AE29-72F387FF0EE0}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -42,13 +49,20 @@ Global
{18856152-0A46-4B7F-BF38-82313AEB61F8}.Release|Any CPU.ActiveCfg = Release|Any CPU
{18856152-0A46-4B7F-BF38-82313AEB61F8}.Release|Any CPU.Build.0 = Release|Any CPU
{18856152-0A46-4B7F-BF38-82313AEB61F8}.Release|Any CPU.Deploy.0 = Release|Any CPU
{557EF898-6CF2-4CBC-AE29-72F387FF0EE0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{557EF898-6CF2-4CBC-AE29-72F387FF0EE0}.Debug|Any CPU.Build.0 = Debug|Any CPU
{557EF898-6CF2-4CBC-AE29-72F387FF0EE0}.Debug|Any CPU.Deploy.0 = Debug|Any CPU
{557EF898-6CF2-4CBC-AE29-72F387FF0EE0}.Release|Any CPU.ActiveCfg = Release|Any CPU
{557EF898-6CF2-4CBC-AE29-72F387FF0EE0}.Release|Any CPU.Build.0 = Release|Any CPU
{557EF898-6CF2-4CBC-AE29-72F387FF0EE0}.Release|Any CPU.Deploy.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{50BED153-FF67-4207-ADB0-2651DDADC2A3} = {439F8827-FED8-45E0-9DA0-9DE277A29722}
{18856152-0A46-4B7F-BF38-82313AEB61F8} = {439F8827-FED8-45E0-9DA0-9DE277A29722}
{557EF898-6CF2-4CBC-AE29-72F387FF0EE0} = {439F8827-FED8-45E0-9DA0-9DE277A29722}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {C967FC56-9F2D-40AD-8B89-52B96DFD776B}
Expand Down
Loading

0 comments on commit 87e5996

Please sign in to comment.