Example #1
0
        public ActionResult CreateSensor(SensorViewModel model)
        {
            if (ModelState.IsValid)
            {
                var sensor = new DataflowICB.Database.Models.Sensor()
                {
                    OwnerId         = this.User.Identity.GetUserId(),
                    Description     = model.Description,
                    IsPublic        = model.IsPublic,
                    Name            = model.Name,
                    URL             = model.Url,
                    PollingInterval = model.PollingInterval,
                    LastUpdate      = DateTime.Now
                };

                if (model.IsValueType)
                {
                    var valueType = new ValueTypeSensor()
                    {
                        MeasurementType = model.MeasurementType,
                        //IsInAcceptableRange = model.ValueTypeSensor.IsInAcceptableRange,
                        Maxvalue = model.MaxValue,
                        MinValue = model.MinValue
                    };
                    sensor.IsBoolType      = false;
                    sensor.ValueTypeSensor = valueType;
                }
                else
                {
                    var boolType = new BoolTypeSensor()
                    {
                        MeasurementType = model.MeasurementType
                    };

                    sensor.IsBoolType     = true;
                    sensor.BoolTypeSensor = boolType;
                }

                this.sensorService.AddSensor(sensor);

                return(this.Json("Successfully Registered !"));
            }
            else
            {
                if (model.IsValueType)
                {
                    return(this.View("RegisterValueSensor", model));
                }
                else
                {
                    return(this.View("RegisterBoolSensor", model));
                }
            }
        }
Example #2
0
        public void ReturnListOfValues()
        {
            //Arrange
            var dbContextMock    = new Mock <ApplicationDbContext>();
            var httpClientMock   = new Mock <IHttpClientProvider>();
            var emailServiceMock = new Mock <IEmailService>();
            int id = 1;

            var sensorServices = new Dataflow.DataServices.SensorService(dbContextMock.Object, httpClientMock.Object, emailServiceMock.Object);

            var valueHistory = new List <ValueHistory>()
            {
                new ValueHistory
                {
                    BoolSensorId = id,
                    Date         = DateTime.ParseExact("03/03/2017 11:21:45", "dd/MM/yyyy HH:mm:ss", CultureInfo.InvariantCulture),
                    Value        = 1.0
                },

                new ValueHistory
                {
                    BoolSensorId = id,
                    Date         = DateTime.ParseExact("03/03/2017 11:21:50", "dd/MM/yyyy HH:mm:ss", CultureInfo.InvariantCulture),
                    Value        = 0.0
                },

                new ValueHistory
                {
                    BoolSensorId = id,
                    Date         = DateTime.ParseExact("03/03/2017 11:21:55", "dd/MM/yyyy HH:mm:ss", CultureInfo.InvariantCulture),
                    Value        = 0.0
                }
            };

            var boolSensor = new BoolTypeSensor()
            {
                Id              = id,
                CurrentValue    = true,
                MeasurementType = "Open/Close",
                BoolHistory     = valueHistory
            };

            var historySetMock = new Mock <DbSet <ValueHistory> >().SetupData(valueHistory);

            dbContextMock.SetupGet(m => m.ValueHistory).Returns(historySetMock.Object);

            //Act
            List <SensorApiUpdate> result = sensorServices.HistoryDataForBoolSensorsById(id).ToList();

            //Assert
            Assert.AreEqual(3, result.Count());
            Assert.AreEqual(valueHistory[0].Value.ToString(), result[0].Value);
        }
