Implementation for downloading charts
[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         "strings"
32         "testing"
33
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"
43 )
44
45 var rh *Resthook
46 var successStatus *models.Status
47
48 func TestMain(m *testing.M) {
49
50         successStatus = &models.Status{
51                 Status: &health.HEALTHY,
52         }
53         ricdms.Init()
54         rh = &Resthook{
55                 HealthChecker: HealthCheckerMock{},
56                 Onboarder:     onboard.NewOnboarder(),
57                 ChartMgr:      ch.NewChartmgr(),
58         }
59         code := m.Run()
60         os.Exit(code)
61 }
62
63 func TestHealth(t *testing.T) {
64         resp := rh.GetDMSHealth()
65         switch resp.(type) {
66         case *h.GetHealthCheckOK:
67                 assert.Equal(t, successStatus, resp.(*h.GetHealthCheckOK).Payload)
68
69         case *h.GetHealthCheckInternalServerError:
70                 assert.Fail(t, "Internal Server generated: %v", resp)
71
72         default:
73                 assert.Fail(t, "Unknown type of resp : %v", resp)
74         }
75 }
76
77 func TestOnboard(t *testing.T) {
78         xApp := &models.Descriptor{
79                 Config: "SAMPLE_CONFIGURATION",
80                 Schema: "SAMPLE_SCHEMA",
81         }
82
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)
86                 defer r.Body.Close()
87
88                 err := json.Unmarshal(reqBytes, &d)
89
90                 ricdms.Logger.Debug("after unmarshal : %+v body: %+v", d, string(reqBytes))
91
92                 if err != nil {
93                         assert.Fail(t, "Not able to parse the request body")
94                 }
95
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")
99         }))
100         svr.Listener.Close()
101         svr.Listener, _ = net.Listen("tcp", ricdms.Config.MockServer)
102
103         svr.Start()
104         defer svr.Close()
105
106         resp := rh.OnBoard(xApp)
107         assert.NotEqual(t, nil, resp)
108 }
109
110 func TestGetCharts(t *testing.T) {
111
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")
115         }))
116         svr.Listener.Close()
117         svr.Listener, _ = net.Listen("tcp", ricdms.Config.MockServer)
118
119         svr.Start()
120         defer svr.Close()
121
122         resp := rh.GetCharts()
123         assert.NotEqual(t, nil, resp)
124
125         successResp := resp.(*charts.GetChartsListOK)
126         assert.Equal(t, "SAMPLE_RESPONSE", successResp.Payload)
127 }
128
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)
135                 w.Write(data)
136         }))
137         svr.Listener.Close()
138         svr.Listener, _ = net.Listen("tcp", ricdms.Config.MockServer)
139
140         svr.Start()
141         defer svr.Close()
142
143         resp := rh.DownloadChart("CHART_NAME", "VERSION")
144         assert.IsType(t, &charts.DownloadHelmChartOK{}, resp, "response did not match type")
145 }
146
147 type HealthCheckerMock struct {
148         mock.Mock
149 }
150
151 func (h HealthCheckerMock) GetStatus() *models.Status {
152         return successStatus
153 }