Beispiel #1
0
        public async Task GetItems_ShouldReturnAllItems_IfTheyExist()
        {
            using (ApplicationDbContext context = ApplicationDbContextHelper.GetContext())
            {
                // Arrange
                IMapper mapper      = GetAutoMapperMapper();
                var     itemQueries = new ItemQueries(context, mapper);
                var     item1       = new Item {
                    Id = 1, Name = "Gun1"
                };
                var item2 = new Item {
                    Id = 2, Name = "Gun2"
                };
                var item3 = new Item {
                    Id = 3, Name = "Gun3"
                };
                context.Items.AddRange(item1, item2, item3);
                context.SaveChanges();

                // Act
                IEnumerable <ItemDto> items = await itemQueries.GetItems();

                // Assert
                Assert.NotEmpty(items);
                Assert.Equal(3, items.Count());
                Assert.Equal(new List <int> {
                    1, 2, 3
                }, items.Select(i => i.Id).ToList());
                Assert.Equal(new List <string> {
                    "Gun1", "Gun2", "Gun3"
                }, items.Select(i => i.Name).ToList());
            }
        }
Beispiel #2
0
        public async Task AddItemForUser_ShouldDoNothing_IfValidArgumentsAndMaximumNumberOfItemsNotReached()
        {
            using (ApplicationDbContext context = ApplicationDbContextHelper.GetContext())
            {
                // Arrange
                IMapper mapper          = GetAutoMapperMapper();
                var     userItemQueries = new UserItemQueries(context, mapper);
                int     itemId          = 1;
                int     userId          = 1;
                context.Add(new User {
                    Id = userId, Name = "Rick Sanchez"
                });
                context.Add(new Item {
                    Id = itemId, Name = "Weapon"
                });
                for (int i = 0; i < UserItemDto.MaximumNumberOfAnyItemAllowed - 1; i++)
                {
                    context.Add(new UserItem {
                        ItemId = itemId, UserId = userId
                    });
                }
                context.SaveChanges();

                // Act
                await userItemQueries.AddItemForUser(userId, new ItemDto { Id = itemId });

                // Assert
                Assert.Equal(UserItemDto.MaximumNumberOfAnyItemAllowed, context.UserItems.Count());
            }
        }
Beispiel #3
0
 public ToggleMqttEntityStatusJobTest()
 {
     _initial   = SystemClock.Instance.GetCurrentInstant();
     _clock     = new FakeClock(_initial);
     _dbContext = ApplicationDbContextHelper.BuildTestDbContext(_clock);
     _job       = new ToggleMqttEntityStatusJob(_clock, _dbContext);
 }
Beispiel #4
0
        public async Task RemoveItemForUser_ShouldRemoveItem_IfUserItemCanBeFoundByItemName()
        {
            using (ApplicationDbContext context = ApplicationDbContextHelper.GetContext())
            {
                // Arrange
                IMapper mapper          = GetAutoMapperMapper();
                var     userItemQueries = new UserItemQueries(context, mapper);
                int     userId          = 1;
                int     itemId          = 1;
                string  itemName        = "Shortsword";
                context.Add(new Item {
                    Id = itemId, Name = itemName
                });
                context.Add(new UserItem {
                    ItemId = itemId, UserId = userId
                });
                context.SaveChanges();

                // Act
                await userItemQueries.RemoveItemForUser(userId, new ItemDto { Name = itemName });

                // Assert
                Assert.Empty(context.UserItems);
            }
        }
Beispiel #5
0
 public DeviceServiceTest()
 {
     _dbContext         = ApplicationDbContextHelper.BuildTestDbContext();
     _initial           = SystemClock.Instance.GetCurrentInstant();
     _identifierService = new MqttIdentifierService(new FakeClock(_initial));
     _service           = new DeviceService(_dbContext, _identifierService);
 }
Beispiel #6
0
 public UpdateUserLastSeenAtTimestampJobTest()
 {
     _initial   = SystemClock.Instance.GetCurrentInstant();
     _clock     = new FakeClock(_initial);
     _dbContext = ApplicationDbContextHelper.BuildTestDbContext(_clock);
     _job       = new UpdateUserLastSeenAtTimestampJob(_dbContext);
 }
