Skip to content

Commit 9881557

Browse files
committed
refactoring
1 parent 7801c9e commit 9881557

File tree

10 files changed

+353
-3
lines changed

10 files changed

+353
-3
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
using NUnit.Framework;
2+
using OA.Test.Integration;
3+
using System.Net;
4+
using System.Net.Http;
5+
using System.Threading.Tasks;
6+
7+
namespace OA.Test.Integration
8+
{
9+
public class ApiCustomerTest
10+
{
11+
[TestCase("Get", "api/Customer")]
12+
[TestCase("Get", "api/Customer/Amazon")]
13+
public async Task GetAllCustomerTestAsync(string method, string URL)
14+
{
15+
using (var client = new TestClientProvider().Client)
16+
{
17+
var request = new HttpRequestMessage(new HttpMethod(method), URL);
18+
var response = await client.SendAsync(request);
19+
20+
response.EnsureSuccessStatusCode();
21+
22+
Assert.AreEqual(HttpStatusCode.OK, response.StatusCode);
23+
}
24+
}
25+
}
26+
}

OA/OA.Test.Integration/OA.Test.Integration.csproj

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,14 @@
77
</PropertyGroup>
88

99
<ItemGroup>
10+
<PackageReference Include="Microsoft.AspNetCore.TestHost" Version="3.1.5" />
1011
<PackageReference Include="nunit" Version="3.12.0" />
1112
<PackageReference Include="NUnit3TestAdapter" Version="3.15.1" />
12-
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.4.0"/>
13+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.4.0" />
14+
</ItemGroup>
15+
16+
<ItemGroup>
17+
<ProjectReference Include="..\OA\OA.csproj" />
1318
</ItemGroup>
1419

