public void EntityConverter_ConvertLinkedObjectToJson_Succeeds()
        {
            // Create Object
            var newInvoice = new ComplexEntity
            {
                Currency    = "EUR",
                OrderDate   = new DateTime(2012, 10, 26),
                InvoiceTo   = new Guid("3734121e-1544-4b77-9ae2-7203e9bd6046"),
                Journal     = "50",
                OrderedBy   = new Guid("3734121e-1544-4b77-9ae2-7203e9bd6046"),
                Description = "NewInvoiceForEntityWithCollection"
            };

            var newInvoiceLine = new ComplexEntityLine
            {
                Description = "NewInvoiceForEntityWithCollection",
                Item        = new Guid("4f68481a-7a2c-4fbc-a3a0-0c494df3fa0d")
            };

            var invoicelines = new List <ComplexEntityLine> {
                newInvoiceLine
            };

            newInvoice.Lines = invoicelines;

            var    entityConverter = new EntityConverter();
            string json            = entityConverter.ConvertObjectToJson(newInvoice, null);
            string expected        = JsonFileReader.GetJsonFromFileWithoutWhiteSpace("Expected_Json_Object_ComplexEntity_WithLinkedEntity.txt");

            Assert.AreEqual(expected, json);
        }
        public void EntityConverter_ConvertExistingLinkedObjectToJson_Succeeds()
        {
            // Create Object
            var newInvoice = new SalesInvoice {
                InvoiceID = new Guid("4f68481a-7a2c-4fbc-a3a0-0c494df3fa0d")
            };
            var newInvoiceLine = new SalesInvoiceLine {
                Description = "NewInvoiceForEntityWithCollection"
            };

            newInvoice.SalesInvoiceLines = new List <SalesInvoiceLine> {
                newInvoiceLine
            };

            //ControllerSingleton.GetInstance(new MockObjects.MAPIConnector_Controller(), "www.dummy.com/");
            var entityController = new EntityController(newInvoice, "ID", "4f68481a-7a2c-4fbc-a3a0-0c494df3fa0d", new MockObjects.ApiConnectionMock(), null);

            newInvoiceLine.Description = "ChangedNewInvoiceForEntityWithCollection";

            var    entityConverter    = new EntityConverter();
            var    controllerDelegate = new GetEntityController(GetEntityController);
            string json = entityConverter.ConvertObjectToJson((SalesInvoice)entityController.OriginalEntity, newInvoice, controllerDelegate);

            const string expected = "{\"SalesInvoiceLines\": [{\"Description\": \"ChangedNewInvoiceForEntityWithCollection\"}]}";

            Assert.AreEqual(expected, json);

            throw new NotImplementedException();
        }
        public void EntityConverter_ConvertObjectToJson_ForCreating_Succeeds()
        {
            // Test if objects is converted to json correctly
            var dateTimeEpoc = new DateTime(1970, 1, 1, 0, 0, 0, 0);

            #region Client Object and Json

            var simpleEntity = new SimpleEntity()
            {
                Code            = "123",
                Description     = "FOO",
                Id              = new Guid("53697fab-137f-4242-b710-0139886b50f4"),
                StartDate       = dateTimeEpoc.AddMilliseconds(1387188617287),
                EndDate         = null,
                Boolean         = true,
                NullableBoolean = null,
                Integer         = 5,
                NullableInteger = null
            };

            const string expected = "{\"Code\":\"123\",\"Description\":\"FOO\",\"Id\":\"53697fab-137f-4242-b710-0139886b50f4\",\"StartDate\":\"2013-12-16T10:10:17.287\",\"EndDate\":null,\"Boolean\":true,\"NullableBoolean\":null,\"Integer\":5,\"NullableInteger\":null}";
            #endregion

            var    converter = new EntityConverter();
            string result    = converter.ConvertObjectToJson(simpleEntity, null);

            Assert.AreEqual(expected, result);
        }
        public void EntityConverter_ConvertObject_WithJsonPropertyAttributeSpecifyingDifferentPropertyToJson_SerializesToCorrectJson()
        {
            var bankaccount = new BankAccount();

            bankaccount.BankAccountName = "value";
            const string jsonValue = @"{""BankAccount"":""value""}";

            string json = _entityConverter.ConvertObjectToJson(new BankAccount(), bankaccount, null);

            Assert.AreEqual(jsonValue, json);
        }
        public void EntityConverter_ConvertObjectToJson_ForAlteredFields_Succeeds()
        {
            var account = new Account {
                Name = "New Account"
            };
            var          entityConverter = new EntityConverter();
            const string expected        = "{\"Name\":\"New Account\"}";

            var result = entityConverter.ConvertObjectToJson(new Account(), account, null);

            Assert.AreEqual(expected, result);
        }
        public void EntityConverter_ConvertObjectToJson_WithReadonlyFields_Succeeds()
        {
            var newAccount = new Account {
                AccountManagerHID = 10
            };
            var          entityConverter = new EntityConverter();
            const string expected        = "";

            var result = entityConverter.ConvertObjectToJson(new Account(), newAccount, null);

            Assert.AreEqual(expected, result);
        }
        public void EntityConverter_CreateWithGuid_Succeeds()
        {
            var newAccount = new Account {
                ID = new Guid("8f8b8025-90b3-4307-a8a3-a5111d048fb5")
            };
            var          entityConverter = new EntityConverter();
            const string expected        = "{\"ID\":\"8f8b8025-90b3-4307-a8a3-a5111d048fb5\"}";

            var result = entityConverter.ConvertObjectToJson(new Account(), newAccount, null);

            Assert.AreEqual(expected, result);
        }
        public void EntityConverter_CreateWithoutGuid_Succeeds()
        {
            var newAccount = new Account {
                ID = new Guid()
            };
            var          entityConverter = new EntityConverter();
            const string expected        = "";

            var result = entityConverter.ConvertObjectToJson(new Account(), newAccount, null);

            Assert.AreEqual(expected, result);
        }
        public void EntityConverter_ConvertObjectToJson_ForNoAlteredFields_Succeeds()
        {
            var oldAccount = new Account {
                Name = "New Account"
            };
            var newAccount = new Account {
                Name = "New Account"
            };
            var          entityConverter = new EntityConverter();
            const string expected        = "";

            var result = entityConverter.ConvertObjectToJson(oldAccount, newAccount, null);

            Assert.AreEqual(expected, result);
        }
