Exemple #1
0
        public void LogServer_PerformingStartAction_ShouldCallActionOnTheWrapper()
        {
            //--------------------------------Arrange-------------------------------
            var mockLoggerSource = new Mock <ILoggerSource>();

            mockLoggerSource.Setup(ls => ls.NewConnection(It.IsAny <ILoggerConfig>())).Returns(new Mock <ILoggerConnection>().Object);

            var mockLoggerContext = new Mock <ILoggerContext>();

            mockLoggerContext.Setup(l => l.LoggerConfig).Returns(new Mock <ILoggerConfig>().Object);
            mockLoggerContext.Setup(l => l.Source).Returns(mockLoggerSource.Object);

            Action <IWebSocketConnection> performedAction = null;
            var mockWebSocketServerWrapper = new Mock <IWebSocketServerWrapper>();

            mockWebSocketServerWrapper.Setup(ws => ws.Start(It.IsAny <Action <IWebSocketConnection> >())).Callback((Action <IWebSocketConnection> a) =>
            {
                performedAction = a;
            });

            var mockWebSocketServerFactory = new Mock <IWebSocketServerFactory>();

            mockWebSocketServerFactory.Setup(ws => ws.New(It.IsAny <string>())).Returns(mockWebSocketServerWrapper.Object);

            var logServer = new LogServer(mockWebSocketServerFactory.Object, new Mock <IWriter>().Object, mockLoggerContext.Object);

            var mockClient = new Mock <IWebSocketConnection>();

            mockClient.SetupAllProperties();
            logServer.Start(new List <IWebSocketConnection>());
            //--------------------------------Act-----------------------------------
            performedAction(mockClient.Object);
            //--------------------------------Assert--------------------------------
            mockClient.Verify();
        }
Exemple #2
0
        public void LogServer_PerformingClientOnClose_ShouldCallAction_And_OnMessage_ShouldTryConsume_Success()
        {
            //--------------------------------Arrange-------------------------------
            var mockStreamConfig            = new Mock <IStreamConfig>();
            var mockPublisher               = new Mock <IPublisher>();
            var mockLeaderConnection        = new Mock <IConnection>();
            var mockSourceConnectionFactory = new Mock <ISourceConnectionFactory>();

            var serializer  = new JsonSerializer();
            var testMessage = serializer.Serialize <AuditCommand>(new AuditCommand());

            mockLeaderConnection.Setup(o => o.NewPublisher(It.IsAny <IStreamConfig>())).Returns(mockPublisher.Object);
            mockSourceConnectionFactory.Setup(o => o.NewConnection()).Returns(mockLeaderConnection.Object);

            var mockLoggerSource = new Mock <ILoggerSource>();

            mockLoggerSource.Setup(ls => ls.NewConnection(It.IsAny <ILoggerConfig>())).Returns(new Mock <ILoggerConnection>().Object);

            var mockLoggerContext = new Mock <ILoggerContext>();

            mockLoggerContext.Setup(l => l.LoggerConfig).Returns(new Mock <ILoggerConfig>().Object);
            mockLoggerContext.Setup(l => l.Source).Returns(mockLoggerSource.Object);
            mockLoggerContext.Setup(o => o.LeaderSource).Returns(mockSourceConnectionFactory.Object);
            mockLoggerContext.Setup(o => o.LeaderConfig).Returns(mockStreamConfig.Object);

            Action <IWebSocketConnection> performedAction = null;
            var mockWebSocketServerWrapper = new Mock <IWebSocketServerWrapper>();

            mockWebSocketServerWrapper.Setup(ws => ws.Start(It.IsAny <Action <IWebSocketConnection> >())).Callback((Action <IWebSocketConnection> a) =>
            {
                performedAction = a;
            });

            var mockWebSocketServerFactory = new Mock <IWebSocketServerFactory>();

            mockWebSocketServerFactory.Setup(ws => ws.New(It.IsAny <string>())).Returns(mockWebSocketServerWrapper.Object);
            var consoleString = "";
            var mockWriter    = new Mock <IWriter>();

            mockWriter.Setup(w => w.WriteLine(It.IsAny <string>())).Callback((string s) => { consoleString = s; });
            var logServer = new LogServer(mockWebSocketServerFactory.Object, mockWriter.Object, mockLoggerContext.Object);

            var mockClient = new Mock <IWebSocketConnection>();

            mockClient.SetupAllProperties();
            var clients = new List <IWebSocketConnection> {
                mockClient.Object
            };

            logServer.Start(clients);
            performedAction(mockClient.Object);

            mockClient.Object.OnMessage(Encoding.UTF8.GetString(testMessage));
            //--------------------------------Act-----------------------------------
            mockClient.Object.OnClose();
            //--------------------------------Assert--------------------------------
            Assert.AreEqual(0, clients.Count);

            mockPublisher.Verify(o => o.Publish(It.IsAny <byte[]>()), Times.Once);
        }
