Example #1
0
        public void ApiGetAllMessageSuccess()
        {
            var response = (ObjectResult)ApiController.Get();

            Assert.AreEqual(dbSet.Object, response.Value);
            Assert.AreEqual(200, response.StatusCode);
        }
Example #2
0
        public async Task MessagesController_Get_Returns_OKObjectResult_When_MessageBoardService_Is_Valid()
        {
            // Arrange
            var response = new GetMessagesResponse(new List <Message>
            {
                new Message {
                    User = "******", Content = "Hello Tom.", Created = DateTime.Now.AddMinutes(5)
                },
                new Message {
                    User = "******", Content = "It works.", Created = DateTime.Now
                }
            });
            var service = new Mock <IMessageBoardService>();

            service.Setup(s => s.GetAsync()).ReturnsAsync(response);

            var logger = new Mock <ILogger <MessagesController> >();

            var controller = new MessagesController(service.Object, logger.Object);

            // Act
            var result = await controller.Get();

            // Assert
            Assert.IsType <OkObjectResult>(result);
            Assert.Equal(2, ((result as OkObjectResult).Value as GetMessagesResponse).Messages.Count);
        }
Example #3
0
        public void CanGetMessagesTest()
        {
            _routeService.Setup(x => x.GetMessagesMobile(It.IsAny <string>())).Returns(SampleMessages());

            var actionResult  = _controller.Get();
            var contentResult = actionResult as OkNegotiatedContentResult <MessageList>;

            // Assert
            Assert.IsNotNull(contentResult);
            Assert.IsNotNull(contentResult.Content);
            Assert.IsInstanceOf(typeof(MessageList), contentResult.Content, "the content is not a list of messages");

            var messages = contentResult.Content.MessageItems;

            Assert.AreEqual(SampleMessages().Count, messages.Count());
        }
Example #4
0
        public void Get()
        {
            // Nemám k dipozici VS s podporou Web Performance, pøikládám jinou ukázku testu
            var result = _MessagesController.Get();

            Assert.AreEqual(((List <MetaItMessage>)result.Value)?[0].Msg, "Test");
        }
Example #5
0
        public MessageVM GetDetailsVM(int id)
        {
            try
            {
                IActionResult HttpResult = messageCont.Get(id);

                if (HttpResult is OkObjectResult)
                {
                    var       result  = HttpResult as OkObjectResult;
                    Message   content = result.Value as Message;
                    User      user    = client.GetUser("http://localhost:51520/", "api/User/" + content.SenderUserID);
                    MessageVM vm      = new MessageVM()
                    {
                        DateSent       = content.DateSent,
                        MessageContent = content.MessageContent,
                        Title          = content.Title,
                        UserName       = user.Name
                    };

                    return(vm);
                }
                else
                {
                    return(new MessageVM());
                }
            }
            catch (Exception e)
            {
                return(new MessageVM());
            }
        }
Example #6
0
        public async Task GetByIDNotFoundTestAsync()
        {
            // Arrange
            var message1 = new Message()
            {
                MessageID   = Guid.Parse("11111111-1111-1111-1111-111111111111"),
                CreatedTime = new DateTime(2019, 06, 22),
                Text        = "Hello World"
            };
            var mock = new Mock <IMessageRepository>();

            mock.Setup(m => m.Find(message1.MessageID));
            var controller = new MessagesController(mock.Object);

            // Act
            var result = (await controller.Get(message1.MessageID)).Result;

            // Assert
            mock.Verify(m => m.Find(message1.MessageID));
            Assert.IsNotNull(result);
            Assert.AreEqual(typeof(Microsoft.AspNetCore.Mvc.NotFoundResult), result.GetType(), "should be NotFoundResult");
            var notFoundResult = result as Microsoft.AspNetCore.Mvc.NotFoundResult;

            Assert.AreEqual(404, notFoundResult.StatusCode, "status code should be 404");
        }
