public void testStoreUser()
        {
            try
            {
                //week 3
                //IUserSvc ics = factory.getUserSvc();

                //week 4
                IUserSvc ics = (IUserSvc)factory.getService("IUserSvc");

                // First let's store the user
                Assert.True(ics.storeUser(u));

                // Then let's read it back in
                u = ics.getUser(u.id);
                Assert.True(u.validate());

                // Update user
                Assert.True(BCrypt.CheckPassword("pass123", u.password));

                u.password = BCrypt.HashPassword("pass12345", BCrypt.GenerateSalt());
                Assert.True(ics.storeUser(u));

                // Finally, let's cleanup the file that was created
                Assert.True(ics.deleteUser(u.id));
            }
            catch (Exception e)
            {
                dLog.Error("Exception in testStoreUser: "******"\n" + e.StackTrace);
                Assert.Fail(e.Message + "\n" + e.StackTrace);
            }
        }
        public void testInvalidUser()
        {
            try
            {
                User u = new User();

                Assert.False(u.validate());
            }
            catch (Exception e)
            {
                dLog.Error("Exception in testInvalidUser: "******"\n" + e.StackTrace);
                Assert.Fail(e.Message + "\n" + e.StackTrace);
            }
        }
        public void testNotEqualsUser()
        {
            try
            {
                User u = new User(1, "JimB", BCrypt.HashPassword("pass123", BCrypt.GenerateSalt()));
                User x = new User();

                Assert.False(u.Equals(x));
            }
            catch (Exception e)
            {
                dLog.Error("Exception in testNotEqualsUser: "******"\n" + e.StackTrace);
                Assert.Fail(e.Message + "\n" + e.StackTrace);
            }
        }
        public void testValidateUser()
        {
            try
            {
                User u = new User();
                u.id = 1;
                u.userName = "******";
                u.password = BCrypt.HashPassword("pass123", BCrypt.GenerateSalt());

                Assert.True(u.validate());
            }
            catch (Exception e)
            {
                dLog.Error("Exception in testValidateUser: "******"\n" + e.StackTrace);
                Assert.Fail(e.Message + "\n" + e.StackTrace);
            }
        }
        public void testSocketManager()
        {
            try
            {
                // create new user and pass to service layer to store
                User tUser = new User(1, "JimB", BCrypt.HashPassword("pass123", BCrypt.GenerateSalt()));
                Assert.True(userSvc.storeUser(tUser));

                neatoServerThread = new Thread(neatoServer.startServer);
                neatoServerThread.Name = "Server Thread";
                neatoServerThread.Start();

                //sleep thread before checking
                dLog.Debug("Connection Manager thread started.  Sleeping 3 secs before checking status");
                Thread.Sleep(3000);

                // test if socket is connected
                Assert.True(neatoServer.isConnected());

                // test if we can connect
                IPHostEntry ipHostInfo = Dns.GetHostEntry(Dns.GetHostName());
                IPEndPoint localEP = new IPEndPoint(ipHostInfo.AddressList[0], 8000);
                foreach (IPAddress ip in ipHostInfo.AddressList)
                    if (ip.AddressFamily == AddressFamily.InterNetwork)
                        localEP = new IPEndPoint(ip, 8000);

                dLog.Debug("Local address and port: " + localEP.Address.ToString() + " | " + localEP.Port.ToString());

                client = new Socket(localEP.Address.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
                client.Connect(localEP.Address, 8000);

                //sleep thread before checking
                dLog.Debug("Client connected.  Sleeping 3 secs before checking status");
                Thread.Sleep(3000);

                // test if we now have one connection
                Assert.AreEqual(1, neatoServer.getNumberOfConnections());

                // test Socket Manager
                String inputStr = readObject();
                writeObject(tUser.userName);
                inputStr = readObject();
                writeObject("pass123");
                inputStr = readObject();

                //check out input
                Assert.True(inputStr.Split(' ')[0].Equals("OK"));

                writeObject("exit");

                // disconnect client
                if (client.Connected)
                    client.Shutdown(SocketShutdown.Both);

                if (client.IsBound)
                {
                    client.Close();
                    client = null;
                }

                // test setExit
                Assert.True(neatoServer.setExit(true));

                //sleep thread before checking
                dLog.Debug("Set exit.  Sleeping 3 secs before checking status");
                Thread.Sleep(3000);

                // test if socket is now disconnected
                Assert.False(neatoServer.isConnected());

                // test thread termination
                if (neatoServerThread.IsAlive)
                {
                    if (!neatoServerThread.Join(5000))
                    {
                        Assert.Fail("Thread did not return in timely fashion");
                    }
                }

                // remove the test user
                Assert.True(userSvc.deleteUser(tUser.id));
            }
            catch (Exception e)
            {
                dLog.Error("Exception in testConnectionManager: " + e.Message + "\n" + e.StackTrace);
                Assert.Fail(e.Message + "\n" + e.StackTrace);
            }
        }
        /// <summary>
        /// This method stores a user.
        /// </summary>
        /// <param name="u">The user object to store</param>
        /// <returns>Success/Failure</returns>
        public Boolean storeUser(User u)
        {
            dLog.Info("Entering method storeUser | ID: " + u.id);
            Boolean result = false;

            try
            {
                MongoServer server = MongoServer.Create();
                //MongoCredentials credentials = new MongoCredentials("username", "password");
                MongoDatabase db = server.GetDatabase("neatoBurrito");
                //MongoDatabase db = server.GetDatabase("neatoBurrito", credentials);

                using (server.RequestStart(db))
                {
                    MongoCollection<BsonDocument> coll = db.GetCollection("user");
                    var query = new QueryDocument("id", u.id);

                    dLog.Debug("Finding if user exists");
                    BsonDocument myDoc = coll.FindOne(query);

                    query.Add("username", u.userName);
                    query.Add("password", u.password);

                    //ensure we were passed a valid object before attempting to write
                    if (myDoc == null)
                    {
                        dLog.Debug("Inserting user");
                        coll.Insert(query);

                        result = true;
                    }
                    else
                    {
                        var update = new UpdateDocument();
                        update.Add(query.ToBsonDocument());
                        dLog.Debug("Updating user");
                        dLog.Debug("myDoc: " + myDoc.ToString());
                        dLog.Debug("update Query: " + update.ToString());

                        SafeModeResult wr = coll.Update(new QueryDocument("id", u.id), update, SafeMode.True);

                        dLog.Debug("SafeModeResult: " + wr.Ok);
                        if (wr.LastErrorMessage == null && wr.Ok)
                        {
                            result = true;
                        }
                        else
                        {
                            dLog.Debug("SafeModeResult: " + wr.LastErrorMessage);
                        }
                    }
                }
            }
            catch (Exception e2)
            {
                dLog.Error("Exception in storeEUser: " + e2.Message);
            }
            finally
            {
                //using statement above already calls RequestDone()
            }

            return result;
        }
        /// <summary>
        /// This method retrieves a user.
        /// </summary>
        /// <param name="id">Unique ID of user to retrieve</param>
        /// <returns>user object</returns>
        public User getUser(Int32 id)
        {
            dLog.Info("Entering method getUser | ID: " + id);
            User u = new User();

            try
            {
                MongoServer server = MongoServer.Create();
                //MongoCredentials credentials = new MongoCredentials("username", "password");
                MongoDatabase db = server.GetDatabase("neatoBurrito");
                //MongoDatabase db = server.GetDatabase("neatoBurrito", credentials);

                using (server.RequestStart(db))
                {
                    MongoCollection<BsonDocument> coll = db.GetCollection("user");
                    var query = new QueryDocument("id", id);

                    BsonDocument myDoc = coll.FindOne(query);

                    //ensure we were passed a valid object before attempting to read
                    if (myDoc != null)
                    {
                        dLog.Debug("myDoc: " + myDoc.ToString());

                        #region Read Fields
                        u.id = id;
                        u.userName = myDoc["username"].AsString;
                        u.password = myDoc["password"].AsString;
                        #endregion
                    }
                    dLog.Debug("Finishing setting user");
                }
            }
            catch (Exception e2)
            {
                dLog.Error("Exception in getUser: "******"\n" + e2.StackTrace);
                u = new User();
            }
            finally
            {
                //using statement above already calls RequestDone()
            }

            return u;
        }
 protected void SetUp()
 {
     XmlConfigurator.Configure(new FileInfo("config/log4net.properties"));
     factory = Factory.getInstance();
     u = new User(1, "JimB", BCrypt.HashPassword("pass123", BCrypt.GenerateSalt()));
 }
        /// <summary>
        /// This method stores a user.
        /// </summary>
        /// <param name="u">The user object to store</param>
        /// <returns>Success/Failure</returns>
        public Boolean storeUser(User u)
        {
            dLog.Info("Entering method storeUser | ID: " + u.id);
            Boolean result = false;
            ISession session = null;

            try
            {
                using (session = getSession())
                {
                    using (ITransaction transaction = session.BeginTransaction())
                    {
                        session.Save(u);
                        transaction.Commit();

                        if (transaction.WasCommitted)
                            result = true;
                    }
                }
            }
            catch (Exception e2)
            {
                dLog.Error("Exception in storeUser: " + e2.Message);
            }
            finally
            {
                //ensure that session is close regardless of the errors in try/catch
                if (session != null && session.IsOpen)
                    session.Close();
            }

            return result;
        }
        /// <summary>
        /// This method retrieves a user.
        /// </summary>
        /// <param name="id">Unique ID of user to retrieve</param>
        /// <returns>user object</returns>
        public User getUser(Int32 id)
        {
            dLog.Info("Entering method getUser | ID: " + id);
            User u = new User();
            ISession session = null;

            try {
                using (session = getSession()) {
                    using (ITransaction transaction = session.BeginTransaction())
                    {
                        IQuery query = session.CreateQuery(@"FROM User WHERE id = :id");
                        query.SetParameter("id", id);

                        u = query.List<User>()[0];
                    }

                }
            }
            catch (Exception e2)
            {
                dLog.Error("Exception in getUser: "******"\n" + e2.StackTrace);
                u = new User();
            }
            finally
            {
                //ensure that session is close regardless of the errors in try/catch
                if (session != null && session.IsOpen)
                    session.Close();
            }

            return u;
        }
        public void run()
        {
            try
            {
                String inputStr = "";
                writeObject("Burrito POS Server Connected. Enter Username: "******"exit") && !this.exit)
                {
                    if (tUser.userName == null)
                    {
                        tUser.userName = inputStr;
                        writeObject("OK User " + tUser.userName + ", enter password: "******"Username: "******" | Password: "******"Stored user: "******" | Password: "******"OK User verified. Enter command: ");
                            auth = true;
                        }
                        else
                        {
                            tUser = new User();
                            writeObject("ERROR Invalid Credentials. Enter Username: "******"exit"))
                        {
                            writeObject("OK Command " + tUser.userName + " entered. Enter command: ");
                        }
                    }

                    inputStr = readObject();
                }
            }
            catch (Exception e)
            {
                dLog.Error("Exception in run: " + e.Message + "\n" + e.StackTrace);
            }
            finally
            {
                setExit(true);
            }
        }