MSP-5186 Implement AppGroup CRUD endpoints
This commit is contained in:
1 parent
59f3dc0d2f
commit
cb50a60156
10 files changed
+651
-27
No files matched your search
@@ -65,6 +65,8 @@ func (l Locator) Config(cfg conf.Remote) Config {
|
||||
serviceController := controller.NewService(serviceService)
|
||||
tokenController := controller.NewToken(tokenService)
|
||||
|
||||
appGroupService := service.NewAppGroup(appGroupRep)
|
||||
appGroupController := controller.NewAppGroup(appGroupService)
|
||||
c := routes.Controllers{
|
||||
Secure: secureController,
|
||||
AccessList: accessListController,
|
||||
@@ -72,6 +74,7 @@ func (l Locator) Config(cfg conf.Remote) Config {
|
||||
Service: serviceController,
|
||||
Application: applicationController,
|
||||
Token: tokenController,
|
||||
AppGroup: appGroupController,
|
||||
}
|
||||
mapper := endpoint.DefaultWrapper(l.logger, grpclog.Log(l.logger, true))
|
||||
server := routes.Handler(mapper, c)
|
||||
|
||||
@@ -0,0 +1,108 @@
|
||||
package controller
|
||||
|
||||
import (
|
||||
"context"
|
||||
"isp-system-service/domain"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
"google.golang.org/grpc/codes"
|
||||
"google.golang.org/grpc/status"
|
||||
)
|
||||
|
||||
type AppGroupService interface {
|
||||
Create(ctx context.Context, req domain.CreateAppGroupRequest) (*domain.AppGroup, error)
|
||||
Update(ctx context.Context, req domain.UpdateAppGroupRequest) (*domain.AppGroup, error)
|
||||
DeleteList(ctx context.Context, req domain.IdListRequest) (*domain.DeleteResponse, error)
|
||||
GetByIdList(ctx context.Context, idList []int) ([]domain.AppGroup, error)
|
||||
}
|
||||
|
||||
type AppGroup struct {
|
||||
service AppGroupService
|
||||
}
|
||||
|
||||
func NewAppGroup(service AppGroupService) AppGroup {
|
||||
return AppGroup{
|
||||
service: service,
|
||||
}
|
||||
}
|
||||
|
||||
// Create godoc
|
||||
//
|
||||
// @Tags application_group
|
||||
// @Summary Создать группу приложений
|
||||
// @Description Если группа приложений таким именем существует, возвращает ошибку
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param body body domain.CreateAppGroupRequest true "Объект группы приложений"
|
||||
// @Success 200 {object} domain.AppGroup
|
||||
// @Failure 400 {object} domain.GrpcError
|
||||
// @Failure 409 {object} domain.GrpcError
|
||||
// @Failure 500 {object} domain.GrpcError
|
||||
// @Router /application_group/create [POST]
|
||||
func (c AppGroup) Create(ctx context.Context, req domain.CreateAppGroupRequest) (*domain.AppGroup, error) {
|
||||
result, err := c.service.Create(ctx, req)
|
||||
switch {
|
||||
case errors.Is(err, domain.ErrAppGroupDuplicateName):
|
||||
return nil, status.Errorf(codes.AlreadyExists, "application group with name %s already exists", req.Name)
|
||||
default:
|
||||
return result, err
|
||||
}
|
||||
}
|
||||
|
||||
// Update godoc
|
||||
//
|
||||
// @Tags application_group
|
||||
// @Summary Обновить группу приложений
|
||||
// @Description Если группа приложений таким именем существует или группы приложений с указанным id не существует, возвращает ошибку
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param body body domain.UpdateAppGroupRequest true "Объект группы приложений"
|
||||
// @Success 200 {object} domain.AppGroup
|
||||
// @Failure 400 {object} domain.GrpcError
|
||||
// @Failure 404 {object} domain.GrpcError
|
||||
// @Failure 409 {object} domain.GrpcError
|
||||
// @Failure 500 {object} domain.GrpcError
|
||||
// @Router /application_group/update [POST]
|
||||
func (c AppGroup) Update(ctx context.Context, req domain.UpdateAppGroupRequest) (*domain.AppGroup, error) {
|
||||
result, err := c.service.Update(ctx, req)
|
||||
switch {
|
||||
case errors.Is(err, domain.ErrAppGroupDuplicateName):
|
||||
return nil, status.Errorf(codes.AlreadyExists, "application group with name %s already exists", req.Name)
|
||||
case errors.Is(err, domain.ErrAppGroupNotFound):
|
||||
return nil, status.Errorf(codes.NotFound, "application group with id %d not found", req.Id)
|
||||
default:
|
||||
return result, err
|
||||
}
|
||||
}
|
||||
|
||||
// DeleteList godoc
|
||||
//
|
||||
// @Tags application_group
|
||||
// @Summary Удалить группы приложений
|
||||
// @Description Удаляет группы приложений по списку их идентификаторов, возвращает количество удаленных групп приложений
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param body body domain.IdListRequest true "список идентификаторов групп приложений"
|
||||
// @Success 200 {object} domain.DeleteResponse
|
||||
// @Failure 400 {object} domain.GrpcError
|
||||
// @Failure 500 {object} domain.GrpcError
|
||||
// @Router /application_group/delete_list [POST]
|
||||
func (c AppGroup) DeleteList(ctx context.Context, req domain.IdListRequest) (*domain.DeleteResponse, error) {
|
||||
return c.service.DeleteList(ctx, req)
|
||||
}
|
||||
|
||||
// GetByIdList godoc
|
||||
//
|
||||
// @Tags application_group
|
||||
// @Summary Получить группы приложений по списку идентификаторов
|
||||
// @Description Возвращает группы приложений с указанными идентификаторами
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param body body domain.IdListRequest true "список идентификаторов групп приложений"
|
||||
// @Success 200 {array} domain.AppGroup
|
||||
// @Failure 400 {object} domain.GrpcError
|
||||
// @Failure 500 {object} domain.GrpcError
|
||||
// @Router /application_group/get_by_id_list [POST]
|
||||
func (c AppGroup) GetByIdList(ctx context.Context, req domain.IdListRequest) ([]domain.AppGroup, error) {
|
||||
return c.service.GetByIdList(ctx, req.IdList)
|
||||
}
|
||||
+180
-4
@@ -30,6 +30,19 @@ definitions:
|
||||
count:
|
||||
type: integer
|
||||
type: object
|
||||
domain.AppGroup:
|
||||
properties:
|
||||
createdAt:
|
||||
type: string
|
||||
description:
|
||||
type: string
|
||||
id:
|
||||
type: integer
|
||||
name:
|
||||
type: string
|
||||
updatedAt:
|
||||
type: string
|
||||
type: object
|
||||
domain.Application:
|
||||
properties:
|
||||
createdAt:
|
||||
@@ -133,6 +146,15 @@ definitions:
|
||||
authorized:
|
||||
type: boolean
|
||||
type: object
|
||||
domain.CreateAppGroupRequest:
|
||||
properties:
|
||||
description:
|
||||
type: string
|
||||
name:
|
||||
type: string
|
||||
required:
|
||||
- name
|
||||
type: object
|
||||
domain.CreateApplicationRequest:
|
||||
properties:
|
||||
applicationGroupId:
|
||||
@@ -207,6 +229,16 @@ definitions:
|
||||
errorMessage:
|
||||
type: string
|
||||
type: object
|
||||
domain.IdListRequest:
|
||||
properties:
|
||||
idList:
|
||||
items:
|
||||
type: integer
|
||||
minItems: 1
|
||||
type: array
|
||||
required:
|
||||
- idList
|
||||
type: object
|
||||
domain.Identity:
|
||||
properties:
|
||||
id:
|
||||
@@ -295,6 +327,18 @@ definitions:
|
||||
required:
|
||||
- appId
|
||||
type: object
|
||||
domain.UpdateAppGroupRequest:
|
||||
properties:
|
||||
description:
|
||||
type: string
|
||||
id:
|
||||
type: integer
|
||||
name:
|
||||
type: string
|
||||
required:
|
||||
- id
|
||||
- name
|
||||
type: object
|
||||
domain.UpdateApplicationRequest:
|
||||
properties:
|
||||
description:
|
||||
@@ -444,10 +488,6 @@ paths:
|
||||
description: Bad Request
|
||||
schema:
|
||||
$ref: '#/definitions/domain.GrpcError'
|
||||
"404":
|
||||
description: Not Found
|
||||
schema:
|
||||
$ref: '#/definitions/domain.GrpcError'
|
||||
"409":
|
||||
description: Conflict
|
||||
schema:
|
||||
@@ -721,6 +761,142 @@ paths:
|
||||
summary: Обновить приложение
|
||||
tags:
|
||||
- application
|
||||
/application_group/create:
|
||||
post:
|
||||
consumes:
|
||||
- application/json
|
||||
description: Если группа приложений таким именем существует, возвращает ошибку
|
||||
parameters:
|
||||
- description: Объект группы приложений
|
||||
in: body
|
||||
name: body
|
||||
required: true
|
||||
schema:
|
||||
$ref: '#/definitions/domain.CreateAppGroupRequest'
|
||||
produces:
|
||||
- application/json
|
||||
responses:
|
||||
"200":
|
||||
description: OK
|
||||
schema:
|
||||
$ref: '#/definitions/domain.AppGroup'
|
||||
"400":
|
||||
description: Bad Request
|
||||
schema:
|
||||
$ref: '#/definitions/domain.GrpcError'
|
||||
"409":
|
||||
description: Conflict
|
||||
schema:
|
||||
$ref: '#/definitions/domain.GrpcError'
|
||||
"500":
|
||||
description: Internal Server Error
|
||||
schema:
|
||||
$ref: '#/definitions/domain.GrpcError'
|
||||
summary: Создать группу приложений
|
||||
tags:
|
||||
- application_group
|
||||
/application_group/delete_list:
|
||||
post:
|
||||
consumes:
|
||||
- application/json
|
||||
description: Удаляет группы приложений по списку их идентификаторов, возвращает
|
||||
количество удаленных групп приложений
|
||||
parameters:
|
||||
- description: список идентификаторов групп приложений
|
||||
in: body
|
||||
name: body
|
||||
required: true
|
||||
schema:
|
||||
$ref: '#/definitions/domain.IdListRequest'
|
||||
produces:
|
||||
- application/json
|
||||
responses:
|
||||
"200":
|
||||
description: OK
|
||||
schema:
|
||||
$ref: '#/definitions/domain.DeleteResponse'
|
||||
"400":
|
||||
description: Bad Request
|
||||
schema:
|
||||
$ref: '#/definitions/domain.GrpcError'
|
||||
"500":
|
||||
description: Internal Server Error
|
||||
schema:
|
||||
$ref: '#/definitions/domain.GrpcError'
|
||||
summary: Удалить группы приложений
|
||||
tags:
|
||||
- application_group
|
||||
/application_group/get_by_id_list:
|
||||
post:
|
||||
consumes:
|
||||
- application/json
|
||||
description: Возвращает группы приложений с указанными идентификаторами
|
||||
parameters:
|
||||
- description: список идентификаторов групп приложений
|
||||
in: body
|
||||
name: body
|
||||
required: true
|
||||
schema:
|
||||
$ref: '#/definitions/domain.IdListRequest'
|
||||
produces:
|
||||
- application/json
|
||||
responses:
|
||||
"200":
|
||||
description: OK
|
||||
schema:
|
||||
items:
|
||||
$ref: '#/definitions/domain.AppGroup'
|
||||
type: array
|
||||
"400":
|
||||
description: Bad Request
|
||||
schema:
|
||||
$ref: '#/definitions/domain.GrpcError'
|
||||
"500":
|
||||
description: Internal Server Error
|
||||
schema:
|
||||
$ref: '#/definitions/domain.GrpcError'
|
||||
summary: Получить группы приложений по списку идентификаторов
|
||||
tags:
|
||||
- application_group
|
||||
/application_group/update:
|
||||
post:
|
||||
consumes:
|
||||
- application/json
|
||||
description: Если группа приложений таким именем существует или группы приложений
|
||||
с указанным id не существует, возвращает ошибку
|
||||
parameters:
|
||||
- description: Объект группы приложений
|
||||
in: body
|
||||
name: body
|
||||
required: true
|
||||
schema:
|
||||
$ref: '#/definitions/domain.UpdateAppGroupRequest'
|
||||
produces:
|
||||
- application/json
|
||||
responses:
|
||||
"200":
|
||||
description: OK
|
||||
schema:
|
||||
$ref: '#/definitions/domain.AppGroup'
|
||||
"400":
|
||||
description: Bad Request
|
||||
schema:
|
||||
$ref: '#/definitions/domain.GrpcError'
|
||||
"404":
|
||||
description: Not Found
|
||||
schema:
|
||||
$ref: '#/definitions/domain.GrpcError'
|
||||
"409":
|
||||
description: Conflict
|
||||
schema:
|
||||
$ref: '#/definitions/domain.GrpcError'
|
||||
"500":
|
||||
description: Internal Server Error
|
||||
schema:
|
||||
$ref: '#/definitions/domain.GrpcError'
|
||||
summary: Обновить группу приложений
|
||||
tags:
|
||||
- application_group
|
||||
/domain/create_update_domain:
|
||||
post:
|
||||
consumes:
|
||||
|
||||
@@ -0,0 +1,26 @@
|
||||
package domain
|
||||
|
||||
import "time"
|
||||
|
||||
type AppGroup struct {
|
||||
Id int
|
||||
Name string
|
||||
Description string
|
||||
CreatedAt time.Time
|
||||
UpdatedAt time.Time
|
||||
}
|
||||
|
||||
type CreateAppGroupRequest struct {
|
||||
Name string `validate:"required"`
|
||||
Description string
|
||||
}
|
||||
|
||||
type UpdateAppGroupRequest struct {
|
||||
Id int `validate:"required"`
|
||||
Name string `validate:"required"`
|
||||
Description string
|
||||
}
|
||||
|
||||
type IdListRequest struct {
|
||||
IdList []int `validate:"required,min=1"`
|
||||
}
|
||||
+11
-3
@@ -8,6 +8,7 @@ import (
|
||||
"isp-system-service/entity"
|
||||
|
||||
"github.com/Masterminds/squirrel"
|
||||
"github.com/jackc/pgx/v5/pgconn"
|
||||
"github.com/pkg/errors"
|
||||
"github.com/txix-open/isp-kit/db"
|
||||
"github.com/txix-open/isp-kit/db/query"
|
||||
@@ -104,15 +105,19 @@ func (r AppGroup) CreateAppGroup(ctx context.Context, name string, desc string,
|
||||
INSERT INTO application_group
|
||||
(name, description, domain_id)
|
||||
VALUES ($1, $2, $3)
|
||||
ON CONFLICT (name, domain_id) DO NOTHING
|
||||
RETURNING id, name, description, domain_id, created_at, updated_at
|
||||
`
|
||||
result := entity.AppGroup{}
|
||||
err := r.db.SelectRow(ctx, &result, q, name, desc, domainId)
|
||||
if err != nil {
|
||||
switch {
|
||||
case errors.Is(err, sql.ErrNoRows):
|
||||
return nil, domain.ErrAppGroupDuplicateName
|
||||
case err != nil:
|
||||
return nil, errors.WithMessagef(err, "exec query %s", q)
|
||||
default:
|
||||
return &result, nil
|
||||
}
|
||||
|
||||
return &result, nil
|
||||
}
|
||||
|
||||
func (r AppGroup) UpdateAppGroup(ctx context.Context, id int, name string, description string) (*entity.AppGroup, error) {
|
||||
@@ -124,7 +129,10 @@ func (r AppGroup) UpdateAppGroup(ctx context.Context, id int, name string, descr
|
||||
`
|
||||
result := entity.AppGroup{}
|
||||
err := r.db.SelectRow(ctx, &result, q, name, description, id)
|
||||
var pgErr *pgconn.PgError
|
||||
switch {
|
||||
case errors.As(err, &pgErr) && pgErr.ConstraintName == applicationGroupUniqueNameConstraint:
|
||||
return nil, domain.ErrAppGroupDuplicateName
|
||||
case errors.Is(err, sql.ErrNoRows):
|
||||
return nil, domain.ErrAppGroupNotFound
|
||||
case err != nil:
|
||||
|
||||
@@ -1,10 +1,9 @@
|
||||
package repository
|
||||
|
||||
const (
|
||||
pgUniqueViolationErrorCode = "23505"
|
||||
pgFkViolationErrorCode = "23503"
|
||||
|
||||
applicationPkConstrainName = "application_pkey"
|
||||
applicationUniqueNameConstraintName = "uq_name_application_group_id"
|
||||
applicationFkAppGroupConstraintName = "fk_application_group_id"
|
||||
|
||||
applicationGroupUniqueNameConstraint = "uq_name_domain_name"
|
||||
)
|
||||
+26
-1
@@ -12,6 +12,7 @@ type Controllers struct {
|
||||
Domain controller.Domain
|
||||
Service controller.Service
|
||||
Application controller.Application
|
||||
AppGroup controller.AppGroup
|
||||
Token controller.Token
|
||||
Secure controller.Secure
|
||||
}
|
||||
@@ -36,7 +37,8 @@ func endpointDescriptors(c Controllers) []cluster.EndpointDescriptor {
|
||||
serviceCluster(c),
|
||||
applicationCluster(c),
|
||||
tokenCluster(c),
|
||||
})
|
||||
applicationGroupCluster(c),
|
||||
)
|
||||
}
|
||||
|
||||
func secureCluster(c Controllers) []cluster.EndpointDescriptor {
|
||||
@@ -99,6 +101,7 @@ func domainCluster(c Controllers) []cluster.EndpointDescriptor {
|
||||
}
|
||||
}
|
||||
|
||||
// deprecated
|
||||
func serviceCluster(c Controllers) []cluster.EndpointDescriptor {
|
||||
return []cluster.EndpointDescriptor{
|
||||
{
|
||||
@@ -208,6 +211,28 @@ func tokenCluster(c Controllers) []cluster.EndpointDescriptor {
|
||||
}
|
||||
}
|
||||
|
||||
func applicationGroupCluster(c Controllers) []cluster.EndpointDescriptor {
|
||||
return []cluster.EndpointDescriptor{
|
||||
{
|
||||
Path: "system/application_group/create",
|
||||
Inner: true,
|
||||
Handler: c.AppGroup.Create,
|
||||
}, {
|
||||
Path: "system/application_group/update",
|
||||
Inner: true,
|
||||
Handler: c.AppGroup.Update,
|
||||
}, {
|
||||
Path: "system/application_group/delete_list",
|
||||
Inner: true,
|
||||
Handler: c.AppGroup.DeleteList,
|
||||
}, {
|
||||
Path: "system/application_group/get_by_id_list",
|
||||
Inner: true,
|
||||
Handler: c.AppGroup.GetByIdList,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func concatCluster(clusters ...[]cluster.EndpointDescriptor) []cluster.EndpointDescriptor {
|
||||
var result []cluster.EndpointDescriptor
|
||||
for _, c := range clusters {
|
||||
|
||||
@@ -0,0 +1,69 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"context"
|
||||
"isp-system-service/domain"
|
||||
"isp-system-service/entity"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
type AppGroup struct {
|
||||
repo AppGroupRepo
|
||||
}
|
||||
|
||||
func NewAppGroup(repo AppGroupRepo) AppGroup {
|
||||
return AppGroup{
|
||||
repo: repo,
|
||||
}
|
||||
}
|
||||
|
||||
func (s AppGroup) Create(ctx context.Context, req domain.CreateAppGroupRequest) (*domain.AppGroup, error) {
|
||||
appGroup, err := s.repo.CreateAppGroup(ctx, req.Name, req.Description, 1)
|
||||
if err != nil {
|
||||
return nil, errors.WithMessage(err, "create appGroup")
|
||||
}
|
||||
converted := s.convertAppGroup(*appGroup)
|
||||
return &converted, nil
|
||||
}
|
||||
|
||||
func (s AppGroup) Update(ctx context.Context, req domain.UpdateAppGroupRequest) (*domain.AppGroup, error) {
|
||||
appGroup, err := s.repo.UpdateAppGroup(ctx, req.Id, req.Name, req.Description)
|
||||
if err != nil {
|
||||
return nil, errors.WithMessage(err, "update appGroup")
|
||||
}
|
||||
converted := s.convertAppGroup(*appGroup)
|
||||
return &converted, nil
|
||||
}
|
||||
|
||||
func (s AppGroup) DeleteList(ctx context.Context, req domain.IdListRequest) (*domain.DeleteResponse, error) {
|
||||
deleted, err := s.repo.DeleteAppGroup(ctx, req.IdList)
|
||||
if err != nil {
|
||||
return nil, errors.WithMessage(err, "delete appGroup")
|
||||
}
|
||||
return &domain.DeleteResponse{
|
||||
Deleted: deleted,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (s AppGroup) GetByIdList(ctx context.Context, idList []int) ([]domain.AppGroup, error) {
|
||||
appGroups, err := s.repo.GetAppGroupByIdList(ctx, idList)
|
||||
if err != nil {
|
||||
return nil, errors.WithMessage(err, "get appGroups by id list")
|
||||
}
|
||||
result := make([]domain.AppGroup, 0, len(appGroups))
|
||||
for _, appGroup := range appGroups {
|
||||
result = append(result, s.convertAppGroup(appGroup))
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (s AppGroup) convertAppGroup(appGroup entity.AppGroup) domain.AppGroup {
|
||||
return domain.AppGroup{
|
||||
Id: appGroup.Id,
|
||||
Name: appGroup.Name,
|
||||
Description: *appGroup.Description,
|
||||
CreatedAt: appGroup.CreatedAt,
|
||||
UpdatedAt: appGroup.UpdatedAt,
|
||||
}
|
||||
}
|
||||
+1
-16
@@ -63,23 +63,12 @@ func (s Service) GetByDomainId(ctx context.Context, domainId int) ([]domain.Serv
|
||||
func (s Service) CreateUpdate(ctx context.Context, req domain.ServiceCreateUpdateRequest) (*domain.Service, error) {
|
||||
req.DomainId = 1 // temporary use only 1 domain, soon domain entity will be removed
|
||||
|
||||
existed, err := s.serviceRepo.GetAppGroupByNameAndDomainId(ctx, req.Name, req.DomainId)
|
||||
switch {
|
||||
case errors.Is(err, domain.ErrAppGroupNotFound):
|
||||
case err != nil:
|
||||
return nil, errors.WithMessage(err, "get service by name and domain_id")
|
||||
}
|
||||
|
||||
_, err = s.domainRepo.GetDomainById(ctx, req.DomainId)
|
||||
_, err := s.domainRepo.GetDomainById(ctx, req.DomainId)
|
||||
if err != nil {
|
||||
return nil, errors.WithMessage(err, "get domain by id")
|
||||
}
|
||||
|
||||
if req.Id == 0 {
|
||||
if existed != nil {
|
||||
return nil, domain.ErrDomainDuplicateName
|
||||
}
|
||||
|
||||
serviceEntity, err := s.serviceRepo.CreateAppGroup(ctx, req.Name, req.Description, req.DomainId)
|
||||
if err != nil {
|
||||
return nil, errors.WithMessage(err, "create service")
|
||||
@@ -89,10 +78,6 @@ func (s Service) CreateUpdate(ctx context.Context, req domain.ServiceCreateUpdat
|
||||
return &result, nil
|
||||
}
|
||||
|
||||
if existed != nil && existed.Id != req.Id {
|
||||
return nil, domain.ErrDomainDuplicateName
|
||||
}
|
||||
|
||||
_, err = s.serviceRepo.GetAppGroupById(ctx, req.Id)
|
||||
if err != nil {
|
||||
return nil, errors.WithMessage(err, "get service by id")
|
||||
|
||||
@@ -0,0 +1,225 @@
|
||||
package tests_test
|
||||
|
||||
import (
|
||||
"isp-system-service/assembly"
|
||||
"isp-system-service/conf"
|
||||
"isp-system-service/domain"
|
||||
"isp-system-service/entity"
|
||||
"isp-system-service/repository"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/stretchr/testify/suite"
|
||||
"github.com/txix-open/isp-kit/dbx"
|
||||
"github.com/txix-open/isp-kit/grpc/client"
|
||||
"github.com/txix-open/isp-kit/test"
|
||||
"github.com/txix-open/isp-kit/test/dbt"
|
||||
"github.com/txix-open/isp-kit/test/fake"
|
||||
"github.com/txix-open/isp-kit/test/grpct"
|
||||
"google.golang.org/grpc/codes"
|
||||
"google.golang.org/grpc/status"
|
||||
)
|
||||
|
||||
func TestApplicationGroupSuite(t *testing.T) {
|
||||
t.Parallel()
|
||||
suite.Run(t, &AppGroupSuite{})
|
||||
}
|
||||
|
||||
type AppGroupSuite struct {
|
||||
suite.Suite
|
||||
test *test.Test
|
||||
testDb *dbt.TestDb
|
||||
domainRepo repository.Domain
|
||||
appGroupRepo repository.AppGroup
|
||||
api *client.Client
|
||||
}
|
||||
|
||||
func (s *AppGroupSuite) SetupTest() {
|
||||
s.test, _ = test.New(s.T())
|
||||
|
||||
s.testDb = dbt.New(s.test, dbx.WithMigrationRunner("../migrations", s.test.Logger()))
|
||||
|
||||
locator := assembly.NewLocator(s.testDb, s.test.Logger())
|
||||
config := locator.Config(conf.Remote{})
|
||||
_, s.api = grpct.TestServer(s.test, config.Handler)
|
||||
s.domainRepo = repository.NewDomain(s.testDb)
|
||||
s.appGroupRepo = repository.NewAppGroup(s.testDb)
|
||||
|
||||
q := `
|
||||
INSERT INTO domain
|
||||
(id, name, description, system_id)
|
||||
VALUES ($1, $2, $3, $4)
|
||||
RETURNING id, name, description, system_id, created_at, updated_at
|
||||
`
|
||||
_, err := s.testDb.Exec(s.T().Context(), q, 1, fake.It[string](), fake.It[string](), 1)
|
||||
s.Require().NoError(err)
|
||||
}
|
||||
|
||||
func (s *AppGroupSuite) TestGetByIdList() {
|
||||
toGenerate := 10
|
||||
appGroups := make([]domain.AppGroup, 0, toGenerate)
|
||||
ids := make([]int, 0, toGenerate)
|
||||
for range 10 {
|
||||
appGroup := s.createAppGroup()
|
||||
appGroups = append(appGroups, domain.AppGroup{
|
||||
Id: appGroup.Id,
|
||||
Name: appGroup.Name,
|
||||
Description: *appGroup.Description,
|
||||
CreatedAt: time.Time{},
|
||||
UpdatedAt: time.Time{},
|
||||
})
|
||||
ids = append(ids, appGroup.Id)
|
||||
}
|
||||
result := []domain.AppGroup{}
|
||||
apiReq := domain.IdListRequest{IdList: ids}
|
||||
err := s.api.Invoke("system/application_group/get_by_id_list").
|
||||
JsonRequestBody(apiReq).
|
||||
JsonResponseBody(&result).
|
||||
Do(s.T().Context())
|
||||
s.Require().NoError(err)
|
||||
for i := range result {
|
||||
s.Require().NotEmpty(result[i].CreatedAt)
|
||||
result[i].CreatedAt = time.Time{}
|
||||
|
||||
s.Require().NotEmpty(result[i].UpdatedAt)
|
||||
result[i].UpdatedAt = time.Time{}
|
||||
}
|
||||
s.Require().ElementsMatch(appGroups, result)
|
||||
}
|
||||
|
||||
func (s *AppGroupSuite) TestGetByIdList_NotFoundAppGroups() {
|
||||
appGroup := s.createAppGroup()
|
||||
result := []domain.AppGroup{}
|
||||
|
||||
apiReq := domain.IdListRequest{IdList: []int{appGroup.Id + 1}}
|
||||
err := s.api.Invoke("system/application_group/get_by_id_list").
|
||||
JsonRequestBody(apiReq).
|
||||
JsonResponseBody(&result).
|
||||
Do(s.T().Context())
|
||||
s.Require().NoError(err)
|
||||
s.Require().Empty(result)
|
||||
}
|
||||
|
||||
func (s *AppGroupSuite) TestCreate_HappyPath() {
|
||||
apiReq := domain.CreateAppGroupRequest{
|
||||
Name: fake.It[string](),
|
||||
Description: fake.It[string](),
|
||||
}
|
||||
var result domain.AppGroup
|
||||
err := s.api.Invoke("system/application_group/create").
|
||||
JsonRequestBody(apiReq).
|
||||
JsonResponseBody(&result).
|
||||
Do(s.T().Context())
|
||||
s.Require().NoError(err)
|
||||
|
||||
s.Require().Equal(apiReq.Name, result.Name)
|
||||
s.Require().Equal(apiReq.Description, result.Description)
|
||||
|
||||
appGroup, err := s.appGroupRepo.GetAppGroupById(s.T().Context(), result.Id)
|
||||
s.Require().NoError(err)
|
||||
s.Require().Equal(apiReq.Name, appGroup.Name)
|
||||
s.Require().Equal(apiReq.Description, *appGroup.Description)
|
||||
}
|
||||
|
||||
func (s *AppGroupSuite) TestCreate_AppGroupNameNotUnique() {
|
||||
appGroup := s.createAppGroup()
|
||||
|
||||
apiReq := domain.CreateAppGroupRequest{
|
||||
Name: appGroup.Name,
|
||||
Description: fake.It[string](),
|
||||
}
|
||||
var result domain.AppGroup
|
||||
err := s.api.Invoke("system/application_group/create").
|
||||
JsonRequestBody(apiReq).
|
||||
JsonResponseBody(&result).
|
||||
Do(s.T().Context())
|
||||
statusErr, ok := status.FromError(err)
|
||||
s.Require().True(ok)
|
||||
s.Require().NotNil(statusErr)
|
||||
s.Require().Equal(codes.AlreadyExists, statusErr.Code())
|
||||
}
|
||||
|
||||
func (s *AppGroupSuite) TestUpdate_HappyPath() {
|
||||
appGroupToUpdate := s.createAppGroup()
|
||||
|
||||
apiReq := domain.UpdateAppGroupRequest{
|
||||
Id: appGroupToUpdate.Id,
|
||||
Name: fake.It[string](),
|
||||
Description: fake.It[string](),
|
||||
}
|
||||
var result domain.AppGroup
|
||||
err := s.api.Invoke("system/application_group/update").
|
||||
JsonRequestBody(apiReq).
|
||||
JsonResponseBody(&result).
|
||||
Do(s.T().Context())
|
||||
s.Require().NoError(err)
|
||||
|
||||
s.Require().Equal(apiReq.Name, result.Name)
|
||||
s.Require().Equal(apiReq.Description, result.Description)
|
||||
|
||||
appGroup, err := s.appGroupRepo.GetAppGroupById(s.T().Context(), apiReq.Id)
|
||||
s.Require().NoError(err)
|
||||
s.Require().Equal(apiReq.Name, appGroup.Name)
|
||||
s.Require().Equal(apiReq.Description, *appGroup.Description)
|
||||
}
|
||||
|
||||
func (s *AppGroupSuite) TestUpdate_AppGroupNameNotUnique() {
|
||||
appGroupToUpdate := s.createAppGroup()
|
||||
appGroup := s.createAppGroup()
|
||||
|
||||
apiReq := domain.UpdateAppGroupRequest{
|
||||
Id: appGroupToUpdate.Id,
|
||||
Name: appGroup.Name,
|
||||
Description: fake.It[string](),
|
||||
}
|
||||
err := s.api.Invoke("system/application_group/update").
|
||||
JsonRequestBody(apiReq).
|
||||
Do(s.T().Context())
|
||||
statusErr, ok := status.FromError(err)
|
||||
s.Require().True(ok)
|
||||
s.Require().NotNil(statusErr)
|
||||
s.Require().Equal(codes.AlreadyExists, statusErr.Code())
|
||||
}
|
||||
|
||||
func (s *AppGroupSuite) TestUpdate_AppGroupNotFound() {
|
||||
apiReq := domain.UpdateAppGroupRequest{
|
||||
Id: fake.It[int](),
|
||||
Name: fake.It[string](),
|
||||
Description: fake.It[string](),
|
||||
}
|
||||
err := s.api.Invoke("system/application_group/update").
|
||||
JsonRequestBody(apiReq).
|
||||
Do(s.T().Context())
|
||||
statusErr, ok := status.FromError(err)
|
||||
s.Require().True(ok)
|
||||
s.Require().NotNil(statusErr)
|
||||
s.Require().Equal(codes.NotFound, statusErr.Code())
|
||||
}
|
||||
|
||||
func (s *AppGroupSuite) TestDeleteList() {
|
||||
appGroupToDelete := s.createAppGroup()
|
||||
appGroup := s.createAppGroup()
|
||||
|
||||
var result domain.DeleteResponse
|
||||
apiReq := domain.IdListRequest{IdList: []int{appGroupToDelete.Id}}
|
||||
err := s.api.Invoke("system/application_group/delete_list").
|
||||
JsonRequestBody(apiReq).
|
||||
JsonResponseBody(&result).
|
||||
Do(s.T().Context())
|
||||
s.Require().NoError(err)
|
||||
s.Require().Equal(1, result.Deleted)
|
||||
|
||||
_, err = s.appGroupRepo.GetAppGroupById(s.T().Context(), appGroup.Id)
|
||||
s.Require().NoError(err)
|
||||
}
|
||||
|
||||
func (s *AppGroupSuite) createAppGroup() *entity.AppGroup {
|
||||
appGroup, err := s.appGroupRepo.CreateAppGroup(
|
||||
s.T().Context(),
|
||||
fake.It[string](),
|
||||
fake.It[string](),
|
||||
1,
|
||||
)
|
||||
s.Require().NoError(err)
|
||||
return appGroup
|
||||
}
|
||||
Reference in new issue
Block a user