diff --git a/mohaymen-codestar-Team02/Controllers/AdminController.cs b/mohaymen-codestar-Team02/Controllers/AdminController.cs index 2e122ba..2ad80c2 100644 --- a/mohaymen-codestar-Team02/Controllers/AdminController.cs +++ b/mohaymen-codestar-Team02/Controllers/AdminController.cs @@ -21,7 +21,7 @@ public AdminController(IAdminService adminService) } [HttpGet("users")] - public async Task GetAllUsers([FromBody] int pageNumber) + public async Task GetAllUsers([FromQuery] int pageNumber) { var response = await _adminService.GetUsersPaginated(pageNumber); diff --git a/mohaymen-codestar-Team02/Controllers/AnalystController.cs b/mohaymen-codestar-Team02/Controllers/AnalystController.cs index 80247ed..919bf7d 100644 --- a/mohaymen-codestar-Team02/Controllers/AnalystController.cs +++ b/mohaymen-codestar-Team02/Controllers/AnalystController.cs @@ -1,6 +1,9 @@ using Microsoft.AspNetCore.Mvc; using mohaymen_codestar_Team02.Dto; +using mohaymen_codestar_Team02.Dto.GraphDTO; +using mohaymen_codestar_Team02.Models; using mohaymen_codestar_Team02.Services.AnalystService; +using mohaymen_codestar_Team02.Services.DataAdminService; namespace mohaymen_codestar_Team02.Controllers; @@ -8,7 +11,7 @@ public class AnalystController : ControllerBase { private readonly IAnalystService AnalystService; - public AnalystController(IAnalystService analystService) + public AnalystController(IAnalystService analystService, IDataAdminService dataAdminService) { AnalystService = analystService; } @@ -26,4 +29,28 @@ public async Task ExpandVertex([FromQuery] GraphQueryInfoDto grap var Response = await AnalystService.GetTheVertexNeighbor(graphQueryInfoDto, vertexId); return StatusCode((int)Response.Type, Response); } + + [HttpPost("Analyst")] + public async Task DisplayDataSetAsGraph([FromBody]FilterGraphDto filterGraphDto) + { + ServiceResponse response = + await AnalystService.DisplayGeraphData(filterGraphDto.DatasetId, filterGraphDto.SourceIdentifier, + filterGraphDto.TargetIdentifier, filterGraphDto.VertexIdentifier, filterGraphDto.VertexAttributeValues, filterGraphDto.EdgeAttributeValues); + response.Data.GraphId = filterGraphDto.DatasetId; + return StatusCode((int)response.Type, response); + } + + [HttpGet("Analyst/Vertex/{id}")] + public async Task DisplayVertexAttributes(long id) + { + var response = AnalystService.GetVertexAttributes(id); + return StatusCode((int)response.Type, response); + } + + [HttpGet("Analyst/Edge/{id}")] + public async Task DisplayEdgeAttributes(long id) + { + var response = AnalystService.GetEdgeAttributes(id); + return StatusCode((int)response.Type, response); + } } \ No newline at end of file diff --git a/mohaymen-codestar-Team02/Controllers/DataAdminController.cs b/mohaymen-codestar-Team02/Controllers/DataAdminController.cs index 04618e4..7125b44 100644 --- a/mohaymen-codestar-Team02/Controllers/DataAdminController.cs +++ b/mohaymen-codestar-Team02/Controllers/DataAdminController.cs @@ -1,5 +1,6 @@ using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; +using mohaymen_codestar_Team02.Dto; using mohaymen_codestar_Team02.Dto.GraphDTO; using mohaymen_codestar_Team02.Dto.StoreDataDto; using mohaymen_codestar_Team02.Models; @@ -14,7 +15,7 @@ public class DataAdminController : ControllerBase { private readonly IDataAdminService _dataAdminService; private readonly IFileReader _fileReader; - + public DataAdminController(IDataAdminService dataAdminService, IFileReader fileReader) { @@ -46,25 +47,23 @@ public IActionResult GetDataSetsList() var response = _dataAdminService.DisplayDataSet(); return StatusCode((int)response.Type, response); } - - [HttpGet("DataSets/{dataSetId}")] - public async Task DisplayDataSetAsGraph(long dataSetId, [FromQuery] string vertexIdentifier, - [FromQuery] string sourceIdentifier, [FromQuery] string targetIdentifier) + + [HttpPost("DataSets/Graph")] + public async Task DisplayDataSetAsGraph(GetGraphDto getGraphDto) { ServiceResponse response = - await _dataAdminService.DisplayGeraphData(dataSetId, sourceIdentifier, - targetIdentifier, vertexIdentifier); - response.Data.GraphId = dataSetId; + await _dataAdminService.DisplayGeraphData(getGraphDto.DatasetId, getGraphDto.SourceIdentifier, + getGraphDto.TargetIdentifier, getGraphDto.VertexIdentifier); + response.Data.GraphId = getGraphDto.DatasetId; return StatusCode((int)response.Type, response); } - [HttpGet("DataSets/Vertices/{objectId}")] public async Task DisplayVertexDetails(string objectId) { var respond = _dataAdminService.GetVertexDetail(objectId); return StatusCode((int)respond.Type, respond); - } + } [HttpGet("DataSets/Edges/{objectId}")] public async Task DisplayEdgeDetails(string objectId) @@ -72,18 +71,4 @@ public async Task DisplayEdgeDetails(string objectId) var respond = _dataAdminService.GetEdgeDetail(objectId); return StatusCode((int)respond.Type, respond); } - - [HttpGet("DataSets/Vertex/{id}")] - public async Task DisplayVertexAttributes(long id) - { - var response = _dataAdminService.GetVertexAttributes(id); - return StatusCode((int)response.Type, response); - } - - [HttpGet("DataSets/Edge/{id}")] - public async Task DisplayEdgeAttributes(long id) - { - var response = _dataAdminService.GetEdgeAttributes(id); - return StatusCode((int)response.Type, response); - } } \ No newline at end of file diff --git a/mohaymen-codestar-Team02/Data/DataContext.cs b/mohaymen-codestar-Team02/Data/DataContext.cs index 245b3ec..8cda218 100644 --- a/mohaymen-codestar-Team02/Data/DataContext.cs +++ b/mohaymen-codestar-Team02/Data/DataContext.cs @@ -11,7 +11,7 @@ public DataContext(DbContextOptions options) : base(options) { } - public DbSet Users { get; set; } + public DbSet Users { get; set; } public DbSet Roles { get; set; } public DbSet UserRoles { get; set; } public DbSet VertexEntities { get; set; } diff --git a/mohaymen-codestar-Team02/Dto/GraphDTO/DetailDto.cs b/mohaymen-codestar-Team02/Dto/GraphDto/DetailDto.cs similarity index 100% rename from mohaymen-codestar-Team02/Dto/GraphDTO/DetailDto.cs rename to mohaymen-codestar-Team02/Dto/GraphDto/DetailDto.cs diff --git a/mohaymen-codestar-Team02/Dto/GraphDTO/DisplayGraphDto.cs b/mohaymen-codestar-Team02/Dto/GraphDto/DisplayGraphDto.cs similarity index 100% rename from mohaymen-codestar-Team02/Dto/GraphDTO/DisplayGraphDto.cs rename to mohaymen-codestar-Team02/Dto/GraphDto/DisplayGraphDto.cs diff --git a/mohaymen-codestar-Team02/Dto/GraphDto/FilterGraphDto.cs b/mohaymen-codestar-Team02/Dto/GraphDto/FilterGraphDto.cs new file mode 100644 index 0000000..ef40152 --- /dev/null +++ b/mohaymen-codestar-Team02/Dto/GraphDto/FilterGraphDto.cs @@ -0,0 +1,11 @@ +namespace mohaymen_codestar_Team02.Dto; + +public class FilterGraphDto +{ + public long DatasetId { get; set; } + public string SourceIdentifier { get; set; } + public string TargetIdentifier { get; set; } + public string VertexIdentifier { get; set; } + public Dictionary VertexAttributeValues { get; set; } + public Dictionary EdgeAttributeValues { get; set; } +} \ No newline at end of file diff --git a/mohaymen-codestar-Team02/Dto/GraphDto/GetGraphDto.cs b/mohaymen-codestar-Team02/Dto/GraphDto/GetGraphDto.cs new file mode 100644 index 0000000..ff7e47c --- /dev/null +++ b/mohaymen-codestar-Team02/Dto/GraphDto/GetGraphDto.cs @@ -0,0 +1,9 @@ +namespace mohaymen_codestar_Team02.Dto; + +public class GetGraphDto +{ + public long DatasetId { get; set; } + public string SourceIdentifier { get; set; } + public string TargetIdentifier { get; set; } + public string VertexIdentifier { get; set; } +} \ No newline at end of file diff --git a/mohaymen-codestar-Team02/Dto/Permission/GetPermissionDto.cs b/mohaymen-codestar-Team02/Dto/PermissionDto/GetPermissionDto.cs similarity index 100% rename from mohaymen-codestar-Team02/Dto/Permission/GetPermissionDto.cs rename to mohaymen-codestar-Team02/Dto/PermissionDto/GetPermissionDto.cs diff --git a/mohaymen-codestar-Team02/Dto/Role/GetRoleDto.cs b/mohaymen-codestar-Team02/Dto/RoleDto/GetRoleDto.cs similarity index 100% rename from mohaymen-codestar-Team02/Dto/Role/GetRoleDto.cs rename to mohaymen-codestar-Team02/Dto/RoleDto/GetRoleDto.cs diff --git a/mohaymen-codestar-Team02/Dto/User/ChangePasswordUserDto.cs b/mohaymen-codestar-Team02/Dto/UserDto/ChangePasswordUserDto.cs similarity index 100% rename from mohaymen-codestar-Team02/Dto/User/ChangePasswordUserDto.cs rename to mohaymen-codestar-Team02/Dto/UserDto/ChangePasswordUserDto.cs diff --git a/mohaymen-codestar-Team02/Dto/User/CreateUserDto.cs b/mohaymen-codestar-Team02/Dto/UserDto/CreateUserDto.cs similarity index 100% rename from mohaymen-codestar-Team02/Dto/User/CreateUserDto.cs rename to mohaymen-codestar-Team02/Dto/UserDto/CreateUserDto.cs diff --git a/mohaymen-codestar-Team02/Dto/User/DeleteUserDto.cs b/mohaymen-codestar-Team02/Dto/UserDto/DeleteUserDto.cs similarity index 100% rename from mohaymen-codestar-Team02/Dto/User/DeleteUserDto.cs rename to mohaymen-codestar-Team02/Dto/UserDto/DeleteUserDto.cs diff --git a/mohaymen-codestar-Team02/Dto/User/GetUserDto.cs b/mohaymen-codestar-Team02/Dto/UserDto/GetUserDto.cs similarity index 100% rename from mohaymen-codestar-Team02/Dto/User/GetUserDto.cs rename to mohaymen-codestar-Team02/Dto/UserDto/GetUserDto.cs diff --git a/mohaymen-codestar-Team02/Dto/User/LoginUserDto.cs b/mohaymen-codestar-Team02/Dto/UserDto/LoginUserDto.cs similarity index 100% rename from mohaymen-codestar-Team02/Dto/User/LoginUserDto.cs rename to mohaymen-codestar-Team02/Dto/UserDto/LoginUserDto.cs diff --git a/mohaymen-codestar-Team02/Dto/User/UpdateUserDto.cs b/mohaymen-codestar-Team02/Dto/UserDto/UpdateUserDto.cs similarity index 100% rename from mohaymen-codestar-Team02/Dto/User/UpdateUserDto.cs rename to mohaymen-codestar-Team02/Dto/UserDto/UpdateUserDto.cs diff --git a/mohaymen-codestar-Team02/Dto/UserRole/AddUserRoleDto.cs b/mohaymen-codestar-Team02/Dto/UserRoleDto/AddUserRoleDto.cs similarity index 100% rename from mohaymen-codestar-Team02/Dto/UserRole/AddUserRoleDto.cs rename to mohaymen-codestar-Team02/Dto/UserRoleDto/AddUserRoleDto.cs diff --git a/mohaymen-codestar-Team02/Dto/UserRole/DeleteUserRoleDto.cs b/mohaymen-codestar-Team02/Dto/UserRoleDto/DeleteUserRoleDto.cs similarity index 100% rename from mohaymen-codestar-Team02/Dto/UserRole/DeleteUserRoleDto.cs rename to mohaymen-codestar-Team02/Dto/UserRoleDto/DeleteUserRoleDto.cs diff --git a/mohaymen-codestar-Team02/Models/Auth/UserRole.cs b/mohaymen-codestar-Team02/Models/Auth/UserRole.cs index 443455c..5edc3a9 100644 --- a/mohaymen-codestar-Team02/Models/Auth/UserRole.cs +++ b/mohaymen-codestar-Team02/Models/Auth/UserRole.cs @@ -6,8 +6,8 @@ public class UserRole { public long UserId { get; set; } - [ForeignKey("UserId")] public virtual User User { get; set; } + [ForeignKey("UserId")] public virtual User? User { get; set; } public long RoleId { get; set; } - [ForeignKey("RoleId")] public virtual Role Role { get; set; } + [ForeignKey("RoleId")] public virtual Role? Role { get; set; } } \ No newline at end of file diff --git a/mohaymen-codestar-Team02/Models/Graph/Edge.cs b/mohaymen-codestar-Team02/Models/Graph/Edge.cs index f12d88d..d313a7a 100644 --- a/mohaymen-codestar-Team02/Models/Graph/Edge.cs +++ b/mohaymen-codestar-Team02/Models/Graph/Edge.cs @@ -5,6 +5,6 @@ namespace mohaymen_codestar_Team02.Models; public class Edge : IEdge { public string? Id { get; init; } - public string? Source { get; init; } - public string? Target { get; init; } + public string? Source { get; set; } + public string? Target { get; set; } } \ No newline at end of file diff --git a/mohaymen-codestar-Team02/Models/newModel/Tables.cs b/mohaymen-codestar-Team02/Models/newModel/Tables.cs deleted file mode 100644 index ac2fa11..0000000 --- a/mohaymen-codestar-Team02/Models/newModel/Tables.cs +++ /dev/null @@ -1,9 +0,0 @@ -using System.ComponentModel.DataAnnotations; - -namespace mohaymen_codestar_Team02.Models.newModel; - -public class Tables -{ - [Key] public long Id { get; set; } - public string Name { get; set; } -} \ No newline at end of file diff --git a/mohaymen-codestar-Team02/Program.cs b/mohaymen-codestar-Team02/Program.cs index b00721c..36d2da1 100644 --- a/mohaymen-codestar-Team02/Program.cs +++ b/mohaymen-codestar-Team02/Program.cs @@ -10,7 +10,7 @@ options.AddPolicy("AllowSpecificOrigins", builder => { - builder.WithOrigins("http://localhost:4200") + builder.WithOrigins("http://localhost:4200", "https://codestar-g2.abriment.com/") .AllowAnyHeader() .AllowAnyMethod() .AllowCredentials(); diff --git a/mohaymen-codestar-Team02/Services/Administration/Abstraction/IAdminService.cs b/mohaymen-codestar-Team02/Services/Administration/Abstraction/IAdminService.cs index fe9538c..7662363 100644 --- a/mohaymen-codestar-Team02/Services/Administration/Abstraction/IAdminService.cs +++ b/mohaymen-codestar-Team02/Services/Administration/Abstraction/IAdminService.cs @@ -8,10 +8,10 @@ public interface IAdminService { Task?>> GetUsersPaginated(int pageNumber); Task> GetUserByUsername(string? username); - Task> CreateUser(User user, string password, List roles); - Task> DeleteUser(User user); - Task> UpdateUser(User user); + Task> CreateUser(User? user, string password, List roles); + Task> DeleteUser(User? user); + Task> UpdateUser(User? user); Task>> GetAllRoles(); - Task> AddRole(User user, Role role); - Task> DeleteRole(User user, Role role); + Task> AddRole(User? user, Role? role); + Task> DeleteRole(User? user, Role? role); } \ No newline at end of file diff --git a/mohaymen-codestar-Team02/Services/Administration/AdminService.cs b/mohaymen-codestar-Team02/Services/Administration/AdminService.cs index 557f209..b6b446f 100644 --- a/mohaymen-codestar-Team02/Services/Administration/AdminService.cs +++ b/mohaymen-codestar-Team02/Services/Administration/AdminService.cs @@ -85,7 +85,7 @@ public async Task>> GetAllRoles() return new ServiceResponse>(roles, ApiResponseType.Success, Resources.UsersRetrievedMassage); } - public async Task> CreateUser(User user, string password, List roles) + public async Task> CreateUser(User? user, string password, List roles) { using var scope = _serviceProvider.CreateScope(); var context = scope.ServiceProvider.GetRequiredService(); @@ -136,7 +136,7 @@ private bool IsRoleMatching(List roles, DataContext context) return matchingRoles; } - public async Task> DeleteUser(User user) + public async Task> DeleteUser(User? user) { using var scope = _serviceProvider.CreateScope(); var context = scope.ServiceProvider.GetRequiredService(); @@ -166,7 +166,7 @@ private bool IsRoleMatching(List roles, DataContext context) Resources.UserDeletionSuccessfulMessage); } - public async Task> UpdateUser(User user) + public async Task> UpdateUser(User? user) { using var scope = _serviceProvider.CreateScope(); var context = scope.ServiceProvider.GetRequiredService(); @@ -196,7 +196,7 @@ private bool IsRoleMatching(List roles, DataContext context) Resources.UserUpdateSuccessfulyMessage); } - public async Task> AddRole(User user, Role role) + public async Task> AddRole(User? user, Role? role) { using var scope = _serviceProvider.CreateScope(); var context = scope.ServiceProvider.GetRequiredService(); @@ -237,7 +237,7 @@ private bool IsRoleMatching(List roles, DataContext context) Resources.RoleAddedSuccessfulyMassage); } - public async Task> DeleteRole(User user, Role role) + public async Task> DeleteRole(User? user, Role? role) { using var scope = _serviceProvider.CreateScope(); var context = scope.ServiceProvider.GetRequiredService(); diff --git a/mohaymen-codestar-Team02/Services/AnalystService/AnalystService.cs b/mohaymen-codestar-Team02/Services/AnalystService/AnalystService.cs index 26908df..3b7e040 100644 --- a/mohaymen-codestar-Team02/Services/AnalystService/AnalystService.cs +++ b/mohaymen-codestar-Team02/Services/AnalystService/AnalystService.cs @@ -13,10 +13,16 @@ namespace mohaymen_codestar_Team02.Services.AnalystService; public class AnalystService : IAnalystService { private readonly IServiceProvider _serviceProvider; + private readonly IVertexService _vertexService; + private readonly IEdgeService _edgeService; + private readonly IGraphService _graphService; - public AnalystService(IServiceProvider serviceProvider) + public AnalystService(IServiceProvider serviceProvider, IVertexService vertexService, IEdgeService edgeService, IGraphService graphService) { _serviceProvider = serviceProvider; + _vertexService = vertexService; + _edgeService = edgeService; + _graphService = graphService; } private DataGroup JoinTheEdgeTable(IQueryable validDataSetTable, GraphQueryInfoDto graphQueryInfoDto) @@ -130,7 +136,7 @@ public async Task> GetTheVertexNeighbor(GraphQu validVertexLabel.Add(source.StringValue); validVertexLabel.Add(target.StringValue); validEdges.Add(new Edge() - { Id = edgeValue.Id, Source = source.ObjectId, Target = target.ObjectId }); + { Id = edgeValue.Id, Source = source.ObjectId, Target = target.ObjectId }); } } } @@ -151,10 +157,42 @@ public async Task> GetTheVertexNeighbor(GraphQu Console.WriteLine("finish the creating of vertex"); var responseData = new DisplayGraphDto() - { GraphId = graphQueryInfoDto.datasetId, Edges = validEdges, Vertices = resultVertex }; + { GraphId = graphQueryInfoDto.datasetId, Edges = validEdges, Vertices = resultVertex }; time.Stop(); Console.WriteLine("@@@@@@@@@@@@@@@@@@@@@@@@@@@@ that take (" + time.ElapsedMilliseconds + ") milieSec to find the VertexNeighbor @@@@@@@@@@@@@@@@@@@@@@@@@@@@"); return new ServiceResponse(responseData, ApiResponseType.Success, string.Empty); } + + public async Task> DisplayGeraphData(long databaseId, + string sourceEdgeIdentifierFieldName, + string destinationEdgeIdentifierFieldName, string vertexIdentifierFieldName, Dictionary vertexAttributeValus, Dictionary edgeAttributeValues) + { + var vertices = _vertexService.GetAllVertices(databaseId, vertexIdentifierFieldName, vertexAttributeValus); + var edges = _edgeService.GetAllEdges(databaseId, sourceEdgeIdentifierFieldName, + destinationEdgeIdentifierFieldName, edgeAttributeValues); + var graph = _graphService.GetGraph(vertices, edges, vertexIdentifierFieldName, sourceEdgeIdentifierFieldName, + destinationEdgeIdentifierFieldName); + + var dto = new DisplayGraphDto() + { + Vertices = graph.vertices, + Edges = graph.edges + }; + return new ServiceResponse(dto, ApiResponseType.Success, + Resources.GraphFetchedSuccessfullyMessage); + } + + public ServiceResponse> GetVertexAttributes(long vertexEntityId) + { + var att = _vertexService.GetVertexAttributes(vertexEntityId); + return new ServiceResponse>(att, ApiResponseType.Success, ""); + } + + public ServiceResponse> GetEdgeAttributes(long edgeEntityId) + { + var att = _edgeService.GetEdgeAttributes(edgeEntityId); + return new ServiceResponse>(att, ApiResponseType.Success, ""); + } + } \ No newline at end of file diff --git a/mohaymen-codestar-Team02/Services/AnalystService/IAnalystService.cs b/mohaymen-codestar-Team02/Services/AnalystService/IAnalystService.cs index c6c0a82..7a69b48 100644 --- a/mohaymen-codestar-Team02/Services/AnalystService/IAnalystService.cs +++ b/mohaymen-codestar-Team02/Services/AnalystService/IAnalystService.cs @@ -7,4 +7,13 @@ namespace mohaymen_codestar_Team02.Services.AnalystService; public interface IAnalystService { Task> GetTheVertexNeighbor(GraphQueryInfoDto graphQueryInfoDto, string vertexId); + + Task> DisplayGeraphData(long databaseId, + string sourceEdgeIdentifierFieldName, + string destinationEdgeIdentifierFieldName, string vertexIdentifierFieldName, + Dictionary vertexAttributeValus, Dictionary edgeAttributeValues); + + ServiceResponse> GetVertexAttributes(long vertexEntityId); + + ServiceResponse> GetEdgeAttributes(long edgeEntityId); } \ No newline at end of file diff --git a/mohaymen-codestar-Team02/Services/DataAdminService/Abstraction/IDataAdminService.cs b/mohaymen-codestar-Team02/Services/DataAdminService/Abstraction/IDataAdminService.cs index 370f4f8..cfeb5fb 100644 --- a/mohaymen-codestar-Team02/Services/DataAdminService/Abstraction/IDataAdminService.cs +++ b/mohaymen-codestar-Team02/Services/DataAdminService/Abstraction/IDataAdminService.cs @@ -16,8 +16,4 @@ Task> DisplayGeraphData(long dataSetId, string ServiceResponse GetVertexDetail(string objectId); ServiceResponse GetEdgeDetail(string objectId); - - ServiceResponse> GetVertexAttributes(long vertexEntityId); - - ServiceResponse> GetEdgeAttributes(long edgeEntityId); } \ No newline at end of file diff --git a/mohaymen-codestar-Team02/Services/DataAdminService/DataAdminService.cs b/mohaymen-codestar-Team02/Services/DataAdminService/DataAdminService.cs index 6c16d09..caed92b 100644 --- a/mohaymen-codestar-Team02/Services/DataAdminService/DataAdminService.cs +++ b/mohaymen-codestar-Team02/Services/DataAdminService/DataAdminService.cs @@ -93,13 +93,15 @@ public ServiceResponse> DisplayDataSet() return new ServiceResponse>(dataGroupDtos, ApiResponseType.Success, ""); } - public async Task> DisplayGeraphData(long databaseName, + public async Task> DisplayGeraphData(long databaseId, string sourceEdgeIdentifierFieldName, string destinationEdgeIdentifierFieldName, string vertexIdentifierFieldName) { - var graph = _graphService.GetGraph(databaseName, sourceEdgeIdentifierFieldName, - destinationEdgeIdentifierFieldName, - vertexIdentifierFieldName); + var vertices = _vertexService.GetAllVertices(databaseId, vertexIdentifierFieldName, new Dictionary(){}); + var edges = _edgeService.GetAllEdges(databaseId, sourceEdgeIdentifierFieldName, + destinationEdgeIdentifierFieldName, new Dictionary(){}); + var graph = _graphService.GetGraph(vertices, edges, vertexIdentifierFieldName, sourceEdgeIdentifierFieldName, + destinationEdgeIdentifierFieldName); var dto = new DisplayGraphDto() { @@ -121,16 +123,4 @@ public ServiceResponse GetEdgeDetail(string objectId) return new ServiceResponse(_edgeService.GetEdgeDetails(objectId), ApiResponseType.Success, string.Empty); } - - public ServiceResponse> GetVertexAttributes(long vertexEntityId) - { - var att = _vertexService.GetVertexAttributes(vertexEntityId); - return new ServiceResponse>(att, ApiResponseType.Success, ""); - } - - public ServiceResponse> GetEdgeAttributes(long edgeEntityId) - { - var att = _edgeService.GetEdgeAttributes(edgeEntityId); - return new ServiceResponse>(att, ApiResponseType.Success, ""); - } } \ No newline at end of file diff --git a/mohaymen-codestar-Team02/Services/EdgeService.cs b/mohaymen-codestar-Team02/Services/EdgeService.cs deleted file mode 100644 index ccdf5ca..0000000 --- a/mohaymen-codestar-Team02/Services/EdgeService.cs +++ /dev/null @@ -1,136 +0,0 @@ -using System.Linq; -using AutoMapper; -using Microsoft.EntityFrameworkCore; -using mohaymen_codestar_Team02.Data; -using mohaymen_codestar_Team02.Dto; -using mohaymen_codestar_Team02.Dto.GraphDTO; -using mohaymen_codestar_Team02.Models; -using mohaymen_codestar_Team02.Models.EdgeEAV; -using mohaymen_codestar_Team02.Models.VertexEAV; -using QuikGraph; - -namespace mohaymen_codestar_Team02.Services; - -public class EdgeService : IEdgeService -{ - private readonly IServiceProvider _serviceProvider; - private readonly IMapper _mapper; - - public EdgeService(IServiceProvider serviceProvider, IMapper mapper) - { - _serviceProvider = serviceProvider; - _mapper = mapper; - } - - - public List GetEdgeAttributes(long edgeEntityId) - { - var scope = _serviceProvider.CreateScope(); - var context = scope.ServiceProvider.GetRequiredService(); - - var edgeAttribuite = context.EdgeEntities.Include(ve => ve.EdgeAttributes) - .FirstOrDefault(ve => ve.EdgeEntityId == edgeEntityId) - ?.EdgeAttributes; - - return edgeAttribuite.Select(va => _mapper.Map(va)).ToList(); - } - - public List GetAllEdges(long dataSetId, string vertexIdentifierFieldName, - string sourceEdgeIdentifierFieldName, - string destinationEdgeIdentifierFieldName) - { - var scope = _serviceProvider.CreateScope(); - var context = scope.ServiceProvider.GetRequiredService(); - var vertexSet = context.DataSets.Where(ds => ds.DataGroupId == dataSetId).Include(ds => ds.VertexEntity) - .ThenInclude(ve => ve.VertexAttributes).ThenInclude(vv => vv.VertexValues).FirstOrDefault(); - var edgeSet = context.DataSets.Where(ds => ds.DataGroupId == dataSetId).Include(ds => ds.EdgeEntity) - .ThenInclude(ee => ee.EdgeAttributes).ThenInclude(ev => ev.EdgeValues).FirstOrDefault(); - ; - - var vertexRecords = vertexSet.VertexEntity.VertexAttributes.Select(a => a.VertexValues).SelectMany(v => v) - .GroupBy(v => v.ObjectId); - - var edgeRecords = edgeSet.EdgeEntity.EdgeAttributes.Select(ea => ea.EdgeValues).SelectMany(v => v) - .GroupBy(v => v.ObjectId); - - List edges = new(); - foreach (var record in edgeRecords) - { - GetSourceAndDerstinationValues(sourceEdgeIdentifierFieldName, destinationEdgeIdentifierFieldName, record, - out var sourceValue, out var destinationValue); - - GetSourcesAndDestinations(vertexIdentifierFieldName, vertexRecords, sourceValue, destinationValue, - out var sources, out var destinations); - - foreach (var source in sources) - foreach (var des in destinations) - { - var edge = new Edge() - { - Id = record.Key, - Source = source.Id, - Target = des.Id - }; - edges.Add(edge); - } - } - - return edges; - } - - private void GetSourcesAndDestinations(string vertexIdentifierFieldName, - IEnumerable> vertexRecords, - string sourceValue, string destinationValue, out List sources, out List destinations) - { - sources = new List(); - destinations = new List(); - - foreach (var record1 in vertexRecords) - foreach (var item in record1) - { - if (item.VertexAttribute.Name == vertexIdentifierFieldName && item.StringValue == sourceValue) - { - var vertex = new Vertex() - { - Id = record1.Key - }; - sources.Add(vertex); - } - - if (item.VertexAttribute.Name == vertexIdentifierFieldName && item.StringValue == destinationValue) - { - var vertex = new Vertex() - { - Id = record1.Key - }; - destinations.Add(vertex); - } - } - } - - private void GetSourceAndDerstinationValues(string sourceEdgeIdentifierFieldName, - string destinationEdgeIdentifierFieldName, IGrouping record, out string sourceValue, - out string destinationValue) - { - sourceValue = string.Empty; - destinationValue = string.Empty; - foreach (var item in record) - { - if (item.EdgeAttribute.Name == sourceEdgeIdentifierFieldName) sourceValue = item.StringValue; - - if (item.EdgeAttribute.Name == destinationEdgeIdentifierFieldName) destinationValue = item.StringValue; - } - } - - public DetailDto GetEdgeDetails(string objId) - { - using var scope = _serviceProvider.CreateScope(); - var context = scope.ServiceProvider.GetRequiredService(); - var validValue = context.EdgeValues.Where(value => value.ObjectId.ToLower() == objId.ToLower()).ToList(); - var result = new DetailDto(); - foreach (var value in validValue) - result.AttributeValue[context.EdgeAttributes.Find(value.EdgeAttributeId).Name] = value.StringValue; - - return result; - } -} \ No newline at end of file diff --git a/mohaymen-codestar-Team02/Services/IEdgeService.cs b/mohaymen-codestar-Team02/Services/EdgeService/Abstraction/IEdgeService.cs similarity index 67% rename from mohaymen-codestar-Team02/Services/IEdgeService.cs rename to mohaymen-codestar-Team02/Services/EdgeService/Abstraction/IEdgeService.cs index f83e735..f727f34 100644 --- a/mohaymen-codestar-Team02/Services/IEdgeService.cs +++ b/mohaymen-codestar-Team02/Services/EdgeService/Abstraction/IEdgeService.cs @@ -7,9 +7,9 @@ namespace mohaymen_codestar_Team02.Services; public interface IEdgeService { - public List GetAllEdges(long dataSetId, string vertexIdentifierFieldName, + public Dictionary> GetAllEdges(long dataSetId, string sourceEdgeIdentifierFieldName, - string destinationEdgeIdentifierFieldName); + string destinationEdgeIdentifierFieldName, Dictionary edgeAttributeVales); DetailDto GetEdgeDetails(string objId); public List GetEdgeAttributes(long edgeEntityId); diff --git a/mohaymen-codestar-Team02/Services/EdgeService/EdgeService.cs b/mohaymen-codestar-Team02/Services/EdgeService/EdgeService.cs new file mode 100644 index 0000000..57f0c2b --- /dev/null +++ b/mohaymen-codestar-Team02/Services/EdgeService/EdgeService.cs @@ -0,0 +1,66 @@ +using AutoMapper; +using Microsoft.EntityFrameworkCore; +using mohaymen_codestar_Team02.Data; +using mohaymen_codestar_Team02.Dto; +using mohaymen_codestar_Team02.Dto.GraphDTO; + +namespace mohaymen_codestar_Team02.Services; + +public class EdgeService : IEdgeService +{ + private readonly IServiceProvider _serviceProvider; + private readonly IMapper _mapper; + + public EdgeService(IServiceProvider serviceProvider, IMapper mapper) + { + _serviceProvider = serviceProvider; + _mapper = mapper; + } + + public List GetEdgeAttributes(long edgeEntityId) + { + var scope = _serviceProvider.CreateScope(); + var context = scope.ServiceProvider.GetRequiredService(); + + var edgeAttribuite = context.EdgeEntities.Include(ve => ve.EdgeAttributes) + .FirstOrDefault(ve => ve.EdgeEntityId == edgeEntityId) + ?.EdgeAttributes; + + return edgeAttribuite.Select(va => _mapper.Map(va)).ToList(); + } + + public Dictionary> GetAllEdges(long dataSetId, + string sourceEdgeIdentifierFieldName, + string destinationEdgeIdentifierFieldName, Dictionary edgeAttributeVales) + { + var scope = _serviceProvider.CreateScope(); + var context = scope.ServiceProvider.GetRequiredService(); + + var edgeSet = context.DataSets.Where(ds => ds.DataGroupId == dataSetId).Include(ds => ds.EdgeEntity) + .ThenInclude(ee => ee.EdgeAttributes).ThenInclude(ev => ev.EdgeValues).FirstOrDefault(); + + var edgeRecords = edgeSet.EdgeEntity.EdgeAttributes.Select(ea => ea.EdgeValues).SelectMany(v => v) + .GroupBy(v => v.ObjectId); + + var validEdgeRecords = edgeRecords + .Where(group => + edgeAttributeVales.All(attr => + group.Any(v => v.EdgeAttribute.Name == attr.Key && v.StringValue == attr.Value))); + + var res = validEdgeRecords.ToDictionary(x => x.Key, + x => x.ToDictionary(g => g.EdgeAttribute.Name, g => g.StringValue)); + return res; + } + + public DetailDto GetEdgeDetails(string objId) + { + using var scope = _serviceProvider.CreateScope(); + var context = scope.ServiceProvider.GetRequiredService(); + var validValue = context.EdgeValues.Where(value => value.ObjectId.ToLower() == objId.ToLower()).ToList(); + var result = new DetailDto(); + foreach (var value in validValue) + result.AttributeValue[context.EdgeAttributes.Find(value.EdgeAttributeId).Name] = value.StringValue; + + return result; + } +} \ No newline at end of file diff --git a/mohaymen-codestar-Team02/Services/GraphService.cs b/mohaymen-codestar-Team02/Services/GraphService.cs deleted file mode 100644 index 11ee6f9..0000000 --- a/mohaymen-codestar-Team02/Services/GraphService.cs +++ /dev/null @@ -1,25 +0,0 @@ -using mohaymen_codestar_Team02.Models; -using QuikGraph; - -namespace mohaymen_codestar_Team02.Services; - -public class GraphService : IGraphService -{ - private readonly IVertexService _vertexService; - private readonly IEdgeService _edgeService; - - public GraphService(IVertexService vertexService, IEdgeService edgeService) - { - _vertexService = vertexService; - _edgeService = edgeService; - } - - public (List vertices, List edges) GetGraph(long dataSetID, string sourceEdgeIdentifierFieldName, - string destinationEdgeIdentifierFieldName, string vertexIdentifierFieldName) - { - var vertices = _vertexService.GetAllVertices(dataSetID, vertexIdentifierFieldName); - var edges = _edgeService.GetAllEdges(dataSetID, vertexIdentifierFieldName, sourceEdgeIdentifierFieldName, - destinationEdgeIdentifierFieldName); - return (vertices, edges); - } -} \ No newline at end of file diff --git a/mohaymen-codestar-Team02/Services/GraphService/Abstraction/IGraphService.cs b/mohaymen-codestar-Team02/Services/GraphService/Abstraction/IGraphService.cs new file mode 100644 index 0000000..19f3040 --- /dev/null +++ b/mohaymen-codestar-Team02/Services/GraphService/Abstraction/IGraphService.cs @@ -0,0 +1,11 @@ +using mohaymen_codestar_Team02.Models; +using QuikGraph; + +namespace mohaymen_codestar_Team02.Services; + +public interface IGraphService +{ + public (List vertices, List edges) GetGraph(Dictionary> vertices, + Dictionary> edges, string vertexIdentifierFieldName, + string SourceIdentifierFieldName, string TargetIdentifierFieldName); +} \ No newline at end of file diff --git a/mohaymen-codestar-Team02/Services/GraphService/GraphService.cs b/mohaymen-codestar-Team02/Services/GraphService/GraphService.cs new file mode 100644 index 0000000..69a1574 --- /dev/null +++ b/mohaymen-codestar-Team02/Services/GraphService/GraphService.cs @@ -0,0 +1,126 @@ +using System.Linq; +using mohaymen_codestar_Team02.Data; +using mohaymen_codestar_Team02.Models; +using mohaymen_codestar_Team02.Models.EdgeEAV; +using mohaymen_codestar_Team02.Models.VertexEAV; +using QuikGraph; + +namespace mohaymen_codestar_Team02.Services; + +public class GraphService : IGraphService +{ + private readonly IVertexService _vertexService; + private readonly IEdgeService _edgeService; + + public GraphService(IVertexService vertexService, IEdgeService edgeService) + { + _vertexService = vertexService; + _edgeService = edgeService; + } + + private void GetSourcesAndDestinations(Dictionary> vertxAttValues, + string sourceValue, string destinationValue, out List sources, out List destinations) + { + //sources = new List(); + //destinations = new List(); + + sources = vertxAttValues[sourceValue]; + destinations = vertxAttValues[destinationValue]; + /* + foreach (var v in vertices) + { + if (v.Label == sourceValue) + { + sources.Add(v); + } + if (v.Label == destinationValue) + { + destinations.Add(v); + } + }*/ + + /* + foreach (var record1 in vertexRecords) + { + foreach (var item in record1) + { + if (item.VertexAttribute.Name == vertexIdentifierFieldName && item.StringValue == sourceValue) + { + Vertex vertex = new Vertex() + { + Id = record1.Key + }; + sources.Add(vertex); + } + + if (item.VertexAttribute.Name == vertexIdentifierFieldName && item.StringValue == destinationValue) + { + Vertex vertex = new Vertex() + { + Id = record1.Key + }; + destinations.Add(vertex); + } + } + } + */ + } + + private void GetSourceAndDerstinationValues(string sourceEdgeIdentifierFieldName, + string destinationEdgeIdentifierFieldName, IGrouping record, out string sourceValue, + out string destinationValue) + { + sourceValue = string.Empty; + destinationValue = string.Empty; + foreach (var item in record) + { + if (item.EdgeAttribute.Name == sourceEdgeIdentifierFieldName) sourceValue = item.StringValue; + + if (item.EdgeAttribute.Name == destinationEdgeIdentifierFieldName) destinationValue = item.StringValue; + } + } + + + public (List vertices, List edges) GetGraph(Dictionary> vertices, + Dictionary> edges, string vertexIdentifierFieldName, + string SourceIdentifierFieldName, string TargetIdentifierFieldName) + { + var resEdges = new List(); + + var dicVertices = vertices.GroupBy(x => x.Value[vertexIdentifierFieldName]) + .ToDictionary(x => x.Key, x => x.ToList()); + + var resVertices = vertices + .Select(record => new Vertex + { + Id = record.Key, + Label = record.Value[vertexIdentifierFieldName] + }) + .ToList(); + + foreach (var edge in edges) + { + var sourceValue = edge.Value[SourceIdentifierFieldName]; + var targetValue = edge.Value[TargetIdentifierFieldName]; + + List>> sources; + if (!dicVertices.TryGetValue(sourceValue, out sources)) continue; + List>> targets; + if (!dicVertices.TryGetValue(targetValue, out targets)) continue; + + foreach (var source in sources) + foreach (var target in targets) + { + var newEdge = new Edge() + { + Id = edge.Key, + Source = source.Key, + Target = target.Key + }; + resEdges.Add(newEdge); + } + } + + return (resVertices, resEdges); + } +} \ No newline at end of file diff --git a/mohaymen-codestar-Team02/Services/IGraphService.cs b/mohaymen-codestar-Team02/Services/IGraphService.cs deleted file mode 100644 index 069f0c6..0000000 --- a/mohaymen-codestar-Team02/Services/IGraphService.cs +++ /dev/null @@ -1,11 +0,0 @@ -using mohaymen_codestar_Team02.Models; -using QuikGraph; - -namespace mohaymen_codestar_Team02.Services; - -public interface IGraphService -{ - (List vertices, List edges) GetGraph(long dataSetID, - string sourceEdgeIdentifierFieldName, - string destinationEdgeIdentifierFieldName, string vertexIdentifierFieldName); -} \ No newline at end of file diff --git a/mohaymen-codestar-Team02/Services/IVertexService.cs b/mohaymen-codestar-Team02/Services/VertexService/Abstraction/IVertexService.cs similarity index 60% rename from mohaymen-codestar-Team02/Services/IVertexService.cs rename to mohaymen-codestar-Team02/Services/VertexService/Abstraction/IVertexService.cs index c391cf2..7e5a17a 100644 --- a/mohaymen-codestar-Team02/Services/IVertexService.cs +++ b/mohaymen-codestar-Team02/Services/VertexService/Abstraction/IVertexService.cs @@ -1,12 +1,13 @@ using mohaymen_codestar_Team02.Dto; using mohaymen_codestar_Team02.Dto.GraphDTO; -using mohaymen_codestar_Team02.Models; namespace mohaymen_codestar_Team02.Services; public interface IVertexService { - public List GetAllVertices(long dataSetId, string vertexIdentifierFieldName); + public Dictionary> GetAllVertices(long dataSetId, + string vertexIdentifierFieldName, Dictionary vertexAttributeVales); + DetailDto GetVertexDetails(string objId); public List GetVertexAttributes(long vertexEntityId); } \ No newline at end of file diff --git a/mohaymen-codestar-Team02/Services/VertexService.cs b/mohaymen-codestar-Team02/Services/VertexService/VertexService.cs similarity index 78% rename from mohaymen-codestar-Team02/Services/VertexService.cs rename to mohaymen-codestar-Team02/Services/VertexService/VertexService.cs index 41c07f8..d95952d 100644 --- a/mohaymen-codestar-Team02/Services/VertexService.cs +++ b/mohaymen-codestar-Team02/Services/VertexService/VertexService.cs @@ -1,6 +1,5 @@ using AutoMapper; using Microsoft.EntityFrameworkCore; -using Microsoft.EntityFrameworkCore.Update.Internal; using mohaymen_codestar_Team02.Data; using mohaymen_codestar_Team02.Dto; using mohaymen_codestar_Team02.Dto.GraphDTO; @@ -31,31 +30,28 @@ public List GetVertexAttributes(long vertexEntityId) return vertexAttribuite.Select(va => _mapper.Map(va)).ToList(); } - public List GetAllVertices(long dataSetId, string vertexIdentifierFieldName) + public Dictionary> GetAllVertices(long dataSetId, + string vertexIdentifierFieldName, Dictionary vertexAttributeVales) { using var scope = _serviceProvider.CreateScope(); var context = scope.ServiceProvider.GetRequiredService(); var dataSet = context.DataSets.Where(ds => ds.DataGroupId == dataSetId).Include(ds => ds.VertexEntity) .ThenInclude(ve => ve.VertexAttributes).ThenInclude(vv => vv.VertexValues) - .FirstOrDefault(ds => ds != null); + .FirstOrDefault(); var vertexRecords = dataSet.VertexEntity.VertexAttributes.Select(a => a.VertexValues).SelectMany(v => v) .GroupBy(v => v.ObjectId); - List vertices = new(); - foreach (var record in vertexRecords) - { - var value = record.SingleOrDefault(r => r.VertexAttribute.Name == vertexIdentifierFieldName).StringValue; - var v = new Vertex() - { - Id = record.Key, - Label = value - }; - vertices.Add(v); - } + var validVertexRecords = vertexRecords + .Where(group => + vertexAttributeVales.All(attr => + group.Any(v => v.VertexAttribute.Name == attr.Key && v.StringValue == attr.Value))); - return vertices; + var res = validVertexRecords.ToDictionary(x => x.Key, + x => x.ToDictionary(g => g.VertexAttribute.Name, g => g.StringValue)); + + return res; } public DetailDto GetVertexDetails(string objId) diff --git a/mohaymen-codestar-Team02_XUnitTest/Servies/Administration/AdminServiceTest.cs b/mohaymen-codestar-Team02_XUnitTest/Services/Administration/AdminServiceTest.cs similarity index 83% rename from mohaymen-codestar-Team02_XUnitTest/Servies/Administration/AdminServiceTest.cs rename to mohaymen-codestar-Team02_XUnitTest/Services/Administration/AdminServiceTest.cs index 227215b..64f84e3 100644 --- a/mohaymen-codestar-Team02_XUnitTest/Servies/Administration/AdminServiceTest.cs +++ b/mohaymen-codestar-Team02_XUnitTest/Services/Administration/AdminServiceTest.cs @@ -1,17 +1,17 @@ +using AutoMapper; using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.DependencyInjection; using mohaymen_codestar_Team02.Data; +using mohaymen_codestar_Team02.Dto.Role; +using mohaymen_codestar_Team02.Dto.User; using mohaymen_codestar_Team02.Models; using mohaymen_codestar_Team02.Services; using mohaymen_codestar_Team02.Services.Administration; using mohaymen_codestar_Team02.Services.CookieService; using mohaymen_codestar_Team02.Services.PasswordHandller; using NSubstitute; -using AutoMapper; -using Microsoft.Extensions.DependencyInjection; -using mohaymen_codestar_Team02.Dto.Role; -using mohaymen_codestar_Team02.Dto.User; -namespace mohaymen_codestar_Team02_XUnitTest.Servies.Administration; +namespace mohaymen_codestar_Team02_XUnitTest.Services.Administration; public class AdminServiceTest { @@ -92,7 +92,7 @@ public async Task CreateUser_ShouldReturnCreated_WhenUserIsSuccessfullyRegistere // Arrange FixTheReturnOfCookies("admin"); - var newUser = AddUserWithRole("admin", "SystemAdmin", 1, mockContext); + AddUserWithRole("admin", "SystemAdmin", 1, mockContext); var fakePasswordHash = new byte[] { 1, 2, 3, 4 }; var fakePasswordSalt = new byte[] { 5, 6, 7, 8 }; @@ -106,13 +106,13 @@ public async Task CreateUser_ShouldReturnCreated_WhenUserIsSuccessfullyRegistere var role = new Role { RoleType = RoleType.DataAdmin.ToString() }; mockContext.Roles.Add(role); - mockContext.SaveChanges(); + await mockContext.SaveChangesAsync(); _passwordService.ValidatePassword(Arg.Any()).Returns(true); // Act var result = await _sut.CreateUser( - new User() { UserId = 8, Username = "mamad" }, "password", new List() { "DataAdmin" }); + new User() { UserId = 8, Username = "testUser" }, "password", new List() { "DataAdmin" }); // Assert Assert.Equal(ApiResponseType.Created, result.Type); @@ -140,18 +140,16 @@ public async Task CreateUser_ShouldSetCorrectPasswordHashAndSalt_WhenUserIsSucce x[2] = fakePasswordSalt; }); _passwordService.ValidatePassword(Arg.Any()).Returns(true); - var newUser = new User() { UserId = 8, Username = "mamad" }; + var newUser = new User() { UserId = 8, Username = "testUser" }; // Act - var result = await _sut.CreateUser(newUser, "password", new List()); + await _sut.CreateUser(newUser, "password", new List()); // Assert - Assert.Equal(ApiResponseType.Created, result.Type); Assert.Equal(fakePasswordHash, newUser.PasswordHash); Assert.Equal(fakePasswordSalt, newUser.Salt); } - [Fact] public async Task GetUserByUsername_ShouldReturnUnauthorized_WhenTokenIsEmpty() { @@ -173,8 +171,8 @@ public async Task GetUserByUsername_ShouldReturnSuccess_WhenUserIsFound() // Arrange FixTheReturnOfCookies("admin"); - var adminUser = AddUserWithRole("admin", "SystemAdmin", 1, mockContext); - var testUser = AddUserWithRole("testUser", "SystemAdmin", 2, mockContext); + AddUserWithRole("admin", "SystemAdmin", 1, mockContext); + AddUserWithRole("testUser", "SystemAdmin", 2, mockContext); var userDto = new GetUserDto { Username = "testUser" }; _mapper.Map(Arg.Any()).Returns(userDto); @@ -183,7 +181,25 @@ public async Task GetUserByUsername_ShouldReturnSuccess_WhenUserIsFound() // Assert Assert.Equal(ApiResponseType.Success, result.Type); - Assert.NotNull(result.Data); + } + + [Fact] + public async Task GetUserByUsername_ShouldReturnSpecificUser_WhenUserIsFound() + { + using var scope = _serviceProvider.CreateScope(); + var mockContext = scope.ServiceProvider.GetRequiredService(); + + // Arrange + FixTheReturnOfCookies("admin"); + AddUserWithRole("admin", "SystemAdmin", 1, mockContext); + AddUserWithRole("testUser", "SystemAdmin", 2, mockContext); + var userDto = new GetUserDto { Username = "testUser" }; + _mapper.Map(Arg.Any()).Returns(userDto); + + // Act + var result = await _sut.GetUserByUsername("testUser"); + + // Assert Assert.Equal("testUser", result.Data.Username); } @@ -208,8 +224,8 @@ public async Task GetUserByUsername_ShouldReturnNotFound_WhenUserDoesNotExist() // Arrange FixTheReturnOfCookies("admin"); - var adminUser = AddUserWithRole("admin", "SystemAdmin", 1, mockContext); - var targetUser = AddUserWithRole("targetUser", "SystemAdmin", 2, mockContext); + AddUserWithRole("admin", "SystemAdmin", 1, mockContext); + AddUserWithRole("targetUser", "SystemAdmin", 2, mockContext); var userDto = new GetUserDto { Username = "targetUser" }; _mapper.Map(Arg.Any()).Returns(userDto); @@ -234,14 +250,14 @@ public async Task GetAllUsers_ShouldReturnUnauthorized_WhenTokenIsEmpty() } [Fact] - public async Task GetAllUsers_ShouldReturnAllUsersSuccessfully_WhenAdminExist() + public async Task GetAllUsers_ShouldReturnSuccess_WhenAdminExist() { using var scope = _serviceProvider.CreateScope(); var mockContext = scope.ServiceProvider.GetRequiredService(); // Arrange FixTheReturnOfCookies("admin"); - var adminUser = AddUserWithRole("admin", "SystemAdmin", 1, mockContext); + AddUserWithRole("admin", "SystemAdmin", 1, mockContext); AddUserWithRole("user1", "User", 2, mockContext); AddUserWithRole("user2", "User", 3, mockContext); @@ -252,6 +268,26 @@ public async Task GetAllUsers_ShouldReturnAllUsersSuccessfully_WhenAdminExist() // Assert Assert.Equal(ApiResponseType.Success, result.Type); + } + + [Fact] + public async Task GetAllUsers_ShouldReturnAllUsersSuccessfully_WhenAdminExist() + { + using var scope = _serviceProvider.CreateScope(); + var mockContext = scope.ServiceProvider.GetRequiredService(); + + // Arrange + FixTheReturnOfCookies("admin"); + AddUserWithRole("admin", "SystemAdmin", 1, mockContext); + AddUserWithRole("user1", "User", 2, mockContext); + AddUserWithRole("user2", "User", 3, mockContext); + + _mapper.Map(Arg.Any()).Returns(new GetUserDto()); + + // Act + var result = await _sut.GetUsersPaginated(1); + + // Assert Assert.Equal(3, result.Data.Count); } @@ -269,7 +305,7 @@ public async Task GetAllUsers_ShouldReturnBadRequest_WhenAdminNotFound() } [Fact] - public async Task GetAllRoles_ShouldReturnAllRolesSuccessfully_WhenAdminExists() + public async Task GetAllRoles_ShouldReturnSuccess_WhenAdminExists() { using var scope = _serviceProvider.CreateScope(); var mockContext = scope.ServiceProvider.GetRequiredService(); @@ -278,7 +314,6 @@ public async Task GetAllRoles_ShouldReturnAllRolesSuccessfully_WhenAdminExists() mockContext.Roles.Add(new Role { RoleId = 1, RoleType = "SystemAdmin" }); mockContext.Roles.Add(new Role { RoleId = 2, RoleType = "Analyst" }); await mockContext.SaveChangesAsync(); - _mapper.Map(Arg.Any()).Returns(new GetRoleDto()); // Act @@ -286,31 +321,29 @@ public async Task GetAllRoles_ShouldReturnAllRolesSuccessfully_WhenAdminExists() // Assert Assert.Equal(ApiResponseType.Success, result.Type); - Assert.Equal(2, result.Data.Count); } - private void FixTheReturnOfCookies(string? returnThis) + [Fact] + public async Task GetAllRoles_ShouldReturnAllRolesSuccessfully_WhenAdminExists() { - _cookieService.GetCookieValue().Returns(returnThis); - _tokenService.GetUserNameFromToken().Returns(returnThis); - } + using var scope = _serviceProvider.CreateScope(); + var mockContext = scope.ServiceProvider.GetRequiredService(); - private UserRole AddUserWithRole(string userName, string roleType, long id, DataContext dataContext) - { - var user = new User - { - Salt = Array.Empty(), - PasswordHash = Array.Empty(), - Username = userName, - UserId = id - }; - var role = new Role { RoleType = roleType, RoleId = id }; - var userRole = new UserRole { UserId = user.UserId, RoleId = role.RoleId }; - dataContext.Users.Add(user); - dataContext.Roles.Add(role); - dataContext.UserRoles.Add(userRole); - dataContext.SaveChanges(); - return new UserRole { Role = role, User = user }; + // Arrange + List roles = + [ + new Role { RoleId = 1, RoleType = "SystemAdmin" }, + new Role { RoleId = 2, RoleType = "Analyst" } + ]; + foreach (var role in roles) mockContext.Roles.Add(role); + await mockContext.SaveChangesAsync(); + _mapper.Map(Arg.Any()).Returns(new GetRoleDto()); + + // Act + var result = await _sut.GetAllRoles(); + + // Assert + Assert.Equal(2, result.Data.Count); } [Fact] @@ -367,7 +400,7 @@ public async Task AddRole_ShouldReturnNotFound_WhenUserDoesNotExist() // Arrange FixTheReturnOfCookies("admin"); AddUserWithRole("admin", "SystemAdmin", 1, mockContext); - var user = AddUserWithRole("target", "DataAdmin", 2, mockContext); + AddUserWithRole("target", "DataAdmin", 2, mockContext); // Act var result = await _sut.AddRole(new User() { Username = "testUser" }, @@ -509,7 +542,7 @@ public async Task DeleteRole_ShouldRoleBeNullIfLookingForIt_WhenUserRoleExistsAn // Assert var result = mockContext.UserRoles.SingleOrDefault(ur => - ur.UserId == user.User.UserId && ur.RoleId == user.Role.RoleId); + user.Role != null && user.User != null && ur.UserId == user.User.UserId && ur.RoleId == user.Role.RoleId); Assert.Null(result); } @@ -522,7 +555,7 @@ public async Task DeleteRole_ShouldReturnNotFound_WhenUserDoesNotExist() // Arrange FixTheReturnOfCookies("admin"); AddUserWithRole("admin", "SystemAdmin", 1, mockContext); - var user = AddUserWithRole("target", "DataAdmin", 2, mockContext); + AddUserWithRole("target", "DataAdmin", 2, mockContext); // Act var result = await _sut.DeleteRole(new User() { Username = "testUser" }, @@ -566,10 +599,11 @@ public async Task DeleteUser_ShouldReturnNotFound_WhenUserDoesNotExist() // Arrange FixTheReturnOfCookies("admin"); AddUserWithRole("admin", "SystemAdmin", 1, mockContext); - var user = AddUserWithRole("target", "DataAdmin", 2, mockContext); + AddUserWithRole("target", "DataAdmin", 2, mockContext); // Act var result = await _sut.DeleteUser(new User() { Username = "testUser" }); + // Assert Assert.Equal(ApiResponseType.NotFound, result.Type); } @@ -601,7 +635,7 @@ public async Task DeleteUser_ShouldReturnYouCanNotDeleteYourself_WhenAdminDelete // Arrange FixTheReturnOfCookies("admin"); AddUserWithRole("admin", "SystemAdmin", 1, mockContext); - var user = AddUserWithRole("target", "DataAdmin", 2, mockContext); + AddUserWithRole("target", "DataAdmin", 2, mockContext); // Act var result = await _sut.DeleteUser(new User() { Username = "admin" }); @@ -645,7 +679,7 @@ public async Task UpdateUser_ShouldReturnNotFound_WhenUserNotFound() // Arrange FixTheReturnOfCookies("admin"); AddUserWithRole("admin", "SystemAdmin", 1, mockContext); - var user = AddUserWithRole("target", "DataAdmin", 2, mockContext); + AddUserWithRole("target", "DataAdmin", 2, mockContext); // Act var result = await _sut.UpdateUser(new User() { Username = "testUSer" }); @@ -678,8 +712,59 @@ public async Task UpdateUser_ShouldReturnSuccess_WhenUpdateIsSuccessful() var result = await _sut.UpdateUser(updateUser); // Assert - Assert.NotNull(result); Assert.Equal(ApiResponseType.Success, result.Type); - Assert.NotNull(result.Data); + } + + [Fact] + public async Task UpdateUser_ShouldUpdateUser_WhenUpdateIsSuccessful() + { + using var scope = _serviceProvider.CreateScope(); + var mockContext = scope.ServiceProvider.GetRequiredService(); + + // Arrange + FixTheReturnOfCookies("admin"); + AddUserWithRole("admin", "SystemAdmin", 1, mockContext); + AddUserWithRole("target", "DataAdmin", 2, mockContext); + + _mapper.Map(Arg.Any()).Returns(new GetUserDto()); + + var updateUser = new User() + { + Username = "target", + FirstName = "update", + LastName = "update", + Email = "update@example.com" + }; + var expected = _mapper.Map(updateUser); + + // Act + var result = await _sut.UpdateUser(updateUser); + + // Assert + Assert.Equal(expected, result.Data); + } + + private void FixTheReturnOfCookies(string? returnThis) + { + _cookieService.GetCookieValue().Returns(returnThis); + _tokenService.GetUserNameFromToken().Returns(returnThis); + } + + private UserRole AddUserWithRole(string userName, string roleType, long id, DataContext dataContext) + { + var user = new User + { + Salt = Array.Empty(), + PasswordHash = Array.Empty(), + Username = userName, + UserId = id + }; + var role = new Role { RoleType = roleType, RoleId = id }; + var userRole = new UserRole { UserId = user.UserId, RoleId = role.RoleId }; + dataContext.Users.Add(user); + dataContext.Roles.Add(role); + dataContext.UserRoles.Add(userRole); + dataContext.SaveChanges(); + return new UserRole { Role = role, User = user }; } } \ No newline at end of file diff --git a/mohaymen-codestar-Team02_XUnitTest/Servies/AnalystService/AnalystServiceTest.cs b/mohaymen-codestar-Team02_XUnitTest/Services/AnalystService/AnalystServiceTest.cs similarity index 100% rename from mohaymen-codestar-Team02_XUnitTest/Servies/AnalystService/AnalystServiceTest.cs rename to mohaymen-codestar-Team02_XUnitTest/Services/AnalystService/AnalystServiceTest.cs diff --git a/mohaymen-codestar-Team02_XUnitTest/Servies/Authenticatoin/AuthenticationServiceTest.cs b/mohaymen-codestar-Team02_XUnitTest/Services/Authentication/AuthenticationServiceTest.cs similarity index 93% rename from mohaymen-codestar-Team02_XUnitTest/Servies/Authenticatoin/AuthenticationServiceTest.cs rename to mohaymen-codestar-Team02_XUnitTest/Services/Authentication/AuthenticationServiceTest.cs index 3aacd11..6e3c6cb 100644 --- a/mohaymen-codestar-Team02_XUnitTest/Servies/Authenticatoin/AuthenticationServiceTest.cs +++ b/mohaymen-codestar-Team02_XUnitTest/Services/Authentication/AuthenticationServiceTest.cs @@ -12,7 +12,7 @@ using mohaymen_codestar_Team02.Services.PasswordHandller; using NSubstitute; -namespace mohaymen_codestar_Team02_XUnitTest.Servies.Authenticatoin; +namespace mohaymen_codestar_Team02_XUnitTest.Services.Authentication; public class AuthenticationServiceTests { @@ -20,7 +20,7 @@ public class AuthenticationServiceTests private readonly ITokenService _tokenService; private readonly ICookieService _cookieService; private readonly IPasswordService _passwordService; - private IServiceProvider _serviceProvider; + private readonly IServiceProvider _serviceProvider; public AuthenticationServiceTests() { @@ -70,7 +70,6 @@ public async Task Login_ShouldReturnBadRequest_WhenPasswordIsNullOrEmpty(string // Assert Assert.Equal(ApiResponseType.BadRequest, result.Type); - Assert.Null(result.Data); } [Fact] @@ -85,7 +84,6 @@ public async Task Login_ShouldReturnBadRequest_WhenUserDoesNotExist() // Assert Assert.Equal(ApiResponseType.BadRequest, result.Type); - Assert.Null(result.Data); } [Fact] @@ -104,7 +102,6 @@ public async Task Login_ShouldReturnBadRequest_WhenPasswordIsIncorrect() // Assert Assert.Equal(ApiResponseType.BadRequest, result.Type); - Assert.Null(result.Data); } [Fact] @@ -126,7 +123,6 @@ public async Task Login_ShouldReturnSuccess_WhenCredentialsAreCorrect() // Assert Assert.Equal(ApiResponseType.Success, result.Type); - Assert.NotNull(result.Data); } private void AddUserToDatabase(string username, string password) @@ -154,14 +150,13 @@ public void Logout_ShouldCallGetExpiredCookie_WhenCookieIsPresent() { // Arrange _cookieService.GetCookieValue().Returns("someCookieValue"); + _cookieService.GetExpiredCookie(); // Act var result = _sut.Logout(); // Assert - _cookieService.Received(1).GetExpiredCookie(); Assert.Equal(ApiResponseType.Success, result.Type); - Assert.Null(result.Data); } [Fact] @@ -169,14 +164,12 @@ public void Logout_ShouldNotCallGetExpiredCookie_WhenCookieIsNotPresent() { // Arrange _cookieService.GetCookieValue().Returns((string?)null); - + _cookieService.DidNotReceive().GetExpiredCookie(); // Act var result = _sut.Logout(); // Assert - _cookieService.DidNotReceive().GetExpiredCookie(); Assert.Equal(ApiResponseType.Success, result.Type); - Assert.Null(result.Data); } [Fact] @@ -261,7 +254,7 @@ private void FixTheReturnOfCookies(string? returnThis) _tokenService.GetUserNameFromToken().Returns(returnThis); } - private UserRole AddUserWithRole(string userName, string roleType, long id, List permissions) + private void AddUserWithRole(string userName, string roleType, long id, List permissions) { using var scope = _serviceProvider.CreateScope(); var mockContext = scope.ServiceProvider.GetRequiredService(); @@ -281,6 +274,5 @@ private UserRole AddUserWithRole(string userName, string roleType, long id, List mockContext.Roles.Add(role); mockContext.UserRoles.Add(userRole); mockContext.SaveChanges(); - return new UserRole { Role = role, User = user }; } } \ No newline at end of file diff --git a/mohaymen-codestar-Team02_XUnitTest/Servies/CookieService/CookieServiceTest.cs b/mohaymen-codestar-Team02_XUnitTest/Services/CookieService/CookieServiceTest.cs similarity index 98% rename from mohaymen-codestar-Team02_XUnitTest/Servies/CookieService/CookieServiceTest.cs rename to mohaymen-codestar-Team02_XUnitTest/Services/CookieService/CookieServiceTest.cs index 59a2171..c19401f 100644 --- a/mohaymen-codestar-Team02_XUnitTest/Servies/CookieService/CookieServiceTest.cs +++ b/mohaymen-codestar-Team02_XUnitTest/Services/CookieService/CookieServiceTest.cs @@ -1,7 +1,7 @@ using Microsoft.AspNetCore.Http; using NSubstitute; -namespace mohaymen_codestar_Team02_XUnitTest.Servies.CookieService; +namespace mohaymen_codestar_Team02_XUnitTest.Services.CookieService; public class CookieServiceTests { diff --git a/mohaymen-codestar-Team02_XUnitTest/Servies/DataAdminService/DataAdminServiceTest.cs b/mohaymen-codestar-Team02_XUnitTest/Services/DataAdminService/DataAdminServiceTest.cs similarity index 89% rename from mohaymen-codestar-Team02_XUnitTest/Servies/DataAdminService/DataAdminServiceTest.cs rename to mohaymen-codestar-Team02_XUnitTest/Services/DataAdminService/DataAdminServiceTest.cs index d7ca8bb..bfc9d5b 100644 --- a/mohaymen-codestar-Team02_XUnitTest/Servies/DataAdminService/DataAdminServiceTest.cs +++ b/mohaymen-codestar-Team02_XUnitTest/Services/DataAdminService/DataAdminServiceTest.cs @@ -11,21 +11,21 @@ using mohaymen_codestar_Team02.Services.StoreData.Abstraction; using NSubstitute; -namespace mohaymen_codestar_Team02_XUnitTest.Servies.DataAdminService; +namespace mohaymen_codestar_Team02_XUnitTest.Services.DataAdminService; public class DataAdminServiceTest { - private readonly IStorHandler _storHandler; + private readonly IStorHandler _storeHandler; private readonly IDisplayDataService _displayDataService; private readonly IMapper _mapper; private readonly IServiceProvider _serviceProvider; private DataContext _dataContext; - private readonly mohaymen_codestar_Team02.Services.DataAdminService.DataAdminService _sut; private readonly IEdgeService _edgeService; private readonly IVertexService _vertexService; private readonly ITokenService _tokenService; private readonly ICookieService _cookieService; private readonly IGraphService _graphService; + private readonly mohaymen_codestar_Team02.Services.DataAdminService.DataAdminService _sut; public DataAdminServiceTest() { @@ -34,7 +34,7 @@ public DataAdminServiceTest() _tokenService = Substitute.For(); _vertexService = Substitute.For(); _edgeService = Substitute.For(); - _storHandler = Substitute.For(); + _storeHandler = Substitute.For(); _displayDataService = Substitute.For(); _mapper = Substitute.For(); @@ -50,9 +50,9 @@ public DataAdminServiceTest() _sut = new mohaymen_codestar_Team02.Services.DataAdminService.DataAdminService(_serviceProvider, _tokenService, - _cookieService, _storHandler, _displayDataService, _edgeService, _vertexService, _mapper, _graphService); - _storHandler.EdageStorer.StoreFileData(Arg.Any(), Arg.Any(), Arg.Any()).Returns(true); - _storHandler.VertexStorer.StoreFileData(Arg.Any(), Arg.Any(), Arg.Any()).Returns(true); + _cookieService, _storeHandler, _displayDataService, _edgeService, _vertexService, _mapper, _graphService); + _storeHandler.EdageStorer.StoreFileData(Arg.Any(), Arg.Any(), Arg.Any()).Returns(true); + _storeHandler.VertexStorer.StoreFileData(Arg.Any(), Arg.Any(), Arg.Any()).Returns(true); } private void FixTheReturnOfCookies(string? returnThis) @@ -102,7 +102,7 @@ public async Task StoreData_ReturnsBadRequest_WhenCreatingTheDataGroupIsFail() //Arrange FixTheReturnOfCookies("admin"); AddUserWithRole("admin", "SustemAdmin", 1); - _storHandler.StoreDataSet("mahdddd", Arg.Any()).Returns(-1); + _storeHandler.StoreDataSet("mahdddd", Arg.Any()).Returns(-1); //Action var result = await _sut.StoreData("sample", "sample", "mahdddd", "name", "ma"); //Assert @@ -115,7 +115,7 @@ public async Task StoreData_ReturnsBadRequest_WhenEdageStorerStoreValuesReturnFa //Arrange FixTheReturnOfCookies("admin"); AddUserWithRole("admin", "SustemAdmin", 1); - _storHandler.EdageStorer.StoreFileData(Arg.Any(), Arg.Any(), Arg.Any()).Returns(false); + _storeHandler.EdageStorer.StoreFileData(Arg.Any(), Arg.Any(), Arg.Any()).Returns(false); //Action var result = await _sut.StoreData("sample", "sample", "test", "mahdddd", "mahdddd"); //Assert @@ -128,7 +128,7 @@ public async Task StoreData_ReturnsBadRequest_WhenVertexStorerStoreValuesReturnF // Arrange FixTheReturnOfCookies("admin"); AddUserWithRole("admin", "SustemAdmin", 1); - _storHandler.VertexStorer.StoreFileData(Arg.Any(), Arg.Any(), Arg.Any()).Returns(false); + _storeHandler.VertexStorer.StoreFileData(Arg.Any(), Arg.Any(), Arg.Any()).Returns(false); // Act var result = await _sut.StoreData("sampleEdgeFile", "sampleVertexFile", "testData", "mamama", "mmm"); @@ -143,7 +143,7 @@ public async Task StoreData_ReturnsSuccess_WhenInputAreValid() // Arrange FixTheReturnOfCookies("admin"); AddUserWithRole("admin", "SustemAdmin", 1); - _storHandler.StoreDataSet(Arg.Any(), Arg.Any()).Returns(9); + _storeHandler.StoreDataSet(Arg.Any(), Arg.Any()).Returns(9); // Act var result = await _sut.StoreData("sampleEdgeFile", "sampleVertexFile", "testData", "a", "lll"); // Assert @@ -254,10 +254,4 @@ public void DisplayDataSet_ShouldReturnDataSet_WhenGivenCorrectUsername() // Assert Assert.Equivalent(expected, actual); } - - public ServiceResponse> GetVertexAttributes(long vertexEntityId) - { - var att = _vertexService.GetVertexAttributes(vertexEntityId); - return new ServiceResponse>(att, ApiResponseType.Success, ""); - } } \ No newline at end of file diff --git a/mohaymen-codestar-Team02_XUnitTest/Servies/EdgeServiceTest.cs b/mohaymen-codestar-Team02_XUnitTest/Services/EdgeServiceTest/EdgeServiceTest.cs similarity index 50% rename from mohaymen-codestar-Team02_XUnitTest/Servies/EdgeServiceTest.cs rename to mohaymen-codestar-Team02_XUnitTest/Services/EdgeServiceTest/EdgeServiceTest.cs index dcbce85..d6f9e78 100644 --- a/mohaymen-codestar-Team02_XUnitTest/Servies/EdgeServiceTest.cs +++ b/mohaymen-codestar-Team02_XUnitTest/Services/EdgeServiceTest/EdgeServiceTest.cs @@ -5,11 +5,10 @@ using mohaymen_codestar_Team02.Dto; using mohaymen_codestar_Team02.Models; using mohaymen_codestar_Team02.Models.EdgeEAV; -using mohaymen_codestar_Team02.Models.VertexEAV; using mohaymen_codestar_Team02.Services; using NSubstitute; -namespace mohaymen_codestar_Team02_XUnitTest.Servies; +namespace mohaymen_codestar_Team02_XUnitTest.Services.EdgeServiceTest; public class EdgeServiceTest { @@ -75,34 +74,35 @@ public void GetEdgeAttribute_ShouldReturnAllAttributes_WhenGivenCorrectEdgeId() // Arrange long edgeEntityId = 1; - var AttName1 = "Att1"; - var AttName2 = "Att2"; + var attName1 = "Att1"; + var attName2 = "Att2"; var expected = new List() { new() { Id = 1, - Name = AttName1 + Name = attName1 }, new() { Id = 2, - Name = AttName2 + Name = attName2 } }; var dataset = new DataGroup("Dataset1", 1) { + DataGroupId = 1, EdgeEntity = new EdgeEntity("Transaction", 1) { EdgeAttributes = new List { - new(AttName1, edgeEntityId) + new(attName1, edgeEntityId) { Id = 1 }, - new(AttName2, edgeEntityId) + new(attName2, edgeEntityId) { Id = 2 } @@ -117,14 +117,14 @@ public void GetEdgeAttribute_ShouldReturnAllAttributes_WhenGivenCorrectEdgeId() .Returns(new GetAttributeDto() { Id = 1, - Name = AttName1 + Name = attName1 }); _mapper.Map(Arg.Is(value => value.Id == 2)) .Returns(new GetAttributeDto() { Id = 2, - Name = AttName2 + Name = attName2 }); // Act @@ -141,25 +141,18 @@ public void GetAllEdges_ShouldReturnAllEdges_WhenGivenCorrectDatasetAndIdentifie var contex = scope.ServiceProvider.GetRequiredService(); // Arrange - var datasetName = "DataSet1"; + long datasetId = 1; + var attName1 = "SourceAcount"; + var attName2 = "DestiantionAccount"; + var attName3 = "RandomAtt"; var vertexIdentifierFieldName = "CardID"; - var sourceEdgeIdentifierFieldName = "SourceAcount"; - var destinationEdgeIdentifierFieldName = "DestiantionAccount"; + var sourceEdgeIdentifierFieldName = attName1; + var destinationEdgeIdentifierFieldName = attName2; - var expected = new List() + var attValue = new Dictionary() { - new() - { - Id = "id1", - Source = "id2", - Target = "id3" - }, - new() - { - Id = "id2", - Source = "id4", - Target = "id5" - } + { attName1, "val1" }, + { attName2, "val8" } }; var dataset = new DataGroup("Dataset1", 1) @@ -168,46 +161,82 @@ public void GetAllEdges_ShouldReturnAllEdges_WhenGivenCorrectDatasetAndIdentifie { EdgeAttributes = new List { - new(sourceEdgeIdentifierFieldName, 1) + new(attName1, 1) { EdgeValues = new List { - new("val1", 1, "id1") - { EdgeAttribute = new EdgeAttribute(sourceEdgeIdentifierFieldName, 1) }, - new("val2", 1, "id2") - { EdgeAttribute = new EdgeAttribute(sourceEdgeIdentifierFieldName, 1) } + new("val1", 1, "id1") { EdgeAttribute = new EdgeAttribute(attName1, 1) }, + new("val1", 1, "id2") { EdgeAttribute = new EdgeAttribute(attName1, 1) }, + new("val2", 1, "id3") { EdgeAttribute = new EdgeAttribute(attName1, 1) }, + new("val2", 1, "id4") { EdgeAttribute = new EdgeAttribute(attName1, 1) }, + new("val5", 1, "id5") { EdgeAttribute = new EdgeAttribute(attName1, 1) }, + new("val1", 1, "id6") { EdgeAttribute = new EdgeAttribute(attName1, 1) }, + new("val1", 1, "id7") { EdgeAttribute = new EdgeAttribute(attName1, 1) }, + new("val5", 1, "id8") { EdgeAttribute = new EdgeAttribute(attName1, 1) }, + new("val1", 1, "id9") { EdgeAttribute = new EdgeAttribute(attName1, 1) }, + new("val3", 1, "id10") { EdgeAttribute = new EdgeAttribute(attName1, 1) } + } + }, // 2, 6, 7 + new(attName2, 1) + { + EdgeValues = new List + { + new("val7", 2, "id1") { EdgeAttribute = new EdgeAttribute(attName2, 1) }, + new("val8", 2, "id2") { EdgeAttribute = new EdgeAttribute(attName2, 1) }, + new("val9", 2, "id3") { EdgeAttribute = new EdgeAttribute(attName2, 1) }, + new("val10", 2, "id4") { EdgeAttribute = new EdgeAttribute(attName2, 1) }, + new("val11", 2, "id5") { EdgeAttribute = new EdgeAttribute(attName2, 1) }, + new("val8", 2, "id6") { EdgeAttribute = new EdgeAttribute(attName2, 1) }, + new("val8", 2, "id7") { EdgeAttribute = new EdgeAttribute(attName2, 1) }, + new("val12", 2, "id8") { EdgeAttribute = new EdgeAttribute(attName2, 1) }, + new("val12", 2, "id9") { EdgeAttribute = new EdgeAttribute(attName2, 1) }, + new("val8", 2, "id10") { EdgeAttribute = new EdgeAttribute(attName2, 1) } } }, - new(destinationEdgeIdentifierFieldName, 2) + new(attName3, 1) { EdgeValues = new List { - new("val3", 2, "id1") - { EdgeAttribute = new EdgeAttribute(destinationEdgeIdentifierFieldName, 1) }, - new("val3", 2, "id2") - { EdgeAttribute = new EdgeAttribute(destinationEdgeIdentifierFieldName, 1) } + new("val7", 2, "id1") { EdgeAttribute = new EdgeAttribute(attName3, 1) }, + new("val11", 2, "id2") { EdgeAttribute = new EdgeAttribute(attName3, 1) }, + new("val9", 2, "id3") { EdgeAttribute = new EdgeAttribute(attName3, 1) }, + new("val10", 2, "id4") { EdgeAttribute = new EdgeAttribute(attName3, 1) }, + new("val11", 2, "id5") { EdgeAttribute = new EdgeAttribute(attName3, 1) }, + new("val8", 2, "id6") { EdgeAttribute = new EdgeAttribute(attName3, 1) }, + new("val18", 2, "id7") { EdgeAttribute = new EdgeAttribute(attName3, 1) }, + new("val12", 2, "id8") { EdgeAttribute = new EdgeAttribute(attName3, 1) }, + new("val12", 2, "id9") { EdgeAttribute = new EdgeAttribute(attName3, 1) }, + new("val8", 2, "id10") { EdgeAttribute = new EdgeAttribute(attName3, 1) } } } } + } + }; + + var expected = new Dictionary>() + { + { + "id2", new Dictionary() + { + { attName1, "val1" }, + { attName2, "val8" }, + { attName3, "val11" } + } }, - VertexEntity = new VertexEntity("Account", 1) { - VertexAttributes = new List + "id6", new Dictionary() { - new(vertexIdentifierFieldName, 1) - { - VertexValues = new List - { - new("val1", 1, "id2") - { VertexAttribute = new VertexAttribute(vertexIdentifierFieldName, 1) }, - new("val3", 1, "id3") - { VertexAttribute = new VertexAttribute(vertexIdentifierFieldName, 1) }, - new("val2", 1, "id4") - { VertexAttribute = new VertexAttribute(vertexIdentifierFieldName, 1) }, - new("val3", 1, "id5") - { VertexAttribute = new VertexAttribute(vertexIdentifierFieldName, 1) } - } - } + { attName1, "val1" }, + { attName2, "val8" }, + { attName3, "val8" } + } + }, + { + "id7", new Dictionary() + { + { attName1, "val1" }, + { attName2, "val8" }, + { attName3, "val18" } } } }; @@ -217,8 +246,8 @@ public void GetAllEdges_ShouldReturnAllEdges_WhenGivenCorrectDatasetAndIdentifie // Act - var actual = _sut.GetAllEdges(1, vertexIdentifierFieldName, sourceEdgeIdentifierFieldName, - destinationEdgeIdentifierFieldName); + var actual = _sut.GetAllEdges(datasetId, sourceEdgeIdentifierFieldName, + destinationEdgeIdentifierFieldName, attValue); // Assert Assert.Equivalent(expected, actual); diff --git a/mohaymen-codestar-Team02_XUnitTest/Services/GraphServiceTest/GraphServiceTest.cs b/mohaymen-codestar-Team02_XUnitTest/Services/GraphServiceTest/GraphServiceTest.cs new file mode 100644 index 0000000..e69f83e --- /dev/null +++ b/mohaymen-codestar-Team02_XUnitTest/Services/GraphServiceTest/GraphServiceTest.cs @@ -0,0 +1,143 @@ +using mohaymen_codestar_Team02.Models; +using mohaymen_codestar_Team02.Services; +using NSubstitute; + +namespace mohaymen_codestar_Team02_XUnitTest.Services.GraphServiceTest; + +public class GraphServiceTest +{ + private GraphService _sut; + private readonly IVertexService _vertexService; + private readonly IEdgeService _edgeService; + + public GraphServiceTest() + { + _vertexService = Substitute.For(); + _edgeService = Substitute.For(); + _sut = new GraphService(_vertexService, _edgeService); + } + + [Fact] + public void GetGraph_ShouldReturnListOfVerticesAndDestinations_WhenGivenDatasetNameAndIdentifiers() + { + // Arrange + long datasetId = 1; + var vertexAttName1 = "VertexAttName1"; + var vertexAttName2 = "VertexAttName2"; + var edgeAttName1 = "EdgeAttName1"; + var edgeAttName2 = "EdgeAttName2"; + var edgeAttName3 = "EdgeAttName3"; + var sourceIdentifierFieldName = edgeAttName1; + var targetIdentifierFieldName = edgeAttName2; + var vertexIdentifierFieldName = vertexAttName1; + + var vertexAttributeValues = new Dictionary>() + { + { + "id1", new Dictionary() + { + { vertexAttName1, "val1" }, + { vertexAttName2, "val2" } + } + }, + { + "id2", new Dictionary() + { + { vertexAttName1, "val1" }, + { vertexAttName2, "val3" } + } + }, + { + "id3", new Dictionary() + { + { vertexAttName1, "val2" }, + { vertexAttName2, "val2" } + } + } + }; + + var edgeAttributeValues = new Dictionary>() + { + { + "id1", new Dictionary() + { + { edgeAttName1, "val1" }, // s= 1, 2 d= 3 + { edgeAttName2, "val2" }, + { edgeAttName3, "val8" } + } + }, + { + "id2", new Dictionary() + { + { edgeAttName1, "val2" }, // + { edgeAttName2, "val4" }, + { edgeAttName3, "val8" } + } + }, + { + "id3", new Dictionary() + { + { edgeAttName1, "val2" }, // s= 3 d= 1, 2 + { edgeAttName2, "val1" }, + { edgeAttName3, "val8" } + } + } + }; + + var expectedVertex = new List() + { + new() + { + Id = "id1", + Label = "val1" + }, + new() + { + Id = "id2", + Label = "val1" + }, + new() + { + Id = "id3", + Label = "val2" + } + }; + + var expectedEdge = new List() + { + new() + { + Id = "id1", + Source = "id1", + Target = "id3" + }, + new() + { + Id = "id1", + Source = "id2", + Target = "id3" + }, + new() + { + Id = "id3", + Source = "id3", + Target = "id1" + }, + new() + { + Id = "id3", + Source = "id3", + Target = "id2" + } + }; + + var expected = (expectedVertex, expectedEdge); + + // Act + var actual = _sut.GetGraph(vertexAttributeValues, edgeAttributeValues, vertexIdentifierFieldName, + sourceIdentifierFieldName, targetIdentifierFieldName); + + // Assert + Assert.Equivalent(expected, actual); + } +} \ No newline at end of file diff --git a/mohaymen-codestar-Team02_XUnitTest/Servies/PasswordHandler/PasswordHandlerTest.cs b/mohaymen-codestar-Team02_XUnitTest/Services/PasswordHandler/PasswordHandlerTest.cs similarity index 91% rename from mohaymen-codestar-Team02_XUnitTest/Servies/PasswordHandler/PasswordHandlerTest.cs rename to mohaymen-codestar-Team02_XUnitTest/Services/PasswordHandler/PasswordHandlerTest.cs index 2c5b0ce..9c183ef 100644 --- a/mohaymen-codestar-Team02_XUnitTest/Servies/PasswordHandler/PasswordHandlerTest.cs +++ b/mohaymen-codestar-Team02_XUnitTest/Services/PasswordHandler/PasswordHandlerTest.cs @@ -1,15 +1,10 @@ -namespace mohaymen_codestar_Team02_XUnitTest.Servies.PasswordHandler; - using mohaymen_codestar_Team02.Services.PasswordHandller; +namespace mohaymen_codestar_Team02_XUnitTest.Services.PasswordHandler; + public class PasswordServiceTests { - private readonly PasswordService _sut; - - public PasswordServiceTests() - { - _sut = new PasswordService(); - } + private readonly PasswordService _sut = new(); [Fact] public void CreatePasswordHash_ShouldGenerateDifferentHashAndSalt_ForDifferentPasswords() diff --git a/mohaymen-codestar-Team02_XUnitTest/Servies/ProfileService/ProfileServiceTest.cs b/mohaymen-codestar-Team02_XUnitTest/Services/ProfileService/ProfileServiceTest.cs similarity index 97% rename from mohaymen-codestar-Team02_XUnitTest/Servies/ProfileService/ProfileServiceTest.cs rename to mohaymen-codestar-Team02_XUnitTest/Services/ProfileService/ProfileServiceTest.cs index 382c900..ee465a9 100644 --- a/mohaymen-codestar-Team02_XUnitTest/Servies/ProfileService/ProfileServiceTest.cs +++ b/mohaymen-codestar-Team02_XUnitTest/Services/ProfileService/ProfileServiceTest.cs @@ -10,7 +10,7 @@ using mohaymen_codestar_Team02.Services.PasswordHandller; using NSubstitute; -namespace mohaymen_codestar_Team02_XUnitTest.Servies.ProfileService; +namespace mohaymen_codestar_Team02_XUnitTest.Services.ProfileService; public class ProfileServiceTests { @@ -163,7 +163,7 @@ public async Task UpdateUser_ShouldReturnSuccess_WhenUserIsUpdated() Assert.Equal(ApiResponseType.Success, result.Type); } - private User AddUserToDatabase(string username, string password) + private User? AddUserToDatabase(string username, string password) { using var scope = _serviceProvider.CreateScope(); var mockContext = scope.ServiceProvider.GetRequiredService(); diff --git a/mohaymen-codestar-Team02_XUnitTest/Servies/StorData/EdgeStorerCsvTest.cs b/mohaymen-codestar-Team02_XUnitTest/Services/StoreData/EdgeStorerCsvTest.cs similarity index 94% rename from mohaymen-codestar-Team02_XUnitTest/Servies/StorData/EdgeStorerCsvTest.cs rename to mohaymen-codestar-Team02_XUnitTest/Services/StoreData/EdgeStorerCsvTest.cs index 6a55d50..5de4fc5 100644 --- a/mohaymen-codestar-Team02_XUnitTest/Servies/StorData/EdgeStorerCsvTest.cs +++ b/mohaymen-codestar-Team02_XUnitTest/Services/StoreData/EdgeStorerCsvTest.cs @@ -1,11 +1,9 @@ using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.DependencyInjection; using mohaymen_codestar_Team02.Data; -using mohaymen_codestar_Team02.Models.EdgeEAV; using mohaymen_codestar_Team02.Services.StoreData; -using NSubstitute; -namespace mohaymen_codestar_Team02_XUnitTest.Servies.StorData; +namespace mohaymen_codestar_Team02_XUnitTest.Services.StoreData; public class EdgeStorerCsvTests { diff --git a/mohaymen-codestar-Team02_XUnitTest/Servies/StorData/StoreDataServiceTest.cs b/mohaymen-codestar-Team02_XUnitTest/Services/StoreData/StoreDataServiceTest.cs similarity index 97% rename from mohaymen-codestar-Team02_XUnitTest/Servies/StorData/StoreDataServiceTest.cs rename to mohaymen-codestar-Team02_XUnitTest/Services/StoreData/StoreDataServiceTest.cs index 1dcf722..9987f1f 100644 --- a/mohaymen-codestar-Team02_XUnitTest/Servies/StorData/StoreDataServiceTest.cs +++ b/mohaymen-codestar-Team02_XUnitTest/Services/StoreData/StoreDataServiceTest.cs @@ -6,7 +6,7 @@ using mohaymen_codestar_Team02.Services.StoreData.Abstraction; using NSubstitute; -namespace mohaymen_codestar_Team02_XUnitTest.Servies.StorData; +namespace mohaymen_codestar_Team02_XUnitTest.Services.StoreData; public class StoreDataServiceTest { diff --git a/mohaymen-codestar-Team02_XUnitTest/Servies/StorData/VertexStorerCsvTest.cs b/mohaymen-codestar-Team02_XUnitTest/Services/StoreData/VertexStorerCsvTest.cs similarity index 97% rename from mohaymen-codestar-Team02_XUnitTest/Servies/StorData/VertexStorerCsvTest.cs rename to mohaymen-codestar-Team02_XUnitTest/Services/StoreData/VertexStorerCsvTest.cs index 0a0ff82..ff07319 100644 --- a/mohaymen-codestar-Team02_XUnitTest/Servies/StorData/VertexStorerCsvTest.cs +++ b/mohaymen-codestar-Team02_XUnitTest/Services/StoreData/VertexStorerCsvTest.cs @@ -3,7 +3,7 @@ using mohaymen_codestar_Team02.Data; using mohaymen_codestar_Team02.Services.StoreData; -namespace mohaymen_codestar_Team02_XUnitTest.Servies.StorData; +namespace mohaymen_codestar_Team02_XUnitTest.Services.StoreData; public class VertexStorerCsvTests { diff --git a/mohaymen-codestar-Team02_XUnitTest/Servies/TokenService/TokenServiceTest.cs b/mohaymen-codestar-Team02_XUnitTest/Services/TokenService/TokenServiceTest.cs similarity index 93% rename from mohaymen-codestar-Team02_XUnitTest/Servies/TokenService/TokenServiceTest.cs rename to mohaymen-codestar-Team02_XUnitTest/Services/TokenService/TokenServiceTest.cs index c1576f3..aa685a7 100644 --- a/mohaymen-codestar-Team02_XUnitTest/Servies/TokenService/TokenServiceTest.cs +++ b/mohaymen-codestar-Team02_XUnitTest/Services/TokenService/TokenServiceTest.cs @@ -1,15 +1,13 @@ -namespace mohaymen_codestar_Team02_XUnitTest.Servies.TokenService; - using System.Security.Claims; +using Microsoft.AspNetCore.Http; using Microsoft.Extensions.Configuration; using NSubstitute; -using Xunit; -using mohaymen_codestar_Team02.Services.TokenService; -using Microsoft.AspNetCore.Http; + +namespace mohaymen_codestar_Team02_XUnitTest.Services.TokenService; public class TokenServiceTests { - private readonly TokenService _sut; + private readonly mohaymen_codestar_Team02.Services.TokenService.TokenService _sut; private readonly IConfiguration _configuration; private readonly IHttpContextAccessor _httpContextAccessor; @@ -22,7 +20,7 @@ public TokenServiceTests() _configuration.GetSection("AppSettings:Token").Value .Returns("SuperSuperSuperSuperSuperSuperSuperSuperSuperSuperSuperSuperSuperSuperSecretKey"); - _sut = new TokenService(_configuration, _httpContextAccessor); + _sut = new mohaymen_codestar_Team02.Services.TokenService.TokenService(_configuration, _httpContextAccessor); } [Fact] diff --git a/mohaymen-codestar-Team02_XUnitTest/Servies/VertexServiceTest.cs b/mohaymen-codestar-Team02_XUnitTest/Services/VertexServiceTest/VertexServiceTest.cs similarity index 72% rename from mohaymen-codestar-Team02_XUnitTest/Servies/VertexServiceTest.cs rename to mohaymen-codestar-Team02_XUnitTest/Services/VertexServiceTest/VertexServiceTest.cs index 4c65137..353b712 100644 --- a/mohaymen-codestar-Team02_XUnitTest/Servies/VertexServiceTest.cs +++ b/mohaymen-codestar-Team02_XUnitTest/Services/VertexServiceTest/VertexServiceTest.cs @@ -8,7 +8,7 @@ using mohaymen_codestar_Team02.Services; using NSubstitute; -namespace mohaymen_codestar_Team02_XUnitTest.Servies; +namespace mohaymen_codestar_Team02_XUnitTest.Services.VertexServiceTest; public class VertexServiceTest { @@ -39,20 +39,20 @@ public void GetVertexAttribute_ShouldReturnAllAttributes_WhenGivenCorrectVertexI // Arrange long vertexEntityId = 1; - var AttName1 = "Att1"; - var AttName2 = "Att2"; + var attName1 = "Att1"; + var attName2 = "Att2"; var expected = new List() { new() { Id = 1, - Name = AttName1 + Name = attName1 }, new() { Id = 2, - Name = AttName2 + Name = attName2 } }; @@ -62,11 +62,11 @@ public void GetVertexAttribute_ShouldReturnAllAttributes_WhenGivenCorrectVertexI { VertexAttributes = new List { - new(AttName1, 1) + new(attName1, 1) { Id = 1 }, - new(AttName2, 1) + new(attName2, 1) { Id = 2 } @@ -81,14 +81,14 @@ public void GetVertexAttribute_ShouldReturnAllAttributes_WhenGivenCorrectVertexI .Returns(new GetAttributeDto() { Id = 1, - Name = AttName1 + Name = attName1 }); _mapper.Map(Arg.Is(value => value.Id == 2)) .Returns(new GetAttributeDto() { Id = 2, - Name = AttName2 + Name = attName2 }); // Act @@ -140,23 +140,32 @@ public void GetAllVertices_ShouldReturnAllVertices_WhenGivenCorrectDatasetName() var mockContext = scope.ServiceProvider.GetRequiredService(); // Arrange - var datasetName = "DataSet1"; - var vertexIdentifierFieldName = "CardID"; + long datasetId = 1; + var attName1 = "AttName1"; + var attName2 = "AttName2"; + var vertexIdentifierFieldName = attName1; - var dataset = new DataGroup(datasetName, 1) + var dataset = new DataGroup("DatasetName1", 1) { + DataGroupId = 1, VertexEntity = new VertexEntity("Account", 1) { VertexAttributes = new List { - new(vertexIdentifierFieldName, 1) + new(attName1, 1) { VertexValues = new List { - new("val1", 1, "id1") - { VertexAttribute = new VertexAttribute(vertexIdentifierFieldName, 1) }, - new("val2", 1, "id2") - { VertexAttribute = new VertexAttribute(vertexIdentifierFieldName, 1) } + new("val1", 1, "id1") { VertexAttribute = new VertexAttribute(attName1, 1) }, + new("val2", 1, "id2") { VertexAttribute = new VertexAttribute(attName1, 1) } + } + }, + new(attName2, 1) + { + VertexValues = new List + { + new("val3", 2, "id1") { VertexAttribute = new VertexAttribute(attName2, 1) }, + new("val4", 2, "id2") { VertexAttribute = new VertexAttribute(attName2, 1) } } } } @@ -166,23 +175,25 @@ public void GetAllVertices_ShouldReturnAllVertices_WhenGivenCorrectDatasetName() mockContext.DataSets.Add(dataset); mockContext.SaveChanges(); - List expected = new() + var expected = new Dictionary>() { - new Vertex { - Id = "id1", - Label = "val1" - }, - new Vertex - { - Id = "id2", - Label = "val2" + "id1", new Dictionary() + { + { attName1, "val1" }, + { attName2, "val3" } + } } }; + var attValue = new Dictionary() + { + { attName1, "val1" } + //{attName2, "val3"}, + }; // Act - var actual = _sut.GetAllVertices(1, vertexIdentifierFieldName); + var actual = _sut.GetAllVertices(datasetId, vertexIdentifierFieldName, attValue); // Assert Assert.Equivalent(expected, actual); diff --git a/mohaymen-codestar-Team02_XUnitTest/Servies/GraphServiceTest.cs b/mohaymen-codestar-Team02_XUnitTest/Servies/GraphServiceTest.cs deleted file mode 100644 index 47e8092..0000000 --- a/mohaymen-codestar-Team02_XUnitTest/Servies/GraphServiceTest.cs +++ /dev/null @@ -1,67 +0,0 @@ -using mohaymen_codestar_Team02.Models; -using mohaymen_codestar_Team02.Services; -using NSubstitute; - - -namespace mohaymen_codestar_Team02_XUnitTest.Servies; - -public class GraphServiceTest -{ - private GraphService _sut; - private readonly IVertexService _vertexService; - private readonly IEdgeService _edgeService; - - public GraphServiceTest() - { - _vertexService = Substitute.For(); - _edgeService = Substitute.For(); - _sut = new GraphService(_vertexService, _edgeService); - } - - [Fact] - public void GetGraph_ShouldReturnListOfVerticesAndDestinations_WhenGivenDatasetNameAndIdentifiers() - { - // Arrange - var datasetName = "datasetName1"; - var sourceEdgeIdentifierFieldName = "AccountID"; - var destinationEdgeIdentifierFieldName = "SourceAcount"; - var vertexIdentifierFieldName = "DestiantionAccount"; - - var expectedVertex = new List() - { - new() - { - Id = "id1", - Label = "value1" - }, - new() - { - Id = "id2", - Label = "value2" - } - }; - - var expectedEdge = new List() - { - new() - { - Id = "id1", - Source = "id1", - Target = "id2" - } - }; - - var expected = (expectedVertex, expectedEdge); - - _vertexService.GetAllVertices(1, vertexIdentifierFieldName).Returns(expectedVertex); - _edgeService.GetAllEdges(1, vertexIdentifierFieldName, sourceEdgeIdentifierFieldName, - destinationEdgeIdentifierFieldName).Returns(expectedEdge); - - // Act - var actual = _sut.GetGraph(1, sourceEdgeIdentifierFieldName, destinationEdgeIdentifierFieldName, - vertexIdentifierFieldName); - - // Assert - Assert.Equivalent(expected, actual); - } -} \ No newline at end of file