Skip to content

Commit

Permalink
Fix #1635 - discard device sync if its modelid is not provided
Browse files Browse the repository at this point in the history
  • Loading branch information
kbeaugrand committed Dec 5, 2022
1 parent b210968 commit 7df6d3b
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/AzureIoTHub.Portal.Server/Jobs/SyncDevicesJob.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,12 @@ private async Task SyncDevices()

foreach (var twin in deviceTwins)
{
if (!twin.Tags.Contains(ModelId))
{
this.logger.LogInformation($"Cannot import device '{twin.DeviceId}' since it doesn't have a model identifier.");
continue;
}

var deviceModel = await this.deviceModelRepository.GetByIdAsync(twin.Tags[ModelId]?.ToString() ?? string.Empty);

if (deviceModel == null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -426,5 +426,45 @@ public async Task Execute_ExistingLorawanDeviceWithGreeterVersion_LorawanDeviceU
// Assert
MockRepository.VerifyAll();
}

[Test]
public async Task Execute_MissingModelId_ShouldNotFail()
{
// Arrange
var mockJobExecutionContext = MockRepository.Create<IJobExecutionContext>();

var expectedDeviceModel = Fixture.Create<DeviceModel>();
expectedDeviceModel.SupportLoRaFeatures = true;

var expectedTwinDevice = new Twin
{
DeviceId = Fixture.Create<string>(),
};

_ = this.mockDeviceService
.Setup(x => x.GetAllDevice(
It.IsAny<string>(),
It.IsAny<string>(),
It.IsAny<string>(),
It.IsAny<string>(),
It.IsAny<bool?>(),
It.IsAny<bool?>(),
It.IsAny<Dictionary<string, string>>(),
It.Is<int>(x => x == 100)))
.ReturnsAsync(new PaginationResult<Twin>
{
Items = new List<Twin>
{
expectedTwinDevice
},
TotalItems = 1
});

// Act
await this.syncDevicesJob.Execute(mockJobExecutionContext.Object);

// Assert
MockRepository.VerifyAll();
}
}
}

0 comments on commit 7df6d3b

Please sign in to comment.