Esempio n. 1
0
        public void Resolve_CannotExtractId_ReturnsNull()
        {
            // Arrange
            var key        = "test-flow";
            var instanceId = new FormFlowInstanceId("the-instance", new RouteValueDictionary());
            var stateType  = typeof(TestState);
            var state      = new TestState();

            var stateProvider = new Mock <IUserInstanceStateProvider>();

            stateProvider
            .Setup(s => s.GetInstance(instanceId))
            .Returns(FormFlowInstance.Create(stateProvider.Object, key, instanceId, stateType, state, properties: new Dictionary <object, object>()));

            var instanceResolver = new InstanceResolver(stateProvider.Object);

            var httpContext = new DefaultHttpContext();

            httpContext.Request.QueryString = new QueryString($"?");

            var routeData = new RouteData();

            var actionDescriptor = new ActionDescriptor();

            actionDescriptor.SetProperty(new FormFlowDescriptor(key, stateType, IdGenerationSource.RandomId));

            var actionContext = new ActionContext(httpContext, routeData, actionDescriptor);

            // Act
            var result = instanceResolver.Resolve(actionContext);

            // Assert
            Assert.Null(result);
        }
        public FormFlowInstance CreateInstance(
            string key,
            FormFlowInstanceId instanceId,
            Type stateType,
            object state,
            IReadOnlyDictionary <object, object> properties)
        {
            _instances.Add(instanceId, new Entry()
            {
                Key        = key,
                StateType  = stateType,
                State      = state,
                Properties = properties
            });

            var instance = FormFlowInstance.Create(
                this,
                key,
                instanceId,
                stateType,
                state,
                properties ?? new Dictionary <object, object>());

            return(instance);
        }
        public FormFlowInstance GetInstance(FormFlowInstanceId instanceId)
        {
            _instances.TryGetValue(instanceId, out var entry);

            var instance = entry != null?
                           FormFlowInstance.Create(this, entry.Key, instanceId, entry.StateType, entry.State, entry.Properties) :
                               null;

            return(instance);
        }
        protected FormFlowInstance <TState> GetFormFlowInstanceForRouteParameters <TState>(
            string key,
            IReadOnlyDictionary <string, object> routeParameters)
        {
            var instanceId = FormFlowInstanceId.GenerateForRouteValues(key, routeParameters);

            var instanceStateProvider = Factory.Services.GetRequiredService <IUserInstanceStateProvider>();

            return((FormFlowInstance <TState>)instanceStateProvider.GetInstance(instanceId));
        }
        protected FormFlowInstance <TState> CreateFormFlowInstanceForRouteParameters <TState>(
            string key,
            IReadOnlyDictionary <string, object> routeParameters,
            TState state,
            IReadOnlyDictionary <object, object> properties = null)
        {
            var instanceId = FormFlowInstanceId.GenerateForRouteValues(key, routeParameters);

            var instanceStateProvider = Factory.Services.GetRequiredService <IUserInstanceStateProvider>();

            return((FormFlowInstance <TState>)instanceStateProvider.CreateInstance(
                       key,
                       instanceId,
                       typeof(TState),
                       state,
                       properties));
        }
