Esempio n. 1
0
        /// <summary>
        /// Unit test helper to create a mock HttpResponseMessage action which is errored
        /// </summary>
        /// <returns></returns>
        public static Func <HttpResponseMessage> CreateErrorResponse(HttpStatusCode statusCode = HttpStatusCode.LengthRequired) // general error
        {
            string errorFilePath = "ApiError.json";

            switch (statusCode)
            {
            case HttpStatusCode.Forbidden:
                errorFilePath = "ApiError-Request-Forbidden.json";
                break;

            case HttpStatusCode.Unauthorized:
                errorFilePath = "ApiError-Request-Unauthorized.json";
                break;

            case HttpStatusCode.BadRequest:
                errorFilePath = "ApiError-Request-BadRequest.json";
                break;
            }

            string content         = SampleDataHelper.GetContent(errorFilePath);
            var    responseMessage =
                new Func <HttpResponseMessage>(() => new HttpResponseMessage(statusCode)
            {
                Content = new StringContent(content)
            });

            return(responseMessage);
        }
Esempio n. 2
0
        public async void GetSleepAsync_Success()
        {
            string content = SampleDataHelper.GetContent("GetSleep.json");

            var responseMessage = new Func <HttpResponseMessage>(() =>
            {
                return(new HttpResponseMessage(HttpStatusCode.OK)
                {
                    Content = new StringContent(content)
                });
            });

            var verification = new Action <HttpRequestMessage, CancellationToken>((message, token) =>
            {
                Assert.AreEqual(HttpMethod.Get, message.Method);
                Assert.AreEqual("https://api.fitbit.com/1.2/user/-/sleep/date/2014-10-17.json",
                                message.RequestUri.AbsoluteUri);
            });

            var fitbitClient = Helper.CreateFitbitClient(responseMessage, verification);

            var response = await fitbitClient.GetSleepDateAsync(new DateTime(2014, 10, 17));

            ValidateSleep(response);
        }
Esempio n. 3
0
        public async void GetTimeSeriesDataListAsync_DoubleDate_Success()
        {
            string content = SampleDataHelper.GetContent("TimeSeries-ActivitiesDistance.json");

            var responseMessage = new Func <HttpResponseMessage>(() =>
            {
                return(new HttpResponseMessage(HttpStatusCode.OK)
                {
                    Content = new StringContent(content)
                });
            });

            var verification = new Action <HttpRequestMessage, CancellationToken>((message, token) =>
            {
                Assert.AreEqual(HttpMethod.Get, message.Method);
                Assert.AreEqual("https://api.fitbit.com/1/user/-/activities/distance/date/2014-09-07/2014-09-14.json", message.RequestUri.AbsoluteUri);
            });

            var fitbitClient = Helper.CreateFitbitClient(responseMessage, verification);

            var response = await fitbitClient.GetTimeSeriesAsync(TimeSeriesResourceType.Distance, new DateTime(2014, 9, 7), new DateTime(2014, 9, 14));

            Assert.IsTrue(response.Success);
            ValidateDataList(response.Data);
        }
        public async Task GetDevicesAsync_Success()
        {
            string content = SampleDataHelper.GetContent("GetDevices-Single.json");

            Func <HttpResponseMessage> responseMessage = new Func <HttpResponseMessage>(() =>
            {
                return(new HttpResponseMessage(HttpStatusCode.OK)
                {
                    Content = new StringContent(content)
                });
            });

            Action <HttpRequestMessage, CancellationToken> verification = new Action <HttpRequestMessage, CancellationToken>((message, token) =>
            {
                Assert.AreEqual(HttpMethod.Get, message.Method);
                Assert.AreEqual("https://api.fitbit.com/1/user/-/devices.json", message.RequestUri.AbsoluteUri);
            });

            FitbitClient fitbitClient = Helper.CreateFitbitClient(responseMessage, verification);

            List <Device> response = await fitbitClient.GetDevicesAsync();

            Assert.AreEqual(1, response.Count);
            Device device = response.First();

            ValidateSingleDevice(device);
        }
