forked from dotnet-architecture/eShopOnWeb
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding unit tests, refactoring file access to a service, adding Catal…
…ogImageMissingException.
- Loading branch information
Showing
10 changed files
with
157 additions
and
14 deletions.
There are no files selected for viewing
13 changes: 13 additions & 0 deletions
13
src/ApplicationCore/Exceptions/CatalogImageMissingException.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
using System; | ||
|
||
namespace ApplicationCore.Exceptions | ||
{ | ||
public class CatalogImageMissingException : Exception | ||
{ | ||
public CatalogImageMissingException(string message, | ||
Exception innerException = null) | ||
: base(message, innerException: innerException) | ||
{ | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,5 +14,4 @@ public interface IIdentityParser<T> | |
{ | ||
T Parse(IPrincipal principal); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
using ApplicationCore.Entities; | ||
using Microsoft.eShopWeb.ApplicationCore.Entities; | ||
using System.Security.Principal; | ||
using System.Threading.Tasks; | ||
|
||
namespace ApplicationCore.Interfaces | ||
{ | ||
|
||
public interface IImageService | ||
{ | ||
byte[] GetImageBytesById(int id); | ||
} | ||
|
||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
using ApplicationCore.Interfaces; | ||
using Microsoft.AspNetCore.Hosting; | ||
using System.IO; | ||
|
||
namespace Infrastructure.FileSystem | ||
{ | ||
public class LocalFileImageService : IImageService | ||
{ | ||
private readonly IHostingEnvironment _env; | ||
|
||
public LocalFileImageService(IHostingEnvironment env) | ||
{ | ||
_env = env; | ||
} | ||
public byte[] GetImageBytesById(int id) | ||
{ | ||
var contentRoot = _env.ContentRootPath + "//Pics"; | ||
var path = Path.Combine(contentRoot, id + ".png"); | ||
return File.ReadAllBytes(path); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,25 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>netcoreapp1.1</TargetFramework> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.AspNetCore" Version="1.1.0" /> | ||
<PackageReference Include="Microsoft.AspNetCore.Mvc" Version="1.1.0" /> | ||
|
||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.0.0" /> | ||
<PackageReference Include="Moq" Version="4.7.8" /> | ||
<PackageReference Include="xunit" Version="2.2.0" /> | ||
<PackageReference Include="xunit.runner.visualstudio" Version="2.2.0" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\src\Web\Web.csproj" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<Service Include="{82a7f48d-3b50-4b1e-b82e-3ada8210c358}" /> | ||
</ItemGroup> | ||
|
||
</Project> |
60 changes: 60 additions & 0 deletions
60
tests/UnitTests/Web/Controllers/CatalogControllerGetImage.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
using ApplicationCore.Exceptions; | ||
using ApplicationCore.Interfaces; | ||
using Microsoft.AspNetCore.Hosting; | ||
using Microsoft.AspNetCore.Mvc; | ||
using Microsoft.eShopWeb.Controllers; | ||
using Moq; | ||
using Xunit; | ||
|
||
namespace UnitTests | ||
{ | ||
public class CatalogControllerGetImage | ||
{ | ||
private Mock<IImageService> _mockImageService = new Mock<IImageService>(); | ||
private CatalogController _controller; | ||
private int _testImageId = 123; | ||
private byte[] _testBytes = { 0x01, 0x02, 0x03 }; | ||
|
||
public CatalogControllerGetImage() | ||
{ | ||
_controller = new CatalogController(null, null, _mockImageService.Object); | ||
} | ||
|
||
[Fact] | ||
public void CallsImageServiceWithId() | ||
{ | ||
_mockImageService | ||
.Setup(i => i.GetImageBytesById(_testImageId)) | ||
.Returns(_testBytes) | ||
.Verifiable(); | ||
|
||
_controller.GetImage(_testImageId); | ||
_mockImageService.Verify(); | ||
} | ||
|
||
[Fact] | ||
public void ReturnsFileResultWithBytesGivenSuccess() | ||
{ | ||
_mockImageService | ||
.Setup(i => i.GetImageBytesById(_testImageId)) | ||
.Returns(_testBytes); | ||
|
||
var result = _controller.GetImage(_testImageId); | ||
|
||
var fileResult = Assert.IsType<FileContentResult>(result); | ||
var bytes = Assert.IsType<byte[]>(fileResult.FileContents); | ||
} | ||
|
||
[Fact] | ||
public void ReturnsNotFoundResultGivenImageMissingException() | ||
{ | ||
_mockImageService | ||
.Setup(i => i.GetImageBytesById(_testImageId)) | ||
.Throws(new CatalogImageMissingException("missing image")); | ||
|
||
var result = _controller.GetImage(_testImageId); | ||
|
||
var actionResult = Assert.IsType<NotFoundResult>(result); | ||
} | ||
} | ||
} |