public void GivenValidRequestTokenWhenOnboardingThenThereShouldBeAValidResponse()
        {
            var onboardingService =
                new SecuredOnboardingService(Environment, HttpClient);

            var parameters = new OnboardParameters
            {
                Uuid                   = Guid.NewGuid().ToString(),
                ApplicationId          = ApplicationId,
                ApplicationType        = ApplicationTypeDefinitions.Application,
                CertificationType      = CertificationTypeDefinition.P12,
                GatewayId              = "3",
                RegistrationCode       = "65220a7655",
                CertificationVersionId = CertificationVersionId
            };


            var onboardingResponse = onboardingService.Onboard(parameters, PrivateKey);

            Assert.NotEmpty(onboardingResponse.DeviceAlternateId);
            Assert.NotEmpty(onboardingResponse.SensorAlternateId);
            Assert.NotEmpty(onboardingResponse.CapabilityAlternateId);

            Assert.NotEmpty(onboardingResponse.Authentication.Certificate);
            Assert.NotEmpty(onboardingResponse.Authentication.Secret);
            Assert.NotEmpty(onboardingResponse.Authentication.Type);

            Assert.NotEmpty(onboardingResponse.ConnectionCriteria.Commands);
            Assert.NotEmpty(onboardingResponse.ConnectionCriteria.Measures);
        }
        public void GivenInvalidRequestTokenWhenOnboardingThenThereShouldBeAnException()
        {
            var onboardingService = new OnboardService(Environment, UtcDataService, HttpClient);

            var parameters = new OnboardParameters
            {
                Uuid                   = Guid.NewGuid().ToString(),
                ApplicationId          = ApplicationId,
                ApplicationType        = ApplicationTypeDefinitions.Application,
                CertificationType      = CertificationTypeDefinition.P12,
                GatewayId              = "3",
                RegistrationCode       = "XXXXXXXX",
                CertificationVersionId = CertificationVersionId
            };


            Assert.Throws <OnboardException>(() => onboardingService.Onboard(parameters));
        }
        public void GivenInvalidRequestTokenWhenOnboardingThenThereShouldBeAnException()
        {
            var onboardingService =
                new SecuredOnboardingService(Environment, HttpClient);

            var parameters = new OnboardParameters
            {
                Uuid                   = GetType().FullName,
                ApplicationId          = ApplicationId,
                ApplicationType        = ApplicationTypeDefinitions.Application,
                CertificationType      = CertificationTypeDefinition.P12,
                GatewayId              = "3",
                RegistrationCode       = "XXXXXXXX",
                CertificationVersionId = CertificationVersionId
            };


            Assert.Throws <OnboardException>(() => onboardingService.Onboard(parameters, PrivateKey));
        }
        /// <summary>
        ///     Onboard an endpoint using the simple onboarding procedure and the given parameters.
        /// </summary>
        /// <param name="onboardParameters">The onboarding parameters.</param>
        /// <param name="privateKey">The private key.</param>
        /// <returns>-</returns>
        /// <exception cref="OnboardException">Will be thrown if the onboarding was not successful.</exception>
        public OnboardResponse Onboard(OnboardParameters onboardParameters, string privateKey)
        {
            var onboardingRequest = new OnboardRequest
            {
                ExternalId             = onboardParameters.Uuid,
                ApplicationId          = onboardParameters.ApplicationId,
                CertificationVersionId = onboardParameters.CertificationVersionId,
                GatewayId       = onboardParameters.GatewayId,
                CertificateType = onboardParameters.CertificationType,
                TimeZone        = UtcDataService.TimeZone,
                UtcTimestamp    = UtcDataService.Now
            };

            var requestBody = JsonConvert.SerializeObject(onboardingRequest);

            var httpRequestMessage = new HttpRequestMessage
            {
                Method     = HttpMethod.Post,
                RequestUri = new Uri(_environment.SecuredOnboardingUrl()),
                Content    = new StringContent(requestBody, Encoding.UTF8, "application/json")
            };

            httpRequestMessage.Headers.Authorization =
                new AuthenticationHeaderValue("Bearer", onboardParameters.RegistrationCode);
            httpRequestMessage.Headers.Add("X-Agrirouter-ApplicationId", onboardParameters.ApplicationId);
            httpRequestMessage.Headers.Add("X-Agrirouter-Signature",
                                           SignatureService.XAgrirouterSignature(requestBody, privateKey));

            var httpResponseMessage = _httpClient.SendAsync(httpRequestMessage).Result;

            if (!httpResponseMessage.IsSuccessStatusCode)
            {
                throw new OnboardException(httpResponseMessage.StatusCode,
                                           httpResponseMessage.Content.ReadAsStringAsync().Result);
            }

            var result             = httpResponseMessage.Content.ReadAsStringAsync().Result;
            var onboardingResponse = JsonConvert.DeserializeObject(result, typeof(OnboardResponse));

            return(onboardingResponse as OnboardResponse);
        }