1520
</Project>
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
using Microsoft.AspNetCore.Hosting;
2+
using Microsoft.AspNetCore.TestHost;
3+
using System.Net.Http;
4+
5+
namespace OA.Test.Integration
6+
{
7+
public class TestClientProvider
8+
{
9+
public HttpClient Client { get; private set; }
10+
11+
public TestClientProvider()
12+
{
13+
var server = new TestServer(new WebHostBuilder().UseStartup<Startup>());
14+
15+
Client = server.CreateClient();
16+
}
17+
}
18+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
using AutoMapper;
2+
using Microsoft.AspNetCore.Mvc;
3+
using Moq;
4+
using NUnit.Framework;
5+
using OA.Controllers;
6+
using OA.Domain.Entities;
7+
using OA.Infrastructure.Model;
8+
using OA.Persistence.Contract;
9+
using OA.Service.Contract;
10+
using System.Threading.Tasks;
11+
12+
namespace OA.Test.Unit.Controller
13+
{
14+
public class CustomerControllerTest
15+
{
16+
private CustomerController controller;
17+
private Mock<ICustomerRepository> customerRepositoryMock;
18+
private Mock<IMapper> mapperMock;
19+
private Mock<ICustomerService> customerServiceMock;
20+
21+
[SetUp]
22+
public void Setup()
23+
{
24+
customerRepositoryMock = new Mock<ICustomerRepository>();
25+
mapperMock = new Mock<IMapper>();
26+
customerServiceMock = new Mock<ICustomerService>();
27+
28+
controller = new CustomerController(mapperMock.Object, customerServiceMock.Object, customerRepositoryMock.Object);
29+
30+
customerServiceMock.Setup(x => x.SaveChangesAsync()).Returns(true);
31+
32+
CustomerModel customerModel = new CustomerModel
33+
{ CustomerName = "Shweta Naik", Address = "Bangalore" };
34+
Customer customer = new Customer
35+
{ CustomerName = "Shweta Naik", Address = "Bangalore" };
36+
37+
mapperMock.Setup(x => x.Map<CustomerModel>(customer)).Returns(customerModel);
38+
}
39+
40+
[Test]
41+
public async Task GetAllCustomerTestAsyncOkResult()
42+
{
43+
44+
var response = await controller.Get();
45+
Assert.IsInstanceOf<OkObjectResult>(response);
46+
47+
}
48+
49+
[Test]
50+
public async Task PostCustomerTestAsync()
51+
{
52+
CustomerModel customerModel = new CustomerModel
53+
{ CustomerName = "Shweta Naik", Address = "Bangalore" };
54+
55+
var response = await controller.Post(customerModel);
56+
57+
var item = response.Value;
58+
Assert.IsInstanceOf<CustomerModel>(customerModel);
59+
Assert.AreEqual("Shweta Naik", item.CustomerName);
60+
}
61+
62+
}
63+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
using Microsoft.EntityFrameworkCore;
2+
using NUnit.Framework;
3+
using OA.Data;
4+
using OA.Domain.Entities;
5+
6+
namespace OA.Test.Unit.Data
7+
{
8+
public class CustomerContextTest
9+
{
10+
11+
[Test]
12+
public void CanInsertCustomerIntoDatabasee()
13+
{
14+
15+
using (var context = new CustomerContext())
16+
{
17+
var customer = new Customer();
18+
context.Customers.Add(customer);
19+
Assert.AreEqual(EntityState.Added, context.Entry(customer).State);
20+
}
21+
}
22+
}
23+
}

OA/OA.Test.Unit/OA.Test.Unit.csproj

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,15 @@
77
</PropertyGroup>
88

99
<ItemGroup>
10+
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="3.1.5" />
11+
<PackageReference Include="Moq" Version="4.14.4" />
1012
<PackageReference Include="nunit" Version="3.12.0" />
1113
<PackageReference Include="NUnit3TestAdapter" Version="3.15.1" />
12-
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.4.0"/>
14+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.4.0" />
15+
</ItemGroup>
16+
17+
<ItemGroup>
18+
<ProjectReference Include="..\OA\OA.csproj" />
1319
</ItemGroup>
1420

1521
</Project>
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
using Microsoft.EntityFrameworkCore;
2+
using NUnit.Framework;
3+
using OA.Data;
4+
using OA.Domain.Entities;
5+
using OA.Persistence.Repository;
6+
using System.Linq;
7+
using System.Threading.Tasks;
8+
9+
namespace OA.Test.Unit.Persistence
10+
{
11+
public class CustomerRepositoryTest
12+
{
13+
private DbContextOptionsBuilder builder;
14+
15+
private CustomerContext context;
16+
17+
[SetUp]
18+
public void Setup()
19+
{
20+
builder = new DbContextOptionsBuilder();
21+
builder.UseInMemoryDatabase("InMemoryCustomerDB");
22+
23+
SettingUp();
24+
25+
}
26+
27+
[Test]
28+
public async Task CheckCustomerRepositoryGetAllCustomersAsyn()
29+
{
30+
var customerRepository = new CustomerRepository(context);
31+
var result = await customerRepository.GetAllCustomersAsync();
32+
Assert.LessOrEqual(2, result.Count());
33+
}
34+
35+
[Test]
36+
public async Task CheckCustomerRepositoryGetCustomerAsync()
37+
{
38+
string name = "Shweta Naik";
39+
var customerRepository = new CustomerRepository(context);
40+
var result = await customerRepository.GetCustomerAsync(name);
41+
Assert.AreEqual(name, result.CustomerName);
42+
}
43+
44+
void SettingUp()
45+
{
46+
// Inserting to inmemory database
47+
context = new CustomerContext(builder.Options);
48+
var customerRepository = new GenericRepository<Customer>(context);
49+
customerRepository.Add(new Customer { CustomerName = "Shweta Naik", Address = "Bangalore" });
50+
customerRepository.Add(new Customer { CustomerName = "Amit Naik", Address = "Bangalore" });
51+
customerRepository.SaveChanges();
52+
}
53+
}
54+
}
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
using Microsoft.EntityFrameworkCore;
2+
using NUnit.Framework;
3+
using OA.Data;
4+
using OA.Domain.Entities;
5+
using OA.Persistence.Repository;
6+
using System.Linq;
7+
8+
namespace OA.Test.Unit.Persistence
9+
{
10+
public class GenericRepositoryTest
11+
{
12+
private DbContextOptionsBuilder builder;
13+
14+
Customer customer;
15+
16+
[SetUp]
17+
public void Setup()
18+
{
19+
20+
builder = new DbContextOptionsBuilder();
21+
builder.UseInMemoryDatabase("InMemoryCustomerDB");
22+
23+
customer = new Customer
24+
{
25+
CustomerName = "Shweta Naik",
26+
Address = "Bangalore"
27+
};
28+
}
29+
30+
[Test]
31+
public void CheckGenenricRepositoryAddCustomer()
32+
{
33+
using (var context = new CustomerContext(builder.Options))
34+
{
35+
var customerRepository = new GenericRepository<Customer>(context);
36+
customerRepository.Add(customer);
37+
var result = customerRepository.SaveChanges();
38+
Assert.IsTrue(result);
39+
}
40+
}
41+
42+
[Test]
43+
public void CheckGenenricRepositoryUpdateCustomer()
44+
{
45+
using (var context = new CustomerContext(builder.Options))
46+
{
47+
var customerRepository = new GenericRepository<Customer>(context);
48+
customerRepository.Update(customer);
49+
var result = customerRepository.SaveChanges();
50+
Assert.IsTrue(result);
51+
}
52+
}
53+
54+
[Test]
55+
public void CheckGenenricRepositoryDeleteCustomer()
56+
{
57+
using (var context = new CustomerContext(builder.Options))
58+
{
59+
var customerRepository = new GenericRepository<Customer>(context);
60+
61+
customerRepository.Add(customer);
62+
customerRepository.SaveChanges();
63+
64+
customerRepository.Delete(customer.Id);
65+
var result = customerRepository.SaveChanges();
66+
Assert.IsTrue(result);
67+
}
68+
}
69+
70+
[Test]
71+
public void CheckGenenricRepositoryGetCustomer()
72+
{
73+
using (var context = new CustomerContext(builder.Options))
74+
{
75+
76+
var customerRepository = new GenericRepository<Customer>(context);
77+
customerRepository.Add(new Customer { CustomerName = "Shweta Naik", Address = "Bangalore" });
78+
customerRepository.Add(new Customer { CustomerName = "Amit Naik", Address = "Bangalore" });
79+
customerRepository.SaveChanges();
80+
81+
var cust = customerRepository.GetAll();
82+
83+
Assert.LessOrEqual(2, cust.Count());
84+
}
85+
}
86+
87+
[Test]
88+
public void CheckGenenricRepositoryGetByIdCustomer()
89+
{
90+
using (var context = new CustomerContext(builder.Options))
91+
{
92+
var customerRepository = new GenericRepository<Customer>(context);
93+
customerRepository.Add(new Customer { CustomerName = "Shweta Naik", Address = "Bangalore" });
94+
customerRepository.Add(new Customer { CustomerName = "Amit Naik", Address = "Bangalore" });
95+
customerRepository.SaveChanges();
96+
97+
var cust = customerRepository.GetById(1);
98+
99+
Assert.AreEqual(1, cust.Id);
100+
}
101+
}
102+
103+
}
104+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
using Moq;
2+
using NUnit.Framework;
3+
using OA.Domain.Entities;
4+
using OA.Persistence.Contract;
5+
using OA.Service.Implementation;
6+
7+
namespace OA.Test.Unit.Service
8+
{
9+
public class CustomerServiceTest
10+
{
11+
private CustomerService customerService;
12+
private Mock<IGenericRepository<Customer>> genericRepositoryMock;
13+
Customer customer;
14+
15+
[SetUp]
16+
public void Setup()
17+
{
18+
genericRepositoryMock = new Mock<IGenericRepository<Customer>>();
19+
customerService = new CustomerService(genericRepositoryMock.Object);
20+
21+
genericRepositoryMock.Setup(x => x.SaveChanges()).Returns(true);
22+
23+
customer = new Customer
24+
{
25+
CustomerName = "Shweta Naik",
26+
Address = "Bangalore"
27+
};
28+
}
29+
30+
[Test]
31+
public void CheckCustomerServiceAddCustomer()
32+
{
33+
customerService.AddCustomer(customer);
34+
var result = customerService.SaveChangesAsync();
35+
Assert.IsTrue(result);
36+
37+
}
38+
39+
[Test]
40+
public void CheckCustomerServiceDeleteCustomer()
41+
{
42+
customerService.DeleteCustomer(customer.Id);
43+
var result = customerService.SaveChangesAsync();
44+
Assert.IsTrue(result);
45+
46+
}
47+
}
48+
}

OA/OA/appsettings.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,8 @@
66
"Microsoft.Hosting.Lifetime": "Information"
77
}
88
},
9-
"AllowedHosts": "*"
9+
"AllowedHosts": "*",
10+
"ConnectionStrings": {
11+
"OnionArchConn": "Data Source=(local)\\SQLexpress;Initial Catalog=OnionArchitectureDB;Integrated Security=True"
12+
}
1013
}

0 commit comments

Comments
 (0)