public void testStoreEmployee() { try { //week 3 //IEmployeeSvc ics = factory.getEmployeeSvc(); //week 4 IEmployeeSvc ics = (IEmployeeSvc)factory.getService("IEmployeeSvc"); // First let's store the Employee Assert.True(ics.storeEmployee(e)); // Then let's read it back in e = ics.getEmployee(e.id); Assert.True(e.validate()); // Update Employee e.lastName = "Smith"; Assert.True(ics.storeEmployee(e)); // Finally, let's cleanup the file that was created Assert.True(ics.deleteEmployee(e.id)); } catch(Exception e) { Console.WriteLine("Exception in testStoreEmployee: " + e.Message + "\n" + e.StackTrace); Assert.Fail(e.Message + "\n" + e.StackTrace); } }
public void testInvalidEmployee() { try { Employee e = new Employee(); Assert.False(e.validate()); } catch(Exception e) { Console.WriteLine("Exception in testInvalidEmployee: " + e.Message + "\n" + e.StackTrace); Assert.Fail(e.Message + "\n" + e.StackTrace); } }
public void testNotEqualsEmployee() { try { Employee e = new Employee("Jim","Bloom",1); Employee d = new Employee(); Assert.False(e.Equals(d)); } catch(Exception e) { Console.WriteLine("Exception in testNotEqualsEmployee: " + e.Message + "\n" + e.StackTrace); Assert.Fail(e.Message + "\n" + e.StackTrace); } }
public void testValidateEmployee() { try { Employee e = new Employee(); e.id = 1; e.firstName = "Jim"; e.lastName = "Bloom"; Assert.True(e.validate()); } catch(Exception e) { Console.WriteLine("Exception in testValidateEmployee: " + e.Message + "\n" + e.StackTrace); Assert.Fail(e.Message + "\n" + e.StackTrace); } }
public Boolean login(Employee e, String password) { Boolean result = false; try { 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); String inputStr = readObject(); writeObject(e.firstName); inputStr = readObject(); writeObject(password); inputStr = readObject(); //check out input if (inputStr.Split(' ')[0].Equals("OK")) result = true; writeObject("exit"); } catch (Exception e1) { dLog.Error("Exception in login: " + e1.Message); } finally { if (client.Connected) client.Shutdown(SocketShutdown.Both); if (client.IsBound) { client.Close(); client = null; } } return result; }
private void loginBtn_Click(object sender, EventArgs e) { try { if (userTxt.Text != "" && passTxt.Text != "") { dLog.Debug("User/Pass are good"); Employee emp = new Employee(userTxt.Text, userTxt.Text, 1); //if (authSvc.login(emp, BCrypt.HashPassword(passTxt.Text, BCrypt.GenerateSalt()))) if(authSvc.login(emp, passTxt.Text)) { dLog.Debug("User authenticated; launching Neato Burrito App"); this.Hide(); MainUI mainUI = new MainUI(); mainUI.ShowDialog(); this.Close(); } else { MessageBox.Show("Invalid Credentials.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } } else { MessageBox.Show("Please enter a username and password.", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Information); } } catch (Exception e1) { dLog.Error("Exception in loginBtn_Click: " + e1.Message + "\n" + e1.StackTrace); } }
/// <summary> /// This method retrieves a employee. /// </summary> /// <param name="id">Unique ID of employee to retrieve</param> /// <returns>customer object</returns> public Employee getEmployee(Int32 id) { dLog.Info("Entering method getEmployee | ID: " + id); Employee e = new Employee(); SqlDataReader rs = null; SqlConnection conn = null; SqlCommand stmt = null; try { String sqlStr = "SELECT FirstName, isManager, LastName FROM Employee WHERE id = @id"; conn = new SqlConnection(connString); conn.Open(); stmt = new SqlCommand(sqlStr, conn); stmt.Parameters.Add(new SqlParameter("@id", id)); rs = stmt.ExecuteReader(); while (rs.Read()) { dLog.Info("Got the " + rs.FieldCount + " fields of the record"); e.id = id; #region Read Fields if (!rs.IsDBNull(0)) e.firstName = rs.GetString(0); if (!rs.IsDBNull(1)) e.isManager = rs.GetBoolean(1); if (!rs.IsDBNull(2)) e.lastName = rs.GetString(2); #endregion } } catch (SqlException e1) { dLog.Error("SqlException in getEmployee: " + e1.Message + "\n" + e1.StackTrace); e = new Employee(); } catch (Exception e2) { dLog.Error("Exception in getEmployee: " + e2.Message + "\n" + e2.StackTrace); e = new Employee(); } finally { if (rs != null && !rs.IsClosed) rs.Close(); if (conn != null && conn.State == System.Data.ConnectionState.Open) conn.Close(); } return e; }
/// <summary> /// This method stores a employee. /// </summary> /// <param name="e">The employee object to store</param> /// <returns>Success/Failure</returns> public Boolean storeEmployee(Employee e) { dLog.Info("Entering method storeEmployee | ID: " + e.id); Boolean result = false; SqlConnection conn = null; SqlCommand stmt = null; try { String sqlStr = "SELECT COUNT(1) FROM Employee WHERE id = @id"; conn = new SqlConnection(connString); conn.Open(); stmt = new SqlCommand(sqlStr, conn); stmt.Parameters.Add(new SqlParameter("@id", e.id)); if (Int32.Parse(stmt.ExecuteScalar().ToString()) > 0) { //if first is a valid row, then we need to do an update dLog.Info("Updating employee in database"); sqlStr = "UPDATE Employee SET FirstName=@FirstName, isManager=@isManager, LastName=@LastName WHERE id=@id"; } else { //if first is null, then we need to do an insert dLog.Info("Inserting employee into database"); sqlStr = "INSERT INTO Employee (FirstName,isManager, LastName, id) "; sqlStr += "VALUES (@FirstName, @isManager, @LastName, @id)"; } dLog.Info("SQL Statement: " + sqlStr); stmt = new SqlCommand(sqlStr, conn); #region Add SQL Parameters stmt.Parameters.Add(new SqlParameter("@id", e.id)); stmt.Parameters.Add(new SqlParameter("@FirstName", e.firstName)); stmt.Parameters.Add(new SqlParameter("@isManager", e.isManager)); stmt.Parameters.Add(new SqlParameter("@LastName", e.lastName)); #endregion if (stmt.ExecuteNonQuery() > 0) result = true; } catch (SqlException e1) { dLog.Error("SqlException in storeEmployee: " + e1.Message); } catch (Exception e2) { dLog.Error("Exception in storeEmployee: " + e2.Message); } finally { if (conn.State == System.Data.ConnectionState.Open) conn.Close(); } return result; }
/// <summary> /// This method retrieves a employee. /// </summary> /// <param name="id">Unique ID of employee to retrieve</param> /// <returns>employee object</returns> public Employee getEmployee(Int32 id) { dLog.Info("Entering method getEmployee | ID: " + id); Employee e = new Employee(); ISession session = null; try { using (session = getSession()) { using (ITransaction transaction = session.BeginTransaction()) { IQuery query = session.CreateQuery(@"FROM Employee WHERE id = :id"); query.SetParameter("id", id); e = query.List<Employee>()[0]; } } } catch (Exception e2) { dLog.Error("Exception in getEmployee: " + e2.Message + "\n" + e2.StackTrace); e = new Employee(); } finally { //ensure that session is close regardless of the errors in try/catch if (session != null && session.IsOpen) session.Close(); } return e; }
/// <summary> /// This method stores a employee. /// </summary> /// <param name="e">The employee object to store</param> /// <returns>Success/Failure</returns> public Boolean storeEmployee(Employee e) { dLog.Info("Entering method storeEmployee | ID: " + e.id); Boolean result = false; ISession session = null; try { using (session = getSession()) { using (ITransaction transaction = session.BeginTransaction()) { session.Save(e); transaction.Commit(); if (transaction.WasCommitted) result = true; } } } catch (Exception e2) { dLog.Error("Exception in storeEmployee: " + 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 stores a employee. /// </summary> /// <param name="e">The employee object to store</param> /// <returns>Success/Failure</returns> public Boolean storeEmployee(Employee e) { dLog.Info("Entering method storeEmployee | ID: " + e.id); Boolean result = false; try { MongoServer server = MongoServer.Create(); MongoDatabase db = server.GetDatabase("neatoBurrito"); //MongoCredentials credentials = new MongoCredentials("username", "password"); //MongoDatabase salaries = server.GetDatabase("salaries", credentials); using (server.RequestStart(db)) { MongoCollection<BsonDocument> coll = db.GetCollection("employee"); var query = new QueryDocument("id", e.id); dLog.Debug("Finding if employee exists"); BsonDocument myDoc = coll.FindOne(query); query.Add("firstName", e.firstName); query.Add("isManager", e.isManager); query.Add("lastName", e.lastName); //ensure we were passed a valid object before attempting to write if (myDoc == null) { dLog.Debug("Inserting employee"); coll.Insert(query); result = true; } else { var update = new UpdateDocument(); update.Add(query.ToBsonDocument()); dLog.Debug("Updating employee"); dLog.Debug("myDoc: " + myDoc.ToString()); dLog.Debug("update Query: " + update.ToString()); SafeModeResult wr = coll.Update(new QueryDocument("id", e.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 storeEmployee: " + e2.Message); } finally { //using statement above already calls RequestDone() } return result; }
/// <summary> /// This method retrieves a employee. /// </summary> /// <param name="id">Unique ID of employee to retrieve</param> /// <returns>employee object</returns> public Employee getEmployee(Int32 id) { dLog.Info("Entering method getEmployee | ID: " + id); Employee e = new Employee(); try { MongoServer server = MongoServer.Create(); MongoDatabase db = server.GetDatabase("neatoBurrito"); //MongoCredentials credentials = new MongoCredentials("username", "password"); //MongoDatabase salaries = server.GetDatabase("salaries", credentials); using (server.RequestStart(db)) { MongoCollection<BsonDocument> coll = db.GetCollection("employee"); 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 e.id = id; e.firstName = myDoc["firstName"].AsString; e.isManager = myDoc["isManager"].AsBoolean; e.lastName = myDoc["lastName"].AsString; #endregion } dLog.Debug("Finishing setting employee"); } } catch (Exception e2) { dLog.Error("Exception in getEmployee: " + e2.Message + "\n" + e2.StackTrace); e = new Employee(); } finally { //using statement above already calls RequestDone() } return e; }
/// <summary> /// This method stores a employee. /// </summary> /// <param name="e">The employee object to store</param> /// <returns>Success/Failure</returns> public Boolean storeEmployee(Employee e) { dLog.Info("Entering method storeEmployee | ID: " + e.id); Stream output = null; Boolean result = false; try { //ensure we were passed a valid object before attempting to write if (e.validate()) { output = File.Open("Employee_" + e.id + ".txt", FileMode.Create); BinaryFormatter bFormatter = new BinaryFormatter(); bFormatter.Serialize(output, e); result = true; } } catch (IOException e1) { dLog.Error("IOException in storeEmployee: " + e1.Message); result = false; } catch (Exception e2) { dLog.Error("Exception in storeEmployee: " + e2.Message); result = false; } finally { //ensure that output is close regardless of the errors in try/catch if (output != null) { output.Close(); } } return result; }
protected void SetUp() { factory = Factory.getInstance(); e = new Employee("Jim", "Bloom", 1); }