示例#10
0
        /// <summary>
        /// Updates the entity
        /// </summary>
        /// <returns>True if the entity is updated</returns>
        public Boolean Update(object entity)
        {
            // Convert object to json
            var    converter = new EntityConverter();
            string json      = converter.ConvertObjectToJson(OriginalEntity, entity, _entityControllerDelegate);

            // Update entire object
            Boolean returnValue = false;

            // Update and set _originalentity to current entity (_entity)
            if (_connection.Put(_keyName, _identifier, json))
            {
                returnValue    = true;
                OriginalEntity = Clone(entity);
            }
            return(returnValue);
        }
        public void EntityConverter_ConvertEmptyLinkedObjectToJson_Succeeds()
        {
            // Create Object
            var newInvoice = new ComplexEntity
            {
                Currency    = "EUR",
                OrderDate   = new DateTime(2012, 10, 26),
                InvoiceTo   = new Guid("3734121e-1544-4b77-9ae2-7203e9bd6046"),
                Journal     = "50",
                OrderedBy   = new Guid("3734121e-1544-4b77-9ae2-7203e9bd6046"),
                Description = "NewInvoiceForEntityWithCollection"
            };

            var    entityConverter = new EntityConverter();
            string json            = entityConverter.ConvertObjectToJson(newInvoice, null);
            string expected        = JsonFileReader.GetJsonFromFileWithoutWhiteSpace("Expected_Json_Object_ComplexEntity_WithEmptyLinkedEntities.txt");

            Assert.AreEqual(expected, json);
        }
        /// <summary>
        /// Creates an entity in Exact Online
        /// </summary>
        /// <param name="entity">Entity to create</param>
        /// <returns>True if succeed</returns>
        public Boolean Create(ref T entity)
        {
            var supportedActions = GetSupportedActions(entity);

            if (!supportedActions.CanCreate)
            {
                throw new Exception("Cannot create entity. Entity does not support creation. Please see the Reference Documentation.");
            }

            // Get Json code
            var created     = false;
            var converter   = new EntityConverter();
            var emptyEntity = Activator.CreateInstance <T>();
            var json        = converter.ConvertObjectToJson(emptyEntity, entity, _entityControllerDelegate);

            // Send to API
            var response = _conn.Post(json);

            if (!response.Contains("error"))
            {
                created = true;

                // Set values of API in account entity (to ensure GUID is set)
                response = ApiResponseCleaner.GetJsonObject(response);
                var ec = new EntityConverter();
                entity = ec.ConvertJsonToObject <T>(response);

                // Try to add the entity to the managed entities collections
                if (!AddEntityToManagedEntitiesCollection(entity))
                {
                    throw new Exception("This entity already exists");
                }

                // Check if the endpoint supports a read action. Some endpoints such as PrintQuotation only support create (POST).
                if (supportedActions.CanRead)
                {
                    // Get entity with linked entities (API Response for creating does not return the linked entities)
                    entity = GetEntity(GetIdentifierValue(entity), _expandfield);
                }
            }
            return(created);
        }
        /// <summary>
        /// Creates an entity in Exact Online
        /// </summary>
        /// <param name="entity">Entity to create</param>
        /// <returns>True if succeed</returns>
        public Boolean Create(ref T entity)
        {
            if (!IsCreateable(entity))
            {
                throw new Exception("Cannot create entity. Entity does not support creation. Please see the Reference Documentation.");
            }

            // Get Json code
            var created     = false;
            var converter   = new EntityConverter();
            var emptyEntity = Activator.CreateInstance <T>();
            var json        = converter.ConvertObjectToJson(emptyEntity, entity, _entityControllerDelegate);

            // Send to API
            var response = _conn.Post(json);

            if (!response.Contains("error"))
            {
                created = true;

                // Set values of API in account entity (to ensure GUID is set)
                response = ApiResponseCleaner.GetJsonObject(response);
                var ec = new EntityConverter();
                entity = ec.ConvertJsonToObject <T>(response);

                // Try to add the entity to the managed entities collections
                if (!AddEntityToManagedEntitiesCollection(entity))
                {
                    throw new Exception("This entity already exists");
                }

                /* IGNORE the additional GET request until it is resolved properly by the API team (https://github.com/exactonline/exactonline-api-dotnet-client/issues/9)
                 * // Get entity with linked entities (API Response for creating does not return the linked entities)
                 * entity = GetEntity(GetIdentifierValue(entity), _expandfield);
                 */
            }
            return(created);
        }
