Skip to content

Commit

Permalink
Fix # 277 - Failed to manipulate device model when creating with API … (
Browse files Browse the repository at this point in the history
#283)

* Fix # 277 - Failed to manipulate device model when creating with API and empty model identifier

* Add new unit test to match this issue
  • Loading branch information
kbeaugrand authored Feb 18, 2022
1 parent 4e620bf commit 21cd5b5
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,46 @@ public async Task Post_Should_Create_A_New_Entity()
this.mockRepository.VerifyAll();
}

[Test]
public async Task WhenEmptyModelId_Post_Should_Create_A_New_Entity()
{
// Arrange
var deviceModelsController = this.CreateDeviceModelsController();

var requestModel = new DeviceModel
{
ModelId = String.Empty
};

var mockResponse = this.mockRepository.Create<Response>();

this.mockDeviceTemplatesTableClient.Setup(c => c.UpsertEntityAsync(
It.Is<TableEntity>(x => x.RowKey != requestModel.ModelId && x.PartitionKey == LoRaWANDeviceModelsController.DefaultPartitionKey),
It.IsAny<TableUpdateMode>(),
It.IsAny<CancellationToken>()))
.ReturnsAsync(mockResponse.Object);

this.mockDeviceModelMapper.Setup(c => c.UpdateTableEntity(
It.Is<TableEntity>(x => x.RowKey != requestModel.ModelId && x.PartitionKey == LoRaWANDeviceModelsController.DefaultPartitionKey),
It.IsAny<DeviceModel>()));

this.mockTableClientFactory.Setup(c => c.GetDeviceTemplates())
.Returns(mockDeviceTemplatesTableClient.Object);

// Act
var result = await deviceModelsController.Post(requestModel);

// Assert
Assert.IsNotNull(result);
Assert.IsAssignableFrom<OkResult>(result);
var okObjectResult = result as OkResult;

Assert.IsNotNull(okObjectResult);
Assert.AreEqual(200, okObjectResult.StatusCode);

this.mockRepository.VerifyAll();
}

[Test]
public async Task When_DeviceModelId_Exists_Post_Should_Return_BadRequest()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ public virtual async Task<IActionResult> Post(TModel deviceModel)
TableEntity entity = new TableEntity()
{
PartitionKey = DefaultPartitionKey,
RowKey = deviceModel.ModelId ?? Guid.NewGuid().ToString()
RowKey = string.IsNullOrEmpty(deviceModel.ModelId) ? Guid.NewGuid().ToString() : deviceModel.ModelId
};

await this.SaveEntity(entity, deviceModel);
Expand Down

0 comments on commit 21cd5b5

Please sign in to comment.