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 //==================================================================================
34 ch "gerrit.o-ran-sc.org/r/ric-plt/ricdms/pkg/charts"
35 "gerrit.o-ran-sc.org/r/ric-plt/ricdms/pkg/health"
36 "gerrit.o-ran-sc.org/r/ric-plt/ricdms/pkg/models"
37 "gerrit.o-ran-sc.org/r/ric-plt/ricdms/pkg/onboard"
38 "gerrit.o-ran-sc.org/r/ric-plt/ricdms/pkg/restapi/operations/charts"
39 h "gerrit.o-ran-sc.org/r/ric-plt/ricdms/pkg/restapi/operations/health"
40 "gerrit.o-ran-sc.org/r/ric-plt/ricdms/pkg/ricdms"
41 "github.com/stretchr/testify/assert"
42 "github.com/stretchr/testify/mock"
46 var successStatus *models.Status
48 func TestMain(m *testing.M) {
50 successStatus = &models.Status{
51 Status: &health.HEALTHY,
55 HealthChecker: HealthCheckerMock{},
56 Onboarder: onboard.NewOnboarder(),
57 ChartMgr: ch.NewChartmgr(),
63 func TestHealth(t *testing.T) {
64 resp := rh.GetDMSHealth()
66 case *h.GetHealthCheckOK:
67 assert.Equal(t, successStatus, resp.(*h.GetHealthCheckOK).Payload)
69 case *h.GetHealthCheckInternalServerError:
70 assert.Fail(t, "Internal Server generated: %v", resp)
73 assert.Fail(t, "Unknown type of resp : %v", resp)
77 func TestOnboard(t *testing.T) {
78 xApp := &models.Descriptor{
79 Config: "SAMPLE_CONFIGURATION",
80 Schema: "SAMPLE_SCHEMA",
83 svr := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
84 var d map[string]interface{}
85 reqBytes, _ := ioutil.ReadAll(r.Body)
88 err := json.Unmarshal(reqBytes, &d)
90 ricdms.Logger.Debug("after unmarshal : %+v body: %+v", d, string(reqBytes))
93 assert.Fail(t, "Not able to parse the request body")
96 assert.Equal(t, xApp.Config, d["config-file.json"])
97 assert.Equal(t, xApp.Schema, d["controls-schema.json"])
98 fmt.Fprintf(w, "SAMPLE_RESPONSE")
101 svr.Listener, _ = net.Listen("tcp", ricdms.Config.MockServer)
106 resp := rh.OnBoard(xApp)
107 assert.NotEqual(t, nil, resp)
110 func TestGetCharts(t *testing.T) {
112 svr := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
113 ricdms.Logger.Debug("Mock server running")
114 fmt.Fprintf(w, "SAMPLE_RESPONSE")
117 svr.Listener, _ = net.Listen("tcp", ricdms.Config.MockServer)
122 resp := rh.GetCharts()
123 assert.NotEqual(t, nil, resp)
125 successResp := resp.(*charts.GetChartsListOK)
126 assert.Equal(t, "SAMPLE_RESPONSE", successResp.Payload)
129 func TestDownloadChart(t *testing.T) {
130 svr := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
131 ricdms.Logger.Debug("request received by mock to download chart")
132 reader := strings.NewReader("SAMPLE_RESPONSE")
133 data, _ := io.ReadAll(reader)
134 ricdms.Logger.Debug("writing : %+v", data)
138 svr.Listener, _ = net.Listen("tcp", ricdms.Config.MockServer)
143 resp := rh.DownloadChart("CHART_NAME", "VERSION")
144 assert.IsType(t, &charts.DownloadHelmChartOK{}, resp, "response did not match type")
147 type HealthCheckerMock struct {
151 func (h HealthCheckerMock) GetStatus() *models.Status {