public void RequestAccessToken_WhenOKStatusCode_ShouldSetInstanceUrl()
        {
            MockHttpMessageHandler mockHandler = new MockHttpMessageHandler();
            SFDCClient             client      = new SFDCClient(new HttpClient(mockHandler));

            client.ClientId     = "cliend_id";
            client.ClientSecret = "client_secret";
            client.Username     = "******";
            client.Password     = "******";
            client.ApiVersion   = 45.0;

            string endpoint = "/services/oauth2/token";
            string fakeAccessTokenResponse = "{\"access_token\":\"fake_access_token\"," +
                                             "\"instance_url\":\"https://www.fake_instance_url.com\"," +
                                             "\"id\":\"fake_id\"," +
                                             "\"token_type\":\"fake_token_type\"," +
                                             "\"issued_at\":\"fake_issued_at\"," +
                                             "\"signature\":\"fake_signature\"}";

            mockHandler
            .When(endpoint)
            .Respond(HttpStatusCode.OK, "application/json", fakeAccessTokenResponse);

            client.RequestAccessToken().Wait();

            string expected = "https://www.fake_instance_url.com";
            string actual   = client.InstanceUrl;

            // ensure client.InstanceUrl has been set to the expected value
            Assert.That(expected, Is.EqualTo(actual));
        }
        public void InjectEvent_NullEventFields_ShouldThrowInvalidPlatformEventException()
        {
            MockHttpMessageHandler mockHandler = new MockHttpMessageHandler();
            SFDCClient             client      = new SFDCClient(new HttpClient(mockHandler));

            client.ClientId     = "cliend_id";
            client.ClientSecret = "client_secret";
            client.Username     = "******";
            client.Password     = "******";
            client.ApiVersion   = 10;

            string endpoint = "/services/oauth2/token";
            string fakeAccessTokenResponse = "{\"access_token\":\"fake_access_token\"," +
                                             "\"instance_url\":\"https://www.fake_instance_url.com\"," +
                                             "\"id\":\"fake_id\"," +
                                             "\"token_type\":\"fake_token_type\"," +
                                             "\"issued_at\":\"fake_issued_at\"," +
                                             "\"signature\":\"fake_signature\"}";
            TestEvent evt = new TestEvent {
                ApiName = "Test_Event_Api_Name__e"
                          // should set Fields to avoid exception
            };

            mockHandler
            .When(endpoint)
            .Respond(HttpStatusCode.OK, "application/json", fakeAccessTokenResponse);

            Assert.That(() => {
                client.RequestAccessToken().Wait();
                client.InjectEvent(evt).Wait();
            }, Throws
                        .TypeOf <AggregateException>()
                        .With.InnerException
                        .TypeOf <InvalidPlatformEventException>());
        }
        public void InjectEvent_NullApiVersion_ShouldThrowInsufficientEventInjectionException()
        {
            MockHttpMessageHandler mockHandler = new MockHttpMessageHandler();
            SFDCClient             client      = new SFDCClient(new HttpClient(mockHandler));

            client.ClientId     = "cliend_id";
            client.ClientSecret = "client_secret";
            client.Username     = "******";
            client.Password     = "******";
            // should set client.ApiVersion to avoid exception

            string    endpoint = "/services/oauth2/token";
            TestEvent evt      = new TestEvent {
                ApiName = "Test_Event_Api_Name__e",
                Fields  = new TestEventFields {
                    StringTestField = "TestFieldValue"
                }
            };

            // send empty response (not relevant to test)
            mockHandler
            .When(endpoint)
            .Respond(HttpStatusCode.OK, "application/json", "{}");

            Assert.That(() => {
                client.RequestAccessToken().Wait();
                client.InjectEvent(evt).Wait();
            }, Throws
                        .TypeOf <AggregateException>()
                        .With.InnerException
                        .TypeOf <InsufficientEventInjectionException>());
        }
        /// <summary>
        /// Injects an event into Salesforce using
        /// the supplied CLI authentication and
        /// event arguments.
        /// </summary>
        public int Init(InjectOptions o)
        {
            _AuthArgs  = CreateAuthArgs(o.AuthArgs);
            _EventArgs = CreateEventArgs(o.EventArgs);
            _Client    = CreateClient();

            try
            {
                dynamic evt = null;

                try
                {
                    evt = EventCreator.CreateEvent(
                        _EventArgs.EventClassName,
                        _EventArgs.EventFieldsClassName,
                        _EventArgs.EventFieldsPropValues);
                }
                catch (Exception e) when(
                    e is UnknownPlatformEventException ||
                    e is UnknownPlatformEventFieldsException ||
                    e is InvalidCommandLineArgumentIndexException)
                {
                    Console.WriteLine($"{e.GetType()}: {e.Message}");
                }

                try
                {
                    _Client.RequestAccessToken().Wait();
                }
                catch (Exception e) when(e is InsufficientAccessTokenRequestException)
                {
                    Console.WriteLine($"{e.GetType()}: {e.Message}");
                }

                try
                {
                    _Client.InjectEvent(evt).Wait();
                }
                catch (Exception e) when(
                    e is InsufficientEventInjectionException ||
                    e is InvalidPlatformEventException ||
                    e is EventInjectionUnsuccessfulException)
                {
                    Console.WriteLine($"{e.GetType()}: {e.Message}");
                }
            }
            catch (RuntimeBinderException e)
            {
                Console.WriteLine($"{e.GetType()}: No event was bounded to the dynamic type.");
            }

            return(0);
        }
        public void RequestAccessToken_NullClientId_ShouldThrowInsufficientAccessTokenRequestException()
        {
            SFDCClient client = new SFDCClient(new HttpClient());

            // should set client.ClientId to avoid exception.
            client.ClientSecret = "client_secret";
            client.Username     = "******";
            client.Password     = "******";
            client.ApiVersion   = 45.0;

            Assert.That(() => {
                client.RequestAccessToken().Wait();
            }, Throws
                        .TypeOf <AggregateException>()
                        .With.InnerException
                        .TypeOf <InsufficientAccessTokenRequestException>());
        }
        public void InjectEvent_UnsuccessfulInjection_ShouldThrowEventInjectionUnsuccessfulException()
        {
            MockHttpMessageHandler mockHandler = new MockHttpMessageHandler();
            SFDCClient             client      = new SFDCClient(new HttpClient(mockHandler));

            client.ClientId     = "cliend_id";
            client.ClientSecret = "client_secret";
            client.Username     = "******";
            client.Password     = "******";
            client.ApiVersion   = 10;

            string accessTokenEndpoint     = "/services/oauth2/token";
            string eventInjectionEndpoint  = "/services/data/v10.0/sobjects/Test_Event_Api_Name__e";
            string fakeAccessTokenResponse = "{\"access_token\":\"fake_access_token\"," +
                                             "\"instance_url\":\"https://www.fake_instance_url.com\"," +
                                             "\"id\":\"fake_id\"," +
                                             "\"token_type\":\"fake_token_type\"," +
                                             "\"issued_at\":\"fake_issued_at\"," +
                                             "\"signature\":\"fake_signature\"}";
            string fakeEventInjectionResponse = "{\"id\":\"fake_id\"," +
                                                "\"success\":false," + // indicates an unsuccessful injection
                                                "\"errors\":[]}";
            TestEvent evt = new TestEvent {
                ApiName = "Test_Event_Api_Name__e",
                Fields  = new TestEventFields {
                    StringTestField = "TestFieldValue"
                }
            };

            mockHandler
            .When(accessTokenEndpoint)
            .Respond(HttpStatusCode.OK, "application/json", fakeAccessTokenResponse);

            mockHandler
            .When(eventInjectionEndpoint)
            .Respond(HttpStatusCode.OK, "application/json", fakeEventInjectionResponse);

            Assert.That(() => {
                client.RequestAccessToken().Wait();
                client.InjectEvent(evt).Wait();
            }, Throws
                        .TypeOf <AggregateException>()
                        .With.InnerException
                        .TypeOf <EventInjectionUnsuccessfulException>());
        }