c8deeb279e29194288790563241cef544a909251
[ric-plt/ricdms.git] / pkg / resthooks / resthooks_test.go
1 //==================================================================================
2 //  Copyright (c) 2022 Samsung
3 //
4 //   Licensed under the Apache License, Version 2.0 (the "License");
5 //   you may not use this file except in compliance with the License.
6 //   You may obtain a copy of the License at
7 //
8 //       http://www.apache.org/licenses/LICENSE-2.0
9 //
10 //   Unless required by applicable law or agreed to in writing, software
11 //   distributed under the License is distributed on an "AS IS" BASIS,
12 //   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 //   See the License for the specific language governing permissions and
14 //   limitations under the License.
15 //
16 //   This source code is part of the near-RT RIC (RAN Intelligent Controller)
17 //   platform project (RICP).
18 //==================================================================================
19 //
20 package resthooks
21
22 import (
23         "encoding/json"
24         "fmt"
25         "io"
26         "io/ioutil"
27         "net"
28         "net/http"
29         "net/http/httptest"
30         "os"
31         "path/filepath"
32         "strings"
33         "testing"
34
35         ch "gerrit.o-ran-sc.org/r/ric-plt/ricdms/pkg/charts"
36         "gerrit.o-ran-sc.org/r/ric-plt/ricdms/pkg/health"
37         "gerrit.o-ran-sc.org/r/ric-plt/ricdms/pkg/models"
38         "gerrit.o-ran-sc.org/r/ric-plt/ricdms/pkg/onboard"
39         "gerrit.o-ran-sc.org/r/ric-plt/ricdms/pkg/restapi/operations/charts"
40         h "gerrit.o-ran-sc.org/r/ric-plt/ricdms/pkg/restapi/operations/health"
41         "gerrit.o-ran-sc.org/r/ric-plt/ricdms/pkg/ricdms"
42         "github.com/stretchr/testify/assert"
43         "github.com/stretchr/testify/mock"
44 )
45
46 var rh *Resthook
47 var successStatus *models.Status
48
49 func TestMain(m *testing.M) {
50
51         successStatus = &models.Status{
52                 Status: &health.HEALTHY,
53         }
54         ricdms.Init()
55         rh = &Resthook{
56                 HealthChecker: HealthCheckerMock{},
57                 Onboarder:     onboard.NewOnboarder(),
58                 ChartMgr:      ch.NewChartmgr(),
59         }
60         code := m.Run()
61         os.Exit(code)
62 }
63
64 func TestHealth(t *testing.T) {
65         resp := rh.GetDMSHealth()
66         switch resp.(type) {
67         case *h.GetHealthCheckOK:
68                 assert.Equal(t, successStatus, resp.(*h.GetHealthCheckOK).Payload)
69
70         case *h.GetHealthCheckInternalServerError:
71                 assert.Fail(t, "Internal Server generated: %v", resp)
72
73         default:
74                 assert.Fail(t, "Unknown type of resp : %v", resp)
75         }
76 }
77
78 func TestOnboard(t *testing.T) {
79         xApp := &models.Descriptor{
80                 Config: "SAMPLE_CONFIGURATION",
81                 Schema: "SAMPLE_SCHEMA",
82         }
83
84         svr := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
85                 var d map[string]interface{}
86                 reqBytes, _ := ioutil.ReadAll(r.Body)
87                 defer r.Body.Close()
88
89                 err := json.Unmarshal(reqBytes, &d)
90
91                 ricdms.Logger.Debug("after unmarshal : %+v body: %+v", d, string(reqBytes))
92
93                 if err != nil {
94                         assert.Fail(t, "Not able to parse the request body")
95                 }
96
97                 assert.Equal(t, xApp.Config, d["config-file.json"])
98                 assert.Equal(t, xApp.Schema, d["controls-schema.json"])
99                 fmt.Fprintf(w, "SAMPLE_RESPONSE")
100         }))
101         svr.Listener.Close()
102         svr.Listener, _ = net.Listen("tcp", ricdms.Config.MockServer)
103
104         svr.Start()
105         defer svr.Close()
106
107         resp := rh.OnBoard(xApp)
108         assert.NotEqual(t, nil, resp)
109 }
110
111 func TestGetCharts(t *testing.T) {
112
113         svr := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
114                 ricdms.Logger.Debug("Mock server running")
115                 fmt.Fprintf(w, "SAMPLE_RESPONSE")
116         }))
117         svr.Listener.Close()
118         svr.Listener, _ = net.Listen("tcp", ricdms.Config.MockServer)
119
120         svr.Start()
121         defer svr.Close()
122
123         resp := rh.GetCharts()
124         assert.NotEqual(t, nil, resp)
125
126         successResp := resp.(*charts.GetChartsListOK)
127         assert.Equal(t, "SAMPLE_RESPONSE", successResp.Payload)
128 }
129
130 func TestDownloadChart(t *testing.T) {
131         svr := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
132                 ricdms.Logger.Debug("request received by mock to download chart")
133                 reader := strings.NewReader("SAMPLE_RESPONSE")
134                 data, _ := io.ReadAll(reader)
135                 ricdms.Logger.Debug("writing : %+v", data)
136                 w.Write(data)
137         }))
138         svr.Listener.Close()
139         svr.Listener, _ = net.Listen("tcp", ricdms.Config.MockServer)
140
141         svr.Start()
142         defer svr.Close()
143
144         resp := rh.DownloadChart("CHART_NAME", "VERSION")
145         assert.IsType(t, &charts.DownloadHelmChartOK{}, resp, "response did not match type")
146 }
147
148 func TestGetChartsByName(t *testing.T) {
149         svr := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
150                 ricdms.Logger.Debug("request received by mock to get chart by name")
151                 path, _ := filepath.Abs("./mocks/resp-get-charts-by-name.json")
152                 file, err := os.Open(path)
153
154                 if err != nil {
155                         ricdms.Logger.Error("error in reading file: %v", err)
156                 }
157
158                 jsonData, err := io.ReadAll(file)
159                 if err != nil {
160                         ricdms.Logger.Error("Error in rading file: %v", err)
161                 }
162                 w.Write(jsonData)
163         }))
164
165         svr.Listener.Close()
166         svr.Listener, _ = net.Listen("tcp", ricdms.Config.MockServer)
167
168         svr.Start()
169         defer svr.Close()
170
171         resp := rh.GetChartsByName("TEST_NAME")
172         ricdms.Logger.Debug("resp Data: %s", resp.(*charts.GetChartOK).Payload...)
173         assert.IsType(t, &charts.GetChartOK{}, resp, "response did not match type")
174 }
175
176 type HealthCheckerMock struct {
177         mock.Mock
178 }
179
180 func (h HealthCheckerMock) GetStatus() *models.Status {
181         return successStatus
182 }