예제 #1
0
        public async Task ExecuteAsync_ValidParameters_ReturnsVoidResult()
        {
            var beforCommandCreated = DateTime.Now;
            var unknowCommand       = new UnknowCommand();
            var afterCommandCreated = DateTime.Now;
            var command             = "someunknowncommand";
            var subject             = new DocumentIdSubject {
                Id = "subject"
            };
            var modifiers = new[] { new Modifier {
                                        Name = "1"
                                    }, new Modifier {
                                        Name = "2"
                                    }, new Modifier {
                                        Name = "3"
                                    } };

            unknowCommand.SetParameters(command, subject, modifiers);
            var result = await unknowCommand.ExecuteAsync();

            Assert.Equal(subject, unknowCommand.Subject);
            Assert.Equal(command, unknowCommand.CommandText);
            Assert.Equal(modifiers, unknowCommand.Modifiers);
            Assert.Equal($"unknown command [{command}]", result.Content.ContentAsString());
            Assert.Equal(DocumentViewType.Warning, result.Type);
            Assert.True(unknowCommand.CreationDate >= beforCommandCreated && unknowCommand.CreationDate <= afterCommandCreated);
        }
예제 #2
0
        public void SetParameters_ValidParameters_SetsParameters()
        {
            var documentProcessorStub = new Mock <IDocumentProcessor>();
            var command     = new CommandForTest(documentProcessorStub.Object);
            var commandText = "command";
            var modifiers   = new[]
            {
                new Modifier {
                    Name = "1"
                }, new Modifier {
                    Name = "2"
                }, new Modifier {
                    Name = "3"
                }
            };
            var commandSubject = new DocumentIdSubject {
                Id = "subject"
            };

            command.SetParameters(commandText, commandSubject, modifiers);

            Assert.Equal(commandText, command.CommandText);
            Assert.Equal(commandSubject, command.Subject);
            Assert.Equal(modifiers, command.Modifiers);
        }
예제 #3
0
        public async Task ExecuteAsync_ExecutesCommand()
        {
            var documentProcessorMock = new Mock <IDocumentProcessor>();
            var command     = new CommandForTest(documentProcessorMock.Object);
            var commandText = "command";
            var modifiers   = new[]
            {
                new Modifier {
                    Name = "1"
                }, new Modifier {
                    Name = "2"
                }, new Modifier {
                    Name = "3"
                }
            };
            var commandSubject = new DocumentIdSubject {
                Id = "subject"
            };

            command.SetParameters(commandText, commandSubject, modifiers);
            await command.ExecuteAsync();

            var exp = modifiers.Select(parameter => new ActionParameter
            {
                Name = parameter.Name,
                Data = parameter.Arguments
            }).ToArray();

            documentProcessorMock.Verify(
                it => it.ProcessAsync(commandSubject, ActionType.GetContent,
                                      It.Is <ActionParameter[]>(p => CompareActionParameters(p, exp))), Times.Once);
        }
예제 #4
0
        public async Task ProcessAsync_SubjectActionTypeAndActionParameter_CallsProcessingStrategyWithValidParameters()
        {
            var subject           = new DocumentIdSubject();
            var processingActions = new []
            {
                new ActionParameter {
                    Name = "action1", Data = "some data"
                },
                new ActionParameter {
                    Name = "action2"
                }
            };
            var result                = new DocumentView <DocumentProcessingResultContent>();
            var linkerMock            = new Mock <IDocumentProcessingStrategyTypeLinker>();
            var interfaceProviderMock = new Mock <ISingleInterfaceServiceProvider <IDocumentProcessingStrategy> >();
            var strategyMock          = new Mock <IDocumentProcessingStrategy>();
            var action                = ActionType.AddDocument;
            var strategyType          = typeof(IDocumentProcessingStrategy);

            linkerMock.Setup(it => it.GetStrategyType(action)).Returns(strategyType);
            interfaceProviderMock.Setup(it => it.Resolve(strategyType)).Returns(strategyMock.Object);
            strategyMock.Setup(it => it.ProcessAsync(subject, It.IsAny <DocumentProcessingAction[]>())).ReturnsAsync(result);

            var processor    = new Brace.DocumentProcessor.DocumentProcessor(linkerMock.Object, interfaceProviderMock.Object);
            var documentView = await processor.ProcessAsync(subject, ActionType.AddDocument, processingActions);

            Assert.Equal(result, documentView);
            strategyMock.Verify(it => it.ProcessAsync(subject, It.Is <DocumentProcessingAction[]>(x => x != null && VerifyThatAllParametersWereConvertedCorrectly(x, processingActions))), Times.Once);
        }