Example #3
0
        public void CallValueHistoryOnce_WhenSensorIsExistent()
        {
            //Arrange
            var dbContextMock    = new Mock <ApplicationDbContext>();
            var httpClientMock   = new Mock <IHttpClientProvider>();
            var emailServiceMock = new Mock <IEmailService>();
            int id = 1;

            var sensorServices = new Dataflow.DataServices.SensorService(dbContextMock.Object, httpClientMock.Object, emailServiceMock.Object);

            var valueHistory = new List <ValueHistory>()
            {
                new ValueHistory
                {
                    BoolSensorId = id,
                    Date         = DateTime.ParseExact("03/03/2017 11:21:45", "dd/MM/yyyy HH:mm:ss", CultureInfo.InvariantCulture),
                    Value        = 1.0
                },

                new ValueHistory
                {
                    BoolSensorId = id,
                    Date         = DateTime.ParseExact("03/03/2017 11:21:50", "dd/MM/yyyy HH:mm:ss", CultureInfo.InvariantCulture),
                    Value        = 0.0
                },

                new ValueHistory
                {
                    BoolSensorId = id,
                    Date         = DateTime.ParseExact("03/03/2017 11:21:55", "dd/MM/yyyy HH:mm:ss", CultureInfo.InvariantCulture),
                    Value        = 0.0
                }
            };

            var boolSensor = new BoolTypeSensor()
            {
                Id              = id,
                CurrentValue    = true,
                MeasurementType = "Open/Close",
                BoolHistory     = valueHistory
            };

            var historySetMock = new Mock <DbSet <ValueHistory> >().SetupData(valueHistory);

            dbContextMock.SetupGet(m => m.ValueHistory).Returns(historySetMock.Object);

            //Act
            sensorServices.HistoryDataForBoolSensorsById(id).ToList();

            //Assert
            dbContextMock.Verify(d => d.ValueHistory, Times.Once());
        }
        public void ReturnListOfAllPublicSensorDataModels_WhenIsAdminIsFalse()
        {
            //Arrange
            var  dbContextMock    = new Mock <ApplicationDbContext>();
            var  httpClientMock   = new Mock <IHttpClientProvider>();
            var  emailServiceMock = new Mock <IEmailService>();
            var  userMock         = new Mock <ApplicationUser>();
            bool IsAdmin          = false;

            var termometer = new ValueTypeSensor()
            {
                MinValue        = 15,
                Maxvalue        = 30,
                CurrentValue    = 20,
                MeasurementType = "Temperatura"
            };

            var door = new BoolTypeSensor()
            {
                CurrentValue    = true,
                MeasurementType = "Open/Close"
            };

            List <Sensor> sensors = new List <Sensor>()
            {
                new Sensor()
                {
                    Id              = 5,
                    Name            = "termometur",
                    IsBoolType      = false,
                    URL             = "theGreatUrl",
                    PollingInterval = 20,
                    ValueTypeSensor = termometer,
                    IsPublic        = true,
                    IsShared        = false,
                    OwnerId         = "stringId",
                    Owner           = userMock.Object,
                    IsDeleted       = true
                },

                new Sensor()
                {
                    Id              = 7,
                    Name            = "Door",
                    IsBoolType      = true,
                    URL             = "theGreatUrlPart2",
                    PollingInterval = 25,
                    BoolTypeSensor  = door,
                    IsPublic        = false,
                    IsShared        = false,
                    OwnerId         = "stringId",
                    Owner           = userMock.Object,
                    IsDeleted       = false
                },
            };

            userMock.SetupGet(u => u.UserName).Returns("OwnerOfSensor");

            var sensorsSetMock = new Mock <DbSet <Sensor> >().SetupData(sensors);

            dbContextMock.SetupGet(m => m.Sensors).Returns(sensorsSetMock.Object);

            var sensorServices = new Dataflow.DataServices.SensorService(dbContextMock.Object, httpClientMock.Object, emailServiceMock.Object);

            //Act
            List <SensorDataModel> result = sensorServices.GetAllSensors(IsAdmin).ToList();

            //Assert
            Assert.AreEqual(1, result.Count());
            Assert.AreEqual(sensors[1].Name, result[0].Name);
            dbContextMock.Verify(d => d.Sensors, Times.Once());
        }
