Implementation for downloading charts
[ric-plt/ricdms.git] / pkg / resthooks / resthooks_test.go
index 74ba05e..b18b164 100644 (file)
@@ -22,16 +22,20 @@ package resthooks
 import (
        "encoding/json"
        "fmt"
+       "io"
        "io/ioutil"
        "net"
        "net/http"
        "net/http/httptest"
        "os"
+       "strings"
        "testing"
 
+       ch "gerrit.o-ran-sc.org/r/ric-plt/ricdms/pkg/charts"
        "gerrit.o-ran-sc.org/r/ric-plt/ricdms/pkg/health"
        "gerrit.o-ran-sc.org/r/ric-plt/ricdms/pkg/models"
        "gerrit.o-ran-sc.org/r/ric-plt/ricdms/pkg/onboard"
+       "gerrit.o-ran-sc.org/r/ric-plt/ricdms/pkg/restapi/operations/charts"
        h "gerrit.o-ran-sc.org/r/ric-plt/ricdms/pkg/restapi/operations/health"
        "gerrit.o-ran-sc.org/r/ric-plt/ricdms/pkg/ricdms"
        "github.com/stretchr/testify/assert"
@@ -50,6 +54,7 @@ func TestMain(m *testing.M) {
        rh = &Resthook{
                HealthChecker: HealthCheckerMock{},
                Onboarder:     onboard.NewOnboarder(),
+               ChartMgr:      ch.NewChartmgr(),
        }
        code := m.Run()
        os.Exit(code)
@@ -102,6 +107,43 @@ func TestOnboard(t *testing.T) {
        assert.NotEqual(t, nil, resp)
 }
 
+func TestGetCharts(t *testing.T) {
+
+       svr := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+               ricdms.Logger.Debug("Mock server running")
+               fmt.Fprintf(w, "SAMPLE_RESPONSE")
+       }))
+       svr.Listener.Close()
+       svr.Listener, _ = net.Listen("tcp", ricdms.Config.MockServer)
+
+       svr.Start()
+       defer svr.Close()
+
+       resp := rh.GetCharts()
+       assert.NotEqual(t, nil, resp)
+
+       successResp := resp.(*charts.GetChartsListOK)
+       assert.Equal(t, "SAMPLE_RESPONSE", successResp.Payload)
+}
+
+func TestDownloadChart(t *testing.T) {
+       svr := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+               ricdms.Logger.Debug("request received by mock to download chart")
+               reader := strings.NewReader("SAMPLE_RESPONSE")
+               data, _ := io.ReadAll(reader)
+               ricdms.Logger.Debug("writing : %+v", data)
+               w.Write(data)
+       }))
+       svr.Listener.Close()
+       svr.Listener, _ = net.Listen("tcp", ricdms.Config.MockServer)
+
+       svr.Start()
+       defer svr.Close()
+
+       resp := rh.DownloadChart("CHART_NAME", "VERSION")
+       assert.IsType(t, &charts.DownloadHelmChartOK{}, resp, "response did not match type")
+}
+
 type HealthCheckerMock struct {
        mock.Mock
 }