[RICPLT-2585] Add UTs to init flow
[ric-plt/e2mgr.git] / E2Manager / rNibWriter / rNibWriter_test.go
index c887da6..7f0b550 100644 (file)
@@ -29,55 +29,19 @@ import (
        "time"
 )
 
-func TestInitRNibWriter(t *testing.T) {
-       writerPool = nil
-       initSdlInstanceMock(namespace, 1)
-       available, created := writerPool.Stats()
-       assert.Equal(t, available, 0, "number of available objects in the writerPool should be 0")
-       assert.Equal(t, created, 0, "number of created objects in the writerPool should be 0")
-       w := GetRNibWriter()
-       assert.NotNil(t, w)
-}
-
-func TestInitPool(t *testing.T) {
-       writerPool = nil
-       sdlInstanceMock := new(mocks.MockSdlInstance)
-       initPool(1, func() interface{} {
-               sdlI := common.ISdlInstance(sdlInstanceMock)
-               return &rNibWriterInstance{sdl: &sdlI, namespace: namespace}
-       },
-               func(obj interface{}) {
-               },
-       )
-       assert.NotNil(t, writerPool)
-       assert.NotNil(t, writerPool.New)
-       assert.NotNil(t, writerPool.Destroy)
-       available, created := writerPool.Stats()
-       assert.Equal(t, 0, available, "number of available objects in the writerPool should be 0")
-       assert.Equal(t, 0, created, "number of created objects in the writerPool should be 0")
+func initSdlInstanceMock(namespace string) (w RNibWriter, sdlInstanceMock *mocks.MockSdlInstance) {
+       sdlInstanceMock = new(mocks.MockSdlInstance)
+       w = GetRNibWriter(sdlInstanceMock)
+       return
 }
 
 var namespace = "namespace"
 
-func initSdlInstanceMock(namespace string, poolSize int) *mocks.MockSdlInstance {
-       sdlInstanceMock := new(mocks.MockSdlInstance)
-       initPool(poolSize, func() interface{} {
-               sdlI := common.ISdlInstance(sdlInstanceMock)
-               return &rNibWriterInstance{sdl: &sdlI, namespace: namespace}
-       },
-               func(obj interface{}) {
-               },
-       )
-       return sdlInstanceMock
-}
-
 func TestUpdateNodebInfoSuccess(t *testing.T) {
        inventoryName := "name"
        plmnId := "02f829"
        nbId := "4a952a0a"
-       writerPool = nil
-       sdlInstanceMock := initSdlInstanceMock(namespace, 1)
-       w := GetRNibWriter()
+       w, sdlInstanceMock := initSdlInstanceMock(namespace)
        nodebInfo := &entities.NodebInfo{}
        nodebInfo.RanName = inventoryName
        nodebInfo.GlobalNbId = &entities.GlobalNbId{PlmnId: plmnId, NbId: nbId}
@@ -107,9 +71,7 @@ func TestUpdateNodebInfoMissingInventoryNameFailure(t *testing.T) {
        inventoryName := "name"
        plmnId := "02f829"
        nbId := "4a952a0a"
-       writerPool = nil
-       sdlInstanceMock := initSdlInstanceMock(namespace, 1)
-       w := GetRNibWriter()
+       w, sdlInstanceMock := initSdlInstanceMock(namespace)
        nodebInfo := &entities.NodebInfo{}
        data, err := proto.Marshal(nodebInfo)
        if err != nil {
@@ -133,9 +95,7 @@ func TestUpdateNodebInfoMissingInventoryNameFailure(t *testing.T) {
 
 func TestUpdateNodebInfoMissingGlobalNbId(t *testing.T) {
        inventoryName := "name"
-       writerPool = nil
-       sdlInstanceMock := initSdlInstanceMock(namespace, 1)
-       w := GetRNibWriter()
+       w, sdlInstanceMock := initSdlInstanceMock(namespace)
        nodebInfo := &entities.NodebInfo{}
        nodebInfo.RanName = inventoryName
        data, err := proto.Marshal(nodebInfo)
@@ -157,9 +117,7 @@ func TestUpdateNodebInfoMissingGlobalNbId(t *testing.T) {
 func TestSaveEnb(t *testing.T) {
        name := "name"
        ranName := "RAN:" + name
-       writerPool = nil
-       sdlInstanceMock := initSdlInstanceMock(namespace, 1)
-       w := GetRNibWriter()
+       w, sdlInstanceMock := initSdlInstanceMock(namespace)
        nb := entities.NodebInfo{}
        nb.NodeType = entities.Node_ENB
        nb.ConnectionStatus = 1
@@ -207,9 +165,7 @@ func TestSaveEnb(t *testing.T) {
 
 func TestSaveEnbCellIdValidationFailure(t *testing.T) {
        name := "name"
-       writerPool = nil
-       initSdlInstanceMock(namespace, 1)
-       w := GetRNibWriter()
+       w, _ := initSdlInstanceMock(namespace)
        nb := entities.NodebInfo{}
        nb.NodeType = entities.Node_ENB
        nb.ConnectionStatus = 1
@@ -228,9 +184,7 @@ func TestSaveEnbCellIdValidationFailure(t *testing.T) {
 }
 
 func TestSaveEnbInventoryNameValidationFailure(t *testing.T) {
-       writerPool = nil
-       initSdlInstanceMock(namespace, 1)
-       w := GetRNibWriter()
+       w, _ := initSdlInstanceMock(namespace)
        nb := entities.NodebInfo{}
        nb.NodeType = entities.Node_ENB
        nb.ConnectionStatus = 1
@@ -248,35 +202,9 @@ func TestSaveEnbInventoryNameValidationFailure(t *testing.T) {
        assert.Equal(t, "#utils.ValidateAndBuildNodeBNameKey - an empty inventory name received", rNibErr.Error())
 }
 
-func TestSaveEnbOnClosedPool(t *testing.T) {
-       name := "name"
-       writerPool = nil
-       sdlInstanceMock := initSdlInstanceMock(namespace, 1)
-       w := GetRNibWriter()
-       nb := entities.NodebInfo{}
-       nb.NodeType = entities.Node_ENB
-       nb.ConnectionStatus = 1
-       nb.Ip = "localhost"
-       nb.Port = 5656
-       enb := entities.Enb{}
-       nb.Configuration = &entities.NodebInfo_Enb{Enb: &enb}
-       data, err := proto.Marshal(&nb)
-       if err != nil {
-               t.Errorf("#rNibWriter_test.TestSaveEnbOnClosedPool - Failed to marshal NodeB entity. Error: %v", err)
-       }
-       setExpected := []interface{}{name, data}
-       var e error
-       sdlInstanceMock.On("Set", setExpected).Return(e)
-       writerPool.Close()
-       nbIdentity := &entities.NbIdentity{}
-       assert.Panics(t, func() { w.SaveNodeb(nbIdentity, &nb) })
-}
-
 func TestSaveGnbCellIdValidationFailure(t *testing.T) {
        name := "name"
-       writerPool = nil
-       initSdlInstanceMock(namespace, 1)
-       w := GetRNibWriter()
+       w, _ := initSdlInstanceMock(namespace)
        nb := entities.NodebInfo{}
        nb.NodeType = entities.Node_GNB
        nb.ConnectionStatus = 1
@@ -298,9 +226,7 @@ func TestSaveGnbCellIdValidationFailure(t *testing.T) {
 func TestSaveGnb(t *testing.T) {
        name := "name"
        ranName := "RAN:" + name
-       writerPool = nil
-       sdlInstanceMock := initSdlInstanceMock(namespace, 1)
-       w := GetRNibWriter()
+       w, sdlInstanceMock := initSdlInstanceMock(namespace)
        nb := entities.NodebInfo{}
        nb.NodeType = entities.Node_GNB
        nb.ConnectionStatus = 1
@@ -354,9 +280,7 @@ func TestSaveRanLoadInformationSuccess(t *testing.T) {
                t.Errorf("#rNibWriter_test.TestSaveRanLoadInformationSuccess - Failed to build ran load infromation key. Error: %v", validationErr)
        }
 
-       writerPool = nil
-       sdlInstanceMock := initSdlInstanceMock(namespace, 1)
-       w := GetRNibWriter()
+       w, sdlInstanceMock := initSdlInstanceMock(namespace)
 
        ranLoadInformation := generateRanLoadInformation()
        data, err := proto.Marshal(ranLoadInformation)
@@ -376,9 +300,7 @@ func TestSaveRanLoadInformationSuccess(t *testing.T) {
 
 func TestSaveRanLoadInformationMarshalNilFailure(t *testing.T) {
        inventoryName := "name2"
-       writerPool = nil
-       initSdlInstanceMock(namespace, 1)
-       w := GetRNibWriter()
+       w, _ := initSdlInstanceMock(namespace)
 
        expectedErr := common.NewInternalError(errors.New("proto: Marshal called with nil"))
        err := w.SaveRanLoadInformation(inventoryName, nil)
@@ -387,9 +309,7 @@ func TestSaveRanLoadInformationMarshalNilFailure(t *testing.T) {
 
 func TestSaveRanLoadInformationEmptyInventoryNameFailure(t *testing.T) {
        inventoryName := ""
-       writerPool = nil
-       initSdlInstanceMock(namespace, 1)
-       w := GetRNibWriter()
+       w, _ := initSdlInstanceMock(namespace)
 
        err := w.SaveRanLoadInformation(inventoryName, nil)
        assert.NotNil(t, err)
@@ -405,9 +325,7 @@ func TestSaveRanLoadInformationSdlFailure(t *testing.T) {
                t.Errorf("#rNibWriter_test.TestSaveRanLoadInformationSuccess - Failed to build ran load infromation key. Error: %v", validationErr)
        }
 
-       writerPool = nil
-       sdlInstanceMock := initSdlInstanceMock(namespace, 1)
-       w := GetRNibWriter()
+       w, sdlInstanceMock := initSdlInstanceMock(namespace)
 
        ranLoadInformation := generateRanLoadInformation()
        data, err := proto.Marshal(ranLoadInformation)
@@ -500,9 +418,7 @@ func generateRanLoadInformation() *entities.RanLoadInformation {
 }
 
 func TestSaveNilEntityFailure(t *testing.T) {
-       writerPool = nil
-       initSdlInstanceMock(namespace, 1)
-       w := GetRNibWriter()
+       w, _ := initSdlInstanceMock(namespace)
        expectedErr := common.NewInternalError(errors.New("proto: Marshal called with nil"))
        nbIdentity := &entities.NbIdentity{}
        actualErr := w.SaveNodeb(nbIdentity, nil)
@@ -510,9 +426,7 @@ func TestSaveNilEntityFailure(t *testing.T) {
 }
 
 func TestSaveUnknownTypeEntityFailure(t *testing.T) {
-       writerPool = nil
-       initSdlInstanceMock(namespace, 1)
-       w := GetRNibWriter()
+       w, _ := initSdlInstanceMock(namespace)
        expectedErr := common.NewValidationError("#rNibWriter.saveNodeB - Unknown responding node type, entity: ip:\"localhost\" port:5656 ")
        nbIdentity := &entities.NbIdentity{InventoryName: "name", GlobalNbId: &entities.GlobalNbId{PlmnId: "02f829", NbId: "4a952a0a"}}
        nb := &entities.NodebInfo{}
@@ -527,9 +441,7 @@ func TestSaveEntityFailure(t *testing.T) {
        plmnId := "02f829"
        nbId := "4a952a0a"
 
-       writerPool = nil
-       sdlInstanceMock := initSdlInstanceMock(namespace, 1)
-       w := GetRNibWriter()
+       w, sdlInstanceMock := initSdlInstanceMock(namespace)
        gnb := entities.NodebInfo{}
        gnb.NodeType = entities.Node_GNB
        data, err := proto.Marshal(&gnb)
@@ -545,77 +457,9 @@ func TestSaveEntityFailure(t *testing.T) {
        assert.NotEmpty(t, rNibErr)
 }
 
-func TestGetRNibWriterPoolNotInitializedFailure(t *testing.T) {
-       writerPool = nil
-       assert.Panics(t, func() { GetRNibWriter().SaveNodeb(nil,nil) })
-}
-
 func TestGetRNibWriter(t *testing.T) {
-       writerPool = nil
-       initSdlInstanceMock(namespace, 1)
-       received := GetRNibWriter()
-       assert.Empty(t, received)
-       available, created := writerPool.Stats()
-       assert.Equal(t, 0, available, "number of available objects in the writerPool should be 0")
-       assert.Equal(t, 0, created, "number of created objects in the writerPool should be 0")
-       writerPool.Close()
-}
-
-func TestClose(t *testing.T) {
-       writerPool = nil
-       instanceMock := initSdlInstanceMock(namespace, 2)
-       w1 := GetRNibWriter()
-       w2 := GetRNibWriter()
-       writerPool.Put(w1)
-       writerPool.Put(w2)
-       available, created := writerPool.Stats()
-       assert.Equal(t, 2, available, "number of available objects in the writerPool should be 2")
-       assert.Equal(t, 0, created, "number of created objects in the writerPool should be 0")
-       var e error
-       instanceMock.On("Close").Return(e)
-       Close()
-       available, created = writerPool.Stats()
-       assert.Equal(t, 0, available, "number of available objects in the writerPool should be 0")
-}
-
-func TestCloseOnClosedPoolFailure(t *testing.T) {
-       writerPool = nil
-       instanceMock := initSdlInstanceMock(namespace, 1)
-       w1 := GetRNibWriter()
-       writerPool.Put(w1)
-       available, created := writerPool.Stats()
-       assert.Equal(t, 1, available, "number of available objects in the writerPool should be 1")
-       assert.Equal(t, 0, created, "number of created objects in the writerPool should be 0")
-       var e error
-       instanceMock.On("Close").Return(e)
-       Close()
-       assert.Panics(t, func() { Close() })
-}
-
-func TestCloseFailure(t *testing.T) {
-       writerPool = nil
-       instanceMock := initSdlInstanceMock(namespace, 2)
-       w1 := GetRNibWriter()
-       writerPool.Put(w1)
-       available, created := writerPool.Stats()
-       assert.Equal(t, 1, available, "number of available objects in the writerPool should be 1")
-       assert.Equal(t, 0, created, "number of created objects in the writerPool should be 0")
-       e := errors.New("expected error")
-       instanceMock.On("Close").Return(e)
-       Close()
-       available, created = writerPool.Stats()
-       assert.Equal(t, 0, available, "number of available objects in the writerPool should be 0")
-}
-
-func TestInit(t *testing.T) {
-       writerPool = nil
-       Init("", 1)
-       assert.NotNil(t, writerPool)
-       assert.NotNil(t, writerPool.New)
-       assert.NotNil(t, writerPool.Destroy)
-       available, created := writerPool.Stats()
-       assert.Equal(t, 0, available, "number of available objects in the writerPool should be 0")
-       assert.Equal(t, 0, created, "number of created objects in the writerPool should be 0")
+       received, _ := initSdlInstanceMock(namespace)
+       assert.NotEmpty(t, received)
 }
 
 //Integration tests