public void CreateDeleteActionViaShortcut()
        {
            var action = ConfigKeyAction.Delete("Foo");

            Assert.Equal(ConfigKeyActionType.Delete, action.Type);
            Assert.Equal("Foo", action.Key);
        }
        /// <summary>
        ///     compare the source-env with all target-envs and return the necessary actions to reach each target
        /// </summary>
        /// <param name="targetEnvironment"></param>
        /// <param name="sourceEnvironments"></param>
        /// <returns></returns>
        private IList <EnvironmentComparison> CompareEnvironments(EnvironmentExport targetEnvironment,
                                                                  IDictionary <EnvironmentIdentifier, DtoConfigKey[]> sourceEnvironments)
        {
            var comparisons = new List <EnvironmentComparison>();

            try
            {
                foreach (var(id, sourceKeys) in sourceEnvironments)
                {
                    Output.WriteVerboseLine($"comparing '{targetEnvironment.Category}/{targetEnvironment.Name}' <=> '{id}'");

                    var changedKeys = (Mode & ComparisonMode.Add) != 0
                                          ? targetEnvironment.Keys.Where(key =>
                    {
                        // if the given key does not exist in the target environment or is somehow changed
                        // we add it to the list of changed keys for review
                        var sourceKey = sourceKeys.FirstOrDefault(k => k.Key.Equals(key.Key));

                        // null and "" are treated as equals here
                        return(sourceKey is null ||
                               !(sourceKey.Value ?? string.Empty).Equals(key.Value ?? string.Empty) ||
                               !(sourceKey.Type ?? string.Empty).Equals(key.Type ?? string.Empty) ||
                               !(sourceKey.Description ?? string.Empty).Equals(key.Description ?? string.Empty));
                    }).ToList()
                                          : new List <EnvironmentKeyExport>();

                    var deletedKeys = (Mode & ComparisonMode.Delete) != 0
                                          ? sourceKeys.Where(sk =>
                    {
                        // if any target-key doesn't exist in the source any more,
                        // we add it to the list of deleted keys for review
                        return(targetEnvironment.Keys.All(tk => !tk.Key.Equals(sk.Key)));
                    }).ToList()
                                          : new List <DtoConfigKey>();

                    comparisons.Add(new EnvironmentComparison
                    {
                        Source          = new EnvironmentIdentifier(targetEnvironment.Category, targetEnvironment.Name),
                        Target          = id,
                        RequiredActions = changedKeys.Select(c => KeepNullProperties
                                                                      ? ConfigKeyAction.Set(c.Key, c.Value, c.Description, c.Type)
                                                                      : ConfigKeyAction.Set(c.Key,
                                                                                            c.Value ?? string.Empty,
                                                                                            c.Description ?? string.Empty,
                                                                                            c.Type ?? string.Empty))
                                          .Concat(deletedKeys.Select(d => ConfigKeyAction.Delete(d.Key)))
                                          .ToList()
                    });
                }
            }
            catch (Exception e)
            {
                Output.WriteLine($"error while comparing environments: {e}");
                return(new List <EnvironmentComparison>());
            }

            return(comparisons);
        }
        public void ValidateStructureVariablesModified()
        {
            var validator = CreateValidator();

            var result = validator.ValidateDomainEvent(new StructureVariablesModified(new StructureIdentifier("Foo", 42), new[]
            {
                ConfigKeyAction.Set("Foo", "Bar"),
                ConfigKeyAction.Delete("Baz")
            }));

            Assert.False(result.IsError);
        }
        public void ValidateEnvironmentKeysModified()
        {
            var validator = CreateValidator();

            var result = validator.ValidateDomainEvent(new EnvironmentKeysModified(new EnvironmentIdentifier("Foo", "Bar"), new[]
            {
                ConfigKeyAction.Set("Foo", "Bar", "description", "type"),
                ConfigKeyAction.Delete("Baz")
            }));

            Assert.False(result.IsError);
        }
Exemplo n.º 5
0
        public void ReplayHandlesDeletedVariables()
        {
            var item = new ConfigStructure(new StructureIdentifier("FooBar", 42));

            item.Create(new Dictionary <string, string> {
                { "Foo", "Bar" }
            },
                        new Dictionary <string, string> {
                { "Bar", "Baz" }
            });

            item.ApplyEvent(new ReplayedEvent
            {
                Version     = 1,
                DomainEvent = new StructureVariablesModified(new StructureIdentifier("FooBar", 42),
                                                             new[] { ConfigKeyAction.Delete("Foo") }),
                UtcTime = DateTime.UtcNow
            });

            Assert.Equal(1, item.CurrentVersion);
            Assert.DoesNotContain("Foo", item.Variables.Keys);
        }