Skip to content

Commit

Permalink
Make UpstreamProtocol case insensitive (#264)
Browse files Browse the repository at this point in the history
* Make UpstreamProtocol case insensitive

* Add Tests
  • Loading branch information
varunpuranik authored and myagley committed Sep 11, 2018
1 parent 3afce1b commit f48c780
Show file tree
Hide file tree
Showing 5 changed files with 126 additions and 3 deletions.
9 changes: 9 additions & 0 deletions Microsoft.Azure.Devices.Edge.sln
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "bin", "bin", "{3A879277-5A6
edge-modules\functions\samples\bin\extensions.json = edge-modules\functions\samples\bin\extensions.json
EndProjectSection
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.Azure.Devices.Edge.Hub.Service.Test", "edge-hub\test\Microsoft.Azure.Devices.Edge.Hub.Service.Test\Microsoft.Azure.Devices.Edge.Hub.Service.Test.csproj", "{9BED8F14-63E9-4B4B-88F7-659D5E522CBD}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
CodeCoverage|Any CPU = CodeCoverage|Any CPU
Expand Down Expand Up @@ -419,6 +421,12 @@ Global
{B8D5312A-B37B-4FA3-8B80-2D1A93077DDF}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B8D5312A-B37B-4FA3-8B80-2D1A93077DDF}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B8D5312A-B37B-4FA3-8B80-2D1A93077DDF}.Release|Any CPU.Build.0 = Release|Any CPU
{9BED8F14-63E9-4B4B-88F7-659D5E522CBD}.CodeCoverage|Any CPU.ActiveCfg = CodeCoverage|Any CPU
{9BED8F14-63E9-4B4B-88F7-659D5E522CBD}.CodeCoverage|Any CPU.Build.0 = CodeCoverage|Any CPU
{9BED8F14-63E9-4B4B-88F7-659D5E522CBD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{9BED8F14-63E9-4B4B-88F7-659D5E522CBD}.Debug|Any CPU.Build.0 = Debug|Any CPU
{9BED8F14-63E9-4B4B-88F7-659D5E522CBD}.Release|Any CPU.ActiveCfg = Release|Any CPU
{9BED8F14-63E9-4B4B-88F7-659D5E522CBD}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -483,6 +491,7 @@ Global
{C3BDC9FA-B7D8-44F3-970F-D24281335F46} = {33B7755E-D6F9-4F9B-86A8-5DFE9FEE674E}
{B8D5312A-B37B-4FA3-8B80-2D1A93077DDF} = {C3BDC9FA-B7D8-44F3-970F-D24281335F46}
{3A879277-5A61-4A6A-BB27-5281FA83B1D3} = {33B7755E-D6F9-4F9B-86A8-5DFE9FEE674E}
{9BED8F14-63E9-4B4B-88F7-659D5E522CBD} = {63969606-14B2-4D9D-AB72-A5D60D22037C}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {D71830F5-3AF5-46B4-8A9E-1DCE4F2253AC}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// Copyright (c) Microsoft. All rights reserved.

using System.Runtime.CompilerServices;

[assembly: InternalsVisibleTo("Microsoft.Azure.Devices.Edge.Hub.Service.Test")]
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,7 @@ IContainer BuildContainer(IServiceCollection services)
(bool isEnabled, bool usePersistentStorage, StoreAndForwardConfiguration config, string storagePath) storeAndForward = this.GetStoreAndForwardConfiguration();

IConfiguration mqttSettingsConfiguration = this.Configuration.GetSection("mqttSettings");
Option<UpstreamProtocol> upstreamProtocolOption = Enum.TryParse(this.Configuration.GetValue("UpstreamProtocol", string.Empty), false, out UpstreamProtocol upstreamProtocol)
? Option.Some(upstreamProtocol)
: Option.None<UpstreamProtocol>();
Option<UpstreamProtocol> upstreamProtocolOption = GetUpstreamProtocol(this.Configuration);
int connectivityCheckFrequencySecs = this.Configuration.GetValue("ConnectivityCheckFrequencySecs", 300);
TimeSpan connectivityCheckFrequency = connectivityCheckFrequencySecs < 0 ? TimeSpan.MaxValue : TimeSpan.FromSeconds(connectivityCheckFrequencySecs);

Expand Down Expand Up @@ -191,6 +189,11 @@ IContainer BuildContainer(IServiceCollection services)
return container;
}

internal static Option<UpstreamProtocol> GetUpstreamProtocol(IConfigurationRoot configuration) =>
Enum.TryParse(configuration.GetValue("UpstreamProtocol", string.Empty), true, out UpstreamProtocol upstreamProtocol)
? Option.Some(upstreamProtocol)
: Option.None<UpstreamProtocol>();