Esempio n. 5
0
        public async void GetDayActivitySummaryAsync_Success()
        {
            string content = SampleDataHelper.GetContent("GetActivities.json");

            var responseMessage = new Func <HttpResponseMessage>(() =>
            {
                return(new HttpResponseMessage(HttpStatusCode.OK)
                {
                    Content = new StringContent(content)
                });
            });

            var verification = new Action <HttpRequestMessage, CancellationToken>((message, token) =>
            {
                Assert.AreEqual(HttpMethod.Get, message.Method);
                Assert.AreEqual("https://api.fitbit.com/1/user/-/activities/date/2014-09-27.json", message.RequestUri.AbsoluteUri);
            });

            var fitbitClient = Helper.CreateFitbitClient(responseMessage, verification);

            var response = await fitbitClient.GetDayActivitySummaryAsync(new DateTime(2014, 9, 27));

            Assert.IsTrue(response.Success);
            ValidateActivitySummary(response.Data);
        }
Esempio n. 6
0
        public void Can_Deserialize_ApiNotifications()
        {
            var content      = SampleDataHelper.GetContent("ApiSubscriptionNotification.json");
            var deserializer = new JsonDotNetSerializer();

            var resources = deserializer.Deserialize <List <UpdatedResource> >(content);

            Assert.IsNotNull(resources);
            Assert.AreEqual(3, resources.Count);
            Assert.AreEqual(2, resources.Count(x => x.CollectionType == APICollectionType.foods));
            Assert.AreEqual(1, resources.Count(x => x.CollectionType == APICollectionType.activities));

            var resource = resources[0];

            Assert.AreEqual(APICollectionType.foods, resource.CollectionType);
            Assert.AreEqual(new DateTime(2010, 03, 01), resource.Date);
            Assert.AreEqual("228S74", resource.OwnerId);
            Assert.AreEqual(ResourceOwnerType.User, resource.OwnerType);
            Assert.AreEqual("1234", resource.SubscriptionId);

            resource = resources[1];
            Assert.AreEqual(APICollectionType.foods, resource.CollectionType);
            Assert.AreEqual(new DateTime(2010, 03, 02), resource.Date);
            Assert.AreEqual("228S74", resource.OwnerId);
            Assert.AreEqual(ResourceOwnerType.User, resource.OwnerType);
            Assert.AreEqual("1234", resource.SubscriptionId);

            resource = resources[2];
            Assert.AreEqual(APICollectionType.activities, resource.CollectionType);
            Assert.AreEqual(new DateTime(2010, 03, 01), resource.Date);
            Assert.AreEqual("184X36", resource.OwnerId);
            Assert.AreEqual(ResourceOwnerType.User, resource.OwnerType);
            Assert.AreEqual("2345", resource.SubscriptionId);
        }
Esempio n. 7
0
        public async void GetWaterAsync_Success()
        {
            string content = SampleDataHelper.GetContent("GetWater-WaterData.json");

            var responseMessage = new Func <HttpResponseMessage>(() =>
            {
                return(new HttpResponseMessage(HttpStatusCode.OK)
                {
                    Content = new StringContent(content)
                });
            });

            var verification = new Action <HttpRequestMessage, CancellationToken>((message, token) =>
            {
                Assert.AreEqual(HttpMethod.Get, message.Method);
                Assert.AreEqual("https://api.fitbit.com/1/user/-/foods/log/water/date/2015-01-12.json", message.RequestUri.AbsoluteUri);
            });

            var fitbitClient = Helper.CreateFitbitClient(responseMessage, verification);

            var response = await fitbitClient.GetWaterAsync(new DateTime(2015, 1, 12));

            Assert.IsTrue(response.Success);
            ValidateWaterData(response.Data);
        }
