Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
using CoffeeMachineSimulator.Sender.Model.CoffeeMachine.Simulator.Sender.Model;
using CoffeeMachineSimulator.Data.Entities;
using CoffeeMachineSimulator.Sender.Model.CoffeeMachine.Simulator.Sender.Model;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace CoffeeMachineSimulator.Services.Interfaces
{
public interface ICoffeeDataService
{
Task AddCoffeeData(CoffeeMachineData coffeeMachineData);
Task<List<CoffeeMachineData>> GetCoffeeDatas();
Task DeleteFirstCoffeeData();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
using CoffeeMachineSimulator.Data.Entities;
using CoffeeMachineSimulator.Sender.Model.CoffeeMachine.Simulator.Sender.Model;
using CoffeeMachineSimulator.Services.Interfaces;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace CoffeeMachineSimulator.Services.Services
Expand All @@ -19,10 +22,28 @@ public CoffeeDataService(CoffeeContext context, IMapper mapper)
}
public async Task AddCoffeeData(CoffeeMachineData coffeeMachineData)
{
if (coffeeMachineData == null) throw new Exception("You should not add null entry");

var myCoffeeDataToAdd = mapper.Map<CoffeeDataEntity>(coffeeMachineData);

await coffeeContext.CoffeeDataEntities.AddAsync(myCoffeeDataToAdd);
await coffeeContext.SaveChangesAsync();
}

public async Task DeleteFirstCoffeeData()
{
var coffeeToDelete = await coffeeContext.CoffeeDataEntities.FirstOrDefaultAsync();

coffeeContext.CoffeeDataEntities.Remove(coffeeToDelete);

await coffeeContext.SaveChangesAsync();
}

public async Task<List<CoffeeMachineData>> GetCoffeeDatas()
{
var coffeeDataEntities = await coffeeContext.CoffeeDataEntities.ToListAsync();

return mapper.Map<List<CoffeeMachineData>>(coffeeDataEntities);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ namespace CoffeeMachineSimulator.Tests
{
public class ServiceSetUp
{
protected static readonly Guid CoffeeEspressorId = Guid.NewGuid();
protected static readonly Guid EspressorEspressorId = Guid.NewGuid();

protected readonly Mapper Mapper;
protected CoffeeContext Context { get; private set; }

Expand All @@ -26,6 +25,8 @@ protected void SetUpDatabase()
var options = new DbContextOptionsBuilder<CoffeeContext>()
.UseInMemoryDatabase(databaseName: "CoffeeDb")
.Options;
Guid CoffeeEspressorId = Guid.NewGuid();
Guid EspressorEspressorId = Guid.NewGuid();

Context = new CoffeeContext(options);

Expand All @@ -36,6 +37,9 @@ protected void SetUpDatabase()
Context.Coffees.Add(new CoffeeEntity { Name = "Second Coffee", Price = 30, Sweetness = Sweetness.Sweet, EspressoMachineId = EspressorEspressorId });
Context.Coffees.Add(new CoffeeEntity { Name = "Third Coffee", Price = 45, Sweetness = Sweetness.LessSweet, EspressoMachineId = EspressorEspressorId });

Context.CoffeeDataEntities.Add(new CoffeeDataEntity { City = "Brasov", RecordingTime=DateTime.Now, SensorType="Sensor1", SensorValue=1, SerialNumber="12345" });
Context.CoffeeDataEntities.Add(new CoffeeDataEntity { City = "Cluj", RecordingTime = DateTime.Now, SensorType = "Sensor2", SensorValue = 2, SerialNumber = "12346" });

Context.SaveChanges();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
using CoffeeMachineSimulator.Sender.Model.CoffeeMachine.Simulator.Sender.Model;
using CoffeeMachineSimulator.Services.Services;
using NUnit.Framework;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace CoffeeMachineSimulator.Tests.Services
{
[TestFixture]
public class CoffeeDataServiceTests : ServiceSetUp
{
private CoffeeDataService coffeeDataService;
public CoffeeDataServiceTests() : base() { }

[SetUp]
public void SetUp()
{
coffeeDataService = new CoffeeDataService(Context, Mapper);
}
[Test]
public async Task DeleteFirstCoffeeData_DeletesCoffeeData()
{
var expectedCountOfCoffeeDatas = (await coffeeDataService.GetCoffeeDatas()).Count;

await coffeeDataService.DeleteFirstCoffeeData();

var actualCountOfCoffeeDatas = (await coffeeDataService.GetCoffeeDatas()).Count;

Assert.AreNotEqual(expectedCountOfCoffeeDatas, actualCountOfCoffeeDatas);
}

[Test]
public void AddNullCoffeeData_Throws_NewException()
{
var ex = Assert.ThrowsAsync<Exception>(() => coffeeDataService.AddCoffeeData(null));

Assert.AreEqual(ex.Message, "You should not add null entry");
}

[Test]
public async Task GetCoffeeDatas_Returns_ListOfCoffeeDataEntities()
{
var coffeeDatasReturned = await coffeeDataService.GetCoffeeDatas();

Assert.IsNotNull(coffeeDatasReturned);
Assert.IsInstanceOf(typeof(List<CoffeeMachineData>), coffeeDatasReturned);
}
}

}