1 //==================================================================================
2 // Copyright (c) 2022 Samsung
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
8 // http://www.apache.org/licenses/LICENSE-2.0
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.
16 // This source code is part of the near-RT RIC (RAN Intelligent Controller)
17 // platform project (RICP).
18 //==================================================================================
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"
47 var successStatus *models.Status
49 func TestMain(m *testing.M) {
51 successStatus = &models.Status{
52 Status: &health.HEALTHY,
56 HealthChecker: HealthCheckerMock{},
57 Onboarder: onboard.NewOnboarder(),
58 ChartMgr: ch.NewChartmgr(),
64 func TestHealth(t *testing.T) {
65 resp := rh.GetDMSHealth()
67 case *h.GetHealthCheckOK:
68 assert.Equal(t, successStatus, resp.(*h.GetHealthCheckOK).Payload)
70 case *h.GetHealthCheckInternalServerError:
71 assert.Fail(t, "Internal Server generated: %v", resp)
74 assert.Fail(t, "Unknown type of resp : %v", resp)
78 func TestOnboard(t *testing.T) {
79 xApp := &models.Descriptor{
80 Config: "SAMPLE_CONFIGURATION",
81 Schema: "SAMPLE_SCHEMA",
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)
89 err := json.Unmarshal(reqBytes, &d)
91 ricdms.Logger.Debug("after unmarshal : %+v body: %+v", d, string(reqBytes))
94 assert.Fail(t, "Not able to parse the request body")
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")
102 svr.Listener, _ = net.Listen("tcp", ricdms.Config.MockServer)
107 resp := rh.OnBoard(xApp)
108 assert.NotEqual(t, nil, resp)
111 func TestGetCharts(t *testing.T) {
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")
118 svr.Listener, _ = net.Listen("tcp", ricdms.Config.MockServer)
123 resp := rh.GetCharts()
124 assert.NotEqual(t, nil, resp)
126 successResp := resp.(*charts.GetChartsListOK)
127 assert.Equal(t, "SAMPLE_RESPONSE", successResp.Payload)
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)
139 svr.Listener, _ = net.Listen("tcp", ricdms.Config.MockServer)
144 resp := rh.DownloadChart("CHART_NAME", "VERSION")
145 assert.IsType(t, &charts.DownloadHelmChartOK{}, resp, "response did not match type")
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)
155 ricdms.Logger.Error("error in reading file: %v", err)
158 jsonData, err := io.ReadAll(file)
160 ricdms.Logger.Error("Error in rading file: %v", err)
166 svr.Listener, _ = net.Listen("tcp", ricdms.Config.MockServer)
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")
176 func TestGetChartsByNameAndVersion(t *testing.T) {
177 svr := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
178 ricdms.Logger.Debug("request received by mock to get chart by name and version")
179 path, _ := filepath.Abs("./mocks/resp-get-charts-by-name-and-ver.json")
180 file, err := os.Open(path)
183 ricdms.Logger.Error("error in reading file: %v", err)
186 jsonData, err := io.ReadAll(file)
188 ricdms.Logger.Error("Error in rading file: %v", err)
194 svr.Listener, _ = net.Listen("tcp", ricdms.Config.MockServer)
199 resp := rh.GetChartByNameAndVersion("Test", "1.0.0")
200 ricdms.Logger.Debug("resp data: %s", resp.(*charts.GetChartsFetcherOK).Payload)
201 assert.IsType(t, &charts.GetChartsFetcherOK{}, resp, "response did not match type")
204 type HealthCheckerMock struct {
208 func (h HealthCheckerMock) GetStatus() *models.Status {