Example #7
0
        public async Task GetByIDTestAsync()
        {
            // Arrange
            var message1 = new Message()
            {
                MessageID   = Guid.Parse("11111111-1111-1111-1111-111111111111"),
                CreatedTime = new DateTime(2019, 06, 22),
                Text        = "Hello World"
            };
            var message2 = new Message()
            {
                MessageID   = Guid.Parse("11111111-1111-1111-1111-111111111112"),
                CreatedTime = new DateTime(2019, 06, 22),
                Text        = "Hello again"
            };
            var mockResult = message1;
            var mock       = new Mock <IMessageRepository>();

            mock.Setup(m => m.Find(message1.MessageID)).ReturnsAsync(mockResult);
            var controller = new MessagesController(mock.Object);

            // Act
            var result = (await controller.Get(message1.MessageID)).Result;

            // Assert
            Assert.IsNotNull(result);
            Assert.AreEqual(typeof(Microsoft.AspNetCore.Mvc.OkObjectResult), result.GetType(), "should be OkObjectResult");
            var okResult = result as Microsoft.AspNetCore.Mvc.OkObjectResult;

            Assert.AreEqual(typeof(Message), okResult.Value.GetType(), "should be Message");
            var message = okResult.Value as Message;

            Assert.AreEqual(mockResult, message, "message doesn't match");
        }
        public void MessageController_GetMessages_CallsMessageService_RetrieveMessages_Once()
        {
            var startDate = DateTime.Now.Date.AddDays(-5);
            var endDate   = DateTime.Now.Date;
            var results   = _sut.Get(startDate, endDate);

            _messagesService.Received(1).RetrieveMessages(startDate, endDate);
        }
        public void Get_ShouldReturnNotFound()
        {
            var controller = new MessagesController(context, logger.Object, mapper);

            var result = controller.Get(Guid.NewGuid()) as ObjectResult;

            result.Should().BeOfType <NotFoundObjectResult>();
            result.StatusCode.Should().Be(404);
        }
        public void When_Get_Using_Default_Repository()
        {
            var controller = new MessagesController(null, _messageService);

            var result = (OkObjectResult)controller.Get();

            Assert.AreEqual(result.Value, "Hello World");
            Assert.AreEqual(result.StatusCode, 200);
        }
        public void Get_WhenCalled_ReturnsAllItems()
        {
            // Act
            var result = _controller.Get().Result;

            // Assert
            var items = Assert.IsType <List <ResponseModel> >(result);

            Assert.Equal(AirTableServiceFake.Responses.Count, items.Count);
        }
        public void GetReturnAtleastOneRecord()
        {
            var result = _controller.Get();

            Assert.IsInstanceOf <OkObjectResult>(result);
            var okResult = (OkObjectResult)result;

            Assert.IsInstanceOf <List <Messages> >(okResult.Value);
            var returnValue = (List <Messages>)okResult.Value;

            Assert.NotZero(returnValue.Count);
        }
Example #13
0
        public void Get_ReturnsANotFoundResult_ForInvalidId()
        {
            var id          = Guid.Parse("22222222-2222-2222-2222-222222222222");
            var mockService = new Mock <IMessageService>();

            mockService.Setup(service => service.Get(id))
            .Returns(GetEmptyTestResponse());

            var controller = new MessagesController(mockService.Object);
            var result     = controller.Get(id);

            Assert.IsType <NotFoundResult>(result);
        }
Example #14
0
        public void Get_Exception_Returns_500()
        {
            var expected    = "error";
            var _messageSvc = new Mock <IMessageService>();

            _messageSvc.Setup(svc => svc.GetMessage()).Throws(new Exception("Error"));

            var _sut   = new MessagesController(_messageSvc.Object);
            var actual = (ObjectResult)_sut.Get();

            Assert.Equal((int)HttpStatusCode.InternalServerError, actual.StatusCode);
            Assert.Contains(expected, actual.Value?.ToString().ToLower());
        }
Example #15
0
        public void MessagesController_Get_ReturnAllElementsInRepo_WhenGivenNoParameters()
        {
            var result             = controller.Get();
            var resultObjectResult = result as ObjectResult;
            var resultObject       = resultObjectResult.Value as IEnumerable <Message>;

            Assert.IsType <ObjectResult>(result);
            Assert.Equal(3, resultObject.Count());
        }
Example #16
0
        public void Get_Returns_HelloWorld()
        {
            var expected    = "Hello World!";
            var _messageSvc = new Mock <IMessageService>();

            _messageSvc.Setup(svc => svc.GetMessage())
            .Returns(expected);

            var _sut = new MessagesController(_messageSvc.Object);

            var actual = (ObjectResult)_sut.Get();

            Assert.Equal((int)HttpStatusCode.OK, actual.StatusCode);
            Assert.Equal(expected, actual.Value);
        }
