public CommandContext(CommandTypes types, ConfigChangeRequest changeRequest, IStorageModel model)
            : base(model)
        {
            CommandType = types;
            Check(changeRequest, "changeRequest");
            Check(changeRequest.Name, "changeRequest.Name");
            Check(changeRequest.Value, "changeRequest.Value");

            ChangeRequest         = changeRequest;
            ConfigurationEntryKey = ChangeRequest.Name;
        }
        private static async Task <OperationResult> CreateInMemory(InMemoryStore store, string other = null)
        {
            var changeRequest = new ConfigChangeRequest
            {
                Name  = other == null ? "ConnectionString" : other,
                Value = "/Users/timoheiten/my-db.db"
            };
            var context = new CommandContext(CommandTypes.Create, changeRequest, store);

            OperationResult result = await Factory.RunOperationAsync(context);

            return(result);
        }
        private static async Task <OperationResult> UpdateValueInMemory(InMemoryStore store)
        {
            var changeRequest = new ConfigChangeRequest
            {
                Name  = "ConnectionString",
                Value = "/Users/timoheiten/my-db-2.db"
            };

            var context = new CommandContext(CommandTypes.UpdateValue, changeRequest, store);

            var result = await Factory.RunOperationAsync(context);

            return(result);
        }
        public async Task CreateAsyncHappyPath()
        {
            var request = new ConfigChangeRequest {
                Name = "name", Value = "value"
            };
            var context = new CommandContext(CommandTypes.Create, request, model);

            model.StoreEntityAsync(Arg.Any <ConfigEntity>()).Returns(true);
            var result = await AllCommands.CreateAsync(context);

            Assert.True(result.IsSuccess);
            Assert.Equal("name", result.Result.Name);
            Assert.Equal("value", result.Result.Value);
        }
        public async Task CreateAsyncStorageFailsReturnsStorageFailedOperationResult()
        {
            var request = new ConfigChangeRequest {
                Name = "name", Value = "value"
            };
            var context = new CommandContext(CommandTypes.Create, request, model);

            model.StoreEntityAsync(Arg.Any <ConfigEntity>()).Returns(false);
            var result = await AllCommands.CreateAsync(context);

            Assert.False(result.IsSuccess);
            Assert.Null(result.Result);
            Assert.Equal(ResultType.Forbidden, result.ResultType);
        }
        ///<summary>
        /// load simple json file
        /// save to in Memory store
        /// and load all and print to console
        ///</summary>
        public static async Task Run(Action <object> printCallback)
        {
            var store = new InMemoryStore();
            // upload file from the current directory
            string     json      = JsonFromConfigFile();
            ITransform transform = new JsonTransform();

            OperationResult result = transform.Parse(json);

            if (result.Result is ConfigCollection collection)
            {
                foreach (var item in collection.WrappedConfigEntities)
                {
                    var request = new ConfigChangeRequest {
                        Name = item.Name, Value = item.Value
                    };
                    var context = new CommandContext(CommandTypes.Create, request, store);

                    await Factory.RunOperationAsync(context);
                }
            }
            else
            {
                throw new InvalidCastException("should have been ConfigCollection yet is " + result.Result.GetType().Name);
            }
            foreach (var item in await store.AllEntitesAsync())
            {
                printCallback($"next one is [{item.Name}] - [{item.Value}]");
            }

            printCallback("also find RabbitMQ:Port and change it to 5674");
            ConfigEntity port = await QueryRabbitPortAsync(store);

            printCallback(port.Name + " - " + port.Value);

            var updateRq = new ConfigChangeRequest {
                Name = port.Name, Value = "5674"
            };
            var update = new CommandContext(CommandTypes.UpdateValue, updateRq, store);

            OperationResult rs = await Factory.RunOperationAsync(update);

            printCallback("after the update:");
            port = await QueryRabbitPortAsync(store);

            printCallback(port.Name + " - " + port.Value);
        }
