Skip to content

Commit

Permalink
added user mapper
Browse files Browse the repository at this point in the history
  • Loading branch information
umbertocicciaa committed Nov 7, 2024
1 parent ed4050e commit 694054d
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 2 deletions.
14 changes: 14 additions & 0 deletions UserService/Mappers/UserMapperProfile.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using AutoMapper;
using User;
using UserService.Models;

namespace UserService.Mappers;

public class UserMapperProfile : Profile
{
public UserMapperProfile()
{
CreateMap<UserEntity, UserDto>();
CreateMap<UserDto, UserEntity>();
}
}
26 changes: 26 additions & 0 deletions UserService/Protos/user.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
syntax = "proto3";
import "google/protobuf/wrappers.proto";
import "google/protobuf/empty.proto";
import "google/protobuf/timestamp.proto";

package user;

service User{
rpc SaveUser (UserDto) returns (google.protobuf.Empty);
/*
rpc GetUsers(google.protobuf.Empty) returns (stream UserDto);
rpc GetUserByGovId (UserGovIdRequest) returns (stream UserDto);
rpc GetUserByStudentId (UserStudentIdRequest) returns (stream UserDto);
*/
}

message UserDto {
google.protobuf.StringValue name = 1;
google.protobuf.StringValue surname = 2;
google.protobuf.StringValue gov_id = 3;
google.protobuf.StringValue student_id = 4;
google.protobuf.StringValue email = 5;
google.protobuf.StringValue password = 6;
google.protobuf.StringValue role = 7;
google.protobuf.StringValue phone = 8;
}
70 changes: 70 additions & 0 deletions UserService/Tests/Unit/UserMapperProfileTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
using AutoMapper;
using JetBrains.Annotations;
using MongoDB.Bson;
using User;
using UserService.Mappers;
using UserService.Models;
using Xunit;

namespace UserService.Tests.Unit;

[TestSubject(typeof(UserMapperProfile))]
public class UserMapperProfileTest
{
private readonly IMapper _mapper;

public UserMapperProfileTest()
{
var config = new MapperConfiguration(cfg => cfg.AddProfile<UserMapperProfile>());
_mapper = config.CreateMapper();
}

[Fact]
public void Map_UserDto_To_UserEntity()
{
var userDto = new UserDto
{
Name = "John",
Surname = "asas",
Email = "dasd",
GovId = "2e",
Password = "as",
Role = "xc",
Phone = "z"
};

var userEntity = _mapper.Map<UserEntity>(userDto);
Assert.Equal(userEntity.Name, userDto.Name);
Assert.Equal(userEntity.Surname, userDto.Surname);
Assert.Equal(userEntity.Email, userDto.Email);
Assert.Equal(userEntity.GovId, userDto.GovId);
Assert.Equal(userEntity.Password, userDto.Password);
Assert.Equal(userEntity.Role, userDto.Role);
Assert.Equal(userEntity.Phone, userDto.Phone);
}

[Fact]
public void Map_UserEntity_To_UserDto()
{
var userEntity = new UserEntity
{
Id = ObjectId.GenerateNewId(),
Name = "John",
Surname = "Comp",
Email = "umb",
GovId = "pippo",
Password = "dsda",
Role = "lol",
Phone = "2psd"
};

var userDto = _mapper.Map<UserDto>(userEntity);
Assert.Equal(userEntity.Name, userDto.Name);
Assert.Equal(userEntity.Surname, userDto.Surname);
Assert.Equal(userEntity.Email, userDto.Email);
Assert.Equal(userEntity.GovId, userDto.GovId);
Assert.Equal(userEntity.Password, userDto.Password);
Assert.Equal(userEntity.Role, userDto.Role);
Assert.Equal(userEntity.Phone, userDto.Phone);
}
}
12 changes: 10 additions & 2 deletions UserService/UserService.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,14 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="AutoMapper" Version="13.0.1" />
<PackageReference Include="Grpc.AspNetCore" Version="2.57.0"/>
<PackageReference Include="JetBrains.Annotations" Version="2024.3.0-eap1" />
<PackageReference Include="MongoDB.Bson" Version="3.0.0" />
<PackageReference Include="xunit" Version="2.5.3"/>
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.3"/>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0"/>
<PackageReference Include="Testcontainers.MongoDb" Version="4.0.0"/>
</ItemGroup>

<ItemGroup>
Expand All @@ -20,11 +26,13 @@

<ItemGroup>
<Folder Include="Configs\" />
<Folder Include="Protos\" />
<Folder Include="Repositories\" />
<Folder Include="Services\" />
<Folder Include="Tests\Integration\" />
<Folder Include="Tests\Unit\" />
</ItemGroup>

<ItemGroup>
<Protobuf Include="Protos\user.proto" GrpcServices="Server" />
</ItemGroup>

</Project>

0 comments on commit 694054d

Please sign in to comment.