Example #17
0
        public void GetReturnsOk()
        {
            Message message = new Message()
            {
                Text = "Firsttime"
            };
            MessagesController messagesController = new MessagesController();
            IHttpActionResult  postActionResult   = messagesController.Post(message);
            var postCreatedResult             = postActionResult as CreatedAtRouteNegotiatedContentResult <Message>;
            IHttpActionResult getActionResult = messagesController.Get(postCreatedResult.Content.Id);
            var getCreatedResult = getActionResult as OkNegotiatedContentResult <Message>;

            Assert.IsNotNull(getCreatedResult);
            Assert.AreEqual(message.Text, getCreatedResult.Content.Text);
        }
Example #18
0
        public void GetAllNotNull()
        {
            MessagesController messagesController = new MessagesController();
            Message            message            = new Message()
            {
                Text = "Firsttime"
            };

            for (int i = 0; i < 100; i++)
            {
                messagesController.Post(message);
            }
            IEnumerable <Message> actionResult = messagesController.Get();

            Assert.IsNotNull(actionResult);
        }
        public void Get_ShouldReturnSuccess()
        {
            var controller  = new MessagesController(context, logger.Object, mapper);
            var testMessage = TestData.CreateMessage("2018MESSAGE-1", "test description", 0);

            context.Message.Add(testMessage);
            context.SaveChanges();

            var result = controller.Get(testMessage.Id) as ObjectResult;

            result.Should().BeOfType <OkObjectResult>();
            result.StatusCode.Should().Be(200);
            var model = result.Value as MessageViewModel;

            model.Title.Should().Be("2018MESSAGE-1");
        }
        public async Task GetMessageById_MessageIdDoesNotExist_ReturnNotFound()
        {
            // Arrange
            int messageId          = -999;
            var mockMediator       = new Mock <IMediator>();
            var controller         = new MessagesController(mockMediator.Object, null, null, null, null);
            int expectedStatusCode = (int)HttpStatusCode.NotFound;

            // Act
            var response = await controller.Get(messageId);

            //Assert
            var result = Assert.IsType <NotFoundObjectResult>(response);

            Assert.Equal(expectedStatusCode, result.StatusCode);
        }
Example #21
0
        public void Get_ReturnsAnOkObjectResult_ForValidId()
        {
            var id          = Guid.Parse("11111111-1111-1111-1111-111111111111");
            var mockService = new Mock <IMessageService>();

            mockService.Setup(service => service.Get(id))
            .Returns(GetTestResponse());

            var controller = new MessagesController(mockService.Object);
            var result     = controller.Get(id);

            var okResult = Assert.IsType <OkObjectResult>(result);
            var message  = Assert.IsType <MessageResponse>(okResult.Value);

            Assert.Equal(id, message.Id);
            Assert.Equal("Hello World!", message.Content);
        }
Example #22
0
        public async Task MessagesController_Get_Returns_InternalServerError_When_MessageBoardService_Throws_Exception()
        {
            // Arrange
            var service = new Mock <IMessageBoardService>();

            service.Setup(s => s.GetAsync()).ThrowsAsync(new Exception());

            var logger = new Mock <ILogger <MessagesController> >();

            var controller = new MessagesController(service.Object, logger.Object);

            // Act
            var result = await controller.Get();

            // Assert
            Assert.IsType <StatusCodeResult>(result);
            Assert.Equal(500, (result as StatusCodeResult).StatusCode);
        }
        public void Get_ShouldReturnFail()
        {
            var options = new DbContextOptionsBuilder <HubDbContext>()
                          .UseInMemoryDatabase(Guid.NewGuid().ToString())
                          .Options;
            var mockContext = new Mock <HubDbContext>(options);

            mockContext.Setup(m => m.Message).Throws(new Exception());
            var controller  = new MessagesController(mockContext.Object, logger.Object, mapper);
            var testMessage = TestData.CreateMessage("1", "test description", 0);

            context.Message.Add(testMessage);
            context.SaveChanges();

            var result = controller.Get(testMessage.Id) as ObjectResult;

            result.Should().BeOfType <BadRequestObjectResult>();
            result.StatusCode.Should().Be(400);
        }
