Example #1
0
 /// <summary>
 /// Creates new <see cref="DataItem"/> object.
 /// </summary>
 /// <param name="diffId">The difference identifier.</param>
 /// <param name="side">The side.</param>
 /// <param name="encodedData">The encoded data.</param>
 public static DataItem CreateDataItem(int diffId, DiffItemSide side, string encodedData) =>
 new DataItem
 {
     DifferenceId = diffId,
     Side         = side,
     Data         = encodedData
 };
Example #2
0
        public async Task SaveDataItem_DuplicatedData_ShouldThrowException()
        {
            DiffItemSide itemSide = DiffItemSide.Left;

            _differenceRepository.AnyAsync(d => d.Id == TestConstants.DiffId).ReturnsForAnyArgs(true);
            _dataItemRepository.AnyAsync(Arg.Any <Expression <Func <DataItem, bool> > >()).ReturnsForAnyArgs(true);

            await Assert.ThrowsAsync <InvalidOperationException>(() =>
                                                                 _service.SaveDataItemAsync(TestConstants.DiffId, itemSide, TestConstants.EncryptedDataA));

            await _differenceRepository.DidNotReceive().SaveChangesAsync();
        }
Example #3
0
        public async Task SaveDataItem_NewValidDataToExistingDiff_ShouldSave()
        {
            DiffItemSide itemSide = DiffItemSide.Left;

            _differenceRepository.AnyAsync(Arg.Any <Expression <Func <Difference, bool> > >()).ReturnsForAnyArgs(true);

            await _service.SaveDataItemAsync(TestConstants.DiffId, itemSide, TestConstants.EncryptedDataA);

            _dataItemRepository.DidNotReceiveWithAnyArgs().Delete(Arg.Any <int>());
            await _dataItemRepository.Received().CreateAsync(Arg.Is <DataItem>(di => di.Data == TestConstants.EncryptedDataA));

            await _dataItemRepository.Received().SaveChangesAsync();
        }
Example #4
0
        public async Task SaveDataItem_NewValidDataToNewDiff_ShouldSave()
        {
            DiffItemSide itemSide = DiffItemSide.Left;

            _differenceRepository.AnyAsync(d => d.Id == TestConstants.DiffId).ReturnsForAnyArgs(false);

            await _service.SaveDataItemAsync(TestConstants.DiffId, itemSide, TestConstants.EncryptedDataA);

            await _differenceRepository.Received().CreateAsync(Arg.Is <Difference>(d =>
                                                                                   d.Id == TestConstants.DiffId &&
                                                                                   d.DataItems.Any(di => di.Data == TestConstants.EncryptedDataA)));

            await _differenceRepository.Received().SaveChangesAsync();
        }
Example #5
0
        /// See <see cref="IDiffService.SaveDataItemAsync(int, DiffItemSide, string)"/> for more information.
        public async Task <int> SaveDataItemAsync(int diffId, DiffItemSide itemSide, string encodedData)
        {
            // Assumption: We do not want to save invalid or empty data.
            if (!EncodingHelper.IsValidBase64Data(encodedData))
            {
                throw new ArgumentException("The input data is in incorrect format", nameof(encodedData));
            }

            DataItem item;
            bool     doDiffExists = await _differenceRepository.AnyAsync(d => d.Id == diffId);

            if (!doDiffExists)
            {
                item = DataItemFactory.CreateDataItem(itemSide, encodedData);
                Difference diff = DifferenceFactory.CreateDifference(diffId, item);
                await _differenceRepository.CreateAsync(diff);

                await _differenceRepository.SaveChangesAsync();
            }
            else
            {
                // Assumption: If an item on the same side already exists, the code throws an error.
                bool doItemExists = await _dataItemRepository.AnyAsync(di => di.DifferenceId == diffId && di.Side == itemSide);

                if (doItemExists)
                {
                    throw new InvalidOperationException($"Diff with ID {diffId} already contains {itemSide.ToString().ToLower()} data item.");
                }

                item = DataItemFactory.CreateDataItem(diffId, itemSide, encodedData);
                await _dataItemRepository.CreateAsync(item);

                await _dataItemRepository.SaveChangesAsync();
            }

            return(item.Id);
        }
Example #6
0
 /// <summary>
 /// Creates new <see cref="DataItem"/> object.
 /// </summary>
 /// <param name="side">The side.</param>
 /// <param name="encodedData">The encoded data.</param>
 public static DataItem CreateDataItem(DiffItemSide side, string encodedData) =>
 CreateDataItem(default(int), side, encodedData);