Skip to content

Commit

Permalink
Add Ideas feature #787
Browse files Browse the repository at this point in the history
  • Loading branch information
hocinehacherouf committed Jul 9, 2022
1 parent 3381c36 commit 459d1f0
Show file tree
Hide file tree
Showing 24 changed files with 707 additions and 17 deletions.
58 changes: 58 additions & 0 deletions src/AzureIoTHub.Portal.Server.Tests.Unit/BackendUnitTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// Copyright (c) CGI France. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

namespace AzureIoTHub.Portal.Server.Tests.Unit
{
using System;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using Moq;
using NUnit.Framework;
using RichardSzalay.MockHttp;

public abstract class BackendUnitTest : IDisposable
{
protected virtual ServiceCollection ServiceCollection { get; set; }

protected virtual ServiceProvider Services { get; set; }

protected virtual MockRepository MockRepository { get; set; }

protected virtual MockHttpMessageHandler MockHttpClient { get; set; }

protected virtual AutoFixture.Fixture Fixture { get; } = new();

[SetUp]
public virtual void Setup()
{
ServiceCollection = new ServiceCollection();
_ = ServiceCollection.AddSingleton(typeof(ILogger<>), typeof(NullLogger<>));

// Configure Mockups
MockRepository = new MockRepository(MockBehavior.Strict);

// Add Mock Http Client
MockHttpClient = new MockHttpMessageHandler();
var httpClient = MockHttpClient.ToHttpClient();
httpClient.BaseAddress = new Uri("http://fake.local");
_ = ServiceCollection.AddSingleton(httpClient);
}

[TearDown]
public void TearDown()
{
Services?.Dispose();
}

public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}

protected virtual void Dispose(bool disposing)
{
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// Copyright (c) CGI France. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

namespace AzureIoTHub.Portal.Server.Tests.Unit.Controllers.v1._0
{
using System.Threading.Tasks;
using AutoFixture;
using AzureIoTHub.Portal.Server.Controllers.v1._0;
using AzureIoTHub.Portal.Shared.Models.v1._0;
using FluentAssertions;
using Microsoft.Extensions.DependencyInjection;
using Moq;
using NUnit.Framework;
using Server.Services;

[TestFixture]
public class IdeasControllerTests : BackendUnitTest
{
private Mock<IIdeaService> mockIdeaService;

private IdeasController ideasController;

public override void Setup()
{
base.Setup();

this.mockIdeaService = MockRepository.Create<IIdeaService>();

_ = ServiceCollection.AddSingleton(this.mockIdeaService.Object);

Services = ServiceCollection.BuildServiceProvider();

this.ideasController = new IdeasController(Services.GetRequiredService<IIdeaService>());
}

[Test]
public async Task SubmitIdeaShouldSubmitIdea()
{
// Arrange
var ideaRequest = Fixture.Create<IdeaRequest>();
var expectedIdeaResponse = Fixture.Create<IdeaResponse>();

_ = this.mockIdeaService.Setup(service => service.SubmitIdea(ideaRequest))
.ReturnsAsync(expectedIdeaResponse);

// Act
var result = await this.ideasController.SubmitIdea(ideaRequest);

// Assert
_ = result.Should().BeEquivalentTo(expectedIdeaResponse);
MockRepository.VerifyAll();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,10 @@ public void GetLoRaActivationSettingShouldReturnTrueToString()
.SetupGet(c => c.PortalName)
.Returns(string.Empty);

_ = this.mockConfigHandler
.SetupGet(c => c.IdeasEnabled)
.Returns(false);

var controller = CreateController();

// Act
Expand Down Expand Up @@ -123,6 +127,10 @@ public void CopyrightYearShouldBeEqualsToCurrentYear()
.SetupGet(c => c.PortalName)
.Returns(string.Empty);

_ = this.mockConfigHandler
.SetupGet(c => c.IdeasEnabled)
.Returns(false);

var controller = CreateController();

// Act
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,5 +122,45 @@ public void MetricLoaderRefreshIntervalInMinutesConfigMustHaveDefaultValue()
// Assert
_ = developmentConfigHandler.MetricLoaderRefreshIntervalInMinutes.Should().Be(10);
}

[Test]
public void IdeasEnabledMustHaveDefaultValue()
{
// Arrange
var developmentConfigHandler = new DevelopmentConfigHandler(new ConfigurationManager());

// Assert
_ = developmentConfigHandler.IdeasEnabled.Should().BeFalse();
}

[Test]
public void IdeasUrlMustHaveDefaultValue()
{
// Arrange
var developmentConfigHandler = new DevelopmentConfigHandler(new ConfigurationManager());

// Assert
_ = developmentConfigHandler.IdeasUrl.Should().BeEmpty();
}

[Test]
public void IdeasAuthenticationHeaderMustHaveDefaultValue()
{
// Arrange
var developmentConfigHandler = new DevelopmentConfigHandler(new ConfigurationManager());

// Assert
_ = developmentConfigHandler.IdeasAuthenticationHeader.Should().Be("Ocp-Apim-Subscription-Key");
}

[Test]
public void IdeasAuthenticationTokenMustHaveDefaultValue()
{
// Arrange
var developmentConfigHandler = new DevelopmentConfigHandler(new ConfigurationManager());

// Assert
_ = developmentConfigHandler.IdeasAuthenticationToken.Should().BeEmpty();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -146,5 +146,45 @@ public void MetricLoaderRefreshIntervalInMinutesConfigMustHaveDefaultValue()
// Assert
_ = productionConfigHandler.MetricLoaderRefreshIntervalInMinutes.Should().Be(10);
}

[Test]
public void IdeasEnabledMustHaveDefaultValue()
{
// Arrange
var productionConfigHandler = new ProductionConfigHandler(new ConfigurationManager());

// Assert
_ = productionConfigHandler.IdeasEnabled.Should().BeFalse();
}

[Test]
public void IdeasUrlMustHaveDefaultValue()
{
// Arrange
var productionConfigHandler = new ProductionConfigHandler(new ConfigurationManager());

// Assert
_ = productionConfigHandler.IdeasUrl.Should().BeEmpty();
}

[Test]
public void IdeasAuthenticationHeaderMustHaveDefaultValue()
{
// Arrange
var productionConfigHandler = new ProductionConfigHandler(new ConfigurationManager());

// Assert
_ = productionConfigHandler.IdeasAuthenticationHeader.Should().Be("Ocp-Apim-Subscription-Key");
}

[Test]
public void IdeasAuthenticationTokenMustHaveDefaultValue()
{
// Arrange
var productionConfigHandler = new ProductionConfigHandler(new ConfigurationManager());

// Assert
_ = productionConfigHandler.IdeasAuthenticationToken.Should().BeEmpty();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// Copyright (c) CGI France. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

namespace AzureIoTHub.Portal.Server.Tests.Unit.Services
{
using System.Net.Http;
using System.Threading.Tasks;
using AutoFixture;
using Client.Services;
using FluentAssertions;
using Helpers;
using Microsoft.Extensions.DependencyInjection;
using NUnit.Framework;
using RichardSzalay.MockHttp;
using Shared.Models.v1._0;

[TestFixture]
public class IdeaClientServiceTests : BlazorUnitTest
{
private IIdeaClientService ideaClientService;

public override void Setup()
{
base.Setup();

_ = Services.AddSingleton<IIdeaClientService, IdeaClientService>();

this.ideaClientService = Services.GetRequiredService<IIdeaClientService>();
}

[Test]
public async Task SubmitIdeaShouldSubmitIdea()
{
// Arrange
var ideaRequest = Fixture.Create<IdeaRequest>();
var expectedIdeaResponse = Fixture.Create<IdeaResponse>();

_ = MockHttpClient.When(HttpMethod.Post, "/api/ideas")
.With(m =>
{
_ = m.Content.Should().BeAssignableTo<ObjectContent<IdeaRequest>>();
var body = (ObjectContent<IdeaRequest>) m.Content;
_ = body?.Value.Should().BeEquivalentTo(ideaRequest);
return true;
})
.RespondJson(expectedIdeaResponse);

// Act
var result = await this.ideaClientService.SubmitIdea(ideaRequest);

// Assert
_ = result.Should().BeEquivalentTo(expectedIdeaResponse);
MockHttpClient.VerifyNoOutstandingExpectation();
MockHttpClient.VerifyNoOutstandingRequest();
}
}
}
Loading

0 comments on commit 459d1f0

Please sign in to comment.