Skip to content

Commit

Permalink
Feature/#1281_-_Syncronize edge device between IoT hub and portal dat…
Browse files Browse the repository at this point in the history
…abase (#1314)

* resolve #1281

* fix unit test

* add EdgeDeviceRepository test

* add job unit test

* add new test

* fix test

* fix test
  • Loading branch information
Sben65 authored Oct 7, 2022
1 parent fecd9f5 commit 0d499bc
Show file tree
Hide file tree
Showing 17 changed files with 1,157 additions and 13 deletions.

Large diffs are not rendered by default.

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");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,9 @@ protected override void BuildModel(ModelBuilder modelBuilder)
b.Property<string>("DeviceId")
.HasColumnType("text");

b.Property<string>("EdgeDeviceId")
.HasColumnType("text");

b.Property<string>("LorawanDeviceId")
.HasColumnType("text");

Expand All @@ -241,11 +244,47 @@ protected override void BuildModel(ModelBuilder modelBuilder)

b.HasIndex("DeviceId");

b.HasIndex("EdgeDeviceId");

b.HasIndex("LorawanDeviceId");

b.ToTable("DeviceTagValues");
});

modelBuilder.Entity("AzureIoTHub.Portal.Domain.Entities.EdgeDevice", b =>
{
b.Property<string>("Id")
.HasColumnType("text");

b.Property<string>("DeviceModelId")
.IsRequired()
.HasColumnType("text");

b.Property<bool>("IsEnabled")
.HasColumnType("boolean");

b.Property<string>("Name")
.IsRequired()
.HasColumnType("text");

b.Property<int>("NbDevices")
.HasColumnType("integer");

b.Property<int>("NbModules")
.HasColumnType("integer");

b.Property<string>("Scope")
.IsRequired()
.HasColumnType("text");

b.Property<int>("Version")
.HasColumnType("integer");

b.HasKey("Id");

b.ToTable("EdgeDevices");
});

modelBuilder.Entity("AzureIoTHub.Portal.Domain.Entities.EdgeDeviceModel", b =>
{
b.Property<string>("Id")
Expand Down Expand Up @@ -405,6 +444,10 @@ protected override void BuildModel(ModelBuilder modelBuilder)
.WithMany("Tags")
.HasForeignKey("DeviceId");

b.HasOne("AzureIoTHub.Portal.Domain.Entities.EdgeDevice", null)
.WithMany("Tags")
.HasForeignKey("EdgeDeviceId");

b.HasOne("AzureIoTHub.Portal.Domain.Entities.LorawanDevice", null)
.WithMany("Tags")
.HasForeignKey("LorawanDeviceId");
Expand All @@ -415,6 +458,11 @@ protected override void BuildModel(ModelBuilder modelBuilder)
b.Navigation("Tags");
});

modelBuilder.Entity("AzureIoTHub.Portal.Domain.Entities.EdgeDevice", b =>
{
b.Navigation("Tags");
});

modelBuilder.Entity("AzureIoTHub.Portal.Domain.Entities.LorawanDevice", b =>
{
b.Navigation("Tags");
Expand Down
1 change: 1 addition & 0 deletions src/AzureIoTHub.Portal.Infrastructure/PortalDbContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public class PortalDbContext : DbContext
public DbSet<DeviceModelCommand> DeviceModelCommands { get; set; }
public DbSet<DeviceModel> DeviceModels { get; set; }
public DbSet<Device> Devices { get; set; }
public DbSet<EdgeDevice> EdgeDevices { get; set; }
public DbSet<LorawanDevice> LorawanDevices { get; set; }
public DbSet<DeviceTagValue> DeviceTagValues { get; set; }
public DbSet<EdgeDeviceModel> EdgeDeviceModels { get; set; }
Expand Down
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();
}
}
}
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);
}
}
}
Loading

0 comments on commit 0d499bc

Please sign in to comment.