public void OnDeclineCallbackForPostCalloutRevertsActionSetsJobToCanceledAndUnlocksEntity()
        {
            Mock.Arrange(() => _entityController.LoadEntity(SAMPLE_ENTITY_URI))
            .IgnoreInstance()
            .Returns(UPDATED_ENTITY)
            .MustBeCalled();

            Mock.Arrange(() => _entityController.UpdateEntity(SAMPLE_ENTITY_URI, SAMPLE_ENTITY))
            .IgnoreInstance()
            .OccursOnce();

            Job updatedJob = null;

            Mock.Arrange(() => _coreService.Jobs)
            .IgnoreInstance()
            .ReturnsCollection(new List <Job>(new List <Job> {
                CreateJob(SAMPLE_ENTITY_URI.ToString(), false)
            }))
            .OccursOnce();

            Mock.Arrange(() => _coreService.UpdateObject(Arg.IsAny <Job>()))
            .IgnoreInstance()
            .DoInstead((Job j) => { updatedJob = j; })
            .OccursOnce();

            Mock.Arrange(() => _coreService.SaveChanges())
            .IgnoreInstance()
            .Occurs(2);

            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.OnDeclineCallback(CreateJob(SAMPLE_ENTITY_URI.ToString(), false));

            Assert.AreEqual(JobStateEnum.Canceled.ToString(), updatedJob.State);

            Mock.Assert(_coreService);
            Mock.Assert(_entityController);
        }
Esempio n. 2
0
        public async Task <IHttpActionResult> Decline([FromODataUri] String token, ODataActionParameters parameters)
        {
            var declaringType = System.Reflection.MethodBase.GetCurrentMethod().DeclaringType;
            var fn            = String.Format("{0}:{1}",
                                              declaringType.Namespace,
                                              declaringType.Name);

            try
            {
                Debug.WriteLine(fn);
                var identity = CurrentUserDataProvider.GetIdentity(TenantId);

                var permissionId = CreatePermissionId("CanDecline");
                if (!identity.Permissions.Contains(permissionId))
                {
                    return(StatusCode(HttpStatusCode.Forbidden));
                }

                var job = _coreService.Jobs.Where(j => token == j.Token &&
                                                  CALLOUT_JOB_TYPE == j.Type &&
                                                  j.State == JobStateEnum.Running.ToString())
                          .SingleOrDefault();

                if (null == job)
                {
                    return(StatusCode(HttpStatusCode.NotFound));
                }

                var calloutDefinition = JsonConvert.DeserializeObject <CalloutData>(job.Parameters);
                var lifeCycleManager  = new LifeCycleManager(new DefaultAuthenticationProvider(), calloutDefinition.EntityType);
                lifeCycleManager.OnDeclineCallback(job);

                return(Ok());
            }
            catch (InvalidOperationException e)
            {
                return(BadRequest(String.Format("Decline job with token: '{0} not possible", token)));
            }
            catch (Exception e)
            {
                Debug.WriteLine(String.Format("{0}: {1}\r\n{2}", e.Source, e.Message, e.StackTrace));
                throw;
            }
        }
        public async Task<IHttpActionResult> Decline([FromODataUri] String token, ODataActionParameters parameters)
        {
            var declaringType = System.Reflection.MethodBase.GetCurrentMethod().DeclaringType;
            var fn = String.Format("{0}:{1}",
                declaringType.Namespace,
                declaringType.Name);

            try
            {
                Debug.WriteLine(fn);
                var identity = CurrentUserDataProvider.GetIdentity(TenantId);

                var permissionId = CreatePermissionId("CanDecline");
                if (!identity.Permissions.Contains(permissionId))
                {
                    return StatusCode(HttpStatusCode.Forbidden);
                }

                var job = _coreService.Jobs.Where(j => token == j.Token && 
                    CALLOUT_JOB_TYPE == j.Type &&
                    j.State == JobStateEnum.Running.ToString())
                    .SingleOrDefault();

                if (null == job)
                {
                    return StatusCode(HttpStatusCode.NotFound);
                }

                var calloutDefinition = JsonConvert.DeserializeObject<CalloutData>(job.Parameters);
                var lifeCycleManager = new LifeCycleManager(new DefaultAuthenticationProvider(), calloutDefinition.EntityType);
                lifeCycleManager.OnDeclineCallback(job);

                return Ok();
            }
            catch (InvalidOperationException e)
            {
                return BadRequest(String.Format("Decline job with token: '{0} not possible", token));
            }
            catch (Exception e)
            {
                Debug.WriteLine(String.Format("{0}: {1}\r\n{2}", e.Source, e.Message, e.StackTrace));
                throw;
            }
        }
        public void OnDeclineCallbackForPostCalloutRevertsActionSetsJobToCanceledAndUnlocksEntity()
        {
            Mock.Arrange(() => _entityController.LoadEntity(SAMPLE_ENTITY_URI))
                .IgnoreInstance()
                .Returns(UPDATED_ENTITY)
                .MustBeCalled();

            Mock.Arrange(() => _entityController.UpdateEntity(SAMPLE_ENTITY_URI, SAMPLE_ENTITY))
                .IgnoreInstance()
                .OccursOnce();

            Job updatedJob = null;
            Mock.Arrange(() => _coreService.Jobs)
                .IgnoreInstance()
                .ReturnsCollection(new List<Job>(new List<Job> { CreateJob(SAMPLE_ENTITY_URI.ToString(), false) }))
                .OccursOnce();

            Mock.Arrange(() => _coreService.UpdateObject(Arg.IsAny<Job>()))
                .IgnoreInstance()
                .DoInstead((Job j) => { updatedJob = j; })
                .OccursOnce();

            Mock.Arrange(() => _coreService.SaveChanges())
                .IgnoreInstance()
                .Occurs(2);

            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.OnDeclineCallback(CreateJob(SAMPLE_ENTITY_URI.ToString(), false));

            Assert.AreEqual(JobStateEnum.Canceled.ToString(), updatedJob.State);

            Mock.Assert(_coreService);
            Mock.Assert(_entityController);
        }