public async Task <InvokeResult <DeviceGroupEntry> > AddDeviceToGroupAsync(DeviceRepository deviceRepo, String deviceGroupId, String deviceUniqueId, EntityHeader org, EntityHeader user)
        {
            var group = await GetDeviceGroupAsync(deviceRepo, deviceGroupId, org, user);

            await AuthorizeAsync(group, AuthorizeResult.AuthorizeActions.Update, user, org, "Add Device to Device Group");

            var device = await GetDeviceManagementRepo(deviceRepo).GetDeviceByIdAsync(deviceRepo, deviceUniqueId);

            await AuthorizeAsync(device, AuthorizeResult.AuthorizeActions.Update, user, org, "Add Devvice to Device Group");

            //TODO: Add localization
            if (group.Devices.Where(grp => grp.DeviceUniqueId == deviceUniqueId).Any())
            {
                return(InvokeResult <DeviceGroupEntry> .FromError($"The device [{device.DeviceId}] already belongs to this device group and can not be added again."));
            }

            var entry = DeviceGroupEntry.FromDevice(device, user);

            group.Devices.Add(entry);

            device.DeviceGroups.Add(new EntityHeader()
            {
                Id = group.Id, Text = group.Name
            });

            await GetDeviceManagementRepo(deviceRepo).UpdateDeviceAsync(deviceRepo, device);
            await GetDeviceGroupRepo(deviceRepo).UpdateDeviceGroupAsync(deviceRepo, group);

            return(InvokeResult <DeviceGroupEntry> .Create(entry));
        }
Example #2
0
        public async Task GetDeviceGroupAsync()
        {
            var groupId     = Guid.NewGuid().ToId();
            var createdBy   = EntityHeader.Create("userid", "username");
            var deviceGroup = new DeviceGroup
            {
                Id                = groupId,
                Name              = "name" + groupId,
                IsPublic          = false,
                Key               = "key" + groupId,
                CreatedBy         = createdBy,
                LastUpdatedBy     = EntityHeader.Create("userid", "username"),
                OwnerOrganization = EntityHeader.Create("ownerid", "ownername"),
                OwnerUser         = EntityHeader.Create("userid", "username"),
                DeviceRepository  = EntityHeader.Create(DataFactory.DeviceRepo.Id, DataFactory.DeviceRepo.Name),
                Devices           = new List <DeviceGroupEntry>()
                {
                    DeviceGroupEntry.FromDevice(DataFactory.CreateDevice(groupId), createdBy)
                }
            };
            await DataFactory.DeviceGroupRepo.AddDeviceGroupAsync(DataFactory.DeviceRepo, deviceGroup);

            var response = await DataFactory.DeviceGroupRepo.GetDeviceGroupAsync(DataFactory.DeviceRepo, groupId);

            Assert.AreEqual(groupId, response.Id);
        }
Example #3
0
        public async Task QueryKeyInUseAsync()
        {
            var groupId     = Guid.NewGuid().ToId();
            var createdBy   = EntityHeader.Create("userid", "username");
            var key         = Guid.NewGuid().ToId();
            var orgId       = Guid.NewGuid().ToId();
            var deviceGroup = new DeviceGroup
            {
                Id                = groupId,
                Name              = "name" + groupId,
                IsPublic          = false,
                Key               = key,
                CreatedBy         = createdBy,
                LastUpdatedBy     = EntityHeader.Create("userid", "username"),
                OwnerOrganization = EntityHeader.Create(orgId, "ownername"),
                OwnerUser         = EntityHeader.Create("userid", "username"),
                DeviceRepository  = EntityHeader.Create(DataFactory.DeviceRepo.Id, DataFactory.DeviceRepo.Name),
                Devices           = new List <DeviceGroupEntry>()
                {
                    DeviceGroupEntry.FromDevice(DataFactory.CreateDevice(groupId), createdBy)
                }
            };
            await DataFactory.DeviceGroupRepo.AddDeviceGroupAsync(DataFactory.DeviceRepo, deviceGroup);

            var response = await DataFactory.DeviceGroupRepo.QueryKeyInUseAsync(DataFactory.DeviceRepo, key, orgId);

            Assert.IsTrue(response);

            response = await DataFactory.DeviceGroupRepo.QueryKeyInUseAsync(DataFactory.DeviceRepo, "nokey", orgId);

            Assert.IsFalse(response);
        }
        public async Task DeviceGroups_Remove_ShoudRemoveFromGroup_Valid()
        {
            _deviceGroup.Devices.Add(DeviceGroupEntry.FromDevice(_device, _user));

            var result = await _groupManager.RemoveDeviceFromGroupAsync(_repo.Object, GROUPID, DEVICEID, _org, _user);

            Assert.AreEqual(0, _deviceGroup.Devices.Count);
        }
        public async Task DeviceGroups_Remove_ReturnsInvokeSuccess_Valid()
        {
            _deviceGroup.Devices.Add(DeviceGroupEntry.FromDevice(_device, _user));

            var result = await _groupManager.RemoveDeviceFromGroupAsync(_repo.Object, GROUPID, DEVICEID, _org, _user);

            AssertSuccessful(result.ToInvokeResult());
        }
        public async Task DeviceGroups_Add_ToGroup_AlreadyAdded_Failed()
        {
            _deviceGroup.Devices.Add(DeviceGroupEntry.FromDevice(_device, _user));

            var result = await _groupManager.AddDeviceToGroupAsync(_repo.Object, GROUPID, DEVICEID, _org, _user);

            AssertInvalidError(result.ToInvokeResult(), "The device [device001] already belongs to this device group and can not be added again.");

            Assert.AreEqual(1, _deviceGroup.Devices.Count);
        }
        public async Task DeviceGroups_Add_ToGroup_CreatesCorrectDevice_Invalid()
        {
            var item = DeviceGroupEntry.FromDevice(_device, _user);

            Assert.AreEqual(_user, item.AddedBy);
            Assert.AreEqual(_device.Name, item.Name);
            Assert.AreEqual(_device.Id, item.DeviceUniqueId);
            Assert.AreEqual(_device.DeviceId, item.DeviceId);
            Assert.AreEqual(_device.DeviceType, item.DeviceType);
            Assert.AreEqual(_device.DeviceConfiguration, item.DeviceConfiguration);

            Assert.IsTrue(TimeSpan.FromSeconds(5) > DateTime.UtcNow - item.DateAdded.ToDateTime());

            var result = await _groupManager.AddDeviceToGroupAsync(_repo.Object, GROUPID, DEVICEID, _org, _user);

            Assert.AreEqual(1, _deviceGroup.Devices.Count);
        }