public void GivenGetLastNameMessage_WhenCallingGetStudentByLastNameHandler_ThenCorrespondingReplyIsSend() { // Arrange var repositry = new Mock <ISqlRepository <IStudent> >(MockBehavior.Strict); repositry.Setup(s => s.List(null, null, 0, 0)).Returns(new List <Student> { new Student("test1", "1") { Id = 1 }, new Student("test2", "2") { Id = 2 } }); var claimsHelper = new Mock <IClaimsHelper>(MockBehavior.Strict); claimsHelper.Setup(h => h.GetClaims()).Returns(new Claims() { UserClaims = new UserClaim() { Username = "******" } }); var context = new Mock <IConsumeContext>(MockBehavior.Strict); context.Setup(c => c.Reply(It.IsAny <GetStudentLastNameReplyMessage>())); var handler = new SqlGetStudentLastNameHandler(repositry.Object, claimsHelper.Object) { Context = context.Object }; GetStudentLastnameMessage request = new GetStudentLastnameMessage(Guid.NewGuid(), "2"); GetStudentLastNameReplyMessage reply = null; // Act handler.Execute(request); // Assert context.Verify(c => c.Reply(It.IsAny <GetStudentLastNameReplyMessage>()), Times.Once); }
public void Execute(GetStudentLastnameMessage message) { var username = _claimsHelper.GetClaims().UserClaims.Username; if (username == "ofebres-cordero") { var replyMessage = new GetStudentLastNameReplyMessage(message.CorrelationId) { Student = (Student)_repository.List().FirstOrDefault(_ => _.LastName.ToLowerInvariant() == message.LastName.ToLowerInvariant()) }; Context.Reply(replyMessage); } else { Console.WriteLine("Request Failed - Invalid Token"); Context.Reply(new GetAllStudentsReplyMessage(message.CorrelationId)); } }
public void Execute(GetStudentLastnameMessage message) { var test = Thread.CurrentPrincipal.Identity.Name; if (test == "ofebres-cordero") { var replyMessage = new GetStudentLastNameReplyMessage(message.CorrelationId) { Student = (Student)_studentRepository.List().FirstOrDefault(_ => _.LastName == message.LastName) }; Context.Reply(replyMessage); } else { Console.WriteLine("Request Failed - Invalid Token"); } Context.Reply(new GetAllStudentsReplyMessage(message.CorrelationId)); }
public void SearchStudentGivenLastName_WhenCallingGetByName_ThenThatReplyShouldShowOneStudent() { // Arrange var students = new List <Student> { new Student("Joe", "Smith1") { Id = 1, Grades = new List <Grade> { new Grade { Id = 1, Score = 50, Subject = "Maths", StudentId = 1 }, new Grade { Id = 1, Score = 50, Subject = "maths1", StudentId = 1 }, new Grade { Id = 1, Score = 50, Subject = "twoMaths", StudentId = 1 } } }, new Student("Oli2", "Test2") { Id = 2 }, new Student("Oli3", "Test3") { Id = 3 }, new Student("Oli4", "Test4") { Id = 4 } }; var testName = "Test4"; GetStudentLastnameMessage request = null; GetStudentLastNameReplyMessage reply = null; var queryCountWrapper = new Mock <IQueryCountWrapper>(); queryCountWrapper.SetupGet(qcw => qcw.QueryCount).Returns(0); var bus = new Mock <IBus>(MockBehavior.Strict); bus .Setup(b => b.SendRequest <GetStudentLastnameMessage, GetStudentLastNameReplyMessage> (It.IsAny <string>(), It.IsAny <GetStudentLastnameMessage>(), null, It.IsAny <int>())) .Returns <string, GetStudentLastnameMessage, Dictionary <string, string>, int>( (endpoint, message, headers, timeout) => { request = message; reply = new GetStudentLastNameReplyMessage(message.CorrelationId) { Student = students.First(_ => _.LastName == testName) }; return(reply); }); var claimsHelper = new Mock <IClaimsHelper>(MockBehavior.Strict); var controller = new StudentController(bus.Object, claimsHelper.Object, queryCountWrapper.Object); // Act var result = controller.GetByName(testName); // Assert var expectedResult = reply.Student; Assert.Equal(expectedResult, result); }