示例#14
0
        /// <summary>
        /// Updates the entity
        /// </summary>
        /// <returns>True if the entity is updated</returns>
        public async Task <Boolean> UpdateAsync(object entity)
        {
            // Convert object to json
            var    converter = new EntityConverter();
            string json      = converter.ConvertObjectToJson(OriginalEntity, entity, _entityControllerDelegate);

            if (string.IsNullOrEmpty(json))
            {
                //Nothing to update
                return(true);
            }

            // Update entire object
            Boolean returnValue = false;

            // Update and set _originalentity to current entity (_entity)
            if (await _connection.PutAsync(_keyName, _identifier, json))
            {
                returnValue    = true;
                OriginalEntity = Clone(entity);
            }
            return(returnValue);
        }
示例#15
0
        public void EntityConverter_CreateWithGuid_Succeeds()
        {
            var newAccount = new Account { ID = new Guid("8f8b8025-90b3-4307-a8a3-a5111d048fb5") };
            var entityConverter = new EntityConverter();
            const string expected = "{\"ID\":\"8f8b8025-90b3-4307-a8a3-a5111d048fb5\"}";

            var result = entityConverter.ConvertObjectToJson(new Account(), newAccount, null);

            Assert.AreEqual(expected, result);
        }
示例#16
0
        public void EntityConverter_ConvertExistingLinkedObjectToJson_Succeeds()
        {
            // Create Object
            var newInvoice = new SalesInvoice {InvoiceID = new Guid("4f68481a-7a2c-4fbc-a3a0-0c494df3fa0d")};
            var newInvoiceLine = new SalesInvoiceLine {Description = "NewInvoiceForEntityWithCollection"};
            newInvoice.SalesInvoiceLines = new List<SalesInvoiceLine> { newInvoiceLine };

            //ControllerSingleton.GetInstance(new MockObjects.MAPIConnector_Controller(), "www.dummy.com/");
            var entityController = new EntityController(newInvoice, "ID", "4f68481a-7a2c-4fbc-a3a0-0c494df3fa0d", new MockObjects.ApiConnectionMock(), null);
            newInvoiceLine.Description = "ChangedNewInvoiceForEntityWithCollection";

            var entityConverter = new EntityConverter();
            var controllerDelegate = new GetEntityController(GetEntityController);
            string json = entityConverter.ConvertObjectToJson((SalesInvoice)entityController.OriginalEntity, newInvoice, controllerDelegate);

            const string expected = "{\"SalesInvoiceLines\": [{\"Description\": \"ChangedNewInvoiceForEntityWithCollection\"}]}";
            Assert.AreEqual(expected, json);

            throw new NotImplementedException();
        }
示例#17
0
        public void EntityConverter_ConvertEmptyLinkedObjectToJson_Succeeds()
        {
            // Create Object
            var newInvoice = new ComplexEntity
                {
                    Currency = "EUR",
                    OrderDate = new DateTime(2012, 10, 26),
                    InvoiceTo = new Guid("3734121e-1544-4b77-9ae2-7203e9bd6046"),
                    Journal = "50",
                    OrderedBy = new Guid("3734121e-1544-4b77-9ae2-7203e9bd6046"),
                    Description = "NewInvoiceForEntityWithCollection"
                };

            var entityConverter = new EntityConverter();
            string json = entityConverter.ConvertObjectToJson(newInvoice, null);
            string expected = JsonFileReader.GetJsonFromFileWithoutWhiteSpace("Expected_Json_Object_ComplexEntity_WithEmptyLinkedEntities.txt");
            Assert.AreEqual(expected, json);
        }
