Example #1
0
        public void RequestStateChange(Uri entityUri, String entity, String condition, String tenantId)
        {
            Debug.WriteLine("Got state change request for entity with URI: '{0}' and condition: '{1}'", entityUri, condition);

            CheckForExistingStateChangeLock(entityUri);
            var preCalloutDefinition = LoadCalloutDefinition(condition, entityUri, tenantId,
                                                             Model.CalloutDefinition.CalloutDefinitionType.Pre.ToString());

            CreateStateChangeLockForEntity(entityUri);

            String token = null;

            if (null == preCalloutDefinition)
            {
                DoPostCallout(entityUri, entity, condition, tenantId);
            }
            else
            {
                try
                {
                    var calloutData = CreatePreCalloutData(entityUri, entity, condition);
                    token = CreateJob(entityUri, tenantId, calloutData);
                    lock (_lock)
                    {
                        _calloutExecutor.ExecuteCallout(preCalloutDefinition.Parameters, calloutData);
                    }
                }
                catch (Exception e)
                {
                    Debug.WriteLine("An error occurred while preparing or executing pre '{0}' callout for entity with id '{1}'",
                                    condition,
                                    entityUri.ToString());
                    if (null != token)
                    {
                        ChangeJobState(token, JobStateEnum.Failed);
                    }
                    DeleteStateChangeLockOfEntity(entityUri);
                    throw new InvalidOperationException(e.Message);
                }
            }
        }
        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);
        }