public async Task GivenAnAccountDefinition_WhenICreateADataLinkBetweenTwoSubscriptions_ThenTheDataLinkShouldBePersisted() { // Setup: await using var db = ClientsDbTestProvider.CreateInMemoryClientDb(); var account = DtoProvider.CreateValidAccountDefinition(); var createAccountDelegate = new CreateAccountDelegate(db, Mapper); account = await createAccountDelegate.CreateAccountAsync(account); var dataLink = new DataLinkDto { FromSubscriptionId = 2, // Staging ToSubscriptionId = 1, // Production DataLinkTypeId = 1 // Customization }; // System under test: CreateDataLinkDelegate var createDataLinkDelegate = new CreateDataLinkDelegate(db, Mapper); // Exercise: invoke CreateDataLink var createdDataLink = await createDataLinkDelegate.CreateDataLinkAsync(account.AccountId, dataLink); // Assert createdDataLink.From.Should().NotBeNullOrEmpty(); createdDataLink.To.Should().NotBeNullOrEmpty(); createdDataLink.Type.Should().NotBeNullOrEmpty().And.Be("Customization"); db.DataLinks.Count().Should().Be(1); }
public virtual async Task <DataLinkDto> CreateDataLinkAsync(int accountId, DataLinkDto dataLinkDto) { try { var prefetch = await PrefetchAndValidate(accountId, dataLinkDto); var dataLink = new DataLink { FromSubscriptionId = dataLinkDto.FromSubscriptionId, From = prefetch.From, ToSubscriptionId = dataLinkDto.ToSubscriptionId, To = prefetch.To, DataLinkTypeId = dataLinkDto.DataLinkTypeId, Type = prefetch.Type }; _db.DataLinks.Add(dataLink); await _db.SaveChangesAsync(); return(_mapper.Map <DataLinkDto>(dataLink)); } catch (DbException e) { throw new PersistenceException($"An error occurred while creating the DataLink ({nameof(accountId)} = {accountId}, {nameof(dataLinkDto)} = {JsonConvert.SerializeObject(dataLinkDto)})", e); } }
public async Task GivenADataLinkOperation_WhenICreateAnExistingDataLink_ThenTheDelegateShouldRaiseAnError() { // Setup: await using var db = ClientsDbTestProvider.CreateInMemoryClientDb(); var account = DtoProvider.CreateValidAccountDefinition(); var createAccountDelegate = new CreateAccountDelegate(db, Mapper); account = await createAccountDelegate.CreateAccountAsync(account); var dataLink = new DataLinkDto { FromSubscriptionId = 2, // Staging ToSubscriptionId = 1, // Production DataLinkTypeId = 1 // Customization }; // System under test: CreateDataLinkDelegate var createDataLinkDelegate = new CreateDataLinkDelegate(db, Mapper); // Exercise: invoke CreateDataLink twice try { await createDataLinkDelegate.CreateDataLinkAsync(account.AccountId, dataLink); await createDataLinkDelegate.CreateDataLinkAsync(account.AccountId, dataLink); Assert.Fail($"An invocation to {nameof(CreateDataLinkDelegate.CreateDataLinkAsync)} should not have completed when a duplicate data link."); } catch (Exception e) { // Assert // Exception is the right type e.GetType().Should().Be <MalformedDataLinkException>(); e.Message.Should().Be($"An existing DataLink from subscription with SubscriptionId = 2 to subscription with SubscriptionId = 1 already exists."); // Only one data link is persisted. db.DataLinks.Count().Should().Be(1); } }
public async Task GivenADataLinkOperation_WhenIPassAnInvalidAccount_ThenTheDelegateShouldRaiseAnError() { // Setup: await using var db = ClientsDbTestProvider.CreateInMemoryClientDb(); var account = DtoProvider.CreateValidAccountDefinition(); var createAccountDelegate = new CreateAccountDelegate(db, Mapper); account = await createAccountDelegate.CreateAccountAsync(account); var dataLink = new DataLinkDto { FromSubscriptionId = 2, // Staging ToSubscriptionId = 1, // Production DataLinkTypeId = 1 // Customization }; // System under test: CreateDataLinkDelegate var createDataLinkDelegate = new CreateDataLinkDelegate(db, Mapper); // Exercise: invoke CreateDataLink twice try { await createDataLinkDelegate.CreateDataLinkAsync(-1, dataLink); Assert.Fail($"An invocation to {nameof(CreateDataLinkDelegate.CreateDataLinkAsync)} should not have completed with an invalid {nameof(AccountDto.AccountId)}."); } catch (Exception e) { // Assert // Exception is the right type e.GetType().Should().Be <AccountNotFoundException>(); e.Message.Should().Be($"An account with AccountId = -1 doesn't exist."); // Nothing persisted db.DataLinks.Count().Should().Be(0); } }
//[AuthorizeRbac("dataLinks:write")] public async Task <IActionResult> CreateDataLink(int accountId, int subscriptionId, [FromBody] DataLinkDto dataLink) { dataLink.FromSubscriptionId = subscriptionId; return(Ok(await _createDataLink.CreateDataLinkAsync(accountId, dataLink))); }
private async Task <PrefetchDataLinkResult> PrefetchAndValidate(int accountId, DataLinkDto dataLinkDto) { var errors = new List <ValidationResult>(); Utils.ValidateDto(dataLinkDto, errors); Utils.ThrowAggregateExceptionOnValidationErrors(errors); var doesAccountExistFuture = (from a in _db.Accounts where a.AccountId == accountId select 1).DeferredAny().FutureValue(); var dataLinkTypeFuture = (from t in _db.DataLinkTypes where t.DataLinkTypeId == dataLinkDto.DataLinkTypeId select t).DeferredFirstOrDefault().FutureValue(); var existingDataLink = ( from l in _db.DataLinks where l.FromSubscriptionId == dataLinkDto.FromSubscriptionId && l.ToSubscriptionId == dataLinkDto.ToSubscriptionId select 1 ).DeferredAny().FutureValue(); var subscriptionIdsFuture = ( from s in _db.Subscriptions where s.AccountId == accountId && (s.SubscriptionId == dataLinkDto.FromSubscriptionId || s.SubscriptionId == dataLinkDto.ToSubscriptionId) select s ).Future(); if (!await doesAccountExistFuture.ValueAsync()) { throw new AccountNotFoundException($"An account with AccountId = {accountId} doesn't exist."); } if (await existingDataLink.ValueAsync()) { throw new MalformedDataLinkException($"An existing DataLink from subscription with SubscriptionId = {dataLinkDto.FromSubscriptionId} to subscription with SubscriptionId = {dataLinkDto.ToSubscriptionId} already exists."); } var dataLinkType = await dataLinkTypeFuture.ValueAsync(); if (dataLinkType == null) { throw new DataLinkTypeNotFoundException($"A data link type with DataLinkTypeId = {dataLinkDto.DataLinkTypeId} could not be found."); } var subscriptions = await subscriptionIdsFuture.ToListAsync(); var fromSubscription = subscriptions.FirstOrDefault(s => s.SubscriptionId == dataLinkDto.FromSubscriptionId); var toSubscription = subscriptions.FirstOrDefault(s => s.SubscriptionId == dataLinkDto.ToSubscriptionId); if (fromSubscription == null) { throw new MalformedSubscriptionException($"A subscription with SubscriptionId = {dataLinkDto.FromSubscriptionId} does not exists inside Account with AccountId {accountId}"); } if (toSubscription == null) { throw new MalformedSubscriptionException($"A subscription with SubscriptionId = {dataLinkDto.ToSubscriptionId} does not exists inside Account with AccountId {accountId}"); } return(new PrefetchDataLinkResult { From = fromSubscription, To = toSubscription, Type = dataLinkType }); }