Beispiel #7
0
        public async Task DoesUserHaveItem_ShouldReturnTrue_IfCanBeMatchedOnUserAndItemName()
        {
            using (ApplicationDbContext context = ApplicationDbContextHelper.GetContext())
            {
                // Arrange
                IMapper mapper   = GetAutoMapperMapper();
                int     userId   = 1;
                int     itemId   = 1;
                string  itemName = "Longsword";
                context.Add(new Item {
                    Id = itemId, Name = itemName
                });
                context.Add(new UserItem {
                    ItemId = itemId, UserId = userId
                });
                context.SaveChanges();
                var userItemQueries = new UserItemQueries(context, mapper);

                // Act
                bool matchFound = await userItemQueries
                                  .DoesUserHaveItem(userId, new ItemDto { Name = itemName });

                // Assert
                Assert.True(matchFound);
            }
        }
Beispiel #8
0
        public async Task GetUsers_ShouldReturnAllUsers_IfTheyExist()
        {
            using (ApplicationDbContext context = ApplicationDbContextHelper.GetContext())
            {
                // Arrange
                IMapper mapper      = GetAutoMapperMapper();
                var     userQueries = new UserQueries(context, mapper);
                var     user1       = new User {
                    Id = 1, Name = "Person1"
                };
                var user2 = new User {
                    Id = 2, Name = "Person2"
                };
                var user3 = new User {
                    Id = 3, Name = "Person3"
                };
                context.Users.AddRange(user1, user2, user3);
                context.SaveChanges();

                // Act
                IEnumerable <UserDto> users = await userQueries.GetUsers();

                // Assert
                Assert.NotEmpty(users);
                Assert.Equal(3, users.Count());
                Assert.Equal(new List <int> {
                    1, 2, 3
                }, users.Select(i => i.Id).ToList());
                Assert.Equal(new List <string> {
                    "Person1", "Person2", "Person3"
                }, users.Select(i => i.Name).ToList());
            }
        }
        private async Task <(Sensor, Sensor)> SeedDefaultCaseAsync()
        {
            var roleManager = IdentityHelpers.BuildRoleManager(ApplicationDbContextHelper.BuildTestDbContext());
            await roleManager.CreateAsync(new IdentityRole <long>("Admin"));

            foreach (var userName in new[] { "normal-user-1", "normal-user-2", "normal-user-3", "admin-user" })
            {
                await _userManager.CreateAsync(new User
                {
                    UserName           = userName,
                    NormalizedUserName = userName.ToUpperInvariant(),
                    SecurityStamp      = Guid.NewGuid().ToString()
                }, "normal-password");
            }
            var admin = await _userManager.FindByNameAsync("admin-user");

            await _userManager.AddToRoleAsync(admin, "Admin");

            var deviceName = "device";
            var device     = new Device {
                Name               = deviceName,
                NormalizedName     = deviceName.ToUpperInvariant(),
                AuthenticationType = DeviceAuthenticationType.PreSharedKey,
                PreSharedKey       = "key"
            };

            _dbContext.Devices.Add(device);

            var name1   = "sensor-1";
            var sensor1 = new Sensor {
                Name = name1, NormalizedName = name1.ToUpperInvariant(), Device = device
            };

            _dbContext.Sensors.Add(sensor1);
            var name2   = "sensor-2";
            var sensor2 = new Sensor {
                Name = name2, NormalizedName = name2.ToUpperInvariant(), Device = device
            };

            _dbContext.Sensors.Add(sensor2);

            var user1 = await _userManager.FindByNameAsync("normal-user-1");

            var user2 = await _userManager.FindByNameAsync("normal-user-2");

            _dbContext.UserSensors.Add(new UserSensor {
                UserId = user1.Id, Sensor = sensor1
            });
            _dbContext.UserSensors.Add(new UserSensor {
                UserId = user1.Id, Sensor = sensor2
            });
            _dbContext.UserSensors.Add(new UserSensor {
                UserId = user2.Id, Sensor = sensor1
            });

            await _dbContext.SaveChangesAsync();

            return(sensor1, sensor2);
        }
Beispiel #10
0
 public UserManagementServiceTest()
 {
     _clock       = new FakeClock(SystemClock.Instance.GetCurrentInstant());
     _dbContext   = ApplicationDbContextHelper.BuildTestDbContext(_clock);
     _userManager = IdentityHelpers.BuildUserManager(ApplicationDbContextHelper.BuildTestDbContext(_clock));
     _roleManager = IdentityHelpers.BuildRoleManager(ApplicationDbContextHelper.BuildTestDbContext(_clock));
     _service     = new UserManagementService(_dbContext, _userManager, _roleManager, new NullLogger <UserManagementService>(), _clock);
 }