Esempio n. 8
0
        public async Task GetGoalsAsync()
        {
            string content = SampleDataHelper.GetContent("GetGoals.json");

            var responseMessage = new Func <HttpResponseMessage>(() => new HttpResponseMessage(HttpStatusCode.OK)
            {
                Content = new StringContent(content)
            });

            var verification = new Action <HttpRequestMessage, CancellationToken>((message, token) =>
            {
                Assert.AreEqual(HttpMethod.Get, message.Method);
                Assert.AreEqual("https://api.fitbit.com/1/user/-/activities/goals/weekly.json", message.RequestUri.AbsoluteUri);
            });

            var fitbitClient = Helper.CreateFitbitClient(responseMessage, verification);

            var response = await fitbitClient.GetGoalsAsync(GoalPeriod.Weekly);

            Assert.AreEqual(300, response.ActiveMinutes);
            Assert.AreEqual(3000, response.CaloriesOut);
            Assert.AreEqual(8.05, response.Distance);
            Assert.AreEqual(100, response.Floors);
            Assert.AreEqual(10000, response.Steps);
        }
Esempio n. 9
0
        public async Task GetFriendsMultipleAsync_Success()
        {
            string content = SampleDataHelper.GetContent("GetFriends-Multiple.json");

            var responseMessage = new Func <HttpResponseMessage>(() =>
            {
                return(new HttpResponseMessage(HttpStatusCode.OK)
                {
                    Content = new StringContent(content)
                });
            });

            var verification = new Action <HttpRequestMessage, CancellationToken>((message, token) =>
            {
                Assert.AreEqual(HttpMethod.Get, message.Method);
                Assert.AreEqual("https://api.fitbit.com/1/user/-/friends.json", message.RequestUri.AbsoluteUri);
            });

            var fitbitClient = Helper.CreateFitbitClient(responseMessage, verification);

            var response = await fitbitClient.GetFriendsAsync();

            Assert.AreEqual(3, response.Count);
            ValidateMultipleFriends(response);
        }
        public async Task GetIntraDayTimeSeriesStepsAsync_Success()
        {
            DateTime expectedResult = new DateTime(2016, 3, 8, 0, 1, 0);

            string content         = SampleDataHelper.GetContent("IntradayActivitiesSteps.json");
            var    responseMessage = new Func <HttpResponseMessage>(() =>
            {
                return(new HttpResponseMessage(HttpStatusCode.OK)
                {
                    Content = new StringContent(content)
                });
            });

            var verification = new Action <HttpRequestMessage, CancellationToken>((message, token) =>
            {
                message.Method.Should().Be(HttpMethod.Get);
                message.RequestUri.AbsoluteUri.Should().Be("https://api.fitbit.com/1/user/-/activities/steps/date/2016-03-08/1d.json");
            });

            var fitbitClient = Helper.CreateFitbitClient(responseMessage, verification);
            var response     = await fitbitClient.GetIntraDayTimeSeriesAsync(IntradayResourceType.Steps, new DateTime(2016, 3, 8), new TimeSpan(24, 0, 0));

            response.DataSet[1].Time.Should().Be(expectedResult);
            response.DataSet[1].Value.Should().Be("2");
        }
Esempio n. 11
0
        private FitbitClient SetupFitbitClient(string contentPath, string url, HttpMethod expectedMethod, Action <HttpRequestMessage> additionalChecks = null)
        {
            var content = string.Empty;

            if (contentPath != null)
            {
                content = SampleDataHelper.GetContent(contentPath);
            }

            var responseMessage = new Func <HttpResponseMessage>(() =>
            {
                return(new HttpResponseMessage(HttpStatusCode.NoContent)
                {
                    Content = new StringContent(content)
                });
            });

            var verification = new Action <HttpRequestMessage, CancellationToken>((message, token) =>
            {
                Assert.AreEqual(expectedMethod, message.Method);
                Assert.AreEqual(url, message.RequestUri.AbsoluteUri);
                if (additionalChecks != null)
                {
                    additionalChecks(message);
                }
            });

            return(Helper.CreateFitbitClient(responseMessage, verification));
        }
