From: Mohamed Abukar Date: Wed, 16 Jun 2021 15:33:47 +0000 (+0000) Subject: Improve UT coverage X-Git-Tag: v0.8.4~2 X-Git-Url: https://gerrit.o-ran-sc.org/r/gitweb?a=commitdiff_plain;h=4623ad98eb5826c5fbb0928c972e9f02bed4a57a;p=ric-plt%2Fxapp-frame.git Improve UT coverage Change-Id: If308d64f48adfc1778eeafdc6d66cbb05c0476bf Signed-off-by: Mohamed Abukar --- diff --git a/pkg/xapp/restapi.go b/pkg/xapp/restapi.go index 7019bac..b2ea7ad 100755 --- a/pkg/xapp/restapi.go +++ b/pkg/xapp/restapi.go @@ -22,12 +22,13 @@ package xapp import ( "encoding/json" "fmt" - "github.com/gorilla/mux" - "github.com/spf13/viper" "io/ioutil" "net/http" "os" + "github.com/gorilla/mux" + "github.com/spf13/viper" + "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/models" ) diff --git a/pkg/xapp/utils_test.go b/pkg/xapp/utils_test.go new file mode 100644 index 0000000..358fbe2 --- /dev/null +++ b/pkg/xapp/utils_test.go @@ -0,0 +1,93 @@ +/* +================================================================================== + Copyright (c) 2019 AT&T Intellectual Property. + Copyright (c) 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 xapp + +import ( + "net/http" + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestNewUtils(t *testing.T) { + utils := NewUtils() + assert.NotNil(t, utils, "NewUtils failed") + + assert.Equal(t, utils.FileExists("abcd"), false) + assert.Equal(t, utils.CreateDir("/tmp/abcd"), nil) + assert.Equal(t, utils.WriteToFile("/tmp/abcd/file.txt", "data"), nil) + utils.FetchFiles("./", []string{"go.mod"}) + utils.FetchFiles("./", []string{"go.mod"}) + + utils.DeleteFile("/tmp/abcd") +} + +func TestSymptomdata(t *testing.T) { + assert.Equal(t, Resource.CollectDefaultSymptomData("abcd.tgz", "data"), "/tmp/xapp/") +} + +func TestSymptomdataCollection(t *testing.T) { + var handler = func(w http.ResponseWriter, r *http.Request) { + Resource.SendSymptomDataJson(w, r, "data", "aaaa") + Resource.SendSymptomDataFile(w, r, "./config", "symptomdata.gz") + } + + Resource.InjectQueryRoute("/ric/v1/user", handler, "GET", "foo", "bar", "id", "mykey") + + req, _ := http.NewRequest("GET", "/ric/v1/user?foo=bar&id=mykey", nil) + resp := executeRequest(req, nil) + checkResponseCode(t, http.StatusOK, resp.Code) +} + +func TestSymptomdataCollectionError(t *testing.T) { + var handler = func(w http.ResponseWriter, r *http.Request) { + Resource.SendSymptomDataError(w, r, "Error text") + } + + Resource.InjectQueryRoute("/ric/v1/user", handler, "GET", "foo", "bar", "id", "mykey") + + req, _ := http.NewRequest("GET", "/ric/v1/user?foo=bar&id=mykey", nil) + resp := executeRequest(req, nil) + checkResponseCode(t, http.StatusOK, resp.Code) +} + +func TestGetSymptomDataParams(t *testing.T) { + var handler = func(w http.ResponseWriter, r *http.Request) { + Resource.GetSymptomDataParams(w, r) + } + + Resource.InjectQueryRoute("/ric/v1/user", handler, "GET", "foo", "bar", "id", "mykey") + + req, _ := http.NewRequest("GET", "/ric/v1/user?foo=bar&id=mykey", nil) + resp := executeRequest(req, nil) + checkResponseCode(t, http.StatusOK, resp.Code) +} + +func TestAppconfigHandler(t *testing.T) { + var handler = func(w http.ResponseWriter, r *http.Request) { + appconfigHandler(w, r) + } + + Resource.InjectQueryRoute("/ric/v1/user", handler, "GET", "foo", "bar", "id", "mykey") + + req, _ := http.NewRequest("GET", "/ric/v1/user?foo=bar&id=mykey", nil) + resp := executeRequest(req, nil) + checkResponseCode(t, http.StatusOK, resp.Code) +}