public async Task <bool> Execute(AddScopeParameter addScopeParameter) { var json = addScopeParameter == null ? string.Empty : JsonConvert.SerializeObject(addScopeParameter); _umaServerEventSource.StartToAddScope(json); if (addScopeParameter == null) { throw new ArgumentNullException(nameof(addScopeParameter)); } Scope scope = null; try { scope = await _scopeRepository.Get(addScopeParameter.Id); } catch (Exception ex) { throw new BaseUmaException(ErrorCodes.InternalError, ErrorDescriptions.TheScopeCannotBeRetrieved, ex); } if (scope != null) { throw new BaseUmaException(ErrorCodes.InvalidRequestCode, string.Format(ErrorDescriptions.TheScopeAlreadyExists, addScopeParameter.Id)); } scope = new Scope { Id = addScopeParameter.Id, IconUri = addScopeParameter.IconUri, Name = addScopeParameter.Name }; _scopeParameterValidator.CheckScopeParameter(scope); try { await _scopeRepository.Insert(scope); _umaServerEventSource.FinishToAddScope(json); return(true); } catch (Exception ex) { throw new BaseUmaException(ErrorCodes.InternalError, ErrorDescriptions.TheScopeCannotBeInserted, ex); } }
public async Task When_A_Scope_Is_Inserted_Then_True_Is_Returned() { // ARRANGE InitializeFakeObjects(); var addScopeParameter = new AddScopeParameter(); _scopeRepositoryStub.Setup(s => s.Get(It.IsAny <string>())) .Returns(() => Task.FromResult((Scope)null)); _scopeRepositoryStub.Setup(s => s.Insert(It.IsAny <Scope>())) .Returns(Task.FromResult(true)); // ACT var result = await _insertScopeAction.Execute(addScopeParameter); // ASSERT Assert.True(result); }
public async Task When_An_Error_Occured_While_Retrieving_Scope_Then_Exception_Is_Thrown() { // ARRANGE InitializeFakeObjects(); var addScopeParameter = new AddScopeParameter(); _scopeRepositoryStub.Setup(s => s.Get(It.IsAny <string>())) .Callback(() => { throw new Exception(); }); // ACT & ASSERTS var exception = await Assert.ThrowsAsync <BaseUmaException>(() => _insertScopeAction.Execute(addScopeParameter)); Assert.NotNull(exception); Assert.True(exception.Code == ErrorCodes.InternalError); Assert.True(exception.Message == ErrorDescriptions.TheScopeCannotBeRetrieved); }
public async Task When_Scope_Already_Exists_Then_Exception_Is_Thrown() { // ARRANGE InitializeFakeObjects(); const string scopeId = "scope_id"; var addScopeParameter = new AddScopeParameter { Id = scopeId }; _scopeRepositoryStub.Setup(s => s.Get(It.IsAny <string>())) .Returns(Task.FromResult(new Scope())); // ACT & ASSERTS var exception = await Assert.ThrowsAsync <BaseUmaException>(() => _insertScopeAction.Execute(addScopeParameter)); Assert.NotNull(exception); Assert.True(exception.Code == ErrorCodes.InvalidRequestCode); Assert.True(exception.Message == string.Format(ErrorDescriptions.TheScopeAlreadyExists, scopeId)); }
public Task <bool> InsertScope(AddScopeParameter addScopeParameter) { return(_insertScopeAction.Execute(addScopeParameter)); }