public void PoolOneThreadScenario() { const int iterations = 100; const int initialCount = 5; var pool = new ThirdPartyPool(initialCount, 50); var item = pool.TakeInstance(); pool.Release(item); Assert.AreEqual(initialCount, pool.TotalCount); Assert.AreEqual(initialCount, pool.CurrentCount); ThrowsAssert.Throws <ArgumentException>(() => pool.Release(new ThirdPartyPool(1, 1).TakeInstance())); ThrowsAssert.Throws <InvalidOperationException>(() => pool.Release(item)); for (var i = 0; i < iterations; i++) { using (var slot = pool.TakeInstance()) { Assert.IsFalse(slot.Object.Flag); slot.Object.Flag = true; Assert.AreEqual(initialCount, pool.TotalCount); Assert.AreEqual(initialCount - 1, pool.CurrentCount); } } Assert.AreEqual(initialCount, pool.TotalCount); Assert.AreEqual(initialCount, pool.CurrentCount); }
public void PoolFlushTimout() { var pool = new ThirdPartyPool(0, 2); pool.FlushTimeOut = 1; var insance1 = pool.TakeInstance(); var insance2 = pool.TakeInstance(); // вернули только один pool.Release(insance1); ThrowsAssert.Throws <TimeoutException>(() => pool.TryFlush()); var pool2 = new ThirdPartyPool(0, 2); pool.FlushTimeOut = 1; var insance = pool.TakeInstance(); pool.Release(insance1); Action <ThirdParty> longTimeDestroy = obj => { Thread.Sleep(35000); }; // слишком длительная очистка объекта ThrowsAssert.Throws <TimeoutException>(() => pool.TryFlush(longTimeDestroy)); }
public void ThrowArgumentException_WhenTheTournamentPlaceIsNullOrEmpty() { // Arrange var expectedTournament = new Tournament { Name = "Test name", Place = String.Empty, StartDate = DateTime.Now, EndDate = DateTime.Now, CountryId = Guid.NewGuid() }; var yoyoTournamentsDbContextMock = new Mock <IYoyoTournamentsDbContext>(); var divisionTypeService = new Mock <IDivisionTypeService>(); var tournamentService = new TournamentService(yoyoTournamentsDbContextMock.Object, divisionTypeService.Object); // Act & Assert ThrowsAssert.Throws <ArgumentException>( () => tournamentService.CreateTournament(expectedTournament.Name, expectedTournament.Place, expectedTournament.StartDate, expectedTournament.EndDate, expectedTournament.CountryId) ); }
public void MSTest_MathUtils_TestedWithExtensions_NullCollectionPassed_ShouldThrowArgumentException() { var util = new MathUtils(); List <int> numbers = null; ThrowsAssert.Throws <ArgumentNullException>(() => util.Sum(numbers)); }
public void UpdateEntityWithUriPointingToNonExistingEntityThrowsHttpRequestException() { Mock.Arrange(() => _authenticationProvider.GetAuthValue()) .Returns(SAMPLE_BEARER_TOKEN) .OccursOnce(); Mock.Arrange(() => _authenticationProvider.GetAuthScheme()) .Returns(BEARER_AUTH_TYPE) .OccursOnce(); var headers = new Dictionary <String, String>(); headers.Add("Authorization", SAMPLE_BEARER_TOKEN); var mockedRestCallExecutor = Mock.Create <RestCallExecutor>(); Mock.Arrange(() => mockedRestCallExecutor.Invoke(HttpMethod.Put, SAMPLE_ENTITY_URI.ToString(), Arg.Is(headers), SAMPLE_ENTITY)) .IgnoreInstance() .Throws <HttpRequestException>() .OccursOnce(); var entityController = new EntityController(_authenticationProvider); ThrowsAssert.Throws <HttpRequestException>(() => entityController.UpdateEntity(SAMPLE_ENTITY_URI, SAMPLE_ENTITY)); Mock.Assert(_authenticationProvider); Mock.Assert(mockedRestCallExecutor); }
public void AddStudent_WhenStudentAlreadyExists_ShouldThrowInvalidOperationExceptionWithCorrectMessage() { var student = new Student("Petar", 98765u); var ex = ThrowsAssert.Throws <InvalidOperationException>(() => School.AddStudent(student)); StringAssert.Contains(GlobalErrorMessages.CannodAddSameStudentTwiceErrorMessage, ex.Message); }
public void ThrowArgumentOutOfRangeException_WhenTheTournamentEndDateIsLessThenStartDate() { // Arrange var expectedTournament = new Tournament { Name = "Test name", Place = "Test place", StartDate = DateTime.Now.Add(new TimeSpan(2, 0, 0, 0, 0)), EndDate = DateTime.Now.Add(new TimeSpan(1, 0, 0, 0, 0)), CountryId = Guid.NewGuid() }; var yoyoTournamentsDbContextMock = new Mock <IYoyoTournamentsDbContext>(); var divisionTypeService = new Mock <IDivisionTypeService>(); var tournamentService = new TournamentService(yoyoTournamentsDbContextMock.Object, divisionTypeService.Object); // Act & Assert ThrowsAssert.Throws <ArgumentOutOfRangeException>( () => tournamentService.CreateTournament(expectedTournament.Name, expectedTournament.Place, expectedTournament.StartDate, expectedTournament.EndDate, expectedTournament.CountryId) ); }
public void ThrowArgumentException_WhenTheListWIthDivisionTypesIsNullOrEmpty() { // Arrange var expectedTournament = new Tournament { Name = "Test name", Place = "Test place", StartDate = DateTime.Now.Add(new TimeSpan(1, 0, 0, 0, 0)), EndDate = DateTime.Now.Add(new TimeSpan(2, 0, 0, 0, 0)), CountryId = Guid.NewGuid() }; var divisionTypeDbSetMock = QueryableDbSetMock.GetQueryableMockDbSet(new List <DivisionType>()); var yoyoTournamentsDbContextMock = new Mock <IYoyoTournamentsDbContext>(); yoyoTournamentsDbContextMock.Setup(x => x.DivisionTypes).Returns(divisionTypeDbSetMock.Object); var divisionTypeServiceMock = new Mock <IDivisionTypeService>(); divisionTypeServiceMock.Setup(x => x.GetAllDivisionTypes()).Returns(new List <DivisionType>()); var tournamentService = new TournamentService(yoyoTournamentsDbContextMock.Object, divisionTypeServiceMock.Object); // Act & Assert ThrowsAssert.Throws <NullReferenceException>( () => tournamentService.CreateTournament(expectedTournament.Name, expectedTournament.Place, expectedTournament.StartDate, expectedTournament.EndDate, expectedTournament.CountryId) ); }
public void ThrowException_WhenDbContextIsNull() { // Arrange var divisionTypeServiceMock = new Mock <IDivisionTypeService>(); // Assert ThrowsAssert.Throws <ArgumentNullException>(() => new TournamentService(null, divisionTypeServiceMock.Object)); }
public void ThrowException_WhenDivisionTypeServiceIsNull() { // Arrange var yoyoTournamentsDbContextMock = new Mock <IYoyoTournamentsDbContext>(); // Assert ThrowsAssert.Throws <ArgumentNullException>(() => new TournamentService(yoyoTournamentsDbContextMock.Object, null)); }
public void RemoveStudent_WhenStudentIsNull_ShouldThrowArgumentExceptionWithCorrectMessage() { var course = new Course(); var ex = ThrowsAssert.Throws <ArgumentException>(() => course.RemoveStudent(null)); StringAssert.Contains(GlobalErrorMessages.CannotRemoveNullStudentErrorMessage, ex.Message); }
public void Ctor_CreateWithExistingNumber_ShouldThrowInvalidOperationExceptionWithCorrectMessage() { School.ListOfStudents.Clear(); var student = new Student("Petar", 21365u); var ex = ThrowsAssert.Throws <InvalidOperationException>(() => new Student("Petar", 21365u)); StringAssert.Contains(GlobalErrorMessages.CannodAddStudentWithSameNumberTwiceErrorMessage, ex.Message); }
public void ShouldThrowNullArgument_WhenNoLoggerIsSupplied() { // Arrange var mockedManger = new Mock <IFileManager>(); // Act and assert ThrowsAssert.Throws <ArgumentNullException>(() => new ServiceMapper(mockedManger.Object, null)); }
public void AddStudent_WhenStudentIsNull_ShouldThrowArgumentNullExceptionWithCorrectMessage() { School.ListOfStudents.Clear(); var course = new Course(); var ex = ThrowsAssert.Throws <ArgumentNullException>(() => course.AddStudent(null)); StringAssert.Contains(ex.Message, "student"); }
public void ThrowArgumentNullException_WhenDivisionTypeService() { // Arrange var tournamentServiceMock = new Mock <ITournamentService>(); // Act & Assert ThrowsAssert.Throws <ArgumentNullException>( () => new TournamentController(tournamentServiceMock.Object, null) ); }
public void LifeCycleManagerConstructorLoadedInvalidStateMachineConfigurationThrowsException() { Mock.Arrange(() => _stateMachineConfigLoader.LoadConfiguration(ENTITY_TYPE)) .IgnoreInstance() .Returns("Invalid state machine configuration") .MustBeCalled(); ThrowsAssert.Throws <ArgumentException>(() => new LifeCycleManager(_authenticationProvider, ENTITY_TYPE), "Invalid state machine configuration"); Mock.Assert(_stateMachineConfigLoader); }
public void ThrowArgumentException_WhenTheUserIdIsNullOrEmpty() { // Arrange Guid divisionId = Guid.NewGuid(); var yoyoTournamentsDbContextMock = new Mock <IYoyoTournamentsDbContext>(); var divisionService = new DivisionService(yoyoTournamentsDbContextMock.Object); // Act & Assert ThrowsAssert.Throws <ArgumentException>(() => divisionService.AddUserToDivision(String.Empty, divisionId)); }
public void AddStudent_WhenStudentAlreadyExists_ShouldThrowInvalidOperationExceptionWithCorrectMessage() { School.ListOfStudents.Clear(); var student = new Student("Petar", 12345u); var course = new Course(); course.AddStudent(student); var ex = ThrowsAssert.Throws <InvalidOperationException>(() => course.AddStudent(student)); StringAssert.Contains(GlobalErrorMessages.CannodAddSameStudentTwiceErrorMessage, ex.Message); }
public void ShouldThrowNullArgumentWithSpecificMessage_WhenNoFileManagerIsSupplied() { // Arrange var mockedLogger = new Mock <ILogger>(); // Act var exceptionThrown = ThrowsAssert .Throws <ArgumentNullException>(() => new ServiceMapper(null, mockedLogger.Object)); // Assert StringAssert.Contains(exceptionThrown.Message.ToLower(), "filemanager is null".ToLower()); }
public void Creates_CompondDocument_for_collectione_of_metadatawrapper_throws_notSupportedException() { // Arrange var configuration = CreateContext(); var objectsToTransform = new List <MetaDataWrapper <SampleClass> > { new MetaDataWrapper <SampleClass>(new SampleClass()) }; var sut = new JsonApiTransformer(); // Act => Assert ThrowsAssert.Throws <NotSupportedException>(() => sut.Transform(objectsToTransform, configuration)); }
public void AddMoreThan30Students_ShouldThrowInvalidOperationExceptionWithCorrectMessage() { School.ListOfStudents.Clear(); var course = new Course(); for (uint i = 0; i < 31; i++) { course.AddStudent(new Student("Student " + i, 12000 + i)); } var ex = ThrowsAssert.Throws <InvalidOperationException>(() => course.AddStudent(new Student("Petar", 21365u))); StringAssert.Contains(GlobalErrorMessages.StudentsCannotBeMoreThan30ErrorMessage, ex.Message); }
public void GetIdentityThrowsArgumentNullExceptionIfApplicationNameNotDefined() { Mock.Arrange(() => HttpContext.Current.User.Identity.Name) .Returns(USERNAME) .OccursOnce(); Mock.Arrange(() => ConfigurationManager.AppSettings) .Returns(new NameValueCollection()) .OccursOnce(); ThrowsAssert.Throws <ArgumentNullException>(() => CurrentUserDataProvider.GetIdentity(TENANT_ID)); Mock.Assert(() => HttpContext.Current.User.Identity.Name); Mock.Assert(() => ConfigurationManager.AppSettings); }
public void RequestStateChangeForLockedEntityThrowsInvalidOperationException() { Mock.Arrange(() => _coreService.StateChangeLocks) .IgnoreInstance() .ReturnsCollection(new List <StateChangeLock>(new List <StateChangeLock> { CreateStateChangeLock(SAMPLE_ENTITY_URI) })) .MustBeCalled(); var lifeCycleManager = new LifeCycleManager(_authenticationProvider, ENTITY_TYPE); ThrowsAssert.Throws <InvalidOperationException>(() => lifeCycleManager. RequestStateChange(SAMPLE_ENTITY_URI, SAMPLE_ENTITY, CONTINUE_CONDITION, TENANT_ID)); Mock.Assert(_coreService); }
public void SemaphoreSingleThreadTest() { var semaphore = new LockFreeSemaphore(0, 3); Assert.AreEqual(0, semaphore.CurrentCount); semaphore.Release(); Assert.AreEqual(1, semaphore.CurrentCount); semaphore.Release(2); Assert.AreEqual(3, semaphore.CurrentCount); ThrowsAssert.Throws <SemaphoreFullException>(semaphore.Release); Assert.IsTrue(semaphore.TryTake()); Assert.AreEqual(2, semaphore.CurrentCount); Assert.IsTrue(semaphore.TryTake()); Assert.IsTrue(semaphore.TryTake()); Assert.AreEqual(0, semaphore.CurrentCount); Assert.IsFalse(semaphore.TryTake()); Assert.AreEqual(0, semaphore.CurrentCount); }
public void OnAllowCallbackForPreCalloutRevertsTransactionAndThrowsInvalidOperationExceptionIfChangingStateFails() { Mock.Arrange(() => _coreService.Jobs) .IgnoreInstance() .ReturnsCollection(new List <Job>(new List <Job> { CreateJob(SAMPLE_ENTITY_URI.ToString()) })) .InSequence() .OccursOnce(); Mock.Arrange(() => _coreService.UpdateObject(Arg.IsAny <Job>())) .IgnoreInstance() .InSequence() .OccursOnce(); Mock.Arrange(() => _entityController.LoadEntity(SAMPLE_ENTITY_URI)) .IgnoreInstance() .Returns(UPDATED_ENTITY) .MustBeCalled(); Mock.Arrange(() => _coreService.CalloutDefinitions) .IgnoreInstance() .ReturnsCollection(new List <CalloutDefinition>(new List <CalloutDefinition> { CreateCalloutDefinition(SAMPLE_ENTITY_URI.ToString(), Model.CalloutDefinition.CalloutDefinitionType.Post.ToString()) })) .MustBeCalled(); Mock.Arrange(() => _coreService.SaveChanges()) .IgnoreInstance() .Occurs(3); Mock.Arrange(() => _entityController.UpdateEntity(SAMPLE_ENTITY_URI, UPDATED_ENTITY)) .IgnoreInstance() .Throws <HttpRequestException>() .OccursOnce(); var stateChangeLockToBeDeleted = CreateStateChangeLock(SAMPLE_ENTITY_URI); Mock.Arrange(() => _coreService.StateChangeLocks) .IgnoreInstance() .ReturnsCollection(new List <StateChangeLock>(new List <StateChangeLock> { stateChangeLockToBeDeleted })) .InSequence() .MustBeCalled(); Mock.Arrange(() => _coreService.DeleteObject(stateChangeLockToBeDeleted)) .IgnoreInstance() .OccursOnce(); var lifeCycleManager = new LifeCycleManager(_authenticationProvider, ENTITY_TYPE); lifeCycleManager._calloutExecutor = _calloutExecutor; ThrowsAssert.Throws <InvalidOperationException>(() => lifeCycleManager.OnAllowCallback(CreateJob(SAMPLE_ENTITY_URI.ToString()))); Mock.Assert(_coreService); Mock.Assert(_calloutExecutor); Mock.Assert(_entityController); }
public void OnAllowCallbackForPreCalloutRevertsTransactionAndThrowsInvalidOperationExceptionIfPostCalloutFails() { Job createdJob = null; Job updatedJob = null; Mock.Arrange(() => CurrentUserDataProvider.GetCurrentUsername()) .Returns("Administrator") .MustBeCalled(); Mock.Arrange(() => _coreService.Jobs) .IgnoreInstance() .ReturnsCollection(new List <Job>(new List <Job> { CreateJob(SAMPLE_ENTITY_URI.ToString()) })) .InSequence() .OccursOnce(); Mock.Arrange(() => _coreService.UpdateObject(Arg.IsAny <Job>())) .IgnoreInstance() .InSequence() .OccursOnce(); Mock.Arrange(() => _entityController.LoadEntity(SAMPLE_ENTITY_URI)) .IgnoreInstance() .Returns(SAMPLE_ENTITY) .MustBeCalled(); Mock.Arrange(() => HttpContext.Current.User.Identity) .Returns(_windowsIdentity) .MustBeCalled(); Mock.Arrange(() => CredentialCache.DefaultCredentials) .MustBeCalled(); Mock.Arrange(() => _windowsIdentity.Impersonate()) .MustBeCalled(); Mock.Arrange(() => _coreService.CalloutDefinitions) .IgnoreInstance() .ReturnsCollection(new List <CalloutDefinition>(new List <CalloutDefinition> { CreateCalloutDefinition(SAMPLE_ENTITY_URI.ToString(), Model.CalloutDefinition.CalloutDefinitionType.Post.ToString()) })) .MustBeCalled(); Mock.Arrange(() => _coreService.SaveChanges()) .IgnoreInstance() .Occurs(4); Mock.Arrange(() => _entityController.UpdateEntity(SAMPLE_ENTITY_URI, UPDATED_ENTITY)) .IgnoreInstance() .InSequence() .OccursOnce(); Mock.Arrange(() => _coreService.AddToJobs(Arg.IsAny <Job>())) .IgnoreInstance() .DoInstead((Job j) => { createdJob = j; }) .MustBeCalled(); Mock.Arrange(() => _calloutExecutor.ExecuteCallout(CALLOUT_DEFINITION, Arg.IsAny <CalloutData>())) .Throws <HttpRequestException>() .MustBeCalled(); Mock.Arrange(() => _coreService.Jobs) .IgnoreInstance() .ReturnsCollection(new List <Job>(new List <Job> { CreateJob(SAMPLE_ENTITY_URI.ToString(), false) })) .InSequence() .OccursOnce(); Mock.Arrange(() => _coreService.UpdateObject(Arg.IsAny <Job>())) .IgnoreInstance() .DoInstead((Job j) => { updatedJob = j; }) .InSequence() .OccursOnce(); Mock.Arrange(() => _entityController.UpdateEntity(SAMPLE_ENTITY_URI, SAMPLE_ENTITY)) .IgnoreInstance() .OccursOnce(); var stateChangeLockToBeDeleted = CreateStateChangeLock(SAMPLE_ENTITY_URI); Mock.Arrange(() => _coreService.StateChangeLocks) .IgnoreInstance() .ReturnsCollection(new List <StateChangeLock>(new List <StateChangeLock> { stateChangeLockToBeDeleted })) .InSequence() .MustBeCalled(); Mock.Arrange(() => _coreService.DeleteObject(stateChangeLockToBeDeleted)) .IgnoreInstance() .OccursOnce(); var lifeCycleManager = new LifeCycleManager(_authenticationProvider, ENTITY_TYPE); lifeCycleManager._calloutExecutor = _calloutExecutor; ThrowsAssert.Throws <InvalidOperationException>(() => lifeCycleManager.OnAllowCallback(CreateJob(SAMPLE_ENTITY_URI.ToString()))); Assert.AreEqual(JobStateEnum.Failed.ToString(), updatedJob.State); Assert.AreEqual(EXPECTED_POST_CALLOUT_DATA, createdJob.Parameters); Assert.AreEqual(SAMPLE_ENTITY_URI.ToString(), createdJob.ReferencedItemId); Assert.AreEqual(JobStateEnum.Running.ToString(), createdJob.State); Assert.AreEqual(CALLOUT_JOB_TYPE, createdJob.Type); Assert.AreEqual(TENANT_ID, createdJob.TenantId); Mock.Assert(_coreService); Mock.Assert(_calloutExecutor); Mock.Assert(_entityController); Mock.Assert(() => CurrentUserDataProvider.GetCurrentUsername()); Mock.Assert(() => HttpContext.Current.User.Identity); Mock.Assert(CredentialCache.DefaultCredentials); Mock.Assert(_windowsIdentity); }
public void RequestStateChangeForNonLockedEntityRevertsTransactionAndThrowsInvalidOperationExceptionIfPreCalloutFails() { Job updatedJob = null; Mock.Arrange(() => CurrentUserDataProvider.GetCurrentUsername()) .Returns("Administrator") .MustBeCalled(); Mock.Arrange(() => _coreService.StateChangeLocks) .IgnoreInstance() .ReturnsCollection(new List <StateChangeLock>(new List <StateChangeLock> { CreateStateChangeLock(SAMPLE_ENTITY_URI_2) })) .InSequence() .MustBeCalled(); Mock.Arrange(() => HttpContext.Current.User.Identity) .Returns(_windowsIdentity) .MustBeCalled(); Mock.Arrange(() => CredentialCache.DefaultCredentials) .MustBeCalled(); Mock.Arrange(() => _windowsIdentity.Impersonate()) .MustBeCalled(); Mock.Arrange(() => _coreService.CalloutDefinitions) .IgnoreInstance() .ReturnsCollection(new List <CalloutDefinition>(new List <CalloutDefinition> { CreateCalloutDefinition(SAMPLE_ENTITY_URI.ToString(), Model.CalloutDefinition.CalloutDefinitionType.Pre.ToString()) })) .MustBeCalled(); Mock.Arrange(() => _coreService.AddToStateChangeLocks(Arg.IsAny <StateChangeLock>())) .IgnoreInstance() .MustBeCalled(); Mock.Arrange(() => _coreService.SaveChanges()) .IgnoreInstance() .Occurs(4); Mock.Arrange(() => _coreService.AddToJobs(Arg.IsAny <Job>())) .IgnoreInstance() .MustBeCalled(); Mock.Arrange(() => _calloutExecutor.ExecuteCallout(CALLOUT_DEFINITION, Arg.IsAny <CalloutData>())) .Throws <HttpRequestException>() .MustBeCalled(); Mock.Arrange(() => _coreService.Jobs) .IgnoreInstance() .ReturnsCollection(new List <Job>(new List <Job> { CreateJob(SAMPLE_ENTITY_URI.ToString()) })) .MustBeCalled(); Mock.Arrange(() => _coreService.UpdateObject(Arg.IsAny <Job>())) .IgnoreInstance() .DoInstead((Job j) => { updatedJob = j; }) .MustBeCalled(); var stateChangeLockToBeDeleted = CreateStateChangeLock(SAMPLE_ENTITY_URI); Mock.Arrange(() => _coreService.StateChangeLocks) .IgnoreInstance() .ReturnsCollection(new List <StateChangeLock>(new List <StateChangeLock> { stateChangeLockToBeDeleted })) .InSequence() .MustBeCalled(); Mock.Arrange(() => _coreService.DeleteObject(stateChangeLockToBeDeleted)) .IgnoreInstance() .OccursOnce(); var lifeCycleManager = new LifeCycleManager(_authenticationProvider, ENTITY_TYPE); lifeCycleManager._calloutExecutor = _calloutExecutor; ThrowsAssert.Throws <InvalidOperationException>(() => lifeCycleManager. RequestStateChange(SAMPLE_ENTITY_URI, SAMPLE_ENTITY, CONTINUE_CONDITION, TENANT_ID)); Assert.AreEqual(JobStateEnum.Failed.ToString(), updatedJob.State); Mock.Assert(_coreService); Mock.Assert(_calloutExecutor); Mock.Assert(() => CurrentUserDataProvider.GetCurrentUsername()); Mock.Assert(() => HttpContext.Current.User.Identity); Mock.Assert(CredentialCache.DefaultCredentials); Mock.Assert(_windowsIdentity); }
public void ThrowException_WhenDbContextIsNull() { // Arrange & Act & Assert ThrowsAssert.Throws <ArgumentNullException>(() => new DivisionService(null)); }
public void Ctor_CreateWithLargerThanValidNumber_ShouldThrowArgumentExceptionWithCorrectMessage() { var ex = ThrowsAssert.Throws <ArgumentException>(() => new Student("Georgi", 100000)); StringAssert.Contains(GlobalErrorMessages.StudentNumberNotInRange, ex.Message); }
public void AddStudent_WhenStudentIsNull_ShouldThrowInvalidOperationExceptionWithCorrectMessage() { var ex = ThrowsAssert.Throws <InvalidOperationException>(() => School.AddStudent(null)); StringAssert.Contains(GlobalErrorMessages.CannotAddNullStudentErrorMessage, ex.Message); }