Further UT improvements
[ric-plt/xapp-frame.git] / pkg / xapp / utils_test.go
1 /*
2 ==================================================================================
3   Copyright (c) 2019 AT&T Intellectual Property.
4   Copyright (c) 2019 Nokia
5
6    Licensed under the Apache License, Version 2.0 (the "License");
7    you may not use this file except in compliance with the License.
8    You may obtain a copy of the License at
9
10        http://www.apache.org/licenses/LICENSE-2.0
11
12    Unless required by applicable law or agreed to in writing, software
13    distributed under the License is distributed on an "AS IS" BASIS,
14    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15    See the License for the specific language governing permissions and
16    limitations under the License.
17 ==================================================================================
18 */
19
20 package xapp
21
22 import (
23         "io/ioutil"
24         "net/http"
25         "os"
26         "testing"
27
28         "github.com/stretchr/testify/assert"
29 )
30
31 func TestNewUtils(t *testing.T) {
32         utils := NewUtils()
33         assert.NotNil(t, utils, "NewUtils failed")
34
35         assert.Equal(t, utils.FileExists("abcd"), false)
36         assert.Equal(t, utils.CreateDir("/tmp/abcd"), nil)
37         assert.Equal(t, utils.WriteToFile("/tmp/abcd/file.txt", "data"), nil)
38         utils.FetchFiles("./", []string{"go.mod"})
39         utils.FetchFiles("./", []string{"go.mod"})
40
41         tmpFile, err := ioutil.TempFile("", "symptom")
42         assert.Equal(t, err, nil)
43         defer os.Remove(tmpFile.Name())
44
45         err = utils.ZipFiles(tmpFile, "/tmp/abcd", []string{"/tmp/abcd/file.txt"})
46         assert.Equal(t, err, nil)
47
48         utils.DeleteFile("/tmp/abcd")
49 }
50
51 func TestSymptomdata(t *testing.T) {
52         os.Setenv("RMR_STASH_RT", "config/uta_rtg.rt.stash.inc")
53         assert.Equal(t, Resource.CollectDefaultSymptomData("abcd.tgz", "data"), "/tmp/xapp/")
54 }
55
56 func TestSymptomdataCollection(t *testing.T) {
57         var handler1 = func(w http.ResponseWriter, r *http.Request) {
58                 Resource.SendSymptomDataJson(w, r, "data", "aaaa")
59                 Resource.SendSymptomDataFile(w, r, "./config", "symptomdata.gz")
60         }
61
62         Resource.InjectQueryRoute("/ric/v1/user1", handler1, "GET", "foo", "bar", "id", "mykey")
63
64         req, _ := http.NewRequest("GET", "/ric/v1/user1?foo=bar&id=mykey", nil)
65         resp := executeRequest(req, nil)
66         checkResponseCode(t, http.StatusOK, resp.Code)
67 }
68
69 func TestSymptomdataCollectionError(t *testing.T) {
70         var handler2 = func(w http.ResponseWriter, r *http.Request) {
71                 Resource.SendSymptomDataError(w, r, "Error text")
72         }
73
74         Resource.InjectQueryRoute("/ric/v1/user2", handler2, "GET", "foo", "bar", "id", "mykey")
75
76         req, _ := http.NewRequest("GET", "/ric/v1/user2?foo=bar&id=mykey", nil)
77         resp := executeRequest(req, nil)
78         checkResponseCode(t, http.StatusInternalServerError, resp.Code)
79 }
80
81 func TestGetSymptomDataParams(t *testing.T) {
82         var handler3 = func(w http.ResponseWriter, r *http.Request) {
83                 Resource.GetSymptomDataParams(w, r)
84         }
85
86         Resource.InjectQueryRoute("/ric/v1/user3", handler3, "GET", "timeout", "10", "fromtime", "1", "totime", "2")
87
88         req, _ := http.NewRequest("GET", "/ric/v1/user3?timeout=10&fromtime=1&totime=2", nil)
89         resp := executeRequest(req, nil)
90         checkResponseCode(t, http.StatusOK, resp.Code)
91 }
92
93 func TestAppconfigHandler(t *testing.T) {
94         var handler4 = func(w http.ResponseWriter, r *http.Request) {
95                 appconfigHandler(w, r)
96         }
97
98         Resource.InjectQueryRoute("/ric/v1/user4", handler4, "GET", "foo", "bar", "id", "mykey")
99
100         req, _ := http.NewRequest("GET", "/ric/v1/user4?foo=bar&id=mykey", nil)
101         resp := executeRequest(req, nil)
102         checkResponseCode(t, http.StatusInternalServerError, resp.Code)
103 }