Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Category Controller. #27

Merged
merged 26 commits into from
Aug 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
55bc8b4
first attempt to singletonize the scoped classes
SwimmingRieux Aug 19, 2024
803eb63
add context lazy loading
SwimmingRieux Aug 19, 2024
6a4ebdd
Merge remote-tracking branch 'origin/main' into singleton
SwimmingRieux Aug 19, 2024
0b7afa6
add context container
SwimmingRieux Aug 19, 2024
05cd268
converted all the scoped classes to singleton classes
SwimmingRieux Aug 19, 2024
5eed692
resolved tests errors.
SwimmingRieux Aug 19, 2024
d477355
add get_graph endpoint to graphcontroller
SwimmingRieux Aug 19, 2024
7b66c77
add getgraph endpoint integration test
SwimmingRieux Aug 19, 2024
d6a33ca
merge
SwimmingRieux Aug 19, 2024
727c49d
fix integration test bug
SwimmingRieux Aug 19, 2024
c311712
Make Db methods async.
msmahdinejad Aug 20, 2024
2200a41
create Categories class.
msmahdinejad Aug 20, 2024
067cc97
Add Failed CreateEdgeCategory Unit Tests.
msmahdinejad Aug 20, 2024
a8d84fa
complete CreateEdgeCategory class.
msmahdinejad Aug 20, 2024
4836d84
Pass CreateEdgeCategory Tests.
msmahdinejad Aug 20, 2024
1fed3cf
fix integration test bug.
msmahdinejad Aug 20, 2024
abce627
Add failed CreateNodeCategory Unit Tests.
msmahdinejad Aug 20, 2024
0b9c73a
Complete CreateNodeCategory class & pass its unit tests.
msmahdinejad Aug 20, 2024
b589689
Edit unit tests name.
msmahdinejad Aug 20, 2024
3ab19ff
Fix some bugs.
msmahdinejad Aug 20, 2024
0e03551
Add failed EdgeCategoryReceiver unit tests.
msmahdinejad Aug 20, 2024
adec0b1
Complete EdgeCategoryReceiver class &
msmahdinejad Aug 20, 2024
102b075
Add NodeCategoryReceiver unit tests.
msmahdinejad Aug 20, 2024
200a727
Complete NodeCategoryReceiver class & pass its unit tests.
msmahdinejad Aug 20, 2024
bc6e96d
Add DI.
msmahdinejad Aug 20, 2024
e573a0e
Add Integration Tests.
msmahdinejad Aug 20, 2024
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
@@ -0,0 +1,167 @@
using System.Net.Http.Headers;
using System.Net.Http.Json;
using System.Text.Json;
using System.Text;
using Microsoft.Extensions.Options;
using Moq;
using Newtonsoft.Json;
using RelationshipAnalysis.Dto;
using RelationshipAnalysis.Dto.Category;
using RelationshipAnalysis.Models.Auth;
using RelationshipAnalysis.Services.UserPanelServices.Abstraction.AuthServices;
using RelationshipAnalysis.Settings.JWT;
using JsonSerializer = System.Text.Json.JsonSerializer;

namespace RelationshipAnalysis.Integration.Test.Controllers;

