-
-
Notifications
You must be signed in to change notification settings - Fork 102
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from Boriszn/feature/ISS-1-Add-Automapper
Feature/iss 1 add automapper
- Loading branch information
Showing
17 changed files
with
212 additions
and
55 deletions.
There are no files selected for viewing
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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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
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,26 @@ | ||
using System; | ||
using AutoMapper.Configuration; | ||
using DeviceManager.Api.Data.Model; | ||
using DeviceManager.Api.Model; | ||
|
||
namespace DeviceManager.Api.Mappings | ||
{ | ||
/// <summary> | ||
/// Contains objects mapping | ||
/// </summary> | ||
/// <seealso cref="AutoMapper.Configuration.MapperConfigurationExpression" /> | ||
public class MapsProfile : MapperConfigurationExpression | ||
{ | ||
/// <summary> | ||
/// Initializes a new instance of the <see cref="MapsProfile"/> class | ||
/// </summary> | ||
public MapsProfile() | ||
{ | ||
// Device ViewModel To Device | ||
this.CreateMap<DeviceViewModel, Device>() | ||
.ForMember(dest => dest.DeviceTitle, opt => opt.MapFrom(src => src.Title)) | ||
.ForMember(dest => dest.DeviceId, opt => opt.MapFrom(src => Guid.NewGuid())) | ||
; | ||
} | ||
} | ||
} |
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
File renamed without changes.
File renamed without changes.
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
125 changes: 125 additions & 0 deletions
125
test/DeviceManager.Api.UnitTests/Services/DeviceServiceBuilder.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,125 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Linq.Expressions; | ||
using AutoMapper; | ||
using DeviceManager.Api.Services; | ||
using DeviceManager.Api.Data.Management; | ||
using DeviceManager.Api.Data.Model; | ||
using DeviceManager.Api.Mappings; | ||
using Microsoft.EntityFrameworkCore; | ||
using Moq; | ||
|
||
namespace DeviceManager.Api.UnitTests.Services | ||
{ | ||
public class DeviceServiceBuilder | ||
{ | ||
private readonly Mock<IRepository<Device>> mockRepository; | ||
private readonly Mock<IUnitOfWork> mockUnitOfWork; | ||
private readonly MapperConfiguration mapperConfiguration; | ||
private readonly Mapper mapper; | ||
|
||
public DeviceServiceBuilder() | ||
{ | ||
var mockRepositoryObjet = new MockRepository(MockBehavior.Strict); | ||
|
||
mockUnitOfWork = mockRepositoryObjet.Create<IUnitOfWork>(); | ||
mockRepository = mockRepositoryObjet.Create<IRepository<Device>>(); | ||
|
||
// Default automapper configuration | ||
mapperConfiguration = new MapperConfiguration(new MapsProfile()); | ||
mapper = new Mapper(mapperConfiguration); | ||
} | ||
|
||
/// <summary> | ||
/// With the repository methods mock. | ||
/// </summary> | ||
/// <param name="devicesList">The devices list.</param> | ||
/// <param name="device">The device.</param> | ||
/// <returns></returns> | ||
public DeviceServiceBuilder WithRepositoryMock(List<Device> devicesList, Device device) | ||
{ | ||
// 'GetAll' repository mock | ||
this.mockRepository.Setup(x => x.GetAll(1, 1)).Returns(devicesList.AsQueryable); | ||
|
||
// 'Get' repository mock | ||
this.mockRepository.Setup(x => x.Get(It.IsAny<Guid>())).Returns(device); | ||
|
||
// 'Update' repository mock | ||
this.mockRepository.Setup(x => x.Update(It.IsAny<Device>())).Returns(It.IsAny<EntityState>()); | ||
|
||
// 'Add' repository mock | ||
this.mockRepository.Setup(x => x.Add(It.IsAny<Device>())).Returns(EntityState.Added); | ||
|
||
// 'FindBy' repository mock | ||
this.mockRepository.Setup(x => x.FindBy(It.IsAny<Expression<Func<Device, bool>>>())) | ||
.Returns(() => | ||
devicesList.AsQueryable()); | ||
|
||
return this; | ||
} | ||
|
||
/// <summary> | ||
/// With the repository GetAll mock. | ||
/// </summary> | ||
/// <param name="devicesList">The devices list.</param> | ||
/// <returns></returns> | ||
public DeviceServiceBuilder WithRepositoryGetAllMock(List<Device> devicesList) | ||
{ | ||
// 'GetAll' repository mock | ||
this.mockRepository.Setup(x => x.GetAll()).Returns(devicesList.AsQueryable); | ||
|
||
return this; | ||
} | ||
|
||
/// <summary> | ||
/// With the repository get all mock. | ||
/// </summary> | ||
/// <param name="page">The page.</param> | ||
/// <param name="pageSize">Size of the page.</param> | ||
/// <param name="devicesList">The devices list.</param> | ||
/// <returns></returns> | ||
public DeviceServiceBuilder WithRepositoryGetAllMock(int page, int pageSize, List<Device> devicesList) | ||
{ | ||
// 'GetAll' repository mock | ||
this.mockRepository.Setup(x => x.GetAll(page, pageSize)).Returns(devicesList.AsQueryable); | ||
|
||
return this; | ||
} | ||
|
||
/// <summary> | ||
/// With the repository Get mock. | ||
/// </summary> | ||
/// <param name="device">The device.</param> | ||
/// <returns></returns> | ||
public DeviceServiceBuilder WithRepositoryGetMock(Device device) | ||
{ | ||
// 'Get' repository mock | ||
this.mockRepository.Setup(x => x.Get(It.IsAny<Guid>())).Returns(device); | ||
|
||
return this; | ||
} | ||
|
||
/// <summary> | ||
/// With the unit of work setup. | ||
/// </summary> | ||
/// <returns></returns> | ||
public DeviceServiceBuilder WithUnitOfWorkSetup() | ||
{ | ||
this.mockUnitOfWork.Setup(x => x.Commit()).Returns(1); | ||
this.mockUnitOfWork.Setup(u => u.GetRepository<Device>()).Returns(this.mockRepository.Object); | ||
return this; | ||
} | ||
|
||
/// <summary> | ||
/// Builds this instance. | ||
/// </summary> | ||
/// <returns></returns> | ||
public DeviceService Build() | ||
{ | ||
return new DeviceService( | ||
this.mockUnitOfWork.Object, | ||
mapper); | ||
} | ||
} | ||
} |
Oops, something went wrong.