Example #5
0
        public void CallSensorsOnce_WhenSensorIsExistent()
        {
            //Arrange
            var dbContextMock    = new Mock <ApplicationDbContext>();
            var httpClientMock   = new Mock <IHttpClientProvider>();
            var emailServiceMock = new Mock <IEmailService>();
            var sensorMock       = new Mock <ISensorDataModel>();
            int firstSensorId    = 2;
            int secondSensorId   = 7;

            string firstUsername = "******";
            string firstUserId   = "firstUserId";

            string secondUsername = "******";
            string secondUserId   = "secondUserId";

            sensorMock.Setup(x => x.Id).Returns(firstSensorId);

            var firstUserMock = new Mock <ApplicationUser>();

            firstUserMock.Setup(u => u.UserName).Returns(firstUsername);
            firstUserMock.Setup(u => u.Id).Returns(firstUserId);

            var secondUserMock = new Mock <ApplicationUser>();

            secondUserMock.Setup(u => u.UserName).Returns(secondUsername);
            secondUserMock.Setup(u => u.Id).Returns(secondUserId);

            List <ApplicationUser> users = new List <ApplicationUser>()
            {
                firstUserMock.Object,
                secondUserMock.Object
            };

            var termometer = new ValueTypeSensor()
            {
                MinValue        = 15,
                Maxvalue        = 30,
                CurrentValue    = 20,
                MeasurementType = "Temperatura"
            };

            var door = new BoolTypeSensor()
            {
                CurrentValue    = true,
                MeasurementType = "Open/Close"
            };

            List <Sensor> sensors = new List <Sensor>()
            {
                new Sensor()
                {
                    Id              = firstSensorId,
                    Name            = "termometur",
                    IsBoolType      = false,
                    URL             = "theGreatUrl",
                    PollingInterval = 20,
                    ValueTypeSensor = termometer,
                    IsPublic        = true,
                    IsShared        = true,
                    OwnerId         = "stringId",
                    Owner           = firstUserMock.Object,
                    IsDeleted       = false
                },

                new Sensor()
                {
                    Id              = secondSensorId,
                    Name            = "Door",
                    IsBoolType      = true,
                    URL             = "theGreatUrlPart2",
                    PollingInterval = 25,
                    BoolTypeSensor  = door,
                    IsPublic        = false,
                    IsShared        = false,
                    OwnerId         = "stringId",
                    Owner           = firstUserMock.Object,
                    IsDeleted       = false
                },
            };

            var sensorsSetMock = new Mock <DbSet <Sensor> >().SetupData(sensors);

            var usersSetMock = new Mock <DbSet <ApplicationUser> >().SetupData(users);

            dbContextMock.SetupGet(m => m.Sensors).Returns(sensorsSetMock.Object);

            dbContextMock.SetupGet(m => m.Users).Returns(usersSetMock.Object);

            var sensorServices = new Dataflow.DataServices.SensorService(dbContextMock.Object, httpClientMock.Object, emailServiceMock.Object);

            //Act
            sensorServices.ShareSensorWithUser(firstSensorId, secondUsername);

            //Assert
            dbContextMock.Verify(d => d.Users, Times.Once());
            dbContextMock.Verify(d => d.Sensors, Times.Once());
        }
