-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feature/#1281_-_Syncronize edge device between IoT hub and portal dat…
…abase (#1314) * resolve #1281 * fix unit test * add EdgeDeviceRepository test * add job unit test * add new test * fix test * fix test
- Loading branch information
Showing
17 changed files
with
1,157 additions
and
13 deletions.
There are no files selected for viewing
440 changes: 440 additions & 0 deletions
440
src/AzureIoTHub.Portal.Infrastructure/Migrations/20221004121605_Add EdgeDevice.Designer.cs
Large diffs are not rendered by default.
Oops, something went wrong.
55 changes: 55 additions & 0 deletions
55
src/AzureIoTHub.Portal.Infrastructure/Migrations/20221004121605_Add EdgeDevice.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
// Copyright (c) CGI France. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
#nullable disable | ||
|
||
namespace AzureIoTHub.Portal.Infrastructure.Migrations | ||
{ | ||
using Microsoft.EntityFrameworkCore.Migrations; | ||
|
||
public partial class AddEdgeDevice : Migration | ||
{ | ||
protected override void Up(MigrationBuilder migrationBuilder) | ||
{ | ||
_ = migrationBuilder.AddColumn<string>( | ||
name: "EdgeDeviceId", | ||
table: "DeviceTagValues", | ||
type: "text", | ||
nullable: true); | ||
|
||
_ = migrationBuilder.CreateTable( | ||
name: "EdgeDevices", | ||
columns: table => new | ||
{ | ||
Id = table.Column<string>(type: "text", nullable: false), | ||
Name = table.Column<string>(type: "text", nullable: false), | ||
DeviceModelId = table.Column<string>(type: "text", nullable: false), | ||
Version = table.Column<int>(type: "integer", nullable: false), | ||
IsEnabled = table.Column<bool>(type: "boolean", nullable: false), | ||
Scope = table.Column<string>(type: "text", nullable: false), | ||
NbDevices = table.Column<int>(type: "integer", nullable: false), | ||
NbModules = table.Column<int>(type: "integer", nullable: false) | ||
}, | ||
constraints: table => | ||
{ | ||
_ = table.PrimaryKey("PK_EdgeDevices", x => x.Id); | ||
}); | ||
|
||
_ = migrationBuilder.CreateIndex(name: "IX_DeviceTagValues_EdgeDeviceId", table: "DeviceTagValues", column: "EdgeDeviceId"); | ||
|
||
_ = migrationBuilder.AddForeignKey(name: "FK_DeviceTagValues_EdgeDevices_EdgeDeviceId", table: "DeviceTagValues", column: "EdgeDeviceId", principalTable: "EdgeDevices", principalColumn: "Id"); | ||
} | ||
|
||
protected override void Down(MigrationBuilder migrationBuilder) | ||
{ | ||
_ = migrationBuilder.DropForeignKey(name: "FK_DeviceTagValues_EdgeDevices_EdgeDeviceId", table: "DeviceTagValues"); | ||
|
||
_ = migrationBuilder.DropTable( | ||
name: "EdgeDevices"); | ||
|
||
_ = migrationBuilder.DropIndex(name: "IX_DeviceTagValues_EdgeDeviceId", table: "DeviceTagValues"); | ||
|
||
_ = migrationBuilder.DropColumn(name: "EdgeDeviceId", table: "DeviceTagValues"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
src/AzureIoTHub.Portal.Infrastructure/Repositories/EdgeDeviceRepository.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// 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.Infrastructure.Repositories | ||
{ | ||
using System.Threading.Tasks; | ||
using AzureIoTHub.Portal.Domain.Entities; | ||
using AzureIoTHub.Portal.Domain.Repositories; | ||
using Microsoft.EntityFrameworkCore; | ||
|
||
public class EdgeDeviceRepository : GenericRepository<EdgeDevice>, IEdgeDeviceRepository | ||
{ | ||
public EdgeDeviceRepository(PortalDbContext context) : base(context) | ||
{ | ||
} | ||
|
||
public override Task<EdgeDevice?> GetByIdAsync(object id) | ||
{ | ||
return this.context.Set<EdgeDevice>() | ||
.Include(device => device.Tags) | ||
.Where(device => device.Id.Equals(id.ToString())) | ||
.FirstOrDefaultAsync(); | ||
} | ||
} | ||
} |
60 changes: 60 additions & 0 deletions
60
src/AzureIoTHub.Portal.Tests.Unit/Infrastructure/Repositories/EdgeDeviceRepositoryTest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
// 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.Tests.Unit.Infrastructure.Repositories | ||
{ | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using AutoFixture; | ||
using AzureIoTHub.Portal.Domain.Entities; | ||
using AzureIoTHub.Portal.Infrastructure.Repositories; | ||
using AzureIoTHub.Portal.Tests.Unit.UnitTests.Bases; | ||
using FluentAssertions; | ||
using NUnit.Framework; | ||
|
||
public class EdgeDeviceRepositoryTest : BackendUnitTest | ||
{ | ||
private EdgeDeviceRepository edgeDeviceRepository; | ||
|
||
public override void Setup() | ||
{ | ||
base.Setup(); | ||
|
||
this.edgeDeviceRepository = new EdgeDeviceRepository(DbContext); | ||
} | ||
|
||
[Test] | ||
public async Task GetAllShouldReturnExpectedEdgeDevices() | ||
{ | ||
// Arrange | ||
var expectedEdgeDevices = Fixture.CreateMany<EdgeDevice>(5).ToList(); | ||
|
||
await DbContext.AddRangeAsync(expectedEdgeDevices); | ||
|
||
_ = await DbContext.SaveChangesAsync(); | ||
|
||
//Act | ||
var result = this.edgeDeviceRepository.GetAll().ToList(); | ||
|
||
// Assert | ||
_ = result.Should().BeEquivalentTo(expectedEdgeDevices); | ||
} | ||
|
||
[Test] | ||
public async Task GetByIdAsyncExistingDeviceReturnsExpectedEdgeDevice() | ||
{ | ||
// Arrange | ||
var expectedEdgeDevices = Fixture.Create<EdgeDevice>(); | ||
|
||
_ = DbContext.Add(expectedEdgeDevices); | ||
|
||
_ = await DbContext.SaveChangesAsync(); | ||
|
||
//Act | ||
var result = await this.edgeDeviceRepository.GetByIdAsync(expectedEdgeDevices.Id); | ||
|
||
// Assert | ||
_ = result.Should().BeEquivalentTo(expectedEdgeDevices); | ||
} | ||
} | ||
} |
Oops, something went wrong.