/// <summary>
 /// Adds a new record to the database.
 /// </summary>
 /// <param name="connection"></param>
 /// <param name="transaction"></param>
 /// <param name="log"></param>
 /// <param name="client"></param>
 public void Insert(IDbConnection connection, IDbTransaction transaction, TextWriter log, LogClient client)
 {
     SqlPreparedCommand command = PrepareInsert(connection, transaction, "Insert",
         "Id",
         "IpAddress", "ReverseDns", "ReverseDnsDate");
     client.Id = Sql.ExecuteInsert(command, log, client.IpAddress, client.ReverseDns, client.ReverseDnsDate);
 }
        public void ConnectionClientLogPresenter_Initialise_Loads_Clients_From_Database_And_Copies_To_View()
        {
            var client1 = new LogClient();
            var client2 = new LogClient();
            var session1 = new Mock<LogSession>() { DefaultValue = DefaultValue.Mock }.SetupAllProperties().Object;
            var session2 = new Mock<LogSession>() { DefaultValue = DefaultValue.Mock }.SetupAllProperties().Object;

            _LogClients.Add(client1);
            _LogClients.Add(client2);
            _LogSessions.Add(1L, new List<LogSession>() { session1 });
            _LogSessions.Add(2L, new List<LogSession>() { session2 });

            _View.Setup(v => v.ShowClientsAndSessions(It.IsAny<IList<LogClient>>(), It.IsAny<IDictionary<long, IList<LogSession>>>())).Callback((IList<LogClient> clients, IDictionary<long, IList<LogSession>> sessions) => {
                Assert.AreEqual(2, clients.Count());
                Assert.IsTrue(clients.Where(c => c == client1).Any());
                Assert.IsTrue(clients.Where(c => c == client2).Any());

                Assert.AreEqual(2, sessions.Count());
                Assert.AreEqual(1, sessions[1L].Count);
                Assert.AreSame(session1, sessions[1L][0]);
                Assert.AreEqual(1, sessions[2L].Count);
                Assert.AreSame(session2, sessions[2L][0]);
            });

            _Presenter.Initialise(_View.Object);

            _LogDatabase.Verify(d => d.FetchAll(It.IsAny<IList<LogClient>>(), It.IsAny<IDictionary<long, IList<LogSession>>>()), Times.Once());
            _View.Verify(v => v.ShowClientsAndSessions(It.IsAny<IList<LogClient>>(), It.IsAny<IDictionary<long, IList<LogSession>>>()), Times.Once());
        }
        public void LogClient_IsLocal_Is_Conversion_Of_IpAddress_Property()
        {
            var worksheet = new ExcelWorksheetData(TestContext);

            var logClient = new LogClient() { IpAddress = worksheet.EString("IpAddress") };

            Assert.AreEqual(worksheet.Bool("IsLocal"), logClient.IsLocal);
        }
 /// <summary>
 /// Updates an existing record on the database.
 /// </summary>
 /// <param name="connection"></param>
 /// <param name="transaction"></param>
 /// <param name="log"></param>
 /// <param name="client"></param>
 public void Update(IDbConnection connection, IDbTransaction transaction, TextWriter log, LogClient client)
 {
     SqlPreparedCommand command = PrepareCommand(connection, transaction, "Update",
         String.Format("UPDATE [{0}] SET [IpAddress] = ?, [ReverseDns] = ?, [ReverseDnsDate] = ?" +
                       " WHERE [Id] = ?", TableName), 4);
     Sql.SetParameters(command, client.IpAddress, client.ReverseDns, client.ReverseDnsDate, client.Id);
     Sql.LogCommand(log, command.Command);
     command.Command.ExecuteNonQuery();
 }
        public void LogClient_Constructor_Inititalises_To_Known_State_And_Properties_Work()
        {
            var logClient = new LogClient();

            TestUtilities.TestProperty(logClient, "Id", 0L, 12L);
            TestUtilities.TestProperty(logClient, "IpAddress", null, "Ab");
            TestUtilities.TestProperty(logClient, "ReverseDns", null, "Hh");
            TestUtilities.TestProperty(logClient, "ReverseDnsDate", null, DateTime.Now);
        }
        public void LogClient_Address_Is_Conversion_Of_IpAddress_Property()
        {
            var worksheet = new ExcelWorksheetData(TestContext);

            var logClient = new LogClient() { IpAddress = worksheet.EString("IpAddress") };

            var expected = worksheet.String("Address");
            if(expected == null) Assert.IsNull(logClient.Address);
            else                 Assert.AreEqual(expected, logClient.Address.ToString());
        }
        public void ConnectionClientLogPresenter_Initialise_Looks_Up_ReverseDNS_Details_Via_Provider()
        {
            var client1 = new LogClient() { IpAddress = "1.2.3.4" };
            var ipAddress = IPAddress.Parse(client1.IpAddress);
            _LogClients.Add(client1);

            _Presenter.Initialise(_View.Object);

            _Provider.Verify(p => p.LookupReverseDns(It.IsAny<IPAddress>()), Times.Once());
            _Provider.Verify(p => p.LookupReverseDns(ipAddress), Times.Once());
        }
        /// <summary>
        /// See interface docs.
        /// </summary>
        /// <param name="ipAddress"></param>
        /// <returns></returns>
        public LogSession EstablishSession(string ipAddress)
        {
            LogSession result = null;

            lock(_SyncLock) {
                CreateConnection();

                var client = _ClientTable.SelectByIpAddress(_Connection, _TransactionHelper.Transaction, null, ipAddress);
                if(client == null) {
                    client = new LogClient() { IpAddress = ipAddress };
                    _ClientTable.Insert(_Connection, _TransactionHelper.Transaction, null, client);
                }

                result = new LogSession() {
                    ClientId = client.Id,
                    StartTime = Provider.UtcNow,
                    EndTime = Provider.UtcNow,
                };
                _SessionTable.Insert(_Connection, _TransactionHelper.Transaction, null, result);
            }

            return result;
        }
        /// <summary>
        /// See interface docs.
        /// </summary>
        /// <param name="client"></param>
        public void UpdateClient(LogClient client)
        {
            if(client == null) throw new ArgumentNullException("client");
            if(_Connection == null) throw new InvalidOperationException("The connection must be opened before the client can be updated");

            lock(_SyncLock) {
                _ClientTable.Update(_Connection, _TransactionHelper.Transaction, null, client);
            }
        }
        public void ConnectionClientLogPresenter_Initialise_Logs_Any_Other_Exceptions_From_ReverseDNS_Lookup()
        {
            var client = new LogClient() { IpAddress = "1.2.3.4" };
            _LogClients.Add(client);

            var exception = new InvalidOperationException("Other exception");
            _Provider.Setup(p => p.LookupReverseDns(It.IsAny<IPAddress>())).Callback(() => { throw exception; });

            _Log.Setup(g => g.WriteLine(It.IsAny<string>(), It.IsAny<object[]>())).Callback((string format, object[] parameters) => {
                Assert.IsTrue(parameters.Where(p => {
                    var s = p as string;
                    return s != null && s.Contains("Other exception");
                }).Any());
            });

            _Presenter.Initialise(_View.Object);

            Assert.AreEqual(null, client.ReverseDns);
            Assert.AreEqual(null, client.ReverseDnsDate);
            _LogDatabase.Verify(db => db.UpdateClient(client), Times.Never());
            _View.Verify(db => db.RefreshClientReverseDnsDetails(client), Times.Never());
            _Log.Verify(g => g.WriteLine(It.IsAny<string>(), It.IsAny<object[]>()), Times.Once());
        }
        public void ConnectionClientLogPresenter_Initialise_Ignores_Clients_Whose_IPAddress_Produces_A_SocketException()
        {
            var client = new LogClient() { IpAddress = "1.2.3.4" };
            _LogClients.Add(client);

            _Provider.Setup(p => p.LookupReverseDns(It.IsAny<IPAddress>())).Callback(() => { throw new SocketException(); });

            _Presenter.Initialise(_View.Object);

            Assert.AreEqual(null, client.ReverseDns);
            Assert.AreEqual(null, client.ReverseDnsDate);
            _LogDatabase.Verify(db => db.UpdateClient(client), Times.Never());
            _View.Verify(db => db.RefreshClientReverseDnsDetails(client), Times.Never());
            _Log.Verify(g => g.WriteLine(It.IsAny<string>(), It.IsAny<string>()), Times.Never());
        }
        public void ConnectionClientLogPresenter_Initialise_Uses_IP_Address_If_Reverse_Dns_Gives_Argument_Exception()
        {
            var client = new LogClient() { IpAddress = "1.2.3.4" };
            _LogClients.Add(client);

            _Provider.Setup(p => p.LookupReverseDns(It.IsAny<IPAddress>())).Callback(() => { throw new ArgumentException(); });

            _Presenter.Initialise(_View.Object);

            Assert.AreEqual("1.2.3.4", client.ReverseDns);
            _LogDatabase.Verify(db => db.UpdateClient(client), Times.Once());
            _View.Verify(db => db.RefreshClientReverseDnsDetails(client), Times.Once());
            _Log.Verify(g => g.WriteLine(It.IsAny<string>(), It.IsAny<string>()), Times.Never());
        }
        public void ConnectionClientLogPresenter_Initialise_Updates_Display_With_Reverse_DNS_Details()
        {
            var client = new LogClient() { IpAddress = "1.2.3.4" };
            _LogClients.Add(client);

            _Provider.Setup(p => p.LookupReverseDns(It.IsAny<IPAddress>())).Returns("Reverse Result");

            _Presenter.Initialise(_View.Object);

            _View.Verify(v => v.RefreshClientReverseDnsDetails(client), Times.Once());
        }
 /// <summary>
 /// Deletes a record from the database.
 /// </summary>
 /// <param name="connection"></param>
 /// <param name="transaction"></param>
 /// <param name="log"></param>
 /// <param name="client"></param>
 public void Delete(IDbConnection connection, IDbTransaction transaction, TextWriter log, LogClient client)
 {
     SqlPreparedCommand command = PrepareCommand(connection, transaction, "Delete",
         String.Format("DELETE FROM [{0}] WHERE [Id] = ?", TableName), 1);
     Sql.SetParameters(command, client.Id);
     Sql.LogCommand(log, command.Command);
     command.Command.ExecuteNonQuery();
 }
        public void ConnectionClientLogPresenter_Initialise_Does_Not_Perform_Reverse_DNS_Lookup_On_Clients_That_Already_Have_Reverse_DNS_Details()
        {
            var client1 = new LogClient() { ReverseDns = "ABC" };
            var client2 = new LogClient() { ReverseDnsDate = new DateTime(2010, 9, 8) };
            var client3 = new LogClient() { ReverseDns = "ABC", ReverseDnsDate = new DateTime(2010, 9, 8) };

            client1.IpAddress = client2.IpAddress = client3.IpAddress = "1.2.3.4";

            _LogClients.Add(client1);
            _LogClients.Add(client2);
            _LogClients.Add(client3);

            _Provider.Setup(p => p.InvokeOnBackgroundThread(It.IsAny<Action<IList<LogClient>>>(), It.IsAny<IList<LogClient>>())).Callback((Action<IList<LogClient>> callback, IList<LogClient> clients) => {
                Assert.AreEqual(2, clients.Count);
                Assert.IsTrue(clients.Where(c => c == client1).Any());
                Assert.IsTrue(clients.Where(c => c == client2).Any());
            });

            _Presenter.Initialise(_View.Object);

            _Provider.Verify(p => p.InvokeOnBackgroundThread(It.IsAny<Action<IList<LogClient>>>(), It.IsAny<IList<LogClient>>()), Times.Once());
        }
        public void ConnectionClientLogPresenter_Initialise_Starts_Reverse_DNS_Lookups_On_Background_Thread()
        {
            var client = new LogClient() { IpAddress = "2.3.2.3" };
            _LogClients.Add(client);
            _Provider.Setup(p => p.InvokeOnBackgroundThread(It.IsAny<Action<IList<LogClient>>>(), It.IsAny<IList<LogClient>>())).Callback((Action<IList<LogClient>> callback, IList<LogClient> clients) => {
                Assert.IsNotNull(callback);
                Assert.AreEqual(1, clients.Count);
                Assert.AreSame(client, clients[0]);
            });

            _Presenter.Initialise(_View.Object);

            _Provider.Verify(p => p.InvokeOnBackgroundThread(It.IsAny<Action<IList<LogClient>>>(), It.IsAny<IList<LogClient>>()), Times.Once());
        }
 public void UpdateClient(LogClient client)
 {
     ;
 }
 /// <summary>
 /// See interface docs.
 /// </summary>
 /// <param name="client"></param>
 public void RefreshClientReverseDnsDetails(LogClient client)
 {
     if(InvokeRequired) BeginInvoke(new MethodInvoker(() => { RefreshClientReverseDnsDetails(client); }));
     else               connectionClientListControl.RefreshClientReverseDnsDetails(client);
 }
        public void ConnectionClientLogPresenter_Initialise_Stores_Reverse_DNS_Lookup_On_Database()
        {
            var client = new LogClient() { IpAddress = "1.2.3.4" };
            _LogClients.Add(client);

            var now = new DateTime(2011, 2, 3, 4, 5, 6, 7);
            _Provider.Setup(p => p.UtcNow).Returns(now);
            _Provider.Setup(p => p.LookupReverseDns(It.IsAny<IPAddress>())).Returns("Reverse Result");

            _LogDatabase.Setup(db => db.UpdateClient(It.IsAny<LogClient>())).Callback((LogClient saveClient) => {
                Assert.AreSame(client, saveClient);
                Assert.AreEqual(now, saveClient.ReverseDnsDate);
                Assert.AreEqual("Reverse Result", saveClient.ReverseDns);
            });

            _Presenter.Initialise(_View.Object);

            _LogDatabase.Verify(db => db.UpdateClient(It.IsAny<LogClient>()), Times.Once());
        }