public void ShouldReturnZeroSessionResources() { // Build the test harness. SessionsControllerTestHarness testHarness = new SessionsControllerTestHarness(true, true); // Mock the invocation of the GetSessions business operation. testHarness.MockedSchedulingBusinessLogicComponent .Setup(mock => mock.GetSessions(It.IsAny <IDatabaseConnection>(), It.IsAny <GetSessionsBusinessRequest>())) .Returns(Task.FromResult(new GetSessionsBusinessResponse() { // Mock the Session business response elements. Sessions = new GetSessionsBusinessResponse.SessionBusinessResponseElement[0] })) .Verifiable(); // Invoke the HTTP GET method. HttpRequestMessage httpRequestMessage = testHarness.BuildHttpRequest(HttpMethod.Get, "api/scheduling/sessions"); HttpResponseMessage httpResponseMessage = testHarness.HttpClient.SendAsync(httpRequestMessage).Result; // Verify the mocked components. testHarness.VerifyMockedComponents(); // Build the expected JSON content. StringBuilder expectedJsonContent = new StringBuilder(); expectedJsonContent.Append("["); expectedJsonContent.Append("]"); // Read the HTTP response message content. string httpResponseMessageContent = httpResponseMessage.Content.ReadAsStringAsync().Result; // Validate the HTTP response message. Assert.AreEqual(HttpStatusCode.OK, httpResponseMessage.StatusCode); Assert.AreEqual(expectedJsonContent.ToString(), httpResponseMessageContent); }
public void ShouldReturnErrorCodes() { // Build the test harness. SessionsControllerTestHarness testHarness = new SessionsControllerTestHarness(true, false); // Mock the invocation of the NewSession business operation. testHarness.MockedSchedulingBusinessLogicComponent .Setup(mock => mock.NewSession(It.IsAny <IDatabaseConnection>(), It.IsAny <NewSessionBusinessRequest>())) .Throws(new NewSessionBusinessException() { // Mock the NewSession business exception. Errors = new NewSessionBusinessException.ErrorBusinessExceptionElement[] { new NewSessionBusinessException.ErrorBusinessExceptionElement() { ErrorCode = NewSessionBusinessException.ErrorCodes.InvalidSession, ErroneousValue = null }, new NewSessionBusinessException.ErrorBusinessExceptionElement() { ErrorCode = NewSessionBusinessException.ErrorCodes.InvalidName, ErroneousValue = "Session Alpha" } } }) .Verifiable(); // Build the request JSON content. StringBuilder requestJsonContent = new StringBuilder(); requestJsonContent.Append("{"); requestJsonContent.Append("}"); // Invoke the HTTP POST method. HttpRequestMessage httpRequestMessage = testHarness.BuildHttpRequest(HttpMethod.Post, "api/scheduling/sessions", requestJsonContent.ToString()); HttpResponseMessage httpResponseMessage = testHarness.HttpClient.SendAsync(httpRequestMessage).Result; // Verify the mocked components. testHarness.VerifyMockedComponents(); // Build the expected JSON content. StringBuilder expectedJsonContent = new StringBuilder(); expectedJsonContent.Append("["); expectedJsonContent.Append("{"); expectedJsonContent.Append("\"errorCode\":\"InvalidSession\","); expectedJsonContent.Append("\"erroneousValue\":null"); expectedJsonContent.Append("},"); expectedJsonContent.Append("{"); expectedJsonContent.Append("\"errorCode\":\"InvalidName\","); expectedJsonContent.Append("\"erroneousValue\":\"Session Alpha\""); expectedJsonContent.Append("}"); expectedJsonContent.Append("]"); // Read the HTTP response message content. string httpResponseMessageContent = httpResponseMessage.Content.ReadAsStringAsync().Result; // Validate the HTTP response message. Assert.AreEqual(HttpStatusCode.BadRequest, httpResponseMessage.StatusCode); Assert.AreEqual(expectedJsonContent.ToString(), httpResponseMessageContent); }
public void ShouldReturnOneSessionResource() { // Build the test harness. SessionsControllerTestHarness testHarness = new SessionsControllerTestHarness(true, true); // Mock the invocation of the GetSessions business operation. testHarness.MockedSchedulingBusinessLogicComponent .Setup(mock => mock.GetSessions(It.IsAny <IDatabaseConnection>(), It.IsAny <GetSessionsBusinessRequest>())) .Returns(Task.FromResult(new GetSessionsBusinessResponse() { // Mock the Session business response elements. Sessions = new GetSessionsBusinessResponse.SessionBusinessResponseElement[] { // Mock the Session business response element. new GetSessionsBusinessResponse.SessionBusinessResponseElement() { SessionCode = "6dk61ufcuzp3f7vs", Name = "Session Alpha", StartDate = new DateTime(2001, 1, 1) } } })) .Verifiable(); // Invoke the HTTP GET method. HttpRequestMessage httpRequestMessage = testHarness.BuildHttpRequest(HttpMethod.Get, "api/scheduling/sessions"); HttpResponseMessage httpResponseMessage = testHarness.HttpClient.SendAsync(httpRequestMessage).Result; // Verify the mocked components. testHarness.VerifyMockedComponents(); // Build the expected JSON content. StringBuilder expectedJsonContent = new StringBuilder(); expectedJsonContent.Append("["); expectedJsonContent.Append("{"); expectedJsonContent.Append("\"session\":"); expectedJsonContent.Append("{"); expectedJsonContent.Append("\"sessionCode\":\"6dk61ufcuzp3f7vs\","); expectedJsonContent.Append("\"name\":\"Session Alpha\","); expectedJsonContent.Append("\"startDate\":\"2001-01-01T00:00:00\""); expectedJsonContent.Append("}"); expectedJsonContent.Append("}"); expectedJsonContent.Append("]"); // Read the HTTP response message content. string httpResponseMessageContent = httpResponseMessage.Content.ReadAsStringAsync().Result; // Validate the HTTP response message. Assert.AreEqual(HttpStatusCode.OK, httpResponseMessage.StatusCode); Assert.AreEqual(expectedJsonContent.ToString(), httpResponseMessageContent); }
public void ShouldReturnErrorCodes() { // Build the test harness. SessionsControllerTestHarness testHarness = new SessionsControllerTestHarness(true, false); // Mock the invocation of the NewSession business operation. testHarness.MockedSchedulingBusinessLogicComponent .Setup(mock => mock.NewSession(It.IsAny<IDatabaseConnection>(), It.IsAny<NewSessionBusinessRequest>())) .Throws(new NewSessionBusinessException() { // Mock the NewSession business exception. Errors = new NewSessionBusinessException.ErrorBusinessExceptionElement[] { new NewSessionBusinessException.ErrorBusinessExceptionElement() { ErrorCode = NewSessionBusinessException.ErrorCodes.InvalidSession, ErroneousValue = null }, new NewSessionBusinessException.ErrorBusinessExceptionElement() { ErrorCode = NewSessionBusinessException.ErrorCodes.InvalidName, ErroneousValue = "Session Alpha" } } }) .Verifiable(); // Build the request JSON content. StringBuilder requestJsonContent = new StringBuilder(); requestJsonContent.Append("{"); requestJsonContent.Append("}"); // Invoke the HTTP POST method. HttpRequestMessage httpRequestMessage = testHarness.BuildHttpRequest(HttpMethod.Post, "api/scheduling/sessions", requestJsonContent.ToString()); HttpResponseMessage httpResponseMessage = testHarness.HttpClient.SendAsync(httpRequestMessage).Result; // Verify the mocked components. testHarness.VerifyMockedComponents(); // Build the expected JSON content. StringBuilder expectedJsonContent = new StringBuilder(); expectedJsonContent.Append("["); expectedJsonContent.Append("{"); expectedJsonContent.Append("\"errorCode\":\"InvalidSession\","); expectedJsonContent.Append("\"erroneousValue\":null"); expectedJsonContent.Append("},"); expectedJsonContent.Append("{"); expectedJsonContent.Append("\"errorCode\":\"InvalidName\","); expectedJsonContent.Append("\"erroneousValue\":\"Session Alpha\""); expectedJsonContent.Append("}"); expectedJsonContent.Append("]"); // Read the HTTP response message content. string httpResponseMessageContent = httpResponseMessage.Content.ReadAsStringAsync().Result; // Validate the HTTP response message. Assert.AreEqual(HttpStatusCode.BadRequest, httpResponseMessage.StatusCode); Assert.AreEqual(expectedJsonContent.ToString(), httpResponseMessageContent); }
public void ShouldReturnBadRequest() { // Build the test harness. SessionsControllerTestHarness testHarness = new SessionsControllerTestHarness(false, false); // Build the request JSON content. StringBuilder requestJsonContent = new StringBuilder(); requestJsonContent.Append("{x}"); // Invoke the HTTP POST method. HttpRequestMessage httpRequestMessage = testHarness.BuildHttpRequest(HttpMethod.Post, "api/scheduling/sessions", requestJsonContent.ToString()); HttpResponseMessage httpResponseMessage = testHarness.HttpClient.SendAsync(httpRequestMessage).Result; // Verify the mocked components. testHarness.VerifyMockedComponents(); // Validate the HTTP response message. Assert.AreEqual(HttpStatusCode.BadRequest, httpResponseMessage.StatusCode); }
public void ShouldSucceed() { // Build the test harness. SessionsControllerTestHarness testHarness = new SessionsControllerTestHarness(true, true); // Mock the invocation of the NewSession business operation. testHarness.MockedSchedulingBusinessLogicComponent .Setup(mock => mock.NewSession( It.IsAny<IDatabaseConnection>(), It.Is<NewSessionBusinessRequest>(newSessionBusinessRequest => ( // Match the Session business request element. newSessionBusinessRequest.Session.Name == "Session Alpha" && newSessionBusinessRequest.Session.StartDate == new DateTime(2001, 1, 1) )))) .Returns(Task.FromResult(new NewSessionBusinessResponse() { // Mock the Session business response element. Session = new NewSessionBusinessResponse.SessionBusinessResponseElement() { SessionCode = "6dk61ufcuzp3f7vs" } })) .Verifiable(); // Build the request JSON content. StringBuilder requestJsonContent = new StringBuilder(); requestJsonContent.Append("{"); requestJsonContent.Append("session:"); requestJsonContent.Append("{"); requestJsonContent.Append("\"name\":\"Session Alpha\","); requestJsonContent.Append("\"startDate\":\"2001-01-01T00:00:00\""); requestJsonContent.Append("}"); requestJsonContent.Append("}"); // Invoke the HTTP POST method. HttpRequestMessage httpRequestMessage = testHarness.BuildHttpRequest(HttpMethod.Post, "api/scheduling/sessions", requestJsonContent.ToString()); HttpResponseMessage httpResponseMessage = testHarness.HttpClient.SendAsync(httpRequestMessage).Result; // Verify the mocked components. testHarness.VerifyMockedComponents(); // Build the expected JSON content. StringBuilder expectedJsonContent = new StringBuilder(); expectedJsonContent.Append("{"); expectedJsonContent.Append("\"session\":"); expectedJsonContent.Append("{"); expectedJsonContent.Append("\"sessionCode\":\"6dk61ufcuzp3f7vs\","); expectedJsonContent.Append("\"name\":\"Session Alpha\","); expectedJsonContent.Append("\"startDate\":\"2001-01-01T00:00:00\""); expectedJsonContent.Append("}"); expectedJsonContent.Append("}"); // Read the HTTP response message content. string httpResponseMessageContent = httpResponseMessage.Content.ReadAsStringAsync().Result; // Validate the HTTP response message. Assert.AreEqual(HttpStatusCode.Created, httpResponseMessage.StatusCode); Assert.AreEqual(expectedJsonContent.ToString(), httpResponseMessageContent); }
public void ShouldReturnMultipleSessionResources() { // Build the test harness. SessionsControllerTestHarness testHarness = new SessionsControllerTestHarness(true, true); // Mock the invocation of the GetSessions business operation. testHarness.MockedSchedulingBusinessLogicComponent .Setup(mock => mock.GetSessions(It.IsAny<IDatabaseConnection>(), It.IsAny<GetSessionsBusinessRequest>())) .Returns(Task.FromResult(new GetSessionsBusinessResponse() { // Mock the Session business response elements. Sessions = new GetSessionsBusinessResponse.SessionBusinessResponseElement[] { // Mock the Session business response element. new GetSessionsBusinessResponse.SessionBusinessResponseElement() { SessionCode = "6dk61ufcuzp3f7vs", Name = "Session Alpha", StartDate = new DateTime(2001, 1, 1) }, // Mock the Session business response element. new GetSessionsBusinessResponse.SessionBusinessResponseElement() { SessionCode = "n3p4y556gt9f17hw", Name = "Session Bravo", StartDate = new DateTime(2002, 2, 2) }, // Mock the Session business response element. new GetSessionsBusinessResponse.SessionBusinessResponseElement() { SessionCode = "x36s2tccz8yxp1hq", Name = "Session Charlie", StartDate = new DateTime(2003, 3, 3) } } })) .Verifiable(); // Invoke the HTTP GET method. HttpRequestMessage httpRequestMessage = testHarness.BuildHttpRequest(HttpMethod.Get, "api/scheduling/sessions"); HttpResponseMessage httpResponseMessage = testHarness.HttpClient.SendAsync(httpRequestMessage).Result; // Verify the mocked components. testHarness.VerifyMockedComponents(); // Build the expected JSON content. StringBuilder expectedJsonContent = new StringBuilder(); expectedJsonContent.Append("["); expectedJsonContent.Append("{"); expectedJsonContent.Append("\"session\":"); expectedJsonContent.Append("{"); expectedJsonContent.Append("\"sessionCode\":\"6dk61ufcuzp3f7vs\","); expectedJsonContent.Append("\"name\":\"Session Alpha\","); expectedJsonContent.Append("\"startDate\":\"2001-01-01T00:00:00\""); expectedJsonContent.Append("}"); expectedJsonContent.Append("},"); expectedJsonContent.Append("{"); expectedJsonContent.Append("\"session\":"); expectedJsonContent.Append("{"); expectedJsonContent.Append("\"sessionCode\":\"n3p4y556gt9f17hw\","); expectedJsonContent.Append("\"name\":\"Session Bravo\","); expectedJsonContent.Append("\"startDate\":\"2002-02-02T00:00:00\""); expectedJsonContent.Append("}"); expectedJsonContent.Append("},"); expectedJsonContent.Append("{"); expectedJsonContent.Append("\"session\":"); expectedJsonContent.Append("{"); expectedJsonContent.Append("\"sessionCode\":\"x36s2tccz8yxp1hq\","); expectedJsonContent.Append("\"name\":\"Session Charlie\","); expectedJsonContent.Append("\"startDate\":\"2003-03-03T00:00:00\""); expectedJsonContent.Append("}"); expectedJsonContent.Append("}"); expectedJsonContent.Append("]"); // Read the HTTP response message content. string httpResponseMessageContent = httpResponseMessage.Content.ReadAsStringAsync().Result; // Validate the HTTP response message. Assert.AreEqual(HttpStatusCode.OK, httpResponseMessage.StatusCode); Assert.AreEqual(expectedJsonContent.ToString(), httpResponseMessageContent); }
public void ShouldReturnZeroSessionResources() { // Build the test harness. SessionsControllerTestHarness testHarness = new SessionsControllerTestHarness(true, true); // Mock the invocation of the GetSessions business operation. testHarness.MockedSchedulingBusinessLogicComponent .Setup(mock => mock.GetSessions(It.IsAny<IDatabaseConnection>(), It.IsAny<GetSessionsBusinessRequest>())) .Returns(Task.FromResult(new GetSessionsBusinessResponse() { // Mock the Session business response elements. Sessions = new GetSessionsBusinessResponse.SessionBusinessResponseElement[0] })) .Verifiable(); // Invoke the HTTP GET method. HttpRequestMessage httpRequestMessage = testHarness.BuildHttpRequest(HttpMethod.Get, "api/scheduling/sessions"); HttpResponseMessage httpResponseMessage = testHarness.HttpClient.SendAsync(httpRequestMessage).Result; // Verify the mocked components. testHarness.VerifyMockedComponents(); // Build the expected JSON content. StringBuilder expectedJsonContent = new StringBuilder(); expectedJsonContent.Append("["); expectedJsonContent.Append("]"); // Read the HTTP response message content. string httpResponseMessageContent = httpResponseMessage.Content.ReadAsStringAsync().Result; // Validate the HTTP response message. Assert.AreEqual(HttpStatusCode.OK, httpResponseMessage.StatusCode); Assert.AreEqual(expectedJsonContent.ToString(), httpResponseMessageContent); }