예제 #5
0
        public async Task ProcessAsync_SubjectOfWrongType_ThrowsException()
        {
            var documentRepositoryStub = new Mock <IDocumentRepository>();
            var archivistFactoryStub   = new Mock <IArchivistFactory>();
            var strategy = new UpdateDocumentStrategy(documentRepositoryStub.Object, archivistFactoryStub.Object);
            var subject  = new DocumentIdSubject {
                Id = "1234567890"
            };
            var exception = await Assert.ThrowsAsync <DocumentProcessingStrategyException>(async() => await strategy.ProcessAsync(subject, null));

            Assert.Equal($"Invalid subject type - {typeof(DocumentIdSubject)}. It should be {typeof(UpdateDocumentSubject)}.", exception.Message);
        }
예제 #6
0
        protected void Validate_InvalidModifiers_ReturnsInvalidValidationResult(CommandType commandType, Modifier modifier)
        {
            var documentProcessorStub = Mock.Of <IDocumentProcessor>();
            var command     = (TCommand)Activator.CreateInstance(typeof(TCommand), documentProcessorStub);
            var commandText = CommandType.GetContent.ToString();
            var subject     = new DocumentIdSubject {
                Id = $"{typeof(TCommand).Name} document"
            };

            command.SetParameters(commandText, subject, new[] { modifier });
            var validationResult = command.Validate();

            Assert.False(validationResult.IsValid);
            Assert.Equal($"Invalid command modifiers: parameter '{modifier.Name}' can't be used with the '{commandType.ToString().ToLower()}' command", validationResult.ValidationMessage);
        }
예제 #7
0
        private static Subject GetSubject(string sentence)
        {
            Subject subject      = null;
            var     subjectMatch = Regex.Matches(sentence, SubjectPattern);

            if (subjectMatch.Count != 0)
            {
                if (subjectMatch.Count > 1)
                {
                    throw new CommandInterpreterException("Invalid number of subjects. Only one subject can be specified.");
                }
                try
                {
                    string documentName    = null;
                    string documentContent = null;
                    var    hasContent      = false;
                    var    dynamicSubject  = JsonConvert.DeserializeObject(subjectMatch[0].Value);
                    foreach (JProperty token in (IEnumerable)dynamicSubject)
                    {
                        if (token.Name == "name")
                        {
                            documentName = token.Value.ToString();
                        }
                        if (token.Name == "content")
                        {
                            hasContent      = true;
                            documentContent = token.Value?.ToString();
                        }
                    }
                    subject = hasContent
                        ? new AddDocumentSubject {
                        Id = documentName, Content = documentContent
                    }
                        : new DocumentIdSubject {
                        Id = documentName
                    };
                }
                catch
                {
                    subject = new DocumentIdSubject {
                        Id = subjectMatch[0].Value.Trim('{', '}')
                    };
                }
            }
            return(subject);
        }
예제 #8
0
        public async Task ProcessAsync_Subject_ReturnsValidProcessingResult()
        {
            var documentRepositoryMock = new Mock <IDocumentRepository>();
            var subject = new DocumentIdSubject {
                Id = "123"
            };
            var strategy = new DeleteDocumentStrategy(documentRepositoryMock.Object);
            var result   = await strategy.ProcessAsync(subject, null);

            documentRepositoryMock.Verify(it => it.DeleteAsync(subject.Id), Times.Once);
            Assert.NotNull(result);
            Assert.IsType <DocumentView <DocumentProcessingResultContent> >(result);
            var documentView = (DocumentView <DocumentProcessingResultContent>)result;

            Assert.Equal(DocumentViewType.Ok, documentView.Type);
            Assert.Equal(DocumentProcessingResultType.Deleted, documentView.Content.ProcessingResultType);
        }
예제 #9
0
        protected async Task GetActionType_Void_ReturnsActionType(ActionType actionType)
        {
            var documentProcessorMock = new Mock <IDocumentProcessor>();
            var subject = new DocumentIdSubject {
                Id = "documentName"
            };
            var actionParameters = new ActionParameter[0];

            documentProcessorMock.Setup(it => it.ProcessAsync(subject, actionType, actionParameters))
            .ReturnsAsync(Mock.Of <DocumentView>());
            var command = (TCommand)Activator.CreateInstance(typeof(TCommand), documentProcessorMock.Object);

            command.SetParameters(string.Empty, subject, new Modifier[0]);
            await command.ExecuteAsync();

            documentProcessorMock.Verify(it => it.ProcessAsync(subject, actionType, actionParameters), Times.Once);
        }
예제 #10
0
        public void Validate_ValidParameters_ValidValidationResult()
        {
            var documentProcessorStub = new Mock <IDocumentProcessor>();
            var command        = new CommandForTest(documentProcessorStub.Object);
            var commandText    = "print";
            var commandSubject = new DocumentIdSubject {
                Id = "test"
            };
            var modifiers = new[] { new Modifier {
                                        Name = "decrypt"
                                    } };

            command.SetParameters(commandText, commandSubject, modifiers);
            var validationResult = command.Validate();

            Assert.True(validationResult.IsValid);
            Assert.Null(validationResult.ValidationMessage);
        }
