From 584d75571c8035f21051d26d3a020e8eb1ae89a0 Mon Sep 17 00:00:00 2001 From: Naman Gupta/Single vRAN Call Control /SRI-Bangalore/Engineer/Samsung Electronics Date: Tue, 27 Sep 2022 10:32:15 +0530 Subject: [PATCH] Fix UT and change function to private Fix UT and change function to private Signed-off-by: naman.gupta Change-Id: Id045e21945249764ad31d2417e11288fee5f870b --- a1-go/pkg/restful/restful.go | 4 +- a1-go/pkg/resthooks/resthooks.go | 39 ++++++++------- a1-go/pkg/resthooks/resthooks_test.go | 93 ++++++++++++++++++++++------------- tox.ini | 4 +- 4 files changed, 84 insertions(+), 56 deletions(-) diff --git a/a1-go/pkg/restful/restful.go b/a1-go/pkg/restful/restful.go index b7e2094..f34a5ad 100644 --- a/a1-go/pkg/restful/restful.go +++ b/a1-go/pkg/restful/restful.go @@ -90,9 +90,9 @@ func (r *Restful) setupHandler() *operations.A1API { return a1_mediator.NewA1ControllerGetPolicyInstanceOK().WithPayload(resp) } if r.rh.IsPolicyInstanceNotFound(err) { - return a1_mediator.NewA1ControllerGetPolicyInstanceNotFound + return a1_mediator.NewA1ControllerGetPolicyInstanceNotFound() } - return a1_mediator.NewA1ControllerGetPolicyInstanceServiceUnavailable + return a1_mediator.NewA1ControllerGetPolicyInstanceServiceUnavailable() }) api.A1MediatorA1ControllerGetAllInstancesForTypeHandler = a1_mediator.A1ControllerGetAllInstancesForTypeHandlerFunc(func(params a1_mediator.A1ControllerGetAllInstancesForTypeParams) middleware.Responder { diff --git a/a1-go/pkg/resthooks/resthooks.go b/a1-go/pkg/resthooks/resthooks.go index 8ed8a08..2767bda 100644 --- a/a1-go/pkg/resthooks/resthooks.go +++ b/a1-go/pkg/resthooks/resthooks.go @@ -26,21 +26,21 @@ import ( "fmt" "strconv" "strings" + "time" "gerrit.o-ran-sc.org/r/ric-plt/a1/pkg/a1" "gerrit.o-ran-sc.org/r/ric-plt/a1/pkg/models" "gerrit.o-ran-sc.org/r/ric-plt/sdlgo" - "github.com/santhosh-tekuri/jsonschema/v5" "gopkg.in/yaml.v2" ) const ( - a1PolicyPrefix = "a1.policy_type." - a1MediatorNs = "A1m_ns" - a1InstancePrefix = "a1.policy_instance." + a1PolicyPrefix = "a1.policy_type." + a1MediatorNs = "A1m_ns" + a1InstancePrefix = "a1.policy_instance." a1InstanceMetadataPrefix = "a1.policy_inst_metadata." - a1HandlerPrefix = "a1.policy_handler." + a1HandlerPrefix = "a1.policy_handler." ) var typeAlreadyError = errors.New("Policy Type already exists") @@ -118,7 +118,7 @@ func (rh *Resthook) GetPolicyType(policyTypeId models.PolicyTypeID) *models.Poli valmap, err := rh.db.Get(a1MediatorNs, keys[:]) - a1.Logger.Debug("policytype map : ", valmap) + a1.Logger.Debug("policytype map : %+v", valmap) if len(valmap) == 0 { a1.Logger.Error("policy type Not Present for policyid : %v", policyTypeId) @@ -183,8 +183,6 @@ func (rh *Resthook) CreatePolicyType(policyTypeId models.PolicyTypeID, httpreque return nil } - - func toStringKeys(val interface{}) (interface{}, error) { var err error switch val := val.(type) { @@ -215,7 +213,7 @@ func toStringKeys(val interface{}) (interface{}, error) { } } -func validate(httpBodyString string, schemaString string) bool { +var validate(httpBodyString string, schemaString string) bool { var m interface{} err := yaml.Unmarshal([]byte(httpBodyString), &m) if err != nil { @@ -244,7 +242,7 @@ func validate(httpBodyString string, schemaString string) bool { return true } -func (rh *Resthook) StorePolicyInstance(policyTypeId models.PolicyTypeID, policyInstanceID models.PolicyInstanceID, httpBody interface{}) (string, error) { +func (rh *Resthook) storePolicyInstance(policyTypeId models.PolicyTypeID, policyInstanceID models.PolicyInstanceID, httpBody interface{}) (string, error) { var keys [1]string operation := "CREATE" typekey := a1PolicyPrefix + strconv.FormatInt((int64(policyTypeId)), 10) @@ -344,23 +342,26 @@ func (rh *Resthook) CreatePolicyInstance(policyTypeId models.PolicyTypeID, polic a1.Logger.Debug("schema to validate %+v", string(schemaStr)) a1.Logger.Debug("httpbody to validate %+v", httpBody) schemaString := fmt.Sprint(string(schemaStr)) - httpBodyString := fmt.Sprint((httpBody)) + httpBodyMarshal, err := json.Marshal(httpBody) + httpBodyString := string((httpBodyMarshal)) + a1.Logger.Debug("schema to validate sprint %+v", (schemaString)) + a1.Logger.Debug("httpbody to validate sprint %+v", httpBodyString) isvalid := validate(httpBodyString, schemaString) if isvalid { var operation string - operation, err = rh.StorePolicyInstance(policyTypeId, policyInstanceID, httpBody) + operation, err = rh.storePolicyInstance(policyTypeId, policyInstanceID, httpBody) if err != nil { a1.Logger.Error("error :%+v", err) return err } a1.Logger.Debug("policy instance :%+v", operation) - iscreated,errmetadata := rh.StorePolicyInstanceMetadata(policyTypeId, policyInstanceID) + iscreated, errmetadata := rh.storePolicyInstanceMetadata(policyTypeId, policyInstanceID) if errmetadata != nil { a1.Logger.Error("error :%+v", errmetadata) return errmetadata } if iscreated { - a1.Logger.Debug("policy instance metadata created") + a1.Logger.Debug("policy instance metadata created") } } else { a1.Logger.Error("%+v", invalidJsonSchema) @@ -416,15 +417,15 @@ func (rh *Resthook) GetPolicyInstance(policyTypeId models.PolicyTypeID, policyIn return valStr, nil } -func (rh *Resthook) GetAllPolicyInstance(policyTypeId models.PolicyTypeID) []models.PolicyInstanceID ,error { +func (rh *Resthook) GetAllPolicyInstance(policyTypeId models.PolicyTypeID) ([]models.PolicyInstanceID, error) { a1.Logger.Debug("GetAllPolicyInstance") - var policyTypeInstances = []models.PolicyInstanceID{} + var policyTypeInstances = []models.PolicyInstanceID{} keys, err := rh.db.GetAll("A1m_ns") if err != nil { a1.Logger.Error("error in retrieving policy. err: %v", err) - return policyTypeInstances ,err + return policyTypeInstances, err } a1.Logger.Debug("keys : %+v", keys) typekey := a1InstancePrefix + strconv.FormatInt((int64(policyTypeId)), 10) + "." @@ -437,10 +438,10 @@ func (rh *Resthook) GetAllPolicyInstance(policyTypeId models.PolicyTypeID) []mod } } - if len(policyTypeInstances)==0{ + if len(policyTypeInstances) == 0 { a1.Logger.Debug("policy instance Not Present ") } a1.Logger.Debug("return : %+v", policyTypeInstances) - return policyTypeInstances ,nil + return policyTypeInstances, nil } diff --git a/a1-go/pkg/resthooks/resthooks_test.go b/a1-go/pkg/resthooks/resthooks_test.go index 98d4f8c..3afbb8e 100644 --- a/a1-go/pkg/resthooks/resthooks_test.go +++ b/a1-go/pkg/resthooks/resthooks_test.go @@ -25,6 +25,7 @@ import ( "os" "strconv" "testing" + "time" "gerrit.o-ran-sc.org/r/ric-plt/a1/pkg/a1" "gerrit.o-ran-sc.org/r/ric-plt/a1/pkg/models" @@ -61,8 +62,6 @@ func TestGetPolicyType(t *testing.T) { policyTypeId := models.PolicyTypeID(20001) - resp := rh.GetPolicyType(policyTypeId) - var policyTypeSchema models.PolicyTypeSchema name := "admission_control_policy_mine" policyTypeSchema.Name = &name @@ -74,10 +73,11 @@ func TestGetPolicyType(t *testing.T) { "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, policyTypeId).Return(map[string]interface{}{key: policyTypeSchema}, nil) - + sdlInst.On("Get", a1MediatorNs, keys[:]).Return(map[string]interface{}{key: policyTypeSchema}, nil) + resp := rh.GetPolicyType(policyTypeId) assert.NotNil(t, resp) sdlInst.AssertExpectations(t) @@ -113,24 +113,34 @@ func TestCreatePolicyType(t *testing.T) { } func TestCreatePolicyTypeInstance(t *testing.T) { - var policyTypeId models.PolicyTypeID - policyTypeId = 20001 var policyInstanceID models.PolicyInstanceID policyInstanceID = "123456" - httpBody := `{ - "enforce":true, - "window_length":20, - "blocking_rate":20, - "trigger_threshold":10 - }` - instancekey := a1PolicyPrefix + strconv.FormatInt(20001, 10) + "." + string(policyInstanceID) - data, _ := json.Marshal(httpBody) - a1.Logger.Debug("Marshaled String : %+v", string(data)) - a1.Logger.Debug("key : %+v", instancekey) + 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("CREATE", nil) - errresp := rh.CreatePolicyInstance(policyTypeId, policyInstanceID, httpBody) + 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) + + errresp := rh.CreatePolicyInstance(policyTypeId, policyInstanceID, instancedata) assert.Nil(t, errresp) sdlInst.AssertExpectations(t) @@ -156,8 +166,8 @@ func TestGetPolicyInstance(t *testing.T) { //Setup Expectations sdlInst.On("Get", a1MediatorNs, keys[:]).Return(httpBody, nil) - resp := rh.GetPolicyInstance(policyTypeId, policyInstanceID) - a1.Logger.Error("resp : %+v", resp) + resp, err := rh.GetPolicyInstance(policyTypeId, policyInstanceID) + a1.Logger.Error("err : %+v", err) assert.NotNil(t, resp) sdlInst.AssertExpectations(t) @@ -166,7 +176,8 @@ func TestGetPolicyInstance(t *testing.T) { func TestGetAllPolicyIntances(t *testing.T) { var policyTypeId models.PolicyTypeID policyTypeId = 20005 - resp := rh.GetAllPolicyInstance(policyTypeId) + resp, err := rh.GetAllPolicyInstance(policyTypeId) + a1.Logger.Error("err : %+v", err) assert.Equal(t, 2, len(resp)) } @@ -184,17 +195,33 @@ func (s *SdlMock) Get(ns string, keys []string) (map[string]interface{}, error) args := s.MethodCalled("Get", ns, keys) a1.Logger.Debug("keys :%+v", args.Get(1)) policytypeid := int64(20001) - - policyTypeSchemaString := `{"name":"admission_control_policy_mine","description":"various parameters to control admission of dual connection","policy_type_id": 20001,"create_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": 1001,"description": "% Connections to block"},"additionalProperties": false}}}` - - a1.Logger.Error(" policyTypeSchemaString %+v", policyTypeSchemaString) - policyTypeSchema, _ := json.Marshal((policyTypeSchemaString)) - // a1.Logger.Error(" policyTypeSchema error %+v", err) + 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) + } else if keys[0] == "a1.policy_type.20001" { + policySchemaString = `{"name":"admission_control_policy_mine", + "description":"various parameters to control admission of dual connection", + "policy_type_id": 20001, + "create_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": 1001, + "description": "% Connections to block"}, + "additionalProperties": false}}}` + key = a1PolicyPrefix + strconv.FormatInt((policytypeid), 10) + } + a1.Logger.Error(" policy SchemaString %+v", policySchemaString) + policyTypeSchema, _ := json.Marshal((policySchemaString)) a1.Logger.Error(" policyTypeSchema %+v", string(policyTypeSchema)) - var p models.PolicyTypeSchema - _ = json.Unmarshal([]byte(string(policyTypeSchemaString)), &p) - a1.Logger.Error("unmarshalled policyTypeSchema %+v", p.CreateSchema) - key := a1PolicyPrefix + strconv.FormatInt((policytypeid), 10) a1.Logger.Error(" key for policy type %+v", key) mp := map[string]interface{}{key: string(policyTypeSchema)} a1.Logger.Error("Get Called and mp return %+v ", mp) @@ -208,7 +235,7 @@ func (s *SdlMock) SetIfNotExists(ns string, key string, data interface{}) (bool, func (s *SdlMock) Set(ns string, pairs ...interface{}) error { args := s.MethodCalled("Set", ns, pairs) - return args.Error(1) + return args.Error(0) } func (s *SdlMock) SetIf(ns string, key string, oldData, newData interface{}) (bool, error) { args := s.MethodCalled("SetIfNotExists", ns, key, oldData, newData) diff --git a/tox.ini b/tox.ini index 4a319cb..760b7a3 100644 --- a/tox.ini +++ b/tox.ini @@ -25,7 +25,7 @@ minversion = 2.0 testpaths = tests [testenv:code] -basepython = python3.8 +basepython = python3 deps= pytest coverage @@ -44,7 +44,7 @@ commands = coverage xml -i [testenv:flake8] -basepython = python3.8 +basepython = python3 skip_install = true deps = flake8 commands = flake8 setup.py a1 tests -- 2.16.6