diff options
| author | Roy Ben-Shabat <Roy@Twine-s.com> | 2019-08-08 10:58:48 +0300 |
|---|---|---|
| committer | Roy Ben-Shabat <Roy@Twine-s.com> | 2019-08-08 10:58:48 +0300 |
| commit | 97411eb2550c6b483181b823da12f127cf903170 (patch) | |
| tree | f80ebf27c494c940efd652dc873cf52f27158130 /Software | |
| parent | 265e4bcf4b21b2317a88171b3326f65e58515d9c (diff) | |
| download | Tango-97411eb2550c6b483181b823da12f127cf903170.tar.gz Tango-97411eb2550c6b483181b823da12f127cf903170.zip | |
Unit Test for organizations.
Diffstat (limited to 'Software')
| -rw-r--r-- | Software/Visual_Studio_v2/Tango.BLL/Mappers/OrganizationToOrganizationEntityMapper.cs | 2 | ||||
| -rw-r--r-- | Software/Visual_Studio_v2/Tango.BLL/Services/OrganizationsService.cs | 44 | ||||
| -rw-r--r-- | Software/Visual_Studio_v2/Tango.UnitTests/BLL/Services/OrganizationsService_TST.cs | 63 | ||||
| -rw-r--r-- | Software/Visual_Studio_v2/Tango.UnitTests/DAL/MongoRepository_TST.cs (renamed from Software/Visual_Studio_v2/Tango.UnitTests/DAL/Mongo.cs) | 12 | ||||
| -rw-r--r-- | Software/Visual_Studio_v2/Tango.UnitTests/Tango.UnitTests.csproj | 7 |
5 files changed, 121 insertions, 7 deletions
diff --git a/Software/Visual_Studio_v2/Tango.BLL/Mappers/OrganizationToOrganizationEntityMapper.cs b/Software/Visual_Studio_v2/Tango.BLL/Mappers/OrganizationToOrganizationEntityMapper.cs index 741b2f0e8..0d6079f1f 100644 --- a/Software/Visual_Studio_v2/Tango.BLL/Mappers/OrganizationToOrganizationEntityMapper.cs +++ b/Software/Visual_Studio_v2/Tango.BLL/Mappers/OrganizationToOrganizationEntityMapper.cs @@ -11,6 +11,7 @@ namespace Tango.BLL.Mappers public void Map(Organization source, OrganizationEntity target) { target.ID = source.ID; + target.Name = source.Name; new AddressToAddressModelMapper().Map(source.Address, target.Address); new ContactToContactModelMapper().Map(source.Contact, target.Contact); } @@ -18,6 +19,7 @@ namespace Tango.BLL.Mappers public void Map(OrganizationEntity source, Organization target) { target.ID = source.ID; + target.Name = source.Name; new AddressToAddressModelMapper().Map(source.Address, target.Address); new ContactToContactModelMapper().Map(source.Contact, target.Contact); } diff --git a/Software/Visual_Studio_v2/Tango.BLL/Services/OrganizationsService.cs b/Software/Visual_Studio_v2/Tango.BLL/Services/OrganizationsService.cs index c18c57eba..a502849e3 100644 --- a/Software/Visual_Studio_v2/Tango.BLL/Services/OrganizationsService.cs +++ b/Software/Visual_Studio_v2/Tango.BLL/Services/OrganizationsService.cs @@ -13,21 +13,59 @@ namespace Tango.BLL.Services public class OrganizationsService : ServiceBase<Organization> { private IRepository<OrganizationEntity> _repository; + private OrganizationToOrganizationEntityMapper _mapper; public OrganizationsService(IRepository<OrganizationEntity> repository) { _repository = repository; + _mapper = new OrganizationToOrganizationEntityMapper(); } public async Task<List<Organization>> GetAllOrganizations() { var entities = await _repository.GetAllAsync(); - OrganizationToOrganizationEntityMapper mapper = new OrganizationToOrganizationEntityMapper(); - - var organizations = entities.Select(x => mapper.Create(x)).ToList(); + var organizations = entities.Select(x => _mapper.Create(x)).ToList(); return organizations; } + + public Task<long> DeleteAllOrganizations() + { + return _repository.DeleteAsync(x => true); + } + + public async Task<Organization> AddOrganization(Organization organization) + { + OrganizationEntity entity = _mapper.Create(organization); + + entity = await _repository.Insert(entity); + + _mapper.Map(entity, organization); + + return organization; + } + + public async Task<Organization> GetOrganizationByID(String id) + { + var entity = (await _repository.GetAsync(x => x.ID == id)).ToList().SingleOrDefault(); + + if (entity == null) + { + throw new KeyNotFoundException($"Could not find organization with id {id}."); + } + + return _mapper.Create(entity); + } + + public async Task<long> DeleteOrganizationByID(String id) + { + return await _repository.DeleteAsync(x => x.ID == id); + } + + public async Task<long> Count() + { + return await _repository.Count(); + } } } diff --git a/Software/Visual_Studio_v2/Tango.UnitTests/BLL/Services/OrganizationsService_TST.cs b/Software/Visual_Studio_v2/Tango.UnitTests/BLL/Services/OrganizationsService_TST.cs new file mode 100644 index 000000000..4db1af5c4 --- /dev/null +++ b/Software/Visual_Studio_v2/Tango.UnitTests/BLL/Services/OrganizationsService_TST.cs @@ -0,0 +1,63 @@ +using Microsoft.VisualStudio.TestTools.UnitTesting; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Tango.BLL.Objects; +using Tango.BLL.Services; +using Tango.DAL.Entities; +using Tango.DAL.Mongo; + +namespace Tango.UnitTests.BLL.Services +{ + [TestClass] + [TestCategory("BLL.OrganizationsService")] + public class OrganizationsService_TST + { + [TestMethod] + public void Simple_Operations() + { + OrganizationsService service = new OrganizationsService(new MongoRepository<OrganizationEntity>(new MongoDataBaseSettings() + { + Address = "mongodb://localhost:27017", + DatabaseName = "TEST" + })); + + service.DeleteAllOrganizations().GetAwaiter().GetResult(); + + Organization org1 = new Organization(); + org1.Name = "Org 1"; + org1.Address.AddressString = "Yohana 7 a"; + org1.Address.City = "Gan Yavne"; + org1.Address.State = "Israel"; + org1.Contact.Email = "roy@twine-s.com"; + org1.Contact.FirstName = "Roy"; + org1.Contact.LastName = "Ben Shabat"; + + service.AddOrganization(org1).GetAwaiter().GetResult(); + + Assert.IsNotNull(org1.ID); + + Organization orgDuplicate = new Organization(); + orgDuplicate.Name = org1.Name; + + Assert.ThrowsException<MongoDB.Driver.MongoWriteException>(() => + { + service.AddOrganization(orgDuplicate).GetAwaiter().GetResult(); + }); + + var org2 = service.GetOrganizationByID(org1.ID).GetAwaiter().GetResult(); + + Assert.AreEqual(org1.Name, org2.Name); + + long count = service.DeleteOrganizationByID(org1.ID).GetAwaiter().GetResult(); + + Assert.AreEqual(count, 1); + + count = service.Count().GetAwaiter().GetResult(); + + Assert.AreEqual(count, 0); + } + } +} diff --git a/Software/Visual_Studio_v2/Tango.UnitTests/DAL/Mongo.cs b/Software/Visual_Studio_v2/Tango.UnitTests/DAL/MongoRepository_TST.cs index d15b1c7a9..709a9c733 100644 --- a/Software/Visual_Studio_v2/Tango.UnitTests/DAL/Mongo.cs +++ b/Software/Visual_Studio_v2/Tango.UnitTests/DAL/MongoRepository_TST.cs @@ -7,7 +7,7 @@ using System.Linq; namespace Tango.UnitTests.DAL { [TestClass] - public class Mongo + public class MongoRepository_TST { [TestMethod] public void Simple_Operations() @@ -22,9 +22,17 @@ namespace Tango.UnitTests.DAL OrganizationEntity org1 = new OrganizationEntity(); org1.Name = "Org 1"; + org1.Address.AddressString = "Yohana 7 a"; + org1.Address.City = "Gan Yavne"; + org1.Address.State = "Israel"; + org1.Contact.Email = "roy@twine-s.com"; + org1.Contact.FirstName = "Roy"; + org1.Contact.LastName = "Ben Shabat"; repo.Insert(org1).GetAwaiter().GetResult(); + Assert.IsNotNull(org1.ID); + OrganizationEntity orgDuplicate = new OrganizationEntity(); orgDuplicate.Name = org1.Name; @@ -33,8 +41,6 @@ namespace Tango.UnitTests.DAL repo.Insert(orgDuplicate).GetAwaiter().GetResult(); }); - Assert.IsNotNull(org1.ID); - var org2 = repo.GetAsync(x => x.ID == org1.ID).GetAwaiter().GetResult().FirstOrDefault(); Assert.IsNotNull(org2); diff --git a/Software/Visual_Studio_v2/Tango.UnitTests/Tango.UnitTests.csproj b/Software/Visual_Studio_v2/Tango.UnitTests/Tango.UnitTests.csproj index e79ce12ae..23726f122 100644 --- a/Software/Visual_Studio_v2/Tango.UnitTests/Tango.UnitTests.csproj +++ b/Software/Visual_Studio_v2/Tango.UnitTests/Tango.UnitTests.csproj @@ -72,7 +72,8 @@ </Reference> </ItemGroup> <ItemGroup> - <Compile Include="DAL\Mongo.cs" /> + <Compile Include="BLL\Services\OrganizationsService_TST.cs" /> + <Compile Include="DAL\MongoRepository_TST.cs" /> <Compile Include="Properties\AssemblyInfo.cs" /> </ItemGroup> <ItemGroup> @@ -80,6 +81,10 @@ <None Include="packages.config" /> </ItemGroup> <ItemGroup> + <ProjectReference Include="..\Tango.BLL\Tango.BLL.csproj"> + <Project>{5f1f3818-a7e7-4810-95de-f5c1442b21c1}</Project> + <Name>Tango.BLL</Name> + </ProjectReference> <ProjectReference Include="..\Tango.DAL.Mongo\Tango.DAL.Mongo.csproj"> <Project>{9d1f1789-745c-4725-9dc4-12e0b0090a64}</Project> <Name>Tango.DAL.Mongo</Name> |