Exemple #3
0
        public void LogServer_PerformingClientOnClose_ShouldCallAction()
        {
            //--------------------------------Arrange-------------------------------
            var mockLoggerSource = new Mock <ILoggerSource>();

            mockLoggerSource.Setup(ls => ls.NewConnection(It.IsAny <ILoggerConfig>())).Returns(new Mock <ILoggerConnection>().Object);

            var mockLoggerContext = new Mock <ILoggerContext>();

            mockLoggerContext.Setup(l => l.LoggerConfig).Returns(new Mock <ILoggerConfig>().Object);
            mockLoggerContext.Setup(l => l.Source).Returns(mockLoggerSource.Object);

            Action <IWebSocketConnection> performedAction = null;
            var mockWebSocketServerWrapper = new Mock <IWebSocketServerWrapper>();

            mockWebSocketServerWrapper.Setup(ws => ws.Start(It.IsAny <Action <IWebSocketConnection> >())).Callback((Action <IWebSocketConnection> a) =>
            {
                performedAction = a;
            });

            var mockWebSocketServerFactory = new Mock <IWebSocketServerFactory>();

            mockWebSocketServerFactory.Setup(ws => ws.New(It.IsAny <string>())).Returns(mockWebSocketServerWrapper.Object);
            var consoleString = "";
            var mockWriter    = new Mock <IWriter>();

            mockWriter.Setup(w => w.WriteLine(It.IsAny <string>())).Callback((string s) => { consoleString = s; });
            var logServer = new LogServer(mockWebSocketServerFactory.Object, mockWriter.Object, mockLoggerContext.Object);

            var mockClient = new Mock <IWebSocketConnection>();

            mockClient.SetupAllProperties();
            var clients = new List <IWebSocketConnection> {
                mockClient.Object
            };

            logServer.Start(clients);
            performedAction(mockClient.Object);
            //--------------------------------Act-----------------------------------
            mockClient.Object.OnClose();
            //--------------------------------Assert--------------------------------
            Assert.AreEqual(0, clients.Count);
            Assert.AreEqual("Logging Server OnClose...", consoleString);
        }
Exemple #4
0
        public void LogServer_Start_ShouldCallWebSocketStart()
        {
            //--------------------------------Arrange-------------------------------
            var mockLoggerContext = new Mock <ILoggerContext>();

            mockLoggerContext.Setup(l => l.LoggerConfig).Returns(new Mock <ILoggerConfig>().Object);
            var mockWebSocketServerWrapper = new Mock <IWebSocketServerWrapper>();

            var mockWebSocketServerFactory = new Mock <IWebSocketServerFactory>();

            mockWebSocketServerFactory.Setup(ws => ws.New(It.IsAny <string>())).Returns(mockWebSocketServerWrapper.Object);

            var logServer  = new LogServer(mockWebSocketServerFactory.Object, new Mock <IWriter>().Object, mockLoggerContext.Object);
            var mockClient = new Mock <IWebSocketConnection>();

            //--------------------------------Act-----------------------------------
            logServer.Start(new List <IWebSocketConnection>());
            //--------------------------------Assert--------------------------------
            Assert.IsNotNull(logServer);
            mockWebSocketServerWrapper.Verify(w => w.Start(It.IsAny <Action <IWebSocketConnection> >()), Times.Once);
        }