diff --git a/src/AzureIoTHub.Portal.Server.Tests/Controllers/v1.0/ConcentratorsControllerTests.cs b/src/AzureIoTHub.Portal.Server.Tests/Controllers/v1.0/ConcentratorsControllerTests.cs deleted file mode 100644 index 30f51dd12..000000000 --- a/src/AzureIoTHub.Portal.Server.Tests/Controllers/v1.0/ConcentratorsControllerTests.cs +++ /dev/null @@ -1,237 +0,0 @@ -using AzureIoTHub.Portal.Server.Controllers.V10.LoRaWAN; -using AzureIoTHub.Portal.Server.Managers; -using AzureIoTHub.Portal.Server.Mappers; -using AzureIoTHub.Portal.Server.Services; -using AzureIoTHub.Portal.Shared.Models.V10.LoRaWAN.Concentrator; -using Microsoft.AspNetCore.Mvc; -using Microsoft.Azure.Devices; -using Microsoft.Azure.Devices.Shared; -using Microsoft.Extensions.Logging; -using Moq; -using NUnit.Framework; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; - -namespace AzureIoTHub.Portal.Server.Tests.Controllers.V10 -{ - [TestFixture] - public class ConcentratorsControllerTests - { - private MockRepository mockRepository; - - private Mock mockRouterManager; - private Mock mockConcentratorTwinMapper; - private Mock mockDeviceService; - private Mock> mockLogger; - - [SetUp] - public void SetUp() - { - this.mockRepository = new MockRepository(MockBehavior.Strict); - - this.mockLogger = this.mockRepository.Create>(); - this.mockRouterManager = this.mockRepository.Create(); - this.mockConcentratorTwinMapper = this.mockRepository.Create(); - this.mockDeviceService = this.mockRepository.Create(); - } - - private LoRaWANConcentratorsController CreateController() - { - return new LoRaWANConcentratorsController( - this.mockLogger.Object, - this.mockDeviceService.Object, - this.mockRouterManager.Object, - this.mockConcentratorTwinMapper.Object); - } - - [Test] - public async Task GetAllDeviceConcentrator_WithNoArgument_ReturnConcentratorList() - { - // Arrange - var twin = new Twin("aaa"); - twin.Tags["deviceType"] = "LoRa Concentrator"; - var concentrator = new Concentrator - { - DeviceId = twin.DeviceId, - DeviceType = "LoRa Concentrator" - }; - - this.mockDeviceService.Setup(x => x.GetAllDevice( - It.Is(x => x == "LoRa Concentrator"), - It.Is(x => string.IsNullOrEmpty(x)))) - .ReturnsAsync(new[] - { - twin - }); - - this.mockConcentratorTwinMapper.Setup(x => x.CreateDeviceDetails(It.Is(c => c.DeviceId == twin.DeviceId))) - .Returns(concentrator); - - var concentratorController = this.CreateController(); - - // Act - var response = await concentratorController.GetAllDeviceConcentrator(); - - // Assert - Assert.IsNotNull(response); - Assert.IsAssignableFrom(response.Result); - var okObjectResult = response.Result as ObjectResult; - - Assert.IsNotNull(okObjectResult); - Assert.AreEqual(200, okObjectResult.StatusCode); - Assert.IsNotNull(okObjectResult.Value); - var deviceList = okObjectResult.Value as IEnumerable; - Assert.IsNotNull(deviceList); - Assert.AreEqual(1, deviceList.Count()); - var device = deviceList.First(); - Assert.IsNotNull(device); - Assert.AreEqual(twin.DeviceId, device.DeviceId); - Assert.AreEqual("LoRa Concentrator", device.DeviceType); - - this.mockRepository.VerifyAll(); - } - - [Test] - public async Task GetDeviceConcentrator_With_Valid_Argument_Should_Return_Concentrator() - { - // Arrange - var twin = new Twin("aaa"); - twin.Tags["deviceType"] = "LoRa Concentrator"; - var concentrator = new Concentrator - { - DeviceId = twin.DeviceId, - DeviceType = "LoRa Concentrator" - }; - - this.mockDeviceService.Setup(x => x.GetDeviceTwin(It.Is(c => c == twin.DeviceId))) - .ReturnsAsync(twin); - - this.mockConcentratorTwinMapper.Setup(x => x.CreateDeviceDetails(It.Is(c => c.DeviceId == twin.DeviceId))) - .Returns(concentrator); - - var concentratorController = this.CreateController(); - - // Act - var response = await concentratorController.GetDeviceConcentrator(twin.DeviceId); - - // Assert - Assert.IsNotNull(response); - Assert.IsAssignableFrom(response.Result); - var okObjectResult = response.Result as OkObjectResult; - - Assert.IsNotNull(okObjectResult); - Assert.AreEqual(200, okObjectResult.StatusCode); - Assert.IsNotNull(okObjectResult.Value); - Assert.IsAssignableFrom(okObjectResult.Value); - var device = okObjectResult.Value as Concentrator; - Assert.IsNotNull(device); - Assert.AreEqual(twin.DeviceId, device.DeviceId); - Assert.AreEqual("LoRa Concentrator", device.DeviceType); - - this.mockRepository.VerifyAll(); - } - - [Test] - public async Task CreateDeviceAsync_With_Valid_Argument_Should_Return_OkResult() - { - // Arrange - var concentratorController = this.CreateController(); - var concentrator = new Concentrator - { - DeviceId = "4512457896451156", - LoraRegion = Guid.NewGuid().ToString(), - IsEnabled = true - }; - - var routerConfig = new RouterConfig(); - var mockResult = new BulkRegistryOperationResult - { - IsSuccessful = true - }; - - var twin = new Twin - { - DeviceId = concentrator.DeviceId, - }; - - this.mockDeviceService.Setup(c => c.CreateDeviceWithTwin( - It.Is(x => x == twin.DeviceId), - It.Is(x => !x), - It.Is(x => x.DeviceId == twin.DeviceId), - It.Is(x => x == DeviceStatus.Enabled))) - .ReturnsAsync(mockResult); - - this.mockRouterManager.Setup(x => x.GetRouterConfig(It.Is(c => c == concentrator.LoraRegion))) - .ReturnsAsync(routerConfig); - - this.mockConcentratorTwinMapper.Setup(x => x.UpdateTwin(It.Is(c => c.DeviceId == twin.DeviceId), It.Is(c => c.DeviceId == concentrator.DeviceId))); - this.mockLogger.Setup(x => x.Log(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny>())); - - // Act - var result = await concentratorController.CreateDeviceAsync(concentrator); - - // Assert - Assert.IsNotNull(result); - Assert.IsAssignableFrom(result); - var okObjectResult = result as ObjectResult; - Assert.IsNotNull(okObjectResult); - Assert.AreEqual(200, okObjectResult.StatusCode); - Assert.IsNotNull(okObjectResult.Value); - Assert.IsAssignableFrom(okObjectResult.Value); - Assert.AreEqual(mockResult, okObjectResult.Value); - } - - [Test] - public async Task UpdateDeviceAsync_With_Valid_Argument_Should_Return_OkResult() - { - // Arrange - var concentratorController = this.CreateController(); - var concentrator = new Concentrator - { - DeviceId = "4512457896451156", - LoraRegion = Guid.NewGuid().ToString(), - IsEnabled = true, - RouterConfig = new RouterConfig() - }; - - var twin = new Twin - { - DeviceId = concentrator.DeviceId, - }; - - var device = new Device(concentrator.DeviceId); - - this.mockRouterManager.Setup(x => x.GetRouterConfig(It.Is(c => c == concentrator.LoraRegion))) - .ReturnsAsync(concentrator.RouterConfig); - this.mockConcentratorTwinMapper.Setup(x => x.UpdateTwin(It.Is(c => c.DeviceId == twin.DeviceId), It.Is(c => c.DeviceId == concentrator.DeviceId))); - - this.mockDeviceService.Setup(x => x.GetDevice(It.Is(c => c == concentrator.DeviceId))) - .ReturnsAsync(device); - this.mockDeviceService.Setup(x => x.UpdateDevice(It.Is(c => c.Id == concentrator.DeviceId))) - .ReturnsAsync(device); - this.mockDeviceService.Setup(x => x.GetDeviceTwin(It.Is(c => c == concentrator.DeviceId))) - .ReturnsAsync(twin); - this.mockDeviceService.Setup(x => x.UpdateDeviceTwin(It.Is(c => c == concentrator.DeviceId), It.Is(c => c.DeviceId == concentrator.DeviceId))) - .ReturnsAsync(twin); - - // Act - var result = await concentratorController.UpdateDeviceAsync(concentrator); - - // Assert - Assert.IsNotNull(result); - Assert.IsAssignableFrom(result); - var okObjectResult = result as ObjectResult; - Assert.IsNotNull(okObjectResult); - Assert.AreEqual(200, okObjectResult.StatusCode); - } - - [Test] - public async Task DeleteDeviceAsync() - { - await Task.CompletedTask; - Assert.Inconclusive(); - } - } -} diff --git a/src/AzureIoTHub.Portal.Server.Tests/Controllers/v1.0/LoRaWAN/LoRaWANConcentratorsControllerTest.cs b/src/AzureIoTHub.Portal.Server.Tests/Controllers/v1.0/LoRaWAN/LoRaWANConcentratorsControllerTest.cs index 368606a1e..c9682e0fc 100644 --- a/src/AzureIoTHub.Portal.Server.Tests/Controllers/v1.0/LoRaWAN/LoRaWANConcentratorsControllerTest.cs +++ b/src/AzureIoTHub.Portal.Server.Tests/Controllers/v1.0/LoRaWAN/LoRaWANConcentratorsControllerTest.cs @@ -9,6 +9,7 @@ using AzureIoTHub.Portal.Shared.Models.V10.LoRaWAN.Concentrator; using AzureIoTHub.Portal.Shared.Models.V10.LoRaWAN.LoRaDevice; using AzureIoTHub.Portal.Shared.Models.V10.LoRaWAN.LoRaDeviceModel; +using FluentAssertions; using Microsoft.AspNetCore.Mvc; using Microsoft.Azure.Devices; using Microsoft.Azure.Devices.Shared; @@ -22,6 +23,8 @@ using System.Net.Http; using System.Threading; using System.Threading.Tasks; +using static AzureIoTHub.Portal.Server.Startup; + namespace AzureIoTHub.Portal.Server.Tests.Controllers.v10.LoRaWAN { [TestFixture] @@ -33,6 +36,7 @@ public class LoRaWANConcentratorsControllerTest private Mock mockDeviceService; private Mock mockRouterConfigManager; private Mock mockConcentratorTwinMapper; + private Mock mockConfigHandler; [SetUp] public void SetUp() @@ -42,6 +46,7 @@ public void SetUp() this.mockDeviceService = this.mockRepository.Create(); this.mockRouterConfigManager = this.mockRepository.Create(); this.mockConcentratorTwinMapper = this.mockRepository.Create(); + this.mockConfigHandler = this.mockRepository.Create(); } private LoRaWANConcentratorsController CreateLoRaWANConcentratorsController() @@ -101,8 +106,6 @@ public async Task GetAllDeviceConcentrator_With_no_deviceType_Should_return_empt { // Arrange var concentratorsController = CreateLoRaWANConcentratorsController(); - int count = 100; - TwinCollection twinCollection = new TwinCollection(); this.mockDeviceService.Setup(c => c.GetAllDevice( It.Is(x => x == "LoRa Concentrator"), @@ -122,13 +125,152 @@ public async Task GetAllDeviceConcentrator_With_no_deviceType_Should_return_empt Assert.IsNotNull(okObjectResult); Assert.AreEqual(200, okObjectResult.StatusCode); - // Assert.IsNull(okObjectResult.Value); var deviceList = okObjectResult.Value as IEnumerable; Assert.IsNull(deviceList); - // Assert.IsFalse(deviceList.Any()); this.mockRepository.VerifyAll(); } + + [Test] + public async Task GetDeviceConcentrator_With_Valid_Argument_Should_Return_Concentrator() + { + // Arrange + var twin = new Twin("aaa"); + twin.Tags["deviceType"] = "LoRa Concentrator"; + var concentrator = new Concentrator + { + DeviceId = twin.DeviceId, + DeviceType = "LoRa Concentrator" + }; + + this.mockDeviceService.Setup(x => x.GetDeviceTwin(It.Is(c => c == twin.DeviceId))) + .ReturnsAsync(twin); + + this.mockConcentratorTwinMapper.Setup(x => x.CreateDeviceDetails(It.Is(c => c.DeviceId == twin.DeviceId))) + .Returns(concentrator); + + var concentratorController = this.CreateLoRaWANConcentratorsController(); + + // Act + var response = await concentratorController.GetDeviceConcentrator(twin.DeviceId); + + // Assert + Assert.IsNotNull(response); + Assert.IsAssignableFrom(response.Result); + var okObjectResult = response.Result as OkObjectResult; + + Assert.IsNotNull(okObjectResult); + Assert.AreEqual(200, okObjectResult.StatusCode); + Assert.IsNotNull(okObjectResult.Value); + Assert.IsAssignableFrom(okObjectResult.Value); + var device = okObjectResult.Value as Concentrator; + Assert.IsNotNull(device); + Assert.AreEqual(twin.DeviceId, device.DeviceId); + Assert.AreEqual("LoRa Concentrator", device.DeviceType); + + this.mockRepository.VerifyAll(); + } + + [Test] + public async Task CreateDeviceAsync_With_Valid_Argument_Should_Return_OkResult() + { + // Arrange + var concentratorController = this.CreateLoRaWANConcentratorsController(); + var concentrator = new Concentrator + { + DeviceId = "4512457896451156", + LoraRegion = Guid.NewGuid().ToString(), + IsEnabled = true + }; + + var routerConfig = new RouterConfig(); + var mockResult = new BulkRegistryOperationResult + { + IsSuccessful = true + }; + + var twin = new Twin + { + DeviceId = concentrator.DeviceId, + }; + + this.mockDeviceService.Setup(c => c.CreateDeviceWithTwin( + It.Is(x => x == twin.DeviceId), + It.Is(x => !x), + It.Is(x => x.DeviceId == twin.DeviceId), + It.Is(x => x == DeviceStatus.Enabled))) + .ReturnsAsync(mockResult); + + this.mockRouterConfigManager.Setup(x => x.GetRouterConfig(It.Is(c => c == concentrator.LoraRegion))) + .ReturnsAsync(routerConfig); + + this.mockConcentratorTwinMapper.Setup(x => x.UpdateTwin(It.Is(c => c.DeviceId == twin.DeviceId), It.Is(c => c.DeviceId == concentrator.DeviceId))); + this.mockLogger.Setup(x => x.Log(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny>())); + + // Act + var result = await concentratorController.CreateDeviceAsync(concentrator); + + // Assert + Assert.IsNotNull(result); + Assert.IsAssignableFrom(result); + var okObjectResult = result as ObjectResult; + Assert.IsNotNull(okObjectResult); + Assert.AreEqual(200, okObjectResult.StatusCode); + Assert.IsNotNull(okObjectResult.Value); + Assert.IsAssignableFrom(okObjectResult.Value); + Assert.AreEqual(mockResult, okObjectResult.Value); + } + + [Test] + public async Task UpdateDeviceAsync_With_Valid_Argument_Should_Return_OkResult() + { + // Arrange + var concentratorController = this.CreateLoRaWANConcentratorsController(); + var concentrator = new Concentrator + { + DeviceId = "4512457896451156", + LoraRegion = Guid.NewGuid().ToString(), + IsEnabled = true, + RouterConfig = new RouterConfig() + }; + + var twin = new Twin + { + DeviceId = concentrator.DeviceId, + }; + + var device = new Device(concentrator.DeviceId); + + this.mockRouterConfigManager.Setup(x => x.GetRouterConfig(It.Is(c => c == concentrator.LoraRegion))) + .ReturnsAsync(concentrator.RouterConfig); + this.mockConcentratorTwinMapper.Setup(x => x.UpdateTwin(It.Is(c => c.DeviceId == twin.DeviceId), It.Is(c => c.DeviceId == concentrator.DeviceId))); + + this.mockDeviceService.Setup(x => x.GetDevice(It.Is(c => c == concentrator.DeviceId))) + .ReturnsAsync(device); + this.mockDeviceService.Setup(x => x.UpdateDevice(It.Is(c => c.Id == concentrator.DeviceId))) + .ReturnsAsync(device); + this.mockDeviceService.Setup(x => x.GetDeviceTwin(It.Is(c => c == concentrator.DeviceId))) + .ReturnsAsync(twin); + this.mockDeviceService.Setup(x => x.UpdateDeviceTwin(It.Is(c => c == concentrator.DeviceId), It.Is(c => c.DeviceId == concentrator.DeviceId))) + .ReturnsAsync(twin); + + // Act + var result = await concentratorController.UpdateDeviceAsync(concentrator); + + // Assert + Assert.IsNotNull(result); + Assert.IsAssignableFrom(result); + var okObjectResult = result as ObjectResult; + Assert.IsNotNull(okObjectResult); + Assert.AreEqual(200, okObjectResult.StatusCode); + } + + [Test] + public async Task DeleteDeviceAsync() + { + await Task.CompletedTask; + Assert.Inconclusive(); + } } } diff --git a/src/AzureIoTHub.Portal.Server.Tests/Controllers/v1.0/SettingsControllerTest.cs b/src/AzureIoTHub.Portal.Server.Tests/Controllers/v1.0/SettingsControllerTest.cs index 2642d4caf..ab331de8b 100644 --- a/src/AzureIoTHub.Portal.Server.Tests/Controllers/v1.0/SettingsControllerTest.cs +++ b/src/AzureIoTHub.Portal.Server.Tests/Controllers/v1.0/SettingsControllerTest.cs @@ -3,13 +3,8 @@ using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Options; using Moq; -using Moq.Protected; using NUnit.Framework; using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; using static AzureIoTHub.Portal.Server.Startup; namespace AzureIoTHub.Portal.Server.Tests.Controllers.V10 @@ -73,12 +68,12 @@ public void GetOIDCSettings_Should_return_value() public void GetLoRaActivationSetting_Should_return_true_to_string() { // Arrange - string loraFeatureStatus = "true"; + bool loraFeatureStatus = true; this.mockConfiguration.SetupGet(c => c.Value).Returns(value: null); this.mockConfigHandler - .SetupGet(c => c.LoRaFeatureSetting) + .SetupGet(c => c.IsLoRaEnabled) .Returns(loraFeatureStatus); var controller = this.CreateController(); diff --git a/src/AzureIoTHub.Portal.Server.Tests/Filters/LoRaFeatureActiveFilterAttributeTest.cs b/src/AzureIoTHub.Portal.Server.Tests/Filters/LoRaFeatureActiveFilterAttributeTest.cs new file mode 100644 index 000000000..bc9a8bc4b --- /dev/null +++ b/src/AzureIoTHub.Portal.Server.Tests/Filters/LoRaFeatureActiveFilterAttributeTest.cs @@ -0,0 +1,83 @@ +using AzureIoTHub.Portal.Server.Filters; +using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Mvc; +using Microsoft.AspNetCore.Mvc.Abstractions; +using Microsoft.AspNetCore.Mvc.Filters; +using Microsoft.AspNetCore.Mvc.ModelBinding; +using Microsoft.AspNetCore.Routing; +using Microsoft.Extensions.DependencyInjection; +using Moq; +using NUnit.Framework; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using static AzureIoTHub.Portal.Server.Startup; + +namespace AzureIoTHub.Portal.Server.Tests.Filters +{ + [TestFixture] + public class LoRaFeatureActiveFilterAttributeTest + { + private MockRepository mockRepository; + // private Mock mockActionExecutingContext; + private Mock mockConfigHandler; + + [SetUp] + public void SetUp() + { + this.mockRepository = new MockRepository(MockBehavior.Strict); + + // this.mockActionExecutingContext = this.mockRepository.Create(); + this.mockConfigHandler = this.mockRepository.Create(); + } + + private LoRaFeatureActiveFilterAttribute CreateAttributeFilter() + { + return new LoRaFeatureActiveFilterAttribute(); + } + + [Test] + public void OnActionExecuting_With_LoRa_feature_disable_Should_return_400() + { + // Arrange + this.mockConfigHandler + .SetupGet(c => c.IsLoRaEnabled) + .Returns(false); + + var serviceProviderMock = new Mock(); + serviceProviderMock.Setup(provider => provider.GetService(typeof(ConfigHandler))) + .Returns(this.mockConfigHandler.Object); + + var httpContextMock = new Mock(); + httpContextMock.SetupGet(context => context.RequestServices) + .Returns(serviceProviderMock.Object); + + var actionContext = new ActionContext( + httpContextMock.Object, + Mock.Of(), + Mock.Of()); + + var actionExecutingContext = new ActionExecutingContext( + actionContext, + new List(), + new Dictionary(), + Mock.Of()); + + //var mockActionExecutingContext = new Mock(); + //mockActionExecutingContext + // .Setup(x => x.HttpContext.RequestServices.GetService(typeof(ConfigHandler))) + // .Returns(this.mockConfigHandler.Object); + + var actionFilter = this.CreateAttributeFilter(); + + // Act + actionFilter.OnActionExecuting(actionExecutingContext); + + // Assert + var contentResult = actionExecutingContext.Result as ContentResult; + Assert.AreEqual((int)System.Net.HttpStatusCode.BadRequest, contentResult.StatusCode); + } + } +}