Merge "Misc improvements in GO code"
diff --git a/dmaap-mediator-producer/internal/jobs/jobs_test.go b/dmaap-mediator-producer/internal/jobs/jobs_test.go
index b301c2f..6ca39b2 100644
--- a/dmaap-mediator-producer/internal/jobs/jobs_test.go
+++ b/dmaap-mediator-producer/internal/jobs/jobs_test.go
@@ -31,6 +31,7 @@
"time"
"github.com/stretchr/testify/require"
+ "oransc.org/nonrtric/dmaapmediatorproducer/internal/config"
)
const typeDefinition = `{"types": [{"id": "type1", "dmaapTopicUrl": "events/unauthenticated.SEC_FAULT_OUTPUT/dmaapmediatorproducer/type1"}]}`
@@ -51,11 +52,11 @@
t.Errorf("Unable to create temporary config file for types due to: %v", err)
}
types, err := handlerUnderTest.GetTypes()
- wantedType := TypeDefinition{
+ wantedType := config.TypeDefinition{
Id: "type1",
DmaapTopicURL: "events/unauthenticated.SEC_FAULT_OUTPUT/dmaapmediatorproducer/type1",
}
- wantedTypes := []TypeDefinition{wantedType}
+ wantedTypes := []config.TypeDefinition{wantedType}
assertions.EqualValues(wantedTypes, types)
assertions.Nil(err)
diff --git a/test/usecases/oruclosedlooprecovery/goversion/internal/config/config_test.go b/test/usecases/oruclosedlooprecovery/goversion/internal/config/config_test.go
index e278e60..a5b1624 100644
--- a/test/usecases/oruclosedlooprecovery/goversion/internal/config/config_test.go
+++ b/test/usecases/oruclosedlooprecovery/goversion/internal/config/config_test.go
@@ -23,7 +23,6 @@
import (
"bytes"
"os"
- "reflect"
"testing"
log "github.com/sirupsen/logrus"
@@ -31,6 +30,7 @@
)
func TestNew_envVarsSetConfigContainSetValues(t *testing.T) {
+ assertions := require.New(t)
os.Setenv("LOG_LEVEL", "Debug")
os.Setenv("CONSUMER_HOST", "consumerHost")
os.Setenv("CONSUMER_PORT", "8095")
@@ -54,9 +54,9 @@
SDNPassword: "pwd",
ORUToODUMapFile: "file",
}
- if got := New(); !reflect.DeepEqual(got, &wantConfig) {
- t.Errorf("New() = %v, want %v", got, &wantConfig)
- }
+
+ got := New()
+ assertions.Equal(&wantConfig, got)
}
func TestNew_faultyIntValueSetConfigContainDefaultValueAndWarnInLog(t *testing.T) {
@@ -80,9 +80,10 @@
SDNPassword: "Kp8bJ4SXszM0WXlhak3eHlcse2gAw84vaoGGmJvUy2U",
ORUToODUMapFile: "o-ru-to-o-du-map.csv",
}
- if got := New(); !reflect.DeepEqual(got, &wantConfig) {
- t.Errorf("New() = %v, want %v", got, &wantConfig)
- }
+
+ got := New()
+ assertions.Equal(&wantConfig, got)
+
logString := buf.String()
assertions.Contains(logString, "Invalid int value: wrong for variable: CONSUMER_PORT. Default value: 0 will be used")
}
@@ -108,12 +109,8 @@
SDNPassword: "Kp8bJ4SXszM0WXlhak3eHlcse2gAw84vaoGGmJvUy2U",
ORUToODUMapFile: "o-ru-to-o-du-map.csv",
}
- if got := New(); !reflect.DeepEqual(got, &wantConfig) {
- t.Errorf("New() = %v, want %v", got, &wantConfig)
- }
- if got := New(); !reflect.DeepEqual(got, &wantConfig) {
- t.Errorf("New() = %v, want %v", got, &wantConfig)
- }
+ got := New()
+ assertions.Equal(&wantConfig, got)
logString := buf.String()
assertions.Contains(logString, "Invalid log level: wrong. Log level will be Info!")
}
diff --git a/test/usecases/oruclosedlooprecovery/goversion/internal/linkfailure/linkfailurehandler.go b/test/usecases/oruclosedlooprecovery/goversion/internal/linkfailure/linkfailurehandler.go
index c21371a..558d0d7 100644
--- a/test/usecases/oruclosedlooprecovery/goversion/internal/linkfailure/linkfailurehandler.go
+++ b/test/usecases/oruclosedlooprecovery/goversion/internal/linkfailure/linkfailurehandler.go
@@ -32,7 +32,6 @@
)
type Configuration struct {
- ConsumerAddress string
InfoCoordAddress string
SDNRAddress string
SDNRUser string
@@ -40,18 +39,19 @@
}
const rawSdnrPath = "/rests/data/network-topology:network-topology/topology=topology-netconf/node=[O-DU-ID]/yang-ext:mount/o-ran-sc-du-hello-world:network-function/du-to-ru-connection=[O-RU-ID]"
-
const unlockMessage = `{"o-ran-sc-du-hello-world:du-to-ru-connection": [{"name":"[O-RU-ID]","administrative-state":"UNLOCKED"}]}`
type LinkFailureHandler struct {
lookupService repository.LookupService
config Configuration
+ client restclient.HTTPClient
}
-func NewLinkFailureHandler(ls repository.LookupService, conf Configuration) *LinkFailureHandler {
+func NewLinkFailureHandler(ls repository.LookupService, conf Configuration, client restclient.HTTPClient) *LinkFailureHandler {
return &LinkFailureHandler{
lookupService: ls,
config: conf,
+ client: client,
}
}
@@ -74,7 +74,7 @@
if oDuId, err := lfh.lookupService.GetODuID(oRuId); err == nil {
sdnrPath := getSdnrPath(oRuId, oDuId)
unlockMessage := lfh.getUnlockMessage(oRuId)
- if error := restclient.Put(lfh.config.SDNRAddress+sdnrPath, unlockMessage, lfh.config.SDNRUser, lfh.config.SDNRPassword); error == nil {
+ if error := restclient.Put(lfh.config.SDNRAddress+sdnrPath, unlockMessage, lfh.client, lfh.config.SDNRUser, lfh.config.SDNRPassword); error == nil {
log.Debugf("Sent unlock message for O-RU: %v to O-DU: %v.", oRuId, oDuId)
} else {
log.Warn(error)
diff --git a/test/usecases/oruclosedlooprecovery/goversion/internal/linkfailure/linkfailurehandler_test.go b/test/usecases/oruclosedlooprecovery/goversion/internal/linkfailure/linkfailurehandler_test.go
index 9653c99..a3df704 100644
--- a/test/usecases/oruclosedlooprecovery/goversion/internal/linkfailure/linkfailurehandler_test.go
+++ b/test/usecases/oruclosedlooprecovery/goversion/internal/linkfailure/linkfailurehandler_test.go
@@ -34,7 +34,6 @@
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
"oransc.org/usecase/oruclosedloop/internal/repository"
- "oransc.org/usecase/oruclosedloop/internal/restclient"
"oransc.org/usecase/oruclosedloop/internal/ves"
"oransc.org/usecase/oruclosedloop/mocks"
)
@@ -55,8 +54,6 @@
StatusCode: http.StatusOK,
}, nil)
- restclient.Client = &clientMock
-
lookupServiceMock := mocks.LookupService{}
lookupServiceMock.On("GetODuID", mock.Anything).Return("HCL-O-DU-1122", nil)
@@ -65,7 +62,7 @@
SDNRAddress: "http://localhost:9990",
SDNRUser: "admin",
SDNRPassword: "pwd",
- })
+ }, &clientMock)
responseRecorder := httptest.NewRecorder()
r := newRequest(http.MethodPost, "/", getFaultMessage("ERICSSON-O-RU-11220", "CRITICAL"), t)
@@ -122,7 +119,7 @@
lookupServiceMock.On("GetODuID", mock.Anything).Return("HCL-O-DU-1122", nil)
- handlerUnderTest := NewLinkFailureHandler(&lookupServiceMock, Configuration{})
+ handlerUnderTest := NewLinkFailureHandler(&lookupServiceMock, Configuration{}, nil)
responseRecorder := httptest.NewRecorder()
r := newRequest(http.MethodPost, "/", getFaultMessage("ERICSSON-O-RU-11220", "NORMAL"), t)
@@ -151,7 +148,7 @@
Id: "ERICSSON-O-RU-11220",
})
- handlerUnderTest := NewLinkFailureHandler(&lookupServiceMock, Configuration{})
+ handlerUnderTest := NewLinkFailureHandler(&lookupServiceMock, Configuration{}, nil)
responseRecorder := httptest.NewRecorder()
r := newRequest(http.MethodPost, "/", getFaultMessage("ERICSSON-O-RU-11220", "CRITICAL"), t)
diff --git a/test/usecases/oruclosedlooprecovery/goversion/internal/repository/csvhelp.go b/test/usecases/oruclosedlooprecovery/goversion/internal/repository/csvhelp.go
index ccaff8b..c5afda7 100644
--- a/test/usecases/oruclosedlooprecovery/goversion/internal/repository/csvhelp.go
+++ b/test/usecases/oruclosedlooprecovery/goversion/internal/repository/csvhelp.go
@@ -31,11 +31,11 @@
type CsvFileHelperImpl struct{}
-func NewCsvFileHelper() CsvFileHelperImpl {
+func NewCsvFileHelperImpl() CsvFileHelperImpl {
return CsvFileHelperImpl{}
}
-func (h *CsvFileHelperImpl) GetCsvFromFile(name string) ([][]string, error) {
+func (h CsvFileHelperImpl) GetCsvFromFile(name string) ([][]string, error) {
if csvFile, err := os.Open(name); err == nil {
defer csvFile.Close()
reader := csv.NewReader(csvFile)
diff --git a/test/usecases/oruclosedlooprecovery/goversion/internal/repository/csvhelp_test.go b/test/usecases/oruclosedlooprecovery/goversion/internal/repository/csvhelp_test.go
index dfd29cc..ddcc229 100644
--- a/test/usecases/oruclosedlooprecovery/goversion/internal/repository/csvhelp_test.go
+++ b/test/usecases/oruclosedlooprecovery/goversion/internal/repository/csvhelp_test.go
@@ -22,52 +22,46 @@
import (
"os"
- "reflect"
"testing"
+
+ "github.com/stretchr/testify/require"
)
func TestCsvFileHelperImpl_GetCsvFromFile(t *testing.T) {
+ assertions := require.New(t)
filePath := createTempCsvFile()
defer os.Remove(filePath)
type args struct {
name string
}
tests := []struct {
- name string
- fileHelper *CsvFileHelperImpl
- args args
- want [][]string
- wantErr bool
+ name string
+ args args
+ want [][]string
+ wantErrString string
}{
{
- name: "Read from file should return array of content",
- fileHelper: &CsvFileHelperImpl{},
+ name: "Read from file should return array of content",
args: args{
name: filePath,
},
- want: [][]string{{"O-RU-ID", "O-DU-ID"}},
- wantErr: false,
+ want: [][]string{{"O-RU-ID", "O-DU-ID"}},
},
{
- name: "File missing should return error",
- fileHelper: &CsvFileHelperImpl{},
+ name: "File missing should return error",
args: args{
name: "nofile.csv",
},
- want: nil,
- wantErr: true,
+ wantErrString: "open nofile.csv: no such file or directory",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
- h := &CsvFileHelperImpl{}
+ h := NewCsvFileHelperImpl()
got, err := h.GetCsvFromFile(tt.args.name)
- if (err != nil) != tt.wantErr {
- t.Errorf("CsvFileHelperImpl.GetCsvFromFile() error = %v, wantErr %v", err, tt.wantErr)
- return
- }
- if !reflect.DeepEqual(got, tt.want) {
- t.Errorf("CsvFileHelperImpl.GetCsvFromFile() = %v, want %v", got, tt.want)
+ assertions.Equal(tt.want, got)
+ if tt.wantErrString != "" {
+ assertions.Contains(err.Error(), tt.wantErrString)
}
})
}
diff --git a/test/usecases/oruclosedlooprecovery/goversion/internal/repository/lookupservice_test.go b/test/usecases/oruclosedlooprecovery/goversion/internal/repository/lookupservice_test.go
index 5819456..8cfdbdc 100644
--- a/test/usecases/oruclosedlooprecovery/goversion/internal/repository/lookupservice_test.go
+++ b/test/usecases/oruclosedlooprecovery/goversion/internal/repository/lookupservice_test.go
@@ -22,13 +22,23 @@
import (
"errors"
- "reflect"
"testing"
+ "github.com/stretchr/testify/require"
"oransc.org/usecase/oruclosedloop/mocks"
)
+func TestIdNotMappedError(t *testing.T) {
+ assertions := require.New(t)
+
+ actualError := IdNotMappedError{
+ Id: "1",
+ }
+ assertions.Equal("O-RU-ID: 1 not mapped.", actualError.Error())
+}
+
func TestNewLookupServiceImpl(t *testing.T) {
+ assertions := require.New(t)
mockCsvFileHelper := &mocks.CsvFileHelper{}
type args struct {
fileHelper CsvFileHelper
@@ -54,66 +64,61 @@
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
- if got := NewLookupServiceImpl(tt.args.fileHelper, tt.args.fileName); !reflect.DeepEqual(got, tt.want) {
- t.Errorf("NewLookupServiceImpl() = %v, want %v", got, tt.want)
- }
+ got := NewLookupServiceImpl(tt.args.fileHelper, tt.args.fileName)
+ assertions.Equal(tt.want, got)
})
}
}
func TestLookupServiceImpl_Init(t *testing.T) {
- mockCsvFileHelper := &mocks.CsvFileHelper{}
- mockCsvFileHelper.On("GetCsvFromFile", "./map.csv").Return([][]string{{"O-RU-ID", "O-DU-ID"}}, nil).Once()
- mockCsvFileHelper.On("GetCsvFromFile", "foo.csv").Return(nil, errors.New("Error")).Once()
- type fields struct {
- csvFileHelper CsvFileHelper
+ assertions := require.New(t)
+ type args struct {
csvFileName string
- oRuIdToODuIdMap map[string]string
+ mockReturn [][]string
+ mockReturnError error
}
tests := []struct {
- name string
- fields fields
- wantErr bool
+ name string
+ args args
+ wantedORuIdToODuIdMap map[string]string
+ wantErr error
}{
{
- name: "Init with proper csv file should not return error",
- fields: fields{
- csvFileHelper: mockCsvFileHelper,
- csvFileName: "./map.csv",
- oRuIdToODuIdMap: map[string]string{}},
- wantErr: false,
+ name: "Init with proper csv file should not return error and map should be initialized",
+ args: args{
+ csvFileName: "./map.csv",
+ mockReturn: [][]string{{"O-RU-ID", "O-DU-ID"}},
+ },
+ wantedORuIdToODuIdMap: map[string]string{"O-RU-ID": "O-DU-ID"},
},
{
- name: "Init with missing file should return error",
- fields: fields{
- csvFileHelper: mockCsvFileHelper,
+ name: "Init with missing file should return error and map should not be initialized",
+ args: args{
csvFileName: "foo.csv",
- oRuIdToODuIdMap: map[string]string{},
+ mockReturnError: errors.New("Error"),
},
- wantErr: true,
+ wantedORuIdToODuIdMap: map[string]string{},
+ wantErr: errors.New("Error"),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
- s := LookupServiceImpl{
- csvFileHelper: tt.fields.csvFileHelper,
- csvFileName: tt.fields.csvFileName,
- oRuIdToODuIdMap: tt.fields.oRuIdToODuIdMap,
- }
- if err := s.Init(); (err != nil) != tt.wantErr {
- t.Errorf("LookupServiceImpl.Init() error = %v, wantErr %v", err, tt.wantErr)
- } else if !tt.wantErr {
- wantedMap := map[string]string{"O-RU-ID": "O-DU-ID"}
- if !reflect.DeepEqual(wantedMap, s.oRuIdToODuIdMap) {
- t.Errorf("LookupServiceImpl.Init() map not initialized, wanted map: %v, got map: %v", wantedMap, s.oRuIdToODuIdMap)
- }
- }
+ mockCsvFileHelper := &mocks.CsvFileHelper{}
+ mockCsvFileHelper.On("GetCsvFromFile", tt.args.csvFileName).Return(tt.args.mockReturn, tt.args.mockReturnError)
+
+ s := NewLookupServiceImpl(mockCsvFileHelper, tt.args.csvFileName)
+
+ err := s.Init()
+
+ assertions.Equal(tt.wantErr, err, tt.name)
+ assertions.Equal(tt.wantedORuIdToODuIdMap, s.oRuIdToODuIdMap)
+ mockCsvFileHelper.AssertNumberOfCalls(t, "GetCsvFromFile", 1)
})
}
- mockCsvFileHelper.AssertNumberOfCalls(t, "GetCsvFromFile", 2)
}
func TestLookupServiceImpl_GetODuID(t *testing.T) {
+ assertions := require.New(t)
type fields struct {
csvFileHelper CsvFileHelper
csvFileName string
@@ -139,8 +144,7 @@
args: args{
oRuId: "O-RU-ID",
},
- want: "O-DU-ID",
- wantErr: nil,
+ want: "O-DU-ID",
},
{
name: "Id not mapped should return IdNotMappedError",
@@ -152,7 +156,6 @@
args: args{
oRuId: "O-RU-ID",
},
- want: "",
wantErr: IdNotMappedError{Id: "O-RU-ID"},
},
}
@@ -163,14 +166,11 @@
csvFileName: tt.fields.csvFileName,
oRuIdToODuIdMap: tt.fields.oRuIdToODuIdMap,
}
+
got, err := s.GetODuID(tt.args.oRuId)
- if err != tt.wantErr {
- t.Errorf("LookupServiceImpl.GetODuID() error = %v, wantErr %v", err, tt.wantErr)
- return
- }
- if got != tt.want {
- t.Errorf("LookupServiceImpl.GetODuID() = %v, want %v", got, tt.want)
- }
+
+ assertions.Equal(tt.wantErr, err, tt.name)
+ assertions.Equal(tt.want, got, tt.name)
})
}
}
diff --git a/test/usecases/oruclosedlooprecovery/goversion/internal/restclient/client.go b/test/usecases/oruclosedlooprecovery/goversion/internal/restclient/client.go
index 310a449..7932bfa 100644
--- a/test/usecases/oruclosedlooprecovery/goversion/internal/restclient/client.go
+++ b/test/usecases/oruclosedlooprecovery/goversion/internal/restclient/client.go
@@ -25,7 +25,6 @@
"fmt"
"io"
"net/http"
- "time"
)
type RequestError struct {
@@ -33,10 +32,6 @@
Body []byte
}
-func (pe RequestError) Error() string {
- return fmt.Sprintf("Request failed due to error response with status: %v and body: %v", pe.StatusCode, string(pe.Body))
-}
-
// HTTPClient interface
type HTTPClient interface {
Get(url string) (*http.Response, error)
@@ -44,46 +39,23 @@
Do(*http.Request) (*http.Response, error)
}
-var (
- Client HTTPClient
-)
-
-func init() {
- Client = &http.Client{
- Timeout: time.Second * 5,
- }
+func (pe RequestError) Error() string {
+ return fmt.Sprintf("Request failed due to error response with status: %v and body: %v", pe.StatusCode, string(pe.Body))
}
-func Get(url string) ([]byte, error) {
- if response, err := Client.Get(url); err == nil {
- if isResponseSuccess(response.StatusCode) {
- defer response.Body.Close()
- if responseData, err := io.ReadAll(response.Body); err == nil {
- return responseData, nil
- } else {
- return nil, err
- }
- } else {
- return nil, getResponseError(response)
- }
- } else {
- return nil, err
- }
+func PutWithoutAuth(url string, body []byte, client HTTPClient) error {
+ return do(http.MethodPut, url, body, client)
}
-func PutWithoutAuth(url string, body []byte) error {
- return do(http.MethodPut, url, body)
+func Put(url string, body string, client HTTPClient, userName string, password string) error {
+ return do(http.MethodPut, url, []byte(body), client, userName, password)
}
-func Put(url string, body string, userName string, password string) error {
- return do(http.MethodPut, url, []byte(body), userName, password)
+func Delete(url string, client HTTPClient) error {
+ return do(http.MethodDelete, url, nil, client)
}
-func Delete(url string) error {
- return do(http.MethodDelete, url, nil)
-}
-
-func do(method string, url string, body []byte, userInfo ...string) error {
+func do(method string, url string, body []byte, client HTTPClient, userInfo ...string) error {
if req, reqErr := http.NewRequest(method, url, bytes.NewBuffer(body)); reqErr == nil {
if body != nil {
req.Header.Set("Content-Type", "application/json; charset=utf-8")
@@ -91,7 +63,7 @@
if len(userInfo) > 0 {
req.SetBasicAuth(userInfo[0], userInfo[1])
}
- if response, respErr := Client.Do(req); respErr == nil {
+ if response, respErr := client.Do(req); respErr == nil {
if isResponseSuccess(response.StatusCode) {
return nil
} else {
diff --git a/test/usecases/oruclosedlooprecovery/goversion/internal/restclient/client_test.go b/test/usecases/oruclosedlooprecovery/goversion/internal/restclient/client_test.go
index 9b482b5..4b2f7fe 100644
--- a/test/usecases/oruclosedlooprecovery/goversion/internal/restclient/client_test.go
+++ b/test/usecases/oruclosedlooprecovery/goversion/internal/restclient/client_test.go
@@ -41,73 +41,6 @@
assertions.Equal("Request failed due to error response with status: 400 and body: error", actualError.Error())
}
-func TestGet(t *testing.T) {
- assertions := require.New(t)
-
- type args struct {
- url string
- mockReturnStatus int
- mockReturnBody []byte
- mockReturnError error
- }
- tests := []struct {
- name string
- args args
- want []byte
- wantErr error
- }{
- {
- name: "Ok response",
- args: args{
- url: "ok",
- mockReturnStatus: http.StatusOK,
- mockReturnBody: []byte("body"),
- mockReturnError: nil,
- },
- want: []byte("body"),
- wantErr: nil,
- },
- {
- name: "Bad request should get RequestError",
- args: args{
- url: "badRequest",
- mockReturnStatus: http.StatusBadRequest,
- mockReturnBody: []byte("bad request"),
- mockReturnError: nil,
- },
- want: nil,
- wantErr: RequestError{
- StatusCode: http.StatusBadRequest,
- Body: []byte("bad request"),
- },
- },
- {
- name: "Server unavailable should get error",
- args: args{
- url: "serverUnavailable",
- mockReturnError: fmt.Errorf("Server unavailable"),
- },
- want: nil,
- wantErr: fmt.Errorf("Server unavailable"),
- },
- }
- for _, tt := range tests {
- t.Run(tt.name, func(t *testing.T) {
- clientMock := mocks.HTTPClient{}
- clientMock.On("Get", tt.args.url).Return(&http.Response{
- StatusCode: tt.args.mockReturnStatus,
- Body: ioutil.NopCloser(bytes.NewReader(tt.args.mockReturnBody)),
- }, tt.args.mockReturnError)
- Client = &clientMock
-
- got, err := Get(tt.args.url)
- assertions.Equal(tt.wantErr, err, tt.name)
- assertions.Equal(tt.want, got, tt.name)
- clientMock.AssertCalled(t, "Get", tt.args.url)
- })
- }
-}
-
func TestPutWithoutAuth(t *testing.T) {
assertions := require.New(t)
@@ -115,9 +48,8 @@
clientMock.On("Do", mock.Anything).Return(&http.Response{
StatusCode: http.StatusOK,
}, nil)
- Client = &clientMock
- error := PutWithoutAuth("url", []byte("body"))
+ error := PutWithoutAuth("url", []byte("body"), &clientMock)
assertions.Nil(error)
var actualRequest *http.Request
@@ -142,9 +74,8 @@
clientMock.On("Do", mock.Anything).Return(&http.Response{
StatusCode: http.StatusOK,
}, nil)
- Client = &clientMock
- error := Put("url", "body", "admin", "pwd")
+ error := Put("url", "body", &clientMock, "admin", "pwd")
assertions.Nil(error)
var actualRequest *http.Request
@@ -171,9 +102,8 @@
clientMock.On("Do", mock.Anything).Return(&http.Response{
StatusCode: http.StatusOK,
}, nil)
- Client = &clientMock
- error := Delete("url")
+ error := Delete("url", &clientMock)
assertions.Nil(error)
var actualRequest *http.Request
@@ -232,9 +162,8 @@
StatusCode: tt.args.mockReturnStatus,
Body: ioutil.NopCloser(bytes.NewReader(tt.args.mockReturnBody)),
}, tt.args.mockReturnError)
- Client = &clientMock
- err := do("PUT", tt.args.url, nil)
+ err := do("PUT", tt.args.url, nil, &clientMock)
assertions.Equal(tt.wantErr, err, tt.name)
})
}
diff --git a/test/usecases/oruclosedlooprecovery/goversion/main.go b/test/usecases/oruclosedlooprecovery/goversion/main.go
index 5574f8c..86da4d4 100644
--- a/test/usecases/oruclosedlooprecovery/goversion/main.go
+++ b/test/usecases/oruclosedlooprecovery/goversion/main.go
@@ -24,6 +24,7 @@
"encoding/json"
"fmt"
"net/http"
+ "time"
"github.com/gorilla/mux"
log "github.com/sirupsen/logrus"
@@ -33,40 +34,63 @@
"oransc.org/usecase/oruclosedloop/internal/restclient"
)
-var consumerConfig linkfailure.Configuration
+const timeoutHTTPClient = time.Second * 5
+const jobId = "14e7bb84-a44d-44c1-90b7-6995a92ad43c"
+
+var infoCoordAddress string
+var linkfailureConfig linkfailure.Configuration
var lookupService repository.LookupService
var host string
var port string
-
-const jobId = "14e7bb84-a44d-44c1-90b7-6995a92ad43c"
+var client restclient.HTTPClient
func init() {
configuration := config.New()
+ client = &http.Client{
+ Timeout: timeoutHTTPClient,
+ }
+
log.SetLevel(configuration.LogLevel)
- if configuration.ConsumerHost == "" || configuration.ConsumerPort == 0 {
- log.Fatal("Consumer host and port must be provided!")
+ if err := validateConfiguration(configuration); err != nil {
+ log.Fatalf("Unable to start consumer due to: %v", err)
}
host = configuration.ConsumerHost
port = fmt.Sprint(configuration.ConsumerPort)
- csvFileHelper := repository.NewCsvFileHelper()
- lookupService = repository.NewLookupServiceImpl(&csvFileHelper, configuration.ORUToODUMapFile)
- if initErr := lookupService.Init(); initErr != nil {
+ csvFileHelper := repository.NewCsvFileHelperImpl()
+ if initErr := initializeLookupService(csvFileHelper, configuration); initErr != nil {
log.Fatalf("Unable to create LookupService due to inability to get O-RU-ID to O-DU-ID map. Cause: %v", initErr)
}
- consumerConfig = linkfailure.Configuration{
- InfoCoordAddress: configuration.InfoCoordinatorAddress,
- SDNRAddress: configuration.SDNRHost + ":" + fmt.Sprint(configuration.SDNRPort),
- SDNRUser: configuration.SDNRUser,
- SDNRPassword: configuration.SDNPassword,
+
+ infoCoordAddress = configuration.InfoCoordinatorAddress
+
+ linkfailureConfig = linkfailure.Configuration{
+ SDNRAddress: configuration.SDNRHost + ":" + fmt.Sprint(configuration.SDNRPort),
+ SDNRUser: configuration.SDNRUser,
+ SDNRPassword: configuration.SDNPassword,
}
}
+func validateConfiguration(configuration *config.Config) error {
+ if configuration.ConsumerHost == "" || configuration.ConsumerPort == 0 {
+ return fmt.Errorf("consumer host and port must be provided")
+ }
+ return nil
+}
+
+func initializeLookupService(csvFileHelper repository.CsvFileHelper, configuration *config.Config) error {
+ lookupService = repository.NewLookupServiceImpl(csvFileHelper, configuration.ORUToODUMapFile)
+ if initErr := lookupService.Init(); initErr != nil {
+ return initErr
+ }
+ return nil
+}
+
func main() {
defer deleteJob()
- messageHandler := linkfailure.NewLinkFailureHandler(lookupService, consumerConfig)
+ messageHandler := linkfailure.NewLinkFailureHandler(lookupService, linkfailureConfig, client)
r := mux.NewRouter()
r.HandleFunc("/", messageHandler.MessagesHandler).Methods(http.MethodPost)
r.HandleFunc("/admin/start", startHandler).Methods(http.MethodPost)
@@ -87,7 +111,7 @@
JobDefinition: "{}",
}
body, _ := json.Marshal(jobRegistrationInfo)
- putErr := restclient.PutWithoutAuth(consumerConfig.InfoCoordAddress+"/data-consumer/v1/info-jobs/"+jobId, body)
+ putErr := restclient.PutWithoutAuth(infoCoordAddress+"/data-consumer/v1/info-jobs/"+jobId, body, client)
if putErr != nil {
http.Error(w, fmt.Sprintf("Unable to register consumer job: %v", putErr), http.StatusBadRequest)
return
@@ -105,5 +129,5 @@
}
func deleteJob() error {
- return restclient.Delete(consumerConfig.InfoCoordAddress + "/data-consumer/v1/info-jobs/" + jobId)
+ return restclient.Delete(infoCoordAddress+"/data-consumer/v1/info-jobs/"+jobId, client)
}