public async Task Deleting_tenant_should_work()
        {
            var(resultCreated, tenantCreated) = await TenantManagementApi.AddTenant(UniqueTestTenant).UnpackSingleOrDefault();

            var resultDeleted = await TenantManagementApi.DeleteTenant(tenantCreated.TenantId);

            Assert.True(resultCreated.Success);
            Assert.True(resultDeleted.Success);
        }
        public async Task Created_dto_should_match_original_dto()
        {
            var tenant = UniqueTestTenant;

            var(resultCreated, tenantCreated) = await TenantManagementApi.AddTenant(tenant).UnpackSingleOrDefault();

            Assert.True(resultCreated.Success);
            Assert.Equal(tenant.Name, tenantCreated.Name);
            Assert.Equal(tenant.Hostnames, tenantCreated.Hostnames);
        }
        public async Task Created_dto_should_match_fetched_dto()
        {
            var(resultCreated, tenantCreated) = await TenantManagementApi.AddTenant(UniqueTestTenant).UnpackSingleOrDefault();

            var(resultFetched, tenantFetched) = await TenantManagementApi.GetTenant(tenantCreated.TenantId).UnpackSingleOrDefault();

            Assert.True(resultCreated.Success);
            Assert.True(resultFetched.Success);
            Assert.Equal(tenantCreated, tenantFetched);
        }
        public async Task Service_should_find_tenant_by_hostname()
        {
            var(resultCreated, tenantCreated) = await TenantManagementApi.AddTenant(UniqueTestTenant).UnpackSingleOrDefault();

            var(resultFetched, tenantFetched) = await TenantManagementApi.Search(new TenantSearch(hostname : tenantCreated.Hostnames.First())).UnpackSingleOrDefault();

            Assert.True(resultCreated.Success);
            Assert.True(resultFetched.Success);
            Assert.Equal(tenantCreated.TenantId, tenantFetched.TenantId);
        }
        public async Task Updated_dto_should_match_original_dto()
        {
            var(resultCreated, tenantCreated) = await TenantManagementApi.AddTenant(UniqueTestTenant).UnpackSingleOrDefault();

            tenantCreated.Name += " UPDATED";
            tenantCreated.Hostnames.Add($"new-hostname-for-tenant-{Guid.NewGuid()}");

            var(resultUpdated, tenantUpdated) = await TenantManagementApi.UpdateTenant(tenantCreated).UnpackSingleOrDefault();

            Assert.True(resultCreated.Success);
            Assert.True(resultUpdated.Success);
            Assert.Equal(tenantCreated, tenantUpdated);
        }
        public async Task Creating_tenant_with_already_assigned_host_should_fail_gracefully()
        {
            var tenantDto1 = UniqueTestTenant;
            var tenantDto2 = UniqueTestTenant;

            tenantDto2.Hostnames = tenantDto1.Hostnames;

            var resultCreated1 = await TenantManagementApi.AddTenant(tenantDto1);

            var resultCreated2 = await TenantManagementApi.AddTenant(tenantDto2);

            Assert.True(resultCreated1.Success);
            Assert.False(resultCreated2.Success);
        }
        public async Task Service_should_return_all_created_tenants()
        {
            var(_, tenantCreated1) = await TenantManagementApi.AddTenant(UniqueTestTenant).UnpackSingleOrDefault();

            var(_, tenantCreated2) = await TenantManagementApi.AddTenant(UniqueTestTenant).UnpackSingleOrDefault();

            var tenants = await TenantManagementApi.GetTenants();

            var count = tenants
                        .Data
                        .Where(x => x.TenantId == tenantCreated1.TenantId || x.TenantId == tenantCreated2.TenantId)
                        .Count();

            Assert.Equal(2, count);
        }
        public async Task Adding_already_assigned_hostname_should_fail_gracefully()
        {
            var(createdResult1, tenant1) = await TenantManagementApi.AddTenant(UniqueTestTenant).UnpackSingleOrDefault();

            var(createdResult2, tenant2) = await TenantManagementApi.AddTenant(UniqueTestTenant).UnpackSingleOrDefault();

            foreach (var host in tenant2.Hostnames)
            {
                tenant1.Hostnames.Add(host);
            }

            var tenantUpdatedResult = await TenantManagementApi.UpdateTenant(tenant1);

            Assert.True(createdResult1.Success);
            Assert.True(createdResult2.Success);
            Assert.False(tenantUpdatedResult.Success);
        }
        private async Task LoadTenantsAndRoles()
        {
            var rolesResult = await RoleManagementApi.GetRoles();

            var tenantsResult = await TenantManagementApi.GetTenants();

            var roles   = rolesResult.Data;
            var tenants = tenantsResult.Data;

            if (!roles.Any(x => x.Name == Role1.Name))
            {
                var result = await RoleManagementApi.AddRole(Role1);

                Role1 = result.Data.Single();
            }

            if (!roles.Any(x => x.Name == Role2.Name))
            {
                var result = await RoleManagementApi.AddRole(Role2);

                Role2 = result.Data.Single();
            }

            if (!tenants.Any(x => x.Name == Tenant1.Name))
            {
                var result = await TenantManagementApi.AddTenant(Tenant1);

                Tenant1 = result.Data.Single();
            }

            if (!tenants.Any(x => x.Name == Tenant2.Name))
            {
                var result = await TenantManagementApi.AddTenant(Tenant2);

                Tenant2 = result.Data.Single();
            }
        }
        public async Task Creating_tenant_with_invalid_parameters_should_fail_gracefully()
        {
            var resultCreated = await TenantManagementApi.AddTenant(new TenantDto());

            Assert.False(resultCreated.Success);
        }