Beispiel #11
0
 public DashboardServiceTest()
 {
     _initial     = SystemClock.Instance.GetCurrentInstant();
     _dbContext   = ApplicationDbContextHelper.BuildTestDbContext();
     _userManager = IdentityHelpers.BuildUserManager(ApplicationDbContextHelper.BuildTestDbContext());
     _roleManager = IdentityHelpers.BuildRoleManager(ApplicationDbContextHelper.BuildTestDbContext());
     _service     = new DashboardService(_dbContext, _userManager);
 }
Beispiel #12
0
        public RoomTypeServiceTest()
        {
            var mockLogger = new Mock <ILogger <RoomTypeService> >();
            var dbContext  = new ApplicationDbContextHelper().DbContext();
            var unitOfWork = new ApplicationUnitOfWork(dbContext);

            _roomTypeService = new RoomTypeService(unitOfWork, mockLogger.Object);
        }
        public MqttEntityIdentifierSuggestionServiceTest()
        {
            var mockedAccessor = new Mock <IIdentifierDictionaryFileAccessor>();

            mockedAccessor.Setup(x => x.ListIdentifiers(It.IsAny <string>())).Returns(new List <string> {
                "id1", "id2", "id3"
            });
            _identifierService = new MqttIdentifierService(new FakeClock(Instant.FromUtc(2020, 1, 1, 0, 0)));

            _dbContext = ApplicationDbContextHelper.BuildTestDbContext();
            _service   = new MqttEntityIdentifierSuggestionService(_identifierService, mockedAccessor.Object, _dbContext);
        }
        public MosquittoBrokerMessageServiceTest()
        {
            _initial   = SystemClock.Instance.GetCurrentInstant();
            _clock     = new FakeClock(_initial);
            _dbContext = ApplicationDbContextHelper.BuildTestDbContext(_clock);
            var keyService = new SecureKeySuggestionService();

            _clientAuthService = new MosquittoClientAuthenticationService(keyService);
            var mockPlugin        = new Mock <MosquittoBrokerPluginPidService>();
            var identifierSerivce = new MqttIdentifierService(_clock);

            _deviceService = new DeviceService(_dbContext, identifierSerivce);
            _service       = new MosquittoBrokerMessageService(_clientAuthService, mockPlugin.Object, _deviceService);
        }
Beispiel #15
0
        public async Task GetItem_ShouldReturnNull_IfItemCannotBeMatchedToIncomingData()
        {
            using (ApplicationDbContext context = ApplicationDbContextHelper.GetContext())
            {
                // Arrange
                IMapper mapper      = GetAutoMapperMapper();
                var     itemQueries = new ItemQueries(context, mapper);

                // Act
                ItemDto item = await itemQueries.GetItem(new ItemDto());

                // Assert
                Assert.Null(item);
            }
        }
Beispiel #16
0
        public async Task GetAllItemsForUser_ShouldReturnEmptyList_IfUserCannotBeMatchedOrDataDoesntExist()
        {
            using (ApplicationDbContext context = ApplicationDbContextHelper.GetContext())
            {
                // Arrange
                IMapper mapper          = GetAutoMapperMapper();
                var     userItemQueries = new UserItemQueries(context, mapper);

                // Act
                IEnumerable <UserItemDto> userItems = await userItemQueries.GetAllItemsForUser(1);

                // Assert
                Assert.Empty(userItems);
            }
        }
Beispiel #17
0
        public async Task GetMatchingItemsForUser_ShouldReturnNull_IfUserCannotBeMatchedOrDataDoesntExist()
        {
            using (ApplicationDbContext context = ApplicationDbContextHelper.GetContext())
            {
                // Arrange
                IMapper mapper          = GetAutoMapperMapper();
                var     userItemQueries = new UserItemQueries(context, mapper);

                // Act
                UserItemDto userItem = await userItemQueries
                                       .GetMatchingItemsForUser(1, new ItemDto { Id = 1, Name = "Weapon" });

                // Assert
                Assert.Null(userItem);
            }
        }
Beispiel #18
0
        public async Task DoesUserHaveItem_ShouldReturnFalse_IfNoUserItemCanBeMatched()
        {
            using (ApplicationDbContext context = ApplicationDbContextHelper.GetContext())
            {
                // Arrange
                IMapper mapper          = GetAutoMapperMapper();
                var     userItemQueries = new UserItemQueries(context, mapper);

                // Act
                bool matchFound = await userItemQueries
                                  .DoesUserHaveItem(1, new ItemDto { Id = 1, Name = "ExampleItem" });

                // Assert
                Assert.False(matchFound);
            }
        }