Esempio n. 12
0
        public void Can_Process_Single_Request()
        {
            const int    ExpectedResourceUpdateCount = 1;
            const string Expected_ownerId            = "224F7B";
            const string Expected_subId = "21951163-8ad9-444b-8225-ff230b6e9885-activities";

            DateTime Expected_Date = new DateTime(2012, 5, 24);

            string content = SampleDataHelper.GetContent("SingleSubscriptionNotification.xml");

            SubscriptionManager sut = new SubscriptionManager();

            List <UpdatedResource> resultResourceList = sut.ProcessUpdateReponseBody(content);

            Assert.IsNotNull(resultResourceList);
            Assert.AreEqual(ExpectedResourceUpdateCount, resultResourceList.Count);

            UpdatedResource result = resultResourceList[0];

            Assert.AreEqual(Expected_ownerId, result.OwnerId);
            Assert.AreEqual(Expected_subId, result.SubscriptionId);

            Assert.AreEqual(APICollectionType.foods, result.CollectionType);
            Assert.AreEqual(ResourceOwnerType.User, result.OwnerType);
            Assert.AreEqual(Expected_Date, result.Date);
        }
        public async void GetIntraDayTimeSeriesAsync_Success()
        {
            string content = SampleDataHelper.GetContent("IntradayActivitiesCalories.json");

            var responseMessage = new Func <HttpResponseMessage>(() =>
            {
                return(new HttpResponseMessage(HttpStatusCode.OK)
                {
                    Content = new StringContent(content)
                });
            });

            var verification = new Action <HttpRequestMessage, CancellationToken>((message, token) =>
            {
                Assert.AreEqual(HttpMethod.Get, message.Method);
                Assert.AreEqual("https://api.fitbit.com/1/user/-/activities/calories/date/2015-03-20/1d.json", message.RequestUri.AbsoluteUri);
            });

            var fitbitClient = Helper.CreateFitbitClient(responseMessage, verification);

            var response = await fitbitClient.GetIntraDayTimeSeriesAsync(IntradayResourceType.CaloriesOut, new DateTime(2015, 3, 20), new TimeSpan(24, 0, 0));

            Assert.IsTrue(response.Success);

            Assert.AreEqual(response.Data.DataSet[1].Time, new DateTime(2015, 3, 20, 0, 1, 0));

            //ValidateDataList(response.Data);
        }
Esempio n. 14
0
        public void Can_Process_Multiple_Request()
        {
            const int    Expect_TWO_UpdatedResources = 2;
            const string Expected_ownerId            = "2242TQ";
            const string Expected_subId = "b4b6dc1a5ead4b4e84f7b7f5b2f16b21-activities";

            DateTime Expected_Date = new DateTime(2012, 7, 25);

            string content = SampleDataHelper.GetContent("MultipleSingleSubscriptionNotification.xml");

            SubscriptionManager sut = new SubscriptionManager();

            List <UpdatedResource> resultResourceList = sut.ProcessUpdateReponseBody(content);

            Assert.IsNotNull(resultResourceList);
            Assert.AreEqual(Expect_TWO_UpdatedResources, resultResourceList.Count);

            UpdatedResource result = resultResourceList[1];

            Assert.AreEqual(Expected_ownerId, result.OwnerId);
            Assert.AreEqual(Expected_subId, result.SubscriptionId);

            Assert.AreEqual(APICollectionType.activities, result.CollectionType);
            Assert.AreEqual(ResourceOwnerType.User, result.OwnerType);
            Assert.AreEqual(Expected_Date, result.Date);
        }
Esempio n. 15
0
        public async void GetUserProfileAsync_Success()
        {
            string content = SampleDataHelper.GetContent("UserProfile.json");

            var responseMessage = new Func <HttpResponseMessage>(() =>
            {
                return(new HttpResponseMessage(HttpStatusCode.OK)
                {
                    Content = new StringContent(content)
                });
            });

            var verification = new Action <HttpRequestMessage, CancellationToken>((message, token) =>
            {
                Assert.AreEqual(HttpMethod.Get, message.Method);
                Assert.AreEqual("https://api.fitbit.com/1/user/-/profile.json", message.RequestUri.AbsoluteUri);
            });

            var fitbitClient = Helper.CreateFitbitClient(responseMessage, verification);

            var response = await fitbitClient.GetUserProfileAsync();

            Assert.IsTrue(response.Success);
            var user = response.Data;

            ValidateSingleUserProfile(user);
        }
