public void ReturnsAllUsers_WhenParametersAreCorrect() { //Arrange var users = new List <ApplicationUser>() { new ApplicationUser() { UserName = "******", }, new ApplicationUser() { UserName = "******", }, new ApplicationUser() { UserName = "******", } }; var dbContextMock = new Mock <ApplicationDbContext>(); var usersDbSetMock = new Mock <DbSet <ApplicationUser> >().SetupData(users); dbContextMock.SetupGet(u => u.Users).Returns(usersDbSetMock.Object); SensorDbService service = new SensorDbService(dbContextMock.Object); //Act var usersResult = service.ListAllUsers(); //Assert Assert.IsNotNull(usersResult); }
public void AddsToDatabase_IfInputIsValud() { ICollection <Measurement> measurementsInDb = new List <Measurement>(); IEnumerable <Measurement> measurementsToInsertInDb = new List <Measurement>() { new Measurement() { Date = DateTime.Now, MeasurementType = "%", Value = "30", SensorId = 1, MeasurementId = 1 }, new Measurement() { Date = DateTime.Now, MeasurementType = "W", Value = "1200", SensorId = 1, MeasurementId = 2 }, new Measurement() { Date = DateTime.Now, MeasurementType = "%", Value = "70", SensorId = 1, MeasurementId = 3 } }; ICollection <LastValue> lastValuesInDb = new List <LastValue>() { new LastValue() { SensorId = 1 } }; var mockDbContext = new Mock <ApplicationDbContext>(); var measurementsDbSetMock = new Mock <DbSet <Measurement> >().SetupData(measurementsInDb); var lastValuesDbSetMock = new Mock <DbSet <LastValue> >().SetupData(lastValuesInDb); mockDbContext.SetupGet(c => c.Measurements).Returns(measurementsDbSetMock.Object); mockDbContext.SetupGet(c => c.LastValues).Returns(lastValuesDbSetMock.Object); SensorDbService service = new SensorDbService(mockDbContext.Object); //Act service.AddNewMeasurementsToDb(measurementsToInsertInDb); //Assert mockDbContext.Verify(c => c.Measurements, Times.Exactly(measurementsToInsertInDb.Count())); mockDbContext.Verify(c => c.LastValues, Times.Exactly(measurementsToInsertInDb.Count() * 2)); mockDbContext.Verify(c => c.SaveChanges(), Times.Once); }
public void ListSharedWithMeSensors_WhenUserHasAny() { //Arrange var sharedSensors = new List <Sensor>() { new Sensor() { SensorId = 1, Name = "sensor", SensorType = new SensorType() { Tag = " " }, ApplicationUser = new ApplicationUser() { UserName = "******" }, MeasurementType = "%", LastValue = new LastValue() { Value = "60" }, MinValue = 50, MaxValue = 70 } }; var users = new List <ApplicationUser>() { new ApplicationUser() { UserName = "******", SharedWithMe = sharedSensors } }; var dbContextMock = new Mock <ApplicationDbContext>(); var usersDbSetMock = new Mock <DbSet <ApplicationUser> >().SetupData(users); dbContextMock.SetupGet(m => m.Users).Returns(usersDbSetMock.Object); SensorDbService service = new SensorDbService(dbContextMock.Object); //Act service.ListSharedWithMeSensors("user"); //Assert dbContextMock.Verify(u => u.Users, Times.Once); }
public void SharesASensor_WhenParametersAreCorrect() { int sensorId = 1; string username = "******"; var sensors = new List <Sensor>() { new Sensor() { SensorId = 1, Name = "sensor", SensorType = new SensorType(), MeasurementType = "%", LastValue = new LastValue() { Value = "60" }, MinValue = 50, MaxValue = 70 } }; var users = new List <ApplicationUser>() { new ApplicationUser() { UserName = username, } }; var dbContextMock = new Mock <ApplicationDbContext>(); var usersDbSetMock = new Mock <DbSet <ApplicationUser> >().SetupData(users); var sensorsDbSetMock = new Mock <DbSet <Sensor> >().SetupData(sensors); dbContextMock.SetupGet(m => m.Users).Returns(usersDbSetMock.Object); dbContextMock.SetupGet(m => m.Sensors).Returns(sensorsDbSetMock.Object); SensorDbService service = new SensorDbService(dbContextMock.Object); //Act service.ShareSensor(sensorId, username); //var sharedSensor = dbContextMock.Object.Users.First(u => u.UserName == username).SharedWithMe.Last(); //var sharedSens = dbContextMock.Object.Sensors.First(s => s.SensorId == sensorId).SharedWith.First(); dbContextMock.Verify(c => c.Users, Times.Once); dbContextMock.Verify(c => c.Sensors, Times.Once); //Assert.AreEqual(sharedSens, sensors[0]); }
public void RegistersSensor_WhenParametersAreCorrect() { //Arrange var sensor = new Sensor() { LastValue = new LastValue() }; //var sensor = new Sensor(); var users = new List <ApplicationUser>() { new ApplicationUser() { UserName = "******", OwnedSensors = new List <Sensor>() { sensor } } }; //var lastValue = new LastValue(); var sensorTypes = new List <SensorType>() { new SensorType() }; var dbContextMock = new Mock <ApplicationDbContext>(); var sensorTypesDbSetMock = new Mock <DbSet <SensorType> >().SetupData(sensorTypes); var usersDbSetMock = new Mock <DbSet <ApplicationUser> >().SetupData(users); dbContextMock.SetupGet(c => c.SensorTypes).Returns(sensorTypesDbSetMock.Object); //new line dbContextMock.SetupGet(c => c.Users).Returns(usersDbSetMock.Object); SensorDbService service = new SensorDbService(dbContextMock.Object); //Act service.RegisterNewSensor("user", sensor); //service.RegisterNewSensor("user", sensor, lastValue); //Assert dbContextMock.Verify(d => d.SensorTypes, Times.Once); dbContextMock.Verify(d => d.Users, Times.Once); dbContextMock.Verify(d => d.SaveChanges(), Times.Once); }
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IWebHostEnvironment env, SensorDbService _sensor) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } app.UseWebSockets(); app.UseHttpsRedirection(); app.UseRouting(); app.UseAuthorization(); app.Use(async(context, next) => await new WebSocketService(_sensor).ProcessWebsocketSession(context, next)); app.UseEndpoints(endpoints => { endpoints.MapControllers(); }); }
public SensorController(SensorDbService sensor) { _sensor = sensor; }
public void CreatesSensor_WhenInputIsCorrect() { int sensorId = 1; var sensors = new List <Sensor>() { new Sensor() { SensorId = 1, ApplicationUser = new ApplicationUser() { UserName = "******" }, Name = "sensorOne", SensorType = new SensorType() { SensorIdICB = "idIcb", Tag = "tag" }, MeasurementType = "%", LastValue = new LastValue() { Value = "60", PollingInterval = 20 }, OperatingRange = "", AddedOn = DateTime.Now, MinValue = 50, MaxValue = 70, IsPublic = true, DescriptionGivenByTheUser = "******" }, new Sensor() { SensorId = 2, ApplicationUser = new ApplicationUser() { UserName = "******" }, Name = "sensorTwo", SensorType = new SensorType() { SensorIdICB = "idIcb", Tag = "tag" }, MeasurementType = "%", LastValue = new LastValue() { Value = "50", PollingInterval = 20 }, OperatingRange = "", AddedOn = DateTime.Now, MinValue = 40, MaxValue = 60, IsPublic = true, DescriptionGivenByTheUser = "******" }, }; var dbContextMock = new Mock <ApplicationDbContext>(); var sensorsDbSetMock = new Mock <DbSet <Sensor> >().SetupData(sensors); dbContextMock.SetupGet(m => m.Sensors).Returns(sensorsDbSetMock.Object); SensorDbService service = new SensorDbService(dbContextMock.Object); //Act service.GetSpecificSensorDisplayDataAsync(sensorId); //Assert dbContextMock.Verify(c => c.Sensors, Times.Once); //var expectedSensorDisplayData = new SensorDisplayData() //{ // SensorIdICB = personalSensors[0].SensorType.SensorIdICB, // UserName = personalSensors[0].ApplicationUser.UserName, // SensorName = personalSensors[0].Name, // SensorTag = personalSensors[0].SensorType.Tag, // MeasurementType = personalSensors[0].MeasurementType, // LastValue = personalSensors[0].LastValue.Value, // MinValue = personalSensors[0].MinValue, // MaxValue = personalSensors[0].MaxValue, // IsPublic = personalSensors[0].IsPublic, // PollingInterval = personalSensors[0].LastValue.PollingInterval, // OperatingRange = personalSensors[0].OperatingRange, // AddedOn = personalSensors[0].AddedOn, // DescriptionGivenByTheUser = personalSensors[0].DescriptionGivenByTheUser //}; // public virtual async Task<SensorDisplayData> GetSpecificSensorDisplayDataAsync(int sensorId) // { // return await dbContext.Sensors.Select(sensor => new SensorDisplayData // { // SensorId = sensor.SensorId, // SensorIdICB = sensor.SensorType.SensorIdICB, // UserName = sensor.ApplicationUser.UserName, // SensorName = sensor.Name, // SensorTag = sensor.SensorType.Tag, // MeasurementType = sensor.MeasurementType, // LastValue = sensor.LastValue.Value, // MinValue = sensor.MinValue, // MaxValue = sensor.MaxValue, // IsPublic = sensor.IsPublic, // PollingInterval = sensor.LastValue.PollingInterval, // AddedOn = sensor.AddedOn, // DescriptionGivenByTheUser = sensor.DescriptionGivenByTheUser // }).FirstOrDefaultAsync(x => x.SensorId == sensorId); // } }
public void ListsOwnUsers_WhenParametersAreCorrect() { string user = "******"; //Arrange var ownSensors = new List <Sensor>() { new Sensor() { ApplicationUser = new ApplicationUser() { UserName = "******" }, Name = "sensor", SensorType = new SensorType() { SensorIdICB = "idIcb", Tag = "tag" }, MeasurementType = "%", LastValue = new LastValue() { Value = "60", PollingInterval = 20 }, OperatingRange = "", AddedOn = DateTime.Now, MinValue = 50, MaxValue = 70, IsPublic = true, DescriptionGivenByTheUser = "******" }, new Sensor() { ApplicationUser = new ApplicationUser() { UserName = "******" }, Name = "sensor", SensorType = new SensorType() { SensorIdICB = "idIcb", Tag = "tag" }, MeasurementType = "%", LastValue = new LastValue() { Value = "50", PollingInterval = 20 }, OperatingRange = "", AddedOn = DateTime.Now, MinValue = 40, MaxValue = 60, IsPublic = true, DescriptionGivenByTheUser = "******" }, }; var users = new List <ApplicationUser>() { new ApplicationUser() { UserName = "******", OwnedSensors = ownSensors } }; var dbContextMock = new Mock <ApplicationDbContext>(); var usersDbSetMock = new Mock <DbSet <ApplicationUser> >().SetupData(users); dbContextMock.SetupGet(m => m.Users).Returns(usersDbSetMock.Object); SensorDbService service = new SensorDbService(dbContextMock.Object); //Act var sensors = service.ListOwnSensors(user); //Assert Assert.IsNotNull(sensors); dbContextMock.Verify(u => u.Users, Times.Once); Assert.AreEqual(sensors.Count(), ownSensors.Count()); }