Beispiel #19
0
        public async Task GetItem_ShouldReturnNull_IfItemWithIdCannotBeFound()
        {
            using (ApplicationDbContext context = ApplicationDbContextHelper.GetContext())
            {
                // Arrange
                IMapper mapper      = GetAutoMapperMapper();
                int     itemId      = 1;
                var     itemQueries = new ItemQueries(context, mapper);

                // Act
                ItemDto item = await itemQueries.GetItem(itemId);

                // Assert
                Assert.Null(item);
            }
        }
Beispiel #20
0
        public async Task GetUser_ShouldReturnNull_IfUserWithIdCannotBeFound()
        {
            using (ApplicationDbContext context = ApplicationDbContextHelper.GetContext())
            {
                // Arrange
                IMapper mapper      = GetAutoMapperMapper();
                int     userId      = 1;
                var     userQueries = new UserQueries(context, mapper);

                // Act
                UserDto user = await userQueries.GetUser(userId);

                // Assert
                Assert.Null(user);
            }
        }
Beispiel #21
0
        public async Task GetMatchingItemsForUser_ShouldReturnUserItem_IfUserAndItemIdCanBeMatched()
        {
            using (ApplicationDbContext context = ApplicationDbContextHelper.GetContext())
            {
                // Arrange
                int    userId   = 1;
                string userName = "******";
                context.Add(new User {
                    Id = userId, Name = userName
                });

                int    numberOfSwords = 46;
                int    swordId        = 1;
                string swordName      = "Longsword";
                context.Add(new Item {
                    Id = swordId, Name = swordName
                });

                for (int i = 0; i < numberOfSwords; i++)
                {
                    context.Add(new UserItem {
                        ItemId = swordId, UserId = userId
                    });
                }

                context.SaveChanges();

                IMapper mapper          = GetAutoMapperMapper();
                var     userItemQueries = new UserItemQueries(context, mapper);

                // Act
                UserItemDto userItem = await userItemQueries
                                       .GetMatchingItemsForUser(userId, new ItemDto { Id = swordId });

                // Assert
                Assert.NotNull(userItem);
                Assert.Equal(userId, userItem.UserId);
                Assert.Equal(userName, userItem.UserName);
                Assert.Equal(numberOfSwords, userItem.ItemCount);
                Assert.Equal(swordId, userItem.ItemId);
                Assert.Equal(swordName, userItem.ItemName);
            }
        }
Beispiel #22
0
        public async Task AddItemForUser_ShouldDoNothing_IfUserCannotBeMatched()
        {
            using (ApplicationDbContext context = ApplicationDbContextHelper.GetContext())
            {
                // Arrange
                IMapper mapper          = GetAutoMapperMapper();
                var     userItemQueries = new UserItemQueries(context, mapper);
                int     itemId          = 1;
                context.Add(new Item {
                    Id = itemId, Name = "Weapon"
                });
                context.SaveChanges();

                // Act
                await userItemQueries.AddItemForUser(1, new ItemDto { Id = itemId });

                // Assert
                Assert.Empty(context.UserItems);
            }
        }
Beispiel #23
0
        public async Task RemoveItemForUser_ShouldDoNothing_IfUserItemCannotBeFound()
        {
            using (ApplicationDbContext context = ApplicationDbContextHelper.GetContext())
            {
                // Arrange
                IMapper mapper          = GetAutoMapperMapper();
                var     userItemQueries = new UserItemQueries(context, mapper);
                int     itemId          = 1;
                context.Add(new UserItem {
                    Id = itemId, UserId = 2
                });
                context.SaveChanges();

                // Act
                await userItemQueries.RemoveItemForUser(1, new ItemDto { Id = itemId });

                // Assert
                Assert.Single(context.UserItems);
            }
        }
Beispiel #24
0
        public MqttMessageIngestionServiceTest()
        {
            _dbContext = ApplicationDbContextHelper.BuildTestDbContext();
            _initial   = SystemClock.Instance.GetCurrentInstant();
            _clock     = new FakeClock(_initial);

            var mock = new Mock <IMosquittoBrokerPidAccessor>();

            mock.SetupGet(a => a.BrokerPid).Returns(10000);
            var mockPlugin = new Mock <IMosquittoBrokerPluginPidService>();

            mockPlugin.SetupGet(a => a.BrokerPidFromAuthPlugin).Returns(10000);

            _brokerService = new MosquittoBrokerService(_clock, new NullLogger <MosquittoBrokerService>(), mock.Object, mockPlugin.Object);

            _mqttIdentifierService = new MqttIdentifierService(_clock);
            _deviceService         = new DeviceService(_dbContext, _mqttIdentifierService);
            _valueService          = new SensorValueService(_dbContext);

            _service = new MqttMessageIngestionService(_deviceService, _mqttIdentifierService, _brokerService, _valueService, new NullLogger <MqttMessageIngestionService>());
        }
