From 68a3eec5f7e3257141f8043f548608296a6737b1 Mon Sep 17 00:00:00 2001 From: Amichai Date: Mon, 2 Sep 2019 16:26:00 +0300 Subject: [PATCH] [RICPLT-1899] - RNIB improvements Signed-off-by: Amichai Change-Id: If7a4ba44484639887dc79b8c6843dab6122a525e --- common/internalError.go | 30 ++++ common/internalError_test.go | 32 ++++ common/rNibErrors.go | 67 -------- common/rNibErrors_test.go | 100 ------------ common/resourceNotFoundError.go | 36 +++++ common/resourceNotFoundError_test.go | 42 +++++ common/utils.go | 37 ++--- common/utils_test.go | 26 ++-- common/validationError.go | 36 +++++ common/validationError_test.go | 42 +++++ reader/rNibReader.go | 147 +++++++++--------- reader/rNibReader_test.go | 293 +++++++++++++++++++---------------- 12 files changed, 471 insertions(+), 417 deletions(-) create mode 100644 common/internalError.go create mode 100644 common/internalError_test.go delete mode 100644 common/rNibErrors.go delete mode 100644 common/rNibErrors_test.go create mode 100644 common/resourceNotFoundError.go create mode 100644 common/resourceNotFoundError_test.go create mode 100644 common/validationError.go create mode 100644 common/validationError_test.go diff --git a/common/internalError.go b/common/internalError.go new file mode 100644 index 0000000..386c5e2 --- /dev/null +++ b/common/internalError.go @@ -0,0 +1,30 @@ +// +// Copyright 2019 AT&T Intellectual Property +// Copyright 2019 Nokia +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// + +package common + +type InternalError struct{ + err error +} + +func NewInternalError(error error) error { + return &InternalError{err:error} +} + +func (e InternalError) Error() string { + return e.err.Error() +} diff --git a/common/internalError_test.go b/common/internalError_test.go new file mode 100644 index 0000000..54c01ce --- /dev/null +++ b/common/internalError_test.go @@ -0,0 +1,32 @@ +// +// Copyright 2019 AT&T Intellectual Property +// Copyright 2019 Nokia +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// + +package common + +import ( + "github.com/pkg/errors" + "github.com/stretchr/testify/assert" + "testing" +) + +func TestNewInternalError(t *testing.T){ + e := errors.New("Expected error") + expectedErr := NewInternalError(e) + assert.NotNil(t, expectedErr) + assert.IsType(t, &InternalError{}, expectedErr) + assert.Contains(t, expectedErr.Error(), e.Error()) +} \ No newline at end of file diff --git a/common/rNibErrors.go b/common/rNibErrors.go deleted file mode 100644 index c5d6620..0000000 --- a/common/rNibErrors.go +++ /dev/null @@ -1,67 +0,0 @@ -// -// Copyright 2019 AT&T Intellectual Property -// Copyright 2019 Nokia -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// - -package common - -import "fmt" - -const( - RESOURCE_NOT_FOUND int = 1 - INTERNAL_ERROR int = 2 - VALIDATION_ERROR int = 3 -) - -var rNibError_names = map[int]string{ - RESOURCE_NOT_FOUND:"RESOURCE_NOT_FOUND", - INTERNAL_ERROR:"INTERNAL_ERROR", - VALIDATION_ERROR:"VALIDATION_ERROR", -} - -type IRNibError interface{ - error - GetCode() int - GetError() error -} - -type RNibError struct{ - err error - errorCode int -} - -func NewResourceNotFoundError(error error) IRNibError { - return IRNibError(&RNibError{err:error, errorCode:RESOURCE_NOT_FOUND}) -} - -func NewInternalError(error error) IRNibError { - return IRNibError(&RNibError{err:error, errorCode:INTERNAL_ERROR}) -} - -func NewValidationError(error error) IRNibError { - return IRNibError(&RNibError{err:error, errorCode:VALIDATION_ERROR}) -} - -func (e RNibError) GetError() error { - return e.err -} - -func (e RNibError) GetCode() int { - return e.errorCode -} - -func (e RNibError) Error() string { - return fmt.Sprintf("%d %s - %s", e.errorCode, rNibError_names[e.errorCode], e.err) -} \ No newline at end of file diff --git a/common/rNibErrors_test.go b/common/rNibErrors_test.go deleted file mode 100644 index 5e4ea77..0000000 --- a/common/rNibErrors_test.go +++ /dev/null @@ -1,100 +0,0 @@ -// -// Copyright 2019 AT&T Intellectual Property -// Copyright 2019 Nokia -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// - -package common - -import ( - "fmt" - "github.com/pkg/errors" - "github.com/stretchr/testify/assert" - "testing" -) - -func TestNewResourceNotFoundError(t *testing.T){ - e := errors.New("Expected error") - expectedErr := NewResourceNotFoundError(e) - assert.NotNil(t, expectedErr) - assert.Equal(t, RESOURCE_NOT_FOUND, expectedErr.GetCode()) - assert.Contains(t, expectedErr.Error(), fmt.Sprintf("%d",expectedErr.GetCode())) - assert.Contains(t, expectedErr.Error(), rNibError_names[expectedErr.GetCode()]) - assert.Contains(t, expectedErr.Error(), e.Error()) - assert.Equal(t, RESOURCE_NOT_FOUND, expectedErr.GetCode()) -} - -func TestNewInternalError(t *testing.T){ - e := errors.New("Expected error") - expectedErr := NewInternalError(e) - assert.NotNil(t, expectedErr) - assert.Equal(t, INTERNAL_ERROR, expectedErr.GetCode()) - assert.Contains(t, expectedErr.Error(), fmt.Sprintf("%d",expectedErr.GetCode())) - assert.Contains(t, expectedErr.Error(), rNibError_names[expectedErr.GetCode()]) - assert.Contains(t, expectedErr.Error(), e.Error()) - assert.Equal(t, INTERNAL_ERROR, expectedErr.GetCode()) -} - -func TestNewValidationError(t *testing.T){ - e := errors.New("Expected error") - expectedErr := NewValidationError(e) - assert.NotNil(t, expectedErr) - assert.Equal(t, VALIDATION_ERROR, expectedErr.GetCode()) - assert.Contains(t, expectedErr.Error(), fmt.Sprintf("%d",expectedErr.GetCode())) - assert.Contains(t, expectedErr.Error(), rNibError_names[expectedErr.GetCode()]) - assert.Contains(t, expectedErr.Error(), e.Error()) - assert.Equal(t, VALIDATION_ERROR, expectedErr.GetCode()) -} - -func TestNewRNibErrorWithEmptyError(t *testing.T){ - var e error - expectedErr := NewResourceNotFoundError(e) - assert.NotNil(t, expectedErr) - assert.Equal(t, RESOURCE_NOT_FOUND, expectedErr.GetCode()) - assert.Contains(t, expectedErr.Error(), fmt.Sprintf("%d",expectedErr.GetCode())) - assert.Contains(t, expectedErr.Error(), rNibError_names[expectedErr.GetCode()]) - assert.Equal(t, 1, expectedErr.GetCode()) -} - -func TestGetError(t *testing.T){ - e := errors.New("Expected error") - expectedErr := NewInternalError(e) - assert.NotNil(t, expectedErr) - assert.NotNil(t, expectedErr.GetError()) - assert.Equal(t, expectedErr.GetError(), e) -} - -func TestGetCodeInternalError(t *testing.T){ - e := errors.New("Expected error") - expectedErr := NewInternalError(e) - assert.NotNil(t, expectedErr) - assert.NotNil(t, expectedErr.GetError()) - assert.Equal(t, INTERNAL_ERROR, expectedErr.GetCode()) -} - -func TestGetCodeNotFoundError(t *testing.T){ - e := errors.New("Expected error") - expectedErr := NewResourceNotFoundError(e) - assert.NotNil(t, expectedErr) - assert.NotNil(t, expectedErr.GetError()) - assert.Equal(t, RESOURCE_NOT_FOUND, expectedErr.GetCode()) -} - -func TestGetCodeValidationError(t *testing.T){ - e := errors.New("Expected error") - expectedErr := NewValidationError(e) - assert.NotNil(t, expectedErr) - assert.NotNil(t, expectedErr.GetError()) - assert.Equal(t, VALIDATION_ERROR, expectedErr.GetCode()) -} \ No newline at end of file diff --git a/common/resourceNotFoundError.go b/common/resourceNotFoundError.go new file mode 100644 index 0000000..4775d0f --- /dev/null +++ b/common/resourceNotFoundError.go @@ -0,0 +1,36 @@ +// +// Copyright 2019 AT&T Intellectual Property +// Copyright 2019 Nokia +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// + +package common + +import "fmt" + +type ResourceNotFoundError struct{ + message string +} + +func NewResourceNotFoundError(msg string) error { + return &ResourceNotFoundError{message:msg} +} + +func NewResourceNotFoundErrorf(fmtMsg string, a ...interface{}) error { + return &ResourceNotFoundError{message:fmt.Sprintf(fmtMsg, a...)} +} + +func (e ResourceNotFoundError) Error() string { + return e.message +} \ No newline at end of file diff --git a/common/resourceNotFoundError_test.go b/common/resourceNotFoundError_test.go new file mode 100644 index 0000000..d7ff568 --- /dev/null +++ b/common/resourceNotFoundError_test.go @@ -0,0 +1,42 @@ +// +// Copyright 2019 AT&T Intellectual Property +// Copyright 2019 Nokia +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// + +package common + +import ( + "fmt" + "github.com/stretchr/testify/assert" + "testing" +) + +func TestNewResourceNotFoundError(t *testing.T){ + msg := "Expected error" + expectedErr := NewResourceNotFoundError(msg) + assert.NotNil(t, expectedErr) + assert.IsType(t, &ResourceNotFoundError{}, expectedErr) + assert.Contains(t, expectedErr.Error(), msg) +} + +func TestNewResourceNotFoundErrorf(t *testing.T){ + msg := "Expected error: %s, %s" + var args []interface{} + args = append(args, "arg1", "arg2") + expectedErr := NewResourceNotFoundErrorf(msg, args...) + assert.NotNil(t, expectedErr) + assert.IsType(t, &ResourceNotFoundError{}, expectedErr) + assert.Contains(t, expectedErr.Error(), fmt.Sprintf(msg, args...)) +} \ No newline at end of file diff --git a/common/utils.go b/common/utils.go index 7b7b4e2..baf5d23 100644 --- a/common/utils.go +++ b/common/utils.go @@ -17,17 +17,15 @@ package common import ( - "errors" "fmt" ) /* ValidateAndBuildCellIdKey builds key according to the specified format returns the resulting string */ -func ValidateAndBuildCellIdKey(cellId string) (string, IRNibError) { +func ValidateAndBuildCellIdKey(cellId string) (string, error) { if cellId == "" { - e := errors.New("#utils.ValidateAndBuildCellIdKey - an empty cell id received") - return "", NewValidationError(e) + return "", NewValidationError("#utils.ValidateAndBuildCellIdKey - an empty cell id received") } return fmt.Sprintf("CELL:%s", cellId), nil } @@ -35,10 +33,9 @@ func ValidateAndBuildCellIdKey(cellId string) (string, IRNibError) { /* ValidateAndBuildNrCellIdKey builds key according to the specified format returns the resulting string */ -func ValidateAndBuildNrCellIdKey(cellId string) (string, IRNibError) { +func ValidateAndBuildNrCellIdKey(cellId string) (string, error) { if cellId == "" { - e := errors.New("#utils.ValidateAndBuildNrCellIdKey - an empty cell id received") - return "", NewValidationError(e) + return "", NewValidationError("#utils.ValidateAndBuildNrCellIdKey - an empty cell id received") } return fmt.Sprintf("NRCELL:%s", cellId), nil } @@ -46,10 +43,9 @@ func ValidateAndBuildNrCellIdKey(cellId string) (string, IRNibError) { /* ValidateAndBuildNodeBNameKey builds key according to the specified format returns the resulting string */ -func ValidateAndBuildNodeBNameKey(inventoryName string) (string, IRNibError) { +func ValidateAndBuildNodeBNameKey(inventoryName string) (string, error) { if inventoryName == "" { - e := errors.New("#utils.ValidateAndBuildNodeBNameKey - an empty inventory name received") - return "", NewValidationError(e) + return "", NewValidationError("#utils.ValidateAndBuildNodeBNameKey - an empty inventory name received") } return fmt.Sprintf("RAN:%s", inventoryName), nil } @@ -57,18 +53,15 @@ func ValidateAndBuildNodeBNameKey(inventoryName string) (string, IRNibError) { /* ValidateAndBuildNodeBIdKey builds key according to the specified format returns the resulting string */ -func ValidateAndBuildNodeBIdKey(nodeType string, plmnId string, nbId string) (string, IRNibError) { +func ValidateAndBuildNodeBIdKey(nodeType string, plmnId string, nbId string) (string, error) { if nodeType == "" { - e := errors.New("#utils.ValidateAndBuildNodeBIdKey - an empty node type received") - return "", NewValidationError(e) + return "", NewValidationError("#utils.ValidateAndBuildNodeBIdKey - an empty node type received") } if plmnId == "" { - e := errors.New("#utils.ValidateAndBuildNodeBIdKey - an empty plmnId received") - return "", NewValidationError(e) + return "", NewValidationError("#utils.ValidateAndBuildNodeBIdKey - an empty plmnId received") } if nbId == "" { - e := errors.New("#utils.ValidateAndBuildNodeBIdKey - an empty nbId received") - return "", NewValidationError(e) + return "", NewValidationError("#utils.ValidateAndBuildNodeBIdKey - an empty nbId received") } return fmt.Sprintf("%s:%s:%s", nodeType, plmnId, nbId), nil } @@ -76,19 +69,17 @@ func ValidateAndBuildNodeBIdKey(nodeType string, plmnId string, nbId string) (st /* ValidateAndBuildCellNamePciKey builds key according to the specified format returns the resulting string */ -func ValidateAndBuildCellNamePciKey(inventoryName string, pci uint32) (string, IRNibError) { +func ValidateAndBuildCellNamePciKey(inventoryName string, pci uint32) (string, error) { if inventoryName == "" { - e := errors.New("#utils.ValidateAndBuildCellNamePciKey - an empty inventory name received") - return "", NewValidationError(e) + return "", NewValidationError("#utils.ValidateAndBuildCellNamePciKey - an empty inventory name received") } return fmt.Sprintf("PCI:%s:%02x", inventoryName, pci), nil } -func ValidateAndBuildRanLoadInformationKey(inventoryName string) (string, IRNibError) { +func ValidateAndBuildRanLoadInformationKey(inventoryName string) (string, error) { if inventoryName == "" { - e := errors.New("#utils.ValidateAndBuildRanLoadInformationKey - an empty inventory name received") - return "", NewValidationError(e) + return "", NewValidationError("#utils.ValidateAndBuildRanLoadInformationKey - an empty inventory name received") } return fmt.Sprintf("LOAD:%s", inventoryName), nil diff --git a/common/utils_test.go b/common/utils_test.go index f9d71cc..6df653e 100644 --- a/common/utils_test.go +++ b/common/utils_test.go @@ -66,22 +66,22 @@ func TestValidateAndBuildCellNamePciKeyNameValidationFailure(t *testing.T){ pci := 9999 _, err := ValidateAndBuildCellNamePciKey("", uint32(pci)) assert.NotNil(t, err) - assert.Equal(t, VALIDATION_ERROR, err.GetCode()) - assert.Equal(t, "3 VALIDATION_ERROR - #utils.ValidateAndBuildCellNamePciKey - an empty inventory name received", err.Error()) + assert.IsType(t, &ValidationError{}, err) + assert.Equal(t, "#utils.ValidateAndBuildCellNamePciKey - an empty inventory name received", err.Error()) } func TestValidateAndBuildCellKeyCellidValidationFailure(t *testing.T) { _, err := ValidateAndBuildCellIdKey("") assert.NotNil(t, err) - assert.Equal(t, VALIDATION_ERROR, err.GetCode()) - assert.Equal(t, "3 VALIDATION_ERROR - #utils.ValidateAndBuildCellIdKey - an empty cell id received", err.Error()) + assert.IsType(t, &ValidationError{}, err) + assert.Equal(t, "#utils.ValidateAndBuildCellIdKey - an empty cell id received", err.Error()) } func TestValidateAndBuildNodeBNameKeyNameValidationFailure(t *testing.T) { _, err := ValidateAndBuildNodeBNameKey("") assert.NotNil(t, err) - assert.Equal(t, VALIDATION_ERROR, err.GetCode()) - assert.Equal(t, "3 VALIDATION_ERROR - #utils.ValidateAndBuildNodeBNameKey - an empty inventory name received", err.Error()) + assert.IsType(t, &ValidationError{}, err) + assert.Equal(t, "#utils.ValidateAndBuildNodeBNameKey - an empty inventory name received", err.Error()) } func TestValidateAndBuildNodeBIdKeyNodeTypeValidationFailure(t *testing.T) { @@ -89,8 +89,8 @@ func TestValidateAndBuildNodeBIdKeyNodeTypeValidationFailure(t *testing.T) { nbId := "eeee" _, err := ValidateAndBuildNodeBIdKey("", plmnId, nbId) assert.NotNil(t, err) - assert.Equal(t, VALIDATION_ERROR, err.GetCode()) - assert.Equal(t, "3 VALIDATION_ERROR - #utils.ValidateAndBuildNodeBIdKey - an empty node type received", err.Error()) + assert.IsType(t, &ValidationError{}, err) + assert.Equal(t, "#utils.ValidateAndBuildNodeBIdKey - an empty node type received", err.Error()) } func TestValidateAndBuildNodeBIdKeyPlmnIdValidationFailure(t *testing.T) { @@ -98,8 +98,8 @@ func TestValidateAndBuildNodeBIdKeyPlmnIdValidationFailure(t *testing.T) { nbId := "aaaa" _, err := ValidateAndBuildNodeBIdKey(nodeType, "", nbId) assert.NotNil(t, err) - assert.Equal(t, VALIDATION_ERROR, err.GetCode()) - assert.Equal(t, "3 VALIDATION_ERROR - #utils.ValidateAndBuildNodeBIdKey - an empty plmnId received", err.Error()) + assert.IsType(t, &ValidationError{}, err) + assert.Equal(t, "#utils.ValidateAndBuildNodeBIdKey - an empty plmnId received", err.Error()) } func TestValidateAndBuildNodeBIdKeyNbIdValidationFailure(t *testing.T) { @@ -107,8 +107,8 @@ func TestValidateAndBuildNodeBIdKeyNbIdValidationFailure(t *testing.T) { plmnId := "cccc" _, err := ValidateAndBuildNodeBIdKey(nodeType, plmnId, "") assert.NotNil(t, err) - assert.Equal(t, VALIDATION_ERROR, err.GetCode()) - assert.Equal(t, "3 VALIDATION_ERROR - #utils.ValidateAndBuildNodeBIdKey - an empty nbId received", err.Error()) + assert.IsType(t, &ValidationError{}, err) + assert.Equal(t, "#utils.ValidateAndBuildNodeBIdKey - an empty nbId received", err.Error()) } func TestValidateAndBuildCellNamePciKeySuccess(t *testing.T){ @@ -144,5 +144,5 @@ func TestValidateAndBuildRanLoadInformationKeyFailure(t *testing.T) { name := "" _, err := ValidateAndBuildRanLoadInformationKey(name) assert.NotNil(t, err) - assert.Equal(t, VALIDATION_ERROR, err.GetCode()) + assert.IsType(t, &ValidationError{}, err) } \ No newline at end of file diff --git a/common/validationError.go b/common/validationError.go new file mode 100644 index 0000000..50eb710 --- /dev/null +++ b/common/validationError.go @@ -0,0 +1,36 @@ +// +// Copyright 2019 AT&T Intellectual Property +// Copyright 2019 Nokia +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// + +package common + +import "fmt" + +type ValidationError struct{ + message string +} + +func NewValidationError(msg string) error { + return &ValidationError{message:msg} +} + +func NewValidationErrorf(fmtMsg string, a ...interface{}) error { + return &ValidationError{message: fmt.Sprintf(fmtMsg, a...)} +} + +func (e ValidationError) Error() string { + return e.message +} \ No newline at end of file diff --git a/common/validationError_test.go b/common/validationError_test.go new file mode 100644 index 0000000..8223514 --- /dev/null +++ b/common/validationError_test.go @@ -0,0 +1,42 @@ +// +// Copyright 2019 AT&T Intellectual Property +// Copyright 2019 Nokia +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// + +package common + +import ( + "fmt" + "github.com/stretchr/testify/assert" + "testing" +) + +func TestNewValidationError(t *testing.T){ + msg := "Expected error" + expectedErr := NewValidationError(msg) + assert.NotNil(t, expectedErr) + assert.IsType(t, &ValidationError{}, expectedErr) + assert.Contains(t, expectedErr.Error(), msg) +} + +func TestNewValidationErrorf(t *testing.T){ + msg := "Expected error: %s, %s" + var args []interface{} + args = append(args, "arg1", "arg2") + expectedErr := NewValidationErrorf(msg, args...) + assert.NotNil(t, expectedErr) + assert.IsType(t, &ValidationError{}, expectedErr) + assert.Contains(t, expectedErr.Error(), fmt.Sprintf(msg, args...)) +} \ No newline at end of file diff --git a/reader/rNibReader.go b/reader/rNibReader.go index 49ee38c..f6493b6 100644 --- a/reader/rNibReader.go +++ b/reader/rNibReader.go @@ -21,7 +21,6 @@ import ( "gerrit.o-ran-sc.org/r/ric-plt/nodeb-rnib.git/entities" "gerrit.o-ran-sc.org/r/ric-plt/sdlgo" "github.com/golang/protobuf/proto" - "github.com/pkg/errors" "reflect" ) @@ -37,25 +36,25 @@ RNibReader interface allows retrieving data from redis BD by various keys */ type RNibReader interface { // GetNodeb retrieves responding nodeb entity from redis DB by nodeb inventory name - GetNodeb(inventoryName string) (*entities.NodebInfo, common.IRNibError) + GetNodeb(inventoryName string) (*entities.NodebInfo, error) // GetNodebByGlobalNbId retrieves responding nodeb entity from redis DB by nodeb global Id - GetNodebByGlobalNbId(nodeType entities.Node_Type, globalNbId *entities.GlobalNbId) (*entities.NodebInfo, common.IRNibError) + GetNodebByGlobalNbId(nodeType entities.Node_Type, globalNbId *entities.GlobalNbId) (*entities.NodebInfo, error) // GetCellList retrieves the list of cell entities belonging to responding nodeb entity from redis DB by nodeb inventory name - GetCellList(inventoryName string) (*entities.Cells, common.IRNibError) + GetCellList(inventoryName string) (*entities.Cells, error) // GetListGnbIds retrieves the list of gNodeb identity entities - GetListGnbIds() (*[]*entities.NbIdentity, common.IRNibError) + GetListGnbIds() ([]*entities.NbIdentity, error) // GetListEnbIds retrieves the list of eNodeb identity entities - GetListEnbIds() (*[]*entities.NbIdentity, common.IRNibError) + GetListEnbIds() ([]*entities.NbIdentity, error) // Close closes reader's pool - GetCountGnbList() (int, common.IRNibError) + GetCountGnbList() (int, error) // GetCell retrieves the cell entity belonging to responding nodeb from redis DB by nodeb inventory name and cell pci - GetCell(inventoryName string, pci uint32) (*entities.Cell, common.IRNibError) + GetCell(inventoryName string, pci uint32) (*entities.Cell, error) // GetCellById retrieves the cell entity from redis DB by cell type and cell Id - GetCellById(cellType entities.Cell_Type, cellId string) (*entities.Cell, common.IRNibError) + GetCellById(cellType entities.Cell_Type, cellId string) (*entities.Cell, error) // GetListNodebIds returns the full list of Nodeb identity entities - GetListNodebIds()([]*entities.NbIdentity, common.IRNibError) + GetListNodebIds()([]*entities.NbIdentity, error) // GetRanLoadInformation retrieves nodeb load information entity from redis DB by nodeb inventory name - GetRanLoadInformation(inventoryName string) (*entities.RanLoadInformation, common.IRNibError) + GetRanLoadInformation(inventoryName string) (*entities.RanLoadInformation, error) } /* @@ -68,7 +67,10 @@ func Init(namespace string, poolSize int) { return &rNibReaderInstance{sdl: &sdlI, namespace: namespace} }, func(obj interface{}) { - (*obj.(*rNibReaderInstance).sdl).Close() + i, ok := obj.(*rNibReaderInstance) + if ok{ + (*i.sdl).Close() + } }) } @@ -76,32 +78,37 @@ func initPool(poolSize int, newObj func() interface{}, destroyObj func(interface readerPool = common.NewPool(poolSize, newObj, destroyObj) } -/* -GetRNibReader returns RNibReader instance from the pool -*/ -func GetRNibReader() RNibReader { - return readerPool.Get().(RNibReader) -} - -func (w *rNibReaderInstance) GetNodeb(inventoryName string) (*entities.NodebInfo, common.IRNibError) { +func (*rNibReaderInstance) GetNodeb(inventoryName string) (*entities.NodebInfo, error) { + w := readerPool.Get().(*rNibReaderInstance) defer readerPool.Put(w) key, rNibErr := common.ValidateAndBuildNodeBNameKey(inventoryName) if rNibErr != nil { return nil, rNibErr } - return w.getNodeb(key) + nbInfo := &entities.NodebInfo{} + err := w.getByKeyAndUnmarshal(key, nbInfo) + if err!= nil{ + return nil, err + } + return nbInfo, nil } -func (w *rNibReaderInstance) GetNodebByGlobalNbId(nodeType entities.Node_Type, globalNbId *entities.GlobalNbId) (*entities.NodebInfo, common.IRNibError) { +func (*rNibReaderInstance) GetNodebByGlobalNbId(nodeType entities.Node_Type, globalNbId *entities.GlobalNbId) (*entities.NodebInfo, error) { + w := readerPool.Get().(*rNibReaderInstance) defer readerPool.Put(w) key, rNibErr := common.ValidateAndBuildNodeBIdKey(nodeType.String(), globalNbId.GetPlmnId(), globalNbId.GetNbId()) if rNibErr != nil { return nil, rNibErr } - return w.getNodeb(key) + nbInfo := &entities.NodebInfo{} + err := w.getByKeyAndUnmarshal(key, nbInfo) + if err!= nil{ + return nil, err + } + return nbInfo, nil } -func (w *rNibReaderInstance) GetCellList(inventoryName string) (*entities.Cells, common.IRNibError) { +func (w *rNibReaderInstance) GetCellList(inventoryName string) (*entities.Cells, error) { cells := &entities.Cells{} nb, err := w.GetNodeb(inventoryName) if err != nil { @@ -111,25 +118,29 @@ func (w *rNibReaderInstance) GetCellList(inventoryName string) (*entities.Cells, cells.Type = entities.Cell_LTE_CELL cells.List = &entities.Cells_ServedCellInfos{ServedCellInfos: &entities.ServedCellInfoList{ServedCells: nb.GetEnb().GetServedCells()}} return cells, nil - } else if nb.GetGnb() != nil && len(nb.GetGnb().GetServedNrCells()) > 0 { + } + if nb.GetGnb() != nil && len(nb.GetGnb().GetServedNrCells()) > 0 { cells.Type = entities.Cell_NR_CELL cells.List = &entities.Cells_ServedNrCells{ServedNrCells: &entities.ServedNRCellList{ServedCells: nb.GetGnb().GetServedNrCells()}} return cells, nil } - return nil, common.NewResourceNotFoundError(errors.Errorf("#rNibReader.GetCellList - served cells not found. Responding node RAN name: %s.", inventoryName)) + return nil, common.NewResourceNotFoundErrorf("#rNibReader.GetCellList - served cells not found. Responding node RAN name: %s.", inventoryName) } -func (w *rNibReaderInstance) GetListGnbIds() (*[]*entities.NbIdentity, common.IRNibError) { +func (*rNibReaderInstance) GetListGnbIds() ([]*entities.NbIdentity, error) { + w := readerPool.Get().(*rNibReaderInstance) defer readerPool.Put(w) return w.getListNodebIdsByType(entities.Node_GNB.String()) } -func (w *rNibReaderInstance) GetListEnbIds() (*[]*entities.NbIdentity, common.IRNibError) { +func (*rNibReaderInstance) GetListEnbIds() ([]*entities.NbIdentity, error) { + w := readerPool.Get().(*rNibReaderInstance) defer readerPool.Put(w) return w.getListNodebIdsByType(entities.Node_ENB.String()) } -func (w *rNibReaderInstance) GetCountGnbList() (int, common.IRNibError) { +func (*rNibReaderInstance) GetCountGnbList() (int, error) { + w := readerPool.Get().(*rNibReaderInstance) defer readerPool.Put(w) size, err := (*w.sdl).GroupSize(entities.Node_GNB.String()) if err != nil { @@ -138,33 +149,46 @@ func (w *rNibReaderInstance) GetCountGnbList() (int, common.IRNibError) { return int(size), nil } -func (w *rNibReaderInstance) GetCell(inventoryName string, pci uint32) (*entities.Cell, common.IRNibError) { +func (*rNibReaderInstance) GetCell(inventoryName string, pci uint32) (*entities.Cell, error) { + w := readerPool.Get().(*rNibReaderInstance) defer readerPool.Put(w) key, rNibErr := common.ValidateAndBuildCellNamePciKey(inventoryName, pci) if rNibErr != nil { return nil, rNibErr } - return w.getCellByKey(key) + cell := &entities.Cell{} + err := w.getByKeyAndUnmarshal(key, cell) + if err!= nil{ + return nil, err + } + return cell, err } -func (w *rNibReaderInstance) GetCellById(cellType entities.Cell_Type, cellId string) (*entities.Cell, common.IRNibError) { +func (*rNibReaderInstance) GetCellById(cellType entities.Cell_Type, cellId string) (*entities.Cell, error) { + w := readerPool.Get().(*rNibReaderInstance) defer readerPool.Put(w) var key string - var rNibErr common.IRNibError + var rNibErr error if cellType == entities.Cell_LTE_CELL { key, rNibErr = common.ValidateAndBuildCellIdKey(cellId) } else if cellType == entities.Cell_NR_CELL { key, rNibErr = common.ValidateAndBuildNrCellIdKey(cellId) } else { - return nil, common.NewValidationError(errors.Errorf("#rNibReader.GetCellById - invalid cell type: %v", cellType)) + return nil, common.NewValidationErrorf("#rNibReader.GetCellById - invalid cell type: %v", cellType) } if rNibErr != nil { return nil, rNibErr } - return w.getCellByKey(key) + cell := &entities.Cell{} + err := w.getByKeyAndUnmarshal(key, cell) + if err!= nil{ + return nil, err + } + return cell, err } -func (w *rNibReaderInstance) GetListNodebIds()([]*entities.NbIdentity, common.IRNibError){ +func (*rNibReaderInstance) GetListNodebIds()([]*entities.NbIdentity, error){ + w := readerPool.Get().(*rNibReaderInstance) defer readerPool.Put(w) dataEnb, err := (*w.sdl).GetMembers(entities.Node_ENB.String()) if err != nil{ @@ -180,11 +204,12 @@ func (w *rNibReaderInstance) GetListNodebIds()([]*entities.NbIdentity, common.IR } allIds := append(dataEnb, dataGnb...) allIds = append(allIds, dataUnknown...) - data, rnibErr := unmarshalIdentityList(allIds) - return *data, rnibErr + data, rnibErr := w.unmarshalIdentityList(allIds) + return data, rnibErr } -func (w *rNibReaderInstance) GetRanLoadInformation(inventoryName string) (*entities.RanLoadInformation, common.IRNibError){ +func (*rNibReaderInstance) GetRanLoadInformation(inventoryName string) (*entities.RanLoadInformation, error){ + w := readerPool.Get().(*rNibReaderInstance) key, rNibErr := common.ValidateAndBuildRanLoadInformationKey(inventoryName) if rNibErr != nil { return nil, rNibErr @@ -197,7 +222,7 @@ func (w *rNibReaderInstance) GetRanLoadInformation(inventoryName string) (*entit return loadInfo, err } -func (w *rNibReaderInstance) getByKeyAndUnmarshal(key string, entity proto.Message)common.IRNibError{ +func (w *rNibReaderInstance) getByKeyAndUnmarshal(key string, entity proto.Message)error{ data, err := (*w.sdl).Get([]string{key}) if err != nil { return common.NewInternalError(err) @@ -209,50 +234,18 @@ func (w *rNibReaderInstance) getByKeyAndUnmarshal(key string, entity proto.Messa } return nil } - return common.NewResourceNotFoundError(errors.Errorf("#rNibReader.getByKeyAndUnmarshal - entity of type %s not found. Key: %s", reflect.TypeOf(entity).String(), key)) -} - -func (w *rNibReaderInstance) getNodeb(key string) (*entities.NodebInfo, common.IRNibError) { - data, err := (*w.sdl).Get([]string{key}) - if err != nil { - return nil, common.NewInternalError(err) - } - nb := entities.NodebInfo{} - if data != nil && data[key] != nil { - err = proto.Unmarshal([]byte(data[key].(string)), &nb) - if err != nil { - return nil, common.NewInternalError(err) - } - return &nb, nil - } - return nil, common.NewResourceNotFoundError(errors.Errorf("#rNibReader.getNodeb - responding node not found. Key: %s", key)) -} - -func (w *rNibReaderInstance) getCellByKey(key string) (*entities.Cell, common.IRNibError) { - data, err := (*w.sdl).Get([]string{key}) - if err != nil { - return nil, common.NewInternalError(err) - } - cell := entities.Cell{} - if data != nil && data[key] != nil { - err = proto.Unmarshal([]byte(data[key].(string)), &cell) - if err != nil { - return nil, common.NewInternalError(err) - } - return &cell, nil - } - return nil, common.NewResourceNotFoundError(errors.Errorf("#rNibReader.getCellByKey - cell not found, key: %s", key)) + return common.NewResourceNotFoundErrorf("#rNibReader.getByKeyAndUnmarshal - entity of type %s not found. Key: %s", reflect.TypeOf(entity).String(), key) } -func (w *rNibReaderInstance) getListNodebIdsByType(nbType string) (*[]*entities.NbIdentity, common.IRNibError) { +func (w *rNibReaderInstance) getListNodebIdsByType(nbType string) ([]*entities.NbIdentity, error) { data, err := (*w.sdl).GetMembers(nbType) if err != nil { return nil, common.NewInternalError(err) } - return unmarshalIdentityList(data) + return w.unmarshalIdentityList(data) } -func unmarshalIdentityList(data []string) (*[]*entities.NbIdentity, common.IRNibError) { +func (w *rNibReaderInstance) unmarshalIdentityList(data []string) ([]*entities.NbIdentity, error) { var members []*entities.NbIdentity for _, d := range data { member := entities.NbIdentity{} @@ -262,7 +255,7 @@ func unmarshalIdentityList(data []string) (*[]*entities.NbIdentity, common.IRNib } members = append(members, &member) } - return &members, nil + return members, nil } func Close() { diff --git a/reader/rNibReader_test.go b/reader/rNibReader_test.go index fc1dee8..270f27b 100644 --- a/reader/rNibReader_test.go +++ b/reader/rNibReader_test.go @@ -18,10 +18,10 @@ package reader import ( "encoding/json" - "errors" "gerrit.o-ran-sc.org/r/ric-plt/nodeb-rnib.git/common" "gerrit.o-ran-sc.org/r/ric-plt/nodeb-rnib.git/entities" "github.com/golang/protobuf/proto" + "github.com/pkg/errors" "github.com/stretchr/testify/assert" "testing" "time" @@ -74,7 +74,7 @@ func TestGetNodeB(t *testing.T) { name := "name" readerPool = nil sdlInstanceMock := initSdlInstanceMock(namespace, 1) - w := GetRNibReader() + w := &rNibReaderInstance{} nb := entities.NodebInfo{} nb.ConnectionStatus = 1 nb.Ip = "localhost" @@ -107,7 +107,7 @@ func TestGetNodeBNotFoundFailure(t *testing.T) { name := "name" readerPool = nil sdlInstanceMock := initSdlInstanceMock(namespace, 1) - w := GetRNibReader() + w := &rNibReaderInstance{} var e error var ret map[string]interface{} redisKey, rNibErr := common.ValidateAndBuildNodeBNameKey(name) @@ -118,15 +118,15 @@ func TestGetNodeBNotFoundFailure(t *testing.T) { getNb, er := w.GetNodeb(name) assert.NotNil(t, er) assert.Nil(t, getNb) - assert.Equal(t, 1, er.GetCode()) - assert.EqualValues(t, "1 RESOURCE_NOT_FOUND - #rNibReader.getNodeb - responding node not found. Key: RAN:name", er.Error()) + assert.IsType(t, &common.ResourceNotFoundError{}, er) + assert.EqualValues(t, "#rNibReader.getByKeyAndUnmarshal - entity of type *entities.NodebInfo not found. Key: RAN:name", er.Error()) } func TestGetNodeBUnmarshalFailure(t *testing.T) { name := "name" readerPool = nil sdlInstanceMock := initSdlInstanceMock(namespace, 1) - w := GetRNibReader() + w := &rNibReaderInstance{} var e error ret := make(map[string]interface{}, 1) redisKey, rNibErr := common.ValidateAndBuildNodeBNameKey(name) @@ -138,17 +138,17 @@ func TestGetNodeBUnmarshalFailure(t *testing.T) { getNb, er := w.GetNodeb(name) assert.NotNil(t, er) assert.Nil(t, getNb) - assert.Equal(t, 2, er.GetCode()) - assert.EqualValues(t, "2 INTERNAL_ERROR - proto: can't skip unknown wire type 4", er.Error()) + assert.IsType(t, &common.InternalError{}, er) + assert.EqualValues(t, "proto: can't skip unknown wire type 4", er.Error()) } func TestGetNodeBSdlgoFailure(t *testing.T) { name := "name" errMsg := "expected Sdlgo error" - errMsgExpected := "2 INTERNAL_ERROR - expected Sdlgo error" + errMsgExpected := "expected Sdlgo error" readerPool = nil sdlInstanceMock := initSdlInstanceMock(namespace, 1) - w := GetRNibReader() + w := &rNibReaderInstance{} e := errors.New(errMsg) var ret map[string]interface{} redisKey, rNibErr := common.ValidateAndBuildNodeBNameKey(name) @@ -159,7 +159,7 @@ func TestGetNodeBSdlgoFailure(t *testing.T) { getNb, er := w.GetNodeb(name) assert.NotNil(t, er) assert.Nil(t, getNb) - assert.Equal(t, 2, er.GetCode()) + assert.IsType(t, &common.InternalError{}, er) assert.EqualValues(t, errMsgExpected, er.Error()) } @@ -167,7 +167,7 @@ func TestGetNodeBCellsListEnb(t *testing.T) { name := "name" readerPool = nil sdlInstanceMock := initSdlInstanceMock(namespace, 1) - w := GetRNibReader() + w := &rNibReaderInstance{} nb := entities.NodebInfo{} nb.ConnectionStatus = 1 nb.Ip = "localhost" @@ -199,7 +199,7 @@ func TestGetNodeBCellsListGnb(t *testing.T) { name := "name" readerPool = nil sdlInstanceMock := initSdlInstanceMock(namespace, 1) - w := GetRNibReader() + w := &rNibReaderInstance{} nb := entities.NodebInfo{} nb.ConnectionStatus = 1 nb.Ip = "localhost" @@ -232,7 +232,7 @@ func TestGetNodeBCellsListNodeUnmarshalFailure(t *testing.T) { name := "name" readerPool = nil sdlInstanceMock := initSdlInstanceMock(namespace, 1) - w := GetRNibReader() + w := &rNibReaderInstance{} var e error ret := make(map[string]interface{}, 1) redisKey, rNibErr := common.ValidateAndBuildNodeBNameKey(name) @@ -244,15 +244,15 @@ func TestGetNodeBCellsListNodeUnmarshalFailure(t *testing.T) { cells, er := w.GetCellList(name) assert.NotNil(t, er) assert.Nil(t, cells) - assert.Equal(t, 2, er.GetCode()) - assert.EqualValues(t, "2 INTERNAL_ERROR - proto: can't skip unknown wire type 4", er.Error()) + assert.IsType(t, &common.InternalError{}, er) + assert.EqualValues(t, "proto: can't skip unknown wire type 4", er.Error()) } func TestGetNodeBCellsListNodeNotFoundFailure(t *testing.T) { name := "name" readerPool = nil sdlInstanceMock := initSdlInstanceMock(namespace, 1) - w := GetRNibReader() + w := &rNibReaderInstance{} var e error var ret map[string]interface{} redisKey, rNibErr := common.ValidateAndBuildNodeBNameKey(name) @@ -263,15 +263,15 @@ func TestGetNodeBCellsListNodeNotFoundFailure(t *testing.T) { cells, er := w.GetCellList(name) assert.NotNil(t, er) assert.Nil(t, cells) - assert.Equal(t, 1, er.GetCode()) - assert.EqualValues(t, "1 RESOURCE_NOT_FOUND - #rNibReader.getNodeb - responding node not found. Key: RAN:name", er.Error()) + assert.IsType(t, &common.ResourceNotFoundError{}, er) + assert.EqualValues(t, "#rNibReader.getByKeyAndUnmarshal - entity of type *entities.NodebInfo not found. Key: RAN:name", er.Error()) } func TestGetNodeBCellsListNotFoundFailureEnb(t *testing.T) { name := "name" readerPool = nil sdlInstanceMock := initSdlInstanceMock(namespace, 1) - w := GetRNibReader() + w := &rNibReaderInstance{} nb := entities.NodebInfo{} nb.ConnectionStatus = 1 nb.Ip = "localhost" @@ -291,14 +291,14 @@ func TestGetNodeBCellsListNotFoundFailureEnb(t *testing.T) { sdlInstanceMock.On("Get", []string{redisKey}).Return(ret, e) _, er := w.GetCellList(name) assert.NotNil(t, er) - assert.EqualValues(t, "1 RESOURCE_NOT_FOUND - #rNibReader.GetCellList - served cells not found. Responding node RAN name: name.", er.Error()) + assert.EqualValues(t, "#rNibReader.GetCellList - served cells not found. Responding node RAN name: name.", er.Error()) } func TestGetNodeBCellsListNotFoundFailureGnb(t *testing.T) { name := "name" readerPool = nil sdlInstanceMock := initSdlInstanceMock(namespace, 1) - w := GetRNibReader() + w := &rNibReaderInstance{} nb := entities.NodebInfo{} nb.ConnectionStatus = 1 nb.Ip = "localhost" @@ -318,13 +318,17 @@ func TestGetNodeBCellsListNotFoundFailureGnb(t *testing.T) { sdlInstanceMock.On("Get", []string{redisKey}).Return(ret, e) _, er := w.GetCellList(name) assert.NotNil(t, er) - assert.EqualValues(t, "1 RESOURCE_NOT_FOUND - #rNibReader.GetCellList - served cells not found. Responding node RAN name: name.", er.Error()) + assert.EqualValues(t, "#rNibReader.GetCellList - served cells not found. Responding node RAN name: name.", er.Error()) } func TestCloseOnClosedPoolFailure(t *testing.T) { readerPool = nil instanceMock := initSdlInstanceMock(namespace, 1) - w1 := GetRNibReader() + w1 := &rNibReaderInstance{} + _, err := w1.GetNodeb("") + if err == nil{ + t.Errorf("#rNibReader_test.TestCloseOnClosedPoolFailure - failed to validate key parameter") + } readerPool.Put(w1) available, created := readerPool.Stats() assert.Equal(t, 1, available, "number of available objects in the readerPool should be 1") @@ -338,10 +342,13 @@ func TestCloseOnClosedPoolFailure(t *testing.T) { func TestCloseFailure(t *testing.T) { readerPool = nil instanceMock := initSdlInstanceMock(namespace, 2) - w1 := GetRNibReader() - readerPool.Put(w1) + w1 := &rNibReaderInstance{} + _, err := w1.GetNodeb("") + if err == nil{ + t.Errorf("#rNibReader_test.TestCloseFailure - failed to validate key parameter") + } available, created := readerPool.Stats() - assert.Equal(t, 1, available, "number of available objects in the readerPool should be 1") + assert.Equal(t, 1, available, "number of available objects in the readerPool should be 2") assert.Equal(t, 1, created, "number of created objects in the readerPool should be 1") e := errors.New("expected error") instanceMock.On("Close").Return(e) @@ -354,29 +361,29 @@ func TestCloseFailure(t *testing.T) { func TestGetListGnbIdsUnmarshalFailure(t *testing.T) { readerPool = nil sdlInstanceMock := initSdlInstanceMock(namespace, 1) - w := GetRNibReader() + w := &rNibReaderInstance{} var e error sdlInstanceMock.On("GetMembers", entities.Node_GNB.String()).Return([]string{"data"}, e) ids, er := w.GetListGnbIds() assert.NotNil(t, er) assert.Nil(t, ids) - assert.Equal(t, 2, er.GetCode()) - assert.Equal(t, "2 INTERNAL_ERROR - proto: can't skip unknown wire type 4", er.Error()) + assert.IsType(t, &common.InternalError{}, er) + assert.Equal(t, "proto: can't skip unknown wire type 4", er.Error()) } func TestGetListGnbIdsSdlgoFailure(t *testing.T) { errMsg := "expected Sdlgo error" - errMsgExpected := "2 INTERNAL_ERROR - expected Sdlgo error" + errMsgExpected := "expected Sdlgo error" readerPool = nil sdlInstanceMock := initSdlInstanceMock(namespace, 1) - w := GetRNibReader() + w := &rNibReaderInstance{} e := errors.New(errMsg) var data []string sdlInstanceMock.On("GetMembers", entities.Node_GNB.String()).Return(data, e) ids, er := w.GetListGnbIds() assert.NotNil(t, er) assert.Nil(t, ids) - assert.Equal(t, 2, er.GetCode()) + assert.IsType(t, &common.InternalError{}, er) assert.EqualValues(t, errMsgExpected, er.Error()) } @@ -384,7 +391,7 @@ func TestGetListNodesIdsGnbSdlgoFailure(t *testing.T) { readerPool = nil sdlInstanceMock := initSdlInstanceMock(namespace, 1) - w := GetRNibReader() + w := &rNibReaderInstance{} name := "name" plmnId := "02f829" @@ -398,7 +405,7 @@ func TestGetListNodesIdsGnbSdlgoFailure(t *testing.T) { sdlInstanceMock.On("GetMembers", entities.Node_ENB.String()).Return([]string{string(data)}, nilError) errMsg := "expected Sdlgo error" - errMsgExpected := "2 INTERNAL_ERROR - expected Sdlgo error" + errMsgExpected := "expected Sdlgo error" expectedError := errors.New(errMsg) var nilData []string sdlInstanceMock.On("GetMembers", entities.Node_GNB.String()).Return(nilData, expectedError) @@ -406,7 +413,7 @@ func TestGetListNodesIdsGnbSdlgoFailure(t *testing.T) { ids, er := w.GetListNodebIds() assert.NotNil(t, er) assert.Nil(t, ids) - assert.Equal(t, 2, er.GetCode()) + assert.IsType(t, &common.InternalError{}, er) assert.EqualValues(t, errMsgExpected, er.Error()) } @@ -414,7 +421,7 @@ func TestGetListNodesIdsEnbSdlgoFailure(t *testing.T) { readerPool = nil sdlInstanceMock := initSdlInstanceMock(namespace, 1) - w := GetRNibReader() + w := &rNibReaderInstance{} name := "name" plmnId := "02f829" @@ -428,7 +435,7 @@ func TestGetListNodesIdsEnbSdlgoFailure(t *testing.T) { sdlInstanceMock.On("GetMembers", entities.Node_GNB.String()).Return([]string{string(data)}, nilError) errMsg := "expected Sdlgo error" - errMsgExpected := "2 INTERNAL_ERROR - expected Sdlgo error" + errMsgExpected := "expected Sdlgo error" expectedError := errors.New(errMsg) var nilData []string sdlInstanceMock.On("GetMembers", entities.Node_ENB.String()).Return(nilData, expectedError) @@ -436,7 +443,7 @@ func TestGetListNodesIdsEnbSdlgoFailure(t *testing.T) { ids, er := w.GetListNodebIds() assert.NotNil(t, er) assert.Nil(t, ids) - assert.Equal(t, 2, er.GetCode()) + assert.IsType(t, &common.InternalError{}, er) assert.EqualValues(t, errMsgExpected, er.Error()) } @@ -444,7 +451,7 @@ func TestGetListNodesIdsSuccess(t *testing.T) { readerPool = nil sdlInstanceMock := initSdlInstanceMock(namespace, 1) - w := GetRNibReader() + w := &rNibReaderInstance{} var nilError error name := "name" @@ -485,14 +492,14 @@ func TestGetListNodesIdsSuccess(t *testing.T) { func TestGetListEnbIdsUnmarshalFailure(t *testing.T) { readerPool = nil sdlInstanceMock := initSdlInstanceMock(namespace, 1) - w := GetRNibReader() + w := &rNibReaderInstance{} var e error sdlInstanceMock.On("GetMembers", entities.Node_ENB.String()).Return([]string{"data"}, e) ids, er := w.GetListEnbIds() assert.NotNil(t, er) assert.Nil(t, ids) - assert.Equal(t, 2, er.GetCode()) - assert.Equal(t, "2 INTERNAL_ERROR - proto: can't skip unknown wire type 4", er.Error()) + assert.IsType(t, &common.InternalError{}, er) + assert.Equal(t, "proto: can't skip unknown wire type 4", er.Error()) } func TestGetListEnbIdsOneId(t *testing.T) { @@ -501,7 +508,7 @@ func TestGetListEnbIdsOneId(t *testing.T) { nbId := "4a952a0a" readerPool = nil sdlInstanceMock := initSdlInstanceMock(namespace, 1) - w := GetRNibReader() + w := &rNibReaderInstance{} nbIdentity := &entities.NbIdentity{InventoryName: name, GlobalNbId: &entities.GlobalNbId{PlmnId: plmnId, NbId: nbId}} var e error data, err := proto.Marshal(nbIdentity) @@ -511,21 +518,21 @@ func TestGetListEnbIdsOneId(t *testing.T) { sdlInstanceMock.On("GetMembers", entities.Node_ENB.String()).Return([]string{string(data)}, e) ids, er := w.GetListEnbIds() assert.Nil(t, er) - assert.Len(t, *ids, 1) - assert.Equal(t, (*ids)[0].GetInventoryName(), name) - assert.Equal(t, (*ids)[0].GetGlobalNbId().GetPlmnId(), nbIdentity.GetGlobalNbId().GetPlmnId()) - assert.Equal(t, (*ids)[0].GetGlobalNbId().GetNbId(), nbIdentity.GetGlobalNbId().GetNbId()) + assert.Len(t, ids, 1) + assert.Equal(t, (ids)[0].GetInventoryName(), name) + assert.Equal(t, (ids)[0].GetGlobalNbId().GetPlmnId(), nbIdentity.GetGlobalNbId().GetPlmnId()) + assert.Equal(t, (ids)[0].GetGlobalNbId().GetNbId(), nbIdentity.GetGlobalNbId().GetNbId()) } func TestGetListEnbIdsNoIds(t *testing.T) { readerPool = nil sdlInstanceMock := initSdlInstanceMock(namespace, 1) - w := GetRNibReader() + w := &rNibReaderInstance{} var e error sdlInstanceMock.On("GetMembers", entities.Node_ENB.String()).Return([]string{}, e) ids, er := w.GetListEnbIds() assert.Nil(t, er) - assert.Len(t, *ids, 0) + assert.Len(t, ids, 0) } func TestGetListEnbIds(t *testing.T) { @@ -535,7 +542,7 @@ func TestGetListEnbIds(t *testing.T) { listSize := 3 readerPool = nil sdlInstanceMock := initSdlInstanceMock(namespace, 1) - w := GetRNibReader() + w := &rNibReaderInstance{} idsData := make([]string, listSize) idsEntities := make([]*entities.NbIdentity, listSize) for i := 0; i < listSize; i++ { @@ -551,8 +558,8 @@ func TestGetListEnbIds(t *testing.T) { sdlInstanceMock.On("GetMembers", entities.Node_ENB.String()).Return(idsData, e) ids, er := w.GetListEnbIds() assert.Nil(t, er) - assert.Len(t, *ids, listSize) - for i, id := range *ids { + assert.Len(t, ids, listSize) + for i, id := range ids { assert.Equal(t, id.GetInventoryName(), name) assert.Equal(t, id.GetGlobalNbId().GetPlmnId(), idsEntities[i].GetGlobalNbId().GetPlmnId()) assert.Equal(t, id.GetGlobalNbId().GetNbId(), idsEntities[i].GetGlobalNbId().GetNbId()) @@ -565,7 +572,7 @@ func TestGetListGnbIdsOneId(t *testing.T) { nbId := "4a952a0a" readerPool = nil sdlInstanceMock := initSdlInstanceMock(namespace, 1) - w := GetRNibReader() + w := &rNibReaderInstance{} nbIdentity := &entities.NbIdentity{InventoryName: name, GlobalNbId: &entities.GlobalNbId{PlmnId: plmnId, NbId: nbId}} var e error data, err := proto.Marshal(nbIdentity) @@ -575,21 +582,21 @@ func TestGetListGnbIdsOneId(t *testing.T) { sdlInstanceMock.On("GetMembers", entities.Node_GNB.String()).Return([]string{string(data)}, e) ids, er := w.GetListGnbIds() assert.Nil(t, er) - assert.Len(t, *ids, 1) - assert.Equal(t, (*ids)[0].GetInventoryName(), name) - assert.Equal(t, (*ids)[0].GetGlobalNbId().GetPlmnId(), nbIdentity.GetGlobalNbId().GetPlmnId()) - assert.Equal(t, (*ids)[0].GetGlobalNbId().GetNbId(), nbIdentity.GetGlobalNbId().GetNbId()) + assert.Len(t, ids, 1) + assert.Equal(t, (ids)[0].GetInventoryName(), name) + assert.Equal(t, (ids)[0].GetGlobalNbId().GetPlmnId(), nbIdentity.GetGlobalNbId().GetPlmnId()) + assert.Equal(t, (ids)[0].GetGlobalNbId().GetNbId(), nbIdentity.GetGlobalNbId().GetNbId()) } func TestGetListGnbIdsNoIds(t *testing.T) { readerPool = nil sdlInstanceMock := initSdlInstanceMock(namespace, 1) - w := GetRNibReader() + w := &rNibReaderInstance{} var e error sdlInstanceMock.On("GetMembers", entities.Node_GNB.String()).Return([]string{}, e) ids, er := w.GetListGnbIds() assert.Nil(t, er) - assert.Len(t, *ids, 0) + assert.Len(t, ids, 0) } func TestGetListGnbIds(t *testing.T) { @@ -599,7 +606,7 @@ func TestGetListGnbIds(t *testing.T) { listSize := 3 readerPool = nil sdlInstanceMock := initSdlInstanceMock(namespace, 1) - w := GetRNibReader() + w := &rNibReaderInstance{} idsData := make([]string, listSize) idsEntities := make([]*entities.NbIdentity, listSize) for i := 0; i < listSize; i++ { @@ -615,8 +622,8 @@ func TestGetListGnbIds(t *testing.T) { sdlInstanceMock.On("GetMembers", entities.Node_GNB.String()).Return(idsData, e) ids, er := w.GetListGnbIds() assert.Nil(t, er) - assert.Len(t, *ids, listSize) - for i, id := range *ids { + assert.Len(t, ids, listSize) + for i, id := range ids { assert.Equal(t, id.GetInventoryName(), name) assert.Equal(t, id.GetGlobalNbId().GetPlmnId(), idsEntities[i].GetGlobalNbId().GetPlmnId()) assert.Equal(t, id.GetGlobalNbId().GetNbId(), idsEntities[i].GetGlobalNbId().GetNbId()) @@ -625,24 +632,24 @@ func TestGetListGnbIds(t *testing.T) { func TestGetListEnbIdsSdlgoFailure(t *testing.T) { errMsg := "expected Sdlgo error" - errMsgExpected := "2 INTERNAL_ERROR - expected Sdlgo error" + errMsgExpected := "expected Sdlgo error" readerPool = nil sdlInstanceMock := initSdlInstanceMock(namespace, 1) - w := GetRNibReader() + w := &rNibReaderInstance{} e := errors.New(errMsg) var data []string sdlInstanceMock.On("GetMembers", entities.Node_ENB.String()).Return(data, e) ids, er := w.GetListEnbIds() assert.NotNil(t, er) assert.Nil(t, ids) - assert.Equal(t, 2, er.GetCode()) + assert.IsType(t, &common.InternalError{}, er) assert.EqualValues(t, errMsgExpected, er.Error()) } func TestGetCountGnbListOneId(t *testing.T) { readerPool = nil sdlInstanceMock := initSdlInstanceMock(namespace, 1) - w := GetRNibReader() + w := &rNibReaderInstance{} var e error sdlInstanceMock.On("GroupSize", entities.Node_GNB.String()).Return(1, e) count, er := w.GetCountGnbList() @@ -653,7 +660,7 @@ func TestGetCountGnbListOneId(t *testing.T) { func TestGetCountGnbList(t *testing.T) { readerPool = nil sdlInstanceMock := initSdlInstanceMock(namespace, 1) - w := GetRNibReader() + w := &rNibReaderInstance{} var e error sdlInstanceMock.On("GroupSize", entities.Node_GNB.String()).Return(3, e) count, er := w.GetCountGnbList() @@ -663,17 +670,17 @@ func TestGetCountGnbList(t *testing.T) { func TestGetCountGnbListSdlgoFailure(t *testing.T) { errMsg := "expected Sdlgo error" - errMsgExpected := "2 INTERNAL_ERROR - expected Sdlgo error" + errMsgExpected := "expected Sdlgo error" readerPool = nil sdlInstanceMock := initSdlInstanceMock(namespace, 1) - w := GetRNibReader() + w := &rNibReaderInstance{} e := errors.New(errMsg) var count int sdlInstanceMock.On("GroupSize", entities.Node_GNB.String()).Return(count, e) count, er := w.GetCountGnbList() assert.NotNil(t, er) assert.Equal(t, 0, count) - assert.Equal(t, 2, er.GetCode()) + assert.IsType(t, &common.InternalError{}, er) assert.EqualValues(t, errMsgExpected, er.Error()) } @@ -682,7 +689,7 @@ func TestGetCell(t *testing.T) { var pci uint32 = 10 readerPool = nil sdlInstanceMock := initSdlInstanceMock(namespace, 1) - w := GetRNibReader() + w := &rNibReaderInstance{} cellEntity := entities.Cell{Type: entities.Cell_LTE_CELL, Cell: &entities.Cell_ServedCellInfo{ServedCellInfo: &entities.ServedCellInfo{Pci: pci}}} cellData, err := proto.Marshal(&cellEntity) if err != nil { @@ -708,7 +715,7 @@ func TestGetCellNotFoundFailure(t *testing.T) { var pci uint32 readerPool = nil sdlInstanceMock := initSdlInstanceMock(namespace, 1) - w := GetRNibReader() + w := &rNibReaderInstance{} var e error var ret map[string]interface{} key, rNibErr := common.ValidateAndBuildCellNamePciKey(name, pci) @@ -719,8 +726,8 @@ func TestGetCellNotFoundFailure(t *testing.T) { cell, er := w.GetCell(name, pci) assert.NotNil(t, er) assert.Nil(t, cell) - assert.Equal(t, 1, er.GetCode()) - assert.EqualValues(t, "1 RESOURCE_NOT_FOUND - #rNibReader.getCellByKey - cell not found, key: PCI:name:00", er.Error()) + assert.IsType(t, &common.ResourceNotFoundError{}, er) + assert.EqualValues(t, "#rNibReader.getByKeyAndUnmarshal - entity of type *entities.Cell not found. Key: PCI:name:00", er.Error()) } func TestGetCellUnmarshalFailure(t *testing.T) { @@ -728,7 +735,7 @@ func TestGetCellUnmarshalFailure(t *testing.T) { var pci uint32 readerPool = nil sdlInstanceMock := initSdlInstanceMock(namespace, 1) - w := GetRNibReader() + w := &rNibReaderInstance{} var e error ret := make(map[string]interface{}, 1) key, rNibErr := common.ValidateAndBuildCellNamePciKey(name, pci) @@ -740,18 +747,18 @@ func TestGetCellUnmarshalFailure(t *testing.T) { cell, er := w.GetCell(name, pci) assert.NotNil(t, er) assert.Nil(t, cell) - assert.Equal(t, 2, er.GetCode()) - assert.EqualValues(t, "2 INTERNAL_ERROR - proto: can't skip unknown wire type 4", er.Error()) + assert.IsType(t, &common.InternalError{}, er) + assert.EqualValues(t, "proto: can't skip unknown wire type 4", er.Error()) } func TestGetCellSdlgoFailure(t *testing.T) { name := "name" var pci uint32 errMsg := "expected Sdlgo error" - errMsgExpected := "2 INTERNAL_ERROR - expected Sdlgo error" + errMsgExpected := "expected Sdlgo error" readerPool = nil sdlInstanceMock := initSdlInstanceMock(namespace, 1) - w := GetRNibReader() + w := &rNibReaderInstance{} e := errors.New(errMsg) var ret map[string]interface{} key, rNibErr := common.ValidateAndBuildCellNamePciKey(name, pci) @@ -762,14 +769,14 @@ func TestGetCellSdlgoFailure(t *testing.T) { cell, er := w.GetCell(name, pci) assert.NotNil(t, er) assert.Nil(t, cell) - assert.Equal(t, 2, er.GetCode()) + assert.IsType(t, &common.InternalError{}, er) assert.EqualValues(t, errMsgExpected, er.Error()) } func TestGetNodebById(t *testing.T) { readerPool = nil sdlInstanceMock := initSdlInstanceMock(namespace, 1) - w := GetRNibReader() + w := &rNibReaderInstance{} nb := entities.NodebInfo{NodeType: entities.Node_ENB} nb.ConnectionStatus = 1 nb.Ip = "localhost" @@ -807,7 +814,7 @@ func TestGetNodebByIdNotFoundFailureEnb(t *testing.T) { nbId := "4a952a0a" readerPool = nil sdlInstanceMock := initSdlInstanceMock(namespace, 1) - w := GetRNibReader() + w := &rNibReaderInstance{} var e error key, rNibErr := common.ValidateAndBuildNodeBIdKey(entities.Node_ENB.String(), plmnId, nbId) if rNibErr != nil { @@ -819,8 +826,8 @@ func TestGetNodebByIdNotFoundFailureEnb(t *testing.T) { getNb, er := w.GetNodebByGlobalNbId(entities.Node_ENB, globalNbId) assert.NotNil(t, er) assert.Nil(t, getNb) - assert.Equal(t, 1, er.GetCode()) - assert.EqualValues(t, "1 RESOURCE_NOT_FOUND - #rNibReader.getNodeb - responding node not found. Key: ENB:02f829:4a952a0a", er.Error()) + assert.IsType(t, &common.ResourceNotFoundError{}, er) + assert.EqualValues(t, "#rNibReader.getByKeyAndUnmarshal - entity of type *entities.NodebInfo not found. Key: ENB:02f829:4a952a0a", er.Error()) } func TestGetNodebByIdNotFoundFailureGnb(t *testing.T) { @@ -828,7 +835,7 @@ func TestGetNodebByIdNotFoundFailureGnb(t *testing.T) { nbId := "4a952a0a" readerPool = nil sdlInstanceMock := initSdlInstanceMock(namespace, 1) - w := GetRNibReader() + w := &rNibReaderInstance{} var e error key, rNibErr := common.ValidateAndBuildNodeBIdKey(entities.Node_GNB.String(), plmnId, nbId) if rNibErr != nil { @@ -840,8 +847,8 @@ func TestGetNodebByIdNotFoundFailureGnb(t *testing.T) { getNb, er := w.GetNodebByGlobalNbId(entities.Node_GNB, globalNbId) assert.NotNil(t, er) assert.Nil(t, getNb) - assert.Equal(t, 1, er.GetCode()) - assert.EqualValues(t, "1 RESOURCE_NOT_FOUND - #rNibReader.getNodeb - responding node not found. Key: GNB:02f829:4a952a0a", er.Error()) + assert.IsType(t, &common.ResourceNotFoundError{}, er) + assert.EqualValues(t, "#rNibReader.getByKeyAndUnmarshal - entity of type *entities.NodebInfo not found. Key: GNB:02f829:4a952a0a", er.Error()) } func TestGetNodeByIdUnmarshalFailure(t *testing.T) { @@ -849,7 +856,7 @@ func TestGetNodeByIdUnmarshalFailure(t *testing.T) { nbId := "4a952a0a" readerPool = nil sdlInstanceMock := initSdlInstanceMock(namespace, 1) - w := GetRNibReader() + w := &rNibReaderInstance{} key, rNibErr := common.ValidateAndBuildNodeBIdKey(entities.Node_ENB.String(), plmnId, nbId) if rNibErr != nil { t.Errorf("Failed to validate nodeb identity, plmnId: %s, nbId: %s", plmnId, nbId) @@ -862,18 +869,18 @@ func TestGetNodeByIdUnmarshalFailure(t *testing.T) { getNb, er := w.GetNodebByGlobalNbId(entities.Node_ENB, globalNbId) assert.NotNil(t, er) assert.Nil(t, getNb) - assert.Equal(t, 2, er.GetCode()) - assert.EqualValues(t, "2 INTERNAL_ERROR - proto: can't skip unknown wire type 4", er.Error()) + assert.IsType(t, &common.InternalError{}, er) + assert.EqualValues(t, "proto: can't skip unknown wire type 4", er.Error()) } func TestGetNodeByIdSdlgoFailure(t *testing.T) { plmnId := "02f829" nbId := "4a952a0a" errMsg := "expected Sdlgo error" - errMsgExpected := "2 INTERNAL_ERROR - expected Sdlgo error" + errMsgExpected := "expected Sdlgo error" readerPool = nil sdlInstanceMock := initSdlInstanceMock(namespace, 1) - w := GetRNibReader() + w := &rNibReaderInstance{} key, rNibErr := common.ValidateAndBuildNodeBIdKey(entities.Node_GNB.String(), plmnId, nbId) if rNibErr != nil { t.Errorf("Failed to validate nodeb identity, plmnId: %s, nbId: %s", plmnId, nbId) @@ -885,7 +892,7 @@ func TestGetNodeByIdSdlgoFailure(t *testing.T) { getNb, er := w.GetNodebByGlobalNbId(entities.Node_GNB, globalNbId) assert.NotNil(t, er) assert.Nil(t, getNb) - assert.Equal(t, 2, er.GetCode()) + assert.IsType(t, &common.InternalError{}, er) assert.EqualValues(t, errMsgExpected, er.Error()) } @@ -894,7 +901,7 @@ func TestGetCellById(t *testing.T) { var pci uint32 = 10 readerPool = nil sdlInstanceMock := initSdlInstanceMock(namespace, 1) - w := GetRNibReader() + w := &rNibReaderInstance{} cellEntity := entities.Cell{Type: entities.Cell_LTE_CELL, Cell: &entities.Cell_ServedCellInfo{ServedCellInfo: &entities.ServedCellInfo{Pci: pci}}} cellData, err := proto.Marshal(&cellEntity) if err != nil { @@ -919,7 +926,7 @@ func TestGetCellByIdNotFoundFailureEnb(t *testing.T) { cellId := "bbbb" readerPool = nil sdlInstanceMock := initSdlInstanceMock(namespace, 1) - w := GetRNibReader() + w := &rNibReaderInstance{} var e error var ret map[string]interface{} key, rNibErr := common.ValidateAndBuildCellIdKey(cellId) @@ -930,15 +937,15 @@ func TestGetCellByIdNotFoundFailureEnb(t *testing.T) { cell, er := w.GetCellById(entities.Cell_LTE_CELL, cellId) assert.NotNil(t, er) assert.Nil(t, cell) - assert.Equal(t, 1, er.GetCode()) - assert.EqualValues(t, "1 RESOURCE_NOT_FOUND - #rNibReader.getCellByKey - cell not found, key: CELL:bbbb", er.Error()) + assert.IsType(t, &common.ResourceNotFoundError{}, er) + assert.EqualValues(t, "#rNibReader.getByKeyAndUnmarshal - entity of type *entities.Cell not found. Key: CELL:bbbb", er.Error()) } func TestGetCellByIdNotFoundFailureGnb(t *testing.T) { cellId := "bbbb" readerPool = nil sdlInstanceMock := initSdlInstanceMock(namespace, 1) - w := GetRNibReader() + w := &rNibReaderInstance{} var e error var ret map[string]interface{} key, rNibErr := common.ValidateAndBuildNrCellIdKey(cellId) @@ -949,51 +956,51 @@ func TestGetCellByIdNotFoundFailureGnb(t *testing.T) { cell, er := w.GetCellById(entities.Cell_NR_CELL, cellId) assert.NotNil(t, er) assert.Nil(t, cell) - assert.Equal(t, 1, er.GetCode()) - assert.EqualValues(t, "1 RESOURCE_NOT_FOUND - #rNibReader.getCellByKey - cell not found, key: NRCELL:bbbb", er.Error()) + assert.IsType(t, &common.ResourceNotFoundError{}, er) + assert.EqualValues(t, "#rNibReader.getByKeyAndUnmarshal - entity of type *entities.Cell not found. Key: NRCELL:bbbb", er.Error()) } func TestGetCellByIdTypeValidationFailure(t *testing.T) { cellId := "dddd" readerPool = nil initSdlInstanceMock(namespace, 1) - w := GetRNibReader() + w := &rNibReaderInstance{} cell, er := w.GetCellById(5, cellId) assert.NotNil(t, er) assert.Nil(t, cell) - assert.Equal(t, 3, er.GetCode()) - assert.EqualValues(t, "3 VALIDATION_ERROR - #rNibReader.GetCellById - invalid cell type: 5", er.Error()) + assert.IsType(t, &common.ValidationError{}, er) + assert.EqualValues(t, "#rNibReader.GetCellById - invalid cell type: 5", er.Error()) } func TestGetCellByIdValidationFailureGnb(t *testing.T) { cellId := "" readerPool = nil initSdlInstanceMock(namespace, 1) - w := GetRNibReader() + w := &rNibReaderInstance{} cell, er := w.GetCellById(entities.Cell_NR_CELL, cellId) assert.NotNil(t, er) assert.Nil(t, cell) - assert.Equal(t, 3, er.GetCode()) - assert.EqualValues(t, "3 VALIDATION_ERROR - #utils.ValidateAndBuildNrCellIdKey - an empty cell id received", er.Error()) + assert.IsType(t, &common.ValidationError{}, er) + assert.EqualValues(t, "#utils.ValidateAndBuildNrCellIdKey - an empty cell id received", er.Error()) } func TestGetCellByIdValidationFailureEnb(t *testing.T) { cellId := "" readerPool = nil initSdlInstanceMock(namespace, 1) - w := GetRNibReader() + w := &rNibReaderInstance{} cell, er := w.GetCellById(entities.Cell_LTE_CELL, cellId) assert.NotNil(t, er) assert.Nil(t, cell) - assert.Equal(t, 3, er.GetCode()) - assert.EqualValues(t, "3 VALIDATION_ERROR - #utils.ValidateAndBuildCellIdKey - an empty cell id received", er.Error()) + assert.IsType(t, &common.ValidationError{}, er) + assert.EqualValues(t, "#utils.ValidateAndBuildCellIdKey - an empty cell id received", er.Error()) } func TestGetRanLoadInformation(t *testing.T) { name := "name" readerPool = nil sdlInstanceMock := initSdlInstanceMock(namespace, 1) - w := GetRNibReader() + w := &rNibReaderInstance{} loadInfo := generateRanLoadInformation() var e error data, err := proto.Marshal(loadInfo) @@ -1020,11 +1027,23 @@ func TestGetRanLoadInformation(t *testing.T) { assert.EqualValues(t, expected, actual) } +func TestGetRanLoadInformationValidationFailure(t *testing.T) { + name := "" + readerPool = nil + initSdlInstanceMock(namespace, 1) + w := &rNibReaderInstance{} + getNb, er := w.GetRanLoadInformation(name) + assert.NotNil(t, er) + assert.Nil(t, getNb) + assert.IsType(t, &common.ValidationError{}, er) + assert.EqualValues(t, "#utils.ValidateAndBuildRanLoadInformationKey - an empty inventory name received", er.Error()) +} + func TestGetRanLoadInformationNotFoundFailure(t *testing.T) { name := "name" readerPool = nil sdlInstanceMock := initSdlInstanceMock(namespace, 1) - w := GetRNibReader() + w := &rNibReaderInstance{} var e error var ret map[string]interface{} redisKey, rNibErr := common.ValidateAndBuildRanLoadInformationKey(name) @@ -1035,15 +1054,15 @@ func TestGetRanLoadInformationNotFoundFailure(t *testing.T) { getNb, er := w.GetRanLoadInformation(name) assert.NotNil(t, er) assert.Nil(t, getNb) - assert.Equal(t, 1, er.GetCode()) - assert.EqualValues(t, "1 RESOURCE_NOT_FOUND - #rNibReader.getByKeyAndUnmarshal - entity of type *entities.RanLoadInformation not found. Key: LOAD:name", er.Error()) + assert.IsType(t, &common.ResourceNotFoundError{}, er) + assert.EqualValues(t, "#rNibReader.getByKeyAndUnmarshal - entity of type *entities.RanLoadInformation not found. Key: LOAD:name", er.Error()) } func TestGetRanLoadInformationUnmarshalFailure(t *testing.T) { name := "name" readerPool = nil sdlInstanceMock := initSdlInstanceMock(namespace, 1) - w := GetRNibReader() + w := &rNibReaderInstance{} var e error ret := make(map[string]interface{}, 1) redisKey, rNibErr := common.ValidateAndBuildRanLoadInformationKey(name) @@ -1055,17 +1074,17 @@ func TestGetRanLoadInformationUnmarshalFailure(t *testing.T) { getNb, er := w.GetRanLoadInformation(name) assert.NotNil(t, er) assert.Nil(t, getNb) - assert.Equal(t, 2, er.GetCode()) - assert.EqualValues(t, "2 INTERNAL_ERROR - proto: can't skip unknown wire type 4", er.Error()) + assert.IsType(t, &common.InternalError{}, er) + assert.EqualValues(t, "proto: can't skip unknown wire type 4", er.Error()) } func TestGetRanLoadInformationSdlgoFailure(t *testing.T) { name := "name" errMsg := "expected Sdlgo error" - errMsgExpected := "2 INTERNAL_ERROR - expected Sdlgo error" + errMsgExpected := "expected Sdlgo error" readerPool = nil sdlInstanceMock := initSdlInstanceMock(namespace, 1) - w := GetRNibReader() + w := &rNibReaderInstance{} e := errors.New(errMsg) var ret map[string]interface{} redisKey, rNibErr := common.ValidateAndBuildRanLoadInformationKey(name) @@ -1076,7 +1095,7 @@ func TestGetRanLoadInformationSdlgoFailure(t *testing.T) { getNb, er := w.GetRanLoadInformation(name) assert.NotNil(t, er) assert.Nil(t, getNb) - assert.Equal(t, 2, er.GetCode()) + assert.IsType(t, &common.InternalError{}, er) assert.EqualValues(t, errMsgExpected, er.Error()) } @@ -1123,7 +1142,7 @@ func generateCellLoadInformation() *entities.CellLoadInformation { } compInformationItem := &entities.CompInformationItem{ - CompHypothesisSets: []*entities.CompHypothesisSet{&entities.CompHypothesisSet{CellId: "789", CompHypothesis:"xxx"}}, + CompHypothesisSets: []*entities.CompHypothesisSet{{CellId: "789", CompHypothesis: "xxx"}}, BenefitMetric:50, } @@ -1154,11 +1173,11 @@ func generateRanLoadInformation() *entities.RanLoadInformation { } //integration tests - +// //func TestGetEnbInteg(t *testing.T){ // name := "nameEnb1" // Init("namespace", 1) -// w := GetRNibReader() +// w := &rNibReaderInstance{} // nb, err := w.GetNodeb(name) // if err != nil{ // fmt.Println(err) @@ -1170,7 +1189,7 @@ func generateRanLoadInformation() *entities.RanLoadInformation { //func TestGetEnbCellsInteg(t *testing.T){ // name := "nameEnb1" // Init("namespace", 1) -// w := GetRNibReader() +// w := &rNibReaderInstance{} // cells, err := w.GetCellList(name) // if err != nil{ // fmt.Println(err) @@ -1184,7 +1203,7 @@ func generateRanLoadInformation() *entities.RanLoadInformation { //func TestGetGnbInteg(t *testing.T){ // name := "nameGnb1" // Init("namespace", 1) -// w := GetRNibReader() +// w := &rNibReaderInstance{} // nb, err := w.GetNodeb(name) // if err != nil{ // fmt.Println(err) @@ -1196,7 +1215,7 @@ func generateRanLoadInformation() *entities.RanLoadInformation { //func TestGetGnbCellsInteg(t *testing.T){ // name := "nameGnb1" // Init("namespace", 1) -// w := GetRNibReader() +// w := &rNibReaderInstance{} // cells, err := w.GetCellList(name) // if err != nil{ // fmt.Println(err) @@ -1209,12 +1228,12 @@ func generateRanLoadInformation() *entities.RanLoadInformation { // //func TestGetListEnbIdsInteg(t *testing.T) { // Init("namespace", 1) -// w := GetRNibReader() +// w := &rNibReaderInstance{} // ids, err := w.GetListEnbIds() // if err != nil{ // fmt.Println(err) // } else { -// for _, id := range *ids{ +// for _, id := range ids{ // fmt.Printf("#TestGetListEnbIdsInteg - ENB ID: %s\n", id) // } // } @@ -1222,12 +1241,12 @@ func generateRanLoadInformation() *entities.RanLoadInformation { // //func TestGetListGnbIdsInteg(t *testing.T) { // Init("namespace", 1) -// w := GetRNibReader() +// w := &rNibReaderInstance{} // ids, err := w.GetListGnbIds() // if err != nil{ // fmt.Println(err) // } else { -// for _, id := range *ids{ +// for _, id := range ids{ // fmt.Printf("#TestGetListGnbIdsInteg - GNB ID: %s\n", id) // } // } @@ -1235,7 +1254,7 @@ func generateRanLoadInformation() *entities.RanLoadInformation { // //func TestGetCountGnbListInteg(t *testing.T) { // Init("namespace", 1) -// w := GetRNibReader() +// w := &rNibReaderInstance{} // count, err := w.GetCountGnbList() // if err != nil{ // fmt.Println(err) @@ -1248,7 +1267,7 @@ func generateRanLoadInformation() *entities.RanLoadInformation { // name := "nameGnb7" // pci := 0x0a // Init("namespace", 1) -// w := GetRNibReader() +// w := &rNibReaderInstance{} // cell, err := w.GetCell(name, uint32(pci)) // if err != nil{ // fmt.Println(err) @@ -1261,7 +1280,7 @@ func generateRanLoadInformation() *entities.RanLoadInformation { // name := "nameEnb1" // pci := 0x22 // Init("namespace", 1) -// w := GetRNibReader() +// w := &rNibReaderInstance{} // cell, err := w.GetCell(name, uint32(pci)) // if err != nil { // fmt.Println(err) @@ -1272,7 +1291,7 @@ func generateRanLoadInformation() *entities.RanLoadInformation { // //func TestGetEnbCellByIdInteg(t *testing.T){ // Init("namespace", 1) -// w := GetRNibReader() +// w := &rNibReaderInstance{} // cell, err := w.GetCellById(entities.Cell_NR_CELL, "45d") // if err != nil{ // fmt.Println(err) @@ -1283,7 +1302,7 @@ func generateRanLoadInformation() *entities.RanLoadInformation { // //func TestGetListNbIdsInteg(t *testing.T) { // Init("e2Manager", 1) -// w := GetRNibReader() +// w := &rNibReaderInstance{} // ids, err := w.GetListNodebIds() // if err != nil{ // fmt.Println(err) @@ -1296,10 +1315,10 @@ func generateRanLoadInformation() *entities.RanLoadInformation { // //func TestGetRanLoadInformationInteg(t *testing.T){ // Init("e2Manager", 1) -// w := GetRNibReader() +// w := &rNibReaderInstance{} // ranLoadInformation, err := w.GetRanLoadInformation("ran_integ") // if err != nil{ -// t.Errorf("#rNibReader_test.TestGetRanLoadInformationInteg - Failed to save RanLoadInformation entity. Error: %v", err) +// t.Errorf("#rNibReader_test.TestGetRanLoadInformationInteg - Failed to get RanLoadInformation entity. Error: %v", err) // } // assert.NotNil(t, ranLoadInformation) // fmt.Printf("#rNibReader_test.TestGetRanLoadInformationInteg - GNB ID: %s\n", ranLoadInformation) -- 2.16.6