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

Node file upload #28

Merged
merged 16 commits into from
Aug 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc.Testing;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Diagnostics;
using Microsoft.Extensions.DependencyInjection;
using RelationshipAnalysis.Context;
using RelationshipAnalysis.Models;
Expand All @@ -25,7 +26,9 @@ protected override void ConfigureWebHost(IWebHostBuilder builder)
}


services.AddDbContext<ApplicationDbContext>(options => { options.UseInMemoryDatabase(_databaseName).UseLazyLoadingProxies(); });
services.AddDbContext<ApplicationDbContext>(options => { options.UseInMemoryDatabase(_databaseName)
.ConfigureWarnings(w => w.Ignore(InMemoryEventId.TransactionIgnoredWarning))
.UseLazyLoadingProxies(); });


var serviceProvider = services.BuildServiceProvider();
Expand Down Expand Up @@ -73,24 +76,38 @@ private void SeedDatabase(ApplicationDbContext dbContext)
dbContext.Roles.Add(role);
dbContext.SaveChanges();

var nodeCategory1 = new NodeCategory { NodeCategoryName = "Account" };
var nodeCategory2 = new NodeCategory { NodeCategoryName = "Person" };
var nodeCategory1 = new NodeCategory
{
NodeCategoryId = 1,
NodeCategoryName = "Account"
};
var nodeCategory2 = new NodeCategory
{
NodeCategoryId = 2,
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 edgeCategory = new EdgeCategory
{
EdgeCategoryId = 1,
EdgeCategoryName = "Transaction"
};


dbContext.NodeCategories.Add(nodeCategory1);
Expand All @@ -102,15 +119,16 @@ private void SeedDatabase(ApplicationDbContext dbContext)

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

dbContext.Edges.Add(edge);

dbContext.Edges.Add(edge);

dbContext.SaveChangesAsync();

}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
using System.Net.Http.Headers;
using System.Net.Http.Json;
using System.Text.Json;
using System.Text;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Options;
using Moq;
using Newtonsoft.Json;
using NSubstitute;
using RelationshipAnalysis.Dto;
using RelationshipAnalysis.Dto.Graph;
using RelationshipAnalysis.Models.Auth;
using RelationshipAnalysis.Models.Graph;
using RelationshipAnalysis.Services.UserPanelServices.Abstraction.AuthServices;
using RelationshipAnalysis.Settings.JWT;
using JsonSerializer = System.Text.Json.JsonSerializer;

namespace RelationshipAnalysis.Integration.Test.Controllers;

Expand Down Expand Up @@ -49,8 +56,16 @@ public async Task GetGraph_ShouldReturnGraph_WhenUserIsAuthorized()
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", token);


var nodeCategory1 = new NodeCategory { NodeCategoryName = "Account" };
var nodeCategory2 = new NodeCategory { NodeCategoryName = "Person" };
var nodeCategory1 = new NodeCategory
{
NodeCategoryId = 1,
NodeCategoryName = "Account"
};
var nodeCategory2 = new NodeCategory
{
NodeCategoryId = 2,
NodeCategoryName = "Person"
};

var node1 = new Node
{
Expand All @@ -68,7 +83,11 @@ public async Task GetGraph_ShouldReturnGraph_WhenUserIsAuthorized()
NodeCategoryId = nodeCategory2.NodeCategoryId
};

var edgeCategory = new EdgeCategory { EdgeCategoryName = "Transaction"};
var edgeCategory = new EdgeCategory
{
EdgeCategoryId = 1,
EdgeCategoryName = "Transaction"
};



Expand Down Expand Up @@ -118,4 +137,68 @@ public async Task GetGraph_ShouldReturnGraph_WhenUserIsAuthorized()
Assert.Equivalent(responseData.edges, expectedEdges);

}
[Fact]
public async Task UploadNode_ShouldReturnSuccess_WhenDtoIsValid()
{
// Arrange
var csvContent = @"""AccountID"",""CardID"",""IBAN""
""6534454617"",""6104335000000190"",""IR120778801496000000198""
""4000000028"",""6037699000000020"",""IR033880987114000000028""
";
var mockFile = CreateFileMock(csvContent);

var fileContent = new StreamContent(mockFile.OpenReadStream());
fileContent.Headers.ContentType = new MediaTypeHeaderValue("multipart/form-data");

var formDataContent = new MultipartFormDataContent();
formDataContent.Add(new StringContent("Account"), "NodeCategoryName");
formDataContent.Add(new StringContent("AccountID"), "UniqueAttributeHeaderName");
formDataContent.Add(fileContent, "file", mockFile.FileName);

var request = new HttpRequestMessage(HttpMethod.Post, "api/graph/uploadnode");

request.Content = formDataContent;

Mock<IOptions<JwtSettings>> jwtSettingsMock = new();
jwtSettingsMock.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(jwtSettingsMock.Object).GenerateJwtToken(user);
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", token);


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

// Assert
Assert.Equal(200, (int)response.StatusCode);
Assert.Equal(Resources.SuccessfulNodeAdditionMessage, response.Content.ReadFromJsonAsync<MessageDto>().Result.Message);
}

private IFormFile CreateFileMock(string csvContent)
{
var csvFileName = "test.csv";
var fileMock = Substitute.For<IFormFile>();
var stream = new MemoryStream();
var writer = new StreamWriter(stream);
writer.Write(csvContent);
writer.Flush();
stream.Position = 0;

fileMock.OpenReadStream().Returns(stream);
fileMock.FileName.Returns(csvFileName);
fileMock.Length.Returns(stream.Length);
return fileMock;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0"/>
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="8.0.8" />
<PackageReference Include="Moq" Version="4.20.70" />
<PackageReference Include="NSubstitute" Version="5.1.0" />
<PackageReference Include="xunit" Version="2.5.3"/>
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.3"/>
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="8.0.0" />
Expand All @@ -29,4 +30,25 @@
<ProjectReference Include="..\RelationshipAnalysis\RelationshipAnalysis.csproj" />
</ItemGroup>

<ItemGroup>
<EmbeddedResource Update="Resources.resx">
<Generator>ResXFileCodeGenerator</Generator>
<LastGenOutput>Resources.Designer.cs</LastGenOutput>
</EmbeddedResource>
</ItemGroup>

<ItemGroup>
<Compile Update="Resources.Designer.cs">
<DesignTime>True</DesignTime>
<AutoGen>True</AutoGen>
<DependentUpon>Resources.resx</DependentUpon>
</Compile>
</ItemGroup>

<ItemGroup>
<Reference Include="NSubstitute">
<HintPath>..\..\..\..\.nuget\packages\nsubstitute\5.1.0\lib\net6.0\NSubstitute.dll</HintPath>
</Reference>
</ItemGroup>

</Project>
Loading