Beispiel #1
0
        private Model GetSimulationApiModel()
        {
            var simulationApiModel = new Model
            {
                Id           = "id",
                ETag         = "etag",
                StartTime    = DateTimeOffset.UtcNow.ToString(),
                EndTime      = DateTimeOffset.UtcNow.AddHours(1).ToString(),
                Enabled      = false,
                DeviceModels = new List <SimulationDeviceModelRef>()
                {
                    new SimulationDeviceModelRef()
                    {
                        Id    = "device_id",
                        Count = 1
                    }
                },
                IotHubs = new List <SimulationIotHub> {
                    new SimulationIotHub("HostName=[hubname];SharedAccessKeyName=[iothubowner];SharedAccessKey=[valid key]")
                },
                RateLimits = new SimulationRateLimits
                {
                    ConnectionsPerSecond        = 100,
                    RegistryOperationsPerMinute = 100,
                    TwinReadsPerSecond          = 10,
                    TwinWritesPerSecond         = 10,
                    DeviceMessagesPerSecond     = 120
                }
            };

            return(simulationApiModel);
        }
Beispiel #2
0
        public void ItSetsSimulationStatisticsFromServiceModel()
        {
            // Arrange
            var simulation = this.GetSimulationModel();
            var statistics = new SimulationStatisticsModel {
                ActiveDevices = 10, TotalMessagesSent = 100, FailedDeviceConnections = 1, FailedDevicePropertiesUpdates = 2, FailedMessages = 3
            };

            simulation.Statistics = statistics;
            var now = DateTimeOffset.UtcNow;

            simulation.ActualStartTime = now.AddSeconds(-60);
            simulation.StoppedTime     = now;
            simulation.Enabled         = false;
            // Avg messages = 100/60 (TotalMessagesSent / stoppedTime - startTime)
            var expectedAvgMessages = 1.67;

            // Act
            var result = Model.FromServiceModel(simulation);

            // Assert
            Assert.IsType <Model>(result);
            Assert.Equal(simulation.Id, result.Id);
            Assert.NotNull(result.Statistics);
            Assert.Equal(statistics.ActiveDevices, result.Statistics.ActiveDevices);
            Assert.Equal(statistics.TotalMessagesSent, result.Statistics.TotalMessagesSent);
            Assert.Equal(statistics.FailedDeviceConnections, result.Statistics.FailedDeviceConnections);
            Assert.Equal(statistics.FailedDevicePropertiesUpdates, result.Statistics.FailedDevicePropertiesUpdates);
            Assert.Equal(statistics.FailedMessages, result.Statistics.FailedMessages);
            Assert.Equal(expectedAvgMessages, result.Statistics.AverageMessagesPerSecond);
        }
Beispiel #3
0
        // Map service model to API model
        public static SimulationApiModel FromServiceModel(Simulation value)
        {
            if (value == null)
            {
                return(null);
            }

            var result = new SimulationApiModel
            {
                ETag    = value.ETag,
                Id      = value.Id,
                Enabled = value.Enabled,
                IotHub  = new SimulationIotHub(value.IotHubConnectionString)
            };

            // Ignore the date if the simulation doesn't have a start time
            if (value.StartTime.HasValue && !value.StartTime.Value.Equals(DateTimeOffset.MinValue))
            {
                result.StartTime = value.StartTime?.ToString(DATE_FORMAT);
            }

            // Ignore the date if the simulation doesn't have an end time
            if (value.EndTime.HasValue && !value.EndTime.Value.Equals(DateTimeOffset.MaxValue))
            {
                result.EndTime = value.EndTime?.ToString(DATE_FORMAT);
            }

            result.DeviceModels = SimulationDeviceModelRef.FromServiceModel(value.DeviceModels);
            result.version      = value.Version;
            result.created      = value.Created;
            result.modified     = value.Modified;

            return(result);
        }
Beispiel #4
0
        // Map service model to API model
        public static SimulationApiModel FromServiceModel(
            Simulation value)
        {
            if (value == null)
            {
                return(null);
            }

            var result = new SimulationApiModel
            {
                ETag                            = value.ETag,
                Id                              = value.Id,
                Name                            = value.Name,
                Description                     = value.Description,
                Enabled                         = value.Enabled,
                Running                         = value.ShouldBeRunning,
                ActiveNow                       = value.IsActiveNow,
                DeleteDevicesOnce               = value.DeleteDevicesOnce,
                DevicesDeletionComplete         = value.DevicesDeletionComplete,
                DeleteDevicesWhenSimulationEnds = value.DeleteDevicesWhenSimulationEnds,
                IotHubs                         = new List <SimulationIotHub>(),
                ReplayFileId                    = value.ReplayFileId,
                ReplayFileIndefinitely          = value.ReplayFileRunIndefinitely
            };

            foreach (var iotHubConnectionString in value.IotHubConnectionStrings)
            {
                var iotHub = new SimulationIotHub {
                    ConnectionString = iotHubConnectionString
                };
                result.IotHubs.Add(iotHub);
            }

            // Ignore the date if the simulation doesn't have a start time
            if (value.StartTime.HasValue && !value.StartTime.Value.Equals(DateTimeOffset.MinValue))
            {
                result.StartTime = value.StartTime?.ToString(DATE_FORMAT);
            }

            // Ignore the date if the simulation doesn't have an end time
            if (value.EndTime.HasValue && !value.EndTime.Value.Equals(DateTimeOffset.MaxValue))
            {
                result.EndTime = value.EndTime?.ToString(DATE_FORMAT);
            }

            // Ignore the date if the simulation doesn't have an end time
            if (value.StoppedTime.HasValue && !value.StoppedTime.Value.Equals(DateTimeOffset.MaxValue))
            {
                result.StoppedTime = value.StoppedTime?.ToString(DATE_FORMAT);
            }

            result.DeviceModels = SimulationDeviceModelRef.FromServiceModel(value.DeviceModels);
            result.Statistics   = GetSimulationStatistics(value);
            result.RateLimits   = SimulationRateLimits.FromServiceModel(value.RateLimits);

            result.created  = value.Created;
            result.modified = value.Modified;

            return(result);
        }
