Skip to content

Commit

Permalink
#1924 Add TU and update log message type
Browse files Browse the repository at this point in the history
  • Loading branch information
delager authored and kbeaugrand committed Jun 1, 2023
1 parent 0d2797b commit 8df0f11
Show file tree
Hide file tree
Showing 2 changed files with 167 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ private async Task SyncThingsAsDevices()
var thingShadow = await this.amazonIoTDataClient.GetThingShadowAsync(thingShadowRequest);
if (thingShadow.HttpStatusCode.Equals(HttpStatusCode.NotFound))
{
this.logger.LogWarning($"Cannot import device '{thing.ThingName}' since it doesn't have related thing shadow");
this.logger.LogInformation($"Cannot import device '{thing.ThingName}' since it doesn't have related thing shadow");
continue;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -273,5 +273,171 @@ public async Task ExecuteExistingDeviceWithOlderVersionDeviceNotUpdated()
// Assert
MockRepository.VerifyAll();
}

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

var expectedDeviceModel = Fixture.Create<DeviceModel>();
var existingDevice = new Device
{
Id = Fixture.Create<string>(),
Name = Fixture.Create<string>(),
DeviceModel = expectedDeviceModel,
DeviceModelId = expectedDeviceModel.Id,
Version = 2
};

var thingsListing = new ListThingsResponse
{
Things = new List<ThingAttribute>()
{
new ThingAttribute
{
ThingName = existingDevice.Name
}
}
};

_ = this.amazonIoTClient.Setup(client => client.ListThingsAsync(It.IsAny<CancellationToken>()))
.ReturnsAsync(thingsListing);

_ = this.amazonIoTClient.Setup(client => client.DescribeThingAsync(It.IsAny<DescribeThingRequest>(), It.IsAny<CancellationToken>()))
.ReturnsAsync(new DescribeThingResponse()
{
ThingId = existingDevice.Id,
ThingName = existingDevice.Name,
Version = 1
});

_ = this.mockDeviceRepository.Setup(x => x.GetAllAsync(It.IsAny<Expression<Func<Device, bool>>>(), It.IsAny<CancellationToken>(), d => d.Tags, d => d.Labels))
.ReturnsAsync(new List<Device>());

_ = this.mockUnitOfWork.Setup(work => work.SaveAsync())
.Returns(Task.CompletedTask);

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

// Assert
MockRepository.VerifyAll();
}

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

var expectedDeviceModel = Fixture.Create<DeviceModel>();
var existingDevice = new Device
{
Id = Fixture.Create<string>(),
Name = Fixture.Create<string>(),
DeviceModel = expectedDeviceModel,
DeviceModelId = expectedDeviceModel.Id,
Version = 2
};

var thingsListing = new ListThingsResponse
{
Things = new List<ThingAttribute>()
{
new ThingAttribute
{
ThingName = existingDevice.Name
}
}
};

_ = this.amazonIoTClient.Setup(client => client.ListThingsAsync(It.IsAny<CancellationToken>()))
.ReturnsAsync(thingsListing);

_ = this.amazonIoTClient.Setup(client => client.DescribeThingAsync(It.IsAny<DescribeThingRequest>(), It.IsAny<CancellationToken>()))
.ReturnsAsync(new DescribeThingResponse()
{
ThingId = existingDevice.Id,
ThingName = existingDevice.Name,
ThingTypeName = existingDevice.DeviceModel.Name,
Version = 1
});

_ = this.mockDeviceModelRepository
.Setup(x => x.GetByName(existingDevice.DeviceModel.Name))
.Returns((DeviceModel)null);

_ = this.mockDeviceRepository.Setup(x => x.GetAllAsync(It.IsAny<Expression<Func<Device, bool>>>(), It.IsAny<CancellationToken>(), d => d.Tags, d => d.Labels))
.ReturnsAsync(new List<Device>());

_ = this.mockUnitOfWork.Setup(work => work.SaveAsync())
.Returns(Task.CompletedTask);

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

// Assert
MockRepository.VerifyAll();
}

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

var expectedDeviceModel = Fixture.Create<DeviceModel>();
var existingDevice = new Device
{
Id = Fixture.Create<string>(),
Name = Fixture.Create<string>(),
DeviceModel = expectedDeviceModel,
DeviceModelId = expectedDeviceModel.Id,
Version = 2
};

var thingsListing = new ListThingsResponse
{
Things = new List<ThingAttribute>()
{
new ThingAttribute
{
ThingName = existingDevice.Name
}
}
};

_ = this.amazonIoTClient.Setup(client => client.ListThingsAsync(It.IsAny<CancellationToken>()))
.ReturnsAsync(thingsListing);

_ = this.amazonIoTClient.Setup(client => client.DescribeThingAsync(It.IsAny<DescribeThingRequest>(), It.IsAny<CancellationToken>()))
.ReturnsAsync(new DescribeThingResponse()
{
ThingId = existingDevice.Id,
ThingName = existingDevice.Name,
ThingTypeName = existingDevice.DeviceModel.Name,
Version = 1
});

_ = this.mockDeviceModelRepository
.Setup(x => x.GetByName(existingDevice.DeviceModel.Name))
.Returns(expectedDeviceModel);

_ = this.amazonIoTDataClient.Setup(client => client.GetThingShadowAsync(It.IsAny<GetThingShadowRequest>(), It.IsAny<CancellationToken>()))
.ReturnsAsync(new GetThingShadowResponse() { HttpStatusCode = HttpStatusCode.NotFound });

_ = this.mockDeviceRepository.Setup(x => x.GetAllAsync(It.IsAny<Expression<Func<Device, bool>>>(), It.IsAny<CancellationToken>(), d => d.Tags, d => d.Labels))
.ReturnsAsync(new List<Device>());

_ = this.mockUnitOfWork.Setup(work => work.SaveAsync())
.Returns(Task.CompletedTask);

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

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

0 comments on commit 8df0f11

Please sign in to comment.