Ejemplo n.º 1
0
        public async void CanCreateAndDeleteNetscanGroups()
        {
            var allNetscanGroups = await PortalClient.GetAllAsync <NetscanGroup>().ConfigureAwait(false);

            const string name        = "API Unit Test CanCreateAndDeleteNetscanGroups";
            const string description = "API Unit Test CanCreateAndDeleteNetscanGroups Description";

            var existingTestNetscanGroup = allNetscanGroups.SingleOrDefault(group => group.Name == name);

            if (existingTestNetscanGroup != null)
            {
                await PortalClient.DeleteAsync <NetscanGroup>(existingTestNetscanGroup.Id).ConfigureAwait(false);
            }
            Assert.DoesNotContain(await PortalClient.GetAllAsync <NetscanGroup>().ConfigureAwait(false), group => group.Name == name);
            // Definitely not there now

            // Create one
            var netscanGroupCreationDto = new NetscanGroupCreationDto
            {
                Name        = name,
                Description = description
            };
            var newNetscanGroup = await PortalClient.CreateAsync(netscanGroupCreationDto).ConfigureAwait(false);

            Assert.Contains(await PortalClient.GetAllAsync <NetscanGroup>().ConfigureAwait(false), group => group.Name == name);

            await PortalClient.DeleteAsync(newNetscanGroup).ConfigureAwait(false);

            Assert.DoesNotContain(await PortalClient.GetAllAsync <NetscanGroup>().ConfigureAwait(false), group => group.Name == name);
        }
        private static async Task <TGroup> CreateGroupAsync <TGroup, TItem>(
            PortalClient portalClient,
            TGroup parentGroup,
            Structure <TGroup, TItem> structure,
            List <Property> variables,
            List <Property> originalProperties)
            where TGroup : IdentifiedItem, IHasEndpoint, new()
            where TItem : IdentifiedItem, IHasEndpoint, new()
        {
            var name       = Substitute(structure.Name, variables);
            var properties = structure.ApplyProperties
                                ? originalProperties
                             .Select(p => new Property
            {
                Name  = p.Name,
                Value = Substitute(p.Value, variables)
            })
                             .ToList()
                                : null;
            var groupTypeName = typeof(TGroup).Name;
            CreationDto <TGroup> creationDto;

            switch (groupTypeName)
            {
            case nameof(CollectorGroup):
                creationDto = new CollectorGroupCreationDto
                {
                    Name = name
                } as CreationDto <TGroup>;
                break;

            case nameof(DashboardGroup):
                creationDto = new DashboardGroupCreationDto
                {
                    ParentId = parentGroup?.Id.ToString() ?? "1",
                    Name     = name
                } as CreationDto <TGroup>;
                break;

            case nameof(DeviceGroup):
                creationDto = new DeviceGroupCreationDto
                {
                    ParentId         = parentGroup?.Id.ToString() ?? "1",
                    Name             = name,
                    AppliesTo        = Substitute(structure.AppliesTo, variables),
                    CustomProperties = properties
                } as CreationDto <TGroup>;
                break;

            case nameof(NetscanGroup):
                creationDto = new NetscanGroupCreationDto
                {
                    Name = name
                } as CreationDto <TGroup>;
                break;

            case nameof(ReportGroup):
                creationDto = new ReportGroupCreationDto
                {
                    Name = name
                } as CreationDto <TGroup>;
                break;

            case nameof(WebsiteGroup):
                creationDto = new WebsiteGroupCreationDto
                {
                    ParentId   = parentGroup?.Id.ToString() ?? "1",
                    Name       = name,
                    Properties = properties
                } as CreationDto <TGroup>;
                break;

            default:
                throw new NotSupportedException($"Creating {groupTypeName}s not supported.");
            }
            return(await portalClient
                   .CreateAsync(creationDto)
                   .ConfigureAwait(false));
        }