Beispiel #5
0
        public void ItReturnsSimulationApiModelFromServiceModel()
        {
            // Arrange
            var simulation = this.GetSimulationModel();

            this.SetupDefaultRateLimits();

            // Act
            var result = Model.FromServiceModel(simulation);

            // Assert
            Assert.IsType <Model>(result);
            Assert.Equal(simulation.Id, result.Id);
        }
Beispiel #6
0
        private Microsoft.Azure.IoTSolutions.DeviceSimulation.WebService.v1.Models.SimulationApiModel.SimulationApiModel GetSimulationApiModel()
        {
            var simulationApiModel = new Microsoft.Azure.IoTSolutions.DeviceSimulation.WebService.v1.Models.SimulationApiModel.SimulationApiModel
            {
                Id           = "id",
                ETag         = "etag",
                StartTime    = DateTimeOffset.UtcNow.ToString(),
                EndTime      = DateTimeOffset.UtcNow.AddHours(1).ToString(),
                Enabled      = false,
                DeviceModels = new List <SimulationDeviceModelRef>()
                {
                    new SimulationDeviceModelRef()
                    {
                        Id    = "device_id",
                        Count = 1
                    }
                },
                IotHub = new SimulationIotHub("HostName=[hubname];SharedAccessKeyName=[iothubowner];SharedAccessKey=[valid key]")
            };

            return(simulationApiModel);
        }
Beispiel #7
0
        private Model GetBasicSimulationApiModel()
        {
            var simulationApiModel = new Model
            {
                Id           = "id",
                ETag         = "etag",
                StartTime    = DateTimeOffset.UtcNow.ToString(),
                EndTime      = DateTimeOffset.UtcNow.AddHours(1).ToString(),
                Enabled      = false,
                DeviceModels = new List <SimulationDeviceModelRef>()
                {
                    new SimulationDeviceModelRef()
                    {
                        Id    = "device_id",
                        Count = 1
                    }
                },
                IotHubs = new List <SimulationIotHub> {
                    new SimulationIotHub("HostName=[hubname];SharedAccessKeyName=[iothubowner];SharedAccessKey=[valid key]")
                }
            };

            return(simulationApiModel);
        }
        // Map service model to API model
        public static SimulationApiModel FromServiceModel(
            Simulation value,
            IServicesConfig servicesConfig,
            IDeploymentConfig deploymentConfig,
            IIotHubConnectionStringManager connectionStringManager,
            ISimulationRunner simulationRunner,
            IRateLimiting rateReporter)
        {
            if (value == null)
            {
                return(null);
            }

            var result = new SimulationApiModel
            {
                ETag        = value.ETag,
                Id          = value.Id,
                Name        = value.Name,
                Description = value.Description,
                Enabled     = value.Enabled,
                Running     = value.ShouldBeRunning,
                StartTime   = value.StartTime.ToString(),
                EndTime     = value.EndTime.ToString(),
                StoppedTime = value.StoppedTime.ToString(),
                IotHubs     = new List <SimulationIotHub>()
            };

            foreach (var iotHubConnectionString in value.IotHubConnectionStrings)
            {
                var iotHub = new SimulationIotHub {
                    ConnectionString = iotHubConnectionString
                };
                result.IotHubs.Add(iotHub);
            }

            // Ignore the date if the simulation doesn't have a start time
            if (value.StartTime.HasValue && !value.StartTime.Value.Equals(DateTimeOffset.MinValue))
            {
                result.StartTime = value.StartTime?.ToString(DATE_FORMAT);
            }

            // Ignore the date if the simulation doesn't have an end time
            if (value.EndTime.HasValue && !value.EndTime.Value.Equals(DateTimeOffset.MaxValue))
            {
                result.EndTime = value.EndTime?.ToString(DATE_FORMAT);
            }

            // Ignore the date if the simulation doesn't have an end time
            if (value.StoppedTime.HasValue && !value.StoppedTime.Value.Equals(DateTimeOffset.MaxValue))
            {
                result.StoppedTime = value.StoppedTime?.ToString(DATE_FORMAT);
            }

            result.DeviceModels = SimulationDeviceModelRef.FromServiceModel(value.DeviceModels);
            result.Statistics   = SimulationStatistics.FromServiceModel(value.Statistics);
            result.created      = value.Created;
            result.modified     = value.Modified;

            result.AppendHubPropertiesAndStatistics(servicesConfig, deploymentConfig, connectionStringManager, simulationRunner, rateReporter);

            return(result);
        }