Exemplo n.º 1
0
        public void GetWeatherInfo_ThrowException_ReturnEmptyInstance()
        {
            #region Arange

            var loggerStub = new Mock <ILogger <AzureMaps> >();
            var logger     = loggerStub.Object;

            var iOptionsStub = new AzureMaps();
            var iOptions     = Options.Create <AzureMaps>(iOptionsStub);

            var httpClientFactoryStub = new Mock <IHttpClientFactory>();
            var actualHttpRequestUrl  = string.Empty;
            httpClientFactoryStub.Setup(m => m.CreateClient(It.IsAny <string>()))
            .Callback <string>(url => actualHttpRequestUrl = url)
            .Throws(new Exception("Test Case8"));
            var httpClientFactory = httpClientFactoryStub.Object;

            #endregion

            #region Action

            var targetService = new AzureMapsClientService(logger, iOptions, httpClientFactory);
            var actual        = targetService.WeatherGetCurrentConditions(string.Empty);

            #endregion

            #region Assert

            Assert.Null(actual.results);

            #endregion
        }
Exemplo n.º 2
0
        public void GetWeatherInfo_StatusCode200_ReturnSuccess()
        {
            #region Arange

            var loggerStub = new Mock <ILogger <AzureMaps> >();
            var logger     = loggerStub.Object;

            var iOptionsStub = new AzureMaps();
            var iOptions     = Options.Create <AzureMaps>(iOptionsStub);

            //The best way to create a mock HTTPClient instance is by mocking HttpMessageHandler.
            //Mocking of HttpMessageHandler will also ensure the client calling actual endpoints are faked by intercepting it.
            var httpMessageHandlerStub = this.GetHttpResponseMessageStub <CurrentConditionsModel>(new CurrentConditionsModel()
            {
                results = new Result[] {
                    (new Result()
                    {
                        dateTime = DateTime.Now,
                        cloudCover = "cloudCover",
                    })
                }
            }, HttpStatusCode.OK);

            var httpClientStub        = new HttpClient(httpMessageHandlerStub.Object);
            var httpClientFactoryStub = new Mock <IHttpClientFactory>();
            var actualHttpRequestUrl  = string.Empty;
            httpClientFactoryStub.Setup(m => m.CreateClient(It.IsAny <string>()))
            .Callback <string>(url => actualHttpRequestUrl = url)
            .Returns(httpClientStub);
            var httpClientFactory = httpClientFactoryStub.Object;

            #endregion

            #region Action

            var targetService = new AzureMapsClientService(logger, iOptions, httpClientFactory);
            var actual        = targetService.WeatherGetCurrentConditions(string.Empty);

            #endregion

            #region Assert

            Assert.Equal("cloudCover", actual.results.Single().cloudCover);
            Assert.Equal("https://atlas.microsoft.com/weather/currentConditions/json?api-version=1.0&subscription-key=プライマリーキー(Configに設定された値)", actualHttpRequestUrl);

            #endregion
        }
        public MainPage()
        {
            InitializeComponent();

            MyMap.MapServiceToken = BingMapServiceToken;
            azureMaps             = new AzureMaps(AzureMapsSubscriptionKey);

            NearbyLocations = new ObservableCollection <LocationData>();
            RouteWaypoints  = new ObservableCollection <LocationData>();
            Locations       = new ObservableCollection <LocationData>();
            MappedLocations = new ObservableCollection <LocationData>(Locations);

            RouteDatas = new ObservableCollection <RouteData>();

            // MappedLocations is a superset of Locations, so any changes in Locations
            // need to be reflected in MappedLocations.
            Locations.CollectionChanged += (s, e) =>
            {
                if (e.NewItems != null)
                {
                    foreach (LocationData item in e.NewItems)
                    {
                        MappedLocations.Add(item);
                    }
                }
                if (e.OldItems != null)
                {
                    foreach (LocationData item in e.OldItems)
                    {
                        MappedLocations.Remove(item);
                    }
                }

                if (Locations.Count > 0)
                {
                    PinnedLocationExpander.Visibility = Visibility.Visible;
                }
                else
                {
                    PinnedLocationExpander.Visibility = Visibility.Collapsed;
                }
            };

            // GoButton to be enable only when RouteWaypoints contain two or more waypoints.
            // Rearrange the waypoint will trigger this block of code and  will perform
            // GetDirections automatically.
            RouteWaypoints.CollectionChanged += (s, e) =>
            {
                if (RouteWaypoints.Count >= 2)
                {
                    GetRouteDirections();
                    GoButton.IsEnabled = true;
                }
                else
                {
                    GoButton.IsEnabled = false;
                    RouteDatas.Clear();
                }
            };

            /// Search nearby POIs by selected category, populate NearbyLocations list and together
            /// update Locations list so the pin will shown on map.
            NearbyLocations.CollectionChanged += (s, e) =>
            {
                if (e.NewItems != null)
                {
                    foreach (LocationData item in e.NewItems)
                    {
                        Locations.Add(item);
                    }
                }
                if (e.OldItems != null)
                {
                    foreach (LocationData item in e.OldItems)
                    {
                        Locations.Remove(item);
                    }
                }

                if (NearbyLocations.Count > 0)
                {
                    PinnedLocationExpander.Visibility = Visibility.Collapsed;
                    NearbyLocationExpander.Visibility = Visibility.Visible;
                }
                else
                {
                    NearbyLocationExpander.Visibility = Visibility.Collapsed;
                }
            };
        }