Exemplo n.º 1
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();
        }
Exemplo n.º 2
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);
        }