Example #24
0
        public void ShouldReturnMessageTypeFromGet()
        {
            // Arrange
            var options = new DbContextOptionsBuilder <ApiContext>()
                          .UseInMemoryDatabase(databaseName: "ShouldReturnMessageTypeFromGet")
                          .Options;

            var context    = new ApiContext(options);
            var controller = new MessagesController(context);

            Seed(context);

            //Act
            var result = controller.Get(); //calls Get() to get username


            //Assert
            var model = Assert.IsAssignableFrom <IEnumerable <Mercury.Models.Messages> >(result);
        }
        public void MessagesController_DecoratesListResponseWithPaginationHeaders()
        {
            for (int i = 0; i < 20; i++)
            {
                _state.Add(new MessageModel(Guid.NewGuid(), "Niklas", $"Message{i}", _clock.GetCurrentInstant()));
            }

            var result = _controller
                         .Get(limit: 5, offset: 5, sort: "id");

            Assert.AreEqual(StatusCodes.Status200OK, result.StatusCode);
            Assert.IsInstanceOfType(result.Value, typeof(Pagination <MessageModel>));

            var messages = (Pagination <MessageModel>)result.Value;

            Assert.AreEqual(5, messages.Items.Count());

            string linkHeader = _controller.Response.Headers[MessageBoardConstants.LinkHeader];

            Assert.AreEqual($"<5,0,id>; rel=\"first\",<5,15,id>; rel=\"last\",<5,10,id>; rel=\"next\",<5,0,id>; rel=\"prev\"", linkHeader);
        }
Example #26
0
        public void GetMessagesOfUserTest()
        {
            // Arrange


            var controller = new MessagesController(_context);


            var convmessages = new MapperConfiguration(cfg =>
            {
                cfg.CreateMap <DAL.Entities.Message, MessageInBytes>();
            }).CreateMapper().Map <List <MessageInBytes> >(users[0].Messages);
            // Act
            var result = controller.Get(1).ToList();

            // Assert

            var model = Assert.IsType <List <MessageInBytes> >(result);

            Assert.True(Stuff.Compare(model, convmessages));
        }
        public async Task GetMessage_Happy()
        {
            var messageService = new Mock <IMessageService>();
            var controller     = new MessagesController(messageService.Object);
            var userName       = "******";
            var user           = new ClaimsPrincipal(new GenericIdentity(userName));

            controller.ControllerContext = new ControllerContext()
            {
                HttpContext = new DefaultHttpContext()
                {
                    User = user
                }
            };

            messageService.Setup(x => x.GetMessage(userName, It.IsAny <Guid>())).ReturnsAsync(new MessageDTO());

            OkObjectResult result = await controller.Get(Guid.NewGuid()) as OkObjectResult;

            Assert.NotNull(result);
            Assert.NotNull(result.Value);
        }
        public void GetReturnsTest()
        {
            var expected = new List<Message>
            {
                new Message { Content = "Message 1" },
                new Message { Content = "Message 2" },
            };

            mockMessageHandler.Setup(handler => handler.GetMessages()).Returns(expected).Verifiable();

            var controller = new MessagesController(mockLogger.Object, mockMessageHandler.Object);
            var actual = controller.Get().Result as OkObjectResult;

            Assert.IsType<OkObjectResult>(actual);
            Assert.Equal(expected.Count, (actual.Value as IEnumerable<Message>).Count());
            for (var index = 0; index < expected.Count; index++)
            {
                Assert.Equal(expected[index].Content, (actual.Value as IEnumerable<Message>).ElementAt(index).Content);
            }

            mockMessageHandler.VerifyAll();
        }
Example #29
0
        public async Task GetTestAsync()
        {
            // Arrange
            var message1 = new Message()
            {
                MessageID   = Guid.Parse("11111111-1111-1111-1111-111111111111"),
                CreatedTime = new DateTime(2019, 06, 22),
                Text        = "Hello World"
            };
            var message2 = new Message()
            {
                MessageID   = Guid.Parse("11111111-1111-1111-1111-111111111112"),
                CreatedTime = new DateTime(2019, 06, 22),
                Text        = "Hello again"
            };
            var mockResults = new List <Message>()
            {
                message1,
                message1
            };
            var mock = new Mock <IMessageRepository>();

            mock.Setup(m => m.FindAll()).ReturnsAsync(mockResults);
            var controller = new MessagesController(mock.Object);

            // Act
            var result = (await controller.Get()).Result;

            // Assert
            Assert.IsNotNull(result);
            Assert.AreEqual(typeof(Microsoft.AspNetCore.Mvc.OkObjectResult), result.GetType(), "should be OkObjectResult");
            var okResult = result as Microsoft.AspNetCore.Mvc.OkObjectResult;

            Assert.AreEqual(typeof(List <Message>), okResult.Value.GetType(), "should be List<Message");
            var messages = okResult.Value as IEnumerable <Message>;

            Assert.AreEqual(mockResults, messages, "messages don't match");
        }
Example #30
0
 public void GetTest()
 {
     Assert.AreEqual(22, cmd.Get().Count());
 }