using System;
using System.Security.Cryptography;
using NUnit.Framework;
using Microsoft.Practices.EnterpriseLibrary.Security;
namespace SmartInstitute.Facade.FacadeTest
{
/// <summary>
/// Test the entire Facade simulating a real use of the entire system
///
/// Here's the test plan:
/// 1. Create new student
/// 2. Try changing soem property and save
/// 3. Try deep load
/// 4. Get all courses and register the courses to the student
/// 5. Calculate assessment
/// 6. Ensure accounts record created properly
/// 7. Drop course, delete some course
/// 8. Recalculate assessment
/// 9. Ensure accounts record updated properly
/// 10. Add some payment record in accounts
/// 11. Cleanup accounts
/// </summary>
[TestFixture]
public class TestFacade
{
public TestFacade()
{
//
// TODO: Add constructor logic here
//
}
private Facade _Facade;
private Student student = new Student( 0, "First", "Middle", "Last", "Present1", "Present2",
"Permanent1", "Permanent2", Guid.NewGuid().ToString(),
1, 3.78f, 10, DateTime.Now, DateTime.Now,
DateTime.Now );
/// <summary>
/// This method is used to construct the test environment prior to running the tests.
/// </summary>
[TestFixtureSetUp]
public void Init()
{
_Facade = new Facade();
}
/// <summary>
/// This method is used to restore the environment after the tests are completed.
/// </summary>
[TestFixtureTearDown]
public void Dispose()
{
}
[Test]
public void Step_01_NewStudent()
{
_Facade.AddNewStudent( student );
Assert.IsTrue( student.ID > 0, "Student not properly inserted" );
}
[Test]
public void Step_02_SaveStudent()
{
student.StudentID = student.StudentID + "E";
Assert.IsTrue( _Facade.SaveStudent( student ) );
Assert.IsTrue( student.StudentID.EndsWith("E"), "Save did not work properly" );
}
[Test]
public void Step_03_DeepLoad()
{
student = _Facade.GetStudentDetail( student.ID );
Assert.IsNotNull( student, "Get Student Detail not working" );
Assert.IsNotNull( student.AssessmentCollection, "No assessment found" );
Assert.IsNotNull( student.AccountCollection, "No account found" );
}
[Test]
public void Step_04_AddCourses()
{
// Assign all the courses to the student
CourseCollection courses = _Facade.GetAllCoursesAndSections();
Assert.IsTrue( courses.Count >= 2, "At least 2 courses needed for test" );
CourseTakenByStudentCollection coursesTaken = new CourseTakenByStudentCollection();
// Add the first section of all course to registration
foreach( Course course in courses )
{
Assert.IsTrue( course.CourseSectionCollection.Count >= 1, "At least one section must be present for each course" );
CourseSection section = course.CourseSectionCollection[0];
CourseTakenByStudent courseTaken = new CourseTakenByStudent( 0, student.ID,
course.ID, section.ID, 1, false, DateTime.Now );
coursesTaken.Add( courseTaken );
}
// Register the selected sections
Assert.AreEqual( _Facade.RegisterCourses( student.ID, coursesTaken ), coursesTaken.Count );
// Ensure all courses are properly registered
student.CourseTakenByStudentCollection = _Facade.GetRegisteredCourses( student.ID );
Assert.AreEqual( coursesTaken.Count, student.CourseTakenByStudentCollection.Count );
}
[Test]
public void Step_05_CalculateAssessment()
{
Assessment assessment = _Facade.CalculateAssessment( student.ID );
Assert.IsNotNull( assessment, "Assessment not properly calculated" );
Assert.IsTrue( assessment.ID == 0, "Assessment should not be inserted upon calculation" );
Assert.IsTrue( _Facade.SaveAssessment( student.ID, assessment ), "Assessment not inserted properly" );
Assert.IsTrue( assessment.ID > 0, "Assessment not properly inserted in database" );
Assessment ass = _Facade.GetAssessment( student.ID );
Assert.IsNotNull( ass, "Assessment get failed" );
}
[Test]
public void Step_06_ModifyRegistration()
{
// Drop all course
foreach( CourseTakenByStudent2 courseTaken in student.CourseTakenByStudentCollection )
{
courseTaken.Dropped = true;
}
Assert.IsTrue( _Facade.SaveRegistration( student.ID, student.CourseTakenByStudentCollection ),
"Registration save failed" );
Assessment ass = _Facade.GetAssessment( student.ID );
Assert.IsNotNull( ass, "Assessment was not saved properly" );
Assert.AreEqual( ass.TuitionFee, 0, "Tuition fee is supposed to be zero for all dropped course" );
// undrop all course
foreach( CourseTakenByStudent2 courseTaken in student.CourseTakenByStudentCollection )
{
courseTaken.Dropped = false;
}
Assert.IsTrue( _Facade.SaveRegistration( student.ID, student.CourseTakenByStudentCollection ),
"Registration save failed" );
Assessment assAfter = _Facade.GetAssessment( student.ID );
Assert.IsNotNull( assAfter, "Assessment was not saved properly" );
Assert.IsTrue( assAfter.TuitionFee > 0, "Tuition fee is supposed to be greater than 0" );
}
[Test]
public void Step_07_EnsureAccounts()
{
AccountCollection accounts = _Facade.GetAccountsOfStudent( student.ID );
Assert.AreEqual( accounts.Count, 1, "There should be an account record after assessment" );
Assert.AreEqual( accounts[0].Type, (int)AccountTypeEnum.Assessment, "Invalid type of account head" );
Account payment1 = new Account( 0, student.ID, "First Payment", DateTime.Now,
0, 1000, 0, DateTime.Now, (int)AccountTypeEnum.Other, string.Empty, string.Empty, string.Empty );
Assert.IsTrue( _Facade.AddNewAccount( payment1 ), "New Account was not inserted" );
Account payment2 = new Account( 0, student.ID, "Second Payment", DateTime.Now,
0, 1000, 0, DateTime.Now, (int)AccountTypeEnum.Other, string.Empty, string.Empty, string.Empty );
Assert.IsTrue( _Facade.AddNewAccount( payment2 ), "New Account was not inserted" );
}
[Test]
public void Step_08_AddSyncTest()
{
// Refresh full object graph
student = _Facade.GetStudentDetail( student.ID );
int oldCount = student.AccountCollection.Count;
Account payment1 = new Account( 0, student.ID, "First Payment", DateTime.Now,
0, 1000, 0, DateTime.Now, (int)AccountTypeEnum.Other, string.Empty, string.Empty, string.Empty );
student.AccountCollection.Add( payment1 );
// Perform sync
Assert.IsTrue( _Facade.SyncStudent( student ), "Synchronization failed" );
// Refresh full object graph
student = _Facade.GetStudentDetail( student.ID );
// Test
Assert.AreEqual( 1, student.AssessmentCollection.Count, "Assessment was calculated" );
Assert.AreEqual( oldCount+1, student.AccountCollection.Count, "Account was not deleted" );
}
[Test]
public void Step_08_DeleteSyncTest()
{
// Refresh full object graph
student = _Facade.GetStudentDetail( student.ID );
foreach( Assessment ass in student.AssessmentCollection )
ass.MarkToDelete();
foreach( Account acc in student.AccountCollection )
acc.MarkToDelete();
foreach( CourseTakenByStudent ctbs in student.CourseTakenByStudentCollection )
ctbs.MarkToDelete();
// Perform sync
Assert.IsTrue( _Facade.SyncStudent( student ), "Synchronization failed" );
// Refresh full object graph
student = _Facade.GetStudentDetail( student.ID );
// Test
Assert.AreEqual( 1, student.AssessmentCollection.Count, "Assessment was not deleted" );
Assert.AreEqual( 1, student.AccountCollection.Count, "Account was not deleted" );
Assert.AreEqual( 0, student.CourseTakenByStudentCollection.Count, "CourseTakenByStudent was not deleted" );
}
[Test]
public void Step_80_LoginTest()
{
// Login attempt using credential
string token = _Facade.Login( "Administrator", HashStringMD5( "admin" ) );
//IToken token = _Facade.Login( "Administrator", "admin" );
Assert.IsNotNull( token, "Login failed for admnistrator using credential" );
// Invalid attempt
Assert.IsNull( _Facade.Login( Guid.NewGuid().ToString(), HashStringMD5( "admin" ) ),
"Security Bridge! Authentication not working" );
// Login attempt using security token
bool result = _Facade.Login( token );
Assert.IsTrue( result, "Login failed using security token" );
// Logout the user
_Facade.Logout( token );
Assert.IsFalse( _Facade.Login( token ), "Logout not working, token still accepted" );
// Test authorization
Assert.IsTrue( _Facade.IsInRole("Administrator", "Administrators"), "Role based security not working" );
Assert.IsTrue( _Facade.IsAuthorized("Administrator", Constants.Authorization.CAN_LOGIN), "Rule Provider not working" );
Assert.IsFalse( _Facade.IsInRole("Administrator", "Anonymous"), "Role based security not working" );
try
{
Assert.IsFalse( _Facade.IsAuthorized("Administrator", "gaga"), "Rule Provider not working" );
}
catch
{
}
}
[Test]
public void Step_99_Cleanup()
{
_Facade.DeleteStudent( student.ID );
}
private string HashStringMD5( string textToHash )
{
MD5CryptoServiceProvider MD5 = new MD5CryptoServiceProvider();
byte[] byteValue = System.Text.Encoding.UTF8.GetBytes(textToHash);
byte[] byteHash = MD5.ComputeHash(byteValue);
MD5.Clear();
return Convert.ToBase64String(byteHash);
}
}
}