public void Configure(IApplicationBuilder app)
{
var webSocketListenerRegistry = app.ApplicationServices.GetService(typeof(IWebSocketListenerRegistry)) as IWebSocketListenerRegistry;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp2.1</TargetFramework>
<TreatWarningsAsErrors>True</TreatWarningsAsErrors>
<Configurations>Debug;Release;CodeCoverage</Configurations>
<HighEntropyVA>true</HighEntropyVA>
</PropertyGroup>

<!--
Normally, the 'Debug' configuration would work for code coverage, but Microsoft.CodeCoverage currently requires '<DebugType>full</DebugType>' for .NET Core.
See https://github.com/Microsoft/vstest-docs/blob/06f9dc0aeb47be7204dc4e1a98c110ead3e978c7/docs/analyze.md#setup-a-project.
That setting seems to break the "Open Test" context menu in VS IDE, so we'll use a dedicated configuration for code coverage.
-->
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'CodeCoverage|AnyCPU' ">
<IntermediateOutputPath>obj\CodeCoverage</IntermediateOutputPath>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\CodeCoverage</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.5.0" />
<PackageReference Include="Moq" Version="4.8.2" />
<PackageReference Include="xunit" Version="2.2.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.2.0" />
<PackageReference Include="Microsoft.CodeCoverage" Version="1.0.3" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\..\edge-util\test\Microsoft.Azure.Devices.Edge.Util.Test.Common\Microsoft.Azure.Devices.Edge.Util.Test.Common.csproj" />
<ProjectReference Include="..\..\src\Microsoft.Azure.Devices.Edge.Hub.Service\Microsoft.Azure.Devices.Edge.Hub.Service.csproj" />
</ItemGroup>

<ItemGroup>
<Service Include="{82a7f48d-3b50-4b1e-b82e-3ada8210c358}" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// Copyright (c) Microsoft. All rights reserved.

namespace Microsoft.Azure.Devices.Edge.Hub.Service.Test
{
using System.Collections;
using System.Collections.Generic;
using Microsoft.Azure.Devices.Edge.Hub.CloudProxy;
using Microsoft.Azure.Devices.Edge.Util;
using Microsoft.Azure.Devices.Edge.Util.Test.Common;
using Microsoft.Extensions.Configuration;
using Moq;
using Xunit;

[Unit]
public class StartupTest
{
static IEnumerable<object[]> GetUpstreamProtocolData()
{
yield return new object[] { "Mqtt", Option.Some(UpstreamProtocol.Mqtt) };
yield return new object[] { "MQTT", Option.Some(UpstreamProtocol.Mqtt) };
yield return new object[] { "mqtt", Option.Some(UpstreamProtocol.Mqtt) };
yield return new object[] { "MqTt", Option.Some(UpstreamProtocol.Mqtt) };

yield return new object[] { "Amqp", Option.Some(UpstreamProtocol.Amqp) };
yield return new object[] { "AMQP", Option.Some(UpstreamProtocol.Amqp) };
yield return new object[] { "amqp", Option.Some(UpstreamProtocol.Amqp) };
yield return new object[] { "AmqP", Option.Some(UpstreamProtocol.Amqp) };

yield return new object[] { "MqttWs", Option.Some(UpstreamProtocol.MqttWs) };
yield return new object[] { "MQTTWS", Option.Some(UpstreamProtocol.MqttWs) };
yield return new object[] { "mqttws", Option.Some(UpstreamProtocol.MqttWs) };
yield return new object[] { "MqTtWs", Option.Some(UpstreamProtocol.MqttWs) };

yield return new object[] { "AmqpWs", Option.Some(UpstreamProtocol.AmqpWs) };
yield return new object[] { "AMQPWs", Option.Some(UpstreamProtocol.AmqpWs) };
yield return new object[] { "amqpws", Option.Some(UpstreamProtocol.AmqpWs) };
yield return new object[] { "amqPwS", Option.Some(UpstreamProtocol.AmqpWs) };

yield return new object[] { "amqPwSt", Option.None<UpstreamProtocol>() };
yield return new object[] { "", Option.None<UpstreamProtocol>() };
yield return new object[] { " ", Option.None<UpstreamProtocol>() };
yield return new object[] { "mqttwebsockets", Option.None<UpstreamProtocol>() };
}

[Theory]
[MemberData(nameof(GetUpstreamProtocolData))]
public void ParseUpstreamProtocolTest(string input, Option<UpstreamProtocol> expectedValue)
{
// Arrange
IConfigurationRoot configRoot = new ConfigurationBuilder()
.AddInMemoryCollection(
new Dictionary<string, string>
{
{ "UpstreamProtocol", input }
})
.Build();

// Act
Option<UpstreamProtocol> upstreamProtocol = Startup.GetUpstreamProtocol(configRoot);

// Assert
Assert.Equal(expectedValue.HasValue, upstreamProtocol.HasValue);
Assert.Equal(expectedValue.OrDefault(), upstreamProtocol.OrDefault());
}
}
}

0 comments on commit f48c780

Please sign in to comment.