Esempio n. 1
0
        async Task ExecuteLoadItemsCommand()
        {
            if (IsBusy)
            {
                return;
            }

            IsBusy = true;

            try
            {
                SWApiService swApiService = new SWApiService(new HttpClient());
                var          starships    = await swApiService.GetShips("starships");

                foreach (var ship in starships)
                {
                    ShipModels.Add(ship);
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex);
            }
            finally
            {
                IsBusy = false;
            }
        }
Esempio n. 2
0
        static async Task Main()
        {
            int distance = 0;

            while (true)
            {
                Console.WriteLine("Set distance: ");
                if (int.TryParse(Console.ReadLine(), out distance) && distance > 0)
                {
                    break;
                }

                Console.WriteLine("Distance must be a positive number");
            }

            var swApi = new SWApiService(new ApiService());

            try
            {
                var result = await swApi.GetAllStarshipsParallelly();

                foreach (var starship in result.OrderBy(x => x.Name))
                {
                    Console.WriteLine($"{starship.Name}: {starship.CalculateStops(distance)?.ToString() ?? "unknown"}");
                }
            }
            catch (HttpRequestException)
            {
                Console.WriteLine("There was an error while communicating with SW API");
            }
        }
Esempio n. 3
0
        public async Task PostResultsAsync()
        {
            Random       random     = new Random();
            SWApiService apiService = new SWApiService(new HttpClient());

            PeopleModels = new List <PeopleModel>();
            ShipModels   = new List <ShipModel>();

            var people = await apiService.GetPeople("people");

            foreach (var person in people)
            {
                PeopleModels.Add(person);
            }

            var starships = await apiService.GetShips("starships");

            foreach (var ship in starships)
            {
                ShipModels.Add(ship);
            }

            SwHomePageVm swHomePageVm = new SwHomePageVm();

            swHomePageVm.name = PeopleModels[random.Next(1, 10)].name;
            swHomePageVm.ship = ShipModels[random.Next(1, 10)].name;

            var result = "You are " + swHomePageVm.name + " flying the " + swHomePageVm.ship + ".";

            answer.Text = result;
        }
        async Task ExecuteLoadPeopleCommand()
        {
            if (IsBusy)
            {
                return;
            }

            IsBusy = true;

            try
            {
                SWApiService swApiService = new SWApiService(new HttpClient());
                var          people       = await swApiService.GetPeople("people");

                foreach (var person in people)
                {
                    PeopleModels.Add(person);
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex);
            }
            finally
            {
                IsBusy = false;
            }
        }
Esempio n. 5
0
        public async Task TestGetStarships2()
        {
            var secondPageUrl = "Second_Page_Url";
            var pageCount     = StarshipApiServiceMock.Starships.Count / 2 + 1;

            // Init mock for 2 pages
            var mock = new Mock <IApiService>();

            mock.Setup(x => x.GetRequestAsync <StarshipsResponse>(secondPageUrl)).Returns(Task.FromResult(new StarshipsResponse()
            {
                Count     = StarshipApiServiceMock.Starships.Count,
                Starships = StarshipApiServiceMock.Starships.Skip(pageCount).ToList()
            }));
            mock.Setup(x => x.GetRequestAsync <StarshipsResponse>(It.IsNotIn(secondPageUrl))).Returns(Task.FromResult(new StarshipsResponse()
            {
                Count       = StarshipApiServiceMock.Starships.Count,
                Starships   = StarshipApiServiceMock.Starships.Take(pageCount).ToList(),
                NextPageUrl = secondPageUrl
            }));

            var mockService = new SWApiService(mock.Object);
            var starships   = await mockService.GetAllStarships();

            Assert.NotEmpty(starships);
            Assert.Equal(StarshipApiServiceMock.Starships.Count, starships.Count);

            // Order should match for both parallel and sequantional request
            for (int i = 0; i < StarshipApiServiceMock.Starships.Count; i++)
            {
                Assert.True(StarshipsEqual(starships[i], StarshipApiServiceMock.Starships[i]));
            }
        }
Esempio n. 6
0
        public async Task TestStarshipParallelGetOrder()
        {
            var slowService = new SWApiService(new StarshipSlowApiServiceMock());
            var starships   = await slowService.GetAllStarships();

            var starships2 = await slowService.GetAllStarshipsParallelly();

            Assert.Equal(starships.Count, starships2.Count);
            for (int i = 0; i < starships.Count; i++)
            {
                Assert.True(StarshipsEqual(starships[i], starships2[i]));
            }
        }
Esempio n. 7
0
        private async Task TestStarshipsCollection(StarshipsResponse response, Action <List <Starship> > test)
        {
            var mock = new Mock <IApiService>();

            mock.Setup(x => x.GetRequestAsync(It.IsNotNull <string>())).Returns(Task.FromResult(JsonConvert.SerializeObject(response)));
            mock.Setup(x => x.GetRequestAsync <StarshipsResponse>(It.IsNotNull <string>())).Returns(Task.FromResult(response));

            var mockService = new SWApiService(mock.Object);
            var starships   = await mockService.GetAllStarships();

            var starships2 = await mockService.GetAllStarshipsParallelly();

            test.Invoke(starships);
            test.Invoke(starships2);
        }