Exemple #1
0
        public void CallsGetSpecificSensorDisplayDataAsyncInSensorDbService_WhenUserIsLoggedIn()
        {
            // Arrange
            // arrange controller
            Mock <ApplicationDbContext> mockApplicationDbContext = new Mock <ApplicationDbContext>();
            Mock <SensorDbService>      mockSensorDbService      = new Mock <SensorDbService>(mockApplicationDbContext.Object);
            Mock <HttpClient>           mockHttpClient           = new Mock <HttpClient>();
            Mock <SensorApiService>     mockSensorApiService     = new Mock <SensorApiService>(mockHttpClient.Object);

            string loggedUser = "******";

            int testSensorId = 7;

            SensorDisplayData expected = new SensorDisplayData
            {
                SensorId = testSensorId,
                UserName = loggedUser
            };

            mockSensorDbService.Setup(x => x.GetSpecificSensorDisplayDataAsync(testSensorId))
            .Returns(Task.Run(() => expected));

            SensorControllerMock sensorController = new SensorControllerMock(loggedUser, mockSensorDbService.Object, mockSensorApiService.Object);

            // act and assert
            sensorController.WithCallTo(c => c.ModifySelectedSensor(testSensorId))
            .ShouldRenderPartialView("SensorPartialViews/_ModifySelectedSensor")
            .WithModel <SensorDisplayData>(actual =>
            {
                Assert.IsNotNull(actual);
                Assert.IsTrue(actual.SensorId == expected.SensorId);
                Assert.IsTrue(actual.UserName == expected.UserName);
            });
        }
Exemple #2
0
        public async Task ThrowHttpException_WhenUserIsntLoggedIn()
        {
            // Arrange
            // arrange controller
            Mock <ApplicationDbContext> mockApplicationDbContext = new Mock <ApplicationDbContext>();
            Mock <SensorDbService>      mockSensorDbService      = new Mock <SensorDbService>(mockApplicationDbContext.Object);
            Mock <HttpClient>           mockHttpClient           = new Mock <HttpClient>();
            Mock <SensorApiService>     mockSensorApiService     = new Mock <SensorApiService>(mockHttpClient.Object);

            string loggedUser = "******";

            int testSensorId = 7;

            SensorDisplayData testModel = new SensorDisplayData
            {
                SensorId = testSensorId,
                UserName = "******"
            };

            mockSensorDbService.Setup(x => x.GetSpecificSensorDisplayDataAsync(testSensorId))
            .Returns(Task.Run(() => testModel));

            SensorControllerMock sensorController = new SensorControllerMock(loggedUser, mockSensorDbService.Object, mockSensorApiService.Object);

            // act and assert
            await Assert.ThrowsExceptionAsync <HttpException>(() => sensorController.ModifySelectedSensor(testSensorId));
        }
Exemple #3
0
        public void ThrowHttpException_WhenUserIsntLoggedIn()
        {
            // Arrange
            // arrange controller
            Mock <ApplicationDbContext> mockApplicationDbContext = new Mock <ApplicationDbContext>();
            Mock <SensorDbService>      mockSensorDbService      = new Mock <SensorDbService>(mockApplicationDbContext.Object);
            Mock <HttpClient>           mockHttpClient           = new Mock <HttpClient>();
            Mock <SensorApiService>     mockSensorApiService     = new Mock <SensorApiService>(mockHttpClient.Object);

            string loggedUser = "******";

            SensorControllerMock sensorController = new SensorControllerMock(loggedUser, mockSensorDbService.Object, mockSensorApiService.Object);

            // act and assert
            Assert.ThrowsException <HttpException>(() => sensorController.ModifySelectedSensor(new SensorDisplayData
            {
                UserName = "******"
            }));
        }
        public void ReturnDefaultViewWithCorrectViewModel()
        {
            // Arrange

            //arrange username
            string loggeduser = "******";

            // arrange controller
            Mock <ApplicationDbContext> mockApplicationDbContext = new Mock <ApplicationDbContext>();

            Mock <SensorDbService> mockSensorDbService = new Mock <SensorDbService>(mockApplicationDbContext.Object);

            Mock <HttpClient>       mockHttpClient       = new Mock <HttpClient>();
            Mock <SensorApiService> mockSensorApiService = new Mock <SensorApiService>(mockHttpClient.Object);

            // arrange availableSensorsFromAPI
            SensorDisplayData sensorDisplayData1 = new SensorDisplayData();
            SensorDisplayData sensorDisplayData2 = new SensorDisplayData();

            List <SensorDisplayData> expected = new List <SensorDisplayData>();

            expected.Add(sensorDisplayData1);
            expected.Add(sensorDisplayData2);

            // arrange mockSensorDbService
            mockSensorDbService.Setup(x => x.ListOwnSensors(loggeduser))
            .Returns(expected);

            // arrange actual controller
            SensorControllerMock sensorController = new SensorControllerMock(loggeduser, mockSensorDbService.Object, mockSensorApiService.Object);

            // act and assert
            sensorController.WithCallTo(c => c.ListPersonalSensors())
            .ShouldRenderDefaultView()
            .WithModel <IEnumerable <SensorDisplayData> >(actual =>
            {
                Assert.IsNotNull(actual);
                CollectionAssert.AreEquivalent(expected, actual.ToList());
            });
        }
