diff options
5 files changed, 146 insertions, 10 deletions
| -rw-r--r-- | src/PCC.sln | 13 | ||||
| -rw-r--r-- | src/Service/PCC.Services/PartsService.cs | 7 | ||||
| -rw-r--r-- | src/Tests/PCC.Service.Tests/PCC.Service.Tests.csproj | 23 | ||||
| -rw-r--r-- | src/Tests/PCC.Service.Tests/PartsServiceTests.cs | 107 | ||||
| -rw-r--r-- | src/Web/PCC.Web/Controllers/CraftController.cs | 6 |
diff --git a/src/PCC.sln b/src/PCC.sln index 878e299..06d5a0b 100644 --- a/src/PCC.sln +++ b/src/PCC.sln @@ -25,7 +25,9 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Common", "Common", "{D65284 EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AutoMapperConfiguration", "Common\AutoMapperConfiguration\AutoMapperConfiguration.csproj", "{A7248860-7DE1-44E7-BBF3-84AD66BD9B01}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ConsoleApp1", "ConsoleApp1\ConsoleApp1.csproj", "{1DDA8B94-16E6-424D-8F2E-77186E11B04A}" +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Tests", "Tests", "{A0190EB9-C3B6-4860-8ADA-A573D702CABB}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PCC.Service.Tests", "Tests\PCC.Service.Tests\PCC.Service.Tests.csproj", "{7777FD98-A209-4B88-B2FC-50BBC8FDCE4D}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -61,10 +63,10 @@ Global {A7248860-7DE1-44E7-BBF3-84AD66BD9B01}.Debug|Any CPU.Build.0 = Debug|Any CPU {A7248860-7DE1-44E7-BBF3-84AD66BD9B01}.Release|Any CPU.ActiveCfg = Release|Any CPU {A7248860-7DE1-44E7-BBF3-84AD66BD9B01}.Release|Any CPU.Build.0 = Release|Any CPU - {1DDA8B94-16E6-424D-8F2E-77186E11B04A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {1DDA8B94-16E6-424D-8F2E-77186E11B04A}.Debug|Any CPU.Build.0 = Debug|Any CPU - {1DDA8B94-16E6-424D-8F2E-77186E11B04A}.Release|Any CPU.ActiveCfg = Release|Any CPU - {1DDA8B94-16E6-424D-8F2E-77186E11B04A}.Release|Any CPU.Build.0 = Release|Any CPU + {7777FD98-A209-4B88-B2FC-50BBC8FDCE4D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {7777FD98-A209-4B88-B2FC-50BBC8FDCE4D}.Debug|Any CPU.Build.0 = Debug|Any CPU + {7777FD98-A209-4B88-B2FC-50BBC8FDCE4D}.Release|Any CPU.ActiveCfg = Release|Any CPU + {7777FD98-A209-4B88-B2FC-50BBC8FDCE4D}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -77,6 +79,7 @@ Global {D4746A63-EE35-4774-905A-809B34E606F1} = {6DADFFC7-A55F-4EEF-9A8A-1172EB3CB0DF} {89B8C7A6-0192-46D3-B64E-7C36CEA22B21} = {6DADFFC7-A55F-4EEF-9A8A-1172EB3CB0DF} {A7248860-7DE1-44E7-BBF3-84AD66BD9B01} = {D65284CF-A362-481B-8692-7F2C842E7E19} + {7777FD98-A209-4B88-B2FC-50BBC8FDCE4D} = {A0190EB9-C3B6-4860-8ADA-A573D702CABB} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {01EE2CC2-F8F2-4011-8F26-46EDB74C38C7} diff --git a/src/Service/PCC.Services/PartsService.cs b/src/Service/PCC.Services/PartsService.cs index 4c48ca2..c902131 100644 --- a/src/Service/PCC.Services/PartsService.cs +++ b/src/Service/PCC.Services/PartsService.cs @@ -19,6 +19,13 @@ namespace PCC.Services public async Task<bool> CreateProcessorPart(ProcessorServiceModel processorServiceModel) { + if (string.IsNullOrEmpty(processorServiceModel.Brand)) + { + // TODO: Move message to constant + // Refactor this + throw new ArgumentException("Processor Brand cannot be null or empty!"); + } + Processor processorEntity = processorServiceModel.To<Processor>(); processorEntity.Id = Guid.NewGuid().ToString(); diff --git a/src/Tests/PCC.Service.Tests/PCC.Service.Tests.csproj b/src/Tests/PCC.Service.Tests/PCC.Service.Tests.csproj new file mode 100644 index 0000000..b339b14 --- /dev/null +++ b/src/Tests/PCC.Service.Tests/PCC.Service.Tests.csproj @@ -0,0 +1,23 @@ +<Project Sdk="Microsoft.NET.Sdk"> + + <PropertyGroup> + <TargetFramework>netcoreapp3.1</TargetFramework> + + <IsPackable>false</IsPackable> + </PropertyGroup> + + <ItemGroup> + <PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="5.0.4" /> + <PackageReference Include="NUnit" Version="3.12.0" /> + <PackageReference Include="NUnit3TestAdapter" Version="3.16.1" /> + <PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.5.0" /> + </ItemGroup> + + <ItemGroup> + <ProjectReference Include="..\..\Data\PCC.Data.Models\PCC.Data.Models.csproj" /> + <ProjectReference Include="..\..\Data\PCC.Data\PCC.Data.csproj" /> + <ProjectReference Include="..\..\Service\PCC.Services.Models\PCC.Services.Models.csproj" /> + <ProjectReference Include="..\..\Service\PCC.Services\PCC.Services.csproj" /> + </ItemGroup> + +</Project> diff --git a/src/Tests/PCC.Service.Tests/PartsServiceTests.cs b/src/Tests/PCC.Service.Tests/PartsServiceTests.cs new file mode 100644 index 0000000..46b8624 --- /dev/null +++ b/src/Tests/PCC.Service.Tests/PartsServiceTests.cs @@ -0,0 +1,107 @@ +namespace PCC.Service.Tests +{ + using AutoMapperConfiguration; + using Microsoft.EntityFrameworkCore; + using NUnit.Framework; + using PCC.Data; + using PCC.Data.Models; + using PCC.Services; + using PCC.Services.Models.Parts; + using System; + using System.Threading.Tasks; + + [TestFixture] + public class PartsServiceTests + { + private PCCDbContext pCCDbContext; + + private IPartsService partService; + + [SetUp] + public void Setup() + { + AutoMapperConfig.RegisterMappings( + typeof(PCCUser).Assembly.GetTypes(), + typeof(PartServiceModel).Assembly.GetTypes()); + + DbContextOptions<PCCDbContext> options = new DbContextOptionsBuilder<PCCDbContext>() + .UseInMemoryDatabase($"TESTS-DB-{Guid.NewGuid().ToString()}") + .Options; + + this.pCCDbContext = new PCCDbContext(options); + this.partService = new PartsService(this.pCCDbContext); + } + + [Test] + public async Task CreateProcessorPart_WithValidData_ShouldCorrectlyCreateEntity() + { + ProcessorServiceModel partServiceModel = new ProcessorServiceModel + { + Brand = "Intel", + Model = "Core I9", + Cores = 8, + Clockspeed = 4500, + ElectricalPowerConsumption = 3000, + Price = 2500, + Socket = "QWERTY", + ImageUrl = "https://never-gonna-give-you-up.com/rick.png", + }; + + Processor expectedEntity = new Processor + { + Brand = "Intel", + Model = "Core I9", + Cores = 8, + Clockspeed = 4500, + ElectricalPowerConsumption = 3000, + Price = 2500, + Socket = "QWERTY", + ImageUrl = "https://never-gonna-give-you-up.com/rick.png", + }; + + bool result = await this.partService.CreateProcessorPart(partServiceModel); + Processor actualEntity = await this.pCCDbContext.Processors.FirstOrDefaultAsync(); + + // TODO: Check if result is truthy (should be extracted into separate test) + Assert.True(result, "CreateProcessorPart() method should return truthy value with valid data."); + + // TODO: This can be a constant message with placeholder + Assert.AreEqual(expectedEntity.Brand, actualEntity.Brand, "CreateProcessorPart() method does not map Brand property correctly."); + Assert.AreEqual(expectedEntity.Model, actualEntity.Model, "CreateProcessorPart() method does not map Model property correctly."); + Assert.AreEqual(expectedEntity.Cores, actualEntity.Cores, "CreateProcessorPart() method does not map Cores property correctly."); + Assert.AreEqual(expectedEntity.Clockspeed, actualEntity.Clockspeed, "CreateProcessorPart() method does not map Clockspeed property correctly."); + Assert.AreEqual(expectedEntity.ElectricalPowerConsumption, actualEntity.ElectricalPowerConsumption, "CreateProcessorPart() method does not map ElectricalPowerConsumption property correctly."); + Assert.AreEqual(expectedEntity.Price, actualEntity.Price, "CreateProcessorPart() method does not map Price property correctly."); + Assert.AreEqual(expectedEntity.Socket, actualEntity.Socket, "CreateProcessorPart() method does not map Socket property correctly."); + Assert.AreEqual(expectedEntity.ImageUrl, actualEntity.ImageUrl, "CreateProcessorPart() method does not map ImageUrl property correctly."); + Assert.NotNull(actualEntity.Id, "CreateProcessorPart() method does not set Id property correctly."); + } + + [Test] + public async Task CreateProcessorPart_WithInvalidData_ShouldCorrectlyCreateEntity() + { + ProcessorServiceModel partServiceModel = new ProcessorServiceModel + { + Brand = null, + Model = "Core I9", + Cores = 8, + Clockspeed = 4500, + ElectricalPowerConsumption = 3000, + Price = 2500, + Socket = "QWERTY", + ImageUrl = "https://never-gonna-give-you-up.com/rick.png", + }; + + Exception ex = Assert.ThrowsAsync<ArgumentException>(() => this.partService.CreateProcessorPart(partServiceModel)); + + Assert.That(ex.Message == "Processor Brand cannot be null or empty!", "CreateProcessorPart() method with invalid Brand property, should throw ArgumentException with correct message."); + } + + [TearDown] + public void Dispose() + { + this.pCCDbContext.Dispose(); + this.partService = null; + } + } +} diff --git a/src/Web/PCC.Web/Controllers/CraftController.cs b/src/Web/PCC.Web/Controllers/CraftController.cs index ac2134f..ad0456c 100644 --- a/src/Web/PCC.Web/Controllers/CraftController.cs +++ b/src/Web/PCC.Web/Controllers/CraftController.cs @@ -1,8 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; -using Microsoft.AspNetCore.Mvc; +using Microsoft.AspNetCore.Mvc; namespace PCC.Web.Controllers { |