public void Setup() { //----------------------------------------------------------------------------------------------------------- // Setup some test params //----------------------------------------------------------------------------------------------------------- _model = new ShowsRootModel() { payload = null, skip = 0, take = 1, totalRecords = 1 }; _testRequestDataPath = string.Format("{0}{1}", PathHelper.GetTestProjectPath(), TEST_REQUEST); _testResponseDataPath = string.Format("{0}{1}", PathHelper.GetTestProjectPath(), TEST_RESPONSE); _testServiceResponseDataPath = string.Format("{0}{1}", PathHelper.GetTestProjectPath(), TEST_SERVICE_RESPONSE); _testRequestData = File.ReadAllText(_testRequestDataPath); _testResponseData = File.ReadAllText(_testResponseDataPath); _testServiceResponseData = File.ReadAllText(_testServiceResponseDataPath); List <ShowResponseModel> ShowResponseModel = JsonConvert.DeserializeObject <List <ShowResponseModel> >(_testServiceResponseData); //----------------------------------------------------------------------------------------------------------- // Arrange Mocks //----------------------------------------------------------------------------------------------------------- _mockJsonHelper = new Mock <IJsonHelper>(); _mockShowService = new Mock <IShowService>(); _mockJsonHelper.Setup(x => x.CheckForValidJson(It.IsAny <ShowsRootModel>())).Returns(true); _mockShowService.Setup(x => x.ProcessShowResponse(It.IsAny <ShowsRootModel>())).Returns(ShowResponseModel); //----------------------------------------------------------------------------------------------------------- // Inject //----------------------------------------------------------------------------------------------------------- _controller = new HomeController(_mockJsonHelper.Object, _mockShowService.Object); }
public void Index_Response_Is_Valid() { //----------------------------------------------------------------------------------------------------------- // Setup //----------------------------------------------------------------------------------------------------------- _expectedJson = JObject.Parse(_testResponseData)["response"]; _model = JsonConvert.DeserializeObject <ShowsRootModel>(_testRequestData); ////----------------------------------------------------------------------------------------------------------- //// Act ////----------------------------------------------------------------------------------------------------------- _response = _controller.Index(_model); _json = JObject.Parse(_response.Content.ReadAsStringAsync().Result)["response"]; //----------------------------------------------------------------------------------------------------------- // Json Tests //----------------------------------------------------------------------------------------------------------- JToken.DeepEquals(_expectedJson, _json); //----------------------------------------------------------------------------------------------------------- // Assert //----------------------------------------------------------------------------------------------------------- Assert.IsInstanceOfType(_response, typeof(HttpResponseMessage)); Assert.IsNotNull(_response); Assert.AreEqual(HttpStatusCode.OK, _response.StatusCode); }
public void Setup() { //----------------------------------------------------------------------------------------------------------- // Setup some test params //----------------------------------------------------------------------------------------------------------- _testRequestDataPath = string.Format("{0}{1}", PathHelper.GetTestProjectPath(), TEST_REQUEST); _testRequestData = File.ReadAllText(_testRequestDataPath); _model = JsonConvert.DeserializeObject <ShowsRootModel>(_testRequestData); //----------------------------------------------------------------------------------------------------------- // Arrange Mocks //----------------------------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------------------------- // Inject //----------------------------------------------------------------------------------------------------------- _showService = new ShowService(); }
public void Index_Model_Is_Valid() { //----------------------------------------------------------------------------------------------------------- // Setup //----------------------------------------------------------------------------------------------------------- var testRequestDataPath = string.Format("{0}{1}", PathHelper.GetTestProjectPath(), TEST_REQUEST); var testRequestData = File.ReadAllText(testRequestDataPath); _model = JsonConvert.DeserializeObject <ShowsRootModel>(testRequestData); ////----------------------------------------------------------------------------------------------------------- //// Act ////----------------------------------------------------------------------------------------------------------- _response = _controller.Index(_model); //----------------------------------------------------------------------------------------------------------- // Assert //----------------------------------------------------------------------------------------------------------- Assert.AreEqual(10, _model.take); Assert.AreEqual(75, _model.totalRecords); }
public void Index_Model_Is_Null() { //----------------------------------------------------------------------------------------------------------- // Setup //----------------------------------------------------------------------------------------------------------- _expectedResponseResult = (string)JObject.FromObject(new { error = "Could not decode request: JSON parsing failed." })["error"]; _model = null; _response = _controller.Index(_model); ////----------------------------------------------------------------------------------------------------------- //// Act ////----------------------------------------------------------------------------------------------------------- var actualResponseMessage = (string)JObject.Parse(_response.Content.ReadAsStringAsync().Result)["error"]; //----------------------------------------------------------------------------------------------------------- // Assert //----------------------------------------------------------------------------------------------------------- Assert.IsInstanceOfType(_response, typeof(HttpResponseMessage)); Assert.IsNotNull(_response); // Check we have a response Assert.AreEqual(HttpStatusCode.BadRequest, _response.StatusCode); Assert.AreEqual(actualResponseMessage, _expectedResponseResult); }
/// <summary> /// Process the show response. Only return where DRM is true and episode count is greater than 0 /// </summary> /// <returns> /// List<ShowResponseModel> /// </returns> public List <ShowResponseModel> ProcessShowResponse(ShowsRootModel model) { try { if (model.payload != null) { if (model.payload.Count > 0) { //var resp = model.payload.Where(x => x.drm && x.episodeCount > 0).ToList(); var resp = (from c in model.payload where c.episodeCount > 0 && c.drm select new ShowResponseModel() { image = c.image.showImage, slug = c.slug, title = c.title } ).ToList(); if (resp != null) { if (resp.Count > 0) { return(resp); } } } } } catch (Exception e) { //silent error for the purpose of this task. } return(null); }
public HttpResponseMessage Index([FromBody] ShowsRootModel model) { if (model == null) { return(BadResponse()); } //do some error handling first. if (model.payload == null) { return(BadResponse()); } //run a further check in case we got past the payload check if (!_jsonHelper.CheckForValidJson(model)) { return(BadResponse()); } //passed the above error handling. Now apply the logic and process the response var resp = _showService.ProcessShowResponse(model); var response = JsonResponse(JsonResult(resp), HttpStatusCode.OK); return(response); }