Example #7
0
        public void CommandContextThrowsOnNameOrValueNull()
        {
            ConfigChangeRequest changeRequest = null;

            Assert.Throws <ArgumentException>(() => new CommandContext(CommandTypes.Create, changeRequest, model));

            changeRequest = new ConfigChangeRequest()
            {
                Name = "name"
            };
            Assert.Throws <ArgumentException>(() => new CommandContext(CommandTypes.Create, changeRequest, model));
            changeRequest = new ConfigChangeRequest()
            {
                Value = "name"
            };
            Assert.Throws <ArgumentException>(() => new CommandContext(CommandTypes.Create, changeRequest, model));
        }
        public async Task DeleteAsyncConfigEntityNotFound()
        {
            var request = new ConfigChangeRequest {
                Name = "name", Value = "value"
            };
            var context  = new CommandContext(CommandTypes.Delete, request, model);
            var expected = new ConfigEntity {
                Name = "name", Value = "old"
            };

            model.GetEntityByNameAsync("name").Returns((ConfigEntity)null);

            var result = await AllCommands.DeleteAsync(context);

            Assert.False(result.IsSuccess);
            Assert.Null(result.Result);
            Assert.Equal(ResultType.NotFound, result.ResultType);
        }
        public async Task UpdateAsyncConfigEntityNotStored()
        {
            var request = new ConfigChangeRequest {
                Name = "name", Value = "value"
            };
            var context  = new CommandContext(CommandTypes.UpdateValue, request, model);
            var expected = new ConfigEntity {
                Name = "name", Value = "old"
            };

            model.GetEntityByNameAsync("name").Returns(expected);
            model.StoreEntityAsync(Arg.Any <ConfigEntity>()).Returns(false);

            var result = await AllCommands.UpdateAsync(context);

            Assert.False(result.IsSuccess);
            Assert.Null(result.Result);
            Assert.Equal(ResultType.Forbidden, result.ResultType);
        }
        public async Task UpdateAsyncHappyPath()
        {
            var request = new ConfigChangeRequest {
                Name = "name", Value = "value"
            };
            var context  = new CommandContext(CommandTypes.UpdateValue, request, model);
            var expected = new ConfigEntity {
                Name = "name", Value = "old"
            };

            model.GetEntityByNameAsync("name").Returns(expected);
            model.StoreEntityAsync(Arg.Any <ConfigEntity>()).Returns(true);
            var result = await AllCommands.UpdateAsync(context);

            await model.Received().StoreEntityAsync(Arg.Any <ConfigEntity>());

            Assert.True(result.IsSuccess);
            Assert.Equal("name", result.Result.Name);
            Assert.Equal("value", result.Result.Value);
        }
        ///<summary>
        /// set value to null explicitly to initiate delete
        ///</summary>
        public async Task <OperationResult> RunUseCaseAsync()
        {
            // check if context exists
            var             query  = new QueryContext(_name, QueryTypes.ValueRequest, _model);
            OperationResult result = await OperateAsync(query);

            var request = new ConfigChangeRequest()
            {
                Value = _value,
                Name  = _name
            };
            bool         isDelete = _value == null;
            CommandTypes type     = isDelete
                                ? CommandTypes.Delete
                                : FindCommandType(result);

            var cmd           = new CommandContext(type, request, _model);
            var commandResult = await OperateAsync(cmd);

            return(commandResult);
        }
        private static async Task <OperationResult> DeleteAndQueryAfter(InMemoryStore store)
        {
            var changeRequest = new ConfigChangeRequest
            {
                Name  = "ConnectionString",
                Value = "null"
            };
            var context = new CommandContext(CommandTypes.Delete, changeRequest, store);

            OperationResult result = await Factory.RunOperationAsync(context);

            var queryResult = await ReadConfigEntityInMemory("ConnectionString", store);

            if (queryResult.IsSuccess == false)
            {
                printCallback("now after delete it was not found!");
            }
            else
            {
                printCallback("could still find --> error in test");
            }
            return(result);
        }
Example #13
0
        public static async Task Run(Action <object> log)
        {
            // run migrate async
            // use efstore
            // run different tests  by using in memory service
            var persistent     = new EfStore("Data Source=./config.db");
            var memoryInteract = new MemoryInteract(persistent, persistent);

            var authModel = new AuthModel {
                Name = "timoh", Password = "******"
            };
            var appClaims = new AppClaimModel[]
            {
                new AppClaimModel()
                {
                    ApplicationName  = "test-app-1",
                    ConfigEntitiyKey = "ConnectionString",
                    CanRead          = true,
                    CanWrite         = true
                }
            };
            // add user
            var             createUser = persistent.CreateUserContext(authModel, persistent, appClaims);
            OperationResult result     = await memoryInteract.Run(new ContextModel
            {
                Type      = ContextType.AddUser,
                AppClaims = appClaims,
                User      = authModel,
                Value     = "",
                Key       = "",
            });

            Debug.Assert(result.IsSuccess);

            // create
            var changeRequest = new ConfigChangeRequest
            {
                Name  = "ConnectionString",
                Value = "/Users/timoheiten/my-db.db"
            };
            var context = new CommandContext(CommandTypes.Create, changeRequest, persistent);

            result = await Factory.RunOperationAsync(context);

            Debug.Assert(result.IsSuccess);
            // read
            result = await Factory.RunOperationAsync(persistent.QueryOne(changeRequest.Name));

            Debug.Assert(result.IsSuccess);
            Debug.Assert(result.Result.Value == changeRequest.Value);

            // delete
            result = await Factory.RunOperationAsync(persistent.DeleteContext(changeRequest.Name));

            Debug.Assert(result.IsSuccess);

            // read after delete
            result = await Factory.RunOperationAsync(persistent.QueryOne(changeRequest.Name));

            Debug.Assert(result.IsSuccess == false);
            Debug.Assert(result.ResultType == ResultType.NotFound);

            string padding = "-".PadRight(50, '-');

            System.Console.WriteLine(padding);
            System.Console.WriteLine($"if no assertion error occured, everything is working fine with EF store and Sqlite");
            System.Console.WriteLine(padding);
        }