Skip to content
Merged
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
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AutoMapper;
using AutoMapper;
using WizardWorld.Persistance.Models.Houses;

namespace WizardWorld.Application.Requests.Houses {
class HouseMappingProfile : Profile {
public class HouseMappingProfile : Profile {
public HouseMappingProfile() {
CreateMap<House, HouseDto>();
CreateMap<Trait, TraitDto>();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Collections.Generic;
using MediatR.AspNet;
using WizardWorld.Persistance.Models.Houses;

namespace WizardWorld.Application.Requests.Houses.Queries.GetHouses {
public class GetHousesQuery : IQuery<List<HouseDto>> { }
Expand Down
1 change: 1 addition & 0 deletions WizardWorldApi/WizardWorldApi.Tests.Integrations/Seeder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ public static void Seed(ApplicationDbContext context) {
context.AddRange(IngredientsGenerator.Ingredients);
context.AddRange(ElixirsGenerator.Elixirs);
context.AddRange(WizardsGenerator.Wizards);
context.AddRange(HousesGenerator.Houses);
context.SaveChanges();
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using FluentAssertions;
using NUnit.Framework;
using WizardWorld.Application.Requests.Houses;
using WizardWorldApi.Tests.Integrations.Extensions;
using WizardWorldApi.Tests.Shared;

namespace WizardWorldApi.Tests.Integrations.Tests {
class HouseControllerTests {
private HttpClient _client;

[OneTimeSetUp]
public void Setup() {
var factory = new WizardWorldWebApplicationFactory();
_client = factory.CreateClient();
}

[OneTimeTearDown]
public void CleanUp() {
_client.Dispose();
}

[Test]
public async Task GetById_ShouldReturnHouse() {
// Arrange
var expectedHouse = HousesGenerator.Houses.First();
// Act
var response = await _client.GetAsync($"Houses/{expectedHouse.Id}");
response.StatusCode.Should().Be(HttpStatusCode.OK);
var house = await response.Content.DeserializeAsync<HouseDto>();

// Assert
house.Should().BeEquivalentTo(expectedHouse, o => o.Excluding(h => h.Heads)
.Excluding(h => h.Traits));
house.Heads.Should().BeEquivalentTo(expectedHouse.Heads, o => o.Excluding(s => s.HouseId));
house.Traits.Should().BeEquivalentTo(expectedHouse.Traits, o => o.Excluding(s => s.HouseId));

}

[Test]
public async Task Get_ShouldReturnHousesList() {
// Arrange
var expectedHouses = HousesGenerator.Houses;
// Act
var response = await _client.GetAsync("Houses");
var content = response.Content.ReadAsStringAsync().Result;

response.StatusCode.Should().Be(HttpStatusCode.OK);
var houses = await response.Content.DeserializeAsync<List<HouseDto>>();

// Assert
houses.Should().NotBeEmpty();
foreach (var house in houses) {
var expectedHouse = expectedHouses.Single(a => a.Id == house.Id);
house.Should().BeEquivalentTo(expectedHouse, o => o.Excluding(h => h.Heads)
.Excluding(h => h.Traits));
house.Heads.Should().BeEquivalentTo(expectedHouse.Heads, o => o.Excluding(s => s.HouseId));
house.Traits.Should().BeEquivalentTo(expectedHouse.Traits, o => o.Excluding(s => s.HouseId));
}
}

[Test]
public async Task GetById_NotExistingId_ShouldReturnNotFound() {
// Arrange
var notExistingId = Guid.NewGuid();
// Act
var response = await _client.GetAsync($"Houses/{notExistingId}");
// Assert
response.StatusCode.Should().Be(HttpStatusCode.NotFound);
}
}
}
34 changes: 34 additions & 0 deletions WizardWorldApi/WizardWorldApi.Tests.Shared/HousesGenerator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using System;
using System.Collections.Generic;
using Bogus;
using WizardWorld.Persistance.Models.Houses;

namespace WizardWorldApi.Tests.Shared {
public static class HousesGenerator {
public static IEnumerable<House> Houses { get; set; }

static HousesGenerator() {
var houseFaker = new Faker<House>()
.RuleFor(a => a.Id, f => Guid.NewGuid())
.RuleFor(a => a.Name, f => f.Lorem.Sentence())
.RuleFor(a => a.HouseColours, f => f.Lorem.Sentence())
.RuleFor(a => a.Founder, f => f.Lorem.Sentence())
.RuleFor(a => a.Animal, f => f.Lorem.Sentence())
.RuleFor(a => a.Element, f => f.Lorem.Sentence())
.RuleFor(a => a.Ghost, f => f.Lorem.Sentence())
.RuleFor(a => a.CommonRoom, f => f.Lorem.Sentence())
.RuleFor(a => a.Heads, (f, c) => new Faker<HouseHead>()
.RuleFor(a => a.Id, f => Guid.NewGuid())
.RuleFor(a => a.FirstName, f => f.Lorem.Sentence())
.RuleFor(a => a.LastName, f => f.Lorem.Sentence())
.RuleFor(a => a.HouseId, f => c.Id)
.Generate(3))
.RuleFor(a => a.Traits, (f, c) => new Faker<Trait>()
.RuleFor(a => a.Id, f => Guid.NewGuid())
.RuleFor(a => a.Name, f => f.PickRandom<TraitName>())
.RuleFor(a => a.HouseId, f => c.Id)
.Generate(3));
Houses = houseFaker.Generate(10);
}
}
}
39 changes: 39 additions & 0 deletions WizardWorldApi/WizardWorldApi.Tests.Unit/HouseMappingTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using System.Linq;
using AutoMapper;
using FluentAssertions;
using FluentAssertions.Equivalency;
using NUnit.Framework;
using WizardWorld.Application.Requests.Houses;
using WizardWorldApi.Tests.Shared;

namespace WizardWorldApi.Tests.Unit {
public class HouseMappingTests {
private static IMapper _mapper;

public HouseMappingTests() {
var mappingConfig = new MapperConfiguration(mc => { mc.AddProfile(new HouseMappingProfile()); });
_mapper = mappingConfig.CreateMapper();
}

[Test]
public void Map_House_ShouldReturnHouseDto() {
// Arrange
var house = HousesGenerator.Houses.First();

// Act
var houseDto = _mapper.Map<HouseDto>(house);

// Assert
houseDto.Id.Should().Be(house.Id);
houseDto.Name.Should().Be(house.Name);
houseDto.HouseColours.Should().Be(house.HouseColours);
houseDto.Founder.Should().Be(house.Founder);
houseDto.Animal.Should().Be(house.Animal);
houseDto.Element.Should().Be(house.Element);
houseDto.Ghost.Should().Be(house.Ghost);
houseDto.CommonRoom.Should().Be(house.CommonRoom);
houseDto.Heads.Should().BeEquivalentTo(house.Heads, o => o.Excluding(s => s.HouseId));
houseDto.Traits.Should().BeEquivalentTo(house.Traits, o => o.Excluding(s => s.HouseId));
}
}
}
34 changes: 34 additions & 0 deletions WizardWorldApi/WizardWorldApi/Controllers/HousesController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using MediatR;
using Microsoft.AspNetCore.Mvc;
using WizardWorld.Application.Requests.Houses;
using WizardWorld.Application.Requests.Houses.Queries.GetHouseById;
using WizardWorld.Application.Requests.Houses.Queries.GetHouses;
using WizardWorld.Persistance.Models.Houses;

namespace WizardWorldApi.Controllers {
[ApiController]
[Route("[controller]")]
public class HousesController : ControllerBase {
private readonly IMediator _mediator;

public HousesController(IMediator mediator) {
_mediator = mediator;
}

[HttpGet]
public async Task<List<HouseDto>> Get([FromQuery] GetHousesQuery query) {
return await _mediator.Send(query);
}

[HttpGet("{id}")]
public async Task<HouseDto> GetById([FromRoute] Guid id) {
var query = new GetHouseByIdQuery {
Id = id
};
return await _mediator.Send(query);
}
}
}