Example #6
0
        public void CallSensorsOnceAndReturnSensorWithId_WhenSensorIsExistent()
        {
            //Arrange
            var dbContextMock    = new Mock <ApplicationDbContext>();
            var httpClientMock   = new Mock <IHttpClientProvider>();
            var emailServiceMock = new Mock <IEmailService>();
            var sensorMock       = new Mock <ISensorDataModel>();
            int Id = 2;

            sensorMock.Setup(x => x.Id).Returns(Id);

            var userMock = new Mock <ApplicationUser>();

            var termometer = new ValueTypeSensor()
            {
                MinValue        = 15,
                Maxvalue        = 30,
                CurrentValue    = 20,
                MeasurementType = "Temperatura"
            };

            var door = new BoolTypeSensor()
            {
                CurrentValue    = true,
                MeasurementType = "Open/Close"
            };

            List <Sensor> sensors = new List <Sensor>()
            {
                new Sensor()
                {
                    Id              = Id,
                    Name            = "termometur",
                    IsBoolType      = false,
                    URL             = "theGreatUrl",
                    PollingInterval = 20,
                    ValueTypeSensor = termometer,
                    IsPublic        = true,
                    IsShared        = false,
                    OwnerId         = "stringId",
                    Owner           = userMock.Object,
                    IsDeleted       = false
                },

                new Sensor()
                {
                    Id              = 7,
                    Name            = "Door",
                    IsBoolType      = true,
                    URL             = "theGreatUrlPart2",
                    PollingInterval = 25,
                    BoolTypeSensor  = door,
                    IsPublic        = false,
                    IsShared        = false,
                    OwnerId         = "stringId",
                    Owner           = userMock.Object,
                    IsDeleted       = false
                },
            };

            var sensorsSetMock = new Mock <DbSet <Sensor> >().SetupData(sensors);

            dbContextMock.SetupGet(m => m.Sensors).Returns(sensorsSetMock.Object);

            var sensorServices = new Dataflow.DataServices.SensorService(dbContextMock.Object, httpClientMock.Object, emailServiceMock.Object);

            //Act
            var resultSensor = sensorServices.GetUserSensorById(Id);

            //Assert
            Assert.AreEqual(Id, resultSensor.Id);
            dbContextMock.Verify(d => d.Sensors, Times.Once());
        }
        public void ShouldGetUsersSharedSensors()
        {
            //Arrange
            var    dbContextMock       = new Mock <ApplicationDbContext>();
            var    httpClientMock      = new Mock <IHttpClientProvider>();
            var    emailServiceMock    = new Mock <IEmailService>();
            var    ownerMock           = new Mock <ApplicationUser>();
            var    userToShareWithMock = new Mock <ApplicationUser>();
            string username            = "******";
            string email = "*****@*****.**";

            ownerMock.SetupGet(u => u.UserName).Returns(username);
            ownerMock.SetupGet(u => u.Email).Returns(email);

            var sensorServices = new Dataflow.DataServices.SensorService(dbContextMock.Object, httpClientMock.Object, emailServiceMock.Object);

            List <ApplicationUser> users = new List <ApplicationUser>
            {
                new ApplicationUser {
                    UserName = "******", Email = "*****@*****.**"
                },
                new ApplicationUser {
                    UserName = "******", Email = "*****@*****.**"
                },
                new ApplicationUser {
                    UserName = "******", Email = "*****@*****.**"
                },
                ownerMock.Object
            };

            List <string> usernames = new List <string>
            {
                "Ivan",
                "Mariq",
                "Prolet"
            };

            var termometer = new ValueTypeSensor()
            {
                MinValue        = 15,
                Maxvalue        = 30,
                CurrentValue    = 20,
                MeasurementType = "Temperatura",
                IsConnected     = true
            };

            var door = new BoolTypeSensor()
            {
                CurrentValue    = true,
                MeasurementType = "Open/Close",
                IsConnected     = true
            };

            List <Sensor> sensors = new List <Sensor>()
            {
                new Sensor()
                {
                    Id              = 4,
                    Name            = "termometur",
                    Description     = "meresht temperatura",
                    IsBoolType      = false,
                    URL             = "theGreatUrl",
                    PollingInterval = 20,
                    IsPublic        = true,
                    IsShared        = true,
                    OwnerId         = "stringId",
                    Owner           = ownerMock.Object,
                    IsDeleted       = false,
                    SharedWithUsers = users,
                    ValueTypeSensor = termometer,
                },

                new Sensor()
                {
                    Id              = 7,
                    Name            = "Door",
                    Description     = "meresht otvarqne i zatvarqne",
                    IsBoolType      = true,
                    URL             = "theGreatUrlPart2",
                    PollingInterval = 25,
                    IsPublic        = false,
                    IsShared        = false,
                    OwnerId         = "stringId",
                    Owner           = ownerMock.Object,
                    IsDeleted       = false,
                    BoolTypeSensor  = door
                },
            };
            List <Sensor> sharedSensors = new List <Sensor>();

            sharedSensors.Add(sensors[0]);

            ownerMock.SetupGet(u => u.SharedSensors).Returns(sharedSensors);

            var sensorsSetMock = new Mock <DbSet <Sensor> >().SetupData(sensors);

            var usersSetMock = new Mock <DbSet <ApplicationUser> >().SetupData(users);

            dbContextMock.SetupGet(m => m.Sensors).Returns(sensorsSetMock.Object);

            dbContextMock.SetupGet(m => m.Users).Returns(usersSetMock.Object);

            //Act
            List <SensorDataModel> result = sensorServices.GetSharedWithUserSensors(username).ToList();

            //Assert
            Assert.AreEqual(1, result.Count);
            Assert.AreEqual(sensors[0].IsShared, result[0].IsShared);
            dbContextMock.Verify(d => d.Users, Times.Once);
        }
        public void ReturnListOfAllSensorForUser()
        {
            //Arrange
            var dbContextMock    = new Mock <ApplicationDbContext>();
            var httpClientMock   = new Mock <IHttpClientProvider>();
            var emailServiceMock = new Mock <IEmailService>();
            var sensorMock       = new Mock <ISensorDataModel>();
            int Id = 2;

            sensorMock.Setup(x => x.Id).Returns(Id);

            var    userMock = new Mock <ApplicationUser>();
            string username = "******";

            userMock.SetupGet(u => u.UserName).Returns(username);

            var    secondUserMock = new Mock <ApplicationUser>();
            string secondUsername = "******";

            secondUserMock.SetupGet(u => u.UserName).Returns(secondUsername);

            var termometer = new ValueTypeSensor()
            {
                MinValue        = 15,
                Maxvalue        = 30,
                CurrentValue    = 20,
                MeasurementType = "Temperatura",
                IsConnected     = true
            };

            var door = new BoolTypeSensor()
            {
                CurrentValue    = true,
                MeasurementType = "Open/Close",
                IsConnected     = true
            };

            List <Sensor> sensors = new List <Sensor>()
            {
                new Sensor()
                {
                    Id              = Id,
                    Name            = "termometur",
                    IsBoolType      = false,
                    URL             = "theGreatUrl",
                    PollingInterval = 20,
                    ValueTypeSensor = termometer,
                    IsPublic        = true,
                    IsShared        = false,
                    OwnerId         = "stringId",
                    Owner           = userMock.Object,
                    IsDeleted       = true,
                },

                new Sensor()
                {
                    Id              = 7,
                    Name            = "Door",
                    IsBoolType      = true,
                    URL             = "theGreatUrlPart2",
                    PollingInterval = 25,
                    BoolTypeSensor  = door,
                    IsPublic        = false,
                    IsShared        = false,
                    OwnerId         = "stringId",
                    Owner           = userMock.Object,
                    IsDeleted       = false
                },

                new Sensor()
                {
                    Id              = 9,
                    Name            = "Door",
                    IsBoolType      = true,
                    URL             = "theGreatUrlPart2",
                    PollingInterval = 25,
                    BoolTypeSensor  = door,
                    IsPublic        = false,
                    IsShared        = false,
                    OwnerId         = "stringId",
                    Owner           = secondUserMock.Object,
                    IsDeleted       = false
                },
            };

            var sensorsSetMock = new Mock <DbSet <Sensor> >().SetupData(sensors);

            dbContextMock.SetupGet(m => m.Sensors).Returns(sensorsSetMock.Object);

            var sensorServices = new Dataflow.DataServices.SensorService(dbContextMock.Object, httpClientMock.Object, emailServiceMock.Object);

            //Act
            List <SensorDataModel> result = sensorServices.GetAllSensorsForUser(username).ToList();

            //Assert
            Assert.AreEqual(1, result.Count());
            Assert.AreEqual(sensors[1].Id, result[0].Id);
        }
