コード例 #1
0
        public async Task PostDiffDataShouldUpdateLeftDataWhenLeftDataAlreadyExists()
        {
            // Arrange
            var someValidId             = int.MaxValue;
            var dataToBeUpdated         = new byte[] { 9, 8, 7, 6, 5, 4, 3, 2, 1 };
            var base64StringToBeUpdated = Convert.ToBase64String(dataToBeUpdated);

            // Act
            IHttpActionResult insertResponse = await _diffController.PostLeftDiffData(someValidId, _leftBase64StringValidData);

            IHttpActionResult updatedResponse = await _diffController.PostLeftDiffData(someValidId, base64StringToBeUpdated);

            // Assert
            Assert.IsInstanceOfType(insertResponse, typeof(OkResult));
            Assert.IsInstanceOfType(updatedResponse, typeof(OkResult));

            // Only the updated record should be present.
            Assert.AreEqual(1, _repository.DiffDataCount());

            InMemoryDiffData savedData = _repository.GetById(someValidId);

            // The record in db should be retrieved with the provided id via API call.
            Assert.IsNotNull(savedData);

            // The Left data stored should be the same as provided in the second API call.
            Assert.IsTrue(dataToBeUpdated.SequenceEqual(savedData.Left));

            // Right should not contains any data, since the record was updated with Left data only.
            Assert.AreEqual(null, savedData.Right);
        }
コード例 #2
0
        public async Task PostDiffDataShouldInsertOnlyLeftDataWhenCallToLeftDataIsValid()
        {
            // Arrange
            var someValidId  = int.MaxValue;
            var validData    = new byte[] { 1, 2, 3, 4, 5 };
            var diffAnaliser = new DiffAnalyser();

            // Act
            IHttpActionResult response = await _diffController.PostLeftDiffData(someValidId, _leftBase64StringValidData);

            // Assert
            Assert.IsInstanceOfType(response, typeof(OkResult));
            // Only the newly created record should be present.
            Assert.AreEqual(1, _repository.DiffDataCount());

            InMemoryDiffData savedData = _repository.GetById(someValidId);

            // The record in db should be retrieved with the provided id via API call.
            Assert.IsNotNull(savedData);

            // The left data stored should be the same as provided via API call.
            Assert.IsTrue(validData.SequenceEqual(savedData.Left));

            // Right should not contains any data, since the record was created
            // and only left data was provided.
            Assert.AreEqual(null, savedData.Right);
        }
コード例 #3
0
        public async Task PostDiffDataShouldInsertTwoRecordsWhenProvidedIdsAreDifferents()
        {
            // Arrange
            var firstRecordId  = 1;
            var secondRecordId = 2;

            // Act
            IHttpActionResult firstRecordResponse = await _diffController.PostLeftDiffData(firstRecordId, _leftBase64StringValidData);

            IHttpActionResult secondRecordResponse = await _diffController.PostLeftDiffData(secondRecordId, _rightBase64StringValidData);

            // Assert
            Assert.IsInstanceOfType(firstRecordResponse, typeof(OkResult));
            Assert.IsInstanceOfType(secondRecordResponse, typeof(OkResult));

            // Database need contains the 2 inserted records.
            Assert.AreEqual(2, _repository.DiffDataCount());

            InMemoryDiffData firstSavedData = _repository.GetById(firstRecordId);

            // The record in db should be retrieved with the provided id(firstRecordId) via API call.
            Assert.IsNotNull(firstSavedData);

            InMemoryDiffData secondSavedData = _repository.GetById(secondRecordId);

            // The record in db should be retrieved with the provided id(secondRecordId) via API call.
            Assert.IsNotNull(secondSavedData);

            // Both records should contains left data.
            Assert.IsTrue(_leftValidData.SequenceEqual(firstSavedData.Left));
            Assert.IsTrue(_rightValidData.SequenceEqual(secondSavedData.Left));
        }
コード例 #4
0
        public async Task PostDiffDataShouldUpdateRightDataWhenLeftDataAlreadyExists()
        {
            // Arrange
            var someValidId = int.MaxValue;

            // Act
            IHttpActionResult insertLeftResponse = await _diffController.PostLeftDiffData(someValidId, _leftBase64StringValidData);

            IHttpActionResult updatedRightResponse = await _diffController.PostRightDiffData(someValidId, _rightBase64StringValidData);

            // Assert
            Assert.IsInstanceOfType(insertLeftResponse, typeof(OkResult));
            Assert.IsInstanceOfType(updatedRightResponse, typeof(OkResult));

            // Only the updated record should be present.
            Assert.AreEqual(1, _repository.DiffDataCount());

            InMemoryDiffData savedData = _repository.GetById(someValidId);

            // The record in db should be retrieved with the provided id via API call.
            Assert.IsNotNull(savedData);

            // The left and the right data should be populated.
            // The first API call will create the record and the second API call will update the same record.
            Assert.IsTrue(_leftValidData.SequenceEqual(savedData.Left));
            Assert.IsTrue(_rightValidData.SequenceEqual(savedData.Right));
        }
コード例 #5
0
        /// <summary>
        /// Save the data to storage, adding the data if not present
        /// or updating the same if it already exists.
        /// </summary>
        /// <param name="id">Identifier of left/right blocks to be persisted</param>
        /// <param name="data">Data to be saved.</param>
        /// <param name="itemType">Define if the data being saved is related to left or right.</param>
        public Task Save(int id, string data, DiffItemType itemType)
        {
            byte[] binaryData = Convert.FromBase64String(data);

            // There isn't any invalid possible id, so any received id not present in
            // database will generate a new entry.
            InMemoryDiffData diffData = _repository.GetById(id);

            if (diffData == null)
            {
                diffData = new InMemoryDiffData {
                    Id = id
                };
            }

            switch (itemType)
            {
            case DiffItemType.Left:
                diffData.Left = binaryData;
                break;

            case DiffItemType.Right:
                diffData.Right = binaryData;
                break;

            default:
                throw new ArgumentOutOfRangeException(nameof(itemType));
            }

            _repository.Save(diffData);

            // Here a Task is being returned just in order to alow
            // other methods to simulate a async call to some IO resource.
            return(Task.CompletedTask);
        }
コード例 #6
0
        /// <summary>
        /// Persists the data checking if the provided data already exists in order to insert or update.
        /// </summary>
        /// <param name="data">Diff data to be persisted.</param>
        public static void AddOrUpdate(InMemoryDiffData data)
        {
            if (data == null)
            {
                throw new ArgumentNullException(nameof(data));
            }

            _fakeDb.AddOrUpdate(data.Id, data, (key, existingData) =>
            {
                existingData.Left  = data.Left;
                existingData.Right = data.Right;
                return(existingData);
            });
        }