public void TestAddCorrectWidgetParameters() { // Arrange const string userNameConst = "tretyakov"; const string widgetIdConst = "111"; const string widgetNameConst = "my widget"; const string widgetContentConst = "<xml /><content></content>"; const string widgetItemIdConst = "5"; var requestMock = new Mock<HttpRequestMessage>(); requestMock.Object.Method = new HttpMethod("POST"); requestMock.Object.Properties.Add("MS_HttpConfiguration", GetHttpConfiguration()); string userName = string.Empty, widgetId = string.Empty, widgetName = string.Empty, contentUri = string.Empty; IPrincipal principal = Thread.CurrentPrincipal; Thread.CurrentPrincipal = new GenericPrincipal(new GenericIdentity(userNameConst), new string[] {}); var mock = new Mock<IWidgetItemRepository>(); mock.Setup(m => m.Add(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>())) .Returns(widgetItemIdConst) .Callback<string, string, string, string>( (userNameValue, widgetIdValue, widgetNameValue, contentUriValue) => { userName = userNameValue; widgetId = widgetIdValue; widgetName = widgetNameValue; contentUri = contentUriValue; } ); var widgetsController = new WidgetsController(mock.Object) {Request = requestMock.Object}; string response = string.Empty; Exception exception = null; // Act try { response = widgetsController.Post(widgetIdConst, widgetNameConst, widgetContentConst); } catch (Exception e) { exception = e; } // Assert mock.Verify(m => m.Add(userNameConst, widgetIdConst, widgetNameConst, It.IsAny<string>()), Times.Once()); Assert.IsNull(exception); Assert.IsTrue(response == widgetItemIdConst); Assert.IsTrue(userName == userNameConst); Assert.IsTrue(widgetId == widgetIdConst); Assert.IsTrue(widgetName == widgetNameConst); Assert.IsFalse(string.IsNullOrEmpty(contentUri)); Assert.IsTrue(File.Exists(contentUri)); Thread.CurrentPrincipal = principal; }
public void GetWidgetTest() { var wc = new WidgetsController(); var result = wc.GetWidget("bed09fd4-a977-47b7-88aa-27d32881e50d"); Assert.IsNotNull(result, "No widget found"); }
public void GetAllWidgetsTest() { var wc = new WidgetsController(); var result = wc.GetAllWidgets(); Assert.IsNotNull(result, "No widgets found"); }
public async void PostWidgetWillReturnBadRequestForEmptyWidget() { var myController = new WidgetsController(_mockedRepository.Object); var actionResult = await myController.PostWidget(null); Assert.NotNull(actionResult); Assert.IsType <BadRequestResult>(actionResult.Result); }
public async void PutWidgetWillReturnBadRequestWhenIdsDoNotMatch() { var myController = new WidgetsController(_mockedRepository.Object); var actionResult = await myController.PutWidget(_widgets.First().Id + 1, _widgets.First()); Assert.NotNull(actionResult); Assert.IsType <BadRequestResult>(actionResult); }
public async void GetWidgetByIdWillReturnNotFoundWhenWidgetIsNull() { _mockedRepository.Setup(x => x.GetByIdAsync(It.IsAny <int>())).ReturnsAsync(() => null); var myController = new WidgetsController(_mockedRepository.Object); var actionResult = await myController.GetWidget(It.IsAny <int>()); Assert.NotNull(actionResult); Assert.IsType <NotFoundResult>(actionResult.Result); }
public async void PutWidgetWillReturnNotFoundWhenThereAreNoChanges() { _mockedRepository.Setup(x => x.PutAsync(It.IsAny <int>(), It.IsAny <Widget>())).ReturnsAsync(0); var myController = new WidgetsController(_mockedRepository.Object); var actionResult = await myController.PutWidget(_widgets.First().Id, _widgets.First()); Assert.NotNull(actionResult); Assert.IsType <NotFoundResult>(actionResult); }
public async void DeleteWidgetWithCorrectIdWillReturnOk() { _mockedRepository.Setup(x => x.DeleteByIdAsync(It.IsAny <int>())).ReturnsAsync(1); var myController = new WidgetsController(_mockedRepository.Object); var actionResult = await myController.DeleteWidget(It.IsAny <int>()); Assert.NotNull(actionResult); Assert.IsType <OkObjectResult>(actionResult.Result); }
public async void PutWidgetWillReturnOkResultWhenEverythingIsOk() { _mockedRepository.Setup(x => x.PutAsync(It.IsAny <int>(), It.IsAny <Widget>())).ReturnsAsync(1); var myController = new WidgetsController(_mockedRepository.Object); var actionResult = await myController.PutWidget(_widgets.First().Id, _widgets.First()); Assert.NotNull(actionResult); Assert.IsType <OkObjectResult>(actionResult); }
public async void PostWidgetWithWrongDataWillReturnNotFound() { _mockedRepository.Setup(x => x.AddAsync(It.IsAny <Widget>())).ReturnsAsync(0); var myController = new WidgetsController(_mockedRepository.Object); var actionResult = await myController.PostWidget(_widgets.First()); Assert.NotNull(actionResult); Assert.IsType <NotFoundResult>(actionResult.Result); }
public void Constructor_WhenAllParametersAreValid_ShouldReturnInstance() { // Arrange var widgetService = this.widgetServiceMock.Object; // Act var result = new WidgetsController(widgetService); // Assert result.Should().NotBeNull().And.BeOfType <WidgetsController>(); }
public async Task EquipmentVisualization_notFound() { var repository = new WidgetRepository(_options, new HttpClient()); var controller = new WidgetsController(repository, _mapper, _logger); var result = await controller.EquipmentVisualization(-1); result.Should().NotBeNull(); var okResult = result.Should().BeOfType <NotFoundResult>().Subject; Assert.True(okResult.StatusCode.Equals(404)); }
public void TestAddInvalidWidgetContent() { // Arrange const string userNameConst = "tretyakov"; const string widgetIdConst = "111"; const string widgetNameConst = "my widget"; const string widgetContentConst = ""; var requestMock = new Mock <HttpRequestMessage>(); requestMock.Object.Method = new HttpMethod("POST"); requestMock.Object.Properties.Add("MS_HttpConfiguration", GetHttpConfiguration()); IPrincipal principal = Thread.CurrentPrincipal; Thread.CurrentPrincipal = new GenericPrincipal(new GenericIdentity(userNameConst), new string[] {}); var mock = new Mock <IWidgetItemRepository>(); mock.Setup(m => m.Add(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <string>(), It.IsAny <string>())) .Returns("5"); var widgetsController = new WidgetsController(mock.Object) { Request = requestMock.Object }; string response = string.Empty; Exception exception = null; HttpResponseException responseException = null; // Act try { response = widgetsController.Post(widgetIdConst, widgetNameConst, widgetContentConst); } catch (HttpResponseException e) { responseException = e; } catch (Exception e) { exception = e; } // Assert mock.Verify(m => m.Add(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <string>(), It.IsAny <string>()), Times.Never()); Assert.IsNull(exception); Assert.IsTrue(string.IsNullOrEmpty(response)); Assert.IsNotNull(responseException); Assert.IsTrue(responseException.Response.StatusCode == HttpStatusCode.BadRequest); Thread.CurrentPrincipal = principal; }
public async void GetWidgetByIdWillReturnOkResult() { _mockedRepository.Setup(x => x.GetByIdAsync(It.IsAny <int>())).ReturnsAsync(_widgets.First); var myController = new WidgetsController(_mockedRepository.Object); var actionResult = await myController.GetWidget(It.IsAny <int>()); OkObjectResult result = actionResult.Result as OkObjectResult; Assert.NotNull(result); Assert.IsType <OkObjectResult>(result); Assert.Equal(_widgets.First(), result.Value); }
public async Task EquipmentVisualization_okResult() { var repository = new WidgetRepository(_options, new HttpClient()); var controller = new WidgetsController(repository, _mapper, _logger); var result = await controller.EquipmentVisualization(12); result.Should().NotBeNull(); var okResult = result.Should().BeOfType <OkObjectResult>().Subject; Assert.True(okResult.StatusCode.Equals(200)); var model = okResult.Value.Should().BeAssignableTo <WidgetResponse>().Subject; Assert.False(model.DefaultCss.Equals(null)); Assert.True(model.Code.Length > 0); }
public void LoanPortfolioPerformance_WhenCalled_ReturnsOkWithData() { _loanRepositoryMock.Setup(lr => lr.LoanRequestAmountPerYearInquire()) .Returns(new Dictionary <string, decimal> { { "2016", 1 }, { "2017", 2 }, { "2018", 3 } }); var controller = new WidgetsController(_loanRepositoryMock.Object); var result = controller.LoanPortfolioPerformance() as OkObjectResult; result.StatusCode.ShouldBe(200); var content = result.Value as Dictionary <string, decimal>; content.ContainsKey("2016").ShouldBe(true); content.ContainsValue(3).ShouldBe(true); }
public void TestAddInvalidWidgetName() { // Arrange const string userNameConst = "tretyakov"; const string widgetIdConst = "111"; const string widgetNameConst = ""; const string widgetContentConst = "<xml /><content></content>"; var requestMock = new Mock<HttpRequestMessage>(); requestMock.Object.Method = new HttpMethod("POST"); requestMock.Object.Properties.Add("MS_HttpConfiguration", GetHttpConfiguration()); IPrincipal principal = Thread.CurrentPrincipal; Thread.CurrentPrincipal = new GenericPrincipal(new GenericIdentity(userNameConst), new string[] {}); var mock = new Mock<IWidgetItemRepository>(); mock.Setup(m => m.Add(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>())) .Returns("5"); var widgetsController = new WidgetsController(mock.Object) {Request = requestMock.Object}; string response = string.Empty; Exception exception = null; HttpResponseException responseException = null; // Act try { response = widgetsController.Post(widgetIdConst, widgetNameConst, widgetContentConst); } catch (HttpResponseException e) { responseException = e; } catch (Exception e) { exception = e; } // Assert mock.Verify(m => m.Add(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>()), Times.Never()); Assert.IsNull(exception); Assert.IsTrue(string.IsNullOrEmpty(response)); Assert.IsNotNull(responseException); Assert.IsTrue(responseException.Response.StatusCode == HttpStatusCode.BadRequest); Thread.CurrentPrincipal = principal; }
protected WidgetsControllerUnitTest() { _dbSet = new DbSetMock <Widget>(new List <Widget>()); _controller = new WidgetsController(MockWallboardContext()); }
public WidgetsControllerTest() { _mockedRepository = new Mock <IWidgetRepository>(); _widgetsController = new WidgetsController(_mockedRepository.Object); _widgets = WidgetBuilder.BuildWithId(); }
public void TestAddCorrectWidgetParameters() { // Arrange const string userNameConst = "tretyakov"; const string widgetIdConst = "111"; const string widgetNameConst = "my widget"; const string widgetContentConst = "<xml /><content></content>"; const string widgetItemIdConst = "5"; var requestMock = new Mock <HttpRequestMessage>(); requestMock.Object.Method = new HttpMethod("POST"); requestMock.Object.Properties.Add("MS_HttpConfiguration", GetHttpConfiguration()); string userName = string.Empty, widgetId = string.Empty, widgetName = string.Empty, contentUri = string.Empty; IPrincipal principal = Thread.CurrentPrincipal; Thread.CurrentPrincipal = new GenericPrincipal(new GenericIdentity(userNameConst), new string[] {}); var mock = new Mock <IWidgetItemRepository>(); mock.Setup(m => m.Add(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <string>(), It.IsAny <string>())) .Returns(widgetItemIdConst) .Callback <string, string, string, string>( (userNameValue, widgetIdValue, widgetNameValue, contentUriValue) => { userName = userNameValue; widgetId = widgetIdValue; widgetName = widgetNameValue; contentUri = contentUriValue; } ); var widgetsController = new WidgetsController(mock.Object) { Request = requestMock.Object }; string response = string.Empty; Exception exception = null; // Act try { response = widgetsController.Post(widgetIdConst, widgetNameConst, widgetContentConst); } catch (Exception e) { exception = e; } // Assert mock.Verify(m => m.Add(userNameConst, widgetIdConst, widgetNameConst, It.IsAny <string>()), Times.Once()); Assert.IsNull(exception); Assert.IsTrue(response == widgetItemIdConst); Assert.IsTrue(userName == userNameConst); Assert.IsTrue(widgetId == widgetIdConst); Assert.IsTrue(widgetName == widgetNameConst); Assert.IsFalse(string.IsNullOrEmpty(contentUri)); Assert.IsTrue(File.Exists(contentUri)); Thread.CurrentPrincipal = principal; }
public void Should_Return_Collection() { var dataContext = new Mock <IAtomicCmsDataRepository>(); var logger = new Mock <LoggingService>(); ICollection <PageTag> pagesTagsCollection = new List <PageTag>(); CmsPage page1 = new CmsPage() { Id = 1 }; CmsPage page2 = new CmsPage() { Id = 2 }; CmsPage page3 = new CmsPage() { Id = 3 }; CmsTag tag1 = new CmsTag() { Id = 1 }; CmsTag tag2 = new CmsTag() { Id = 2 }; CmsTag tag3 = new CmsTag() { Id = 3 }; pagesTagsCollection.Add(new PageTag() { Page = page1, Tag = tag1 }); pagesTagsCollection.Add(new PageTag() { Page = page1, Tag = tag2 }); pagesTagsCollection.Add(new PageTag() { Page = page2, Tag = tag1 }); pagesTagsCollection.Add(new PageTag() { Page = page2, Tag = tag3 }); pagesTagsCollection.Add(new PageTag() { Page = page3, Tag = tag1 }); pagesTagsCollection.Add(new PageTag() { Page = page3, Tag = tag2 }); dataContext.Setup(x => x.GetPagesTags()).Returns(pagesTagsCollection); WidgetService widgetService = new WidgetService(dataContext.Object, logger.Object); var c = new WidgetsController(widgetService); ActionResult ar = c.TagCloud(); var view = ar as PartialViewResult; Assert.IsNotNull(view); Assert.IsNotEmpty((ICollection)view.ViewData.Model); ICollection <TagCount> tagCounts = view.ViewData.Model as ICollection <TagCount>; Assert.IsNotNull(tagCounts); Assert.AreEqual(3, tagCounts.Count); Assert.AreEqual(3, new List <TagCount>(tagCounts)[0].Count); Assert.AreEqual(2, new List <TagCount>(tagCounts)[1].Count); Assert.AreEqual(1, new List <TagCount>(tagCounts)[2].Count); }