public void GetAll_Called_ReturnsUserIdentityResponse()
        {
            // Given
            var response = new UserIdentityListResponse { Results = new List<UserIdentity> { new UserIdentity { Id = 1 } } };
            _client.Setup(b => b.Get<UserIdentityListResponse>(It.IsAny<Uri>())).Returns(response);
            var userIdentityResource = new UserIdentityResource(_client.Object);

            // When
            var result = userIdentityResource.GetAll(4321);

            // Then
            Assert.That(result, Is.EqualTo(response));
        }
        public async void GetAllAsync_Called_ReturnsUserIdentityResponse()
        {
            // Given
            var response = new UserIdentityListResponse {
                Results = new List <UserIdentity> {
                    new UserIdentity {
                        Id = 1
                    }
                }
            };

            _client.Setup(b => b.GetAsync <UserIdentityListResponse>(It.IsAny <Uri>())).Returns(TaskHelper.CreateTaskFromResult(response));
            var userIdentityResource = new UserIdentityResource(_client.Object);

            // When
            var result = await userIdentityResource.GetAllAsync(4321);

            // Then
            Assert.That(result, Is.EqualTo(response));
        }
        public void GetAll_Called_ReturnsUserIdentityResponse()
        {
            // Given
            var response = new UserIdentityListResponse {
                Results = new List <UserIdentity> {
                    new UserIdentity {
                        Id = 1
                    }
                }
            };

            _client.Setup(b => b.Get <UserIdentityListResponse>(It.IsAny <Uri>())).Returns(response);
            var userIdentityResource = new UserIdentityResource(_client.Object);

            // When
            var result = userIdentityResource.GetAll(4321);

            // Then
            Assert.That(result, Is.EqualTo(response));
        }
