Improve UT coverage 37/6337/1
authorMohamed Abukar <abukar.mohamed@nokia.com>
Wed, 16 Jun 2021 15:33:47 +0000 (15:33 +0000)
committerMohamed Abukar <abukar.mohamed@nokia.com>
Wed, 16 Jun 2021 15:34:44 +0000 (15:34 +0000)
Change-Id: If308d64f48adfc1778eeafdc6d66cbb05c0476bf
Signed-off-by: Mohamed Abukar <abukar.mohamed@nokia.com>
pkg/xapp/restapi.go
pkg/xapp/utils_test.go [new file with mode: 0644]

index 7019bac..b2ea7ad 100755 (executable)
@@ -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 (file)
index 0000000..358fbe2
--- /dev/null
@@ -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)
+}