Esempio n. 6
0
        public void CompleteInstance(FormFlowInstanceId instanceId)
        {
            var storeKey = GetKeyForInstance(instanceId);

            if (_store.TryGetState(storeKey, out var serialized))
            {
                var entry = (StoreEntry)_stateSerializer.Deserialize(serialized);

                entry.Completed = true;

                var updateSerialized = _stateSerializer.Serialize(entry);
                _store.SetState(storeKey, updateSerialized);
            }
            else
            {
                throw new ArgumentException("Instance does not exist.", nameof(instanceId));
            }
        }
        public void TryResolve_RandomIdGenerationSourceMissingQueryParameter_ReturnsFalse()
        {
            // Arrange
            var flowDescriptor = new FormFlowDescriptor(
                key: "key",
                stateType: typeof(MyState),
                idGenerationSource: IdGenerationSource.RandomId);

            var httpContext = new DefaultHttpContext();

            var routeData = new RouteData();

            // Act
            var created = FormFlowInstanceId.TryResolve(flowDescriptor, httpContext.Request, routeData, out var instanceId);

            // Assert
            Assert.False(created);
        }
        public async Task GetOrCreateInstanceAsync_InstanceAlreadyExists_ReturnsExistingInstance()
        {
            // Arrange
            var key       = "test-flow";
            var stateType = typeof(TestState);
            var state     = new TestState();

            var flowDescriptor = new FormFlowDescriptor(key, stateType, IdGenerationSource.RandomId);

            var stateProvider = new InMemoryInstanceStateProvider();

            var instanceId = FormFlowInstanceId.GenerateForRandomId();

            var existingInstance = FormFlowInstance.Create(
                stateProvider,
                key,
                instanceId,
                stateType,
                state,
                new Dictionary <object, object>());

            var httpContext = new DefaultHttpContext();

            httpContext.Features.Set(new FormFlowInstanceFeature(existingInstance));

            var routeData = new RouteData();

            var actionDescriptor = new ActionDescriptor();

            actionDescriptor.SetProperty(flowDescriptor);

            var actionContext = new ActionContext(httpContext, routeData, actionDescriptor);

            var instanceFactory = new FormFlowInstanceFactory(flowDescriptor, actionContext, stateProvider);

            // Act
            var instance = await instanceFactory.GetOrCreateInstanceAsync(() => Task.FromResult(new TestState()));

            // Assert
            Assert.Same(existingInstance, instance);
        }
        public void TryResolve_RandomIdGenerationSourceContainsQueryParameter_ReturnsTrue()
        {
            // Arrange
            var flowDescriptor = new FormFlowDescriptor(
                key: "key",
                stateType: typeof(MyState),
                idGenerationSource: IdGenerationSource.RandomId);

            var httpContext = new DefaultHttpContext();

            httpContext.Request.QueryString = new QueryString("?ffiid=some-id");

            var routeData = new RouteData();

            // Act
            var created = FormFlowInstanceId.TryResolve(flowDescriptor, httpContext.Request, routeData, out var instanceId);

            // Assert
            Assert.True(created);
            Assert.Equal("some-id", instanceId.ToString());
        }
Esempio n. 10
0
        public FormFlowInstance CreateInstance(
            string key,
            FormFlowInstanceId instanceId,
            Type stateType,
            object state,
            IReadOnlyDictionary <object, object> properties)
        {
            if (key == null)
            {
                throw new ArgumentNullException(nameof(key));
            }

            if (stateType == null)
            {
                throw new ArgumentNullException(nameof(stateType));
            }

            if (state == null)
            {
                throw new ArgumentNullException(nameof(state));
            }

            properties ??= new Dictionary <object, object>();

            var entry = new StoreEntry()
            {
                Key = key,
                StateTypeAssemblyQualifiedName = stateType.AssemblyQualifiedName,
                State      = state,
                Properties = properties,
                Completed  = false
            };
            var serialized = _stateSerializer.Serialize(entry);

            var storeKey = GetKeyForInstance(instanceId);

            _store.SetState(storeKey, serialized);

            return(FormFlowInstance.Create(this, key, instanceId, stateType, state, properties));
        }
Esempio n. 11
0
        public void TryResolve_RouteValuesGenerationSourceMissingRouteParameter_ReturnsFalse()
        {
            // Arrange
            var flowDescriptor = new FormFlowDescriptor(
                key: "key",
                stateType: typeof(MyState),
                idGenerationSource: IdGenerationSource.RouteValues,
                idRouteParameterNames: new[] { "id1", "id2" });

            var httpContext = new DefaultHttpContext();

            var routeData = new RouteData(new RouteValueDictionary()
            {
                { "id1", "foo" }
            });

            // Act
            var created = FormFlowInstanceId.TryResolve(flowDescriptor, httpContext.Request, routeData, out var instanceId);

            // Assert
            Assert.False(created);
        }
Esempio n. 12
0
        public void UpdateState_DeletedInstance_ThrowsInvalidOperationException()
        {
            // Arrange
            var instanceId = new FormFlowInstanceId("instance", new Microsoft.AspNetCore.Routing.RouteValueDictionary());

            var stateProvider = new Mock <IUserInstanceStateProvider>();

            var instance = (FormFlowInstance <MyState>)FormFlowInstance.Create(
                stateProvider.Object,
                "key",
                instanceId,
                typeof(MyState),
                new MyState(),
                properties: new Dictionary <object, object>());

            var newState = new MyState();

            instance.Complete();

            // Act & Assert
            Assert.Throws <InvalidOperationException>(() => instance.UpdateState(newState));
        }
