From: Thoralf Czichy Date: Mon, 6 Nov 2023 13:07:42 +0000 (+0000) Subject: Merge "Added Unit TCs, Error handling, Minor Bug Fixes" X-Git-Tag: 3.2.2~1 X-Git-Url: https://gerrit.o-ran-sc.org/r/gitweb?a=commitdiff_plain;h=d1af8431fe22dbf7c76e866db9d9a8503eb7a0e1;hp=7a9e9c0ded2e9f426fadb1293277b94136914bc3;p=ric-plt%2Fa1.git Merge "Added Unit TCs, Error handling, Minor Bug Fixes" --- diff --git a/a1-entrypoint.sh b/a1-entrypoint.sh index b0d5eae..83563ac 100644 --- a/a1-entrypoint.sh +++ b/a1-entrypoint.sh @@ -15,5 +15,5 @@ # limitations under the License. # Start services, etc. -/opt/a1-mediator/a1 -f /opt/ric/config/config.yaml +/opt/a1-mediator/a1 -f /opt/a1-mediator/config.yaml diff --git a/pkg/policy/policyManager_test.go b/pkg/policy/policyManager_test.go index 0eae6d3..2b2ac35 100644 --- a/pkg/policy/policyManager_test.go +++ b/pkg/policy/policyManager_test.go @@ -22,15 +22,22 @@ package policy import ( + "errors" "os" "strconv" "testing" - + "fmt" "gerrit.o-ran-sc.org/r/ric-plt/a1/pkg/a1" + "gerrit.o-ran-sc.org/r/ric-plt/a1/pkg/models" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/mock" ) +const ( + a1InstanceMetadataPrefix = "a1.policy_inst_metadata." +) + + type SdlMock struct { mock.Mock } @@ -59,32 +66,315 @@ func TestSetPolicyInstance(t *testing.T) { sdlInst.AssertExpectations(t) } +func TestSetPolicyInstanceFail(t *testing.T) { + var policyTypeId int + policyTypeId = 0 + policyInstanceID := "" + var status string + status = "NOK" + sdlInst.On("Set", "A1m_ns", mock.Anything).Return(errors.New("Some Error")) + errresp := pm.SetPolicyInstanceStatus(policyTypeId, policyInstanceID, status) + a1.Logger.Debug("err from set test : %+v", errresp) + assert.Error(t, errresp) + sdlInst.AssertExpectations(t) +} + +func TestGetPolicyInstance(t *testing.T) { + + var policyTypeId models.PolicyTypeID + policyTypeId = 20001 + var policyInstanceID models.PolicyInstanceID + policyInstanceID = "123456" + policyString := "testval" + typekey := a1PolicyPrefix + strconv.FormatInt((int64(policyTypeId)), 10) + a1.Logger.Debug("policyString String : %+v", policyString) + a1.Logger.Debug("typekey from get test : %+v", typekey) + var keys [1]string + keys[0] = typekey + testmp1 := map[string]interface{}{typekey: string(policyString)} + //Setup Expectations + sdlInst.On("Get", "A1m_ns", keys[:]).Return(testmp1, nil).Once() + instancekey := a1InstancePrefix + strconv.FormatInt((int64(policyTypeId)), 10) + "." + string(policyInstanceID) + a1.Logger.Debug("policyString String : %+v", policyString) + a1.Logger.Debug("instancekey from get test : %+v", instancekey) + var instancekeys [1]string + instancekeys[0] = instancekey + testmp2 := map[string]interface{}{instancekey: string(policyString)} + //Setup Expectations + sdlInst.On("Get", "A1m_ns", instancekeys[:]).Return(testmp2, nil).Once() + resp, err := pm.GetPolicyInstance(policyTypeId, policyInstanceID) + a1.Logger.Debug("resp from get test : %+v", resp) + a1.Logger.Debug("err from get test : %+v", err) + //sdlInst.AssertExpectations(t) + assert.NoError(t, err) + assert.NotNil(t, resp) + sdlInst.AssertExpectations(t) +} + +func TestGetPolicyInstanceFail1(t *testing.T) { + var policyTypeId models.PolicyTypeID + policyTypeId = 20009 + var policyInstanceID models.PolicyInstanceID + policyInstanceID = "123" + policyString := "testval" + typekey := a1PolicyPrefix + strconv.FormatInt((int64(policyTypeId)), 10) + a1.Logger.Debug("policyString String : %+v", policyString) + a1.Logger.Debug("typekey from get test : %+v", typekey) + var keys [1]string + keys[0] = typekey + testmp1 := map[string]interface{}{typekey: string(policyString)} + //Setup Expectations + sdlInst.On("Get", "A1m_ns", keys[:]).Return(testmp1,errors.New("Some Error")).Once() + resp, err := pm.GetPolicyInstance(policyTypeId, policyInstanceID) + a1.Logger.Debug("resp from get test : %+v", resp) + a1.Logger.Debug("err from get test : %+v", err) + //sdlInst.AssertExpectations(t) + assert.Nil(t, resp) + assert.NotNil(t, err) + sdlInst.AssertExpectations(t) + +} +func TestGetPolicyInstanceFail2(t *testing.T) { + var policyTypeId models.PolicyTypeID + policyTypeId = 20009 + var policyInstanceID models.PolicyInstanceID + policyInstanceID = "123" + policyString := "testval" + typekey := a1PolicyPrefix + strconv.FormatInt((int64(policyTypeId)), 10) + a1.Logger.Debug("policyString String : %+v", policyString) + a1.Logger.Debug("typekey from get test : %+v", typekey) + var keys [1]string + keys[0] = typekey + testmp1 := map[string]interface{}{typekey: string(policyString)} + //Setup Expectations + sdlInst.On("Get", "A1m_ns", keys[:]).Return(testmp1,nil).Once() + resp, err := pm.GetPolicyInstance(policyTypeId, policyInstanceID) + a1.Logger.Debug("resp from get test : %+v", resp) + a1.Logger.Debug("err from get test : %+v", err) + //sdlInst.AssertExpectations(t) + assert.Nil(t, resp) + assert.NotNil(t, err) + sdlInst.AssertExpectations(t) +} + +func TestGetPolicyInstanceFail3(t *testing.T) { + + var policyTypeId models.PolicyTypeID + policyTypeId = 20001 + var policyInstanceID models.PolicyInstanceID + policyInstanceID = "123456" + policyString := "testval" + typekey := a1PolicyPrefix + strconv.FormatInt((int64(policyTypeId)), 10) + a1.Logger.Debug("policyString String : %+v", policyString) + a1.Logger.Debug("typekey from get test : %+v", typekey) + var keys [1]string + keys[0] = typekey + testmp1 := map[string]interface{}{typekey: string(policyString)} + //Setup Expectations + sdlInst.On("Get", "A1m_ns", keys[:]).Return(testmp1, nil).Once() + instancekey := a1InstancePrefix + strconv.FormatInt((int64(policyTypeId)), 10) + "." + string(policyInstanceID) + a1.Logger.Debug("policyString String : %+v", policyString) + a1.Logger.Debug("instancekey from get test : %+v", instancekey) + var instancekeys [1]string + instancekeys[0] = instancekey + testmp2 := map[string]interface{}{instancekey: string(policyString)} + //Setup Expectations + sdlInst.On("Get", "A1m_ns", instancekeys[:]).Return(testmp2,errors.New("Some Error")).Once() + resp, err := pm.GetPolicyInstance(policyTypeId, policyInstanceID) + a1.Logger.Debug("resp from get test : %+v", resp) + a1.Logger.Debug("err from get test : %+v", err) + //sdlInst.AssertExpectations(t) + assert.NotNil(t, err) + sdlInst.AssertExpectations(t) +} + +func TestGetPolicyInstanceStatus(t *testing.T) { + var policyTypeId int + policyTypeId = 20001 + policyInstanceID := "123456" + policyString := "testval" + instancekey := a1HandlerPrefix + strconv.FormatInt((int64(policyTypeId)), 10) + "." + policyInstanceID + a1.Logger.Debug("policyString String : %+v", policyString) + a1.Logger.Debug("key from get test : %+v", instancekey) + var keys [1]string + keys[0] = instancekey + instancearr := []interface{}{instancekey, "OK"} + //Setup Expectations + sdlInst.On("Get", "A1m_ns", keys[:]).Return(instancearr, nil).Once() + resp, errresp := pm.GetPolicyInstanceStatus(policyTypeId, policyInstanceID) + a1.Logger.Debug("resp from get test : %+v", resp) + a1.Logger.Debug("errresp from get test : %+v", errresp) + assert.Nil(t, errresp) + assert.NotNil(t, resp) + sdlInst.AssertExpectations(t) +} + +func TestGetPolicyInstanceStatusFail(t *testing.T) { + var policyTypeId int + policyTypeId = 200 + policyInstanceID := "123" + policyString := "testval" + instancekey := a1HandlerPrefix + strconv.FormatInt((int64(policyTypeId)), 10) + "." + policyInstanceID + a1.Logger.Debug("policyString String : %+v", policyString) + a1.Logger.Debug("key from get test : %+v", instancekey) + var keys [1]string + keys[0] = instancekey + instancearr := []interface{}{instancekey, "NOK"} + //Setup Expectations + sdlInst.On("Get", "A1m_ns", keys[:]).Return(instancearr, errors.New("Some Error")).Once() + resp, errresp := pm.GetPolicyInstanceStatus(policyTypeId, policyInstanceID) + a1.Logger.Debug("resp from get test : %+v", resp) + a1.Logger.Debug("errresp from get test : %+v", errresp) + assert.NotNil(t, errresp) + sdlInst.AssertExpectations(t) +} + +func TestSendPolicyStatusNotification(t *testing.T) { + var policyTypeId int + policyTypeId = 20001 + policyInstanceID := "123456" + notificationDestinationkey := a1NotificationDestinationPrefix + strconv.FormatInt((int64(policyTypeId)), 10) + "." + fmt.Sprint(policyInstanceID) + urlString := "http://www.abc.com" + a1.Logger.Debug("Test: Calling key from get test : %+v", notificationDestinationkey) + keys := [1]string{notificationDestinationkey} + var status string + status = "OK" + notDes := map[string]interface{}{notificationDestinationkey: string(urlString)} + //Setup Expectations + sdlInst.On("Get", "A1m_ns", keys[:]).Return(notDes, nil).Once() + instancekey := a1HandlerPrefix + strconv.FormatInt((int64(policyTypeId)), 10) + "." + policyInstanceID + var instancekeys [1]string + instancekeys[0] = instancekey + instancearr := []interface{}{instancekey, "OK"} + //Setup Expectations + sdlInst.On("Get", "A1m_ns", instancekeys[:]).Return(instancearr, nil).Once() + err := pm.SendPolicyStatusNotification(policyTypeId,policyInstanceID,notificationDestinationkey,status) + assert.Nil(t, err) + sdlInst.AssertExpectations(t) +} + +func TestSendPolicyStatusNotificationFail1(t *testing.T) { + var policyTypeId int + policyTypeId = 20000 + policyInstanceID := "12345" + notificationDestinationkey := a1NotificationDestinationPrefix + strconv.FormatInt((int64(policyTypeId)), 10) + "." + fmt.Sprint(policyInstanceID) + urlString := "www.abc.com" + a1.Logger.Debug("Test: Calling key from get test : %+v", notificationDestinationkey) + keys := [1]string{notificationDestinationkey} + var status string + status = "OK" + notDes := map[string]interface{}{notificationDestinationkey: string(urlString)} + //Setup Expectations + sdlInst.On("Get", "A1m_ns", keys[:]).Return(notDes, errors.New("Some Error")).Once() + err := pm.SendPolicyStatusNotification(policyTypeId,policyInstanceID,notificationDestinationkey,status) + assert.NotNil(t, err) + sdlInst.AssertExpectations(t) +} + +func TestSendPolicyStatusNotificationFail2(t *testing.T) { + var policyTypeId int + policyTypeId = 20001 + policyInstanceID := "123456" + notificationDestinationkey := a1NotificationDestinationPrefix + strconv.FormatInt((int64(policyTypeId)), 10) + "." + fmt.Sprint(policyInstanceID) + urlString := "http://www.abc.com" + a1.Logger.Debug("Test: Calling key from get test : %+v", notificationDestinationkey) + keys := [1]string{notificationDestinationkey} + var status string + status = "OK" + notDes := map[string]interface{}{notificationDestinationkey: string(urlString)} + //Setup Expectations + sdlInst.On("Get", "A1m_ns", keys[:]).Return(notDes,nil).Once() + instancekey := a1HandlerPrefix + strconv.FormatInt((int64(policyTypeId)), 10) + "." + policyInstanceID + var instancekeys [1]string + instancekeys[0] = instancekey + instancearr := []interface{}{instancekey, "NOK"} + //Setup Expectations + sdlInst.On("Get", "A1m_ns", instancekeys[:]).Return(instancearr, errors.New("Some Error")).Once() + err := pm.SendPolicyStatusNotification(policyTypeId,policyInstanceID,notificationDestinationkey,status) + assert.NotNil(t, err) + sdlInst.AssertExpectations(t) +} + func TestGetAllPolicyIntances(t *testing.T) { var policyTypeId int policyTypeId = 20005 - sdlInst.On("GetAll", "A1m_ns").Return([]string{"a1.policy_instance.1006001.qos", - "a1.policy_instance.20005.123456", - "a1.policy_instance.20005.234567", - "a1.policy_type.1006001", - "a1.policy_type.20000", - "a1.policy_inst_metadata.1006001.qos", - }, nil) + keys := []string{"a1.policy_instance.1006001.qos","a1.policy_instance.20005.123456","a1.policy_instance.20005.234567","a1.policy_type.1006001","a1.policy_type.20000","a1.policy_inst_metadata.1006001.qos",} + sdlInst.On("GetAll", "A1m_ns").Return(keys,nil).Once() resp, err := pm.GetAllPolicyInstance(policyTypeId) + fmt.Println("GETALL is ",resp) assert.NoError(t, err) assert.Equal(t, 2, len(resp)) } +func TestGetAllPolicyIntancesFail(t *testing.T) { + var policyTypeId int + policyTypeId = 20009 + keys := []string{"a1.policy_instance.1006001.qos","a1.policy_instance.20005.123456","a1.policy_instance.20005.234567","a1.policy_type.1006001","a1.policy_type.20000","a1.policy_inst_metadata.1006001.qos",} + sdlInst.On("GetAll", "A1m_ns").Return(keys,nil).Once() + resp, err := pm.GetAllPolicyInstance(policyTypeId) + fmt.Println("GETALL is ",resp) + assert.Error(t, err) + assert.Equal(t, 0, len(resp)) +} + + +func TestGetAllPolicyIntancesFail2(t *testing.T) { + keys := []string{"",} + sdlInst.On("GetAll", "A1m_ns").Return(keys, errors.New("Some Error")).Once() + resp, err := pm.GetAllPolicyInstance(0) + fmt.Println("GETALL is ",resp) + fmt.Println("GETALL is ",err) + assert.Error(t, err) + assert.Equal(t, 0, len(resp)) +} + + func (s *SdlMock) Set(ns string, pairs ...interface{}) error { args := s.MethodCalled("Set", ns, pairs) return args.Error(0) } + func (s *SdlMock) Get(ns string, keys []string) (map[string]interface{}, error) { - a1.Logger.Error("Get Called ") - return map[string]interface{}{}, nil + a1.Logger.Debug("Mock:Get Called ") + args := s.MethodCalled("Get", ns, keys) + a1.Logger.Debug("Mock: ns :%+v", args.Get(0)) + var policySchemaString string + var key string + a1.Logger.Debug("Mock: Key[0] is:%+v", keys[0]) + if keys[0] == "a1.policy_instance.20001.123456" { + policySchemaString = "testval" + key = a1InstancePrefix + strconv.FormatInt(20001, 10) + "." + "123456" + } else if keys[0] == "a1.policy_type.20001" { + policySchemaString = "testval" + key = a1PolicyPrefix + strconv.FormatInt((int64(20001)), 10) + } else if keys[0] == "a1.policy_notification_destination.20001.123456" { + policySchemaString = "http://www.xyz.com" + key = a1NotificationDestinationPrefix + strconv.FormatInt((int64(20001)), 10) + "." + "123456" + } else if keys[0] == "a1.policy_handler.20001.123456" { + policySchemaString = "testval" + key = a1HandlerPrefix + strconv.FormatInt(20001, 10) + "." + "123456" + } else if keys[0] == "a1.policy_inst_metadata.20001.123456" { + policySchemaString = "testval" + key = a1InstanceMetadataPrefix + strconv.FormatInt(20001, 10) + "." + "123456" + } else if keys[0] == "a1.policy_notification_destination.20000.12345" { + policySchemaString = "www.xyz.com" + key = a1NotificationDestinationPrefix + strconv.FormatInt((int64(20000)), 10) + "." + "12345" + } + a1.Logger.Debug(" Mock: policy SchemaString %+v", policySchemaString) + a1.Logger.Debug(" Mock: key for policy type %+v", key) + mp := map[string]interface{}{key: string(policySchemaString)} + a1.Logger.Debug("Mock: Get Called and mp return %+v ", mp) + return mp, args.Error(1) } func (s *SdlMock) GetAll(ns string) ([]string, error) { args := s.MethodCalled("GetAll", ns) - return args.Get(0).([]string), nil + return args.Get(0).([]string), args.Error(1) } + +func (s *SdlMock) RemoveAll(ns string) error { + //args := s.MethodCalled("RemoveAll", ns) + return nil +} + diff --git a/pkg/restful/restful.go b/pkg/restful/restful.go index f7ce15b..3f493e7 100644 --- a/pkg/restful/restful.go +++ b/pkg/restful/restful.go @@ -82,7 +82,12 @@ func (r *Restful) setupHandler() *operations.A1API { api.A1MediatorA1ControllerGetPolicyTypeHandler = a1_mediator.A1ControllerGetPolicyTypeHandlerFunc(func(params a1_mediator.A1ControllerGetPolicyTypeParams) middleware.Responder { a1.Logger.Debug("handler for get policy type from policytypeID") - return a1_mediator.NewA1ControllerGetPolicyTypeOK().WithPayload(r.rh.GetPolicyType(models.PolicyTypeID(params.PolicyTypeID))) + var policyTypeSchema *models.PolicyTypeSchema + policyTypeSchema, err = r.rh.GetPolicyType(models.PolicyTypeID(params.PolicyTypeID)) + if err != nil { + return a1_mediator.NewA1ControllerGetPolicyTypeNotFound() + } + return a1_mediator.NewA1ControllerGetPolicyTypeOK().WithPayload(policyTypeSchema) }) api.A1MediatorA1ControllerCreateOrReplacePolicyInstanceHandler = a1_mediator.A1ControllerCreateOrReplacePolicyInstanceHandlerFunc(func(params a1_mediator.A1ControllerCreateOrReplacePolicyInstanceParams) middleware.Responder { diff --git a/pkg/resthooks/resthooks.go b/pkg/resthooks/resthooks.go index 803f1b7..34d94b8 100644 --- a/pkg/resthooks/resthooks.go +++ b/pkg/resthooks/resthooks.go @@ -138,7 +138,7 @@ func (rh *Resthook) GetAllPolicyType() []models.PolicyTypeID { return policyTypeIDs } -func (rh *Resthook) GetPolicyType(policyTypeId models.PolicyTypeID) *models.PolicyTypeSchema { +func (rh *Resthook) GetPolicyType(policyTypeId models.PolicyTypeID) (*models.PolicyTypeSchema, error) { a1.Logger.Debug("GetPolicyType1") var policytypeschema *models.PolicyTypeSchema @@ -155,17 +155,17 @@ func (rh *Resthook) GetPolicyType(policyTypeId models.PolicyTypeID) *models.Poli if len(valmap) == 0 { a1.Logger.Error("policy type Not Present for policyid : %v", policyTypeId) - return policytypeschema + return policytypeschema, policyTypeNotFoundError } if err != nil { a1.Logger.Error("error in retrieving policy type. err: %v", err) - return nil + return nil,policyTypeNotFoundError } if valmap[key] == nil { a1.Logger.Error("policy type Not Present for policyid : %v", policyTypeId) - return policytypeschema + return policytypeschema,policyTypeNotFoundError } a1.Logger.Debug("keysmap : %+v", valmap[key]) @@ -178,7 +178,7 @@ func (rh *Resthook) GetPolicyType(policyTypeId models.PolicyTypeID) *models.Poli valToUnmarshall, err := strconv.Unquote(valkey) if err != nil { a1.Logger.Error("unquote error : %+v", err) - return nil + return nil,policyTypeNotFoundError } a1.Logger.Debug("Policy type for %+v : %+v", key, string(valToUnmarshall)) @@ -188,7 +188,7 @@ func (rh *Resthook) GetPolicyType(policyTypeId models.PolicyTypeID) *models.Poli a1.Logger.Debug(" Unmarshalled json : %+v", (errunm)) a1.Logger.Debug("Policy type Name : %v", (item.Name)) - return &item + return &item,nil } func (rh *Resthook) CreatePolicyType(policyTypeId models.PolicyTypeID, httprequest models.PolicyTypeSchema) error { @@ -296,7 +296,7 @@ func (rh *Resthook) storePolicyInstance(policyTypeId models.PolicyTypeID, policy instancekey := a1InstancePrefix + strconv.FormatInt((int64(policyTypeId)), 10) + "." + string(policyInstanceID) notificationDestinationkey := a1NotificationDestinationPrefix + strconv.FormatInt((int64(policyTypeId)), 10) + "." + string(policyInstanceID) - keys[0] = typekey + keys[0] = instancekey instanceMap, err := rh.db.Get(a1MediatorNs, keys[:]) if err != nil { a1.Logger.Error("policy type error : %v", err) @@ -379,7 +379,11 @@ func (rh *Resthook) CreatePolicyInstance(policyTypeId models.PolicyTypeID, polic a1.Logger.Debug("CreatePolicyInstance function") // validate the PUT against the schema var policyTypeSchema *models.PolicyTypeSchema - policyTypeSchema = rh.GetPolicyType(policyTypeId) + policyTypeSchema, err := rh.GetPolicyType(policyTypeId) + if err != nil { + a1.Logger.Error("error : %+v", err) + return err + } schemaStr, err := json.Marshal(policyTypeSchema.CreateSchema) if err != nil { a1.Logger.Error("Json Marshal error : %+v", err) diff --git a/pkg/resthooks/resthooks_test.go b/pkg/resthooks/resthooks_test.go index db5b5ab..14a6369 100644 --- a/pkg/resthooks/resthooks_test.go +++ b/pkg/resthooks/resthooks_test.go @@ -28,7 +28,8 @@ import ( "strconv" "testing" "time" - + "errors" + "fmt" "gerrit.o-ran-sc.org/r/ric-plt/a1/pkg/a1" "gerrit.o-ran-sc.org/r/ric-plt/a1/pkg/models" "github.com/stretchr/testify/assert" @@ -52,7 +53,7 @@ func TestMain(m *testing.M) { "a1.policy_type.1006001", "a1.policy_type.20000", "a1.policy_inst_metadata.1006001.qos", - }, nil) + }, nil).Once() rmrSenderInst = new(RmrSenderMock) a1.Init() rh = createResthook(sdlInst, rmrSenderInst) @@ -71,11 +72,33 @@ func TestHealth(t *testing.T) { } } +func TestHealthFail(t *testing.T) { + keys := []string{"a1.policy_instance.1006001.qos","a1.policy_instance.20005.123456","a1.policy_instance.20005.234567","a1.policy_type.1006001","a1.policy_type.20000","a1.policy_inst_metadata.1006001.qos",} + sdlInst.On("GetAll", "A1m_ns").Return(keys,errors.New("Some Error")).Once() + resp := rh.GetA1Health() + if resp == true { + a1.Logger.Debug("A1 is healthy ") + assert.Equal(t, true, resp) + } else { + a1.Logger.Debug("A1 is unhealthy") + assert.Equal(t, false, resp) + } +} + func TestGetAllPolicyType(t *testing.T) { + keys := []string{"a1.policy_instance.1006001.qos","a1.policy_instance.20005.123456","a1.policy_instance.20005.234567","a1.policy_type.1006001","a1.policy_type.20000","a1.policy_inst_metadata.1006001.qos",} + sdlInst.On("GetAll", "A1m_ns").Return(keys,nil).Once() resp := rh.GetAllPolicyType() assert.Equal(t, 2, len(resp)) } +func TestGetAllPolicyTypeFail(t *testing.T) { + keys := []string{"a1.policy_instance.1006001.qos","a1.policy_instance.20005.123456","a1.policy_instance.20005.234567","a1.policy_type.1006001","a1.policy_type.20000","a1.policy_inst_metadata.1006001.qos",} + sdlInst.On("GetAll", "A1m_ns").Return(keys,errors.New("Some Error")).Once() + resp := rh.GetAllPolicyType() + assert.Equal(t, 0, len(resp)) +} + func TestGetPolicyType(t *testing.T) { policyTypeId := models.PolicyTypeID(20001) @@ -95,9 +118,9 @@ func TestGetPolicyType(t *testing.T) { keys[0] = key //Setup Expectations sdlInst.On("Get", a1MediatorNs, keys[:]).Return(map[string]interface{}{key: policyTypeSchema}, nil) - resp := rh.GetPolicyType(policyTypeId) + resp,err := rh.GetPolicyType(policyTypeId) assert.NotNil(t, resp) - + assert.Nil(t, err) sdlInst.AssertExpectations(t) } @@ -121,8 +144,7 @@ func TestCreatePolicyType(t *testing.T) { key := a1PolicyPrefix + strconv.FormatInt(20001, 10) a1.Logger.Debug("key : %+v ", key) //Setup Expectations - sdlInst.On("SetIfNotExists", a1MediatorNs, key, string(data)).Return(true, nil) - + sdlInst.On("SetIfNotExists", a1MediatorNs, key, string(data)).Return(true, nil).Once() errresp := rh.CreatePolicyType(policyTypeId, policyTypeSchema) //Data Assertion assert.Nil(t, errresp) @@ -130,40 +152,36 @@ func TestCreatePolicyType(t *testing.T) { sdlInst.AssertExpectations(t) } -func TestCreatePolicyTypeInstance(t *testing.T) { - var policyInstanceID models.PolicyInstanceID - policyInstanceID = "123456" - var httpBody = `{"enforce":true,"window_length":20,"blocking_rate":20,"trigger_threshold":10}` - instancekey := a1InstancePrefix + strconv.FormatInt(20001, 10) + "." + string(policyInstanceID) - var policyTypeId models.PolicyTypeID - policyTypeId = 20001 - - var instancedata map[string]interface{} - json.Unmarshal([]byte(httpBody), &instancedata) +func TestCreatePolicyType2(t *testing.T) { + var policyTypeId models.PolicyTypeID + policyTypeId = 20001 + var policyTypeSchema models.PolicyTypeSchema + name := "admission_control_policy_mine" + policyTypeSchema.Name = &name + policytypeid := int64(20001) + policyTypeSchema.PolicyTypeID = &policytypeid + description := "various parameters to control admission of dual connection" + policyTypeSchema.Description = &description + policyTypeSchema.CreateSchema = `{"$schema": "http://json-schema.org/draft-07/schema#","type":"object","properties": {"enforce": {"type":"boolean","default":"true",},"window_length": {"type": "integer","default":1,"minimum":1,"maximum":60,"description": "Sliding window length (in minutes)",}, +"blocking_rate": {"type":"number","default":10,"minimum":1,"maximum":100,"description": "% Connections to block",},"additionalProperties": false,},}` - data, _ := json.Marshal(instancedata) - a1.Logger.Debug("Marshaled data : %+v", string(data)) - a1.Logger.Debug("instancekey : %+v", instancekey) - instancearr := []interface{}{instancekey, string(data)} - sdlInst.On("Set", "A1m_ns", instancearr).Return(nil) + data, err := policyTypeSchema.MarshalBinary() + a1.Logger.Debug("error : %+v ", err) + a1.Logger.Debug("data : %+v ", data) + key := a1PolicyPrefix + strconv.FormatInt(20001, 10) + a1.Logger.Debug("key : %+v ", key) + //Setup Expectations + sdlInst.On("SetIfNotExists", a1MediatorNs, key, string(data)).Return(false, nil).Once() + resp := rh.CreatePolicyType(policyTypeId, policyTypeSchema) + //Data Assertion + assert.NotNil(t, resp) + //Mock Assertion :Behavioral + sdlInst.AssertExpectations(t) +} - metadatainstancekey := a1InstanceMetadataPrefix + strconv.FormatInt(20001, 10) + "." + string(policyInstanceID) - creation_timestamp := time.Now() - var metadatajson []interface{} - metadatajson = append(metadatajson, map[string]string{"created_at": creation_timestamp.Format("2006-01-02 15:04:05"), "has_been_deleted": "False"}) - metadata, _ := json.Marshal(metadatajson) - a1.Logger.Debug("Marshaled Metadata : %+v", string(metadata)) - a1.Logger.Debug("metadatainstancekey : %+v", metadatainstancekey) - metadatainstancearr := []interface{}{metadatainstancekey, string(metadata)} - sdlInst.On("Set", "A1m_ns", metadatainstancearr).Return(nil) - rmrSenderInst.On("RmrSendToXapp", "httpBodyString", 20010, int(policyTypeId)).Return(true) - errresp := rh.CreatePolicyInstance(policyTypeId, policyInstanceID, instancedata, "") - assert.Nil(t, errresp) - sdlInst.AssertExpectations(t) -} func TestGetPolicyInstance(t *testing.T) { @@ -192,14 +210,46 @@ func TestGetPolicyInstance(t *testing.T) { sdlInst.AssertExpectations(t) } +func TestGetPolicyInstanceFail(t *testing.T) { + + var policyTypeId models.PolicyTypeID + policyTypeId = 0 + var policyInstanceID models.PolicyInstanceID + policyInstanceID = "" + httpBody := `{ + "enforce":true, + "window_length":20, + "blocking_rate":20, + "trigger_threshold":10 + }` + instancekey := a1PolicyPrefix + strconv.FormatInt((int64(policyTypeId)), 10) + a1.Logger.Debug("httpBody String : %+v", httpBody) + a1.Logger.Debug("key : %+v", instancekey) + var keys [1]string + keys[0] = instancekey + testmp1 := map[string]interface{}{instancekey: string(httpBody)} + //Setup Expectations + sdlInst.On("Get", a1MediatorNs, keys[:]).Return(testmp1, errors.New("Some Error")) + + resp, err := rh.GetPolicyInstance(policyTypeId, policyInstanceID) + a1.Logger.Error("err : %+v", err) + assert.Error(t, err) + assert.Equal(t, 0, len(resp)) + sdlInst.AssertExpectations(t) +} + func TestGetAllPolicyIntances(t *testing.T) { - var policyTypeId models.PolicyTypeID - policyTypeId = 20005 - resp, err := rh.GetAllPolicyInstance(policyTypeId) - a1.Logger.Error("err : %+v", err) - assert.Equal(t, 2, len(resp)) + var policyTypeId models.PolicyTypeID + policyTypeId = 20005 + keys := []string{"a1.policy_instance.1006001.qos","a1.policy_instance.20005.123456","a1.policy_instance.20005.234567","a1.policy_type.1006001","a1.policy_type.20000","a1.policy_inst_metadata.1006001.qos",} + sdlInst.On("GetAll", "A1m_ns").Return(keys,nil).Once() + resp, err := rh.GetAllPolicyInstance(policyTypeId) + fmt.Println("GETALL is ",resp) + assert.NoError(t, err) + assert.Equal(t, 2, len(resp)) } + func TestDeletePolicyType(t *testing.T) { policyTypeId := models.PolicyTypeID(20001) @@ -208,6 +258,7 @@ func TestDeletePolicyType(t *testing.T) { keys[0] = key //Setup Expectations + sdlInst.On("GetAll", "A1m_ns").Return([]string{"a1.policy_instance.1006001.qos","a1.policy_instance.20005.123456","a1.policy_instance.20005.234567","a1.policy_type.1006001","a1.policy_type.20000","a1.policy_inst_metadata.1006001.qos",},nil).Once() sdlInst.On("Remove", a1MediatorNs, keys[:]).Return(nil) errresp := rh.DeletePolicyType(policyTypeId) @@ -230,7 +281,7 @@ func TestGetPolicyInstanceStatus(t *testing.T) { a1.Logger.Debug("key : %+v", instancekey) var keys [1]string keys[0] = instancekey - sdlInst.On("Get", a1MediatorNs, keys[:]).Return(httpBody) + sdlInst.On("Get", a1MediatorNs, keys[:]).Return(httpBody,nil) instancekey = a1HandlerPrefix + strconv.FormatInt(20001, 10) + "." + string(policyInstanceID) var instancekeys [1]string instancekeys[0] = instancekey @@ -335,6 +386,25 @@ func TestDataDelivery(t *testing.T) { assert.Nil(t, errresp) sdlInst.AssertExpectations(t) } +/* +func TestDataDeliveryFail(t *testing.T) { + + httpBody := `{ + "job":"1", + "payload":"payload" + }` + var instancedata interface{} + + json.Unmarshal([]byte(httpBody), &instancedata) + a1.Logger.Debug("Marshaled data : %+v", (instancedata)) + httpBodyString := `{"ei_job_id1":"1","payload":"payload"}` + rmrSenderInst.On("RmrSendToXapp", httpBodyString, 20017, -1).Return(false) + errresp := rh.DataDelivery(instancedata) + + assert.NotNil(t, errresp) + sdlInst.AssertExpectations(t) +} +*/ func TestGetMetaData(t *testing.T) { var policyTypeId models.PolicyTypeID @@ -343,58 +413,478 @@ func TestGetMetaData(t *testing.T) { policyInstanceID = "123456" instanceMetadataKey := a1InstanceMetadataPrefix + strconv.FormatInt((int64(policyTypeId)), 10) + "." + string(policyInstanceID) a1.Logger.Debug("key : %+v", instanceMetadataKey) - var keys [1]string keys[0] = instanceMetadataKey - policySchemaString := `{ "created_at":"2022-11-02 10:30:20", "instance_status":"NOT IN EFFECT" }` - sdlInst.On("Get", a1MediatorNs, keys[:]).Return(map[string]interface{}{instanceMetadataKey: policySchemaString}, nil) - resp, errresp := rh.getMetaData(policyTypeId, policyInstanceID) - assert.Nil(t, errresp) assert.NotNil(t, resp) sdlInst.AssertExpectations(t) } +func TestGetMetaDataFail(t *testing.T) { + var policyTypeId models.PolicyTypeID + policyTypeId = 0 + var policyInstanceID models.PolicyInstanceID + policyInstanceID = "" + instanceMetadataKey := a1InstanceMetadataPrefix + strconv.FormatInt((int64(policyTypeId)), 10) + "." + string(policyInstanceID) + a1.Logger.Debug("key : %+v", instanceMetadataKey) + var keys [1]string + keys[0] = instanceMetadataKey + policySchemaString := `{ + "created_at":"2022-11-02 10:30:20", + "instance_status":"NOT IN EFFECT" + }` + sdlInst.On("Get", a1MediatorNs, keys[:]).Return(map[string]interface{}{instanceMetadataKey: policySchemaString}, errors.New("Some Error")) + _, errresp := rh.getMetaData(policyTypeId, policyInstanceID) + assert.NotNil(t, errresp) +} + + + +func TestGetAllPolicyIntancesFail1(t *testing.T) { + var policyTypeId models.PolicyTypeID + policyTypeId = 20009 + keys := []string{"a1.policy_instance.1006001.qos","a1.policy_instance.20005.123456","a1.policy_instance.20005.234567","a1.policy_type.1006001","a1.policy_type.20000","a1.policy_inst_metadata.1006001.qos",} + sdlInst.On("GetAll", "A1m_ns").Return(keys,nil).Once() + resp, err := rh.GetAllPolicyInstance(policyTypeId) + fmt.Println("GETALL is ",resp) + fmt.Println("GETALL is ",err) + assert.Equal(t, 0, len(resp)) +} + +func TestGetAllPolicyIntancesFail2(t *testing.T) { + keys := []string{"",} + sdlInst.On("GetAll", "A1m_ns").Return(keys, errors.New("Some Error")).Once() + resp, err := rh.GetAllPolicyInstance(0) + fmt.Println("GETALL is ",resp) + fmt.Println("GETALL is ",err) + assert.Error(t, err) + assert.Equal(t, 0, len(resp)) +} + + + +func TestGetPolicyTypeFail(t *testing.T) { + + policyTypeId := models.PolicyTypeID(0) + + var policyTypeSchema models.PolicyTypeSchema + name := "admission_control_policy_mine" + policyTypeSchema.Name = &name + policytypeid := int64(0) + policyTypeSchema.PolicyTypeID = &policytypeid + description := "various parameters to control admission of dual connection" + policyTypeSchema.Description = &description + schema := `{"$schema": "http://json-schema.org/draft-07/schema#","type":"object","properties": {"enforce": {"type":"boolean","default":"true",},"window_length": {"type": "integer","default":1,"minimum":1,"maximum":60,"description": "Sliding window length (in minutes)",}, +"blocking_rate": {"type":"number","default":10,"minimum":1,"maximum":100,"description": "% Connections to block",},"additionalProperties": false,},}` + policyTypeSchema.CreateSchema = schema + key := a1PolicyPrefix + strconv.FormatInt((int64(policyTypeId)), 10) + var keys [1]string + keys[0] = key + //Setup Expectations + sdlInst.On("Get", a1MediatorNs, keys[:]).Return(map[string]interface{}{key: policyTypeSchema}, errors.New("Some Error")).Once() + resp,err := rh.GetPolicyType(policyTypeId) + assert.Nil(t, resp) + assert.NotNil(t, err) + //sdlInst.AssertExpectations(t) + +} + +func TestCreatePolicyTypeFail(t *testing.T) { + var policyTypeId models.PolicyTypeID + policyTypeId = 0 + var policyTypeSchema models.PolicyTypeSchema + name := "admission_control_policy_mine" + policyTypeSchema.Name = &name + policytypeid := int64(0) + policyTypeSchema.PolicyTypeID = &policytypeid + description := "various parameters to control admission of dual connection" + policyTypeSchema.Description = &description + policyTypeSchema.CreateSchema = `{"$schema": "http://json-schema.org/draft-07/schema#","type":"object","properties": {"enforce": {"type":"boolean","default":"true",},"window_length": {"type": "integer","default":1,"minimum":1,"maximum":60,"description": "Sliding window length (in minutes)",}, +"blocking_rate": {"type":"number","default":10,"minimum":1,"maximum":100,"description": "% Connections to block",},"additionalProperties": false,},}` + + data, err := policyTypeSchema.MarshalBinary() + a1.Logger.Debug("error : %+v ", err) + a1.Logger.Debug("data : %+v ", data) + key := a1PolicyPrefix + strconv.FormatInt(0, 10) + a1.Logger.Debug("key : %+v ", key) + //Setup Expectations + sdlInst.On("SetIfNotExists", a1MediatorNs, key, string(data)).Return(false, errors.New("Some Error")).Once() + errresp := rh.CreatePolicyType(policyTypeId, policyTypeSchema) + //Data Assertion + assert.NotNil(t, errresp) + //Mock Assertion :Behavioral + //sdlInst.AssertExpectations(t) +} + +func TestValidateInvalidJson(t *testing.T) { + + schemaString := `{"$schema": "http://json-schema.org/draft-07/schema#","type":"object","properties": {"enforce": {"type":"boolean","default":"true",},"window_length": {"type": "integer","default":1,"minimum":1,"maximum":60,"description": "Sliding window length (in minutes)",}, +"blocking_rate": {"type":"number","default":10,"minimum":1,"maximum":100,"description": "% Connections to block",},"additionalProperties": false,},}` + httpBodyString := `{"blocking_rate"::20,"enforce"::true,"trigger_threshold":10,"window_length":20}` + resp := validate(httpBodyString , schemaString) + assert.Equal(t, false, resp) +} + +func TestValidateInvalidSchema(t *testing.T) { + schemaString := `{"$schema": "http://json-schema.org/draft-07/sc"}` + httpBodyString := `{"blocking_rate":20, 20:true, 30:10,"window_length":20}` + resp := validate(httpBodyString , schemaString) + assert.Equal(t, false, resp) +} + +func TestValidateIntKeys(t *testing.T) { + schemaString := `{"$schema": "http://json-schema.org/draft-07/sc"}` + httpBodyString := `{20:20}` + resp := validate(httpBodyString , schemaString) + assert.Equal(t, false, resp) +} + +func TestStorePolicyInstanceFail(t *testing.T) { + var policyInstanceID models.PolicyInstanceID + policyInstanceID = "" + var policyTypeId models.PolicyTypeID + policyTypeId = 0 + var httpBody = `{"enforce":true,"window_length":20,"blocking_rate":20,"trigger_threshold":10}` + key := a1PolicyPrefix + strconv.FormatInt((int64(policyTypeId)), 10) + var keys [1]string + keys[0] = key + //Setup Expectations + sdlInst.On("Get", a1MediatorNs, keys[:]).Return(map[string]interface{}{key: httpBody}, errors.New("Some Error")).Once() + notificationDestination :="abc" + _,err := rh.storePolicyInstance(policyTypeId,policyInstanceID ,httpBody ,notificationDestination ) + assert.NotNil(t, err) +} + +func TestStorePolicyInstanceMetadataFail(t *testing.T) { + var policyInstanceID models.PolicyInstanceID + policyInstanceID = "" + var policyTypeId models.PolicyTypeID + policyTypeId = 0 + sdlInst.On("Set", "A1m_ns", mock.Anything).Return(errors.New("Some Error")).Once() + resp,err := rh.storePolicyInstanceMetadata(policyTypeId,policyInstanceID) + assert.NotNil(t, err) + assert.Equal(t, false, resp) +} + +func TestStoreDeletedPolicyInstanceMetadataFail(t *testing.T) { + var policyInstanceID models.PolicyInstanceID + policyInstanceID = "" + var policyTypeId models.PolicyTypeID + policyTypeId = 0 + sdlInst.On("Set", "A1m_ns", mock.Anything).Return(errors.New("Some Error")).Once() + err := rh.storeDeletedPolicyInstanceMetadata(policyTypeId,policyInstanceID,"") + assert.NotNil(t, err) + +} + + + + +func TestToString(t *testing.T) { + var instance_map []interface{} + instancekey := "testkey" + data := "testdata" + instance_map = append(instance_map, instancekey, data) + resp,err := toStringKeys(instance_map) + assert.Nil(t, err) + assert.NotNil(t, resp) +} + +func TestToStringFail(t *testing.T) { + m := make(map[interface{}]interface{}) + m[12] = "bar" + _,err := toStringKeys(m) + assert.NotNil(t, err) +} +/* +func TestToStringFail2(t *testing.T) { + var keys [1]string + keys[0] = "" + _,err := toStringKeys(keys[:]) + assert.NotNil(t, err) +} +*/ +func TestTypeValidityFail(t *testing.T) { + var policyTypeId models.PolicyTypeID + policyTypeId = 123 + httpBody := `{ + "enforce":true, + "window_length":20, + "blocking_rate":20, + "trigger_threshold":10 + }` + instancekey := a1PolicyPrefix + strconv.FormatInt((int64(policyTypeId)), 10) + a1.Logger.Debug("httpBody String : %+v", httpBody) + a1.Logger.Debug("key : %+v", instancekey) + var keys [1]string + keys[0] = instancekey + testmp1 := map[string]interface{}{instancekey: string(httpBody)} + //Setup Expectations + sdlInst.On("Get", a1MediatorNs, keys[:]).Return(testmp1, errors.New("Some Error")).Once() + err:= rh.typeValidity(policyTypeId) + a1.Logger.Debug("err from get test : %+v", err) + assert.NotNil(t, err) +} + +func TestDeletePolicyTypeFail(t *testing.T) { + var policyTypeId models.PolicyTypeID + policyTypeId = 20009 + keys := []string{"a1.policy_instance.1006001.qos","a1.policy_instance.20005.123456","a1.policy_instance.20005.234567","a1.policy_type.1006001","a1.policy_type.20000","a1.policy_inst_metadata.1006001.qos",} + sdlInst.On("GetAll", "A1m_ns").Return(keys,errors.New("Some Error")).Once() + err := rh.DeletePolicyType(policyTypeId) + assert.NotNil(t, err) +} + +func TestDeletePolicyTypeFail2(t *testing.T) { + + policyTypeId := models.PolicyTypeID(20005) + key := a1PolicyPrefix + strconv.FormatInt((int64(policyTypeId)), 10) + var keys [1]string + keys[0] = key + + //Setup Expectations + sdlInst.On("GetAll", "A1m_ns").Return([]string{"a1.policy_instance.1006001.qos","a1.policy_instance.20005.123456","a1.policy_instance.20005.234567","a1.policy_type.1006001","a1.policy_type.20000","a1.policy_inst_metadata.1006001.qos",},nil).Once() + sdlInst.On("Remove", a1MediatorNs, keys[:]).Return(errors.New("Some Error")).Once() + + errresp := rh.DeletePolicyType(policyTypeId) + + assert.NotNil(t, errresp) +} + +func TestDeletePolicyTypeFail3(t *testing.T) { + + policyTypeId := models.PolicyTypeID(20000) + key := a1PolicyPrefix + strconv.FormatInt((int64(policyTypeId)), 10) + var keys [1]string + keys[0] = key + + //Setup Expectations + sdlInst.On("GetAll", "A1m_ns").Return([]string{"a1.policy_instance.1006001.qos","a1.policy_instance.20005.123456","a1.policy_instance.20005.234567","a1.policy_type.1006001","a1.policy_type.20000","a1.policy_inst_metadata.1006001.qos",},nil).Once() + sdlInst.On("Remove", a1MediatorNs, keys[:]).Return(errors.New("Some Error")).Once() + errresp := rh.DeletePolicyType(policyTypeId) + assert.NotNil(t, errresp) +} + +func TestGetPolicyInstanceStatusFail(t *testing.T) { + var policyTypeId models.PolicyTypeID + policyTypeId = 0 + var policyInstanceID models.PolicyInstanceID + policyInstanceID = "" + httpBody := `{ + "created_at":"0001-01-01T00:00:00.000Z", + "instance_status":"NOT IN EFFECT" + }` + instancekey := a1InstanceMetadataPrefix + strconv.FormatInt(20001, 10) + "." + string(policyInstanceID) + a1.Logger.Debug("httpBody String : %+v", httpBody) + a1.Logger.Debug("key : %+v", instancekey) + var keys [1]string + keys[0] = instancekey + sdlInst.On("Get", a1MediatorNs, keys[:]).Return(httpBody,nil) + instancekey = a1HandlerPrefix + strconv.FormatInt(20001, 10) + "." + string(policyInstanceID) + var instancekeys [1]string + instancekeys[0] = instancekey + instancearr := []interface{}{instancekey, "OK"} + sdlInst.On("Get", a1MediatorNs, instancekeys[:]).Return(instancearr, errors.New("Some Error")) + _, errresp := rh.GetPolicyInstanceStatus(policyTypeId, policyInstanceID) + assert.NotNil(t, errresp) +} + +func TestDeleteInstanceDataFail(t *testing.T) { + var policyTypeId models.PolicyTypeID + policyTypeId = 0 + var policyInstanceID models.PolicyInstanceID + policyInstanceID = "" + instancekey := a1InstancePrefix + strconv.FormatInt((int64(policyTypeId)), 10) + "." + string(policyInstanceID) + var keys [1]string + keys[0] = instancekey + + //Setup Expectations + sdlInst.On("Remove", a1MediatorNs, keys[:]).Return(errors.New("Some Error")) + + errresp := rh.deleteInstancedata(policyTypeId,policyInstanceID) + + assert.NotNil(t, errresp) +} + + +func TestDeleteNotificationDestinationFail(t *testing.T) { + var policyTypeId models.PolicyTypeID + policyTypeId = 0 + var policyInstanceID models.PolicyInstanceID + policyInstanceID = "" + notificationDestinationkey := a1NotificationDestinationPrefix + strconv.FormatInt((int64(policyTypeId)), 10) + "." + string(policyInstanceID) + var keys [1]string + keys[0] = notificationDestinationkey + + //Setup Expectations + sdlInst.On("Remove", a1MediatorNs, keys[:]).Return(errors.New("Some Error")) + + errresp := rh.deleteNotificationDestination(policyTypeId,policyInstanceID) + + assert.NotNil(t, errresp) +} + +func TestDeleteMetadataFail(t *testing.T) { + var policyTypeId models.PolicyTypeID + policyTypeId = 0 + var policyInstanceID models.PolicyInstanceID + policyInstanceID = "" + instanceMetadataKey := a1InstanceMetadataPrefix + strconv.FormatInt((int64(policyTypeId)), 10) + "." + string(policyInstanceID) + var keys [1]string + keys[0] = instanceMetadataKey + + //Setup Expectations + sdlInst.On("Remove", a1MediatorNs, keys[:]).Return(errors.New("Some Error")) + + errresp := rh.deleteMetadata(policyTypeId,policyInstanceID) + + assert.NotNil(t, errresp) +} + + +func TestGetPolicyInstanceStFail(t *testing.T) { + var policyTypeId models.PolicyTypeID + policyTypeId = 200 + var policyInstanceID models.PolicyInstanceID + policyInstanceID = "123" + policyString := "testval" + instancekey := a1HandlerPrefix + strconv.FormatInt((int64(policyTypeId)), 10) + "." + string(policyInstanceID) + a1.Logger.Debug("policyString String : %+v", policyString) + a1.Logger.Debug("key from get test : %+v", instancekey) + var keys [1]string + keys[0] = instancekey + instancearr := []interface{}{instancekey, "NOK"} + //Setup Expectations + sdlInst.On("Get", "A1m_ns", keys[:]).Return(instancearr, errors.New("Some Error")).Once() + resp, errresp := rh.getPolicyInstanceStatus(policyTypeId, policyInstanceID) + a1.Logger.Debug("resp from get test : %+v", resp) + a1.Logger.Debug("errresp from get test : %+v", errresp) + assert.NotNil(t, errresp) +} + +func TestCreatePolicyTypeInstance(t *testing.T) { + var policyInstanceID models.PolicyInstanceID + policyInstanceID = "123456" + var httpBody = `{"enforce":true,"window_length":20,"blocking_rate":20,"trigger_threshold":10}` + instancekey := a1InstancePrefix + strconv.FormatInt(20001, 10) + "." + string(policyInstanceID) + var policyTypeId models.PolicyTypeID + policyTypeId = 20001 + + var instancedata map[string]interface{} + + json.Unmarshal([]byte(httpBody), &instancedata) + + data, _ := json.Marshal(instancedata) + a1.Logger.Debug("Marshaled data : %+v", string(data)) + a1.Logger.Debug("instancekey : %+v", instancekey) + instancearr := []interface{}{instancekey, string(data)} + sdlInst.On("Set", "A1m_ns", instancearr).Return(nil) + + metadatainstancekey := a1InstanceMetadataPrefix + strconv.FormatInt(20001, 10) + "." + string(policyInstanceID) + creation_timestamp := time.Now() + var metadatajson []interface{} + metadatajson = append(metadatajson, map[string]string{"created_at": creation_timestamp.Format("2006-01-02 15:04:05"), "has_been_deleted": "False"}) + metadata, _ := json.Marshal(metadatajson) + a1.Logger.Debug("Marshaled Metadata : %+v", string(metadata)) + a1.Logger.Debug("metadatainstancekey : %+v", metadatainstancekey) + metadatainstancearr := []interface{}{metadatainstancekey, string(metadata)} + sdlInst.On("Set", "A1m_ns", metadatainstancearr).Return(nil) + sdlInst.On("SetIfNotExists", a1MediatorNs, instancekey, string(httpBody), string(data)).Return(true, nil) + notificationDestinationkey := a1NotificationDestinationPrefix + strconv.FormatInt((int64(policyTypeId)), 10) + "." + string(policyInstanceID) + notificationDestination := "https://www.abc.com" + notificationarr := []interface{}{notificationDestinationkey, string(notificationDestination)} + sdlInst.On("Set", "A1m_ns", notificationarr).Return(nil) + + rmrSenderInst.On("RmrSendToXapp", "httpBodyString", 20010, int(policyTypeId)).Return(true) + + errresp := rh.CreatePolicyInstance(policyTypeId, policyInstanceID, instancedata, notificationDestination) + + assert.Nil(t, errresp) +} + +func TestCreatePolicyTypeInstance2(t *testing.T) { + var policyInstanceID models.PolicyInstanceID + policyInstanceID = "123" + var httpBody = `{"enforce":true,"window_length":20,"blocking_rate":20,"trigger_threshold":10}` + instancekey := a1InstancePrefix + strconv.FormatInt(20001, 10) + "." + string(policyInstanceID) + var policyTypeId models.PolicyTypeID + policyTypeId = 20001 + + var instancedata map[string]interface{} + + json.Unmarshal([]byte(httpBody), &instancedata) + + data, _ := json.Marshal(instancedata) + a1.Logger.Debug("Marshaled data : %+v", string(data)) + a1.Logger.Debug("instancekey : %+v", instancekey) + instancearr := []interface{}{instancekey, string(data)} + sdlInst.On("Set", "A1m_ns", instancearr).Return(nil) + sdlInst.On("Get", "A1m_ns", mock.Anything).Return(instancearr, nil).Once() + + metadatainstancekey := a1InstanceMetadataPrefix + strconv.FormatInt(20001, 10) + "." + string(policyInstanceID) + creation_timestamp := time.Now() + var metadatajson []interface{} + metadatajson = append(metadatajson, map[string]string{"created_at": creation_timestamp.Format("2006-01-02 15:04:05"), "has_been_deleted": "False"}) + metadata, _ := json.Marshal(metadatajson) + a1.Logger.Debug("Marshaled Metadata : %+v", string(metadata)) + a1.Logger.Debug("metadatainstancekey : %+v", metadatainstancekey) + metadatainstancearr := []interface{}{metadatainstancekey, string(metadata)} + sdlInst.On("Set", "A1m_ns", metadatainstancearr).Return(nil) + sdlInst.On("SetIfNotExists", a1MediatorNs, instancekey, string(httpBody), string(data)).Return(true, nil) + notificationDestinationkey := a1NotificationDestinationPrefix + strconv.FormatInt((int64(policyTypeId)), 10) + "." + string(policyInstanceID) + notificationDestination := "https://www.abc.com" + notificationarr := []interface{}{notificationDestinationkey, string(notificationDestination)} + sdlInst.On("Set", "A1m_ns", notificationarr).Return(nil) + + rmrSenderInst.On("RmrSendToXapp", "httpBodyString", 20010, int(policyTypeId)).Return(true) + + errresp := rh.CreatePolicyInstance(policyTypeId, policyInstanceID, instancedata, notificationDestination) + + assert.Nil(t, errresp) +} + +func TestCreatePolicyTypeInstanceFail(t *testing.T) { + var policyInstanceID models.PolicyInstanceID + policyInstanceID = "123456" + var policyTypeId models.PolicyTypeID + policyTypeId = 20001 + errresp := rh.CreatePolicyInstance(policyTypeId, policyInstanceID, "", "") + assert.NotNil(t, errresp) +} + type SdlMock struct { mock.Mock } func (s *SdlMock) GetAll(ns string) ([]string, error) { - args := s.MethodCalled("GetAll", ns) - return args.Get(0).([]string), nil + args := s.MethodCalled("GetAll", ns) + return args.Get(0).([]string), args.Error(1) } func (s *SdlMock) Get(ns string, keys []string) (map[string]interface{}, error) { a1.Logger.Debug("Get Called ") args := s.MethodCalled("Get", ns, keys) a1.Logger.Debug("ns :%+v", args.Get(0)) - policytypeid := int64(20001) - policyInstanceID := "123456" var policySchemaString string var key string if keys[0] == "a1.policy_instance.20001.123456" { - policySchemaString = `{ - "enforce":true, - "window_length":20, - "blocking_rate":20, - "trigger_threshold":10 - }` - key = a1InstancePrefix + strconv.FormatInt(policytypeid, 10) + "." + string(policyInstanceID) + policySchemaString = `{"enforce":true,"window_length":20,"blocking_rate":20,"trigger_threshold":10}` + key = a1InstancePrefix + strconv.FormatInt(20001, 10) + "." + "123456" } else if keys[0] == "a1.policy_type.20001" { policySchemaString = `{"create_schema":{"$schema":"http://json-schema.org/draft-07/schema#","properties":{"additionalProperties":false,"blocking_rate":{"default":10,"description":"% Connections to block","maximum":1001,"minimum":1,"type":"number"},"enforce":{"default":"true","type":"boolean"},"window_length":{"default":1,"description":"Sliding window length (in minutes)","maximum":60,"minimum":1,"type":"integer"}},"type":"object"},"description":"various parameters to control admission of dual connection","name":"admission_control_policy_mine","policy_type_id":20001}` - key = a1PolicyPrefix + strconv.FormatInt((policytypeid), 10) + key = a1PolicyPrefix + strconv.FormatInt((20001), 10) } else if keys[0] == "a1.policy_inst_metadata.20001.123456" { policySchemaString = `{ "created_at":"2022-11-02 10:30:20", "instance_status":"NOT IN EFFECT" }` - key = a1InstanceMetadataPrefix + strconv.FormatInt(policytypeid, 10) + "." + string(policyInstanceID) + key = a1InstanceMetadataPrefix + strconv.FormatInt(20001, 10) + "." + "123456" } a1.Logger.Debug(" policy SchemaString %+v", policySchemaString) policyTypeSchema, _ := json.Marshal((policySchemaString)) @@ -403,7 +893,7 @@ func (s *SdlMock) Get(ns string, keys []string) (map[string]interface{}, error) a1.Logger.Debug(" key for policy type %+v", key) mp := map[string]interface{}{key: string(policySchemaString)} a1.Logger.Debug("Get Called and mp return %+v ", mp) - return mp, nil + return mp, args.Error(1) } func (s *SdlMock) SetIfNotExists(ns string, key string, data interface{}) (bool, error) { args := s.MethodCalled("SetIfNotExists", ns, key, data)