tests and fixes for action triggers APIs

This commit is contained in:
Radu Ioan Fericean
2016-01-29 19:07:46 +02:00
parent cb8023eb22
commit aa43ed7ca9
2 changed files with 18 additions and 55 deletions

View File

@@ -1034,7 +1034,7 @@ func TestApierGetAccountActionTriggers(t *testing.T) {
req := AttrAcntAction{Tenant: "cgrates.org", Account: "dan2"}
if err := rater.Call("ApierV1.GetAccountActionTriggers", req, &reply); err != nil {
t.Error("Got error on ApierV1.GetAccountActionTimings: ", err.Error())
} else if len(reply) != 1 || reply[0].ActionsId != "WARN_VIA_HTTP" {
} else if len(reply) != 1 || reply[0].ActionsId != "LOG_BALANCE" {
t.Errorf("Unexpected action triggers received %v", reply)
}
}
@@ -1049,13 +1049,16 @@ func TestApierRemAccountActionTriggers(t *testing.T) {
req := AttrAcntAction{Tenant: "cgrates.org", Account: "dan2"}
if err := rater.Call("ApierV1.GetAccountActionTriggers", req, &reply); err != nil {
t.Error("Got error on ApierV1.GetAccountActionTimings: ", err.Error())
} else if len(reply) != 1 || reply[0].ActionsId != "WARN_VIA_HTTP" {
} else if len(reply) != 1 || reply[0].ActionsId != "LOG_BALANCE" {
for _, atr := range reply {
t.Logf("ATR: %+v", atr)
}
t.Errorf("Unexpected action triggers received %v", reply)
}
var rmReply string
rmReq := AttrRemAcntActionTriggers{Tenant: "cgrates.org", Account: "dan2", ActionTriggersUniqueId: reply[0].UniqueID}
if err := rater.Call("ApierV1.RemAccountActionTriggers", rmReq, &rmReply); err != nil {
t.Error("Got error on ApierV1.RemActionTiming: ", err.Error())
rmReq := AttrRemoveAccountActionTriggers{Tenant: "cgrates.org", Account: "dan2", UniqueID: reply[0].UniqueID}
if err := rater.Call("ApierV1.RemoveAccountActionTriggers", rmReq, &rmReply); err != nil {
t.Error("Got error on ApierV1.RemoveActionTiming: ", err.Error())
} else if rmReply != OK {
t.Error("Unexpected answer received", rmReply)
}
@@ -1101,13 +1104,13 @@ func TestApierGetAccountActionPlan(t *testing.T) {
t.Error("Unexpected action plan received")
} else {
if reply[0].ActionPlanId != "ATMS_1" {
t.Errorf("Unexpected ActionPlanId received")
t.Errorf("Unexpected ActionoveAccountPlanId received")
}
}
}
// Test here RemActionTiming
func TestApierRemActionTiming(t *testing.T) {
// Test here RemoveActionTiming
func TestApierRemUniqueIDActionTiming(t *testing.T) {
if !*testLocal {
return
}
@@ -1254,9 +1257,9 @@ func TestApierResetDataAfterLoadFromFolder(t *testing.T) {
if rcvStats.Destinations != 4 ||
rcvStats.RatingPlans != 3 ||
rcvStats.RatingProfiles != 3 ||
rcvStats.Actions != 5 ||
rcvStats.Actions != 6 ||
rcvStats.DerivedChargers != 2 {
t.Errorf("Calling ApierV1.GetCacheStats received: %v", rcvStats)
t.Errorf("Calling ApierV1.GetCacheStats received: %+v", rcvStats)
}
}
}

View File

@@ -1,8 +1,6 @@
package v1
import (
"regexp"
"github.com/cgrates/cgrates/engine"
"github.com/cgrates/cgrates/utils"
)
@@ -12,53 +10,15 @@ func (self *ApierV1) GetAccountActionTriggers(attrs AttrAcntAction, reply *engin
if missing := utils.MissingStructFields(&attrs, []string{"Tenant", "Account"}); len(missing) != 0 {
return utils.NewErrMandatoryIeMissing(missing...)
}
if balance, err := self.AccountDb.GetAccount(utils.AccountKey(attrs.Tenant, attrs.Account)); err != nil {
if account, err := self.AccountDb.GetAccount(utils.AccountKey(attrs.Tenant, attrs.Account)); err != nil {
return utils.NewErrServerError(err)
} else {
*reply = balance.ActionTriggers
}
return nil
}
type AttrRemAcntActionTriggers struct {
Tenant string // Tenant he account belongs to
Account string // Account name
ActionTriggersId string // Id filtering only specific id to remove (can be regexp pattern)
ActionTriggersUniqueId string
}
// Returns a list of ActionTriggers on an account
func (self *ApierV1) RemAccountActionTriggers(attrs AttrRemAcntActionTriggers, reply *string) error {
if missing := utils.MissingStructFields(&attrs, []string{"Tenant", "Account"}); len(missing) != 0 {
return utils.NewErrMandatoryIeMissing(missing...)
}
accID := utils.AccountKey(attrs.Tenant, attrs.Account)
_, err := engine.Guardian.Guard(func() (interface{}, error) {
ub, err := self.AccountDb.GetAccount(accID)
if err != nil {
return 0, err
ats := account.ActionTriggers
if ats == nil {
ats = engine.ActionTriggers{}
}
nactrs := make(engine.ActionTriggers, 0)
for _, actr := range ub.ActionTriggers {
match, _ := regexp.MatchString(attrs.ActionTriggersId, actr.ID)
if len(attrs.ActionTriggersId) != 0 && match {
continue
}
if len(attrs.ActionTriggersUniqueId) != 0 && attrs.ActionTriggersUniqueId == actr.UniqueID {
continue
}
nactrs = append(nactrs, actr)
}
ub.ActionTriggers = nactrs
if err := self.AccountDb.SetAccount(ub); err != nil {
return 0, err
}
return 0, nil
}, 0, accID)
if err != nil {
return utils.NewErrServerError(err)
*reply = ats
}
*reply = OK
return nil
}