示例#18
0
        public void EntityConverter_ConvertLinkedObjectToJson_Succeeds()
        {
            // Create Object
            var newInvoice = new ComplexEntity
                {
                    Currency = "EUR",
                    OrderDate = new DateTime(2012, 10, 26),
                    InvoiceTo = new Guid("3734121e-1544-4b77-9ae2-7203e9bd6046"),
                    Journal = "50",
                    OrderedBy = new Guid("3734121e-1544-4b77-9ae2-7203e9bd6046"),
                    Description = "NewInvoiceForEntityWithCollection"
                };

            var newInvoiceLine = new ComplexEntityLine
                {
                    Description = "NewInvoiceForEntityWithCollection",
                    Item = new Guid("4f68481a-7a2c-4fbc-a3a0-0c494df3fa0d")
                };

            var invoicelines = new List<ComplexEntityLine> { newInvoiceLine };
            newInvoice.Lines = invoicelines;

            var entityConverter = new EntityConverter();
            string json = entityConverter.ConvertObjectToJson(newInvoice, null);
            string expected = JsonFileReader.GetJsonFromFileWithoutWhiteSpace("Expected_Json_Object_ComplexEntity_WithLinkedEntity.txt");
            Assert.AreEqual(expected, json);
        }
示例#19
0
        public void EntityConverter_ConvertObjectToJson_ForAlteredFields_Succeeds()
        {
            var account = new Account{ Name = "New Account"};
            var entityConverter = new EntityConverter();
            const string expected = "{\"Name\":\"New Account\"}";

            var result = entityConverter.ConvertObjectToJson(new Account(), account, null);

            Assert.AreEqual(expected, result);
        }
示例#20
0
        public void EntityConverter_ConvertObjectToJson_ForCreating_Succeeds()
        {
            // Test if objects is converted to json correctly
            var dateTimeEpoc = new DateTime(1970, 1, 1, 0, 0, 0, 0);

            #region Client Object and Json

            var simpleEntity = new SimpleEntity()
            {
                Code = "123",
                Description = "FOO",
                Id = new Guid("53697fab-137f-4242-b710-0139886b50f4"),
                StartDate = dateTimeEpoc.AddMilliseconds(1387188617287),
                EndDate = null,
                Boolean = true,
                NullableBoolean = null,
                Integer = 5,
                NullableInteger = null
            };

            const string expected = "{\"Code\":\"123\",\"Description\":\"FOO\",\"Id\":\"53697fab-137f-4242-b710-0139886b50f4\",\"StartDate\":\"2013-12-16T10:10:17.287\",\"EndDate\":null,\"Boolean\":true,\"NullableBoolean\":null,\"Integer\":5,\"NullableInteger\":null}";
            #endregion

            var converter = new EntityConverter();
            string result = converter.ConvertObjectToJson(simpleEntity, null);

            Assert.AreEqual(expected, result);
        }
示例#21
0
        public void EntityConverter_ConvertObjectToJson_ForNoAlteredFields_Succeeds()
        {
            var oldAccount = new Account { Name = "New Account" };
            var newAccount = new Account { Name = "New Account" };
            var entityConverter = new EntityConverter();
            const string expected = "";

            var result = entityConverter.ConvertObjectToJson(oldAccount, newAccount, null);

            Assert.AreEqual(expected, result);
        }
示例#22
0
        /// <summary>
        /// Updates the entity
        /// </summary>
        /// <returns>True if the entity is updated</returns>
        public Boolean Update(object entity)
        {
            // Convert object to json
            var converter = new EntityConverter();
            string json = converter.ConvertObjectToJson(OriginalEntity, entity, _entityControllerDelegate);

            // Update entire object
            Boolean returnValue = false;

            // Update and set _originalentity to current entity (_entity)
            if (_connection.Put(_keyName, _identifier, json))
            {
                returnValue = true;
                OriginalEntity = Clone(entity);
            }
            return returnValue;
        }
示例#23
0
        public void EntityConverter_ConvertObjectToJson_WithReadonlyFields_Succeeds()
        {
            var newAccount = new Account { AccountManagerHID = 10 };
            var entityConverter = new EntityConverter();
            const string expected = "";

            var result = entityConverter.ConvertObjectToJson(new Account(), newAccount, null);

            Assert.AreEqual(expected, result);
        }
示例#24
0
        public void EntityConverter_CreateWithoutGuid_Succeeds()
        {
            var newAccount = new Account { ID = new Guid() };
            var entityConverter = new EntityConverter();
            const string expected = "";

            var result = entityConverter.ConvertObjectToJson(new Account(), newAccount, null);

            Assert.AreEqual(expected, result);
        }