/// <summary>
 /// Creates new group.
 /// </summary>
 /// <param name='operations'>
 /// Reference to the
 /// Microsoft.Azure.Management.ApiManagement.IGroupsOperations.
 /// </param>
 /// <param name='resourceGroupName'>
 /// Required. The name of the resource group.
 /// </param>
 /// <param name='serviceName'>
 /// Required. The name of the Api Management service.
 /// </param>
 /// <param name='gid'>
 /// Required. Identifier of the group.
 /// </param>
 /// <param name='parameters'>
 /// Required. Create parameters.
 /// </param>
 /// <returns>
 /// A standard service response including an HTTP status code and
 /// request ID.
 /// </returns>
 public static AzureOperationResponse Create(this IGroupsOperations operations, string resourceGroupName, string serviceName, string gid, GroupCreateParameters parameters)
 {
     return Task.Factory.StartNew((object s) => 
     {
         return ((IGroupsOperations)s).CreateAsync(resourceGroupName, serviceName, gid, parameters);
     }
     , operations, CancellationToken.None, TaskCreationOptions.None, TaskScheduler.Default).Unwrap().GetAwaiter().GetResult();
 }
 /// <summary>
 /// Creates new group.
 /// </summary>
 /// <param name='operations'>
 /// Reference to the
 /// Microsoft.Azure.Management.ApiManagement.IGroupsOperations.
 /// </param>
 /// <param name='resourceGroupName'>
 /// Required. The name of the resource group.
 /// </param>
 /// <param name='serviceName'>
 /// Required. The name of the Api Management service.
 /// </param>
 /// <param name='gid'>
 /// Required. Identifier of the group.
 /// </param>
 /// <param name='parameters'>
 /// Required. Create parameters.
 /// </param>
 /// <returns>
 /// A standard service response including an HTTP status code and
 /// request ID.
 /// </returns>
 public static Task<AzureOperationResponse> CreateAsync(this IGroupsOperations operations, string resourceGroupName, string serviceName, string gid, GroupCreateParameters parameters)
 {
     return operations.CreateAsync(resourceGroupName, serviceName, gid, parameters, CancellationToken.None);
 }
        public void GroupsCreateListUpdateDelete()
        {
            TestUtilities.StartTest("SmapiFunctionalTests", "GroupsCreateListUpdateDelete");

            try
            {
                // list all groups: there should be at least 3 groups: Administrators, Developers, Guests
                var listResponse = ApiManagementClient.Groups.List(ResourceGroupName, ApiManagementServiceName, null);

                Assert.NotNull(listResponse);
                Assert.NotNull(listResponse.Result);
                Assert.True(listResponse.Result.TotalCount >= 3);
                Assert.True(listResponse.Result.Values.Count >= 3);

                // list paged
                listResponse = ApiManagementClient.Groups.List(
                    ResourceGroupName,
                    ApiManagementServiceName,
                    new QueryParameters {Top = 1});

                Assert.NotNull(listResponse);
                Assert.NotNull(listResponse.Result);
                Assert.True(listResponse.Result.TotalCount >= 3);
                Assert.Equal(1, listResponse.Result.Values.Count);

                // create new group with default parameters
                string newGroupId = TestUtilities.GenerateName("newGroupId");
                string newGroupName = TestUtilities.GenerateName("newGroupName");
                var groupCreateParameters = new GroupCreateParameters(newGroupName);

                var createResponse = ApiManagementClient.Groups.Create(
                    ResourceGroupName,
                    ApiManagementServiceName,
                    newGroupId,
                    groupCreateParameters);

                Assert.NotNull(createResponse);
                Assert.Equal(HttpStatusCode.Created, createResponse.StatusCode);

                // get to check it was created
                var getResponse = ApiManagementClient.Groups.Get(ResourceGroupName, ApiManagementServiceName, newGroupId);

                Assert.NotNull(getResponse);
                Assert.NotNull(getResponse.Value);

                Assert.Equal(newGroupId, getResponse.Value.Id);
                Assert.Equal(newGroupName, getResponse.Value.Name);
                Assert.Null(getResponse.Value.Description);
                Assert.Null(getResponse.Value.ExternalId);
                Assert.Equal(GroupTypeContract.Custom, getResponse.Value.Type);
                Assert.Equal(false, getResponse.Value.System);

                // delete the group
                ApiManagementClient.Groups.Delete(ResourceGroupName, ApiManagementServiceName, newGroupId, getResponse.ETag);

                // create a gruop with other parameters
                string newGroupDescription = TestUtilities.GenerateName("newGroupDescription");
                var newGroupType = GroupTypeContract.Custom;

                groupCreateParameters = new GroupCreateParameters
                {
                    Description = newGroupDescription,
                    Name = newGroupName,
                    Type = newGroupType
                };

                createResponse = ApiManagementClient.Groups.Create(
                    ResourceGroupName,
                    ApiManagementServiceName,
                    newGroupId,
                    groupCreateParameters);

                Assert.NotNull(createResponse);
                Assert.Equal(HttpStatusCode.Created, createResponse.StatusCode);

                // get to check it was created
                getResponse = ApiManagementClient.Groups.Get(ResourceGroupName, ApiManagementServiceName, newGroupId);

                Assert.NotNull(getResponse);
                Assert.NotNull(getResponse.Value);

                Assert.Equal(newGroupId, getResponse.Value.Id);
                Assert.Equal(newGroupName, getResponse.Value.Name);
                Assert.Equal(newGroupDescription, getResponse.Value.Description);
                //Assert.Equal(newGroupExternalId, getResponse.Value.ExternalId);
                Assert.Equal(newGroupType, getResponse.Value.Type);
                Assert.Equal(false, getResponse.Value.System);

                // patch group
                string patchedDescription = TestUtilities.GenerateName("patchedDescription");
                var patchResponse = ApiManagementClient.Groups.Update(
                    ResourceGroupName,
                    ApiManagementServiceName,
                    newGroupId,
                    new GroupUpdateParameters
                    {
                        Description = patchedDescription
                    },
                    getResponse.ETag);

                Assert.NotNull(patchResponse);

                // get to check it was patched
                getResponse = ApiManagementClient.Groups.Get(ResourceGroupName, ApiManagementServiceName, newGroupId);

                Assert.NotNull(getResponse);
                Assert.NotNull(getResponse.Value);

                Assert.Equal(newGroupId, getResponse.Value.Id);
                Assert.Equal(newGroupName, getResponse.Value.Name);
                Assert.Equal(patchedDescription, getResponse.Value.Description);
                //Assert.Equal(newGroupExternalId, getResponse.Value.ExternalId);
                Assert.Equal(newGroupType, getResponse.Value.Type);
                Assert.Equal(false, getResponse.Value.System);

                // delete the group 
                var deleteResponse = ApiManagementClient.Groups.Delete(
                    ResourceGroupName,
                    ApiManagementServiceName,
                    newGroupId,
                    getResponse.ETag);

                Assert.NotNull(deleteResponse);
                Assert.Equal(HttpStatusCode.NoContent, deleteResponse.StatusCode);

                // get the deleted api to make sure it was deleted
                try
                {
                    ApiManagementClient.Groups.Get(ResourceGroupName, ApiManagementServiceName, newGroupId);
                    throw new Exception("This code should not have been executed.");
                }
                catch (CloudException ex)
                {
                    Assert.Equal(HttpStatusCode.NotFound, ex.Response.StatusCode);
                }
            }
            finally
            {
                TestUtilities.EndTest();
            }
        }
        public void GroupUsersListAddRemove()
        {
            TestUtilities.StartTest("SmapiFunctionalTests", "GroupUsersListAddRemove");

            try
            {
                // create new group
                string gid = TestUtilities.GenerateName("groupId");

                try
                {
                    var createGroupParams = new GroupCreateParameters
                    {
                        Name = TestUtilities.GenerateName("groupName"),
                        Type = GroupTypeContract.Custom,
                    };

                    ApiManagementClient.Groups.Create(
                        ResourceGroupName,
                        ApiManagementServiceName,
                        gid,
                        createGroupParams);

                    // get the group
                    var getGroupResponse = ApiManagementClient.Groups.Get(
                        ResourceGroupName,
                        ApiManagementServiceName,
                        gid);

                    var group = getGroupResponse.Value;

                    // list all group users
                    var listGroupUsersResponse = ApiManagementClient.GroupUsers.List(
                        ResourceGroupName,
                        ApiManagementServiceName,
                        group.Id,
                        null);

                    Assert.NotNull(listGroupUsersResponse);
                    Assert.NotNull(listGroupUsersResponse.Result);
                    Assert.NotNull(listGroupUsersResponse.Result.Values);

                    // there should be no users in the group
                    Assert.Equal(0, listGroupUsersResponse.Result.TotalCount);
                    Assert.Equal(0, listGroupUsersResponse.Result.Values.Count);

                    // create new user and add it to the group
                    string userId = TestUtilities.GenerateName("userId");
                    try
                    {
                        var creteUserParameters = new UserCreateParameters
                        {
                            FirstName = TestUtilities.GenerateName("Ivan"),
                            LastName = TestUtilities.GenerateName("Ivanov"),
                            Email = TestUtilities.GenerateName("ivan.ivanov") + "@contoso.com",
                            Password = TestUtilities.GenerateName("pwd"),
                            State = UserStateContract.Active,
                            Note = TestUtilities.GenerateName("note")
                        };

                        ApiManagementClient.Users.Create(
                            ResourceGroupName,
                            ApiManagementServiceName,
                            userId,
                            creteUserParameters);

                        // add the user to the group
                        var addResponse = ApiManagementClient.GroupUsers.Add(
                            ResourceGroupName,
                            ApiManagementServiceName,
                            group.Id,
                            userId);

                        Assert.NotNull(addResponse);

                        // list group users
                        listGroupUsersResponse = ApiManagementClient.GroupUsers.List(
                            ResourceGroupName,
                            ApiManagementServiceName,
                            group.Id,
                            null);

                        Assert.NotNull(listGroupUsersResponse);
                        Assert.NotNull(listGroupUsersResponse.Result);
                        Assert.NotNull(listGroupUsersResponse.Result.Values);

                        // there should be one user
                        Assert.Equal(1, listGroupUsersResponse.Result.TotalCount);
                        Assert.Equal(1, listGroupUsersResponse.Result.Values.Count);

                        var user = listGroupUsersResponse.Result.Values.Single();
                        Assert.Equal(userId, user.Id);
                        Assert.Equal(creteUserParameters.FirstName, user.FirstName);
                        Assert.Equal(creteUserParameters.LastName, user.LastName);
                        Assert.Equal(creteUserParameters.Email, user.Email);
                        Assert.Equal(creteUserParameters.Note, user.Note);
                        Assert.Equal(creteUserParameters.State, user.State);

                        // remove the user from the group
                        var removeReponse = ApiManagementClient.GroupUsers.Remove(
                            ResourceGroupName,
                            ApiManagementServiceName,
                            group.Id,
                            userId);

                        Assert.NotNull(removeReponse);

                        // list to make sure it was removed
                        listGroupUsersResponse = ApiManagementClient.GroupUsers.List(
                            ResourceGroupName,
                            ApiManagementServiceName,
                            group.Id,
                            null);

                        Assert.NotNull(listGroupUsersResponse);
                        Assert.NotNull(listGroupUsersResponse.Result);
                        Assert.NotNull(listGroupUsersResponse.Result.Values);

                        // there should be no users
                        Assert.Equal(0, listGroupUsersResponse.Result.TotalCount);
                        Assert.Equal(0, listGroupUsersResponse.Result.Values.Count);
                    }
                    finally
                    {
                        // delete the user
                        ApiManagementClient.Users.Delete(ResourceGroupName, ApiManagementServiceName, userId, "*", true);
                    }
                }
                finally
                {
                    // delete the group
                    ApiManagementClient.Groups.Delete(ResourceGroupName, ApiManagementServiceName, gid, "*");
                }
            }
            finally
            {
                TestUtilities.EndTest();
            }
        }
        public void UserGroupsListAddRemove()
        {
            TestUtilities.StartTest("SmapiFunctionalTests", "UserGroupsListAddRemove");

            try
            {
                // create new group
                string gid = TestUtilities.GenerateName("groupId");

                try
                {
                    var createGroupParams = new GroupCreateParameters
                    {
                        Name = TestUtilities.GenerateName("groupName"),
                        Type = GroupTypeContract.Custom,
                    };

                    ApiManagementClient.Groups.Create(
                        ResourceGroupName,
                        ApiManagementServiceName,
                        gid,
                        createGroupParams);

                    // get the group
                    var getGroupResponse = ApiManagementClient.Groups.Get(
                        ResourceGroupName,
                        ApiManagementServiceName,
                        gid);

                    var group = getGroupResponse.Value;

                    // create new user and add it to the group
                    string userId = TestUtilities.GenerateName("userId");
                    try
                    {
                        var creteUserParameters = new UserCreateParameters
                        {
                            FirstName = TestUtilities.GenerateName("Ivan"),
                            LastName = TestUtilities.GenerateName("Ivanov"),
                            Email = TestUtilities.GenerateName("ivan.ivanov") + "@contoso.com",
                            Password = TestUtilities.GenerateName("pwd"),
                            State = UserStateContract.Active,
                            Note = TestUtilities.GenerateName("note")
                        };

                        ApiManagementClient.Users.Create(
                            ResourceGroupName,
                            ApiManagementServiceName,
                            userId,
                            creteUserParameters);

                        // list user groups
                        var listUserGroupsResponse = ApiManagementClient.UserGroups.List(
                            ResourceGroupName,
                            ApiManagementServiceName,
                            userId,
                            null);

                        Assert.NotNull(listUserGroupsResponse);
                        Assert.NotNull(listUserGroupsResponse.Result);
                        Assert.NotNull(listUserGroupsResponse.Result.Values);

                        // there should be 'Developers' group by default
                        Assert.Equal(1, listUserGroupsResponse.Result.TotalCount);
                        Assert.Equal(1, listUserGroupsResponse.Result.Values.Count);

                        // add the user to the group
                        var addResponse = ApiManagementClient.UserGroups.AddToGroup(
                            ResourceGroupName,
                            ApiManagementServiceName,
                            userId,
                            group.Id);

                        Assert.NotNull(addResponse);

                        // list user groups
                        listUserGroupsResponse = ApiManagementClient.UserGroups.List(
                            ResourceGroupName,
                            ApiManagementServiceName,
                            userId,
                            null);

                        Assert.NotNull(listUserGroupsResponse);
                        Assert.NotNull(listUserGroupsResponse.Result);
                        Assert.NotNull(listUserGroupsResponse.Result.Values);

                        // there should be two groups now
                        Assert.Equal(2, listUserGroupsResponse.Result.TotalCount);
                        Assert.Equal(2, listUserGroupsResponse.Result.Values.Count);

                        var userGroup = listUserGroupsResponse.Result.Values.Single(g => g.Name == group.Name);
                        Assert.Equal(group.Id, userGroup.Id);
                        Assert.Equal(group.IdPath, userGroup.IdPath);
                        Assert.Equal(group.Name, userGroup.Name);
                        Assert.Equal(group.Description, userGroup.Description);
                        Assert.Equal(group.ExternalId, userGroup.ExternalId);
                        Assert.Equal(group.System, userGroup.System);
                        Assert.Equal(group.Type, userGroup.Type);

                        // remove the user from the group
                        var removeReponse = ApiManagementClient.UserGroups.RemoveFromGroup(
                            ResourceGroupName,
                            ApiManagementServiceName,
                            userId,
                            group.Id);

                        Assert.NotNull(removeReponse);

                        // list to make sure it was removed
                        listUserGroupsResponse = ApiManagementClient.UserGroups.List(
                            ResourceGroupName,
                            ApiManagementServiceName,
                            userId,
                            null);

                        Assert.NotNull(listUserGroupsResponse);
                        Assert.NotNull(listUserGroupsResponse.Result);
                        Assert.NotNull(listUserGroupsResponse.Result.Values);

                        // there should be default 'Developers' group
                        Assert.Equal(1, listUserGroupsResponse.Result.TotalCount);
                        Assert.Equal(1, listUserGroupsResponse.Result.Values.Count);
                        Assert.Equal("Developers", listUserGroupsResponse.Result.Values.Single().Name);
                    }
                    finally
                    {
                        // delete the user
                        ApiManagementClient.Users.Delete(ResourceGroupName, ApiManagementServiceName, userId, "*", true);
                    }
                }
                finally
                {
                    // delete the group
                    ApiManagementClient.Groups.Delete(ResourceGroupName, ApiManagementServiceName, gid, "*");
                }
            }
            finally
            {
                TestUtilities.EndTest();
            }
        }