Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

John Hancock - RestaurantReviews implementation #48

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
*/.vs/*
.vs/*
*.user
*/obj/*
*/bin/*
65 changes: 65 additions & 0 deletions RestaurantReviews.API.Tests/RestaurantControllerTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
using Moq;
using RestaurantReviews.API.Controllers;
using RestaurantReviews.Interfaces.Business;
using RestaurantReviews.Interfaces.Models;
using RestaurantReviews.Models;
using System.Collections.Generic;
using Xunit;

namespace RestaurantReviews.API.Tests
{
public class RestaurantControllerTests
{
[Fact]
public void Get_ShouldReturnResultsOfManagerGetAll()
{
// Setup
(var mockManager, var controller) = SetupMocksAndController();
var expected = new List<IRestaurant>();
mockManager.Setup(y => y.GetAll()).Returns(expected);

// Execute
var actionResult = controller.Get();

// Assert
var actual = TestHelper.GetOkResult(actionResult);
Assert.Equal(expected, actual);
}

[Fact]
public void GetById_ShouldReturnResultOfManagerGetById()
{
// Setup
(var mockManager, var controller) = SetupMocksAndController();
var expected = new Restaurant();
mockManager.Setup(y => y.GetById(It.IsAny<long>())).Returns(expected);

// Execute
var actionResult = controller.Get(0);

// Assert
var actual = TestHelper.GetOkResult(actionResult);
Assert.Equal(expected, actual);
}

[Fact]
public void Post_ShouldCallManagerCreateWithCorrectModel()
{
// Setup
(var mockManager, var controller) = SetupMocksAndController();
var newModel = new Restaurant();

// Execute
controller.Post(newModel);

// Assert
mockManager.Verify(c => c.Create(newModel), Times.Once());
}

(Mock<IRestaurantManager>, RestaurantController) SetupMocksAndController()
{
var mockManager = new Mock<IRestaurantManager>();
return (mockManager, new RestaurantController(mockManager.Object));
}
}
}
22 changes: 22 additions & 0 deletions RestaurantReviews.API.Tests/RestaurantReviews.API.Tests.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp2.2</TargetFramework>

<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Mvc.Core" Version="2.2.5" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.0.1" />
<PackageReference Include="Moq" Version="4.12.0" />
<PackageReference Include="xunit" Version="2.4.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\RestaurantReviews.API\RestaurantReviews.API.csproj" />
<ProjectReference Include="..\RestaurantReviews.Interfaces\RestaurantReviews.Interfaces.csproj" />
</ItemGroup>

</Project>
79 changes: 79 additions & 0 deletions RestaurantReviews.API.Tests/ReviewModelValidatorTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
using Moq;
using RestaurantReviews.Business.Validators;
using RestaurantReviews.Interfaces.Models;
using RestaurantReviews.Interfaces.Repositories;
using RestaurantReviews.Models;
using System;
using System.Linq;
using Xunit;

namespace RestaurantReviews.API.Tests
{
public class ReviewModelValidatorTests
{
[Fact]
public void Get_ShouldPassValidationOnValidInput()
{
// Setup
(var validator, var mockRestaurantRepository, var mockUserRepository) = SetupMocksAndValidator();
mockRestaurantRepository.Setup(p => p.GetById(It.IsAny<long>())).Returns(new Restaurant());
mockUserRepository.Setup(p => p.GetById(It.IsAny<long>())).Returns(new User());

// Execute
var errors = validator.Validate(new Review { Content = Guid.NewGuid().ToString() });

// Assert
var actualHasErrors = errors?.Any() == true;
Assert.False(actualHasErrors);
}

[Fact]
public void Get_ShouldFailIfContentIsNull()
{
// Setup
(var validator, var mockRestaurantRepository, var mockUserRepository) = SetupMocksAndValidator();

// Execute
var errors = validator.Validate(new Review());

// Assert
Assert.True(errors?.Any(p => p.Contains("Content is required"))); // <- this is not ideal, but didn't want to implement a more complex error object
}

[Fact]
public void Get_ShouldFailIfRestaurantIdIsInvalid()
{
// Setup
(var validator, var mockRestaurantRepository, var mockUserRepository) = SetupMocksAndValidator();
mockRestaurantRepository.Setup(p => p.GetById(It.IsAny<long>())).Returns((IRestaurant)null);

// Execute
var errors = validator.Validate(new Review());

// Assert
Assert.True(errors?.Any(p => p.Contains("RestaurantId must be associated with an existing restaurant"))); // <- this is not ideal, but didn't want to implement a more complex error object
}

[Fact]
public void Get_ShouldFailIfUserIdIsInvalid()
{
// Setup
(var validator, var mockRestaurantRepository, var mockUserRepository) = SetupMocksAndValidator();
mockUserRepository.Setup(p => p.GetById(It.IsAny<long>())).Returns((IUser)null);

// Execute
var errors = validator.Validate(new Review());

// Assert
Assert.True(errors?.Any(p => p.Contains("UserId must be associated with an existing user"))); // <- this is not ideal, but didn't want to implement a more complex error object
}

(ReviewModelValidator validator, Mock<IRestaurantRepository> mockRestaurantRepository, Mock<IUserRepository> mockUserRepository) SetupMocksAndValidator()
{
var mockRestaurantRepository = new Mock<IRestaurantRepository>();
var mockUserRepository = new Mock<IUserRepository>();
var validator = new ReviewModelValidator(mockRestaurantRepository.Object, mockUserRepository.Object);
return (validator, mockRestaurantRepository, mockUserRepository);
}
}
}
16 changes: 16 additions & 0 deletions RestaurantReviews.API.Tests/TestHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using Microsoft.AspNetCore.Mvc;
using Xunit;

namespace RestaurantReviews.API.Tests
{
public static class TestHelper
{
public static object GetOkResult<T>(ActionResult<T> actionResult)
{
Assert.NotNull(actionResult);
Assert.NotNull(actionResult.Result);
var okObjectResult = Assert.IsType<OkObjectResult>(actionResult.Result);
return okObjectResult.Value;
}
}
}
55 changes: 55 additions & 0 deletions RestaurantReviews.API.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.29201.188
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "RestaurantReviews.API", "RestaurantReviews.API\RestaurantReviews.API.csproj", "{602795A7-B266-4EBB-A969-BDF2E4B6B330}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "RestaurantReviews.Interfaces", "RestaurantReviews.Interfaces\RestaurantReviews.Interfaces.csproj", "{40360CB7-5DD8-43AE-8E82-0496E72B9E61}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "RestaurantReviews.JsonData", "RestaurantReviews.JsonData\RestaurantReviews.JsonData.csproj", "{6B6C94AC-65FC-4337-B3BD-3A49B28FFB78}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "RestaurantReviews.API.Tests", "RestaurantReviews.API.Tests\RestaurantReviews.API.Tests.csproj", "{79018B48-3CFC-4621-BDD9-0CCD9326FC77}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "RestaurantReviews.Models", "RestaurantReviews.Models\RestaurantReviews.Models.csproj", "{F03EB321-4875-4271-90B8-DB80A1ADD0A4}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "RestaurantReviews.Business", "RestaurantReviews.Business\RestaurantReviews.Business.csproj", "{7EA51CB2-275F-4671-8A36-AEF4A13F9FED}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{602795A7-B266-4EBB-A969-BDF2E4B6B330}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{602795A7-B266-4EBB-A969-BDF2E4B6B330}.Debug|Any CPU.Build.0 = Debug|Any CPU
{602795A7-B266-4EBB-A969-BDF2E4B6B330}.Release|Any CPU.ActiveCfg = Release|Any CPU
{602795A7-B266-4EBB-A969-BDF2E4B6B330}.Release|Any CPU.Build.0 = Release|Any CPU
{40360CB7-5DD8-43AE-8E82-0496E72B9E61}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{40360CB7-5DD8-43AE-8E82-0496E72B9E61}.Debug|Any CPU.Build.0 = Debug|Any CPU
{40360CB7-5DD8-43AE-8E82-0496E72B9E61}.Release|Any CPU.ActiveCfg = Release|Any CPU
{40360CB7-5DD8-43AE-8E82-0496E72B9E61}.Release|Any CPU.Build.0 = Release|Any CPU
{6B6C94AC-65FC-4337-B3BD-3A49B28FFB78}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{6B6C94AC-65FC-4337-B3BD-3A49B28FFB78}.Debug|Any CPU.Build.0 = Debug|Any CPU
{6B6C94AC-65FC-4337-B3BD-3A49B28FFB78}.Release|Any CPU.ActiveCfg = Release|Any CPU
{6B6C94AC-65FC-4337-B3BD-3A49B28FFB78}.Release|Any CPU.Build.0 = Release|Any CPU
{79018B48-3CFC-4621-BDD9-0CCD9326FC77}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{79018B48-3CFC-4621-BDD9-0CCD9326FC77}.Debug|Any CPU.Build.0 = Debug|Any CPU
{79018B48-3CFC-4621-BDD9-0CCD9326FC77}.Release|Any CPU.ActiveCfg = Release|Any CPU
{79018B48-3CFC-4621-BDD9-0CCD9326FC77}.Release|Any CPU.Build.0 = Release|Any CPU
{F03EB321-4875-4271-90B8-DB80A1ADD0A4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{F03EB321-4875-4271-90B8-DB80A1ADD0A4}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F03EB321-4875-4271-90B8-DB80A1ADD0A4}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F03EB321-4875-4271-90B8-DB80A1ADD0A4}.Release|Any CPU.Build.0 = Release|Any CPU
{7EA51CB2-275F-4671-8A36-AEF4A13F9FED}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{7EA51CB2-275F-4671-8A36-AEF4A13F9FED}.Debug|Any CPU.Build.0 = Debug|Any CPU
{7EA51CB2-275F-4671-8A36-AEF4A13F9FED}.Release|Any CPU.ActiveCfg = Release|Any CPU
{7EA51CB2-275F-4671-8A36-AEF4A13F9FED}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {CA8F1049-F118-4248-8449-DB1360411DE6}
EndGlobalSection
EndGlobal
40 changes: 40 additions & 0 deletions RestaurantReviews.API/Controllers/RestaurantController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using Microsoft.AspNetCore.Mvc;
using RestaurantReviews.Interfaces.Business;
using RestaurantReviews.Models;
using System.Collections.Generic;

namespace RestaurantReviews.API.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class RestaurantController : ControllerBase
{
private readonly IRestaurantManager _restaurantManager;

public RestaurantController(IRestaurantManager restaurantManager)
{
_restaurantManager = restaurantManager;
}

// GET api/restaurants
[HttpGet]
public ActionResult<IEnumerable<Restaurant>> Get()
{
return Ok(_restaurantManager.GetAll());
}

// GET api/restaurants/5
[HttpGet("{id}")]
public ActionResult<Restaurant> Get(int id)
{
return Ok(_restaurantManager.GetById(id));
}

// POST api/restaurants
[HttpPost]
public void Post([FromBody] Restaurant restaurant)
{
_restaurantManager.Create(restaurant);
}
}
}
54 changes: 54 additions & 0 deletions RestaurantReviews.API/Controllers/ReviewController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
using Microsoft.AspNetCore.Mvc;
using RestaurantReviews.Interfaces.Business;
using RestaurantReviews.Models;
using System.Collections.Generic;

namespace RestaurantReviews.API.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class ReviewController : ControllerBase
{
private readonly IReviewManager _reviewManager;

public ReviewController(IReviewManager reviewManager)
{
_reviewManager = reviewManager;
}

// GET api/review
[HttpGet]
public ActionResult<IEnumerable<Review>> Get()
{
return Ok(_reviewManager.GetAll());
}

// GET api/review/5
[HttpGet("{id}")]
public ActionResult<Review> Get(int id)
{
return Ok(_reviewManager.GetById(id));
}

// GET api/review/user/5
[HttpGet("user/{userId}")]
public ActionResult<Review> GetByUserId(int userId)
{
return Ok(_reviewManager.GetByUserId(userId));
}

// POST api/review
[HttpPost]
public void Post([FromBody] Review review)
{
_reviewManager.Create(review);
}

// DELETE api/review/5
[HttpDelete("{id}")]
public void Delete(int id)
{
_reviewManager.Delete(id);
}
}
}
40 changes: 40 additions & 0 deletions RestaurantReviews.API/Controllers/UserController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using Microsoft.AspNetCore.Mvc;
using RestaurantReviews.Interfaces.Repositories;
using RestaurantReviews.Models;
using System.Collections.Generic;

namespace UserReviews.API.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class UserController : ControllerBase
{
private readonly IUserRepository _repository;

public UserController(IUserRepository userRepository)
{
_repository = userRepository;
}

// GET api/users
[HttpGet]
public ActionResult<IEnumerable<User>> Get()
{
return Ok(_repository.GetAll());
}

// GET api/users/5
[HttpGet("{id}")]
public ActionResult<User> Get(int id)
{
return Ok(_repository.GetById(id));
}

// POST api/users
[HttpPost]
public void Post([FromBody] User user)
{
_repository.Create(user);
}
}
}
Loading