Beispiel #25
0
        public async Task GetItem_ShouldReturnItem_IfItemCanBeMatchedByIncomingId()
        {
            using (ApplicationDbContext context = ApplicationDbContextHelper.GetContext())
            {
                // Arrange
                IMapper mapper      = GetAutoMapperMapper();
                var     itemQueries = new ItemQueries(context, mapper);
                int     itemId      = 1;
                string  itemName    = "Weapon";
                context.Items.Add(new Item {
                    Id = itemId, Name = itemName
                });
                context.SaveChanges();

                // Act
                ItemDto item = await itemQueries.GetItem(new ItemDto { Id = itemId });

                // Assert
                Assert.NotNull(item);
                Assert.Equal(itemName, item.Name);
                Assert.Equal(itemId, item.Id);
            }
        }
Beispiel #26
0
        public async Task GetUser_ShouldReturnUser_IfUserWithIdCanBeFound()
        {
            using (ApplicationDbContext context = ApplicationDbContextHelper.GetContext())
            {
                // Arrange
                IMapper mapper      = GetAutoMapperMapper();
                var     userQueries = new UserQueries(context, mapper);
                int     userId      = 1;
                string  userName    = "******";
                context.Users.Add(new User {
                    Id = userId, Name = userName
                });
                context.SaveChanges();

                // Act
                UserDto user = await userQueries.GetUser(userId);

                // Assert
                Assert.NotNull(user);
                Assert.Equal(userName, user.Name);
                Assert.Equal(userId, user.Id);
            }
        }
 public SensorAssignmentServiceTest()
 {
     _dbContext   = ApplicationDbContextHelper.BuildTestDbContext();
     _userManager = IdentityHelpers.BuildUserManager(ApplicationDbContextHelper.BuildTestDbContext());
     _service     = new SensorAssignmentService(_dbContext, _userManager);
 }
Beispiel #28
0
        public async Task GetAllItemsForUser_ShouldReturnListOfUserItemsWithCorrectCounts_IfDataCanBeMatched()
        {
            using (ApplicationDbContext context = ApplicationDbContextHelper.GetContext())
            {
                // Arrange
                int    userId   = 1;
                string userName = "******";
                context.Add(new User {
                    Id = userId, Name = userName
                });

                int    numberOfSwords = 46;
                int    swordId        = 1;
                string swordName      = "Longsword";
                context.Add(new Item {
                    Id = swordId, Name = swordName
                });

                int    numberOfBatteries = 3;
                int    batteryId         = 2;
                string batteryName       = "Large Battery";
                context.Add(new Item {
                    Id = batteryId, Name = batteryName
                });

                for (int i = 0; i < numberOfSwords; i++)
                {
                    context.Add(new UserItem {
                        ItemId = swordId, UserId = userId
                    });
                }
                for (int i = 0; i < numberOfBatteries; i++)
                {
                    context.Add(new UserItem {
                        ItemId = batteryId, UserId = userId
                    });
                }

                context.SaveChanges();

                IMapper mapper          = GetAutoMapperMapper();
                var     userItemQueries = new UserItemQueries(context, mapper);

                // Act
                IEnumerable <UserItemDto> actualUserItems = await userItemQueries.GetAllItemsForUser(userId);

                // Assert
                var expectedUserItems = new List <UserItemDto>
                {
                    new UserItemDto
                    {
                        UserId    = userId,
                        UserName  = userName,
                        ItemCount = numberOfSwords,
                        ItemId    = swordId,
                        ItemName  = swordName
                    },
                    new UserItemDto
                    {
                        UserId    = userId,
                        UserName  = userName,
                        ItemCount = numberOfBatteries,
                        ItemId    = batteryId,
                        ItemName  = batteryName
                    }
                };
                Assert.Equal
                (
                    JsonConvert.SerializeObject(expectedUserItems),
                    JsonConvert.SerializeObject(actualUserItems)
                );
            }
        }