예제 #11
0
        public void Validate_DuplicatedParameters_InvalidValidationResult()
        {
            var documentProcessorStub = new Mock <IDocumentProcessor>();
            var command        = new CommandForTest(documentProcessorStub.Object);
            var commandText    = "print";
            var commandSubject = new DocumentIdSubject {
                Id = "test"
            };
            var modifiers = new[] { new Modifier {
                                        Name = "decrypt"
                                    }, new Modifier {
                                        Name = "decrypt"
                                    } };

            command.SetParameters(commandText, commandSubject, modifiers);
            var validationResult = command.Validate();

            Assert.False(validationResult.IsValid);
            Assert.Equal("Invalid command modifiers: duplicates found", validationResult.ValidationMessage);
        }
예제 #12
0
        public void Validate_InvalidParameters_InvalidValidationResult()
        {
            var documentProcessorStub = new Mock <IDocumentProcessor>();
            var command        = new CommandForTest(documentProcessorStub.Object);
            var commandText    = CommandType.GetContent.ToString();
            var commandSubject = new DocumentIdSubject {
                Id = "test"
            };
            var modifiers = new[] { new Modifier {
                                        Name = "decrypt"
                                    }, new Modifier {
                                        Name = "encrypt"
                                    } };

            command.SetParameters(commandText, commandSubject, modifiers);
            var validationResult = command.Validate();

            Assert.False(validationResult.IsValid);
            Assert.Equal($"Invalid command modifiers: parameter 'encrypt' can't be used with the '{CommandType.GetContent.ToString().ToLower()}' command", validationResult.ValidationMessage);
        }
예제 #13
0
        public void CreateCommand_InvalidCommandName_CreatesUnknownCommand()
        {
            var command = "someunknowncommand";
            var subject = new DocumentIdSubject {
                Id = "subject"
            };
            var modifiers = new[]
            {
                new Modifier {
                    Name = "1"
                }, new Modifier {
                    Name = "2"
                }, new Modifier {
                    Name = "3"
                }
            };
            var commandMock = new Mock <ICommand>();

            var commandLinkerStub   = new Mock <ICommandLinker>();
            var commandProviderStub = new Mock <ISingleInterfaceServiceProvider <ICommand> >();

            commandProviderStub.Setup(it => it.Resolve(commandMock.Object.GetType())).Returns(commandMock.Object);
            commandLinkerStub.Setup(it => it.GetCommandType(command)).Throws(new LinkerException($"Invalid command identifier - {command}. Cannot translate into CommandType."));
            commandLinkerStub.Setup(it => it.GetCommandType(CommandType.Unknown.ToString())).Returns(commandMock.Object.GetType);
            var commandFactory = new CommandFactory(commandLinkerStub.Object, commandProviderStub.Object);
            var commandInfo    = new CommandInfo
            {
                Command   = command,
                Subject   = subject,
                Modifiers = modifiers
            };
            var commandInstance = commandFactory.CreateCommand(commandInfo);

            Assert.NotNull(commandInstance);
            Assert.Equal(commandInstance, commandMock.Object);
            commandMock.Verify(it => it.SetParameters(command, subject, modifiers), Times.Once);
        }
예제 #14
0
        public void CreateCommand_ValidCommandName_CreatesCommand()
        {
            var command = "test";
            var subject = new DocumentIdSubject {
                Id = "subject"
            };
            var modifiers = new[]
            {
                new Modifier {
                    Name = "1"
                }, new Modifier {
                    Name = "2"
                }, new Modifier {
                    Name = "3"
                }
            };
            var commandMock = new Mock <ICommand>();

            var commandLinkerStub   = new Mock <ICommandLinker>();
            var commandProviderStub = new Mock <ISingleInterfaceServiceProvider <ICommand> >();

            commandProviderStub.Setup(it => it.Resolve(commandMock.Object.GetType())).Returns(commandMock.Object);
            commandLinkerStub.Setup(it => it.GetCommandType(command)).Returns(commandMock.Object.GetType);

            var commandFactory = new CommandFactory(commandLinkerStub.Object, commandProviderStub.Object);
            var commandInfo    = new CommandInfo
            {
                Command   = command,
                Subject   = subject,
                Modifiers = modifiers
            };
            var commandInstance = commandFactory.CreateCommand(commandInfo);

            Assert.NotNull(commandInstance);
            Assert.Equal(commandInstance, commandMock.Object);
            commandMock.Verify(it => it.SetParameters(command, subject, modifiers), Times.Once);
        }