Exemplo n.º 4
0
        /// <summary>
        /// List all user identities.
        /// </summary>
        /// <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='uid'>
        /// Required. Identifier of the user.
        /// </param>
        /// <param name='cancellationToken'>
        /// Cancellation token.
        /// </param>
        /// <returns>
        /// List User Identities operation response details.
        /// </returns>
        public async Task <UserIdentityListResponse> ListAsync(string resourceGroupName, string serviceName, string uid, CancellationToken cancellationToken)
        {
            // Validate
            if (resourceGroupName == null)
            {
                throw new ArgumentNullException("resourceGroupName");
            }
            if (serviceName == null)
            {
                throw new ArgumentNullException("serviceName");
            }
            if (uid == null)
            {
                throw new ArgumentNullException("uid");
            }

            // Tracing
            bool   shouldTrace  = TracingAdapter.IsEnabled;
            string invocationId = null;

            if (shouldTrace)
            {
                invocationId = TracingAdapter.NextInvocationId.ToString();
                Dictionary <string, object> tracingParameters = new Dictionary <string, object>();
                tracingParameters.Add("resourceGroupName", resourceGroupName);
                tracingParameters.Add("serviceName", serviceName);
                tracingParameters.Add("uid", uid);
                TracingAdapter.Enter(invocationId, this, "ListAsync", tracingParameters);
            }

            // Construct URL
            string url = "";

            url = url + "/subscriptions/";
            if (this.Client.Credentials.SubscriptionId != null)
            {
                url = url + Uri.EscapeDataString(this.Client.Credentials.SubscriptionId);
            }
            url = url + "/resourceGroups/";
            url = url + Uri.EscapeDataString(resourceGroupName);
            url = url + "/providers/";
            url = url + "Microsoft.ApiManagement";
            url = url + "/service/";
            url = url + Uri.EscapeDataString(serviceName);
            url = url + "/users/";
            url = url + Uri.EscapeDataString(uid);
            url = url + "/identities";
            List <string> queryParameters = new List <string>();

            queryParameters.Add("api-version=2014-02-14");
            if (queryParameters.Count > 0)
            {
                url = url + "?" + string.Join("&", queryParameters);
            }
            string baseUrl = this.Client.BaseUri.AbsoluteUri;

            // Trim '/' character from the end of baseUrl and beginning of url.
            if (baseUrl[baseUrl.Length - 1] == '/')
            {
                baseUrl = baseUrl.Substring(0, baseUrl.Length - 1);
            }
            if (url[0] == '/')
            {
                url = url.Substring(1);
            }
            url = baseUrl + "/" + url;
            url = url.Replace(" ", "%20");

            // Create HTTP transport objects
            HttpRequestMessage httpRequest = null;

            try
            {
                httpRequest            = new HttpRequestMessage();
                httpRequest.Method     = HttpMethod.Get;
                httpRequest.RequestUri = new Uri(url);

                // Set Headers

                // Set Credentials
                cancellationToken.ThrowIfCancellationRequested();
                await this.Client.Credentials.ProcessHttpRequestAsync(httpRequest, cancellationToken).ConfigureAwait(false);

                // Send Request
                HttpResponseMessage httpResponse = null;
                try
                {
                    if (shouldTrace)
                    {
                        TracingAdapter.SendRequest(invocationId, httpRequest);
                    }
                    cancellationToken.ThrowIfCancellationRequested();
                    httpResponse = await this.Client.HttpClient.SendAsync(httpRequest, cancellationToken).ConfigureAwait(false);

                    if (shouldTrace)
                    {
                        TracingAdapter.ReceiveResponse(invocationId, httpResponse);
                    }
                    HttpStatusCode statusCode = httpResponse.StatusCode;
                    if (statusCode != HttpStatusCode.OK)
                    {
                        cancellationToken.ThrowIfCancellationRequested();
                        CloudException ex = CloudException.Create(httpRequest, null, httpResponse, await httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false));
                        if (shouldTrace)
                        {
                            TracingAdapter.Error(invocationId, ex);
                        }
                        throw ex;
                    }

                    // Create Result
                    UserIdentityListResponse result = null;
                    // Deserialize Response
                    if (statusCode == HttpStatusCode.OK)
                    {
                        cancellationToken.ThrowIfCancellationRequested();
                        string responseContent = await httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false);

                        result = new UserIdentityListResponse();
                        JToken responseDoc = null;
                        if (string.IsNullOrEmpty(responseContent) == false)
                        {
                            responseDoc = JToken.Parse(responseContent);
                        }

                        if (responseDoc != null && responseDoc.Type != JTokenType.Null)
                        {
                            JToken valueArray = responseDoc;
                            if (valueArray != null && valueArray.Type != JTokenType.Null)
                            {
                                foreach (JToken valueValue in ((JArray)valueArray))
                                {
                                    UserIdentityContract userIdentityContractInstance = new UserIdentityContract();
                                    result.Values.Add(userIdentityContractInstance);

                                    JToken providerValue = valueValue["provider"];
                                    if (providerValue != null && providerValue.Type != JTokenType.Null)
                                    {
                                        string providerInstance = ((string)providerValue);
                                        userIdentityContractInstance.Provider = providerInstance;
                                    }

                                    JToken idValue = valueValue["id"];
                                    if (idValue != null && idValue.Type != JTokenType.Null)
                                    {
                                        string idInstance = ((string)idValue);
                                        userIdentityContractInstance.Id = idInstance;
                                    }
                                }
                            }
                        }
                    }
                    result.StatusCode = statusCode;
                    if (httpResponse.Headers.Contains("x-ms-request-id"))
                    {
                        result.RequestId = httpResponse.Headers.GetValues("x-ms-request-id").FirstOrDefault();
                    }

                    if (shouldTrace)
                    {
                        TracingAdapter.Exit(invocationId, result);
                    }
                    return(result);
                }
                finally
                {
                    if (httpResponse != null)
                    {
                        httpResponse.Dispose();
                    }
                }
            }
            finally
            {
                if (httpRequest != null)
                {
                    httpRequest.Dispose();
                }
            }
        }