structmapper/api_test.go
2025-08-07 16:23:09 +02:00

283 lines
6.4 KiB
Go

package structmapper_test
import (
"fmt"
"testing"
"git.apihub24.de/admin/structmapper"
"github.com/stretchr/testify/suite"
)
type testRight1 struct {
Id int
Name string
}
type testRight2 struct {
Id int
Name string
}
type testGroup1 struct {
Id int
Name string
Rights []*testRight1
}
type testGroup2 struct {
Id int
Name string
Rights []*testRight2
}
type testUser1 struct {
Id int
UserName string
Email string
Active bool
Groups []*testGroup1
}
type testUser2 struct {
Id int
UserName string
Email string
Active bool
Groups []*testGroup2
}
type ApiTestSuite struct {
suite.Suite
}
func (suite *ApiTestSuite) SetupSuite() {
// define Mapping for Right
structmapper.RegisterStrategy(func(from *testRight1) (*testRight2, error) {
return &testRight2{
Id: from.Id,
Name: from.Name,
}, nil
})
structmapper.RegisterStrategy(func(from *testRight2) (*testRight1, error) {
return &testRight1{
Id: from.Id,
Name: from.Name,
}, nil
})
// define Mapping for Group
structmapper.RegisterStrategy(func(from *testGroup1) (*testGroup2, error) {
return &testGroup2{
Id: from.Id,
Name: from.Name,
Rights: structmapper.SliceMap[*testRight1, *testRight2](from.Rights),
}, nil
})
structmapper.RegisterStrategy(func(from *testGroup2) (*testGroup1, error) {
return &testGroup1{
Id: from.Id,
Name: from.Name,
Rights: structmapper.SliceMap[*testRight2, *testRight1](from.Rights),
}, nil
})
// define Mapping for User
structmapper.RegisterStrategy(func(from *testUser1) (*testUser2, error) {
return &testUser2{
Id: from.Id,
UserName: from.UserName,
Email: from.Email,
Active: from.Active,
Groups: structmapper.SliceMap[*testGroup1, *testGroup2](from.Groups),
}, nil
})
structmapper.RegisterStrategy(func(from *testUser2) (*testUser1, error) {
return &testUser1{
Id: from.Id,
UserName: from.UserName,
Email: from.Email,
Active: from.Active,
Groups: structmapper.SliceMap[*testGroup2, *testGroup1](from.Groups),
}, nil
})
}
func (suite *ApiTestSuite) TestShouldMapFromTestRight1ToTestRight2() {
right := testRight1{
Id: 1,
Name: "READ",
}
mapped, err := structmapper.Map[*testRight1, *testRight2](&right)
suite.Nil(err)
suite.NotNil(mapped)
suite.Equal(mapped.Id, right.Id)
suite.Equal(mapped.Name, right.Name)
}
func (suite *ApiTestSuite) TestShouldMapFromTestRight2ToTestRight1() {
right := testRight2{
Id: 1,
Name: "READ",
}
mapped, err := structmapper.Map[*testRight2, *testRight1](&right)
suite.Nil(err)
suite.NotNil(mapped)
suite.Equal(mapped.Id, right.Id)
suite.Equal(mapped.Name, right.Name)
}
func (suite *ApiTestSuite) TestShouldMapFromGroup1ToGroup2() {
right := testRight1{
Id: 1,
Name: "READ",
}
group := testGroup1{
Id: 1,
Name: "admin",
Rights: []*testRight1{&right},
}
mapped, err := structmapper.Map[*testGroup1, *testGroup2](&group)
suite.Nil(err)
suite.NotNil(mapped)
suite.Equal(mapped.Id, group.Id)
suite.Equal(mapped.Name, group.Name)
suite.Equal(len(mapped.Rights), len(group.Rights))
}
func (suite *ApiTestSuite) TestShouldMapFromGroup2ToGroup1() {
right := testRight2{
Id: 1,
Name: "READ",
}
group := testGroup2{
Id: 1,
Name: "admin",
Rights: []*testRight2{&right},
}
mapped, err := structmapper.Map[*testGroup2, *testGroup1](&group)
suite.Nil(err)
suite.NotNil(mapped)
suite.Equal(mapped.Id, group.Id)
suite.Equal(mapped.Name, group.Name)
suite.Equal(len(mapped.Rights), len(group.Rights))
}
func (suite *ApiTestSuite) TestShouldMapFromUser1ToUser2() {
right := testRight1{
Id: 1,
Name: "READ",
}
group := testGroup1{
Id: 1,
Name: "admin",
Rights: []*testRight1{&right},
}
user := testUser1{
Id: 1,
UserName: "admin",
Email: "admin@example.de",
Active: true,
Groups: []*testGroup1{&group},
}
mapped, err := structmapper.Map[*testUser1, *testUser2](&user)
suite.Nil(err)
suite.NotNil(mapped)
suite.Equal(mapped.Id, user.Id)
suite.Equal(mapped.UserName, user.UserName)
suite.Equal(mapped.Email, user.Email)
suite.Equal(mapped.Active, user.Active)
suite.Equal(len(mapped.Groups), len(user.Groups))
}
func (suite *ApiTestSuite) TestShouldMapFromUser2ToUser1() {
right := testRight2{
Id: 1,
Name: "READ",
}
group := testGroup2{
Id: 1,
Name: "admin",
Rights: []*testRight2{&right},
}
user := testUser2{
Id: 1,
UserName: "admin",
Email: "admin@example.de",
Active: true,
Groups: []*testGroup2{&group},
}
mapped, err := structmapper.Map[*testUser2, *testUser1](&user)
suite.Nil(err)
suite.NotNil(mapped)
suite.Equal(mapped.Id, user.Id)
suite.Equal(mapped.UserName, user.UserName)
suite.Equal(mapped.Email, user.Email)
suite.Equal(mapped.Active, user.Active)
suite.Equal(len(mapped.Groups), len(user.Groups))
}
func (suite *ApiTestSuite) TestShouldUseAutoMapOnNoStrategy() {
right := testRight1{
Id: 1,
Name: "READ",
}
resultGroup, err := structmapper.Map[testRight1, testGroup1](right)
suite.False(structmapper.StrategyRuntimeException.IsSameAs(err))
suite.False(structmapper.MappingStrategyNotFound.IsSameAs(err))
suite.Equal(resultGroup.Id, right.Id)
suite.Equal(resultGroup.Name, right.Name)
result := structmapper.SliceMap[testRight1, testGroup1]([]testRight1{right})
suite.NotNil(result)
suite.Equal(len(result), 1)
suite.Equal(result[0].Id, right.Id)
suite.Equal(result[0].Name, right.Name)
}
func (suite *ApiTestSuite) TestShouldStrategyErrorsWasReturned() {
expectMessage := "StrategyError"
structmapper.RegisterStrategy(func(from testRight1) (testRight2, error) {
return testRight2{}, fmt.Errorf(expectMessage)
})
_, err := structmapper.Map[testRight1, testRight2](testRight1{
Id: 1,
Name: "READ",
})
suite.NotNil(err)
suite.True(structmapper.StrategyRuntimeException.IsSameAs(err))
}
func (suite *ApiTestSuite) TestShouldMapSerializeRight1ToRight2WithoutStrategy() {
right := testRight1{
Id: 1,
Name: "read",
}
result, err := structmapper.AutoMap[testRight1, testRight2](right)
suite.Nil(err)
suite.NotNil(result)
suite.Equal(right.Id, result.Id)
suite.Equal(right.Name, result.Name)
}
func (suite *ApiTestSuite) TestShouldMapSerializeDifferentStructs() {
right := testRight1{
Id: 1,
Name: "read",
}
result, err := structmapper.AutoMap[testRight1, testGroup1](right)
suite.Nil(err)
suite.NotNil(result)
}
func TestApiTestSuite(t *testing.T) {
suite.Run(t, new(ApiTestSuite))
}