Example #1
0
        public async Task Should_Throw_NotEnoughColorException_When_Not_Enough_Black()
        {
            var printColor = new PrintColorCommand(_content,
                                                   paperLevelIndicator: new EnoughPaperLevelStub(),
                                                   blackLevelIndicator: new EnoughBlackLevelStub(),
                                                   colorLevelIndicator: new NotEnoughColorLevelStub());

            await printColor.Execute();
        }
Example #2
0
        public async Task Should_Throw_NoContentToPrintException_When_Content_Is_Empty_Byte_Array()
        {
            var printColor = new PrintColorCommand(
                content: new byte[0],
                paperLevelIndicator: new EnoughPaperLevelStub(),
                blackLevelIndicator: new EnoughBlackLevelStub(),
                colorLevelIndicator: new EnoughColorLevelStub());

            await printColor.Execute();
        }
Example #3
0
        public async Task Should_Notify_On_Finish_When_Subscribed_To_Channel_PrinterDotPrintedDotCommandId_Only_When_CommandId_Is_Correct()
        {
            Guid incorrectId = Guid.Empty;
            bool areEqual    = false;

            var fn = new FakePrinterNotifier();

            var printColor = new PrintColorCommand(_content,
                                                   paperLevelIndicator: new EnoughPaperLevelStub(),
                                                   blackLevelIndicator: new EnoughBlackLevelStub(),
                                                   colorLevelIndicator: new EnoughColorLevelStub(),
                                                   notifier: fn);

            await fn.Subscribe((message) => areEqual = true, PrinterNotificationTypes.Printed);

            await printColor.Execute();

            Assert.IsFalse(areEqual);
        }
Example #4
0
        public async Task Should_Notify_On_Finish_When_Subscribed_To_Channel_PrinterDotPrintedDotCommandId()
        {
            bool areEqual = false;

            var fn = new FakePrinterNotifier();

            var printColor = new PrintColorCommand(_content,
                                                   paperLevelIndicator: new EnoughPaperLevelStub(),
                                                   blackLevelIndicator: new EnoughBlackLevelStub(),
                                                   colorLevelIndicator: new EnoughColorLevelStub(),
                                                   notifier: fn);

            await fn.Subscribe((message) => areEqual = message.Equals($"Document {printColor.Id} printed"),
                               PrinterNotificationTypes.Printed,
                               correlationId : printColor.Id);

            await printColor.Execute();

            Assert.IsTrue(areEqual);
        }
Example #5
0
        public void Should_Throw_InvalidConfigurationException_For_IColorLevelIndicator_When_Null()
        {
            bool areEqual = false;

            try
            {
                var printColor = new PrintColorCommand(_content,
                                                       paperLevelIndicator: new EnoughPaperLevelStub(),
                                                       blackLevelIndicator: new EnoughBlackLevelStub(),
                                                       colorLevelIndicator: null);
            }
            catch (InvalidConfigurationException ex)
            {
                areEqual = ex.Message.Equals("There is no implementation for Printer.Indicators.IColorLevelIndicator");
            }
            finally
            {
                Assert.IsTrue(areEqual);
            }
        }
Example #6
0
        public async Task Should_Execute_PrintBlackCommand_Without_Errors_When_Configured_Correctly_And_All_Level_Indicators_Are_Sufficient()
        {
            bool isSuccess = true;

            var printColor = new PrintColorCommand(_content,
                                                   paperLevelIndicator: new EnoughPaperLevelStub(),
                                                   blackLevelIndicator: new EnoughBlackLevelStub(),
                                                   colorLevelIndicator: new EnoughColorLevelStub());

            try
            {
                await printColor.Execute();
            }
            catch (Exception)
            {
                isSuccess = false;
            }
            finally
            {
                Assert.IsTrue(isSuccess);
            }
        }
        private static async Task Print(string content)
        {
            byte[] c = System.Text.Encoding.UTF8.GetBytes(content);

            IPaperLevelIndicator pli = _printerType == "p" ?
                                       new ComplexPaperLevelIndicator((Int32)Math.Ceiling((double)(c.Length / 100))) as IPaperLevelIndicator :
                                       new SimplePaperLevelIndicator() as IPaperLevelIndicator;
            IBlackLevelIndicator bli = new SimpleBlackLevelIndicator();
            IPrinterNotifier     n   = null;
            IPrintCommand        cmd;

            if (_notifie == true)
            {
                n = new RabbitMq(_config);
            }

            try
            {
                if (_printType == "c")
                {
                    IColorLevelIndicator cli = _printerType == "p" ?
                                               new ComplexColorLevelIndicator() as IColorLevelIndicator :
                                               new SimpleColorLevelIndicator() as IColorLevelIndicator;
                    cmd = new PrintColorCommand(content: c,
                                                paperLevelIndicator: pli,
                                                blackLevelIndicator: bli,
                                                colorLevelIndicator: cli,
                                                notifier: n);
                }
                else
                {
                    cmd = new PrintBlackCommand(content: c,
                                                paperLevelIndicator: pli,
                                                blackLevelIndicator: bli,
                                                notifier: n);
                }
                if (!(n is null))
                {
                    await n.Subscribe((msg) =>
                    {
                        if (_printType == "c")
                        {
                            ForegroundColor = ConsoleColor.Cyan;
                        }

                        WriteLine(msg);
                        ResetColor();
                    }, PrinterNotificationTypes.Printed, cmd.Id);
                }

                await cmd.Execute();
            }
            catch (CannotConnectToNotificationServiceException)
            {
                Write("Can not connect to notification service. Do you want to proceed? [y/n]: ");
                var key = ReadKey();
                Write(NewLine);
                if (key.Key == ConsoleKey.Y)
                {
                    _notifie = false;
                    await Print(content);
                }
            }
            catch (ApplicationException ex)
            {
                WriteLine(ex.Message);
            }
            catch (Exception ex)
            {
                ForegroundColor = ConsoleColor.Magenta;
                WriteLine(ex.Message);
                ResetColor();

                ConsoleKeyInfo key;
                do
                {
                    key = ReadKey(intercept: false);
                }while (key.Key == ConsoleKey.Enter);

                await ProcessInput("exit");
            }
            finally
            {
                _processing  = false;
                _printerType = null;
                _printType   = null;
                _notifie     = null;
                n?.Dispose();
                await ProcessInput(ReadLine());
            }
        }
Example #8
0
 public async Task PrintColor(byte[] content)
 {
     var command = new PrintColorCommand(content, _pli, _bli, _cli, _n);
     await command.Execute();
 }