public class CategoryControllerTests : IClassFixture<CustomWebApplicationFactory<Program>>
{
private readonly HttpClient _client;

public CategoryControllerTests(CustomWebApplicationFactory<Program> factory)
{
_client = factory.CreateClient();
}

[Fact]
public async Task GetAllNodeCategories_ShouldReturnCorrectList_Whenever()
{
// Arrange
var expectedResult1 = "Person";
var expectedResult2 = "Account";
var request = new HttpRequestMessage(HttpMethod.Get, "/api/category/GetAllNodeCategories");
var jwtSettings = new JwtSettings
{
Key = "kajbdiuhdqhpjQE89HBSDJIABFCIWSGF89GW3EJFBWEIUBCZNMXCJNLZDKNJKSNJKFBIGW3EASHHDUIASZGCUI",
ExpireMinutes = 60
};
Mock<IOptions<JwtSettings>> mockJwtSettings = new();
mockJwtSettings.Setup(m => m.Value).Returns(jwtSettings);
var user = new User()
{
Username = "Test",
UserRoles = new List<UserRole>() { new UserRole() { Role = new Role() { Name = "Admin" } } }
};
var token = new JwtTokenGenerator(mockJwtSettings.Object).GenerateJwtToken(user);
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", token);

// Act
var response = await _client.SendAsync(request);

// Assert
response.EnsureSuccessStatusCode();
var responseData = await response.Content.ReadFromJsonAsync<List<string>>();
Assert.NotNull(responseData);
Assert.Contains(expectedResult1, responseData);
Assert.Contains(expectedResult2, responseData);
}

[Fact]
public async Task GetAllEdgeCategories_ShouldReturnCorrectList_Whenever()
{
// Arrange
var expectedResult1 = "Transaction";
var request = new HttpRequestMessage(HttpMethod.Get, "/api/category/GetAllEdgeCategories");
var jwtSettings = new JwtSettings
{
Key = "kajbdiuhdqhpjQE89HBSDJIABFCIWSGF89GW3EJFBWEIUBCZNMXCJNLZDKNJKSNJKFBIGW3EASHHDUIASZGCUI",
ExpireMinutes = 60
};
Mock<IOptions<JwtSettings>> mockJwtSettings = new();
mockJwtSettings.Setup(m => m.Value).Returns(jwtSettings);
var user = new User()
{
Username = "Test",
UserRoles = new List<UserRole>() { new UserRole() { Role = new Role() { Name = "Admin" } } }
};
var token = new JwtTokenGenerator(mockJwtSettings.Object).GenerateJwtToken(user);
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", token);

// Act
var response = await _client.SendAsync(request);

// Assert
response.EnsureSuccessStatusCode();
var responseData = await response.Content.ReadFromJsonAsync<List<string>>();
Assert.NotNull(responseData);
Assert.Contains(expectedResult1, responseData);
}

[Fact]
public async Task CreateNodeCategory_ShouldReturnCorrectList_Whenever()
{
// Arrange
var request = new HttpRequestMessage(HttpMethod.Post, "/api/category/CreateNodeCategory");
var jwtSettings = new JwtSettings
{
Key = "kajbdiuhdqhpjQE89HBSDJIABFCIWSGF89GW3EJFBWEIUBCZNMXCJNLZDKNJKSNJKFBIGW3EASHHDUIASZGCUI",
ExpireMinutes = 60
};
Mock<IOptions<JwtSettings>> mockJwtSettings = new();
mockJwtSettings.Setup(m => m.Value).Returns(jwtSettings);
var user = new User()
{
Username = "Test",
UserRoles = new List<UserRole>() { new UserRole() { Role = new Role() { Name = "Admin" } } }
};
var token = new JwtTokenGenerator(mockJwtSettings.Object).GenerateJwtToken(user);
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", token);

var dto = new CreateNodeCategoryDto()
{
NodeCategoryName = "NotExist"
};
request.Content = new StringContent(
JsonSerializer.Serialize<CreateNodeCategoryDto>(dto),
Encoding.UTF8,
"application/json"
);

// Act
var response = await _client.SendAsync(request);

// Assert
response.EnsureSuccessStatusCode();
var responseData = await response.Content.ReadFromJsonAsync<MessageDto>();
Assert.NotNull(responseData);
}

[Fact]
public async Task CreateEdgeCategory_ShouldReturnCorrectList_Whenever()
{
// Arrange
var request = new HttpRequestMessage(HttpMethod.Post, "/api/category/CreateEdgeCategory");
var jwtSettings = new JwtSettings
{
Key = "kajbdiuhdqhpjQE89HBSDJIABFCIWSGF89GW3EJFBWEIUBCZNMXCJNLZDKNJKSNJKFBIGW3EASHHDUIASZGCUI",
ExpireMinutes = 60
};
Mock<IOptions<JwtSettings>> mockJwtSettings = new();
mockJwtSettings.Setup(m => m.Value).Returns(jwtSettings);
var user = new User()
{
Username = "Test",
UserRoles = new List<UserRole>() { new UserRole() { Role = new Role() { Name = "Admin" } } }
};
var token = new JwtTokenGenerator(mockJwtSettings.Object).GenerateJwtToken(user);
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", token);

var dto = new CreateEdgeCategoryDto()
{
EdgeCategoryName = "NotExist"
};
request.Content = new StringContent(
JsonSerializer.Serialize<CreateEdgeCategoryDto>(dto),
Encoding.UTF8,
"application/json"
);

// Act
var response = await _client.SendAsync(request);

// Assert
response.EnsureSuccessStatusCode();
var responseData = await response.Content.ReadFromJsonAsync<MessageDto>();
Assert.NotNull(responseData);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using RelationshipAnalysis.Context;
using RelationshipAnalysis.Models;
using RelationshipAnalysis.Models.Auth;
using RelationshipAnalysis.Models.Graph;

namespace RelationshipAnalysis.Integration.Test.Controllers;

Expand Down Expand Up @@ -71,5 +72,46 @@ private void SeedDatabase(ApplicationDbContext dbContext)
dbContext.Users.Add(user);
dbContext.Roles.Add(role);
dbContext.SaveChanges();

var nodeCategory1 = new NodeCategory { NodeCategoryName = "Account" };
var nodeCategory2 = new NodeCategory { NodeCategoryName = "Person" };

var node1 = new Node
{
NodeUniqueString = "Node1",
NodeCategory = nodeCategory1,
NodeCategoryId = nodeCategory1.NodeCategoryId
};

var node2 = new Node
{
NodeUniqueString = "Node2",
NodeCategory = nodeCategory2,
NodeCategoryId = nodeCategory2.NodeCategoryId
};

var edgeCategory = new EdgeCategory { EdgeCategoryName = "Transaction"};


dbContext.NodeCategories.Add(nodeCategory1);
dbContext.NodeCategories.Add(nodeCategory2);
dbContext.Nodes.Add(node1);
dbContext.Nodes.Add(node2);
dbContext.EdgeCategories.Add(edgeCategory);


var edge = new Edge
{
EdgeSourceNodeId = node1.NodeId,
EdgeDestinationNodeId = node2.NodeId,
EdgeCategory = edgeCategory,
EdgeCategoryId = edgeCategory.EdgeCategoryId,
EdgeUniqueString = "Edge1"
};

dbContext.Edges.Add(edge);

dbContext.SaveChangesAsync();

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
using System.Net.Http.Headers;
using System.Net.Http.Json;
using Microsoft.Extensions.Options;
using Moq;
using RelationshipAnalysis.Dto.Graph;
using RelationshipAnalysis.Models.Auth;
using RelationshipAnalysis.Models.Graph;
using RelationshipAnalysis.Services.UserPanelServices.Abstraction.AuthServices;
using RelationshipAnalysis.Settings.JWT;

namespace RelationshipAnalysis.Integration.Test.Controllers;

public class GraphControllerTests : IClassFixture<CustomWebApplicationFactory<Program>>
{

private readonly HttpClient _client;
private readonly JwtSettings _jwtSettings;

public GraphControllerTests(CustomWebApplicationFactory<Program> factory)
{
_client = factory.CreateClient();
_jwtSettings = new JwtSettings
{
Key = "kajbdiuhdqhpjQE89HBSDJIABFCIWSGF89GW3EJFBWEIUBCZNMXCJNLZDKNJKSNJKFBIGW3EASHHDUIASZGCUI",
ExpireMinutes = 60
};
}

[Fact]
public async Task GetGraph_ShouldReturnGraph_WhenUserIsAuthorized()
{
// Arrange
var request = new HttpRequestMessage(HttpMethod.Get, "/api/graph/getgraph");
Mock<IOptions<JwtSettings>> mockJwtSettings = new();
mockJwtSettings.Setup(m => m.Value).Returns(_jwtSettings);
var user = new User
{
Id = 1,
Username = "admin",
PasswordHash = "74b2c5bd3a8de69c8c7c643e8b5c49d6552dc636aeb0995aff6f01a1f661a979",
FirstName = "Admin",
LastName = "User",
Email = "[email protected]",
UserRoles = new List<UserRole>() { new UserRole() { Role = new Role() { Name = "admin" } } }

};

var token = new JwtTokenGenerator(mockJwtSettings.Object).GenerateJwtToken(user);
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", token);


var nodeCategory1 = new NodeCategory { NodeCategoryName = "Account" };
var nodeCategory2 = new NodeCategory { NodeCategoryName = "Person" };

var node1 = new Node
{
NodeId = 1,
NodeUniqueString = "Node1",
NodeCategory = nodeCategory1,
NodeCategoryId = nodeCategory1.NodeCategoryId
};

var node2 = new Node
{
NodeId = 2,
NodeUniqueString = "Node2",
NodeCategory = nodeCategory2,
NodeCategoryId = nodeCategory2.NodeCategoryId
};

var edgeCategory = new EdgeCategory { EdgeCategoryName = "Transaction"};



var expectedNodes = new List<NodeDto>()
{
new NodeDto()
{
id = node1.NodeId.ToString(),
label = $"{node1.NodeCategory.NodeCategoryName}/{node1.NodeUniqueString}"
},
new NodeDto()
{
id = node2.NodeId.ToString(),
label = $"{node2.NodeCategory.NodeCategoryName}/{node2.NodeUniqueString}"
}
};

var edge = new Edge
{
EdgeId = 1,
EdgeSourceNodeId = node1.NodeId,
EdgeDestinationNodeId = node2.NodeId,
EdgeCategory = edgeCategory,
EdgeCategoryId = edgeCategory.EdgeCategoryId,
EdgeUniqueString = "Edge1"
};


var expectedEdges = new List<EdgeDto>()
{
new EdgeDto()
{
id = edge.EdgeId.ToString(),
source = node1.NodeId.ToString(),
target = node2.NodeId.ToString()
}
};


// Act
var response = await _client.SendAsync(request);

// Assert
response.EnsureSuccessStatusCode();
var responseData = await response.Content.ReadFromJsonAsync<GraphDto>();
Assert.Equivalent(responseData.nodes, expectedNodes);
Assert.Equivalent(responseData.edges, expectedEdges);

}
}
Loading