Esempio n. 16
0
        public async void PostWaterLogAsync_Success()
        {
            string content = SampleDataHelper.GetContent("LogWater-WaterLog.json");

            var responseMessage = new Func <HttpResponseMessage>(() =>
            {
                return(new HttpResponseMessage(HttpStatusCode.OK)
                {
                    Content = new StringContent(content)
                });
            });

            var verification = new Action <HttpRequestMessage, CancellationToken>((message, token) =>
            {
                Assert.AreEqual(HttpMethod.Post, message.Method);
                Assert.AreEqual("https://api.fitbit.com/1/user/-/foods/log/water.json?amount=300&date=2015-01-12", message.RequestUri.AbsoluteUri);
            });

            var fitbitClient = Helper.CreateFitbitClient(responseMessage, verification);

            var response = await fitbitClient.LogWaterAsync(new DateTime(2015, 1, 12), new WaterLog { Amount = 300 });

            Assert.IsTrue(response.Success);
            Assert.IsNotNull(response.Data);
            Assert.AreEqual(300, response.Data.Amount);
        }
        public async Task GetIntraDayTimeSeriesCaloriesIntensityMetsAsync_Success()
        {
            DateTime expectedResult = new DateTime(2015, 3, 20, 0, 1, 0);

            string content = SampleDataHelper.GetContent("IntradayActivitiesCalories.json");
            Func <HttpResponseMessage> responseMessage = new Func <HttpResponseMessage>(() =>
            {
                return(new HttpResponseMessage(HttpStatusCode.OK)
                {
                    Content = new StringContent(content)
                });
            });

            Action <HttpRequestMessage, CancellationToken> verification = new Action <HttpRequestMessage, CancellationToken>((message, token) =>
            {
                message.Method.Should().Be(HttpMethod.Get);
                message.RequestUri.AbsoluteUri.Should().Be("https://api.fitbit.com/1/user/-/activities/calories/date/2015-03-20/1d.json");
            });

            FitbitClient fitbitClient = Helper.CreateFitbitClient(responseMessage, verification);
            IntradayData response     = await fitbitClient.GetIntraDayTimeSeriesAsync(IntradayResourceType.CaloriesOut, new DateTime(2015, 3, 20), new TimeSpan(24, 0, 0));

            response.DataSet[1].Time.Should().Be(expectedResult);
            response.DataSet[1].METs.Should().Be("10");
            response.DataSet[1].Level.Should().Be("0");
            response.DataSet[1].Value.Should().Be("1.1857000589370628");
        }
Esempio n. 18
0
        public async Task GetWeightGoalAsync()
        {
            string content = SampleDataHelper.GetContent("GetWeightGoal.json");

            var responseMessage = new Func <HttpResponseMessage>(() => new HttpResponseMessage(HttpStatusCode.OK)
            {
                Content = new StringContent(content)
            });

            var verification = new Action <HttpRequestMessage, CancellationToken>((message, token) =>
            {
                Assert.AreEqual(HttpMethod.Get, message.Method);
                Assert.AreEqual("https://api.fitbit.com/1/user/-/body/log/weight/goal.json", message.RequestUri.AbsoluteUri);
            });

            var fitbitClient = Helper.CreateFitbitClient(responseMessage, verification);

            var response = await fitbitClient.GetWeightGoalsAsync();

            Assert.AreEqual("2019-01-01", response.StartDate.ToString("yyyy-MM-dd"));
            Assert.AreEqual(500, response.StartWeight);
            Assert.AreEqual(100, response.Weight);
            Assert.AreEqual("LOSE", response.GoalType);
            Assert.AreEqual(0.11, response.WeightThreshold);
        }