Esempio n. 13
0
        public void Delete_CallsDeleteOnStateProvider()
        {
            // Arrange
            var instanceId = new FormFlowInstanceId("instance", new Microsoft.AspNetCore.Routing.RouteValueDictionary());

            var stateProvider = new Mock <IUserInstanceStateProvider>();

            var instance = (FormFlowInstance <MyState>)FormFlowInstance.Create(
                stateProvider.Object,
                "key",
                instanceId,
                typeof(MyState),
                new MyState(),
                properties: new Dictionary <object, object>());

            var newState = new MyState();

            // Act
            instance.Complete();

            // Assert
            stateProvider.Verify(mock => mock.CompleteInstance(instanceId));
        }
Esempio n. 14
0
        public FormFlowInstance GetInstance(FormFlowInstanceId instanceId)
        {
            var storeKey = GetKeyForInstance(instanceId);

            if (_store.TryGetState(storeKey, out var serialized))
            {
                var entry = (StoreEntry)_stateSerializer.Deserialize(serialized);

                var stateType = Type.GetType(entry.StateTypeAssemblyQualifiedName);

                return(FormFlowInstance.Create(
                           this,
                           entry.Key,
                           instanceId,
                           stateType,
                           entry.State,
                           entry.Properties,
                           entry.Completed));
            }
            else
            {
                return(null);
            }
        }
Esempio n. 15
0
        public void TryResolve_RouteValuesGenerationSourceContainsAllRouteParameters_ReturnsTrue()
        {
            // Arrange
            var flowDescriptor = new FormFlowDescriptor(
                key: "key",
                stateType: typeof(MyState),
                idGenerationSource: IdGenerationSource.RouteValues,
                idRouteParameterNames: new[] { "id1", "id2" });

            var httpContext = new DefaultHttpContext();

            var routeData = new RouteData(new RouteValueDictionary()
            {
                { "id1", "foo" },
                { "id2", "bar" }
            });

            // Act
            var created = FormFlowInstanceId.TryResolve(flowDescriptor, httpContext.Request, routeData, out var instanceId);

            // Assert
            Assert.True(created);
            Assert.Equal("key?id1=foo&id2=bar", instanceId.ToString());
        }
 public void CompleteInstance(FormFlowInstanceId instanceId)
 {
     _instances.Remove(instanceId);
 }
        public void OnActionExecuting_InstanceArgumentIsBound_DoesNotSetResult()
        {
            // Arrange
            var key       = "key";
            var stateType = typeof(MyState);

            MissingInstanceHandler handler = (flowDescriptor, httpContext) => new CustomResult();

            var options = new FormFlowOptions()
            {
                MissingInstanceHandler = handler
            };

            var services = new ServiceCollection()
                           .AddSingleton(Options.Create(options))
                           .BuildServiceProvider();

            var flowDescriptor = new FormFlowDescriptor(key, stateType, IdGenerationSource.RandomId);

            var httpContext = new DefaultHttpContext();

            httpContext.RequestServices = services;

            var routeData = new RouteData();

            var actionDescriptor = new ActionDescriptor()
            {
                Parameters = new List <ParameterDescriptor>()
                {
                    new ParameterDescriptor()
                    {
                        Name          = "instance",
                        ParameterType = typeof(FormFlowInstance)
                    }
                }
            };

            actionDescriptor.SetProperty(flowDescriptor);

            var actionContext = new ActionContext(httpContext, routeData, actionDescriptor);

            var actionArguments = new Dictionary <string, object>()
            {
                {
                    "instance",
                    FormFlowInstance.Create(
                        new InMemoryInstanceStateProvider(),
                        key,
                        FormFlowInstanceId.GenerateForRandomId(),
                        stateType,
                        new MyState(),
                        new Dictionary <object, object>())
                }
            };

            var context = new ActionExecutingContext(
                actionContext,
                new List <IFilterMetadata>(),
                actionArguments,
                controller: null);

            var filter = new MissingInstanceActionFilter();

            // Act
            filter.OnActionExecuting(context);

            // Assert
            Assert.Null(context.Result);
        }
 public void UpdateInstanceState(FormFlowInstanceId instanceId, object state)
 {
     _instances[instanceId].State = state;
 }