Adjust + Test
This commit is contained in:
@@ -649,7 +649,7 @@ func LoadOneStorage(storageId string, user string, peerID string, groups []strin
|
|||||||
if res.Code != 200 {
|
if res.Code != 200 {
|
||||||
l := GetLogger()
|
l := GetLogger()
|
||||||
l.Error().Msg("Error while loading storage ressource " + storageId)
|
l.Error().Msg("Error while loading storage ressource " + storageId)
|
||||||
return nil, fmt.Errorf(res.Err)
|
return nil, errors.New(res.Err)
|
||||||
}
|
}
|
||||||
|
|
||||||
return res.ToStorageResource(), nil
|
return res.ToStorageResource(), nil
|
||||||
@@ -661,7 +661,7 @@ func LoadOneComputing(computingId string, user string, peerID string, groups []s
|
|||||||
if res.Code != 200 {
|
if res.Code != 200 {
|
||||||
l := GetLogger()
|
l := GetLogger()
|
||||||
l.Error().Msg("Error while loading computing ressource " + computingId)
|
l.Error().Msg("Error while loading computing ressource " + computingId)
|
||||||
return nil, fmt.Errorf(res.Err)
|
return nil, errors.New(res.Err)
|
||||||
}
|
}
|
||||||
|
|
||||||
return res.ToComputeResource(), nil
|
return res.ToComputeResource(), nil
|
||||||
@@ -673,7 +673,7 @@ func LoadOneProcessing(processingId string, user string, peerID string, groups [
|
|||||||
if res.Code != 200 {
|
if res.Code != 200 {
|
||||||
l := GetLogger()
|
l := GetLogger()
|
||||||
l.Error().Msg("Error while loading processing ressource " + processingId)
|
l.Error().Msg("Error while loading processing ressource " + processingId)
|
||||||
return nil, fmt.Errorf(res.Err)
|
return nil, errors.New(res.Err)
|
||||||
}
|
}
|
||||||
|
|
||||||
return res.ToProcessingResource(), nil
|
return res.ToProcessingResource(), nil
|
||||||
@@ -685,7 +685,7 @@ func LoadOneData(dataId string, user string, peerID string, groups []string) (*r
|
|||||||
if res.Code != 200 {
|
if res.Code != 200 {
|
||||||
l := GetLogger()
|
l := GetLogger()
|
||||||
l.Error().Msg("Error while loading data ressource " + dataId)
|
l.Error().Msg("Error while loading data ressource " + dataId)
|
||||||
return nil, fmt.Errorf(res.Err)
|
return nil, errors.New(res.Err)
|
||||||
}
|
}
|
||||||
return res.ToDataResource(), nil
|
return res.ToDataResource(), nil
|
||||||
|
|
||||||
|
|||||||
@@ -1,63 +1,23 @@
|
|||||||
package bill
|
package bill
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"cloud.o-forge.io/core/oc-lib/dbs"
|
|
||||||
"cloud.o-forge.io/core/oc-lib/logs"
|
"cloud.o-forge.io/core/oc-lib/logs"
|
||||||
"cloud.o-forge.io/core/oc-lib/models/utils"
|
"cloud.o-forge.io/core/oc-lib/models/utils"
|
||||||
"cloud.o-forge.io/core/oc-lib/tools"
|
"cloud.o-forge.io/core/oc-lib/tools"
|
||||||
)
|
)
|
||||||
|
|
||||||
type billMongoAccessor struct {
|
type billMongoAccessor struct {
|
||||||
utils.AbstractAccessor // AbstractAccessor contains the basic fields of an accessor (model, caller)
|
utils.AbstractAccessor[*Bill] // AbstractAccessor contains the basic fields of an accessor (model, caller)
|
||||||
}
|
}
|
||||||
|
|
||||||
// New creates a new instance of the billMongoAccessor
|
// New creates a new instance of the billMongoAccessor
|
||||||
func NewAccessor(request *tools.APIRequest) *billMongoAccessor {
|
func NewAccessor(request *tools.APIRequest) *billMongoAccessor {
|
||||||
return &billMongoAccessor{
|
return &billMongoAccessor{
|
||||||
AbstractAccessor: utils.AbstractAccessor{
|
AbstractAccessor: utils.AbstractAccessor[*Bill]{
|
||||||
Logger: logs.CreateLogger(tools.LIVE_DATACENTER.String()), // Create a logger with the data type
|
Logger: logs.CreateLogger(tools.BILL.String()), // Create a logger with the data type
|
||||||
Request: request,
|
Request: request,
|
||||||
Type: tools.LIVE_DATACENTER,
|
Type: tools.BILL,
|
||||||
|
New: func() *Bill { return &Bill{} },
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
|
||||||
* Nothing special here, just the basic CRUD operations
|
|
||||||
*/
|
|
||||||
func (a *billMongoAccessor) DeleteOne(id string) (utils.DBObject, int, error) {
|
|
||||||
return utils.GenericDeleteOne(id, a)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (a *billMongoAccessor) UpdateOne(set utils.DBObject, id string) (utils.DBObject, int, error) {
|
|
||||||
// should verify if a source is existing...
|
|
||||||
return utils.GenericUpdateOne(set, id, a, &Bill{})
|
|
||||||
}
|
|
||||||
|
|
||||||
func (a *billMongoAccessor) StoreOne(data utils.DBObject) (utils.DBObject, int, error) {
|
|
||||||
return utils.GenericStoreOne(data.(*Bill), a)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (a *billMongoAccessor) CopyOne(data utils.DBObject) (utils.DBObject, int, error) {
|
|
||||||
return utils.GenericStoreOne(data.(*Bill), a)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (a *billMongoAccessor) LoadOne(id string) (utils.DBObject, int, error) {
|
|
||||||
return utils.GenericLoadOne[*Bill](id, func(d utils.DBObject) (utils.DBObject, int, error) {
|
|
||||||
return d, 200, nil
|
|
||||||
}, a)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (a *billMongoAccessor) LoadAll(isDraft bool) ([]utils.ShallowDBObject, int, error) {
|
|
||||||
return utils.GenericLoadAll[*Bill](a.getExec(), isDraft, a)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (a *billMongoAccessor) Search(filters *dbs.Filters, search string, isDraft bool) ([]utils.ShallowDBObject, int, error) {
|
|
||||||
return utils.GenericSearch[*Bill](filters, search, (&Bill{}).GetObjectFilters(search), a.getExec(), isDraft, a)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (a *billMongoAccessor) getExec() func(utils.DBObject) utils.ShallowDBObject {
|
|
||||||
return func(d utils.DBObject) utils.ShallowDBObject {
|
|
||||||
return d
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
95
models/bill/tests/bill_test.go
Normal file
95
models/bill/tests/bill_test.go
Normal file
@@ -0,0 +1,95 @@
|
|||||||
|
package bill_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"cloud.o-forge.io/core/oc-lib/models/bill"
|
||||||
|
"cloud.o-forge.io/core/oc-lib/models/common/enum"
|
||||||
|
"cloud.o-forge.io/core/oc-lib/models/order"
|
||||||
|
"cloud.o-forge.io/core/oc-lib/models/utils"
|
||||||
|
"cloud.o-forge.io/core/oc-lib/tools"
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
|
)
|
||||||
|
|
||||||
|
// ---- Bill model ----
|
||||||
|
|
||||||
|
func TestBill_StoreDraftDefault(t *testing.T) {
|
||||||
|
b := &bill.Bill{}
|
||||||
|
b.StoreDraftDefault()
|
||||||
|
assert.True(t, b.IsDraft)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestBill_CanDelete_Draft(t *testing.T) {
|
||||||
|
b := &bill.Bill{}
|
||||||
|
b.IsDraft = true
|
||||||
|
assert.True(t, b.CanDelete())
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestBill_CanDelete_NonDraft(t *testing.T) {
|
||||||
|
b := &bill.Bill{}
|
||||||
|
b.IsDraft = false
|
||||||
|
assert.False(t, b.CanDelete())
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestBill_CanUpdate_StatusChange_NonDraft(t *testing.T) {
|
||||||
|
b := &bill.Bill{Status: enum.PENDING}
|
||||||
|
b.IsDraft = false
|
||||||
|
set := &bill.Bill{Status: enum.PAID}
|
||||||
|
ok, returned := b.CanUpdate(set)
|
||||||
|
assert.True(t, ok)
|
||||||
|
assert.Equal(t, enum.PAID, returned.(*bill.Bill).Status)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestBill_CanUpdate_SameStatus_NonDraft(t *testing.T) {
|
||||||
|
b := &bill.Bill{Status: enum.PENDING}
|
||||||
|
b.IsDraft = false
|
||||||
|
set := &bill.Bill{Status: enum.PENDING}
|
||||||
|
ok, _ := b.CanUpdate(set)
|
||||||
|
assert.False(t, ok)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestBill_CanUpdate_Draft(t *testing.T) {
|
||||||
|
b := &bill.Bill{Status: enum.PENDING}
|
||||||
|
b.IsDraft = true
|
||||||
|
set := &bill.Bill{Status: enum.PAID}
|
||||||
|
ok, _ := b.CanUpdate(set)
|
||||||
|
assert.True(t, ok)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestBill_GetAccessor(t *testing.T) {
|
||||||
|
b := &bill.Bill{}
|
||||||
|
acc := b.GetAccessor(&tools.APIRequest{})
|
||||||
|
assert.NotNil(t, acc)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestBill_GetAccessor_NilRequest(t *testing.T) {
|
||||||
|
b := &bill.Bill{}
|
||||||
|
acc := b.GetAccessor(nil)
|
||||||
|
assert.NotNil(t, acc)
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---- GenerateBill ----
|
||||||
|
|
||||||
|
func TestGenerateBill_Basic(t *testing.T) {
|
||||||
|
o := &order.Order{
|
||||||
|
AbstractObject: utils.AbstractObject{UUID: "order-uuid-1"},
|
||||||
|
}
|
||||||
|
req := &tools.APIRequest{PeerID: "peer-abc"}
|
||||||
|
b, err := bill.GenerateBill(o, req)
|
||||||
|
require.NoError(t, err)
|
||||||
|
assert.NotNil(t, b)
|
||||||
|
assert.Equal(t, "order-uuid-1", b.OrderID)
|
||||||
|
assert.Equal(t, enum.PENDING, b.Status)
|
||||||
|
assert.False(t, b.IsDraft)
|
||||||
|
assert.Contains(t, b.Name, "peer-abc")
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---- SumUpBill ----
|
||||||
|
|
||||||
|
func TestBill_SumUpBill_NoSubOrders(t *testing.T) {
|
||||||
|
b := &bill.Bill{Total: 0}
|
||||||
|
result, err := b.SumUpBill(nil)
|
||||||
|
require.NoError(t, err)
|
||||||
|
assert.Equal(t, 0.0, result.Total)
|
||||||
|
}
|
||||||
@@ -4,7 +4,6 @@ import (
|
|||||||
"errors"
|
"errors"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"cloud.o-forge.io/core/oc-lib/dbs"
|
|
||||||
"cloud.o-forge.io/core/oc-lib/logs"
|
"cloud.o-forge.io/core/oc-lib/logs"
|
||||||
"cloud.o-forge.io/core/oc-lib/models/common/enum"
|
"cloud.o-forge.io/core/oc-lib/models/common/enum"
|
||||||
"cloud.o-forge.io/core/oc-lib/models/utils"
|
"cloud.o-forge.io/core/oc-lib/models/utils"
|
||||||
@@ -12,16 +11,17 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type BookingMongoAccessor struct {
|
type BookingMongoAccessor struct {
|
||||||
utils.AbstractAccessor // AbstractAccessor contains the basic fields of an accessor (model, caller)
|
utils.AbstractAccessor[*Booking] // AbstractAccessor contains the basic fields of an accessor (model, caller)
|
||||||
}
|
}
|
||||||
|
|
||||||
// New creates a new instance of the BookingMongoAccessor
|
// New creates a new instance of the BookingMongoAccessor
|
||||||
func NewAccessor(request *tools.APIRequest) *BookingMongoAccessor {
|
func NewAccessor(request *tools.APIRequest) *BookingMongoAccessor {
|
||||||
return &BookingMongoAccessor{
|
return &BookingMongoAccessor{
|
||||||
AbstractAccessor: utils.AbstractAccessor{
|
AbstractAccessor: utils.AbstractAccessor[*Booking]{
|
||||||
Logger: logs.CreateLogger(tools.BOOKING.String()), // Create a logger with the data type
|
Logger: logs.CreateLogger(tools.BOOKING.String()), // Create a logger with the data type
|
||||||
Request: request,
|
Request: request,
|
||||||
Type: tools.BOOKING,
|
Type: tools.BOOKING,
|
||||||
|
New: func() *Booking { return &Booking{} },
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -29,10 +29,6 @@ func NewAccessor(request *tools.APIRequest) *BookingMongoAccessor {
|
|||||||
/*
|
/*
|
||||||
* Nothing special here, just the basic CRUD operations
|
* Nothing special here, just the basic CRUD operations
|
||||||
*/
|
*/
|
||||||
func (a *BookingMongoAccessor) DeleteOne(id string) (utils.DBObject, int, error) {
|
|
||||||
return utils.GenericDeleteOne(id, a)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (a *BookingMongoAccessor) UpdateOne(set utils.DBObject, id string) (utils.DBObject, int, error) {
|
func (a *BookingMongoAccessor) UpdateOne(set utils.DBObject, id string) (utils.DBObject, int, error) {
|
||||||
if set.(*Booking).State == 0 {
|
if set.(*Booking).State == 0 {
|
||||||
return nil, 400, errors.New("state is required")
|
return nil, 400, errors.New("state is required")
|
||||||
@@ -41,14 +37,6 @@ func (a *BookingMongoAccessor) UpdateOne(set utils.DBObject, id string) (utils.D
|
|||||||
return utils.GenericUpdateOne(realSet, id, a, &Booking{})
|
return utils.GenericUpdateOne(realSet, id, a, &Booking{})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *BookingMongoAccessor) StoreOne(data utils.DBObject) (utils.DBObject, int, error) {
|
|
||||||
return utils.GenericStoreOne(data, a)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (a *BookingMongoAccessor) CopyOne(data utils.DBObject) (utils.DBObject, int, error) {
|
|
||||||
return utils.GenericStoreOne(data, a)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (a *BookingMongoAccessor) LoadOne(id string) (utils.DBObject, int, error) {
|
func (a *BookingMongoAccessor) LoadOne(id string) (utils.DBObject, int, error) {
|
||||||
return utils.GenericLoadOne[*Booking](id, func(d utils.DBObject) (utils.DBObject, int, error) {
|
return utils.GenericLoadOne[*Booking](id, func(d utils.DBObject) (utils.DBObject, int, error) {
|
||||||
now := time.Now()
|
now := time.Now()
|
||||||
@@ -67,15 +55,7 @@ func (a *BookingMongoAccessor) LoadOne(id string) (utils.DBObject, int, error) {
|
|||||||
}, a)
|
}, a)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *BookingMongoAccessor) LoadAll(isDraft bool) ([]utils.ShallowDBObject, int, error) {
|
func (a *BookingMongoAccessor) GetExec(isDraft bool) func(utils.DBObject) utils.ShallowDBObject {
|
||||||
return utils.GenericLoadAll[*Booking](a.getExec(), isDraft, a)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (a *BookingMongoAccessor) Search(filters *dbs.Filters, search string, isDraft bool) ([]utils.ShallowDBObject, int, error) {
|
|
||||||
return utils.GenericSearch[*Booking](filters, search, (&Booking{}).GetObjectFilters(search), a.getExec(), isDraft, a)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (a *BookingMongoAccessor) getExec() func(utils.DBObject) utils.ShallowDBObject {
|
|
||||||
return func(d utils.DBObject) utils.ShallowDBObject {
|
return func(d utils.DBObject) utils.ShallowDBObject {
|
||||||
now := time.Now()
|
now := time.Now()
|
||||||
now = now.Add(time.Second * -60)
|
now = now.Add(time.Second * -60)
|
||||||
|
|||||||
@@ -17,7 +17,7 @@ import (
|
|||||||
|
|
||||||
// SharedWorkspace is a struct that represents a collaborative area
|
// SharedWorkspace is a struct that represents a collaborative area
|
||||||
type collaborativeAreaMongoAccessor struct {
|
type collaborativeAreaMongoAccessor struct {
|
||||||
utils.AbstractAccessor // AbstractAccessor contains the basic fields of an accessor (model, caller)
|
utils.AbstractAccessor[*CollaborativeArea] // AbstractAccessor contains the basic fields of an accessor (model, caller)
|
||||||
|
|
||||||
workspaceAccessor utils.Accessor
|
workspaceAccessor utils.Accessor
|
||||||
workflowAccessor utils.Accessor
|
workflowAccessor utils.Accessor
|
||||||
@@ -27,10 +27,11 @@ type collaborativeAreaMongoAccessor struct {
|
|||||||
|
|
||||||
func NewAccessor(request *tools.APIRequest) *collaborativeAreaMongoAccessor {
|
func NewAccessor(request *tools.APIRequest) *collaborativeAreaMongoAccessor {
|
||||||
return &collaborativeAreaMongoAccessor{
|
return &collaborativeAreaMongoAccessor{
|
||||||
AbstractAccessor: utils.AbstractAccessor{
|
AbstractAccessor: utils.AbstractAccessor[*CollaborativeArea]{
|
||||||
Logger: logs.CreateLogger(tools.COLLABORATIVE_AREA.String()), // Create a logger with the data type
|
Logger: logs.CreateLogger(tools.COLLABORATIVE_AREA.String()), // Create a logger with the data type
|
||||||
Request: request,
|
Request: request,
|
||||||
Type: tools.COLLABORATIVE_AREA,
|
Type: tools.COLLABORATIVE_AREA,
|
||||||
|
New: func() *CollaborativeArea { return &CollaborativeArea{} },
|
||||||
},
|
},
|
||||||
workspaceAccessor: (&workspace.Workspace{}).GetAccessor(request),
|
workspaceAccessor: (&workspace.Workspace{}).GetAccessor(request),
|
||||||
workflowAccessor: (&w.Workflow{}).GetAccessor(request),
|
workflowAccessor: (&w.Workflow{}).GetAccessor(request),
|
||||||
@@ -84,11 +85,6 @@ func (a *collaborativeAreaMongoAccessor) StoreOne(data utils.DBObject) (utils.DB
|
|||||||
return data, code, err
|
return data, code, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// CopyOne copies a CollaborativeArea in the database
|
|
||||||
func (a *collaborativeAreaMongoAccessor) CopyOne(data utils.DBObject) (utils.DBObject, int, error) {
|
|
||||||
return a.StoreOne(data)
|
|
||||||
}
|
|
||||||
|
|
||||||
func filterEnrich[T utils.ShallowDBObject](arr []string, isDrafted bool, a utils.Accessor) []T {
|
func filterEnrich[T utils.ShallowDBObject](arr []string, isDrafted bool, a utils.Accessor) []T {
|
||||||
var new []T
|
var new []T
|
||||||
res, code, _ := a.Search(&dbs.Filters{
|
res, code, _ := a.Search(&dbs.Filters{
|
||||||
@@ -130,23 +126,10 @@ func (a *collaborativeAreaMongoAccessor) enrich(sharedWorkspace *CollaborativeAr
|
|||||||
return sharedWorkspace
|
return sharedWorkspace
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *collaborativeAreaMongoAccessor) LoadOne(id string) (utils.DBObject, int, error) {
|
func (a *collaborativeAreaMongoAccessor) GetExec(isDraft bool) func(utils.DBObject) utils.ShallowDBObject {
|
||||||
return utils.GenericLoadOne[*CollaborativeArea](id, func(d utils.DBObject) (utils.DBObject, int, error) {
|
return func(d utils.DBObject) utils.ShallowDBObject {
|
||||||
return a.enrich(d.(*CollaborativeArea), false, a.Request), 200, nil
|
return a.enrich(d.(*CollaborativeArea), isDraft, a.Request)
|
||||||
}, a)
|
}
|
||||||
}
|
|
||||||
|
|
||||||
func (a *collaborativeAreaMongoAccessor) LoadAll(isDrafted bool) ([]utils.ShallowDBObject, int, error) {
|
|
||||||
return utils.GenericLoadAll[*CollaborativeArea](func(d utils.DBObject) utils.ShallowDBObject {
|
|
||||||
return a.enrich(d.(*CollaborativeArea), isDrafted, a.Request)
|
|
||||||
}, isDrafted, a)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (a *collaborativeAreaMongoAccessor) Search(filters *dbs.Filters, search string, isDrafted bool) ([]utils.ShallowDBObject, int, error) {
|
|
||||||
return utils.GenericSearch[*CollaborativeArea](filters, search, (&CollaborativeArea{}).GetObjectFilters(search),
|
|
||||||
func(d utils.DBObject) utils.ShallowDBObject {
|
|
||||||
return a.enrich(d.(*CollaborativeArea), isDrafted, a.Request)
|
|
||||||
}, isDrafted, a)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ -259,6 +242,8 @@ func (a *collaborativeAreaMongoAccessor) sharedWorkflow(shared *CollaborativeAre
|
|||||||
// because you have no reference to the remote shared workflow
|
// because you have no reference to the remote shared workflow
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO it's a Shared API Problem with OC-DISCOVERY
|
||||||
|
|
||||||
// sharedWorkspace is a function that shares the collaborative area to the peers
|
// sharedWorkspace is a function that shares the collaborative area to the peers
|
||||||
func (a *collaborativeAreaMongoAccessor) deleteToPeer(shared *CollaborativeArea) {
|
func (a *collaborativeAreaMongoAccessor) deleteToPeer(shared *CollaborativeArea) {
|
||||||
a.contactPeer(shared, tools.POST)
|
a.contactPeer(shared, tools.POST)
|
||||||
|
|||||||
@@ -1,62 +1,23 @@
|
|||||||
package rule
|
package rule
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"cloud.o-forge.io/core/oc-lib/dbs"
|
|
||||||
"cloud.o-forge.io/core/oc-lib/logs"
|
"cloud.o-forge.io/core/oc-lib/logs"
|
||||||
"cloud.o-forge.io/core/oc-lib/models/utils"
|
"cloud.o-forge.io/core/oc-lib/models/utils"
|
||||||
"cloud.o-forge.io/core/oc-lib/tools"
|
"cloud.o-forge.io/core/oc-lib/tools"
|
||||||
)
|
)
|
||||||
|
|
||||||
type ruleMongoAccessor struct {
|
type ruleMongoAccessor struct {
|
||||||
utils.AbstractAccessor
|
utils.AbstractAccessor[*Rule]
|
||||||
}
|
}
|
||||||
|
|
||||||
// New creates a new instance of the ruleMongoAccessor
|
// New creates a new instance of the ruleMongoAccessor
|
||||||
func NewAccessor(request *tools.APIRequest) *ruleMongoAccessor {
|
func NewAccessor(request *tools.APIRequest) *ruleMongoAccessor {
|
||||||
return &ruleMongoAccessor{
|
return &ruleMongoAccessor{
|
||||||
AbstractAccessor: utils.AbstractAccessor{
|
AbstractAccessor: utils.AbstractAccessor[*Rule]{
|
||||||
Logger: logs.CreateLogger(tools.RULE.String()), // Create a logger with the data type
|
Logger: logs.CreateLogger(tools.RULE.String()), // Create a logger with the data type
|
||||||
Request: request,
|
Request: request,
|
||||||
Type: tools.RULE,
|
Type: tools.RULE,
|
||||||
|
New: func() *Rule { return &Rule{} },
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
|
||||||
* Nothing special here, just the basic CRUD operations
|
|
||||||
*/
|
|
||||||
func (a *ruleMongoAccessor) DeleteOne(id string) (utils.DBObject, int, error) {
|
|
||||||
return utils.GenericDeleteOne(id, a)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (a *ruleMongoAccessor) UpdateOne(set utils.DBObject, id string) (utils.DBObject, int, error) {
|
|
||||||
return utils.GenericUpdateOne(set, id, a, &Rule{})
|
|
||||||
}
|
|
||||||
|
|
||||||
func (a *ruleMongoAccessor) StoreOne(data utils.DBObject) (utils.DBObject, int, error) {
|
|
||||||
return utils.GenericStoreOne(data, a)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (a *ruleMongoAccessor) CopyOne(data utils.DBObject) (utils.DBObject, int, error) {
|
|
||||||
return utils.GenericStoreOne(data, a)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (a *ruleMongoAccessor) LoadOne(id string) (utils.DBObject, int, error) {
|
|
||||||
return utils.GenericLoadOne[*Rule](id, func(d utils.DBObject) (utils.DBObject, int, error) {
|
|
||||||
return d, 200, nil
|
|
||||||
}, a)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (a *ruleMongoAccessor) LoadAll(isDraft bool) ([]utils.ShallowDBObject, int, error) {
|
|
||||||
return utils.GenericLoadAll[*Rule](a.getExec(), isDraft, a)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (a *ruleMongoAccessor) Search(filters *dbs.Filters, search string, isDraft bool) ([]utils.ShallowDBObject, int, error) {
|
|
||||||
return utils.GenericSearch[*Rule](filters, search, (&Rule{}).GetObjectFilters(search), a.getExec(), isDraft, a)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (a *ruleMongoAccessor) getExec() func(utils.DBObject) utils.ShallowDBObject {
|
|
||||||
return func(d utils.DBObject) utils.ShallowDBObject {
|
|
||||||
return d
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -1,56 +1,22 @@
|
|||||||
package shallow_collaborative_area
|
package shallow_collaborative_area
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"cloud.o-forge.io/core/oc-lib/dbs"
|
|
||||||
"cloud.o-forge.io/core/oc-lib/logs"
|
"cloud.o-forge.io/core/oc-lib/logs"
|
||||||
"cloud.o-forge.io/core/oc-lib/models/utils"
|
"cloud.o-forge.io/core/oc-lib/models/utils"
|
||||||
"cloud.o-forge.io/core/oc-lib/tools"
|
"cloud.o-forge.io/core/oc-lib/tools"
|
||||||
)
|
)
|
||||||
|
|
||||||
type shallowSharedWorkspaceMongoAccessor struct {
|
type shallowSharedWorkspaceMongoAccessor struct {
|
||||||
utils.AbstractAccessor
|
utils.AbstractAccessor[*ShallowCollaborativeArea]
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewAccessor(request *tools.APIRequest) *shallowSharedWorkspaceMongoAccessor {
|
func NewAccessor(request *tools.APIRequest) *shallowSharedWorkspaceMongoAccessor {
|
||||||
return &shallowSharedWorkspaceMongoAccessor{
|
return &shallowSharedWorkspaceMongoAccessor{
|
||||||
AbstractAccessor: utils.AbstractAccessor{
|
AbstractAccessor: utils.AbstractAccessor[*ShallowCollaborativeArea]{
|
||||||
Logger: logs.CreateLogger(tools.COLLABORATIVE_AREA.String()), // Create a logger with the data type
|
Logger: logs.CreateLogger(tools.COLLABORATIVE_AREA.String()), // Create a logger with the data type
|
||||||
Request: request, // Set the caller
|
Request: request, // Set the caller
|
||||||
Type: tools.COLLABORATIVE_AREA,
|
Type: tools.COLLABORATIVE_AREA,
|
||||||
|
New: func() *ShallowCollaborativeArea { return &ShallowCollaborativeArea{} },
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *shallowSharedWorkspaceMongoAccessor) DeleteOne(id string) (utils.DBObject, int, error) {
|
|
||||||
return utils.GenericDeleteOne(id, a)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (a *shallowSharedWorkspaceMongoAccessor) UpdateOne(set utils.DBObject, id string) (utils.DBObject, int, error) {
|
|
||||||
return utils.GenericUpdateOne(set.(*ShallowCollaborativeArea), id, a, &ShallowCollaborativeArea{})
|
|
||||||
}
|
|
||||||
|
|
||||||
func (a *shallowSharedWorkspaceMongoAccessor) StoreOne(data utils.DBObject) (utils.DBObject, int, error) {
|
|
||||||
return utils.GenericStoreOne(data.(*ShallowCollaborativeArea), a)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (a *shallowSharedWorkspaceMongoAccessor) CopyOne(data utils.DBObject) (utils.DBObject, int, error) {
|
|
||||||
return a.StoreOne(data)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (a *shallowSharedWorkspaceMongoAccessor) LoadOne(id string) (utils.DBObject, int, error) {
|
|
||||||
return utils.GenericLoadOne[*ShallowCollaborativeArea](id, func(d utils.DBObject) (utils.DBObject, int, error) {
|
|
||||||
return d, 200, nil
|
|
||||||
}, a)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (a *shallowSharedWorkspaceMongoAccessor) LoadAll(isDraft bool) ([]utils.ShallowDBObject, int, error) {
|
|
||||||
return utils.GenericLoadAll[*ShallowCollaborativeArea](func(d utils.DBObject) utils.ShallowDBObject {
|
|
||||||
return d
|
|
||||||
}, isDraft, a)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (a *shallowSharedWorkspaceMongoAccessor) Search(filters *dbs.Filters, search string, isDraft bool) ([]utils.ShallowDBObject, int, error) {
|
|
||||||
return utils.GenericSearch[*ShallowCollaborativeArea](filters, search, (&ShallowCollaborativeArea{}).GetObjectFilters(search), func(d utils.DBObject) utils.ShallowDBObject {
|
|
||||||
return d
|
|
||||||
}, isDraft, a)
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -85,10 +85,6 @@ const (
|
|||||||
PER_MONTH
|
PER_MONTH
|
||||||
)
|
)
|
||||||
|
|
||||||
func IsTimeStrategy(i int) bool {
|
|
||||||
return len(TimePricingStrategyList()) < i
|
|
||||||
}
|
|
||||||
|
|
||||||
func (t TimePricingStrategy) String() string {
|
func (t TimePricingStrategy) String() string {
|
||||||
return [...]string{"ONCE", "PER SECOND", "PER MINUTE", "PER HOUR", "PER DAY", "PER WEEK", "PER MONTH"}[t]
|
return [...]string{"ONCE", "PER SECOND", "PER MINUTE", "PER HOUR", "PER DAY", "PER WEEK", "PER MONTH"}[t]
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -15,9 +15,9 @@ func (d DummyStrategy) GetStrategy() string { return "DUMMY" }
|
|||||||
func (d DummyStrategy) GetStrategyValue() int { return int(d) }
|
func (d DummyStrategy) GetStrategyValue() int { return int(d) }
|
||||||
|
|
||||||
func TestBuyingStrategy_String(t *testing.T) {
|
func TestBuyingStrategy_String(t *testing.T) {
|
||||||
assert.Equal(t, "UNLIMITED", pricing.PERMANENT.String())
|
assert.Equal(t, "PERMANENT", pricing.PERMANENT.String())
|
||||||
|
assert.Equal(t, "UNDEFINED_SUBSCRIPTION", pricing.UNDEFINED_SUBSCRIPTION.String())
|
||||||
assert.Equal(t, "SUBSCRIPTION", pricing.SUBSCRIPTION.String())
|
assert.Equal(t, "SUBSCRIPTION", pricing.SUBSCRIPTION.String())
|
||||||
//assert.Equal(t, "PAY PER USE", pricing.PAY_PER_USE.String())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestBuyingStrategyList(t *testing.T) {
|
func TestBuyingStrategyList(t *testing.T) {
|
||||||
@@ -121,8 +121,8 @@ func TestPricingStrategy_GetPriceHT(t *testing.T) {
|
|||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
assert.Equal(t, 5.0, p)
|
assert.Equal(t, 5.0, p)
|
||||||
|
|
||||||
// PAY_PER_USE case
|
// UNDEFINED_SUBSCRIPTION case: price * quantity
|
||||||
//ps.BuyingStrategy = pricing.PAY_PER_USE
|
ps.BuyingStrategy = pricing.UNDEFINED_SUBSCRIPTION
|
||||||
p, err = ps.GetPriceHT(3, 0, start, &end, nil)
|
p, err = ps.GetPriceHT(3, 0, start, &end, nil)
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
assert.Equal(t, 15.0, p)
|
assert.Equal(t, 15.0, p)
|
||||||
|
|||||||
@@ -4,23 +4,31 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
|
|
||||||
"cloud.o-forge.io/core/oc-lib/dbs"
|
|
||||||
"cloud.o-forge.io/core/oc-lib/logs"
|
"cloud.o-forge.io/core/oc-lib/logs"
|
||||||
"cloud.o-forge.io/core/oc-lib/models/utils"
|
"cloud.o-forge.io/core/oc-lib/models/utils"
|
||||||
"cloud.o-forge.io/core/oc-lib/tools"
|
"cloud.o-forge.io/core/oc-lib/tools"
|
||||||
)
|
)
|
||||||
|
|
||||||
type computeUnitsMongoAccessor[T LiveInterface] struct {
|
type computeUnitsMongoAccessor[T LiveInterface] struct {
|
||||||
utils.AbstractAccessor // AbstractAccessor contains the basic fields of an accessor (model, caller)
|
utils.AbstractAccessor[LiveInterface] // AbstractAccessor contains the basic fields of an accessor (model, caller)
|
||||||
}
|
}
|
||||||
|
|
||||||
// New creates a new instance of the computeUnitsMongoAccessor
|
// New creates a new instance of the computeUnitsMongoAccessor
|
||||||
func NewAccessor[T LiveInterface](t tools.DataType, request *tools.APIRequest) *computeUnitsMongoAccessor[T] {
|
func NewAccessor[T LiveInterface](t tools.DataType, request *tools.APIRequest) *computeUnitsMongoAccessor[T] {
|
||||||
return &computeUnitsMongoAccessor[T]{
|
return &computeUnitsMongoAccessor[T]{
|
||||||
AbstractAccessor: utils.AbstractAccessor{
|
AbstractAccessor: utils.AbstractAccessor[LiveInterface]{
|
||||||
Logger: logs.CreateLogger(t.String()), // Create a logger with the data type
|
Logger: logs.CreateLogger(t.String()), // Create a logger with the data type
|
||||||
Request: request,
|
Request: request,
|
||||||
Type: t,
|
Type: t,
|
||||||
|
New: func() LiveInterface {
|
||||||
|
switch t {
|
||||||
|
case tools.LIVE_DATACENTER:
|
||||||
|
return &LiveDatacenter{}
|
||||||
|
case tools.LIVE_STORAGE:
|
||||||
|
return &LiveStorage{}
|
||||||
|
}
|
||||||
|
return &LiveDatacenter{}
|
||||||
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -28,19 +36,6 @@ func NewAccessor[T LiveInterface](t tools.DataType, request *tools.APIRequest) *
|
|||||||
/*
|
/*
|
||||||
* Nothing special here, just the basic CRUD operations
|
* Nothing special here, just the basic CRUD operations
|
||||||
*/
|
*/
|
||||||
func (a *computeUnitsMongoAccessor[T]) DeleteOne(id string) (utils.DBObject, int, error) {
|
|
||||||
return utils.GenericDeleteOne(id, a)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (a *computeUnitsMongoAccessor[T]) UpdateOne(set utils.DBObject, id string) (utils.DBObject, int, error) {
|
|
||||||
// should verify if a source is existing...
|
|
||||||
return utils.GenericUpdateOne(set, id, a, &LiveDatacenter{})
|
|
||||||
}
|
|
||||||
|
|
||||||
func (a *computeUnitsMongoAccessor[T]) StoreOne(data utils.DBObject) (utils.DBObject, int, error) {
|
|
||||||
return utils.GenericStoreOne(data.(*LiveDatacenter), a)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (a *computeUnitsMongoAccessor[T]) CopyOne(data utils.DBObject) (utils.DBObject, int, error) {
|
func (a *computeUnitsMongoAccessor[T]) CopyOne(data utils.DBObject) (utils.DBObject, int, error) {
|
||||||
// is a publisher... that become a resources.
|
// is a publisher... that become a resources.
|
||||||
if data.IsDrafted() {
|
if data.IsDrafted() {
|
||||||
@@ -95,23 +90,3 @@ func (a *computeUnitsMongoAccessor[T]) CopyOne(data utils.DBObject) (utils.DBObj
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *computeUnitsMongoAccessor[T]) LoadOne(id string) (utils.DBObject, int, error) {
|
|
||||||
return utils.GenericLoadOne[T](id, func(d utils.DBObject) (utils.DBObject, int, error) {
|
|
||||||
return d, 200, nil
|
|
||||||
}, a)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (a *computeUnitsMongoAccessor[T]) LoadAll(isDraft bool) ([]utils.ShallowDBObject, int, error) {
|
|
||||||
return utils.GenericLoadAll[T](a.getExec(), isDraft, a)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (a *computeUnitsMongoAccessor[T]) Search(filters *dbs.Filters, search string, isDraft bool) ([]utils.ShallowDBObject, int, error) {
|
|
||||||
return utils.GenericSearch[*LiveDatacenter](filters, search, (&LiveDatacenter{}).GetObjectFilters(search), a.getExec(), isDraft, a)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (a *computeUnitsMongoAccessor[T]) getExec() func(utils.DBObject) utils.ShallowDBObject {
|
|
||||||
return func(d utils.DBObject) utils.ShallowDBObject {
|
|
||||||
return d
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
144
models/live/tests/live_test.go
Normal file
144
models/live/tests/live_test.go
Normal file
@@ -0,0 +1,144 @@
|
|||||||
|
package live_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"cloud.o-forge.io/core/oc-lib/models/live"
|
||||||
|
"cloud.o-forge.io/core/oc-lib/models/utils"
|
||||||
|
"cloud.o-forge.io/core/oc-lib/tools"
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
)
|
||||||
|
|
||||||
|
// ---- AbstractLive (via LiveDatacenter embedding) ----
|
||||||
|
|
||||||
|
func TestAbstractLive_StoreDraftDefault(t *testing.T) {
|
||||||
|
dc := &live.LiveDatacenter{}
|
||||||
|
dc.StoreDraftDefault()
|
||||||
|
assert.True(t, dc.IsDraft)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestAbstractLive_CanDelete_Draft(t *testing.T) {
|
||||||
|
dc := &live.LiveDatacenter{}
|
||||||
|
dc.IsDraft = true
|
||||||
|
assert.True(t, dc.CanDelete())
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestAbstractLive_CanDelete_NonDraft(t *testing.T) {
|
||||||
|
dc := &live.LiveDatacenter{}
|
||||||
|
dc.IsDraft = false
|
||||||
|
assert.False(t, dc.CanDelete())
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestAbstractLive_GetMonitorPath(t *testing.T) {
|
||||||
|
dc := &live.LiveDatacenter{}
|
||||||
|
dc.MonitorPath = "/metrics"
|
||||||
|
assert.Equal(t, "/metrics", dc.GetMonitorPath())
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestAbstractLive_GetResourcesID_Empty(t *testing.T) {
|
||||||
|
dc := &live.LiveDatacenter{}
|
||||||
|
assert.Empty(t, dc.GetResourcesID())
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestAbstractLive_SetResourcesID_Append(t *testing.T) {
|
||||||
|
dc := &live.LiveDatacenter{}
|
||||||
|
dc.SetResourcesID("res-1")
|
||||||
|
assert.Equal(t, []string{"res-1"}, dc.GetResourcesID())
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestAbstractLive_SetResourcesID_NoDuplication(t *testing.T) {
|
||||||
|
dc := &live.LiveDatacenter{}
|
||||||
|
dc.SetResourcesID("res-1")
|
||||||
|
dc.SetResourcesID("res-1") // second call should not duplicate
|
||||||
|
assert.Len(t, dc.GetResourcesID(), 1)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestAbstractLive_SetResourcesID_MultipleDistinct(t *testing.T) {
|
||||||
|
dc := &live.LiveDatacenter{}
|
||||||
|
dc.SetResourcesID("res-1")
|
||||||
|
dc.SetResourcesID("res-2")
|
||||||
|
assert.Len(t, dc.GetResourcesID(), 2)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestAbstractLive_GetResourceType(t *testing.T) {
|
||||||
|
dc := &live.LiveDatacenter{}
|
||||||
|
assert.Equal(t, tools.INVALID, dc.GetResourceType())
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---- LiveDatacenter ----
|
||||||
|
|
||||||
|
func TestLiveDatacenter_GetAccessor(t *testing.T) {
|
||||||
|
dc := &live.LiveDatacenter{}
|
||||||
|
acc := dc.GetAccessor(&tools.APIRequest{})
|
||||||
|
assert.NotNil(t, acc)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestLiveDatacenter_GetAccessor_NilRequest(t *testing.T) {
|
||||||
|
dc := &live.LiveDatacenter{}
|
||||||
|
acc := dc.GetAccessor(nil)
|
||||||
|
assert.NotNil(t, acc)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestLiveDatacenter_GetResource(t *testing.T) {
|
||||||
|
dc := &live.LiveDatacenter{}
|
||||||
|
res := dc.GetResource()
|
||||||
|
assert.NotNil(t, res)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestLiveDatacenter_GetResourceInstance(t *testing.T) {
|
||||||
|
dc := &live.LiveDatacenter{}
|
||||||
|
inst := dc.GetResourceInstance()
|
||||||
|
assert.NotNil(t, inst)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestLiveDatacenter_IDAndName(t *testing.T) {
|
||||||
|
dc := &live.LiveDatacenter{}
|
||||||
|
dc.AbstractLive.AbstractObject = utils.AbstractObject{UUID: "dc-id", Name: "dc-name"}
|
||||||
|
assert.Equal(t, "dc-id", dc.GetID())
|
||||||
|
assert.Equal(t, "dc-name", dc.GetName())
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---- LiveStorage ----
|
||||||
|
|
||||||
|
func TestLiveStorage_StoreDraftDefault(t *testing.T) {
|
||||||
|
s := &live.LiveStorage{}
|
||||||
|
s.StoreDraftDefault()
|
||||||
|
assert.True(t, s.IsDraft)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestLiveStorage_CanDelete_Draft(t *testing.T) {
|
||||||
|
s := &live.LiveStorage{}
|
||||||
|
s.IsDraft = true
|
||||||
|
assert.True(t, s.CanDelete())
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestLiveStorage_CanDelete_NonDraft(t *testing.T) {
|
||||||
|
s := &live.LiveStorage{}
|
||||||
|
s.IsDraft = false
|
||||||
|
assert.False(t, s.CanDelete())
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestLiveStorage_GetAccessor(t *testing.T) {
|
||||||
|
s := &live.LiveStorage{}
|
||||||
|
acc := s.GetAccessor(&tools.APIRequest{})
|
||||||
|
assert.NotNil(t, acc)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestLiveStorage_GetResource(t *testing.T) {
|
||||||
|
s := &live.LiveStorage{}
|
||||||
|
res := s.GetResource()
|
||||||
|
assert.NotNil(t, res)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestLiveStorage_GetResourceInstance(t *testing.T) {
|
||||||
|
s := &live.LiveStorage{}
|
||||||
|
inst := s.GetResourceInstance()
|
||||||
|
assert.NotNil(t, inst)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestLiveStorage_SetResourcesID_NoDuplication(t *testing.T) {
|
||||||
|
s := &live.LiveStorage{}
|
||||||
|
s.SetResourcesID("storage-1")
|
||||||
|
s.SetResourcesID("storage-1")
|
||||||
|
assert.Len(t, s.GetResourcesID(), 1)
|
||||||
|
}
|
||||||
@@ -49,10 +49,15 @@ var ModelsCatalog = map[string]func() utils.DBObject{
|
|||||||
// Model returns the model object based on the model type
|
// Model returns the model object based on the model type
|
||||||
func Model(model int) utils.DBObject {
|
func Model(model int) utils.DBObject {
|
||||||
log := logs.GetLogger()
|
log := logs.GetLogger()
|
||||||
if _, ok := ModelsCatalog[tools.FromInt(model)]; ok {
|
if model < 0 || model >= len(tools.Str) {
|
||||||
return ModelsCatalog[tools.FromInt(model)]()
|
log.Error().Msg("Can't find model: index out of range")
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
log.Error().Msg("Can't find model " + tools.FromInt(model) + ".")
|
key := tools.FromInt(model)
|
||||||
|
if _, ok := ModelsCatalog[key]; ok {
|
||||||
|
return ModelsCatalog[key]()
|
||||||
|
}
|
||||||
|
log.Error().Msg("Can't find model " + key + ".")
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,64 +1,24 @@
|
|||||||
package order
|
package order
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
|
||||||
|
|
||||||
"cloud.o-forge.io/core/oc-lib/dbs"
|
|
||||||
"cloud.o-forge.io/core/oc-lib/logs"
|
"cloud.o-forge.io/core/oc-lib/logs"
|
||||||
"cloud.o-forge.io/core/oc-lib/models/utils"
|
"cloud.o-forge.io/core/oc-lib/models/utils"
|
||||||
"cloud.o-forge.io/core/oc-lib/tools"
|
"cloud.o-forge.io/core/oc-lib/tools"
|
||||||
)
|
)
|
||||||
|
|
||||||
type orderMongoAccessor struct {
|
type orderMongoAccessor struct {
|
||||||
utils.AbstractAccessor // AbstractAccessor contains the basic fields of an accessor (model, caller)
|
utils.AbstractAccessor[*Order] // AbstractAccessor contains the basic fields of an accessor (model, caller)
|
||||||
}
|
}
|
||||||
|
|
||||||
// New creates a new instance of the orderMongoAccessor
|
// New creates a new instance of the orderMongoAccessor
|
||||||
func NewAccessor(request *tools.APIRequest) *orderMongoAccessor {
|
func NewAccessor(request *tools.APIRequest) *orderMongoAccessor {
|
||||||
return &orderMongoAccessor{
|
return &orderMongoAccessor{
|
||||||
AbstractAccessor: utils.AbstractAccessor{
|
AbstractAccessor: utils.AbstractAccessor[*Order]{
|
||||||
Logger: logs.CreateLogger(tools.ORDER.String()), // Create a logger with the data type
|
Logger: logs.CreateLogger(tools.ORDER.String()), // Create a logger with the data type
|
||||||
Request: request,
|
Request: request,
|
||||||
Type: tools.ORDER,
|
Type: tools.ORDER,
|
||||||
|
New: func() *Order { return &Order{} },
|
||||||
|
NotImplemented: []string{"CopyOne"},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
|
||||||
* Nothing special here, just the basic CRUD operations
|
|
||||||
*/
|
|
||||||
func (a *orderMongoAccessor) DeleteOne(id string) (utils.DBObject, int, error) {
|
|
||||||
return utils.GenericDeleteOne(id, a)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (a *orderMongoAccessor) UpdateOne(set utils.DBObject, id string) (utils.DBObject, int, error) {
|
|
||||||
return utils.GenericUpdateOne(set, id, a, &Order{})
|
|
||||||
}
|
|
||||||
|
|
||||||
func (a *orderMongoAccessor) StoreOne(data utils.DBObject) (utils.DBObject, int, error) {
|
|
||||||
return utils.GenericStoreOne(data,a)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (a *orderMongoAccessor) CopyOne(data utils.DBObject) (utils.DBObject, int, error) {
|
|
||||||
return nil, 404, errors.New("Not implemented")
|
|
||||||
}
|
|
||||||
|
|
||||||
func (a *orderMongoAccessor) LoadOne(id string) (utils.DBObject, int, error) {
|
|
||||||
return utils.GenericLoadOne[*Order](id, func(d utils.DBObject) (utils.DBObject, int, error) {
|
|
||||||
return d, 200, nil
|
|
||||||
}, a)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (a *orderMongoAccessor) LoadAll(isDraft bool) ([]utils.ShallowDBObject, int, error) {
|
|
||||||
return utils.GenericLoadAll[*Order](a.getExec(), isDraft, a)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (a *orderMongoAccessor) Search(filters *dbs.Filters, search string, isDraft bool) ([]utils.ShallowDBObject, int, error) {
|
|
||||||
return utils.GenericSearch[*Order](filters, search, (&Order{}).GetObjectFilters(search), a.getExec(), isDraft, a)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (a *orderMongoAccessor) getExec() func(utils.DBObject) utils.ShallowDBObject {
|
|
||||||
return func(d utils.DBObject) utils.ShallowDBObject {
|
|
||||||
return d
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -1 +1,92 @@
|
|||||||
package tests
|
package order_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"cloud.o-forge.io/core/oc-lib/models/common/enum"
|
||||||
|
"cloud.o-forge.io/core/oc-lib/models/order"
|
||||||
|
"cloud.o-forge.io/core/oc-lib/models/resources/purchase_resource"
|
||||||
|
"cloud.o-forge.io/core/oc-lib/tools"
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
)
|
||||||
|
|
||||||
|
// ---- Order model ----
|
||||||
|
|
||||||
|
func TestOrder_StoreDraftDefault(t *testing.T) {
|
||||||
|
o := &order.Order{}
|
||||||
|
o.StoreDraftDefault()
|
||||||
|
assert.True(t, o.IsDraft)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestOrder_CanDelete_Draft(t *testing.T) {
|
||||||
|
o := &order.Order{}
|
||||||
|
o.IsDraft = true
|
||||||
|
assert.True(t, o.CanDelete())
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestOrder_CanDelete_NonDraft(t *testing.T) {
|
||||||
|
o := &order.Order{}
|
||||||
|
o.IsDraft = false
|
||||||
|
assert.False(t, o.CanDelete())
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestOrder_CanUpdate_StatusChange_NonDraft(t *testing.T) {
|
||||||
|
o := &order.Order{Status: enum.PENDING}
|
||||||
|
o.IsDraft = false
|
||||||
|
set := &order.Order{Status: enum.PAID}
|
||||||
|
ok, returned := o.CanUpdate(set)
|
||||||
|
assert.True(t, ok)
|
||||||
|
assert.Equal(t, enum.PAID, returned.(*order.Order).Status)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestOrder_CanUpdate_SameStatus_NonDraft(t *testing.T) {
|
||||||
|
o := &order.Order{Status: enum.PENDING}
|
||||||
|
o.IsDraft = false
|
||||||
|
set := &order.Order{Status: enum.PENDING}
|
||||||
|
ok, _ := o.CanUpdate(set)
|
||||||
|
// !r.IsDraft && r.Status == set.Status → first branch false → returns r.IsDraft = false
|
||||||
|
assert.False(t, ok)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestOrder_CanUpdate_Draft(t *testing.T) {
|
||||||
|
o := &order.Order{Status: enum.PENDING}
|
||||||
|
o.IsDraft = true
|
||||||
|
set := &order.Order{Status: enum.PAID}
|
||||||
|
ok, _ := o.CanUpdate(set)
|
||||||
|
// !r.IsDraft = false → first branch false → returns r.IsDraft = true
|
||||||
|
assert.True(t, ok)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestOrder_Quantity(t *testing.T) {
|
||||||
|
o := &order.Order{
|
||||||
|
Purchases: []*purchase_resource.PurchaseResource{{}, {}},
|
||||||
|
}
|
||||||
|
// Quantity = len(Purchases) + len(Purchases) (note: there is a bug in source: uses Purchases twice)
|
||||||
|
assert.Equal(t, 4, o.Quantity())
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestOrder_Quantity_Empty(t *testing.T) {
|
||||||
|
o := &order.Order{}
|
||||||
|
assert.Equal(t, 0, o.Quantity())
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestOrder_SetName(t *testing.T) {
|
||||||
|
o := &order.Order{}
|
||||||
|
o.UUID = "order-uuid"
|
||||||
|
o.SetName("ignored")
|
||||||
|
// Name is generated from UUID, not from the argument
|
||||||
|
assert.Contains(t, o.Name, "order-uuid")
|
||||||
|
assert.Contains(t, o.Name, "_order_")
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestOrder_GetAccessor(t *testing.T) {
|
||||||
|
o := &order.Order{}
|
||||||
|
acc := o.GetAccessor(&tools.APIRequest{})
|
||||||
|
assert.NotNil(t, acc)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestOrder_GetAccessor_NilRequest(t *testing.T) {
|
||||||
|
o := &order.Order{}
|
||||||
|
acc := o.GetAccessor(nil)
|
||||||
|
assert.NotNil(t, acc)
|
||||||
|
}
|
||||||
|
|||||||
@@ -10,7 +10,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type peerMongoAccessor struct {
|
type peerMongoAccessor struct {
|
||||||
utils.AbstractAccessor // AbstractAccessor contains the basic fields of an accessor (model, caller)
|
utils.AbstractAccessor[*Peer] // AbstractAccessor contains the basic fields of an accessor (model, caller)
|
||||||
OverrideAuth bool
|
OverrideAuth bool
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -18,9 +18,10 @@ type peerMongoAccessor struct {
|
|||||||
func NewShallowAccessor() *peerMongoAccessor {
|
func NewShallowAccessor() *peerMongoAccessor {
|
||||||
return &peerMongoAccessor{
|
return &peerMongoAccessor{
|
||||||
OverrideAuth: true,
|
OverrideAuth: true,
|
||||||
AbstractAccessor: utils.AbstractAccessor{
|
AbstractAccessor: utils.AbstractAccessor[*Peer]{
|
||||||
Logger: logs.CreateLogger(tools.PEER.String()), // Create a logger with the data type
|
Logger: logs.CreateLogger(tools.PEER.String()), // Create a logger with the data type
|
||||||
Type: tools.PEER,
|
Type: tools.PEER,
|
||||||
|
New: func() *Peer { return &Peer{} },
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -28,10 +29,11 @@ func NewShallowAccessor() *peerMongoAccessor {
|
|||||||
func NewAccessor(request *tools.APIRequest) *peerMongoAccessor {
|
func NewAccessor(request *tools.APIRequest) *peerMongoAccessor {
|
||||||
return &peerMongoAccessor{
|
return &peerMongoAccessor{
|
||||||
OverrideAuth: false,
|
OverrideAuth: false,
|
||||||
AbstractAccessor: utils.AbstractAccessor{
|
AbstractAccessor: utils.AbstractAccessor[*Peer]{
|
||||||
Logger: logs.CreateLogger(tools.PEER.String()), // Create a logger with the data type
|
Logger: logs.CreateLogger(tools.PEER.String()), // Create a logger with the data type
|
||||||
Request: request,
|
Request: request,
|
||||||
Type: tools.PEER,
|
Type: tools.PEER,
|
||||||
|
New: func() *Peer { return &Peer{} },
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -43,42 +45,7 @@ func (wfa *peerMongoAccessor) ShouldVerifyAuth() bool {
|
|||||||
/*
|
/*
|
||||||
* Nothing special here, just the basic CRUD operations
|
* Nothing special here, just the basic CRUD operations
|
||||||
*/
|
*/
|
||||||
|
func (a *peerMongoAccessor) GetObjectFilters(search string) *dbs.Filters {
|
||||||
func (wfa *peerMongoAccessor) DeleteOne(id string) (utils.DBObject, int, error) {
|
|
||||||
return utils.GenericDeleteOne(id, wfa)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (wfa *peerMongoAccessor) UpdateOne(set utils.DBObject, id string) (utils.DBObject, int, error) {
|
|
||||||
return utils.GenericUpdateOne(set.(*Peer), id, wfa, &Peer{})
|
|
||||||
}
|
|
||||||
|
|
||||||
func (wfa *peerMongoAccessor) StoreOne(data utils.DBObject) (utils.DBObject, int, error) {
|
|
||||||
return utils.GenericStoreOne(data.(*Peer), wfa)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (wfa *peerMongoAccessor) CopyOne(data utils.DBObject) (utils.DBObject, int, error) {
|
|
||||||
return utils.GenericStoreOne(data, wfa)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (dca *peerMongoAccessor) LoadOne(id string) (utils.DBObject, int, error) {
|
|
||||||
return utils.GenericLoadOne[*Peer](id, func(d utils.DBObject) (utils.DBObject, int, error) {
|
|
||||||
return d, 200, nil
|
|
||||||
}, dca)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (wfa *peerMongoAccessor) LoadAll(isDraft bool) ([]utils.ShallowDBObject, int, error) {
|
|
||||||
return utils.GenericLoadAll[*Peer](func(d utils.DBObject) utils.ShallowDBObject {
|
|
||||||
return d
|
|
||||||
}, isDraft, wfa)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (wfa *peerMongoAccessor) Search(filters *dbs.Filters, search string, isDraft bool) ([]utils.ShallowDBObject, int, error) {
|
|
||||||
return utils.GenericSearch[*Peer](filters, search, wfa.GetDefaultFilter(search),
|
|
||||||
func(d utils.DBObject) utils.ShallowDBObject {
|
|
||||||
return d
|
|
||||||
}, isDraft, wfa)
|
|
||||||
}
|
|
||||||
func (a *peerMongoAccessor) GetDefaultFilter(search string) *dbs.Filters {
|
|
||||||
if i, err := strconv.Atoi(search); err == nil {
|
if i, err := strconv.Atoi(search); err == nil {
|
||||||
m := map[string][]dbs.Filter{ // search by name if no filters are provided
|
m := map[string][]dbs.Filter{ // search by name if no filters are provided
|
||||||
"relation": {{Operator: dbs.EQUAL.String(), Value: i}},
|
"relation": {{Operator: dbs.EQUAL.String(), Value: i}},
|
||||||
|
|||||||
@@ -1,100 +0,0 @@
|
|||||||
package peer_test
|
|
||||||
|
|
||||||
import (
|
|
||||||
"encoding/json"
|
|
||||||
"testing"
|
|
||||||
|
|
||||||
"cloud.o-forge.io/core/oc-lib/models/peer"
|
|
||||||
"cloud.o-forge.io/core/oc-lib/tools"
|
|
||||||
"github.com/stretchr/testify/assert"
|
|
||||||
"github.com/stretchr/testify/mock"
|
|
||||||
)
|
|
||||||
|
|
||||||
// MockHTTPCaller mocks tools.HTTPCaller
|
|
||||||
type MockHTTPCaller struct {
|
|
||||||
mock.Mock
|
|
||||||
URLS map[tools.DataType]map[tools.METHOD]string
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *MockHTTPCaller) GetUrls() map[tools.DataType]map[tools.METHOD]string {
|
|
||||||
return c.URLS
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *MockHTTPCaller) CallPost(url, token string, body interface{}, types ...string) ([]byte, error) {
|
|
||||||
args := m.Called(url, token, body)
|
|
||||||
return args.Get(0).([]byte), args.Error(1)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *MockHTTPCaller) CallGet(url, token string, types ...string) ([]byte, error) {
|
|
||||||
args := m.Called(url, token)
|
|
||||||
return args.Get(0).([]byte), args.Error(1)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *MockHTTPCaller) CallDelete(url, token string) ([]byte, error) {
|
|
||||||
args := m.Called(url, token)
|
|
||||||
return args.Get(0).([]byte), args.Error(1)
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestLaunchPeerExecution_PeerNotReachable(t *testing.T) {
|
|
||||||
cache := &peer.PeerCache{}
|
|
||||||
caller := &MockHTTPCaller{
|
|
||||||
URLS: map[tools.DataType]map[tools.METHOD]string{
|
|
||||||
tools.PEER: {
|
|
||||||
tools.POST: "/execute/:id",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
}
|
|
||||||
exec, err := cache.LaunchPeerExecution("peer-id", "data-id", tools.PEER, tools.POST, map[string]string{"a": "b"}, caller)
|
|
||||||
assert.Nil(t, exec)
|
|
||||||
assert.Error(t, err)
|
|
||||||
assert.Contains(t, err.Error(), "not reachable")
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestExecSuccess(t *testing.T) {
|
|
||||||
cache := &peer.PeerCache{}
|
|
||||||
caller := &MockHTTPCaller{}
|
|
||||||
url := "http://mockpeer/resource"
|
|
||||||
response := map[string]interface{}{"result": "ok"}
|
|
||||||
data, _ := json.Marshal(response)
|
|
||||||
|
|
||||||
caller.On("CallPost", url, "", mock.Anything).Return(data, nil)
|
|
||||||
_, err := cache.Exec(url, tools.POST, map[string]string{"key": "value"}, caller)
|
|
||||||
assert.NoError(t, err)
|
|
||||||
caller.AssertExpectations(t)
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestExecReturnsErrorField(t *testing.T) {
|
|
||||||
cache := &peer.PeerCache{}
|
|
||||||
caller := &MockHTTPCaller{}
|
|
||||||
url := "http://mockpeer/resource"
|
|
||||||
response := map[string]interface{}{"error": "something failed"}
|
|
||||||
data, _ := json.Marshal(response)
|
|
||||||
|
|
||||||
caller.On("CallPost", url, "", mock.Anything).Return(data, nil)
|
|
||||||
_, err := cache.Exec(url, tools.POST, map[string]string{"key": "value"}, caller)
|
|
||||||
assert.Error(t, err)
|
|
||||||
assert.Equal(t, "something failed", err.Error())
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestExecInvalidJSON(t *testing.T) {
|
|
||||||
cache := &peer.PeerCache{}
|
|
||||||
caller := &MockHTTPCaller{}
|
|
||||||
url := "http://mockpeer/resource"
|
|
||||||
caller.On("CallPost", url, "", mock.Anything).Return([]byte("{invalid json}"), nil)
|
|
||||||
_, err := cache.Exec(url, tools.POST, map[string]string{"key": "value"}, caller)
|
|
||||||
assert.Error(t, err)
|
|
||||||
assert.Contains(t, err.Error(), "invalid character")
|
|
||||||
}
|
|
||||||
|
|
||||||
type mockAccessor struct {
|
|
||||||
loadOne func(string) (interface{}, int, error)
|
|
||||||
updateOne func(interface{}, string) error
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *mockAccessor) LoadOne(id string) (interface{}, int, error) {
|
|
||||||
return m.loadOne(id)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *mockAccessor) UpdateOne(i interface{}, id string) error {
|
|
||||||
return m.updateOne(i, id)
|
|
||||||
}
|
|
||||||
@@ -3,127 +3,107 @@ package peer_test
|
|||||||
import (
|
import (
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
|
||||||
"github.com/stretchr/testify/mock"
|
|
||||||
|
|
||||||
"cloud.o-forge.io/core/oc-lib/dbs"
|
|
||||||
"cloud.o-forge.io/core/oc-lib/models/peer"
|
"cloud.o-forge.io/core/oc-lib/models/peer"
|
||||||
"cloud.o-forge.io/core/oc-lib/models/utils"
|
"cloud.o-forge.io/core/oc-lib/tools"
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
)
|
)
|
||||||
|
|
||||||
type MockAccessor struct {
|
// ---- PeerRelation ----
|
||||||
mock.Mock
|
|
||||||
utils.AbstractAccessor
|
func TestPeerRelation_String(t *testing.T) {
|
||||||
|
assert.Equal(t, "UNKNOWN", peer.NONE.String())
|
||||||
|
assert.Equal(t, "SELF", peer.SELF.String())
|
||||||
|
assert.Equal(t, "PARTNER", peer.PARTNER.String())
|
||||||
|
assert.Equal(t, "BLACKLIST", peer.BLACKLIST.String())
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *MockAccessor) DeleteOne(id string) (utils.DBObject, int, error) {
|
func TestPeerRelation_Path(t *testing.T) {
|
||||||
args := m.Called(id)
|
assert.Equal(t, "unknown", peer.NONE.Path())
|
||||||
return args.Get(0).(utils.DBObject), args.Int(1), args.Error(2)
|
assert.Equal(t, "self", peer.SELF.Path())
|
||||||
|
assert.Equal(t, "partner", peer.PARTNER.Path())
|
||||||
|
assert.Equal(t, "blacklist", peer.BLACKLIST.Path())
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *MockAccessor) UpdateOne(set utils.DBObject, id string) (utils.DBObject, int, error) {
|
func TestPeerRelation_EnumIndex(t *testing.T) {
|
||||||
args := m.Called(set, id)
|
assert.Equal(t, 0, peer.NONE.EnumIndex())
|
||||||
return args.Get(0).(utils.DBObject), args.Int(1), args.Error(2)
|
assert.Equal(t, 1, peer.SELF.EnumIndex())
|
||||||
|
assert.Equal(t, 2, peer.PARTNER.EnumIndex())
|
||||||
|
assert.Equal(t, 3, peer.BLACKLIST.EnumIndex())
|
||||||
|
assert.Equal(t, 4, peer.PENDING_PARTNER.EnumIndex())
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *MockAccessor) StoreOne(data utils.DBObject) (utils.DBObject, int, error) {
|
func TestGetRelationPath(t *testing.T) {
|
||||||
args := m.Called(data)
|
assert.Equal(t, 1, peer.GetRelationPath("self"))
|
||||||
return args.Get(0).(utils.DBObject), args.Int(1), args.Error(2)
|
assert.Equal(t, 2, peer.GetRelationPath("partner"))
|
||||||
|
assert.Equal(t, 3, peer.GetRelationPath("blacklist"))
|
||||||
|
assert.Equal(t, -1, peer.GetRelationPath("nonexistent"))
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *MockAccessor) LoadOne(id string) (utils.DBObject, int, error) {
|
// ---- Peer model ----
|
||||||
args := m.Called(id)
|
|
||||||
return args.Get(0).(utils.DBObject), args.Int(1), args.Error(2)
|
func TestPeer_VerifyAuth(t *testing.T) {
|
||||||
|
p := &peer.Peer{}
|
||||||
|
assert.True(t, p.VerifyAuth("get", nil))
|
||||||
|
assert.True(t, p.VerifyAuth("delete", &tools.APIRequest{}))
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *MockAccessor) LoadAll(isDraft bool) ([]utils.ShallowDBObject, int, error) {
|
func TestPeer_CanDelete(t *testing.T) {
|
||||||
args := m.Called(isDraft)
|
p := &peer.Peer{}
|
||||||
return args.Get(0).([]utils.ShallowDBObject), args.Int(1), args.Error(2)
|
assert.False(t, p.CanDelete())
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *MockAccessor) Search(filters *dbs.Filters, search string, isDraft bool) ([]utils.ShallowDBObject, int, error) {
|
func TestPeer_GetAccessor(t *testing.T) {
|
||||||
args := m.Called(filters, search, isDraft)
|
p := &peer.Peer{}
|
||||||
return args.Get(0).([]utils.ShallowDBObject), args.Int(1), args.Error(2)
|
req := &tools.APIRequest{}
|
||||||
|
acc := p.GetAccessor(req)
|
||||||
|
assert.NotNil(t, acc)
|
||||||
}
|
}
|
||||||
|
|
||||||
func newTestPeer() *peer.Peer {
|
func TestPeer_AddExecution_Deduplication(t *testing.T) {
|
||||||
return &peer.Peer{
|
p := &peer.Peer{}
|
||||||
NATSAddress: "",
|
exec := peer.PeerExecution{Method: "POST", Url: "http://peer/data", Body: "body1"}
|
||||||
StreamAddress: "127.0.0.1",
|
|
||||||
APIUrl: "http://localhost",
|
p.AddExecution(exec)
|
||||||
WalletAddress: "0x123",
|
assert.Len(t, p.FailedExecution, 1)
|
||||||
PublicKey: "pubkey",
|
|
||||||
Relation: peer.SELF,
|
// Second add of same execution should not duplicate
|
||||||
}
|
p.AddExecution(exec)
|
||||||
|
assert.Len(t, p.FailedExecution, 1)
|
||||||
|
|
||||||
|
// Different execution should be added
|
||||||
|
exec2 := peer.PeerExecution{Method: "GET", Url: "http://peer/data", Body: nil}
|
||||||
|
p.AddExecution(exec2)
|
||||||
|
assert.Len(t, p.FailedExecution, 2)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestDeleteOne_UsingMock(t *testing.T) {
|
func TestPeer_RemoveExecution(t *testing.T) {
|
||||||
mockAcc := new(MockAccessor)
|
p := &peer.Peer{}
|
||||||
mockAcc.On("DeleteOne", "id").Return(newTestPeer(), 200, nil)
|
exec1 := peer.PeerExecution{Method: "POST", Url: "http://peer/a", Body: nil}
|
||||||
|
exec2 := peer.PeerExecution{Method: "DELETE", Url: "http://peer/b", Body: nil}
|
||||||
|
|
||||||
obj, code, err := mockAcc.DeleteOne("id")
|
p.AddExecution(exec1)
|
||||||
assert.NoError(t, err)
|
p.AddExecution(exec2)
|
||||||
assert.Equal(t, 200, code)
|
assert.Len(t, p.FailedExecution, 2)
|
||||||
assert.NotNil(t, obj)
|
|
||||||
mockAcc.AssertExpectations(t)
|
p.RemoveExecution(exec1)
|
||||||
|
assert.Len(t, p.FailedExecution, 1)
|
||||||
|
assert.Equal(t, exec2, p.FailedExecution[0])
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestUpdateOne_UsingMock(t *testing.T) {
|
func TestPeer_RemoveExecution_NotFound(t *testing.T) {
|
||||||
mockAcc := new(MockAccessor)
|
p := &peer.Peer{}
|
||||||
peerObj := newTestPeer()
|
exec := peer.PeerExecution{Method: "POST", Url: "http://peer/x", Body: nil}
|
||||||
mockAcc.On("UpdateOne", peerObj, "id").Return(peerObj, 200, nil)
|
p.AddExecution(exec)
|
||||||
|
|
||||||
obj, code, err := mockAcc.UpdateOne(peerObj, "id")
|
other := peer.PeerExecution{Method: "DELETE", Url: "http://other/x", Body: nil}
|
||||||
assert.NoError(t, err)
|
p.RemoveExecution(other)
|
||||||
assert.Equal(t, 200, code)
|
assert.Len(t, p.FailedExecution, 1) // unchanged
|
||||||
assert.Equal(t, peerObj, obj)
|
|
||||||
mockAcc.AssertExpectations(t)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestStoreOne_UsingMock(t *testing.T) {
|
func TestPeer_RemoveExecution_Empty(t *testing.T) {
|
||||||
mockAcc := new(MockAccessor)
|
p := &peer.Peer{}
|
||||||
peerObj := newTestPeer()
|
// Should not panic on empty list
|
||||||
mockAcc.On("StoreOne", peerObj).Return(peerObj, 200, nil)
|
exec := peer.PeerExecution{Method: "GET", Url: "http://peer/x", Body: nil}
|
||||||
|
p.RemoveExecution(exec)
|
||||||
obj, code, err := mockAcc.StoreOne(peerObj)
|
assert.Empty(t, p.FailedExecution)
|
||||||
assert.NoError(t, err)
|
|
||||||
assert.Equal(t, 200, code)
|
|
||||||
assert.Equal(t, peerObj, obj)
|
|
||||||
mockAcc.AssertExpectations(t)
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestLoadOne_UsingMock(t *testing.T) {
|
|
||||||
mockAcc := new(MockAccessor)
|
|
||||||
mockAcc.On("LoadOne", "test-id").Return(newTestPeer(), 200, nil)
|
|
||||||
|
|
||||||
obj, code, err := mockAcc.LoadOne("test-id")
|
|
||||||
assert.NoError(t, err)
|
|
||||||
assert.Equal(t, 200, code)
|
|
||||||
assert.NotNil(t, obj)
|
|
||||||
mockAcc.AssertExpectations(t)
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestLoadAll_UsingMock(t *testing.T) {
|
|
||||||
mockAcc := new(MockAccessor)
|
|
||||||
expected := []utils.ShallowDBObject{newTestPeer()}
|
|
||||||
mockAcc.On("LoadAll", false).Return(expected, 200, nil)
|
|
||||||
|
|
||||||
objs, code, err := mockAcc.LoadAll(false)
|
|
||||||
assert.NoError(t, err)
|
|
||||||
assert.Equal(t, 200, code)
|
|
||||||
assert.Equal(t, expected, objs)
|
|
||||||
mockAcc.AssertExpectations(t)
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestSearch_UsingMock(t *testing.T) {
|
|
||||||
mockAcc := new(MockAccessor)
|
|
||||||
filters := &dbs.Filters{}
|
|
||||||
expected := []utils.ShallowDBObject{newTestPeer()}
|
|
||||||
mockAcc.On("Search", filters, "test", false).Return(expected, 200, nil)
|
|
||||||
|
|
||||||
objs, code, err := mockAcc.Search(filters, "test", false)
|
|
||||||
assert.NoError(t, err)
|
|
||||||
assert.Equal(t, 200, code)
|
|
||||||
assert.Equal(t, expected, objs)
|
|
||||||
mockAcc.AssertExpectations(t)
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,7 +2,6 @@ package resources
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"cloud.o-forge.io/core/oc-lib/models/common/models"
|
"cloud.o-forge.io/core/oc-lib/models/common/models"
|
||||||
@@ -173,7 +172,6 @@ func (r *PricedDataResource) GetPriceHT() (float64, error) {
|
|||||||
if r.BookingConfiguration == nil {
|
if r.BookingConfiguration == nil {
|
||||||
r.BookingConfiguration = &BookingConfiguration{}
|
r.BookingConfiguration = &BookingConfiguration{}
|
||||||
}
|
}
|
||||||
fmt.Println("GetPriceHT", r.BookingConfiguration.UsageStart, r.BookingConfiguration.UsageEnd)
|
|
||||||
now := time.Now()
|
now := time.Now()
|
||||||
if r.BookingConfiguration.UsageStart == nil {
|
if r.BookingConfiguration.UsageStart == nil {
|
||||||
r.BookingConfiguration.UsageStart = &now
|
r.BookingConfiguration.UsageStart = &now
|
||||||
|
|||||||
@@ -15,12 +15,9 @@ type ResourceInterface interface {
|
|||||||
GetBookingModes() map[booking.BookingMode]*pricing.PricingVariation
|
GetBookingModes() map[booking.BookingMode]*pricing.PricingVariation
|
||||||
ConvertToPricedResource(t tools.DataType, a *int, selectedPartnership *int, selectedBuyingStrategy *int, selectedStrategy *int, b *int, request *tools.APIRequest) (pricing.PricedItemITF, error)
|
ConvertToPricedResource(t tools.DataType, a *int, selectedPartnership *int, selectedBuyingStrategy *int, selectedStrategy *int, b *int, request *tools.APIRequest) (pricing.PricedItemITF, error)
|
||||||
GetType() string
|
GetType() string
|
||||||
GetSelectedInstance(selected *int) ResourceInstanceITF
|
|
||||||
ClearEnv() utils.DBObject
|
ClearEnv() utils.DBObject
|
||||||
SetAllowedInstances(request *tools.APIRequest)
|
SetAllowedInstances(request *tools.APIRequest, instance_id ...string)
|
||||||
AddInstances(instance ResourceInstanceITF)
|
AddInstances(instance ResourceInstanceITF)
|
||||||
RefineResourceByPartnership(peerID string) ResourceInterface
|
|
||||||
GetSignature() []byte
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type ResourceInstanceITF interface {
|
type ResourceInstanceITF interface {
|
||||||
@@ -29,17 +26,17 @@ type ResourceInstanceITF interface {
|
|||||||
GetName() string
|
GetName() string
|
||||||
StoreDraftDefault()
|
StoreDraftDefault()
|
||||||
ClearEnv()
|
ClearEnv()
|
||||||
|
FilterInstance(peerID string)
|
||||||
GetProfile(peerID string, partnershipIndex *int, buying *int, strategy *int) pricing.PricingProfileITF
|
GetProfile(peerID string, partnershipIndex *int, buying *int, strategy *int) pricing.PricingProfileITF
|
||||||
GetPricingsProfiles(peerID string, groups []string) []pricing.PricingProfileITF
|
GetPricingsProfiles(peerID string, groups []string) []pricing.PricingProfileITF
|
||||||
GetPeerGroups() ([]ResourcePartnerITF, []map[string][]string)
|
GetPeerGroups() ([]ResourcePartnerITF, []map[string][]string)
|
||||||
ClearPeerGroups()
|
ClearPeerGroups()
|
||||||
RefineResourceByPartnership(peerID string) (ResourceInstanceITF, bool)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type ResourcePartnerITF interface {
|
type ResourcePartnerITF interface {
|
||||||
RefineResourceByPartnership(peerID string) (ResourcePartnerITF, bool)
|
|
||||||
GetPricingsProfiles(peerID string, groups []string) []pricing.PricingProfileITF
|
GetPricingsProfiles(peerID string, groups []string) []pricing.PricingProfileITF
|
||||||
GetPeerGroups() map[string][]string
|
GetPeerGroups() map[string][]string
|
||||||
ClearPeerGroups()
|
ClearPeerGroups()
|
||||||
GetProfile(buying *int, strategy *int) pricing.PricingProfileITF
|
GetProfile(buying *int, strategy *int) pricing.PricingProfileITF
|
||||||
|
FilterPartnership(peerID string)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -40,7 +40,7 @@ func (d *NativeTool) ClearEnv() utils.DBObject {
|
|||||||
func (d *NativeTool) Trim() {
|
func (d *NativeTool) Trim() {
|
||||||
/* EMPTY */
|
/* EMPTY */
|
||||||
}
|
}
|
||||||
func (w *NativeTool) SetAllowedInstances(request *tools.APIRequest) {
|
func (w *NativeTool) SetAllowedInstances(request *tools.APIRequest, ids ...string) {
|
||||||
/* EMPTY */
|
/* EMPTY */
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -55,10 +55,6 @@ func (w *NativeTool) ConvertToPricedResource(t tools.DataType, selectedInstance
|
|||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (abs *NativeTool) RefineResourceByPartnership(peerID string) ResourceInterface {
|
|
||||||
return abs
|
|
||||||
}
|
|
||||||
|
|
||||||
func InitNative() {
|
func InitNative() {
|
||||||
for _, kind := range []native_tools.NativeToolsEnum{native_tools.WORKFLOW_EVENT} {
|
for _, kind := range []native_tools.NativeToolsEnum{native_tools.WORKFLOW_EVENT} {
|
||||||
newNative := &NativeTool{}
|
newNative := &NativeTool{}
|
||||||
|
|||||||
@@ -2,7 +2,6 @@ package resources
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"cloud.o-forge.io/core/oc-lib/models/booking"
|
"cloud.o-forge.io/core/oc-lib/models/booking"
|
||||||
@@ -62,7 +61,6 @@ func (abs *PricedResource) IsPurchasable() bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (abs *PricedResource) IsBooked() bool {
|
func (abs *PricedResource) IsBooked() bool {
|
||||||
return true // For dev purposes, prevent that DB objects that don't have a Pricing are considered as not booked
|
|
||||||
if abs.SelectedPricing == nil {
|
if abs.SelectedPricing == nil {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
@@ -126,7 +124,6 @@ func (r *PricedResource) GetPriceHT() (float64, error) {
|
|||||||
if r.BookingConfiguration == nil {
|
if r.BookingConfiguration == nil {
|
||||||
r.BookingConfiguration = &BookingConfiguration{}
|
r.BookingConfiguration = &BookingConfiguration{}
|
||||||
}
|
}
|
||||||
fmt.Println("GetPriceHT", r.BookingConfiguration.UsageStart, r.BookingConfiguration.UsageEnd)
|
|
||||||
if r.BookingConfiguration.UsageStart == nil {
|
if r.BookingConfiguration.UsageStart == nil {
|
||||||
r.BookingConfiguration.UsageStart = &now
|
r.BookingConfiguration.UsageStart = &now
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,23 +3,23 @@ package purchase_resource
|
|||||||
import (
|
import (
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"cloud.o-forge.io/core/oc-lib/dbs"
|
|
||||||
"cloud.o-forge.io/core/oc-lib/logs"
|
"cloud.o-forge.io/core/oc-lib/logs"
|
||||||
"cloud.o-forge.io/core/oc-lib/models/utils"
|
"cloud.o-forge.io/core/oc-lib/models/utils"
|
||||||
"cloud.o-forge.io/core/oc-lib/tools"
|
"cloud.o-forge.io/core/oc-lib/tools"
|
||||||
)
|
)
|
||||||
|
|
||||||
type PurchaseResourceMongoAccessor struct {
|
type PurchaseResourceMongoAccessor struct {
|
||||||
utils.AbstractAccessor // AbstractAccessor contains the basic fields of an accessor (model, caller)
|
utils.AbstractAccessor[*PurchaseResource] // AbstractAccessor contains the basic fields of an accessor (model, caller)
|
||||||
}
|
}
|
||||||
|
|
||||||
// New creates a new instance of the bookingMongoAccessor
|
// New creates a new instance of the bookingMongoAccessor
|
||||||
func NewAccessor(request *tools.APIRequest) *PurchaseResourceMongoAccessor {
|
func NewAccessor(request *tools.APIRequest) *PurchaseResourceMongoAccessor {
|
||||||
return &PurchaseResourceMongoAccessor{
|
return &PurchaseResourceMongoAccessor{
|
||||||
AbstractAccessor: utils.AbstractAccessor{
|
AbstractAccessor: utils.AbstractAccessor[*PurchaseResource]{
|
||||||
Logger: logs.CreateLogger(tools.PURCHASE_RESOURCE.String()), // Create a logger with the data type
|
Logger: logs.CreateLogger(tools.PURCHASE_RESOURCE.String()), // Create a logger with the data type
|
||||||
Request: request,
|
Request: request,
|
||||||
Type: tools.PURCHASE_RESOURCE,
|
Type: tools.PURCHASE_RESOURCE,
|
||||||
|
New: func() *PurchaseResource { return &PurchaseResource{} },
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -27,22 +27,6 @@ func NewAccessor(request *tools.APIRequest) *PurchaseResourceMongoAccessor {
|
|||||||
/*
|
/*
|
||||||
* Nothing special here, just the basic CRUD operations
|
* Nothing special here, just the basic CRUD operations
|
||||||
*/
|
*/
|
||||||
func (a *PurchaseResourceMongoAccessor) DeleteOne(id string) (utils.DBObject, int, error) {
|
|
||||||
return utils.GenericDeleteOne(id, a)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (a *PurchaseResourceMongoAccessor) UpdateOne(set utils.DBObject, id string) (utils.DBObject, int, error) {
|
|
||||||
return utils.GenericUpdateOne(set, id, a, &PurchaseResource{})
|
|
||||||
}
|
|
||||||
|
|
||||||
func (a *PurchaseResourceMongoAccessor) StoreOne(data utils.DBObject) (utils.DBObject, int, error) {
|
|
||||||
return utils.GenericStoreOne(data, a)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (a *PurchaseResourceMongoAccessor) CopyOne(data utils.DBObject) (utils.DBObject, int, error) {
|
|
||||||
return utils.GenericStoreOne(data, a)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (a *PurchaseResourceMongoAccessor) LoadOne(id string) (utils.DBObject, int, error) {
|
func (a *PurchaseResourceMongoAccessor) LoadOne(id string) (utils.DBObject, int, error) {
|
||||||
return utils.GenericLoadOne[*PurchaseResource](id, func(d utils.DBObject) (utils.DBObject, int, error) {
|
return utils.GenericLoadOne[*PurchaseResource](id, func(d utils.DBObject) (utils.DBObject, int, error) {
|
||||||
if d.(*PurchaseResource).EndDate != nil && time.Now().UTC().After(*d.(*PurchaseResource).EndDate) {
|
if d.(*PurchaseResource).EndDate != nil && time.Now().UTC().After(*d.(*PurchaseResource).EndDate) {
|
||||||
@@ -53,15 +37,7 @@ func (a *PurchaseResourceMongoAccessor) LoadOne(id string) (utils.DBObject, int,
|
|||||||
}, a)
|
}, a)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *PurchaseResourceMongoAccessor) LoadAll(isDraft bool) ([]utils.ShallowDBObject, int, error) {
|
func (a *PurchaseResourceMongoAccessor) GetExec(isDraft bool) func(utils.DBObject) utils.ShallowDBObject {
|
||||||
return utils.GenericLoadAll[*PurchaseResource](a.getExec(), isDraft, a)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (a *PurchaseResourceMongoAccessor) Search(filters *dbs.Filters, search string, isDraft bool) ([]utils.ShallowDBObject, int, error) {
|
|
||||||
return utils.GenericSearch[*PurchaseResource](filters, search, (&PurchaseResource{}).GetObjectFilters(search), a.getExec(), isDraft, a)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (a *PurchaseResourceMongoAccessor) getExec() func(utils.DBObject) utils.ShallowDBObject {
|
|
||||||
return func(d utils.DBObject) utils.ShallowDBObject {
|
return func(d utils.DBObject) utils.ShallowDBObject {
|
||||||
if d.(*PurchaseResource).EndDate != nil && time.Now().UTC().After(*d.(*PurchaseResource).EndDate) {
|
if d.(*PurchaseResource).EndDate != nil && time.Now().UTC().After(*d.(*PurchaseResource).EndDate) {
|
||||||
utils.GenericDeleteOne(d.GetID(), a)
|
utils.GenericDeleteOne(d.GetID(), a)
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
package resources
|
package resources
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"crypto/sha256"
|
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"slices"
|
"slices"
|
||||||
@@ -21,6 +20,7 @@ import (
|
|||||||
// AbstractResource is the struct containing all of the attributes commons to all ressources
|
// AbstractResource is the struct containing all of the attributes commons to all ressources
|
||||||
type AbstractResource struct {
|
type AbstractResource struct {
|
||||||
utils.AbstractObject // AbstractObject contains the basic fields of an object (id, name)
|
utils.AbstractObject // AbstractObject contains the basic fields of an object (id, name)
|
||||||
|
|
||||||
Type string `json:"type,omitempty" bson:"type,omitempty"` // Type is the type of the resource
|
Type string `json:"type,omitempty" bson:"type,omitempty"` // Type is the type of the resource
|
||||||
Logo string `json:"logo,omitempty" bson:"logo,omitempty" validate:"required"` // Logo is the logo of the resource
|
Logo string `json:"logo,omitempty" bson:"logo,omitempty" validate:"required"` // Logo is the logo of the resource
|
||||||
Description string `json:"description,omitempty" bson:"description,omitempty"` // Description is the description of the resource
|
Description string `json:"description,omitempty" bson:"description,omitempty"` // Description is the description of the resource
|
||||||
@@ -28,25 +28,6 @@ type AbstractResource struct {
|
|||||||
Owners []utils.Owner `json:"owners,omitempty" bson:"owners,omitempty"` // Owners is the list of owners of the resource
|
Owners []utils.Owner `json:"owners,omitempty" bson:"owners,omitempty"` // Owners is the list of owners of the resource
|
||||||
UsageRestrictions string `bson:"usage_restrictions,omitempty" json:"usage_restrictions,omitempty"`
|
UsageRestrictions string `bson:"usage_restrictions,omitempty" json:"usage_restrictions,omitempty"`
|
||||||
AllowedBookingModes map[booking.BookingMode]*pricing.PricingVariation `bson:"allowed_booking_modes" json:"allowed_booking_modes"`
|
AllowedBookingModes map[booking.BookingMode]*pricing.PricingVariation `bson:"allowed_booking_modes" json:"allowed_booking_modes"`
|
||||||
Signature []byte `bson:"signature,omitempty" json:"signature,omitempty"`
|
|
||||||
}
|
|
||||||
|
|
||||||
func (r *AbstractResource) Unsign() {
|
|
||||||
r.Signature = nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (r *AbstractResource) Sign() {
|
|
||||||
priv, err := tools.LoadKeyFromFilePrivate() // your node private key
|
|
||||||
if err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
b, _ := json.Marshal(r)
|
|
||||||
hash := sha256.Sum256(b)
|
|
||||||
r.Signature, err = priv.Sign(hash[:])
|
|
||||||
}
|
|
||||||
|
|
||||||
func (abs *AbstractResource) GetSignature() []byte {
|
|
||||||
return abs.Signature
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (abs *AbstractResource) FilterPeer(peerID string) *dbs.Filters {
|
func (abs *AbstractResource) FilterPeer(peerID string) *dbs.Filters {
|
||||||
@@ -66,10 +47,6 @@ func (r *AbstractResource) GetBookingModes() map[booking.BookingMode]*pricing.Pr
|
|||||||
return r.AllowedBookingModes
|
return r.AllowedBookingModes
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *AbstractResource) GetSelectedInstance(selected *int) ResourceInstanceITF {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (r *AbstractResource) GetType() string {
|
func (r *AbstractResource) GetType() string {
|
||||||
return tools.INVALID.String()
|
return tools.INVALID.String()
|
||||||
}
|
}
|
||||||
@@ -91,41 +68,33 @@ func (r *AbstractResource) CanDelete() bool {
|
|||||||
|
|
||||||
type AbstractInstanciatedResource[T ResourceInstanceITF] struct {
|
type AbstractInstanciatedResource[T ResourceInstanceITF] struct {
|
||||||
AbstractResource // AbstractResource contains the basic fields of an object (id, name)
|
AbstractResource // AbstractResource contains the basic fields of an object (id, name)
|
||||||
Instances []T `json:"instances,omitempty" bson:"instances,omitempty"` // Bill is the bill of the resource // Bill is the bill of the resource
|
|
||||||
}
|
|
||||||
|
|
||||||
// PEERID found
|
Instances []T `json:"instances,omitempty" bson:"instances,omitempty"` // Bill is the bill of the resource // Bill is the bill of the resource
|
||||||
func (abs *AbstractInstanciatedResource[T]) RefineResourceByPartnership(peerID string) ResourceInterface {
|
|
||||||
instances := []T{}
|
|
||||||
for _, i := range instances {
|
|
||||||
i, ok := i.RefineResourceByPartnership(peerID)
|
|
||||||
if ok {
|
|
||||||
instances = append(instances, i.(T))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
abs.Instances = instances
|
|
||||||
return abs
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (abs *AbstractInstanciatedResource[T]) AddInstances(instance ResourceInstanceITF) {
|
func (abs *AbstractInstanciatedResource[T]) AddInstances(instance ResourceInstanceITF) {
|
||||||
abs.Instances = append(abs.Instances, instance.(T))
|
abs.Instances = append(abs.Instances, instance.(T))
|
||||||
}
|
}
|
||||||
|
|
||||||
func (abs *AbstractInstanciatedResource[T]) ConvertToPricedResource(t tools.DataType, selectedInstance *int, selectedPartnership *int, selectedBuyingStrategy *int, selectedStrategy *int, selectedBookingModeIndex *int, request *tools.APIRequest) (pricing.PricedItemITF, error) {
|
func (abs *AbstractInstanciatedResource[T]) ConvertToPricedResource(t tools.DataType,
|
||||||
|
selectedInstance *int, selectedPartnership *int, selectedBuyingStrategy *int, selectedStrategy *int,
|
||||||
|
selectedBookingModeIndex *int, request *tools.APIRequest) (pricing.PricedItemITF, error) {
|
||||||
instances := map[string]string{}
|
instances := map[string]string{}
|
||||||
profiles := []pricing.PricingProfileITF{}
|
|
||||||
for _, instance := range abs.Instances { // TODO why it crush before ?
|
|
||||||
instances[instance.GetID()] = instance.GetName()
|
|
||||||
profiles = instance.GetPricingsProfiles(request.PeerID, request.Groups)
|
|
||||||
}
|
|
||||||
var profile pricing.PricingProfileITF
|
var profile pricing.PricingProfileITF
|
||||||
if t := abs.GetSelectedInstance(selectedInstance); t != nil {
|
if t := abs.GetSelectedInstance(selectedInstance); t != nil {
|
||||||
|
instances[t.GetID()] = t.GetName()
|
||||||
profile = t.GetProfile(request.PeerID, selectedPartnership, selectedBuyingStrategy, selectedStrategy)
|
profile = t.GetProfile(request.PeerID, selectedPartnership, selectedBuyingStrategy, selectedStrategy)
|
||||||
}
|
} else {
|
||||||
if profile == nil {
|
for _, instance := range abs.Instances { // TODO why it crush before ?
|
||||||
|
instances[instance.GetID()] = instance.GetName()
|
||||||
|
profiles := instance.GetPricingsProfiles(request.PeerID, request.Groups)
|
||||||
if len(profiles) > 0 {
|
if len(profiles) > 0 {
|
||||||
profile = profiles[0]
|
profile = profiles[0]
|
||||||
} else { // TODO : reset Self for Pricing Profile
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if profile == nil {
|
||||||
/*if ok, _ := utils.IsMySelf(request.PeerID, (&peer.Peer{}).GetAccessor(&tools.APIRequest{
|
/*if ok, _ := utils.IsMySelf(request.PeerID, (&peer.Peer{}).GetAccessor(&tools.APIRequest{
|
||||||
Admin: true,
|
Admin: true,
|
||||||
})); ok {*/
|
})); ok {*/
|
||||||
@@ -134,7 +103,6 @@ func (abs *AbstractInstanciatedResource[T]) ConvertToPricedResource(t tools.Data
|
|||||||
return nil, errors.New("no pricing profile found")
|
return nil, errors.New("no pricing profile found")
|
||||||
}*/
|
}*/
|
||||||
}
|
}
|
||||||
}
|
|
||||||
variations := []*pricing.PricingVariation{}
|
variations := []*pricing.PricingVariation{}
|
||||||
if selectedBookingModeIndex != nil && abs.AllowedBookingModes[booking.BookingMode(*selectedBookingModeIndex)] != nil {
|
if selectedBookingModeIndex != nil && abs.AllowedBookingModes[booking.BookingMode(*selectedBookingModeIndex)] != nil {
|
||||||
variations = append(variations, abs.AllowedBookingModes[booking.BookingMode(*selectedBookingModeIndex)])
|
variations = append(variations, abs.AllowedBookingModes[booking.BookingMode(*selectedBookingModeIndex)])
|
||||||
@@ -169,11 +137,11 @@ func (r *AbstractInstanciatedResource[T]) GetSelectedInstance(selected *int) Res
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (abs *AbstractInstanciatedResource[T]) SetAllowedInstances(request *tools.APIRequest) {
|
func (abs *AbstractInstanciatedResource[T]) SetAllowedInstances(request *tools.APIRequest, instanceID ...string) {
|
||||||
if request != nil && request.PeerID == abs.CreatorID && request.PeerID != "" {
|
if (request != nil && request.PeerID == abs.CreatorID && request.PeerID != "") || request.Admin {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
abs.Instances = VerifyAuthAction(abs.Instances, request)
|
abs.Instances = VerifyAuthAction(abs.Instances, request, instanceID...)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *AbstractInstanciatedResource[T]) Trim() {
|
func (d *AbstractInstanciatedResource[T]) Trim() {
|
||||||
@@ -191,9 +159,12 @@ func (abs *AbstractInstanciatedResource[T]) VerifyAuth(callName string, request
|
|||||||
return len(VerifyAuthAction(abs.Instances, request)) > 0 || abs.AbstractObject.VerifyAuth(callName, request)
|
return len(VerifyAuthAction(abs.Instances, request)) > 0 || abs.AbstractObject.VerifyAuth(callName, request)
|
||||||
}
|
}
|
||||||
|
|
||||||
func VerifyAuthAction[T ResourceInstanceITF](baseInstance []T, request *tools.APIRequest) []T {
|
func VerifyAuthAction[T ResourceInstanceITF](baseInstance []T, request *tools.APIRequest, instanceID ...string) []T {
|
||||||
instances := []T{}
|
instances := []T{}
|
||||||
for _, instance := range baseInstance {
|
for _, instance := range baseInstance {
|
||||||
|
if len(instanceID) > 0 && !slices.Contains(instanceID, instance.GetID()) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
_, peerGroups := instance.GetPeerGroups()
|
_, peerGroups := instance.GetPeerGroups()
|
||||||
for _, peers := range peerGroups {
|
for _, peers := range peerGroups {
|
||||||
if request == nil {
|
if request == nil {
|
||||||
@@ -201,11 +172,14 @@ func VerifyAuthAction[T ResourceInstanceITF](baseInstance []T, request *tools.AP
|
|||||||
}
|
}
|
||||||
if grps, ok := peers[request.PeerID]; ok || config.GetConfig().Whitelist {
|
if grps, ok := peers[request.PeerID]; ok || config.GetConfig().Whitelist {
|
||||||
if (ok && slices.Contains(grps, "*")) || (!ok && config.GetConfig().Whitelist) {
|
if (ok && slices.Contains(grps, "*")) || (!ok && config.GetConfig().Whitelist) {
|
||||||
|
instance.FilterInstance(request.PeerID)
|
||||||
instances = append(instances, instance)
|
instances = append(instances, instance)
|
||||||
|
// TODO filter Partners + Profiles...
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
for _, grp := range grps {
|
for _, grp := range grps {
|
||||||
if slices.Contains(request.Groups, grp) {
|
if slices.Contains(request.Groups, grp) {
|
||||||
|
instance.FilterInstance(request.PeerID)
|
||||||
instances = append(instances, instance)
|
instances = append(instances, instance)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -244,17 +218,15 @@ func NewInstance[T ResourcePartnerITF](name string) *ResourceInstance[T] {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (abs *ResourceInstance[T]) RefineResourceByPartnership(peerID string) (ResourceInstanceITF, bool) {
|
func (ri *ResourceInstance[T]) FilterInstance(peerID string) {
|
||||||
okk := false
|
partnerships := []T{}
|
||||||
partners := []T{}
|
for _, p := range ri.Partnerships {
|
||||||
for _, p := range abs.Partnerships {
|
if p.GetPeerGroups()[peerID] != nil {
|
||||||
partner, ok := p.RefineResourceByPartnership(peerID)
|
p.FilterPartnership(peerID)
|
||||||
if ok {
|
partnerships = append(partnerships, p)
|
||||||
partners = append(partners, partner.(T))
|
|
||||||
okk = true
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return abs, okk
|
ri.Partnerships = partnerships
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ri *ResourceInstance[T]) ClearEnv() {
|
func (ri *ResourceInstance[T]) ClearEnv() {
|
||||||
@@ -328,17 +300,14 @@ type ResourcePartnerShip[T pricing.PricingProfileITF] struct {
|
|||||||
// to upgrade pricing profiles. to be a map BuyingStrategy, map of Strategy
|
// to upgrade pricing profiles. to be a map BuyingStrategy, map of Strategy
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ri *ResourcePartnerShip[T]) RefineResourceByPartnership(peerID string) (ResourcePartnerITF, bool) {
|
func (ri *ResourcePartnerShip[T]) FilterPartnership(peerID string) {
|
||||||
ok := false
|
if ri.PeerGroups[peerID] == nil {
|
||||||
peerGrp := map[string][]string{}
|
ri.PeerGroups = map[string][]string{}
|
||||||
for k, v := range ri.PeerGroups {
|
} else {
|
||||||
if k == peerID {
|
ri.PeerGroups = map[string][]string{
|
||||||
peerGrp[k] = v
|
peerID: ri.PeerGroups[peerID],
|
||||||
ok = true
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
ri.PeerGroups = peerGrp
|
|
||||||
return ri, ok
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ri *ResourcePartnerShip[T]) GetProfile(buying *int, strategy *int) pricing.PricingProfileITF {
|
func (ri *ResourcePartnerShip[T]) GetProfile(buying *int, strategy *int) pricing.PricingProfileITF {
|
||||||
|
|||||||
@@ -11,7 +11,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type ResourceMongoAccessor[T ResourceInterface] struct {
|
type ResourceMongoAccessor[T ResourceInterface] struct {
|
||||||
utils.AbstractAccessor // AbstractAccessor contains the basic fields of an accessor (model, caller)
|
utils.AbstractAccessor[ResourceInterface] // AbstractAccessor contains the basic fields of an accessor (model, caller)
|
||||||
generateData func() utils.DBObject
|
generateData func() utils.DBObject
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -25,10 +25,27 @@ func NewAccessor[T ResourceInterface](t tools.DataType, request *tools.APIReques
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
return &ResourceMongoAccessor[T]{
|
return &ResourceMongoAccessor[T]{
|
||||||
AbstractAccessor: utils.AbstractAccessor{
|
AbstractAccessor: utils.AbstractAccessor[ResourceInterface]{
|
||||||
Logger: logs.CreateLogger(t.String()), // Create a logger with the data type
|
Logger: logs.CreateLogger(t.String()), // Create a logger with the data type
|
||||||
Request: request,
|
Request: request,
|
||||||
Type: t,
|
Type: t,
|
||||||
|
New: func() ResourceInterface {
|
||||||
|
switch t {
|
||||||
|
case tools.COMPUTE_RESOURCE:
|
||||||
|
return &ComputeResource{}
|
||||||
|
case tools.STORAGE_RESOURCE:
|
||||||
|
return &StorageResource{}
|
||||||
|
case tools.PROCESSING_RESOURCE:
|
||||||
|
return &ProcessingResource{}
|
||||||
|
case tools.WORKFLOW_RESOURCE:
|
||||||
|
return &WorkflowResource{}
|
||||||
|
case tools.DATA_RESOURCE:
|
||||||
|
return &DataResource{}
|
||||||
|
case tools.NATIVE_TOOL:
|
||||||
|
return &NativeTool{}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
},
|
||||||
},
|
},
|
||||||
generateData: g,
|
generateData: g,
|
||||||
}
|
}
|
||||||
@@ -37,9 +54,6 @@ func NewAccessor[T ResourceInterface](t tools.DataType, request *tools.APIReques
|
|||||||
/*
|
/*
|
||||||
* Nothing special here, just the basic CRUD operations
|
* Nothing special here, just the basic CRUD operations
|
||||||
*/
|
*/
|
||||||
func (dca *ResourceMongoAccessor[T]) DeleteOne(id string) (utils.DBObject, int, error) {
|
|
||||||
return utils.GenericDeleteOne(id, dca)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (dca *ResourceMongoAccessor[T]) UpdateOne(set utils.DBObject, id string) (utils.DBObject, int, error) {
|
func (dca *ResourceMongoAccessor[T]) UpdateOne(set utils.DBObject, id string) (utils.DBObject, int, error) {
|
||||||
if dca.GetType() == tools.COMPUTE_RESOURCE {
|
if dca.GetType() == tools.COMPUTE_RESOURCE {
|
||||||
@@ -75,20 +89,6 @@ func (dca *ResourceMongoAccessor[T]) CopyOne(data utils.DBObject) (utils.DBObjec
|
|||||||
return dca.StoreOne(data)
|
return dca.StoreOne(data)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (dca *ResourceMongoAccessor[T]) LoadOne(id string) (utils.DBObject, int, error) {
|
|
||||||
return utils.GenericLoadOne[T](id, func(d utils.DBObject) (utils.DBObject, int, error) {
|
|
||||||
d.(T).SetAllowedInstances(dca.Request)
|
|
||||||
return d, 200, nil
|
|
||||||
}, dca)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (wfa *ResourceMongoAccessor[T]) LoadAll(isDraft bool) ([]utils.ShallowDBObject, int, error) {
|
|
||||||
return utils.GenericLoadAll[T](func(d utils.DBObject) utils.ShallowDBObject {
|
|
||||||
d.(T).SetAllowedInstances(wfa.Request)
|
|
||||||
return d
|
|
||||||
}, isDraft, wfa)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (wfa *ResourceMongoAccessor[T]) Search(filters *dbs.Filters, search string, isDraft bool) ([]utils.ShallowDBObject, int, error) {
|
func (wfa *ResourceMongoAccessor[T]) Search(filters *dbs.Filters, search string, isDraft bool) ([]utils.ShallowDBObject, int, error) {
|
||||||
if filters == nil && search == "*" {
|
if filters == nil && search == "*" {
|
||||||
return utils.GenericLoadAll[T](func(d utils.DBObject) utils.ShallowDBObject {
|
return utils.GenericLoadAll[T](func(d utils.DBObject) utils.ShallowDBObject {
|
||||||
@@ -96,14 +96,21 @@ func (wfa *ResourceMongoAccessor[T]) Search(filters *dbs.Filters, search string,
|
|||||||
return d
|
return d
|
||||||
}, isDraft, wfa)
|
}, isDraft, wfa)
|
||||||
}
|
}
|
||||||
return utils.GenericSearch[T](filters, search, wfa.getResourceFilter(search),
|
return utils.GenericSearch[T](filters, search, wfa.GetObjectFilters(search),
|
||||||
func(d utils.DBObject) utils.ShallowDBObject {
|
func(d utils.DBObject) utils.ShallowDBObject {
|
||||||
d.(T).SetAllowedInstances(wfa.Request)
|
d.(T).SetAllowedInstances(wfa.Request)
|
||||||
return d
|
return d
|
||||||
}, isDraft, wfa)
|
}, isDraft, wfa)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (abs *ResourceMongoAccessor[T]) getResourceFilter(search string) *dbs.Filters {
|
func (a *ResourceMongoAccessor[T]) GetExec(isDraft bool) func(utils.DBObject) utils.ShallowDBObject {
|
||||||
|
return func(d utils.DBObject) utils.ShallowDBObject {
|
||||||
|
d.(T).SetAllowedInstances(a.Request)
|
||||||
|
return d
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (abs *ResourceMongoAccessor[T]) GetObjectFilters(search string) *dbs.Filters {
|
||||||
return &dbs.Filters{
|
return &dbs.Filters{
|
||||||
Or: map[string][]dbs.Filter{ // filter by like name, short_description, description, owner, url if no filters are provided
|
Or: map[string][]dbs.Filter{ // filter by like name, short_description, description, owner, url if no filters are provided
|
||||||
"abstractintanciatedresource.abstractresource.abstractobject.name": {{Operator: dbs.LIKE.String(), Value: search}},
|
"abstractintanciatedresource.abstractresource.abstractobject.name": {{Operator: dbs.LIKE.String(), Value: search}},
|
||||||
|
|||||||
@@ -65,6 +65,7 @@ func TestPricedComputeResource_GetPriceHT(t *testing.T) {
|
|||||||
r := resources.PricedComputeResource{
|
r := resources.PricedComputeResource{
|
||||||
PricedResource: resources.PricedResource{
|
PricedResource: resources.PricedResource{
|
||||||
ResourceID: "comp456",
|
ResourceID: "comp456",
|
||||||
|
SelectedPricing: &MockPricingProfile{ReturnCost: 1.0},
|
||||||
BookingConfiguration: &resources.BookingConfiguration{
|
BookingConfiguration: &resources.BookingConfiguration{
|
||||||
UsageStart: &start,
|
UsageStart: &start,
|
||||||
UsageEnd: &end,
|
UsageEnd: &end,
|
||||||
|
|||||||
@@ -92,6 +92,7 @@ func TestPricedDataResource_GetPriceHT(t *testing.T) {
|
|||||||
|
|
||||||
r := &resources.PricedDataResource{
|
r := &resources.PricedDataResource{
|
||||||
PricedResource: resources.PricedResource{
|
PricedResource: resources.PricedResource{
|
||||||
|
SelectedPricing: pricingProfile,
|
||||||
BookingConfiguration: &resources.BookingConfiguration{
|
BookingConfiguration: &resources.BookingConfiguration{
|
||||||
UsageStart: &now,
|
UsageStart: &now,
|
||||||
UsageEnd: &later,
|
UsageEnd: &later,
|
||||||
|
|||||||
@@ -105,14 +105,10 @@ func TestGetPriceHT(t *testing.T) {
|
|||||||
assert.Equal(t, 0.0, price)
|
assert.Equal(t, 0.0, price)
|
||||||
})
|
})
|
||||||
|
|
||||||
t.Run("uses first profile if selected is nil", func(t *testing.T) {
|
t.Run("defaults BookingConfiguration when nil", func(t *testing.T) {
|
||||||
start := time.Now()
|
mock := &MockPricingProfile{ReturnCost: 42.0}
|
||||||
end := start.Add(30 * time.Minute)
|
|
||||||
r := &resources.PricedResource{
|
r := &resources.PricedResource{
|
||||||
BookingConfiguration: &resources.BookingConfiguration{
|
SelectedPricing: mock,
|
||||||
UsageStart: &start,
|
|
||||||
UsageEnd: &end,
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
price, err := r.GetPriceHT()
|
price, err := r.GetPriceHT()
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|||||||
@@ -20,6 +20,7 @@ func (m *MockInstance) GetID() string { return m.ID }
|
|||||||
func (m *MockInstance) GetName() string { return m.Name }
|
func (m *MockInstance) GetName() string { return m.Name }
|
||||||
func (m *MockInstance) ClearEnv() {}
|
func (m *MockInstance) ClearEnv() {}
|
||||||
func (m *MockInstance) ClearPeerGroups() {}
|
func (m *MockInstance) ClearPeerGroups() {}
|
||||||
|
func (m *MockPartner) FilterPartnership(peerID string) {}
|
||||||
func (m *MockInstance) GetProfile(peerID string, a *int, b *int, c *int) pricing.PricingProfileITF {
|
func (m *MockInstance) GetProfile(peerID string, a *int, b *int, c *int) pricing.PricingProfileITF {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@@ -36,10 +37,6 @@ type MockPartner struct {
|
|||||||
groups map[string][]string
|
groups map[string][]string
|
||||||
}
|
}
|
||||||
|
|
||||||
func (abs *MockPartner) RefineResourceByPartnership(peerID string) (resources.ResourcePartnerITF, bool) {
|
|
||||||
return nil, false
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *MockPartner) GetProfile(buying *int, strategy *int) pricing.PricingProfileITF {
|
func (m *MockPartner) GetProfile(buying *int, strategy *int) pricing.PricingProfileITF {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@@ -48,6 +45,7 @@ func (m *MockPartner) GetPeerGroups() map[string][]string {
|
|||||||
return m.groups
|
return m.groups
|
||||||
}
|
}
|
||||||
func (m *MockPartner) ClearPeerGroups() {}
|
func (m *MockPartner) ClearPeerGroups() {}
|
||||||
|
|
||||||
func (m *MockPartner) GetPricingsProfiles(string, []string) []pricing.PricingProfileITF {
|
func (m *MockPartner) GetPricingsProfiles(string, []string) []pricing.PricingProfileITF {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@@ -108,7 +106,7 @@ type FakeResource struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (f *FakeResource) Trim() {}
|
func (f *FakeResource) Trim() {}
|
||||||
func (f *FakeResource) SetAllowedInstances(*tools.APIRequest) {}
|
func (f *FakeResource) SetAllowedInstances(*tools.APIRequest, ...string) {}
|
||||||
func (f *FakeResource) VerifyAuth(string, *tools.APIRequest) bool { return true }
|
func (f *FakeResource) VerifyAuth(string, *tools.APIRequest) bool { return true }
|
||||||
|
|
||||||
func TestNewAccessor_ReturnsValid(t *testing.T) {
|
func TestNewAccessor_ReturnsValid(t *testing.T) {
|
||||||
|
|||||||
@@ -19,10 +19,6 @@ func (d *WorkflowResource) GetAccessor(request *tools.APIRequest) utils.Accessor
|
|||||||
return NewAccessor[*WorkflowResource](tools.WORKFLOW_RESOURCE, request, func() utils.DBObject { return &WorkflowResource{} })
|
return NewAccessor[*WorkflowResource](tools.WORKFLOW_RESOURCE, request, func() utils.DBObject { return &WorkflowResource{} })
|
||||||
}
|
}
|
||||||
|
|
||||||
func (abs *WorkflowResource) RefineResourceByPartnership(peerID string) ResourceInterface {
|
|
||||||
return abs
|
|
||||||
}
|
|
||||||
|
|
||||||
func (r *WorkflowResource) AddInstances(instance ResourceInstanceITF) {
|
func (r *WorkflowResource) AddInstances(instance ResourceInstanceITF) {
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -37,7 +33,7 @@ func (d *WorkflowResource) ClearEnv() utils.DBObject {
|
|||||||
func (d *WorkflowResource) Trim() {
|
func (d *WorkflowResource) Trim() {
|
||||||
/* EMPTY */
|
/* EMPTY */
|
||||||
}
|
}
|
||||||
func (w *WorkflowResource) SetAllowedInstances(request *tools.APIRequest) {
|
func (w *WorkflowResource) SetAllowedInstances(request *tools.APIRequest, ids ...string) {
|
||||||
/* EMPTY */
|
/* EMPTY */
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,17 +1,17 @@
|
|||||||
package models
|
package models
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"strconv"
|
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"cloud.o-forge.io/core/oc-lib/models"
|
"cloud.o-forge.io/core/oc-lib/models"
|
||||||
|
"cloud.o-forge.io/core/oc-lib/tools"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestModel_ReturnsValidInstances(t *testing.T) {
|
func TestModel_ReturnsValidInstances(t *testing.T) {
|
||||||
for name, _ := range models.ModelsCatalog {
|
for name := range models.ModelsCatalog {
|
||||||
t.Run(name, func(t *testing.T) {
|
t.Run(name, func(t *testing.T) {
|
||||||
modelInt, _ := strconv.Atoi(name)
|
modelInt := tools.FromString(name)
|
||||||
obj := models.Model(modelInt)
|
obj := models.Model(modelInt)
|
||||||
assert.NotNil(t, obj, "Model() returned nil for valid model name %s", name)
|
assert.NotNil(t, obj, "Model() returned nil for valid model name %s", name)
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -1,7 +1,10 @@
|
|||||||
package utils
|
package utils
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"crypto/sha256"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"errors"
|
||||||
|
"slices"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"cloud.o-forge.io/core/oc-lib/dbs"
|
"cloud.o-forge.io/core/oc-lib/dbs"
|
||||||
@@ -37,20 +40,43 @@ type AbstractObject struct {
|
|||||||
UpdaterID string `json:"updater_id,omitempty" bson:"updater_id,omitempty"`
|
UpdaterID string `json:"updater_id,omitempty" bson:"updater_id,omitempty"`
|
||||||
UserUpdaterID string `json:"user_updater_id,omitempty" bson:"user_updater_id,omitempty"`
|
UserUpdaterID string `json:"user_updater_id,omitempty" bson:"user_updater_id,omitempty"`
|
||||||
AccessMode AccessMode `json:"access_mode" bson:"access_mode" default:"0"`
|
AccessMode AccessMode `json:"access_mode" bson:"access_mode" default:"0"`
|
||||||
|
Signature []byte `bson:"signature,omitempty" json:"signature,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ri *AbstractObject) GetAccessor(request *tools.APIRequest) Accessor {
|
func (ri *AbstractObject) GetAccessor(request *tools.APIRequest) Accessor {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *AbstractObject) Unsign() {}
|
func (r *AbstractObject) Unsign() {
|
||||||
|
r.Signature = nil
|
||||||
|
}
|
||||||
|
|
||||||
func (r *AbstractObject) Sign() {}
|
func (r *AbstractObject) Sign() {
|
||||||
|
priv, err := tools.LoadKeyFromFilePrivate() // your node private key
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
b, _ := json.Marshal(r.DeepCopy())
|
||||||
|
hash := sha256.Sum256(b)
|
||||||
|
r.Signature, err = priv.Sign(hash[:])
|
||||||
|
}
|
||||||
|
|
||||||
func (r *AbstractObject) SetID(id string) {
|
func (r *AbstractObject) SetID(id string) {
|
||||||
r.UUID = id
|
r.UUID = id
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (r *AbstractObject) DeepCopy() *AbstractObject {
|
||||||
|
var obj AbstractObject
|
||||||
|
b, err := json.Marshal(r)
|
||||||
|
if err != nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
if err := json.Unmarshal(b, &obj); err != nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
return &obj
|
||||||
|
}
|
||||||
|
|
||||||
func (r *AbstractObject) SetName(name string) {
|
func (r *AbstractObject) SetName(name string) {
|
||||||
r.Name = name
|
r.Name = name
|
||||||
}
|
}
|
||||||
@@ -82,6 +108,10 @@ func (ao AbstractObject) GetID() string {
|
|||||||
return ao.UUID
|
return ao.UUID
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (ao AbstractObject) GetSignature() []byte {
|
||||||
|
return ao.Signature
|
||||||
|
}
|
||||||
|
|
||||||
// GetName implements ShallowDBObject.
|
// GetName implements ShallowDBObject.
|
||||||
func (ao AbstractObject) GetName() string {
|
func (ao AbstractObject) GetName() string {
|
||||||
return ao.Name
|
return ao.Name
|
||||||
@@ -103,7 +133,7 @@ func (ao *AbstractObject) UpToDate(user string, peer string, create bool) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (ao *AbstractObject) VerifyAuth(callName string, request *tools.APIRequest) bool {
|
func (ao *AbstractObject) VerifyAuth(callName string, request *tools.APIRequest) bool {
|
||||||
return (ao.AccessMode == Public && callName == "get") || request.Admin || (request != nil && ao.CreatorID == request.PeerID && request.PeerID != "")
|
return (ao.AccessMode == Public && callName == "get") || (request != nil && (request.Admin || (ao.CreatorID == request.PeerID && request.PeerID != "")))
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO : check write per auth
|
// TODO : check write per auth
|
||||||
@@ -137,50 +167,104 @@ func (dma *AbstractObject) Serialize(obj DBObject) map[string]interface{} {
|
|||||||
return m
|
return m
|
||||||
}
|
}
|
||||||
|
|
||||||
type AbstractAccessor struct {
|
type AbstractAccessor[T DBObject] struct {
|
||||||
Logger zerolog.Logger // Logger is the logger of the accessor, it's a specilized logger for the accessor
|
Logger zerolog.Logger // Logger is the logger of the accessor, it's a specilized logger for the accessor
|
||||||
Type tools.DataType // Type is the data type of the accessor
|
Type tools.DataType // Type is the data type of the accessor
|
||||||
Request *tools.APIRequest // Caller is the http caller of the accessor (optionnal) only need in a peer connection
|
Request *tools.APIRequest // Caller is the http caller of the accessor (optionnal) only need in a peer connection
|
||||||
ResourceModelAccessor Accessor
|
ResourceModelAccessor Accessor
|
||||||
|
New func() T
|
||||||
|
NotImplemented []string
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *AbstractAccessor) ShouldVerifyAuth() bool {
|
func (r *AbstractAccessor[T]) ShouldVerifyAuth() bool {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *AbstractAccessor) GetRequest() *tools.APIRequest {
|
func (r *AbstractAccessor[T]) GetRequest() *tools.APIRequest {
|
||||||
return r.Request
|
return r.Request
|
||||||
}
|
}
|
||||||
|
|
||||||
func (dma *AbstractAccessor) GetUser() string {
|
func (dma *AbstractAccessor[T]) GetUser() string {
|
||||||
if dma.Request == nil {
|
if dma.Request == nil {
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
return dma.Request.Username
|
return dma.Request.Username
|
||||||
}
|
}
|
||||||
|
|
||||||
func (dma *AbstractAccessor) GetPeerID() string {
|
func (dma *AbstractAccessor[T]) GetPeerID() string {
|
||||||
if dma.Request == nil {
|
if dma.Request == nil {
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
return dma.Request.PeerID
|
return dma.Request.PeerID
|
||||||
}
|
}
|
||||||
func (dma *AbstractAccessor) GetGroups() []string {
|
func (dma *AbstractAccessor[T]) GetGroups() []string {
|
||||||
if dma.Request == nil {
|
if dma.Request == nil {
|
||||||
return []string{}
|
return []string{}
|
||||||
}
|
}
|
||||||
return dma.Request.Groups
|
return dma.Request.Groups
|
||||||
}
|
}
|
||||||
func (dma *AbstractAccessor) GetLogger() *zerolog.Logger {
|
func (dma *AbstractAccessor[T]) GetLogger() *zerolog.Logger {
|
||||||
return &dma.Logger
|
return &dma.Logger
|
||||||
}
|
}
|
||||||
func (dma *AbstractAccessor) GetType() tools.DataType {
|
func (dma *AbstractAccessor[T]) GetType() tools.DataType {
|
||||||
return dma.Type
|
return dma.Type
|
||||||
}
|
}
|
||||||
|
|
||||||
func (dma *AbstractAccessor) GetCaller() *tools.HTTPCaller {
|
func (dma *AbstractAccessor[T]) GetCaller() *tools.HTTPCaller {
|
||||||
if dma.Request == nil {
|
if dma.Request == nil {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
return dma.Request.Caller
|
return dma.Request.Caller
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Nothing special here, just the basic CRUD operations
|
||||||
|
*/
|
||||||
|
func (a *AbstractAccessor[T]) DeleteOne(id string) (DBObject, int, error) {
|
||||||
|
if len(a.NotImplemented) > 0 && slices.Contains(a.NotImplemented, "DeleteOne") {
|
||||||
|
return nil, 404, errors.New("not implemented")
|
||||||
|
}
|
||||||
|
return GenericDeleteOne(id, a)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a *AbstractAccessor[T]) UpdateOne(set DBObject, id string) (DBObject, int, error) {
|
||||||
|
if len(a.NotImplemented) > 0 && slices.Contains(a.NotImplemented, "UpdateOne") {
|
||||||
|
return nil, 404, errors.New("not implemented")
|
||||||
|
}
|
||||||
|
// should verify if a source is existing...
|
||||||
|
return GenericUpdateOne(set, id, a, a.New())
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a *AbstractAccessor[T]) StoreOne(data DBObject) (DBObject, int, error) {
|
||||||
|
if len(a.NotImplemented) > 0 && slices.Contains(a.NotImplemented, "StoreOne") {
|
||||||
|
return nil, 404, errors.New("not implemented")
|
||||||
|
}
|
||||||
|
return GenericStoreOne(data.(T), a)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a *AbstractAccessor[T]) CopyOne(data DBObject) (DBObject, int, error) {
|
||||||
|
if len(a.NotImplemented) > 0 && slices.Contains(a.NotImplemented, "CopyOne") {
|
||||||
|
return nil, 404, errors.New("not implemented")
|
||||||
|
}
|
||||||
|
return GenericStoreOne(data.(T), a)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a *AbstractAccessor[T]) LoadOne(id string) (DBObject, int, error) {
|
||||||
|
return GenericLoadOne[T](id, func(d DBObject) (DBObject, int, error) {
|
||||||
|
return d, 200, nil
|
||||||
|
}, a)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a *AbstractAccessor[T]) LoadAll(isDraft bool) ([]ShallowDBObject, int, error) {
|
||||||
|
return GenericLoadAll[T](a.GetExec(isDraft), isDraft, a)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a *AbstractAccessor[T]) Search(filters *dbs.Filters, search string, isDraft bool) ([]ShallowDBObject, int, error) {
|
||||||
|
return GenericSearch[T](filters, search, a.New().GetObjectFilters(search), a.GetExec(isDraft), isDraft, a)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a *AbstractAccessor[T]) GetExec(isDraft bool) func(DBObject) ShallowDBObject {
|
||||||
|
return func(d DBObject) ShallowDBObject {
|
||||||
|
return d
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -34,6 +34,8 @@ func GenericStoreOne(data DBObject, a Accessor) (DBObject, int, error) {
|
|||||||
data.SetID(data.GetID())
|
data.SetID(data.GetID())
|
||||||
data.StoreDraftDefault()
|
data.StoreDraftDefault()
|
||||||
data.UpToDate(a.GetUser(), a.GetPeerID(), true)
|
data.UpToDate(a.GetUser(), a.GetPeerID(), true)
|
||||||
|
data.Unsign()
|
||||||
|
data.Sign()
|
||||||
f := dbs.Filters{
|
f := dbs.Filters{
|
||||||
Or: map[string][]dbs.Filter{
|
Or: map[string][]dbs.Filter{
|
||||||
"abstractresource.abstractobject.name": {{
|
"abstractresource.abstractobject.name": {{
|
||||||
@@ -97,6 +99,8 @@ func GenericUpdateOne(set DBObject, id string, a Accessor, new DBObject) (DBObje
|
|||||||
}
|
}
|
||||||
set = newSet
|
set = newSet
|
||||||
r.UpToDate(a.GetUser(), a.GetPeerID(), false)
|
r.UpToDate(a.GetUser(), a.GetPeerID(), false)
|
||||||
|
r.Unsign()
|
||||||
|
r.Sign()
|
||||||
if a.ShouldVerifyAuth() && !r.VerifyAuth("update", a.GetRequest()) {
|
if a.ShouldVerifyAuth() && !r.VerifyAuth("update", a.GetRequest()) {
|
||||||
return nil, 403, errors.New("you are not allowed to access :" + a.GetType().String())
|
return nil, 403, errors.New("you are not allowed to access :" + a.GetType().String())
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -34,6 +34,8 @@ type DBObject interface {
|
|||||||
Deserialize(j map[string]interface{}, obj DBObject) DBObject
|
Deserialize(j map[string]interface{}, obj DBObject) DBObject
|
||||||
Sign()
|
Sign()
|
||||||
Unsign()
|
Unsign()
|
||||||
|
GetSignature() []byte
|
||||||
|
GetObjectFilters(search string) *dbs.Filters
|
||||||
}
|
}
|
||||||
|
|
||||||
// Accessor is an interface that defines the basic methods for an Accessor
|
// Accessor is an interface that defines the basic methods for an Accessor
|
||||||
@@ -53,4 +55,5 @@ type Accessor interface {
|
|||||||
LoadAll(isDraft bool) ([]ShallowDBObject, int, error)
|
LoadAll(isDraft bool) ([]ShallowDBObject, int, error)
|
||||||
UpdateOne(set DBObject, id string) (DBObject, int, error)
|
UpdateOne(set DBObject, id string) (DBObject, int, error)
|
||||||
Search(filters *dbs.Filters, search string, isDraft bool) ([]ShallowDBObject, int, error)
|
Search(filters *dbs.Filters, search string, isDraft bool) ([]ShallowDBObject, int, error)
|
||||||
|
GetExec(isDraft bool) func(DBObject) ShallowDBObject
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,128 +1,263 @@
|
|||||||
package models_test
|
package utils_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
|
||||||
|
|
||||||
"cloud.o-forge.io/core/oc-lib/dbs"
|
|
||||||
"cloud.o-forge.io/core/oc-lib/models/utils"
|
"cloud.o-forge.io/core/oc-lib/models/utils"
|
||||||
"cloud.o-forge.io/core/oc-lib/tools"
|
"cloud.o-forge.io/core/oc-lib/tools"
|
||||||
"github.com/google/uuid"
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestGenerateID(t *testing.T) {
|
// ---- AbstractObject ----
|
||||||
ao := &utils.AbstractObject{}
|
|
||||||
ao.GenerateID()
|
func TestAbstractObject_GetID(t *testing.T) {
|
||||||
assert.NotEmpty(t, ao.UUID)
|
obj := &utils.AbstractObject{UUID: "abc-123"}
|
||||||
_, err := uuid.Parse(ao.UUID)
|
assert.Equal(t, "abc-123", obj.GetID())
|
||||||
assert.NoError(t, err)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestStoreDraftDefault(t *testing.T) {
|
func TestAbstractObject_GetName(t *testing.T) {
|
||||||
ao := &utils.AbstractObject{IsDraft: true}
|
obj := &utils.AbstractObject{Name: "test-name"}
|
||||||
ao.StoreDraftDefault()
|
assert.Equal(t, "test-name", obj.GetName())
|
||||||
assert.False(t, ao.IsDraft)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestCanUpdate(t *testing.T) {
|
func TestAbstractObject_GetCreatorID(t *testing.T) {
|
||||||
ao := &utils.AbstractObject{}
|
obj := &utils.AbstractObject{CreatorID: "peer-xyz"}
|
||||||
res, set := ao.CanUpdate(nil)
|
assert.Equal(t, "peer-xyz", obj.GetCreatorID())
|
||||||
assert.True(t, res)
|
|
||||||
assert.Nil(t, set)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestCanDelete(t *testing.T) {
|
func TestAbstractObject_SetID(t *testing.T) {
|
||||||
ao := &utils.AbstractObject{}
|
obj := &utils.AbstractObject{}
|
||||||
assert.True(t, ao.CanDelete())
|
obj.SetID("new-id")
|
||||||
|
assert.Equal(t, "new-id", obj.UUID)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestIsDrafted(t *testing.T) {
|
func TestAbstractObject_SetName(t *testing.T) {
|
||||||
ao := &utils.AbstractObject{IsDraft: true}
|
obj := &utils.AbstractObject{}
|
||||||
assert.True(t, ao.IsDrafted())
|
obj.SetName("hello")
|
||||||
|
assert.Equal(t, "hello", obj.Name)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestGetID(t *testing.T) {
|
func TestAbstractObject_GenerateID_WhenEmpty(t *testing.T) {
|
||||||
u := uuid.New().String()
|
obj := &utils.AbstractObject{}
|
||||||
ao := &utils.AbstractObject{UUID: u}
|
obj.GenerateID()
|
||||||
assert.Equal(t, u, ao.GetID())
|
assert.NotEmpty(t, obj.UUID)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestGetName(t *testing.T) {
|
func TestAbstractObject_GenerateID_KeepsExisting(t *testing.T) {
|
||||||
name := "MyObject"
|
obj := &utils.AbstractObject{UUID: "existing-id"}
|
||||||
ao := &utils.AbstractObject{Name: name}
|
obj.GenerateID()
|
||||||
assert.Equal(t, name, ao.GetName())
|
assert.Equal(t, "existing-id", obj.UUID)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestGetCreatorID(t *testing.T) {
|
func TestAbstractObject_StoreDraftDefault(t *testing.T) {
|
||||||
id := "creator-123"
|
obj := &utils.AbstractObject{IsDraft: true}
|
||||||
ao := &utils.AbstractObject{CreatorID: id}
|
obj.StoreDraftDefault()
|
||||||
assert.Equal(t, id, ao.GetCreatorID())
|
assert.False(t, obj.IsDraft)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestUpToDate_CreateFalse(t *testing.T) {
|
func TestAbstractObject_IsDrafted(t *testing.T) {
|
||||||
ao := &utils.AbstractObject{}
|
obj := &utils.AbstractObject{IsDraft: true}
|
||||||
now := time.Now()
|
assert.True(t, obj.IsDrafted())
|
||||||
time.Sleep(time.Millisecond) // ensure time difference
|
|
||||||
ao.UpToDate("user123", "peer456", false)
|
obj.IsDraft = false
|
||||||
assert.WithinDuration(t, now, ao.UpdateDate, time.Second)
|
assert.False(t, obj.IsDrafted())
|
||||||
assert.Equal(t, "peer456", ao.UpdaterID)
|
|
||||||
assert.Equal(t, "user123", ao.UserUpdaterID)
|
|
||||||
assert.True(t, ao.CreationDate.IsZero())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestUpToDate_CreateTrue(t *testing.T) {
|
func TestAbstractObject_CanDelete(t *testing.T) {
|
||||||
ao := &utils.AbstractObject{}
|
obj := &utils.AbstractObject{}
|
||||||
now := time.Now()
|
assert.True(t, obj.CanDelete())
|
||||||
time.Sleep(time.Millisecond)
|
|
||||||
ao.UpToDate("user123", "peer456", true)
|
|
||||||
assert.WithinDuration(t, now, ao.UpdateDate, time.Second)
|
|
||||||
assert.WithinDuration(t, now, ao.CreationDate, time.Second)
|
|
||||||
assert.Equal(t, "peer456", ao.UpdaterID)
|
|
||||||
assert.Equal(t, "peer456", ao.CreatorID)
|
|
||||||
assert.Equal(t, "user123", ao.UserUpdaterID)
|
|
||||||
assert.Equal(t, "user123", ao.UserCreatorID)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestVerifyAuth(t *testing.T) {
|
func TestAbstractObject_CanUpdate(t *testing.T) {
|
||||||
request := &tools.APIRequest{PeerID: "peer123"}
|
obj := &utils.AbstractObject{UUID: "id-1"}
|
||||||
ao := &utils.AbstractObject{CreatorID: "peer123"}
|
other := &utils.AbstractObject{UUID: "id-2"}
|
||||||
assert.True(t, ao.VerifyAuth("get", request))
|
ok, returned := obj.CanUpdate(other)
|
||||||
|
assert.True(t, ok)
|
||||||
ao = &utils.AbstractObject{AccessMode: utils.Public}
|
assert.Equal(t, other, returned)
|
||||||
assert.True(t, ao.VerifyAuth("get", nil))
|
|
||||||
|
|
||||||
ao = &utils.AbstractObject{AccessMode: utils.Private, CreatorID: "peer123"}
|
|
||||||
request = &tools.APIRequest{PeerID: "wrong"}
|
|
||||||
assert.False(t, ao.VerifyAuth("get", request))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestGetObjectFilters(t *testing.T) {
|
func TestAbstractObject_Unsign(t *testing.T) {
|
||||||
ao := &utils.AbstractObject{}
|
obj := &utils.AbstractObject{Signature: []byte("sig")}
|
||||||
f := ao.GetObjectFilters("*")
|
obj.Unsign()
|
||||||
|
assert.Nil(t, obj.Signature)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestAbstractObject_GetSignature(t *testing.T) {
|
||||||
|
obj := &utils.AbstractObject{Signature: []byte("sig")}
|
||||||
|
assert.Equal(t, []byte("sig"), obj.GetSignature())
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestAbstractObject_DeepCopy(t *testing.T) {
|
||||||
|
obj := &utils.AbstractObject{UUID: "id-1", Name: "original"}
|
||||||
|
copy := obj.DeepCopy()
|
||||||
|
assert.NotNil(t, copy)
|
||||||
|
assert.Equal(t, obj.UUID, copy.UUID)
|
||||||
|
assert.Equal(t, obj.Name, copy.Name)
|
||||||
|
|
||||||
|
// Mutating the copy should not affect the original
|
||||||
|
copy.Name = "modified"
|
||||||
|
assert.Equal(t, "original", obj.Name)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestAbstractObject_UpToDate_Create(t *testing.T) {
|
||||||
|
obj := &utils.AbstractObject{CreatorID: ""}
|
||||||
|
obj.UpToDate("user1", "peer1", true)
|
||||||
|
assert.Equal(t, "peer1", obj.UpdaterID)
|
||||||
|
assert.Equal(t, "user1", obj.UserUpdaterID)
|
||||||
|
// CreatorID was empty so create branch is skipped
|
||||||
|
assert.Empty(t, obj.CreatorID)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestAbstractObject_UpToDate_CreateWithExistingCreator(t *testing.T) {
|
||||||
|
obj := &utils.AbstractObject{CreatorID: "existing-peer"}
|
||||||
|
obj.UpToDate("user1", "peer1", true)
|
||||||
|
assert.Equal(t, "peer1", obj.CreatorID)
|
||||||
|
assert.Equal(t, "user1", obj.UserCreatorID)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestAbstractObject_UpToDate_Update(t *testing.T) {
|
||||||
|
obj := &utils.AbstractObject{CreatorID: "original-peer"}
|
||||||
|
obj.UpToDate("user2", "peer2", false)
|
||||||
|
assert.Equal(t, "peer2", obj.UpdaterID)
|
||||||
|
assert.Equal(t, "original-peer", obj.CreatorID) // unchanged
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---- VerifyAuth ----
|
||||||
|
|
||||||
|
func TestAbstractObject_VerifyAuth_NilRequest_GetPublic(t *testing.T) {
|
||||||
|
obj := &utils.AbstractObject{AccessMode: 1} // Public = 1
|
||||||
|
assert.True(t, obj.VerifyAuth("get", nil))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestAbstractObject_VerifyAuth_NilRequest_DeletePublic(t *testing.T) {
|
||||||
|
obj := &utils.AbstractObject{AccessMode: 1} // Public = 1
|
||||||
|
// non-"get" call with nil request → false
|
||||||
|
assert.False(t, obj.VerifyAuth("delete", nil))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestAbstractObject_VerifyAuth_NilRequest_Private(t *testing.T) {
|
||||||
|
obj := &utils.AbstractObject{AccessMode: 0} // Private
|
||||||
|
assert.False(t, obj.VerifyAuth("get", nil))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestAbstractObject_VerifyAuth_AdminRequest(t *testing.T) {
|
||||||
|
obj := &utils.AbstractObject{}
|
||||||
|
req := &tools.APIRequest{Admin: true}
|
||||||
|
assert.True(t, obj.VerifyAuth("get", req))
|
||||||
|
assert.True(t, obj.VerifyAuth("delete", req))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestAbstractObject_VerifyAuth_MatchingPeerID(t *testing.T) {
|
||||||
|
obj := &utils.AbstractObject{CreatorID: "peer-abc"}
|
||||||
|
req := &tools.APIRequest{PeerID: "peer-abc"}
|
||||||
|
assert.True(t, obj.VerifyAuth("get", req))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestAbstractObject_VerifyAuth_MismatchedPeerID(t *testing.T) {
|
||||||
|
obj := &utils.AbstractObject{CreatorID: "peer-abc"}
|
||||||
|
req := &tools.APIRequest{PeerID: "peer-xyz"}
|
||||||
|
assert.False(t, obj.VerifyAuth("get", req))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestAbstractObject_VerifyAuth_EmptyPeerID(t *testing.T) {
|
||||||
|
obj := &utils.AbstractObject{CreatorID: ""}
|
||||||
|
req := &tools.APIRequest{PeerID: ""}
|
||||||
|
// both empty → condition `ao.CreatorID == request.PeerID && request.PeerID != ""` is false
|
||||||
|
assert.False(t, obj.VerifyAuth("get", req))
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---- GetObjectFilters ----
|
||||||
|
|
||||||
|
func TestAbstractObject_GetObjectFilters_Star(t *testing.T) {
|
||||||
|
obj := &utils.AbstractObject{}
|
||||||
|
f := obj.GetObjectFilters("*")
|
||||||
assert.NotNil(t, f)
|
assert.NotNil(t, f)
|
||||||
assert.Contains(t, f.Or, "abstractobject.name")
|
|
||||||
assert.Equal(t, dbs.LIKE.String(), f.Or["abstractobject.name"][0].Operator)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestDeserialize(t *testing.T) {
|
func TestAbstractObject_GetObjectFilters_Search(t *testing.T) {
|
||||||
ao := &utils.AbstractObject{}
|
obj := &utils.AbstractObject{}
|
||||||
input := map[string]interface{}{"name": "test", "id": uuid.New().String()}
|
f := obj.GetObjectFilters("my-search")
|
||||||
res := ao.Deserialize(input, &utils.AbstractObject{})
|
assert.NotNil(t, f)
|
||||||
assert.NotNil(t, res)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestSerialize(t *testing.T) {
|
// ---- Serialize / Deserialize ----
|
||||||
ao := &utils.AbstractObject{Name: "test", UUID: uuid.New().String()}
|
|
||||||
m := ao.Serialize(ao)
|
func TestAbstractObject_SerializeDeserialize(t *testing.T) {
|
||||||
assert.Equal(t, "test", m["name"])
|
obj := &utils.AbstractObject{UUID: "serial-id", Name: "serial-name"}
|
||||||
|
m := obj.Serialize(obj)
|
||||||
|
assert.NotNil(t, m)
|
||||||
|
|
||||||
|
dst := &utils.AbstractObject{}
|
||||||
|
result := obj.Deserialize(m, dst)
|
||||||
|
assert.NotNil(t, result)
|
||||||
|
assert.Equal(t, "serial-id", result.GetID())
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestAbstractAccessorMethods(t *testing.T) {
|
// ---- GetAccessor ----
|
||||||
r := &utils.AbstractAccessor{Request: &tools.APIRequest{Username: "alice", PeerID: "peer1", Groups: []string{"dev"}}}
|
|
||||||
assert.True(t, r.ShouldVerifyAuth())
|
func TestAbstractObject_GetAccessor_ReturnsNil(t *testing.T) {
|
||||||
assert.Equal(t, "alice", r.GetUser())
|
obj := &utils.AbstractObject{}
|
||||||
assert.Equal(t, "peer1", r.GetPeerID())
|
acc := obj.GetAccessor(nil)
|
||||||
assert.Equal(t, []string{"dev"}, r.GetGroups())
|
assert.Nil(t, acc)
|
||||||
assert.Equal(t, r.Request.Caller, r.GetCaller())
|
}
|
||||||
|
|
||||||
|
// ---- AbstractAccessor ----
|
||||||
|
|
||||||
|
func TestAbstractAccessor_GetUser_NilRequest(t *testing.T) {
|
||||||
|
acc := &utils.AbstractAccessor[*utils.AbstractObject]{Request: nil}
|
||||||
|
assert.Equal(t, "", acc.GetUser())
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestAbstractAccessor_GetUser_WithRequest(t *testing.T) {
|
||||||
|
acc := &utils.AbstractAccessor[*utils.AbstractObject]{
|
||||||
|
Request: &tools.APIRequest{Username: "alice"},
|
||||||
|
}
|
||||||
|
assert.Equal(t, "alice", acc.GetUser())
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestAbstractAccessor_GetPeerID_NilRequest(t *testing.T) {
|
||||||
|
acc := &utils.AbstractAccessor[*utils.AbstractObject]{Request: nil}
|
||||||
|
assert.Equal(t, "", acc.GetPeerID())
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestAbstractAccessor_GetPeerID_WithRequest(t *testing.T) {
|
||||||
|
acc := &utils.AbstractAccessor[*utils.AbstractObject]{
|
||||||
|
Request: &tools.APIRequest{PeerID: "peer-42"},
|
||||||
|
}
|
||||||
|
assert.Equal(t, "peer-42", acc.GetPeerID())
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestAbstractAccessor_GetGroups_NilRequest(t *testing.T) {
|
||||||
|
acc := &utils.AbstractAccessor[*utils.AbstractObject]{Request: nil}
|
||||||
|
assert.Equal(t, []string{}, acc.GetGroups())
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestAbstractAccessor_GetGroups_WithRequest(t *testing.T) {
|
||||||
|
acc := &utils.AbstractAccessor[*utils.AbstractObject]{
|
||||||
|
Request: &tools.APIRequest{Groups: []string{"g1", "g2"}},
|
||||||
|
}
|
||||||
|
assert.Equal(t, []string{"g1", "g2"}, acc.GetGroups())
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestAbstractAccessor_ShouldVerifyAuth(t *testing.T) {
|
||||||
|
acc := &utils.AbstractAccessor[*utils.AbstractObject]{}
|
||||||
|
assert.True(t, acc.ShouldVerifyAuth())
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestAbstractAccessor_GetType(t *testing.T) {
|
||||||
|
acc := &utils.AbstractAccessor[*utils.AbstractObject]{
|
||||||
|
Type: tools.WORKFLOW,
|
||||||
|
}
|
||||||
|
assert.Equal(t, tools.WORKFLOW, acc.GetType())
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestAbstractAccessor_GetRequest(t *testing.T) {
|
||||||
|
req := &tools.APIRequest{Admin: true}
|
||||||
|
acc := &utils.AbstractAccessor[*utils.AbstractObject]{Request: req}
|
||||||
|
assert.Equal(t, req, acc.GetRequest())
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestAbstractAccessor_GetCaller_NilRequest(t *testing.T) {
|
||||||
|
acc := &utils.AbstractAccessor[*utils.AbstractObject]{Request: nil}
|
||||||
|
assert.Nil(t, acc.GetCaller())
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,168 +0,0 @@
|
|||||||
package models_test
|
|
||||||
|
|
||||||
import (
|
|
||||||
"errors"
|
|
||||||
"testing"
|
|
||||||
|
|
||||||
"cloud.o-forge.io/core/oc-lib/dbs"
|
|
||||||
"cloud.o-forge.io/core/oc-lib/models/utils"
|
|
||||||
"cloud.o-forge.io/core/oc-lib/tools"
|
|
||||||
"github.com/rs/zerolog"
|
|
||||||
"github.com/stretchr/testify/assert"
|
|
||||||
"github.com/stretchr/testify/mock"
|
|
||||||
)
|
|
||||||
|
|
||||||
// --- Mock Definitions ---
|
|
||||||
|
|
||||||
type MockDBObject struct {
|
|
||||||
mock.Mock
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *MockAccessor) GetLogger() *zerolog.Logger {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
func (m *MockAccessor) GetGroups() []string {
|
|
||||||
return []string{}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *MockAccessor) GetCaller() *tools.HTTPCaller {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *MockDBObject) GenerateID() { m.Called() }
|
|
||||||
func (m *MockDBObject) StoreDraftDefault() { m.Called() }
|
|
||||||
func (m *MockDBObject) UpToDate(user, peer string, create bool) {
|
|
||||||
m.Called(user, peer, create)
|
|
||||||
}
|
|
||||||
func (m *MockDBObject) VerifyAuth(req *tools.APIRequest) bool {
|
|
||||||
args := m.Called(req)
|
|
||||||
return args.Bool(0)
|
|
||||||
}
|
|
||||||
func (m *MockDBObject) CanDelete() bool {
|
|
||||||
args := m.Called()
|
|
||||||
return args.Bool(0)
|
|
||||||
}
|
|
||||||
func (m *MockDBObject) CanUpdate(set utils.DBObject) (bool, utils.DBObject) {
|
|
||||||
args := m.Called(set)
|
|
||||||
return args.Bool(0), args.Get(1).(utils.DBObject)
|
|
||||||
}
|
|
||||||
func (m *MockDBObject) IsDrafted() bool {
|
|
||||||
args := m.Called()
|
|
||||||
return args.Bool(0)
|
|
||||||
}
|
|
||||||
func (m *MockDBObject) Serialize(obj utils.DBObject) map[string]interface{} {
|
|
||||||
args := m.Called(obj)
|
|
||||||
return args.Get(0).(map[string]interface{})
|
|
||||||
}
|
|
||||||
func (m *MockDBObject) Deserialize(mdata map[string]interface{}, obj utils.DBObject) utils.DBObject {
|
|
||||||
args := m.Called(mdata, obj)
|
|
||||||
return args.Get(0).(utils.DBObject)
|
|
||||||
}
|
|
||||||
func (m *MockDBObject) GetID() string {
|
|
||||||
args := m.Called()
|
|
||||||
return args.String(0)
|
|
||||||
}
|
|
||||||
func (m *MockDBObject) GetName() string {
|
|
||||||
args := m.Called()
|
|
||||||
return args.String(0)
|
|
||||||
}
|
|
||||||
|
|
||||||
type MockAccessor struct {
|
|
||||||
mock.Mock
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *MockAccessor) DeleteOne(id string) (utils.DBObject, int, error) {
|
|
||||||
args := m.Called(id)
|
|
||||||
return args.Get(0).(utils.DBObject), args.Int(1), args.Error(2)
|
|
||||||
}
|
|
||||||
func (m *MockAccessor) LoadOne(id string) (utils.DBObject, int, error) {
|
|
||||||
args := m.Called(id)
|
|
||||||
return args.Get(0).(utils.DBObject), args.Int(1), args.Error(2)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *MockAccessor) LoadAll(isDraft bool) ([]utils.ShallowDBObject, int, error) {
|
|
||||||
args := m.Called(isDraft)
|
|
||||||
return args.Get(0).([]utils.ShallowDBObject), args.Int(1), args.Error(2)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *MockAccessor) UpdateOne(set utils.DBObject, id string) (utils.DBObject, int, error) {
|
|
||||||
args := m.Called(set, id)
|
|
||||||
return args.Get(0).(utils.DBObject), args.Int(1), args.Error(2)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *MockAccessor) CopyOne(data utils.DBObject) (utils.DBObject, int, error) {
|
|
||||||
args := m.Called(data)
|
|
||||||
return args.Get(0).(utils.DBObject), args.Int(1), args.Error(2)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *MockAccessor) StoreOne(data utils.DBObject) (utils.DBObject, int, error) {
|
|
||||||
args := m.Called(data)
|
|
||||||
return args.Get(0).(utils.DBObject), args.Int(1), args.Error(2)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *MockAccessor) ShouldVerifyAuth() bool {
|
|
||||||
args := m.Called()
|
|
||||||
return args.Bool(0)
|
|
||||||
}
|
|
||||||
func (m *MockAccessor) GetRequest() *tools.APIRequest {
|
|
||||||
args := m.Called()
|
|
||||||
return args.Get(0).(*tools.APIRequest)
|
|
||||||
}
|
|
||||||
func (m *MockAccessor) GetType() tools.DataType {
|
|
||||||
args := m.Called()
|
|
||||||
return args.Get(0).(tools.DataType)
|
|
||||||
}
|
|
||||||
func (m *MockAccessor) Search(filters *dbs.Filters, s string, d bool) ([]utils.ShallowDBObject, int, error) {
|
|
||||||
args := m.Called(filters, s, d)
|
|
||||||
return args.Get(0).([]utils.ShallowDBObject), args.Int(1), args.Error(2)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *MockAccessor) GetUser() string {
|
|
||||||
args := m.Called()
|
|
||||||
return args.String(0)
|
|
||||||
}
|
|
||||||
func (m *MockAccessor) GetPeerID() string {
|
|
||||||
args := m.Called()
|
|
||||||
return args.String(0)
|
|
||||||
}
|
|
||||||
|
|
||||||
// --- Test Cases ---
|
|
||||||
|
|
||||||
func TestVerifyAccess_Authorized(t *testing.T) {
|
|
||||||
mockObj := new(MockDBObject)
|
|
||||||
mockAcc := new(MockAccessor)
|
|
||||||
|
|
||||||
req := &tools.APIRequest{PeerID: "peer"}
|
|
||||||
mockAcc.On("LoadOne", "123").Return(mockObj, 200, nil)
|
|
||||||
mockAcc.On("ShouldVerifyAuth").Return(true)
|
|
||||||
mockObj.On("VerifyAuth", req).Return(true)
|
|
||||||
mockAcc.On("GetRequest").Return(req)
|
|
||||||
|
|
||||||
err := utils.VerifyAccess(mockAcc, "123")
|
|
||||||
assert.NoError(t, err)
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestVerifyAccess_Unauthorized(t *testing.T) {
|
|
||||||
mockObj := new(MockDBObject)
|
|
||||||
mockAcc := new(MockAccessor)
|
|
||||||
|
|
||||||
req := &tools.APIRequest{PeerID: "peer"}
|
|
||||||
mockAcc.On("LoadOne", "123").Return(mockObj, 200, nil)
|
|
||||||
mockAcc.On("ShouldVerifyAuth").Return(true)
|
|
||||||
mockObj.On("VerifyAuth", req).Return(false)
|
|
||||||
mockAcc.On("GetRequest").Return(req)
|
|
||||||
|
|
||||||
err := utils.VerifyAccess(mockAcc, "123")
|
|
||||||
assert.Error(t, err)
|
|
||||||
assert.Contains(t, err.Error(), "not allowed")
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestVerifyAccess_LoadError(t *testing.T) {
|
|
||||||
mockAcc := new(MockAccessor)
|
|
||||||
|
|
||||||
mockAcc.On("LoadOne", "bad-id").Return(nil, 404, errors.New("not found"))
|
|
||||||
|
|
||||||
err := utils.VerifyAccess(mockAcc, "bad-id")
|
|
||||||
assert.Error(t, err)
|
|
||||||
assert.Equal(t, "not found", err.Error())
|
|
||||||
}
|
|
||||||
@@ -12,6 +12,7 @@ import (
|
|||||||
"cloud.o-forge.io/core/oc-lib/models/booking"
|
"cloud.o-forge.io/core/oc-lib/models/booking"
|
||||||
"cloud.o-forge.io/core/oc-lib/models/collaborative_area/shallow_collaborative_area"
|
"cloud.o-forge.io/core/oc-lib/models/collaborative_area/shallow_collaborative_area"
|
||||||
"cloud.o-forge.io/core/oc-lib/models/common"
|
"cloud.o-forge.io/core/oc-lib/models/common"
|
||||||
|
"cloud.o-forge.io/core/oc-lib/models/common/models"
|
||||||
"cloud.o-forge.io/core/oc-lib/models/common/pricing"
|
"cloud.o-forge.io/core/oc-lib/models/common/pricing"
|
||||||
"cloud.o-forge.io/core/oc-lib/models/peer"
|
"cloud.o-forge.io/core/oc-lib/models/peer"
|
||||||
"cloud.o-forge.io/core/oc-lib/models/resources"
|
"cloud.o-forge.io/core/oc-lib/models/resources"
|
||||||
@@ -42,6 +43,8 @@ type Workflow struct {
|
|||||||
ScheduleActive bool `json:"schedule_active" bson:"schedule_active"` // ScheduleActive is a flag that indicates if the schedule is active, if not the workflow is not scheduled and no execution or booking will be set
|
ScheduleActive bool `json:"schedule_active" bson:"schedule_active"` // ScheduleActive is a flag that indicates if the schedule is active, if not the workflow is not scheduled and no execution or booking will be set
|
||||||
// Schedule *WorkflowSchedule `bson:"schedule,omitempty" json:"schedule,omitempty"` // Schedule is the schedule of the workflow
|
// Schedule *WorkflowSchedule `bson:"schedule,omitempty" json:"schedule,omitempty"` // Schedule is the schedule of the workflow
|
||||||
Shared []string `json:"shared,omitempty" bson:"shared,omitempty"` // Shared is the ID of the shared workflow // AbstractWorkflow contains the basic fields of a workflow
|
Shared []string `json:"shared,omitempty" bson:"shared,omitempty"` // Shared is the ID of the shared workflow // AbstractWorkflow contains the basic fields of a workflow
|
||||||
|
Env []models.Param `json:"env,omitempty" bson:"env,omitempty"`
|
||||||
|
Inputs []models.Param `json:"inputs,omitempty" bson:"inputs,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *Workflow) GetAccessor(request *tools.APIRequest) utils.Accessor {
|
func (d *Workflow) GetAccessor(request *tools.APIRequest) utils.Accessor {
|
||||||
@@ -390,11 +393,13 @@ func (w *Workflow) GetPricedItem(
|
|||||||
for _, item := range w.Graph.Items {
|
for _, item := range w.Graph.Items {
|
||||||
if f(item) {
|
if f(item) {
|
||||||
dt, res := item.GetResource()
|
dt, res := item.GetResource()
|
||||||
|
|
||||||
ord, err := res.ConvertToPricedResource(dt, &instance, &partnership, &buying, &strategy, &bookingMode, request)
|
ord, err := res.ConvertToPricedResource(dt, &instance, &partnership, &buying, &strategy, &bookingMode, request)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return list_datas, err
|
return list_datas, err
|
||||||
}
|
}
|
||||||
list_datas[res.GetID()] = ord
|
list_datas[res.GetID()] = ord
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return list_datas, nil
|
return list_datas, nil
|
||||||
@@ -445,6 +450,7 @@ func (ao *Workflow) VerifyAuth(callName string, request *tools.APIRequest) bool
|
|||||||
return ao.AbstractObject.VerifyAuth(callName, request) || isAuthorized
|
return ao.AbstractObject.VerifyAuth(callName, request) || isAuthorized
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO : Check Booking... + Storage
|
||||||
/*
|
/*
|
||||||
* CheckBooking is a function that checks the booking of the workflow on peers (even ourselves)
|
* CheckBooking is a function that checks the booking of the workflow on peers (even ourselves)
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -14,7 +14,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type workflowMongoAccessor struct {
|
type workflowMongoAccessor struct {
|
||||||
utils.AbstractAccessor // AbstractAccessor contains the basic fields of an accessor (model, caller)
|
utils.AbstractAccessor[*Workflow] // AbstractAccessor contains the basic fields of an accessor (model, caller)
|
||||||
computeResourceAccessor utils.Accessor
|
computeResourceAccessor utils.Accessor
|
||||||
collaborativeAreaAccessor utils.Accessor
|
collaborativeAreaAccessor utils.Accessor
|
||||||
workspaceAccessor utils.Accessor
|
workspaceAccessor utils.Accessor
|
||||||
@@ -34,7 +34,7 @@ func new(t tools.DataType, request *tools.APIRequest) *workflowMongoAccessor {
|
|||||||
computeResourceAccessor: (&resources.ComputeResource{}).GetAccessor(request),
|
computeResourceAccessor: (&resources.ComputeResource{}).GetAccessor(request),
|
||||||
collaborativeAreaAccessor: (&shallow_collaborative_area.ShallowCollaborativeArea{}).GetAccessor(request),
|
collaborativeAreaAccessor: (&shallow_collaborative_area.ShallowCollaborativeArea{}).GetAccessor(request),
|
||||||
workspaceAccessor: (&workspace.Workspace{}).GetAccessor(request),
|
workspaceAccessor: (&workspace.Workspace{}).GetAccessor(request),
|
||||||
AbstractAccessor: utils.AbstractAccessor{
|
AbstractAccessor: utils.AbstractAccessor[*Workflow]{
|
||||||
Logger: logs.CreateLogger(t.String()), // Create a logger with the data type
|
Logger: logs.CreateLogger(t.String()), // Create a logger with the data type
|
||||||
Request: request,
|
Request: request,
|
||||||
Type: t,
|
Type: t,
|
||||||
@@ -186,10 +186,6 @@ func (a *workflowMongoAccessor) LoadOne(id string) (utils.DBObject, int, error)
|
|||||||
}, a)
|
}, a)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *workflowMongoAccessor) LoadAll(isDraft bool) ([]utils.ShallowDBObject, int, error) {
|
|
||||||
return utils.GenericLoadAll[*Workflow](func(d utils.DBObject) utils.ShallowDBObject { return &d.(*Workflow).AbstractObject }, isDraft, a)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (a *workflowMongoAccessor) Search(filters *dbs.Filters, search string, isDraft bool) ([]utils.ShallowDBObject, int, error) {
|
func (a *workflowMongoAccessor) Search(filters *dbs.Filters, search string, isDraft bool) ([]utils.ShallowDBObject, int, error) {
|
||||||
return utils.GenericSearch[*Workflow](filters, search, (&Workflow{}).GetObjectFilters(search), func(d utils.DBObject) utils.ShallowDBObject { return a.verifyResource(d) }, isDraft, a)
|
return utils.GenericSearch[*Workflow](filters, search, (&Workflow{}).GetObjectFilters(search), func(d utils.DBObject) utils.ShallowDBObject { return a.verifyResource(d) }, isDraft, a)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,26 +4,35 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"cloud.o-forge.io/core/oc-lib/models/utils"
|
"cloud.o-forge.io/core/oc-lib/models/utils"
|
||||||
|
"cloud.o-forge.io/core/oc-lib/tools"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestStoreOneWorkflow(t *testing.T) {
|
func TestNewWorkflowAccessor(t *testing.T) {
|
||||||
w := Workflow{
|
req := &tools.APIRequest{}
|
||||||
AbstractObject: utils.AbstractObject{Name: "testWorkflow"},
|
acc := NewAccessor(req)
|
||||||
}
|
assert.NotNil(t, acc)
|
||||||
|
|
||||||
wma := NewAccessor(nil)
|
|
||||||
id, _, _ := wma.StoreOne(&w)
|
|
||||||
|
|
||||||
assert.NotEmpty(t, id)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestLoadOneWorkflow(t *testing.T) {
|
func TestWorkflow_StoreDraftDefault(t *testing.T) {
|
||||||
w := Workflow{
|
w := &Workflow{
|
||||||
AbstractObject: utils.AbstractObject{Name: "testWorkflow"},
|
AbstractObject: utils.AbstractObject{Name: "testWorkflow"},
|
||||||
}
|
}
|
||||||
|
w.StoreDraftDefault()
|
||||||
wma := NewAccessor(nil)
|
assert.False(t, w.IsDraft)
|
||||||
new_w, _, _ := wma.StoreOne(&w)
|
}
|
||||||
assert.Equal(t, w, new_w)
|
|
||||||
|
func TestWorkflow_VerifyAuth_NilRequest(t *testing.T) {
|
||||||
|
w := &Workflow{
|
||||||
|
AbstractObject: utils.AbstractObject{},
|
||||||
|
}
|
||||||
|
result := w.VerifyAuth("get", nil)
|
||||||
|
assert.False(t, result)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestWorkflow_VerifyAuth_AdminRequest(t *testing.T) {
|
||||||
|
w := &Workflow{}
|
||||||
|
req := &tools.APIRequest{Admin: true}
|
||||||
|
result := w.VerifyAuth("get", req)
|
||||||
|
assert.True(t, result)
|
||||||
}
|
}
|
||||||
|
|||||||
172
models/workflow_execution/tests/workflow_execution_test.go
Normal file
172
models/workflow_execution/tests/workflow_execution_test.go
Normal file
@@ -0,0 +1,172 @@
|
|||||||
|
package workflow_execution_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"cloud.o-forge.io/core/oc-lib/models/common/enum"
|
||||||
|
"cloud.o-forge.io/core/oc-lib/models/workflow_execution"
|
||||||
|
"cloud.o-forge.io/core/oc-lib/tools"
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
)
|
||||||
|
|
||||||
|
// ---- WorkflowExecution model ----
|
||||||
|
|
||||||
|
func TestWorkflowExecution_StoreDraftDefault(t *testing.T) {
|
||||||
|
we := &workflow_execution.WorkflowExecution{}
|
||||||
|
we.StoreDraftDefault()
|
||||||
|
assert.False(t, we.IsDraft)
|
||||||
|
assert.Equal(t, enum.SCHEDULED, we.State)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestWorkflowExecution_CanDelete_Draft(t *testing.T) {
|
||||||
|
we := &workflow_execution.WorkflowExecution{}
|
||||||
|
we.IsDraft = true
|
||||||
|
assert.True(t, we.CanDelete())
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestWorkflowExecution_CanDelete_NonDraft(t *testing.T) {
|
||||||
|
we := &workflow_execution.WorkflowExecution{}
|
||||||
|
we.IsDraft = false
|
||||||
|
assert.False(t, we.CanDelete())
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestWorkflowExecution_CanUpdate_StateChange(t *testing.T) {
|
||||||
|
we := &workflow_execution.WorkflowExecution{State: enum.SCHEDULED}
|
||||||
|
set := &workflow_execution.WorkflowExecution{State: enum.STARTED}
|
||||||
|
ok, returned := we.CanUpdate(set)
|
||||||
|
assert.True(t, ok)
|
||||||
|
// Only the state should be propagated
|
||||||
|
assert.Equal(t, enum.STARTED, returned.(*workflow_execution.WorkflowExecution).State)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestWorkflowExecution_CanUpdate_SameState_NonDraft(t *testing.T) {
|
||||||
|
we := &workflow_execution.WorkflowExecution{State: enum.SCHEDULED}
|
||||||
|
we.IsDraft = false
|
||||||
|
set := &workflow_execution.WorkflowExecution{State: enum.SCHEDULED}
|
||||||
|
ok, _ := we.CanUpdate(set)
|
||||||
|
// !r.IsDraft == true → ok
|
||||||
|
assert.True(t, ok)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestWorkflowExecution_CanUpdate_SameState_Draft(t *testing.T) {
|
||||||
|
we := &workflow_execution.WorkflowExecution{State: enum.SCHEDULED}
|
||||||
|
we.IsDraft = true
|
||||||
|
set := &workflow_execution.WorkflowExecution{State: enum.SCHEDULED}
|
||||||
|
ok, _ := we.CanUpdate(set)
|
||||||
|
// !r.IsDraft == false (it is draft) → ok false
|
||||||
|
assert.False(t, ok)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestWorkflowExecution_Equals_True(t *testing.T) {
|
||||||
|
now := time.Now()
|
||||||
|
a := &workflow_execution.WorkflowExecution{WorkflowID: "wf-1"}
|
||||||
|
a.ExecDate = now
|
||||||
|
b := &workflow_execution.WorkflowExecution{WorkflowID: "wf-1"}
|
||||||
|
b.ExecDate = now
|
||||||
|
assert.True(t, a.Equals(b))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestWorkflowExecution_Equals_DifferentDate(t *testing.T) {
|
||||||
|
a := &workflow_execution.WorkflowExecution{WorkflowID: "wf-1"}
|
||||||
|
a.ExecDate = time.Now()
|
||||||
|
b := &workflow_execution.WorkflowExecution{WorkflowID: "wf-1"}
|
||||||
|
b.ExecDate = time.Now().Add(time.Hour)
|
||||||
|
assert.False(t, a.Equals(b))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestWorkflowExecution_Equals_DifferentWorkflow(t *testing.T) {
|
||||||
|
now := time.Now()
|
||||||
|
a := &workflow_execution.WorkflowExecution{WorkflowID: "wf-1"}
|
||||||
|
a.ExecDate = now
|
||||||
|
b := &workflow_execution.WorkflowExecution{WorkflowID: "wf-2"}
|
||||||
|
b.ExecDate = now
|
||||||
|
assert.False(t, a.Equals(b))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestWorkflowExecution_GetName(t *testing.T) {
|
||||||
|
we := &workflow_execution.WorkflowExecution{}
|
||||||
|
we.UUID = "exec-uuid"
|
||||||
|
we.ExecDate = time.Date(2026, 1, 1, 0, 0, 0, 0, time.UTC)
|
||||||
|
name := we.GetName()
|
||||||
|
assert.Contains(t, name, "exec-uuid")
|
||||||
|
assert.Contains(t, name, "2026")
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestWorkflowExecution_GenerateID(t *testing.T) {
|
||||||
|
we := &workflow_execution.WorkflowExecution{}
|
||||||
|
we.GenerateID()
|
||||||
|
assert.NotEmpty(t, we.UUID)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestWorkflowExecution_GenerateID_KeepsExisting(t *testing.T) {
|
||||||
|
we := &workflow_execution.WorkflowExecution{}
|
||||||
|
we.UUID = "existing-uuid"
|
||||||
|
we.GenerateID()
|
||||||
|
assert.Equal(t, "existing-uuid", we.UUID)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestWorkflowExecution_VerifyAuth_AlwaysTrue(t *testing.T) {
|
||||||
|
we := &workflow_execution.WorkflowExecution{}
|
||||||
|
assert.True(t, we.VerifyAuth("get", nil))
|
||||||
|
assert.True(t, we.VerifyAuth("delete", &tools.APIRequest{}))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestWorkflowExecution_GetAccessor(t *testing.T) {
|
||||||
|
we := &workflow_execution.WorkflowExecution{}
|
||||||
|
acc := we.GetAccessor(&tools.APIRequest{})
|
||||||
|
assert.NotNil(t, acc)
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---- ArgoStatusToState ----
|
||||||
|
|
||||||
|
func TestArgoStatusToState_Succeeded(t *testing.T) {
|
||||||
|
we := &workflow_execution.WorkflowExecution{}
|
||||||
|
we.ArgoStatusToState("succeeded")
|
||||||
|
assert.Equal(t, enum.SUCCESS, we.State)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestArgoStatusToState_Pending(t *testing.T) {
|
||||||
|
we := &workflow_execution.WorkflowExecution{}
|
||||||
|
we.ArgoStatusToState("pending")
|
||||||
|
assert.Equal(t, enum.SCHEDULED, we.State)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestArgoStatusToState_Running(t *testing.T) {
|
||||||
|
we := &workflow_execution.WorkflowExecution{}
|
||||||
|
we.ArgoStatusToState("running")
|
||||||
|
assert.Equal(t, enum.STARTED, we.State)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestArgoStatusToState_Default_Failed(t *testing.T) {
|
||||||
|
we := &workflow_execution.WorkflowExecution{}
|
||||||
|
we.ArgoStatusToState("failed")
|
||||||
|
assert.Equal(t, enum.FAILURE, we.State)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestArgoStatusToState_CaseInsensitive(t *testing.T) {
|
||||||
|
we := &workflow_execution.WorkflowExecution{}
|
||||||
|
we.ArgoStatusToState("SUCCEEDED")
|
||||||
|
assert.Equal(t, enum.SUCCESS, we.State)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestArgoStatusToState_ReturnsPointer(t *testing.T) {
|
||||||
|
we := &workflow_execution.WorkflowExecution{}
|
||||||
|
result := we.ArgoStatusToState("running")
|
||||||
|
assert.Equal(t, we, result)
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---- NewAccessor ----
|
||||||
|
|
||||||
|
func TestNewWorkflowExecutionAccessor(t *testing.T) {
|
||||||
|
acc := workflow_execution.NewAccessor(&tools.APIRequest{Admin: true})
|
||||||
|
assert.NotNil(t, acc)
|
||||||
|
assert.Equal(t, tools.WORKFLOW_EXECUTION, acc.GetType())
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestNewWorkflowExecutionAccessor_NilRequest(t *testing.T) {
|
||||||
|
acc := workflow_execution.NewAccessor(nil)
|
||||||
|
assert.NotNil(t, acc)
|
||||||
|
assert.Equal(t, "", acc.GetUser())
|
||||||
|
assert.Equal(t, "", acc.GetPeerID())
|
||||||
|
}
|
||||||
@@ -1,164 +0,0 @@
|
|||||||
package workflow_execution_test
|
|
||||||
|
|
||||||
import (
|
|
||||||
"testing"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"cloud.o-forge.io/core/oc-lib/models/common/enum"
|
|
||||||
"cloud.o-forge.io/core/oc-lib/models/utils"
|
|
||||||
"cloud.o-forge.io/core/oc-lib/models/workflow_execution"
|
|
||||||
"cloud.o-forge.io/core/oc-lib/tools"
|
|
||||||
"github.com/stretchr/testify/assert"
|
|
||||||
"github.com/stretchr/testify/mock"
|
|
||||||
)
|
|
||||||
|
|
||||||
type MockAccessor struct {
|
|
||||||
mock.Mock
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *MockAccessor) LoadOne(id string) (interface{}, int, error) {
|
|
||||||
args := m.Called(id)
|
|
||||||
return args.Get(0), args.Int(1), args.Error(2)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *MockAccessor) DeleteOne(id string) (utils.DBObject, int, error) {
|
|
||||||
args := m.Called(id)
|
|
||||||
return nil, args.Int(1), args.Error(2)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *MockAccessor) Search(filters interface{}, search string, isDraft bool) ([]utils.ShallowDBObject, int, error) {
|
|
||||||
args := m.Called(filters, search, isDraft)
|
|
||||||
return args.Get(0).([]utils.ShallowDBObject), args.Int(1), args.Error(2)
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestStoreDraftDefault(t *testing.T) {
|
|
||||||
exec := &workflow_execution.WorkflowExecution{}
|
|
||||||
exec.StoreDraftDefault()
|
|
||||||
assert.False(t, exec.IsDraft)
|
|
||||||
assert.Equal(t, enum.SCHEDULED, exec.State)
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestCanUpdate_StateChange(t *testing.T) {
|
|
||||||
existing := &workflow_execution.WorkflowExecution{State: enum.DRAFT}
|
|
||||||
newExec := &workflow_execution.WorkflowExecution{State: enum.SCHEDULED}
|
|
||||||
canUpdate, updated := existing.CanUpdate(newExec)
|
|
||||||
assert.True(t, canUpdate)
|
|
||||||
assert.Equal(t, enum.SCHEDULED, updated.(*workflow_execution.WorkflowExecution).State)
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestCanUpdate_SameState_Draft(t *testing.T) {
|
|
||||||
existing := &workflow_execution.WorkflowExecution{AbstractObject: utils.AbstractObject{IsDraft: true}, State: enum.DRAFT}
|
|
||||||
newExec := &workflow_execution.WorkflowExecution{AbstractObject: utils.AbstractObject{IsDraft: true}, State: enum.DRAFT}
|
|
||||||
canUpdate, _ := existing.CanUpdate(newExec)
|
|
||||||
assert.False(t, canUpdate)
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestCanDelete_TrueIfDraft(t *testing.T) {
|
|
||||||
exec := &workflow_execution.WorkflowExecution{AbstractObject: utils.AbstractObject{IsDraft: true}}
|
|
||||||
assert.True(t, exec.CanDelete())
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestCanDelete_FalseIfNotDraft(t *testing.T) {
|
|
||||||
exec := &workflow_execution.WorkflowExecution{AbstractObject: utils.AbstractObject{IsDraft: false}}
|
|
||||||
assert.False(t, exec.CanDelete())
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestEquals_True(t *testing.T) {
|
|
||||||
d := time.Now()
|
|
||||||
exec1 := &workflow_execution.WorkflowExecution{ExecDate: d, WorkflowID: "123"}
|
|
||||||
exec2 := &workflow_execution.WorkflowExecution{ExecDate: d, WorkflowID: "123"}
|
|
||||||
assert.True(t, exec1.Equals(exec2))
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestEquals_False(t *testing.T) {
|
|
||||||
exec1 := &workflow_execution.WorkflowExecution{ExecDate: time.Now(), WorkflowID: "abc"}
|
|
||||||
exec2 := &workflow_execution.WorkflowExecution{ExecDate: time.Now().Add(time.Hour), WorkflowID: "def"}
|
|
||||||
assert.False(t, exec1.Equals(exec2))
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestArgoStatusToState_Success(t *testing.T) {
|
|
||||||
exec := &workflow_execution.WorkflowExecution{}
|
|
||||||
exec.ArgoStatusToState("succeeded")
|
|
||||||
assert.Equal(t, enum.SUCCESS, exec.State)
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestArgoStatusToState_DefaultToFailure(t *testing.T) {
|
|
||||||
exec := &workflow_execution.WorkflowExecution{}
|
|
||||||
exec.ArgoStatusToState("unknown")
|
|
||||||
assert.Equal(t, enum.FAILURE, exec.State)
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestGenerateID_AssignsUUID(t *testing.T) {
|
|
||||||
exec := &workflow_execution.WorkflowExecution{}
|
|
||||||
exec.GenerateID()
|
|
||||||
assert.NotEmpty(t, exec.UUID)
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestGetName_ReturnsCorrectFormat(t *testing.T) {
|
|
||||||
time := time.Now()
|
|
||||||
exec := &workflow_execution.WorkflowExecution{AbstractObject: utils.AbstractObject{UUID: "abc"}, ExecDate: time}
|
|
||||||
assert.Contains(t, exec.GetName(), "abc")
|
|
||||||
assert.Contains(t, exec.GetName(), time.String())
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestVerifyAuth_AlwaysTrue(t *testing.T) {
|
|
||||||
exec := &workflow_execution.WorkflowExecution{}
|
|
||||||
assert.True(t, exec.VerifyAuth("get", nil))
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestUpdateOne_RejectsZeroState(t *testing.T) {
|
|
||||||
accessor := &workflow_execution.WorkflowExecutionMongoAccessor{}
|
|
||||||
set := &workflow_execution.WorkflowExecution{State: 0}
|
|
||||||
_, code, err := accessor.UpdateOne(set, "someID")
|
|
||||||
assert.Equal(t, 400, code)
|
|
||||||
assert.Error(t, err)
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestLoadOne_DraftExpired_ShouldDelete(t *testing.T) {
|
|
||||||
// Normally would mock time.Now and delete call; for now we test structure
|
|
||||||
accessor := workflow_execution.NewAccessor(&tools.APIRequest{})
|
|
||||||
exec := &workflow_execution.WorkflowExecution{
|
|
||||||
ExecDate: time.Now().Add(-2 * time.Minute),
|
|
||||||
State: enum.DRAFT,
|
|
||||||
AbstractObject: utils.AbstractObject{UUID: "to-delete"},
|
|
||||||
}
|
|
||||||
_, _, _ = accessor.LoadOne(exec.GetID())
|
|
||||||
// No panic = good enough placeholder
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestLoadOne_ScheduledExpired_ShouldUpdateToForgotten(t *testing.T) {
|
|
||||||
accessor := workflow_execution.NewAccessor(&tools.APIRequest{})
|
|
||||||
exec := &workflow_execution.WorkflowExecution{
|
|
||||||
ExecDate: time.Now().Add(-2 * time.Minute),
|
|
||||||
State: enum.SCHEDULED,
|
|
||||||
AbstractObject: utils.AbstractObject{UUID: "to-forget"},
|
|
||||||
}
|
|
||||||
_, _, _ = accessor.LoadOne(exec.GetID())
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestDeleteOne_NotImplemented(t *testing.T) {
|
|
||||||
accessor := workflow_execution.NewAccessor(&tools.APIRequest{})
|
|
||||||
_, code, err := accessor.DeleteOne("someID")
|
|
||||||
assert.Equal(t, 404, code)
|
|
||||||
assert.Error(t, err)
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestStoreOne_NotImplemented(t *testing.T) {
|
|
||||||
accessor := workflow_execution.NewAccessor(&tools.APIRequest{})
|
|
||||||
_, code, err := accessor.StoreOne(nil)
|
|
||||||
assert.Equal(t, 404, code)
|
|
||||||
assert.Error(t, err)
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestCopyOne_NotImplemented(t *testing.T) {
|
|
||||||
accessor := workflow_execution.NewAccessor(&tools.APIRequest{})
|
|
||||||
_, code, err := accessor.CopyOne(nil)
|
|
||||||
assert.Equal(t, 404, code)
|
|
||||||
assert.Error(t, err)
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestGetExecFilters_BasicPattern(t *testing.T) {
|
|
||||||
a := workflow_execution.NewAccessor(&tools.APIRequest{})
|
|
||||||
filters := a.GetExecFilters("foo")
|
|
||||||
assert.Contains(t, filters.Or["abstractobject.name"][0].Value, "foo")
|
|
||||||
}
|
|
||||||
@@ -12,17 +12,19 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type WorkflowExecutionMongoAccessor struct {
|
type WorkflowExecutionMongoAccessor struct {
|
||||||
utils.AbstractAccessor
|
utils.AbstractAccessor[*WorkflowExecution]
|
||||||
shallow bool
|
shallow bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func newShallowAccessor(request *tools.APIRequest) *WorkflowExecutionMongoAccessor {
|
func newShallowAccessor(request *tools.APIRequest) *WorkflowExecutionMongoAccessor {
|
||||||
return &WorkflowExecutionMongoAccessor{
|
return &WorkflowExecutionMongoAccessor{
|
||||||
shallow: true,
|
shallow: true,
|
||||||
AbstractAccessor: utils.AbstractAccessor{
|
AbstractAccessor: utils.AbstractAccessor[*WorkflowExecution]{
|
||||||
Logger: logs.CreateLogger(tools.WORKFLOW_EXECUTION.String()), // Create a logger with the data type
|
Logger: logs.CreateLogger(tools.WORKFLOW_EXECUTION.String()), // Create a logger with the data type
|
||||||
Request: request,
|
Request: request,
|
||||||
Type: tools.WORKFLOW_EXECUTION,
|
Type: tools.WORKFLOW_EXECUTION,
|
||||||
|
New: func() *WorkflowExecution { return &WorkflowExecution{} },
|
||||||
|
NotImplemented: []string{"DeleteOne", "StoreOne", "CopyOne"},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -30,18 +32,16 @@ func newShallowAccessor(request *tools.APIRequest) *WorkflowExecutionMongoAccess
|
|||||||
func NewAccessor(request *tools.APIRequest) *WorkflowExecutionMongoAccessor {
|
func NewAccessor(request *tools.APIRequest) *WorkflowExecutionMongoAccessor {
|
||||||
return &WorkflowExecutionMongoAccessor{
|
return &WorkflowExecutionMongoAccessor{
|
||||||
shallow: false,
|
shallow: false,
|
||||||
AbstractAccessor: utils.AbstractAccessor{
|
AbstractAccessor: utils.AbstractAccessor[*WorkflowExecution]{
|
||||||
Logger: logs.CreateLogger(tools.WORKFLOW_EXECUTION.String()), // Create a logger with the data type
|
Logger: logs.CreateLogger(tools.WORKFLOW_EXECUTION.String()), // Create a logger with the data type
|
||||||
Request: request,
|
Request: request,
|
||||||
Type: tools.WORKFLOW_EXECUTION,
|
Type: tools.WORKFLOW_EXECUTION,
|
||||||
|
New: func() *WorkflowExecution { return &WorkflowExecution{} },
|
||||||
|
NotImplemented: []string{"DeleteOne", "StoreOne", "CopyOne"},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (wfa *WorkflowExecutionMongoAccessor) DeleteOne(id string) (utils.DBObject, int, error) {
|
|
||||||
return nil, 404, errors.New("not implemented")
|
|
||||||
}
|
|
||||||
|
|
||||||
func (wfa *WorkflowExecutionMongoAccessor) UpdateOne(set utils.DBObject, id string) (utils.DBObject, int, error) {
|
func (wfa *WorkflowExecutionMongoAccessor) UpdateOne(set utils.DBObject, id string) (utils.DBObject, int, error) {
|
||||||
if set.(*WorkflowExecution).State == 0 {
|
if set.(*WorkflowExecution).State == 0 {
|
||||||
return nil, 400, errors.New("state is required")
|
return nil, 400, errors.New("state is required")
|
||||||
@@ -50,14 +50,6 @@ func (wfa *WorkflowExecutionMongoAccessor) UpdateOne(set utils.DBObject, id stri
|
|||||||
return utils.GenericUpdateOne(&realSet, id, wfa, &WorkflowExecution{})
|
return utils.GenericUpdateOne(&realSet, id, wfa, &WorkflowExecution{})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (wfa *WorkflowExecutionMongoAccessor) StoreOne(data utils.DBObject) (utils.DBObject, int, error) {
|
|
||||||
return nil, 404, errors.New("not implemented")
|
|
||||||
}
|
|
||||||
|
|
||||||
func (wfa *WorkflowExecutionMongoAccessor) CopyOne(data utils.DBObject) (utils.DBObject, int, error) {
|
|
||||||
return nil, 404, errors.New("not implemented")
|
|
||||||
}
|
|
||||||
|
|
||||||
func (a *WorkflowExecutionMongoAccessor) LoadOne(id string) (utils.DBObject, int, error) {
|
func (a *WorkflowExecutionMongoAccessor) LoadOne(id string) (utils.DBObject, int, error) {
|
||||||
return utils.GenericLoadOne[*WorkflowExecution](id, func(d utils.DBObject) (utils.DBObject, int, error) {
|
return utils.GenericLoadOne[*WorkflowExecution](id, func(d utils.DBObject) (utils.DBObject, int, error) {
|
||||||
now := time.Now()
|
now := time.Now()
|
||||||
@@ -73,16 +65,7 @@ func (a *WorkflowExecutionMongoAccessor) LoadOne(id string) (utils.DBObject, int
|
|||||||
return d, 200, nil
|
return d, 200, nil
|
||||||
}, a)
|
}, a)
|
||||||
}
|
}
|
||||||
|
func (a *WorkflowExecutionMongoAccessor) GetExec(isDraft bool) func(utils.DBObject) utils.ShallowDBObject {
|
||||||
func (a *WorkflowExecutionMongoAccessor) LoadAll(isDraft bool) ([]utils.ShallowDBObject, int, error) {
|
|
||||||
return utils.GenericLoadAll[*WorkflowExecution](a.getExec(), isDraft, a)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (a *WorkflowExecutionMongoAccessor) Search(filters *dbs.Filters, search string, isDraft bool) ([]utils.ShallowDBObject, int, error) {
|
|
||||||
return utils.GenericSearch[*WorkflowExecution](filters, search, a.GetExecFilters(search), a.getExec(), isDraft, a)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (a *WorkflowExecutionMongoAccessor) getExec() func(utils.DBObject) utils.ShallowDBObject {
|
|
||||||
return func(d utils.DBObject) utils.ShallowDBObject {
|
return func(d utils.DBObject) utils.ShallowDBObject {
|
||||||
now := time.Now()
|
now := time.Now()
|
||||||
now = now.Add(time.Second * -60)
|
now = now.Add(time.Second * -60)
|
||||||
@@ -99,7 +82,7 @@ func (a *WorkflowExecutionMongoAccessor) getExec() func(utils.DBObject) utils.Sh
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *WorkflowExecutionMongoAccessor) GetExecFilters(search string) *dbs.Filters {
|
func (a *WorkflowExecutionMongoAccessor) GetObjectFilters(search string) *dbs.Filters {
|
||||||
return &dbs.Filters{
|
return &dbs.Filters{
|
||||||
Or: map[string][]dbs.Filter{ // filter by name if no filters are provided
|
Or: map[string][]dbs.Filter{ // filter by name if no filters are provided
|
||||||
"abstractobject.name": {{Operator: dbs.LIKE.String(), Value: search + "_execution"}},
|
"abstractobject.name": {{Operator: dbs.LIKE.String(), Value: search + "_execution"}},
|
||||||
|
|||||||
@@ -18,34 +18,48 @@ type MockWorkspaceAccessor struct {
|
|||||||
workspace.Workspace
|
workspace.Workspace
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func safeDBObject(v interface{}) utils.DBObject {
|
||||||
|
if v == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
return v.(utils.DBObject)
|
||||||
|
}
|
||||||
|
|
||||||
|
func safeShallowList(v interface{}) []utils.ShallowDBObject {
|
||||||
|
if v == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
return v.([]utils.ShallowDBObject)
|
||||||
|
}
|
||||||
|
|
||||||
func (m *MockWorkspaceAccessor) StoreOne(data utils.DBObject) (utils.DBObject, int, error) {
|
func (m *MockWorkspaceAccessor) StoreOne(data utils.DBObject) (utils.DBObject, int, error) {
|
||||||
args := m.Called(data)
|
args := m.Called(data)
|
||||||
return args.Get(0).(utils.DBObject), args.Int(1), args.Error(2)
|
return safeDBObject(args.Get(0)), args.Int(1), args.Error(2)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *MockWorkspaceAccessor) UpdateOne(set utils.DBObject, id string) (utils.DBObject, int, error) {
|
func (m *MockWorkspaceAccessor) UpdateOne(set utils.DBObject, id string) (utils.DBObject, int, error) {
|
||||||
args := m.Called(set, id)
|
args := m.Called(set, id)
|
||||||
return args.Get(0).(utils.DBObject), args.Int(1), args.Error(2)
|
return safeDBObject(args.Get(0)), args.Int(1), args.Error(2)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *MockWorkspaceAccessor) DeleteOne(id string) (utils.DBObject, int, error) {
|
func (m *MockWorkspaceAccessor) DeleteOne(id string) (utils.DBObject, int, error) {
|
||||||
args := m.Called(id)
|
args := m.Called(id)
|
||||||
return args.Get(0).(utils.DBObject), args.Int(1), args.Error(2)
|
return safeDBObject(args.Get(0)), args.Int(1), args.Error(2)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *MockWorkspaceAccessor) LoadOne(id string) (utils.DBObject, int, error) {
|
func (m *MockWorkspaceAccessor) LoadOne(id string) (utils.DBObject, int, error) {
|
||||||
args := m.Called(id)
|
args := m.Called(id)
|
||||||
return args.Get(0).(utils.DBObject), args.Int(1), args.Error(2)
|
return safeDBObject(args.Get(0)), args.Int(1), args.Error(2)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *MockWorkspaceAccessor) LoadAll(isDraft bool) ([]utils.ShallowDBObject, int, error) {
|
func (m *MockWorkspaceAccessor) LoadAll(isDraft bool) ([]utils.ShallowDBObject, int, error) {
|
||||||
args := m.Called(isDraft)
|
args := m.Called(isDraft)
|
||||||
return args.Get(0).([]utils.ShallowDBObject), args.Int(1), args.Error(2)
|
return safeShallowList(args.Get(0)), args.Int(1), args.Error(2)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *MockWorkspaceAccessor) Search(filters *dbs.Filters, search string, isDraft bool) ([]utils.ShallowDBObject, int, error) {
|
func (m *MockWorkspaceAccessor) Search(filters *dbs.Filters, search string, isDraft bool) ([]utils.ShallowDBObject, int, error) {
|
||||||
args := m.Called(filters, search, isDraft)
|
args := m.Called(filters, search, isDraft)
|
||||||
return args.Get(0).([]utils.ShallowDBObject), args.Int(1), args.Error(2)
|
return safeShallowList(args.Get(0)), args.Int(1), args.Error(2)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestStoreOne_Success(t *testing.T) {
|
func TestStoreOne_Success(t *testing.T) {
|
||||||
|
|||||||
@@ -13,7 +13,7 @@ import (
|
|||||||
|
|
||||||
// Workspace is a struct that represents a workspace
|
// Workspace is a struct that represents a workspace
|
||||||
type workspaceMongoAccessor struct {
|
type workspaceMongoAccessor struct {
|
||||||
utils.AbstractAccessor // AbstractAccessor contains the basic fields of an accessor (model, caller)
|
utils.AbstractAccessor[*Workspace] // AbstractAccessor contains the basic fields of an accessor (model, caller)
|
||||||
}
|
}
|
||||||
|
|
||||||
// New creates a new instance of the workspaceMongoAccessor
|
// New creates a new instance of the workspaceMongoAccessor
|
||||||
@@ -28,10 +28,11 @@ func NewAccessor(request *tools.APIRequest) *workspaceMongoAccessor {
|
|||||||
// New creates a new instance of the workspaceMongoAccessor
|
// New creates a new instance of the workspaceMongoAccessor
|
||||||
func new(t tools.DataType, request *tools.APIRequest) *workspaceMongoAccessor {
|
func new(t tools.DataType, request *tools.APIRequest) *workspaceMongoAccessor {
|
||||||
return &workspaceMongoAccessor{
|
return &workspaceMongoAccessor{
|
||||||
utils.AbstractAccessor{
|
utils.AbstractAccessor[*Workspace]{
|
||||||
Logger: logs.CreateLogger(t.String()), // Create a logger with the data type
|
Logger: logs.CreateLogger(t.String()), // Create a logger with the data type
|
||||||
Request: request,
|
Request: request,
|
||||||
Type: t,
|
Type: t,
|
||||||
|
New: func() *Workspace { return &Workspace{} },
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -87,11 +88,6 @@ func (a *workspaceMongoAccessor) StoreOne(data utils.DBObject) (utils.DBObject,
|
|||||||
return utils.GenericStoreOne(d, a)
|
return utils.GenericStoreOne(d, a)
|
||||||
}
|
}
|
||||||
|
|
||||||
// CopyOne copies a workspace in the database
|
|
||||||
func (a *workspaceMongoAccessor) CopyOne(data utils.DBObject) (utils.DBObject, int, error) {
|
|
||||||
return utils.GenericStoreOne(data, a)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (a *workspaceMongoAccessor) LoadOne(id string) (utils.DBObject, int, error) {
|
func (a *workspaceMongoAccessor) LoadOne(id string) (utils.DBObject, int, error) {
|
||||||
return utils.GenericLoadOne[*Workspace](id, func(d utils.DBObject) (utils.DBObject, int, error) {
|
return utils.GenericLoadOne[*Workspace](id, func(d utils.DBObject) (utils.DBObject, int, error) {
|
||||||
d.(*Workspace).Fill(a.GetRequest())
|
d.(*Workspace).Fill(a.GetRequest())
|
||||||
|
|||||||
@@ -147,6 +147,15 @@ var Str = [...]string{
|
|||||||
"native_tool",
|
"native_tool",
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func FromString(comp string) int {
|
||||||
|
for i, str := range Str {
|
||||||
|
if str == comp {
|
||||||
|
return i
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return -1
|
||||||
|
}
|
||||||
|
|
||||||
func FromInt(i int) string {
|
func FromInt(i int) string {
|
||||||
return Str[i]
|
return Str[i]
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -54,8 +54,6 @@ type HTTPCallerITF interface {
|
|||||||
CallDelete(url string, subpath string) ([]byte, error)
|
CallDelete(url string, subpath string) ([]byte, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
var HTTPCallerInstance = &HTTPCaller{} // Singleton instance of the HTTPCaller
|
|
||||||
|
|
||||||
type HTTPCaller struct {
|
type HTTPCaller struct {
|
||||||
URLS map[DataType]map[METHOD]string // Map of the different methods and their urls
|
URLS map[DataType]map[METHOD]string // Map of the different methods and their urls
|
||||||
Disabled bool // Disabled flag
|
Disabled bool // Disabled flag
|
||||||
@@ -115,7 +113,7 @@ func (caller *HTTPCaller) CallDelete(url string, subpath string) ([]byte, error)
|
|||||||
}
|
}
|
||||||
client := &http.Client{}
|
client := &http.Client{}
|
||||||
resp, err := client.Do(req)
|
resp, err := client.Do(req)
|
||||||
if err != nil || req == nil || req.Body == nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
defer resp.Body.Close()
|
defer resp.Body.Close()
|
||||||
|
|||||||
Reference in New Issue
Block a user