Esempio n. 19
0
        public void OAuth2Helper_Can_Deserialize_AccessToken()
        {
            string content = SampleDataHelper.GetContent("AccessToken.json");

            var result = OAuth2Helper.ParseAccessTokenResponse(content);

            Validate(result);
        }
Esempio n. 20
0
        public void Can_Deserialize_AccessToken()
        {
            string content = SampleDataHelper.GetContent("AccessToken.json");

            var deserializer = new JsonDotNetSerializer();
            var result       = deserializer.Deserialize <OAuth2AccessToken>(content);

            Validate(result);
        }
Esempio n. 21
0
        public void Can_Deserialize_Sleep()
        {
            string content = SampleDataHelper.GetContent("GetSleep.json");
            JsonDotNetSerializer deserializer = new JsonDotNetSerializer();

            SleepLogDateBase sleep = deserializer.Deserialize <SleepLogDateBase>(content);

            ValidateSleep(sleep);
        }
        public void Can_Deserialize_Food()
        {
            string content = SampleDataHelper.GetContent("GetFoodLogs.json");
            JsonDotNetSerializer deserializer = new JsonDotNetSerializer();

            Food food = deserializer.Deserialize <Food>(content);

            ValidateFoodData(food);
        }
Esempio n. 23
0
        public void Can_Deserialize_BodyMeasurements()
        {
            string content = SampleDataHelper.GetContent("GetBodyMeasurements.json");
            JsonDotNetSerializer deserializer = new JsonDotNetSerializer();

            BodyMeasurements bodyMeasurements = deserializer.Deserialize <BodyMeasurements>(content);

            ValidateBodyMeasurements(bodyMeasurements);
        }
Esempio n. 24
0
        public void Can_Deserialize_Fat()
        {
            string content      = SampleDataHelper.GetContent("GetFat.json");
            var    deserializer = new JsonDotNetSerializer();

            Fat fat = deserializer.GetFat(content);

            ValidateFat(fat);
        }
Esempio n. 25
0
        public void Can_Deserialize_Activities()
        {
            string content      = SampleDataHelper.GetContent("ActivitiesStats.json");
            var    deserializer = new JsonDotNetSerializer();

            ActivitiesStats stats = deserializer.Deserialize <ActivitiesStats>(content);

            ValidateActivity(stats);
        }
Esempio n. 26
0
        public void Can_Deserialize_Sleep_Log_List()
        {
            string content      = SampleDataHelper.GetContent("GetSleepRange.json");
            var    deserializer = new JsonDotNetSerializer();

            SleepDateRangeBase sleep = deserializer.Deserialize <SleepDateRangeBase>(content);

            ValidateSleepRange(sleep);
        }
Esempio n. 27
0
        public void Can_Deserialize_Sleep_Old()
        {
            string content      = SampleDataHelper.GetContent("GetSleepOld.json");
            var    deserializer = new JsonDotNetSerializer();

            SleepData sleep = deserializer.Deserialize <SleepData>(content);

            ValidateSleepOld(sleep);
        }
Esempio n. 28
0
        public void Can_Deserialize_Weight()
        {
            string content      = SampleDataHelper.GetContent("GetWeight.json");
            var    deserializer = new JsonDotNetSerializer();

            var weight = deserializer.GetWeight(content);

            ValidateWeight(weight);
        }
Esempio n. 29
0
        public void Can_Deserialize_Activity()
        {
            string content      = SampleDataHelper.GetContent("GetActivities.json");
            var    deserializer = new JsonDotNetSerializer();

            Activity activity = deserializer.Deserialize <Activity>(content);

            ValidateActivity(activity);
        }
Esempio n. 30
0
        public void Can_Deserialize_Food()
        {
            string content      = SampleDataHelper.GetContent("GetBloodPressure.json");
            var    deserializer = new JsonDotNetSerializer();

            BloodPressureData bp = deserializer.Deserialize <BloodPressureData>(content);

            ValidateBloodPressureData(bp);
        }