X-Git-Url: https://gerrit.o-ran-sc.org/r/gitweb?a=blobdiff_plain;f=a1-go%2Fpkg%2Fresthooks%2Fresthooks_test.go;h=50a7fe27ebe5a7e443898c257ea52e5bbab15f5e;hb=6579f78d0b544ffdf9cbf4049eb47a64758f5a71;hp=6b7d2cea84bdaf94d4f692ac62ff04c6123c66d8;hpb=6bc8f8ef64a7e90f550dea4af6a10d785d8aeca6;p=ric-plt%2Fa1.git diff --git a/a1-go/pkg/resthooks/resthooks_test.go b/a1-go/pkg/resthooks/resthooks_test.go index 6b7d2ce..50a7fe2 100644 --- a/a1-go/pkg/resthooks/resthooks_test.go +++ b/a1-go/pkg/resthooks/resthooks_test.go @@ -21,10 +21,13 @@ package resthooks import ( + "encoding/json" "os" + "strconv" "testing" - "gerrit.o-ran-sc.org/r/ric-plt/a1/pkg/a1" + "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" ) @@ -35,13 +38,13 @@ var sdlInst *SdlMock func TestMain(m *testing.M) { sdlInst = new(SdlMock) - sdlInst.On("GetAll", "A1m_ns").Return([]string{"a1.policy_instance.1006001.qos", - "a1.policy_type.1006001", - "a1.policy_type.20000", - "a1.policy_inst_metadata.1006001.qos", - }, nil) + sdlInst.On("GetAll", "A1m_ns").Return([]string{"a1.policy_instance.1006001.qos", + "a1.policy_type.1006001", + "a1.policy_type.20000", + "a1.policy_inst_metadata.1006001.qos", + }, nil) - a1.Init() + a1.Init() rh = createResthook(sdlInst) code := m.Run() os.Exit(code) @@ -52,11 +55,153 @@ func TestGetAllPolicyType(t *testing.T) { assert.Equal(t, 2, len(resp)) } +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 + policytypeid := int64(20001) + 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) + + //Setup Expectations + sdlInst.On("Get", a1MediatorNs, policyTypeId).Return(map[string]interface{}{key: policyTypeSchema}, nil) + + assert.NotNil(t, resp) + + sdlInst.AssertExpectations(t) + +} + +func TestCreatePolicyType(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, 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(true, nil) + + errresp := rh.CreatePolicyType(policyTypeId, policyTypeSchema) + //Data Assertion + assert.Nil(t, errresp) + //Mock Assertion :Behavioral + sdlInst.AssertExpectations(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) + + instancearr := []interface{}{instancekey, string(data)} + sdlInst.On("Set", "A1m_ns", instancearr).Return("CREATE", nil) + errresp := rh.CreatePolicyInstance(policyTypeId, policyInstanceID, httpBody) + + assert.Nil(t, errresp) + sdlInst.AssertExpectations(t) +} + +func TestGetPolicyInstance(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) + a1.Logger.Debug("httpBody String : %+v", httpBody) + a1.Logger.Debug("key : %+v", instancekey) + var keys [1]string + keys[0] = instancekey + //Setup Expectations + sdlInst.On("Get", a1MediatorNs, keys[:]).Return(httpBody, nil) + + resp := rh.GetPolicyInstance(policyTypeId, policyInstanceID) + a1.Logger.Error("resp : %+v", resp) + assert.NotNil(t, resp) + + sdlInst.AssertExpectations(t) +} + 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), nil +} + +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("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) + 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) + return mp, nil +} + +func (s *SdlMock) SetIfNotExists(ns string, key string, data interface{}) (bool, error) { + args := s.MethodCalled("SetIfNotExists", ns, key, data) + return args.Bool(0), args.Error(1) +} + +func (s *SdlMock) Set(ns string, pairs ...interface{}) error { + args := s.MethodCalled("Set", ns, pairs) + return args.Error(1) +} +func (s *SdlMock) SetIf(ns string, key string, oldData, newData interface{}) (bool, error) { + args := s.MethodCalled("SetIfNotExists", ns, key, oldData, newData) + return args.Bool(0), args.Error(1) }