Example #9
0
        public void SaveChangedValues_BoolType_WhenParametersAreCorrect()
        {
            //Arrange
            var dbContextMock    = new Mock <ApplicationDbContext>();
            var httpClientMock   = new Mock <IHttpClientProvider>();
            var emailServiceMock = new Mock <IEmailService>();
            var sensorMock       = new Mock <ISensorDataModel>();

            int    sensorId           = 1;
            string changedName        = "Occupancy";
            string changedUrl         = "TheGreatURLPart4";
            int    changedPolling     = 40;
            bool   changedIsPublic    = false;
            string changedMeasureType = "IsAnyoneIn";
            bool   changedIsBool      = true;
            bool   changedIsDeleted   = true;

            sensorMock.Setup(x => x.Id).Returns(sensorId);
            sensorMock.Setup(x => x.Name).Returns(changedName);
            sensorMock.Setup(x => x.URL).Returns(changedUrl);
            sensorMock.Setup(x => x.PollingInterval).Returns(changedPolling);
            sensorMock.Setup(x => x.IsPublic).Returns(changedIsPublic);
            sensorMock.Setup(x => x.MeasurementType).Returns(changedMeasureType);
            sensorMock.Setup(x => x.IsBoolType).Returns(changedIsBool);
            sensorMock.Setup(x => x.IsDeleted).Returns(changedIsDeleted);

            var door = new BoolTypeSensor()
            {
                CurrentValue    = true,
                MeasurementType = "Open/Close"
            };

            List <Sensor> sensors = new List <Sensor>()
            {
                new Sensor()
                {
                    Id              = sensorId,
                    Name            = "vrata",
                    URL             = "theGreatUrl",
                    PollingInterval = 20,
                    IsPublic        = true,
                    OwnerId         = "stringId",
                    BoolTypeSensor  = door,
                    IsBoolType      = true,
                    IsDeleted       = false
                }
            };

            var sensorsSetMock = new Mock <DbSet <Sensor> >().SetupData(sensors);

            dbContextMock.SetupGet(m => m.Sensors).Returns(sensorsSetMock.Object);

            var sensorServices = new Dataflow.DataServices.SensorService(dbContextMock.Object, httpClientMock.Object, emailServiceMock.Object);

            //Act
            sensorServices.EditSensor(sensorMock.Object);

            //Assert
            Assert.AreEqual(changedName, sensors[0].Name);
            Assert.AreEqual(changedUrl, sensors[0].URL);
            Assert.AreEqual(changedPolling, sensors[0].PollingInterval);
            Assert.AreEqual(changedIsPublic, sensors[0].IsPublic);
            Assert.AreEqual(changedMeasureType, sensors[0].BoolTypeSensor.MeasurementType);
            Assert.AreEqual(changedIsBool, sensors[0].IsBoolType);
            Assert.AreEqual(changedIsDeleted, sensors[0].IsDeleted);
        }