Skip to content

Commit

Permalink
Add Model validation and unit test for HTTP 400s
Browse files Browse the repository at this point in the history
  • Loading branch information
davevans committed Jul 29, 2014
1 parent ffd7630 commit 6fbf7f1
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@
<Compile Include="ViewModels\AddressViewModel.cs" />
<Compile Include="ViewModels\ChurchViewModel.cs" />
<Compile Include="ViewModels\LocationViewModel.cs" />
<Compile Include="ViewModels\TimeZoneViewModel.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Church.Common\Church.Common.csproj">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@ public HttpResponseMessage ChurchById(int churchId)
[Route("api/church")]
public HttpResponseMessage AddChurch([FromBody]ChurchViewModel churchViewModel)
{
//todo: validate churchViewModel
if (!ModelState.IsValid)
{
return Request.CreateErrorResponse(HttpStatusCode.BadRequest, @"Invalid Church");
}

var church = Mapper.Map<ChurchViewModel, Model.Core.Church>(churchViewModel);
_churchService.Add(church);
Expand Down
3 changes: 3 additions & 0 deletions src/Church/Church.Host.Owin.Core/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ void RegisterComponents(TinyIoCContainer container)

private void RegisterMappings()
{
Mapper.CreateMap<Model.Core.TimeZone, TimeZoneViewModel>();
Mapper.CreateMap<TimeZoneViewModel, Model.Core.TimeZone>();

Mapper.CreateMap<Model.Core.Church, ChurchViewModel>()
.ForMember(x => x.Id, o => o.MapFrom(d => d.Id))
.ForMember(x => x.Name, o => o.MapFrom(d => d.Name))
Expand Down
10 changes: 8 additions & 2 deletions src/Church/Church.Host.Owin.Core/ViewModels/ChurchViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
namespace Church.Host.Owin.Core.ViewModels
using System.ComponentModel.DataAnnotations;

namespace Church.Host.Owin.Core.ViewModels
{
public class ChurchViewModel
{
public int Id { get; set; }

[Required(AllowEmptyStrings = false, ErrorMessage = "Name is required.")]
public string Name { get; set; }
public Model.Core.TimeZone TimeZone { get; set; }

[Required(ErrorMessage = "TimeZone is requried.")]
public TimeZoneViewModel TimeZone { get; set; }
}
}
12 changes: 12 additions & 0 deletions src/Church/Church.Host.Owin.Core/ViewModels/TimeZoneViewModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System.ComponentModel.DataAnnotations;

namespace Church.Host.Owin.Core.ViewModels
{
public class TimeZoneViewModel
{
[Required]
public int Id { get; set; }
[Required]
public string Name { get; set; }
}
}
80 changes: 74 additions & 6 deletions src/Church/Church.IntegrationTests/ChurchControllerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,32 @@ public void ReturnsCreatedHttpStatusCode()
var churchViewModel = new ChurchViewModel
{
Name = "Foo",
TimeZone = new TimeZone
TimeZone = new TimeZoneViewModel
{
Id = 20,
Name = "Sydney"
}
};

var mockChurchService = MockRepository.GenerateStub<IChurchService>();
mockChurchService.Stub(x => x.Add(Arg<Model.Core.Church>.Is.Anything));
Container.Register(typeof (IChurchService), mockChurchService);

//ACT
var response = Server.HttpClient.PostAsJsonAsync("/api/church", churchViewModel).Result;

//ASSERT
Assert.AreEqual(HttpStatusCode.Created, response.StatusCode, "Expected HttpStatusCode of CREATED");
}

[Test]
public void ReturnsChurchWithId()
{
//arrange
var churchViewModel = new ChurchViewModel
{
Name = "Foo",
TimeZone = new TimeZoneViewModel
{
Id = 20,
Name = "Sydney"
Expand All @@ -104,19 +129,62 @@ public void ReturnsCreatedHttpStatusCode()
mockChurchService.Stub(x => x.Add(Arg<Model.Core.Church>.Is.Anything))
.Callback((Model.Core.Church c) =>
{
c.Id = 101;
return true;
c.Id = 101;
return true;
});


Container.Register(typeof (IChurchService), mockChurchService);

Container.Register(typeof(IChurchService), mockChurchService);

//ACT
var response = Server.HttpClient.PostAsJsonAsync("/api/church", churchViewModel).Result;
var newChurch = JsonConvert.DeserializeObject<ChurchViewModel>(response.Content.ReadAsStringAsync().Result);

//ASSERT
Assert.AreEqual(HttpStatusCode.Created, response.StatusCode, "Expected HttpStatusCode of CREATED");
Assert.AreEqual(101, newChurch.Id, "expected id 101");
}

[Test]
public void ReturnsBadRequestForChurchWithNoName()
{
//arrange
var churchViewModel = new ChurchViewModel
{
Name = null,
TimeZone = new TimeZoneViewModel
{
Id = 20,
Name = "Sydney"
}
};

var mockChurchService = MockRepository.GenerateStub<IChurchService>();
Container.Register(typeof(IChurchService), mockChurchService);

//ACT
var response = Server.HttpClient.PostAsJsonAsync("/api/church", churchViewModel).Result;

//ASSERT
Assert.AreEqual(HttpStatusCode.BadRequest, response.StatusCode, "Expected HttpStatusCode of BadRequest");
}

[Test]
public void ReturnsBadRequestForChurchWithNoTimeZone()
{
//arrange
var churchViewModel = new ChurchViewModel
{
Name = "MyChurch",
};

var mockChurchService = MockRepository.GenerateStub<IChurchService>();
Container.Register(typeof(IChurchService), mockChurchService);

//ACT
var response = Server.HttpClient.PostAsJsonAsync("/api/church", churchViewModel).Result;

//ASSERT
Assert.AreEqual(HttpStatusCode.BadRequest, response.StatusCode, "Expected HttpStatusCode of BadRequest");
}

}
Expand Down

0 comments on commit 6fbf7f1

Please sign in to comment.