public void GetCdsDatabaseCurrenciesAsyncSuccess()
        {
            string expectedLocation   = "unitedstates";
            string expectedRequestUri = $"https://api.bap.microsoft.com/providers/Microsoft.BusinessAppPlatform/locations/{expectedLocation}/environmentCurrencies?api-version=2016-11-01";
            Dictionary <string, string> expectedCurrencies = new Dictionary <string, string>()
            {
                { "location", expectedLocation },
                { "name1", "XCD" },
                { "code1", "XCD" },
                { "symbol1", "EC$" },
                { "name2", "USD" },
                { "code2", "USD" },
                { "symbol2", "US$" },
                { "name3", "AED" },
                { "code3", "AED" },
                { "symbol3", "د.إ." },
            };
            string responseFilePath = @"./data/templates/responses/powerApps/getCdsDatabaseCurrencies.json";

            HttpRequestMessage expectedRequest = TestHelper.CreateHttpRequest(
                HttpMethod.Get,
                expectedRequestUri);

            _httpClient.RegisterExpectedRequest(new ExpectedRequest(expectedRequest));

            HttpResponseMessage expectedResponse = TestHelper.CreateHttpResponse(
                HttpStatusCode.OK,
                null,
                responseFilePath,
                "application/json",
                expectedCurrencies);

            _httpClient.RegisterExpectedResponse(
                expectedRequestUri,
                new ExpectedResponse(expectedResponse));

            IPowerAppsClient client = new PowerAppsClient(_tokenProvider);

            GetPowerAppsCurrenciesResponse response = client.GetCdsDatabaseCurrenciesAsync(expectedLocation).Result;

            Assert.IsNotNull(response, "The response should not be null!");
            Assert.IsNotNull(response.Value, "The response Value member should not be null!");
            Assert.AreEqual(3, response.Value.Length, $"Unexpected number of locations ('3' != '{response.Value.Length}')!");
            Assert.AreEqual(expectedCurrencies["name1"], response.Value[0].Name, $"Unexpected name for currency 1 ('{expectedCurrencies["name1"]}' != '{response.Value[0].Name}')");
            Assert.IsNotNull(response.Value[0].Properties, "The response Value Properties member should not be null for currency 1!");
            Assert.AreEqual(expectedCurrencies["code1"], response.Value[0].Properties.Code, $"Unexpected name for currency 1 ('{expectedCurrencies["code1"]}' != '{response.Value[0].Name}')");
            Assert.AreEqual(expectedCurrencies["symbol1"], response.Value[0].Properties.Symbol, $"Unexpected symbol for currency 1 ('{expectedCurrencies["symbol1"]}' != '{response.Value[0].Properties.Symbol}')");
            Assert.AreEqual(expectedCurrencies["name2"], response.Value[1].Name, $"Unexpected name for currency 2 ('{expectedCurrencies["name2"]}' != '{response.Value[1].Name}')");
            Assert.IsNotNull(response.Value[1].Properties, "The response Value Properties member should not be null for currency 2!");
            Assert.AreEqual(expectedCurrencies["code2"], response.Value[1].Properties.Code, $"Unexpected name for currency 2 ('{expectedCurrencies["code2"]}' != '{response.Value[1].Name}')");
            Assert.AreEqual(expectedCurrencies["symbol2"], response.Value[1].Properties.Symbol, $"Unexpected symbol for currency 2 ('{expectedCurrencies["symbol2"]}' != '{response.Value[1].Properties.Symbol}')");
            Assert.AreEqual(expectedCurrencies["name3"], response.Value[2].Name, $"Unexpected name for currency 3 ('{expectedCurrencies["name3"]}' != '{response.Value[2].Name}')");
            Assert.IsNotNull(response.Value[2].Properties, "The response Value Properties member should not be null for currency 3!");
            Assert.AreEqual(expectedCurrencies["code3"], response.Value[2].Properties.Code, $"Unexpected name for currency 3 ('{expectedCurrencies["code3"]}' != '{response.Value[2].Name}')");
            Assert.AreEqual(expectedCurrencies["symbol3"], response.Value[2].Properties.Symbol, $"Unexpected symbol for currency 3 ('{expectedCurrencies["symbol3"]}' != '{response.Value[2].Properties.Symbol}')");
        }
        private void RunGetPowerAppsCdsDatabaseCurrenciesAsync()
        {
            Task.Run(() =>
            {
                OperationRunner singleRunner = new OperationRunner(
                    null,
                    this,
                    WizardContext.LogFileStream);
                singleRunner.IndeterminateOps = true;
                singleRunner.OnLog           += WriteLog;

                singleRunner.Operations.Enqueue(new Operation()
                {
                    Name = "GetPowerAppsCdsDatabaseCurrencies",
                    OperationFunction = GetPowerAppsCdsDatabaseCurrenciesAsync,
                    ValidateFunction  = (context) =>
                    {
                        return(context.LastOperationStatusCode == 0);
                    },
                    OperationCompletedHandler = (result) =>
                    {
                        GetPowerAppsCurrenciesResponse response = (GetPowerAppsCurrenciesResponse)result;

                        DataModel.InstallationConfiguration.PowerApps.Currencies = response.Value.Select(x => new PowerAppsCdsCurrency()
                        {
                            CurrencyCode = x.Properties.Code, CurrencyName = x.Name, CurrencySymbol = x.Properties.Symbol
                        }).ToList();

                        PowerAppsCdsCurrency defaultCurrency = DataModel.InstallationConfiguration.PowerApps.Currencies.Where(x => x.CurrencyCode == _defaultCurrencyCode).FirstOrDefault();

                        DataModel.InstallationConfiguration.PowerApps.SelectedCurrency =
                            defaultCurrency != null ?
                            defaultCurrency :
                            DataModel.InstallationConfiguration.PowerApps.Currencies.FirstOrDefault();
                    },
                    ExceptionHandler = (ex) =>
                    {
                        DataModel.StatusMessage = "Failed to acquire PowerApps CDS database currencies!";
                    },
                });

                singleRunner.RunOperations();
            });
        }
Пример #3
0
        /// <summary>
        /// Gets available CDS database currencies.
        /// </summary>
        /// <param name="location">The location to get CDS database currencies for.</param>
        /// <returns>Information regarding the available CDS database currencies.</returns>
        public async Task <GetPowerAppsCurrenciesResponse> GetCdsDatabaseCurrenciesAsync(string location)
        {
            LogInformation("Acquiring access token...");
            IHttpClient httpClient = TokenProvider.GetHttpClient(Audience);

            LogInformation($"Acquiring PowerApps Common Data Services database currencies for location '{location}'...");
            HttpResponseMessage response = await httpClient.GetAsync(
                _getCdsDatabaseCurrenciesUri
                .Replace("{location}", location));

            GetPowerAppsCurrenciesResponse result = null;

            if (response.IsSuccessStatusCode)
            {
                result = JsonConvert.DeserializeObject <GetPowerAppsCurrenciesResponse>(await response.Content.ReadAsStringAsync());
            }
            else
            {
                LogError($"ERROR: ({response.StatusCode}) {response.ReasonPhrase}");
                throw new RequestException(response);
            }

            return(result);
        }