// PUT: api/Utilities.svc/LifeCycles(5) public async Task <IHttpActionResult> Put([FromODataUri] String key, LifeCycle lifeCycle) { var declaringType = System.Reflection.MethodBase.GetCurrentMethod().DeclaringType; var fn = String.Format("{0}:{1}", declaringType.Namespace, declaringType.Name); if (!ModelState.IsValid) { Debug.WriteLine("Entity to be changed by LifeCycleManager with id '{0}' has invalid ModelState.", key); return(BadRequest(ModelState)); } if (key != lifeCycle.Id) { return(BadRequest()); } try { Debug.WriteLine(fn); var identity = CurrentUserDataProvider.GetIdentity(TenantId); var permissionId = CreatePermissionId("CanUpdate"); if (!identity.Permissions.Contains(permissionId)) { return(StatusCode(HttpStatusCode.Forbidden)); } var entityUri = new Uri(key); var entity = LoadEntity(new DefaultAuthenticationProvider(), entityUri); var lifeCycleManager = new LifeCycleManager(new DefaultAuthenticationProvider(), ExtractTypeFromUriString(key)); lifeCycleManager.RequestStateChange(entityUri, entity, lifeCycle.Condition, identity.Tid); return(Ok()); } catch (UriFormatException e) { return(BadRequest("Invalid Id - Id has to be a valid URI")); } catch (HttpRequestException e) { return(BadRequest("Loading entity from passed Uri failed (Either not found or not authorized)")); } catch (InvalidOperationException e) { return(BadRequest(String.Format("Changing state with provided condition: '{0}' not possible", lifeCycle.Condition))); } catch (Exception e) { Debug.WriteLine(String.Format("{0}: {1}\r\n{2}", e.Source, e.Message, e.StackTrace)); throw; } }
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 RequestStateChangeForNonLockedEntityWithoutPreCalloutDefinitionChangesStateAndExecutesPostCallout() { Job createdJob = 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) })) .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.AddToStateChangeLocks(Arg.IsAny <StateChangeLock>())) .IgnoreInstance() .MustBeCalled(); Mock.Arrange(() => _coreService.SaveChanges()) .IgnoreInstance() .MustBeCalled(); Mock.Arrange(() => _entityController.UpdateEntity(SAMPLE_ENTITY_URI, UPDATED_ENTITY)) .IgnoreInstance() .MustBeCalled(); Mock.Arrange(() => _coreService.AddToJobs(Arg.IsAny <Job>())) .IgnoreInstance() .DoInstead((Job j) => { createdJob = j; }) .MustBeCalled(); Mock.Arrange(() => _coreService.Jobs) .IgnoreInstance() .ReturnsCollection(new List <Job>(new List <Job> { CreateJob(SAMPLE_ENTITY_URI.ToString()) })) .MustBeCalled(); Mock.Arrange(() => _calloutExecutor.ExecuteCallout(CALLOUT_DEFINITION, Arg.IsAny <CalloutData>())) .MustBeCalled(); var lifeCycleManager = new LifeCycleManager(_authenticationProvider, ENTITY_TYPE); lifeCycleManager._calloutExecutor = _calloutExecutor; lifeCycleManager.RequestStateChange(SAMPLE_ENTITY_URI, SAMPLE_ENTITY, CONTINUE_CONDITION, TENANT_ID); 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()); }
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 RequestStateChangeForNonLockedEntityLocksEntity() { Mock.Arrange(() => CurrentUserDataProvider.GetCurrentUsername()) .Returns("Administrator") .MustBeCalled(); Mock.Arrange(() => _coreService.StateChangeLocks) .IgnoreInstance() .ReturnsCollection(new List <StateChangeLock>(new List <StateChangeLock> { CreateStateChangeLock(SAMPLE_ENTITY_URI_2) })) .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() .MustBeCalled(); Mock.Arrange(() => _coreService.AddToJobs(Arg.IsAny <Job>())) .IgnoreInstance() .MustBeCalled(); Mock.Arrange(() => _coreService.Jobs) .IgnoreInstance() .ReturnsCollection(new List <Job>(new List <Job> { CreateJob(SAMPLE_ENTITY_URI.ToString()) })) .MustBeCalled(); Mock.Arrange(() => _calloutExecutor.ExecuteCallout(CALLOUT_DEFINITION, Arg.IsAny <CalloutData>())) .MustBeCalled(); var lifeCycleManager = new LifeCycleManager(_authenticationProvider, ENTITY_TYPE); lifeCycleManager._calloutExecutor = _calloutExecutor; lifeCycleManager.RequestStateChange(SAMPLE_ENTITY_URI, SAMPLE_ENTITY, CONTINUE_CONDITION, TENANT_ID); Mock.Assert(_coreService); Mock.Assert(_calloutExecutor); Mock.Assert(() => CurrentUserDataProvider.GetCurrentUsername()); Mock.Assert(() => HttpContext.Current.User.Identity); Mock.Assert(CredentialCache.DefaultCredentials); Mock.Assert(_windowsIdentity); }
public async Task<IHttpActionResult> Patch([FromODataUri] String key, Delta<LifeCycle> delta) { var declaringType = System.Reflection.MethodBase.GetCurrentMethod().DeclaringType; var fn = String.Format("{0}:{1}", declaringType.Namespace, declaringType.Name); if (!ModelState.IsValid) { Debug.WriteLine("Entity to be changed by LifeCycleManager with id '{0}' has invalid ModelState.", key); return BadRequest(ModelState); } try { Debug.WriteLine(fn); var identity = CurrentUserDataProvider.GetIdentity(TenantId); var permissionId = CreatePermissionId("CanUpdate"); if (!identity.Permissions.Contains(permissionId)) { return StatusCode(HttpStatusCode.Forbidden); } var entityUri = new Uri(key); var entity = LoadEntity(new DefaultAuthenticationProvider(), entityUri); var lifeCycleManager = new LifeCycleManager(new DefaultAuthenticationProvider(), ExtractTypeFromUriString(key)); lifeCycleManager.RequestStateChange(entityUri, entity, delta.GetEntity().Condition, identity.Tid); return Ok(); } catch (UriFormatException e) { return BadRequest("Invalid Id - Id has to be a valid URI"); } catch (HttpRequestException e) { return BadRequest("Loading entity from passed Uri failed (Either not found or not authorized)"); } catch (InvalidOperationException e) { return BadRequest(String.Format("Chaning state with provided condition: '{0}' not possible", delta.GetEntity().Condition)); } catch (Exception e) { Debug.WriteLine(String.Format("{0}: {1}\r\n{2}", e.Source, e.Message, e.StackTrace)); throw; } }
public void RequestStateChangeForNonLockedEntityWithoutPreCalloutDefinitionChangesStateAndExecutesPostCallout() { Job createdJob = 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) })) .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.AddToStateChangeLocks(Arg.IsAny<StateChangeLock>())) .IgnoreInstance() .MustBeCalled(); Mock.Arrange(() => _coreService.SaveChanges()) .IgnoreInstance() .MustBeCalled(); Mock.Arrange(() => _entityController.UpdateEntity(SAMPLE_ENTITY_URI, UPDATED_ENTITY)) .IgnoreInstance() .MustBeCalled(); Mock.Arrange(() => _coreService.AddToJobs(Arg.IsAny<Job>())) .IgnoreInstance() .DoInstead((Job j) => { createdJob = j; }) .MustBeCalled(); Mock.Arrange(() => _coreService.Jobs) .IgnoreInstance() .ReturnsCollection(new List<Job>(new List<Job> { CreateJob(SAMPLE_ENTITY_URI.ToString()) })) .MustBeCalled(); Mock.Arrange(() => _calloutExecutor.ExecuteCallout(CALLOUT_DEFINITION, Arg.IsAny<CalloutData>())) .MustBeCalled(); var lifeCycleManager = new LifeCycleManager(_authenticationProvider, ENTITY_TYPE); lifeCycleManager._calloutExecutor = _calloutExecutor; lifeCycleManager.RequestStateChange(SAMPLE_ENTITY_URI, SAMPLE_ENTITY, CONTINUE_CONDITION, TENANT_ID); 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()); }
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 RequestStateChangeForNonLockedEntityLocksEntity() { Mock.Arrange(() => CurrentUserDataProvider.GetCurrentUsername()) .Returns("Administrator") .MustBeCalled(); Mock.Arrange(() => _coreService.StateChangeLocks) .IgnoreInstance() .ReturnsCollection(new List<StateChangeLock>(new List<StateChangeLock> { CreateStateChangeLock(SAMPLE_ENTITY_URI_2) })) .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() .MustBeCalled(); Mock.Arrange(() => _coreService.AddToJobs(Arg.IsAny<Job>())) .IgnoreInstance() .MustBeCalled(); Mock.Arrange(() => _coreService.Jobs) .IgnoreInstance() .ReturnsCollection(new List<Job>(new List<Job>{CreateJob(SAMPLE_ENTITY_URI.ToString())})) .MustBeCalled(); Mock.Arrange(() => _calloutExecutor.ExecuteCallout(CALLOUT_DEFINITION, Arg.IsAny<CalloutData>())) .MustBeCalled(); var lifeCycleManager = new LifeCycleManager(_authenticationProvider, ENTITY_TYPE); lifeCycleManager._calloutExecutor = _calloutExecutor; lifeCycleManager.RequestStateChange(SAMPLE_ENTITY_URI, SAMPLE_ENTITY, CONTINUE_CONDITION, TENANT_ID); 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 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); }