Exemple #5
0
        public void CallsModifySpecificSesnorInSensorDbService_WhenUserIsLoggedIn()
        {
            // Arrange
            // arrange controller
            Mock <ApplicationDbContext> mockApplicationDbContext = new Mock <ApplicationDbContext>();
            Mock <SensorDbService>      mockSensorDbService      = new Mock <SensorDbService>(mockApplicationDbContext.Object);
            Mock <HttpClient>           mockHttpClient           = new Mock <HttpClient>();
            Mock <SensorApiService>     mockSensorApiService     = new Mock <SensorApiService>(mockHttpClient.Object);

            string loggedUser = "******";

            SensorDisplayData testSensor = new SensorDisplayData
            {
                UserName = loggedUser
            };

            SensorControllerMock sensorController = new SensorControllerMock(loggedUser, mockSensorDbService.Object, mockSensorApiService.Object);

            //act
            sensorController.ModifySelectedSensor(testSensor);

            // act and assert
            mockSensorDbService.Verify(x => x.ModifySpecificSesnor(testSensor), Times.Once);
        }
        public async Task NotRegisterSensor_WhenSensorIsBooleanAndNonZeroValuesAreSetToMinAndMaxValue()
        {
            // Arrange
            // arrange controller
            Mock <ApplicationDbContext> mockApplicationDbContext = new Mock <ApplicationDbContext>();

            Mock <SensorDbService> mockSensorDbService = new Mock <SensorDbService>(mockApplicationDbContext.Object);

            Mock <HttpClient>       mockHttpClient       = new Mock <HttpClient>();
            Mock <SensorApiService> mockSensorApiService = new Mock <SensorApiService>(mockHttpClient.Object);

            //arrange RegisterNewSensorViewModel
            DateTime testDateTime1 = DateTime.Now;
            Sensor   testSensor    = new Sensor
            {
                SensorId = 8,
                Name     = "dsadas",
                DescriptionGivenByTheUser = "******",
                MeasurementType           = "C",
                IsPublic       = true,
                OperatingRange = "This sensor will return true or false value", //reference
                MinValue       = 13.4,                                          //invalid testing value
                MaxValue       = 14.4,                                          //invalid testing value
                AddedOn        = testDateTime1,
                Measurements   = new HashSet <Measurement>(),
                SharedWith     = new HashSet <ApplicationUser>()
            };

            DateTime  testDateTime2 = DateTime.Now;
            LastValue testLastValue = new LastValue
            {
                SensorId        = 45,
                Sensor          = testSensor,
                SensorIdICB     = "SensorIdICB",
                PollingInterval = 10,
                Value           = "Value",
                LastUpdatedOn   = testDateTime2
            };

            testSensor.LastValue = testLastValue;

            RegisterNewSensorViewModel expected = new RegisterNewSensorViewModel
            {
                Sensor = testSensor
            };

            //koleto. Zaradi validaciite
            SensorType sensorType = new SensorType
            {
                Id             = 342,
                SensorIdICB    = "SensorIdICB",
                IsNumericValue = true,
                MeasureType    = "C",
                MinPollingIntervalInSeconds = 2,
                Tag = "Tag"
            };

            mockSensorDbService.Setup(x => x.GetSpecificSensorType("SensorIdICB"))
            .Returns(sensorType);

            IDictionary <string, SensorReadInData> mockSensorsFromAPI = new Dictionary <string, SensorReadInData>();

            mockSensorsFromAPI.Add("SensorIdICB", new SensorReadInData()
            {
                Tag = "Tag",
                MinPollingIntervalInSeconds = 0,
                MeasureType = "C",
                Description = "This sensor will return values between 6 and 18",
                SensorId    = "SensorIdICB"
            });

            mockSensorApiService.Setup(x => x.ListSensorsFromAPI())
            .Returns(Task.Run(() => mockSensorsFromAPI));

            //arrange username
            string loggeduser = "******";

            // arrange actual controller
            SensorControllerMock controller = new SensorControllerMock(loggeduser, mockSensorDbService.Object, mockSensorApiService.Object);

            //act
            await controller.RegisterNewSensor(expected);

            // assert
            mockSensorDbService.Verify(x => x.RegisterNewSensor(loggeduser, testSensor), Times.Never);
        }