517fb1111d2729927f478c260a19c0c3a07bc6bd
[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/ioutil"
26         "net"
27         "net/http"
28         "net/http/httptest"
29         "os"
30         "testing"
31
32         ch "gerrit.o-ran-sc.org/r/ric-plt/ricdms/pkg/charts"
33         "gerrit.o-ran-sc.org/r/ric-plt/ricdms/pkg/health"
34         "gerrit.o-ran-sc.org/r/ric-plt/ricdms/pkg/models"
35         "gerrit.o-ran-sc.org/r/ric-plt/ricdms/pkg/onboard"
36         "gerrit.o-ran-sc.org/r/ric-plt/ricdms/pkg/restapi/operations/charts"
37         h "gerrit.o-ran-sc.org/r/ric-plt/ricdms/pkg/restapi/operations/health"
38         "gerrit.o-ran-sc.org/r/ric-plt/ricdms/pkg/ricdms"
39         "github.com/stretchr/testify/assert"
40         "github.com/stretchr/testify/mock"
41 )
42
43 var rh *Resthook
44 var successStatus *models.Status
45
46 func TestMain(m *testing.M) {
47
48         successStatus = &models.Status{
49                 Status: &health.HEALTHY,
50         }
51         ricdms.Init()
52         rh = &Resthook{
53                 HealthChecker: HealthCheckerMock{},
54                 Onboarder:     onboard.NewOnboarder(),
55                 ChartMgr:      ch.NewChartmgr(),
56         }
57         code := m.Run()
58         os.Exit(code)
59 }
60
61 func TestHealth(t *testing.T) {
62         resp := rh.GetDMSHealth()
63         switch resp.(type) {
64         case *h.GetHealthCheckOK:
65                 assert.Equal(t, successStatus, resp.(*h.GetHealthCheckOK).Payload)
66
67         case *h.GetHealthCheckInternalServerError:
68                 assert.Fail(t, "Internal Server generated: %v", resp)
69
70         default:
71                 assert.Fail(t, "Unknown type of resp : %v", resp)
72         }
73 }
74
75 func TestOnboard(t *testing.T) {
76         xApp := &models.Descriptor{
77                 Config: "SAMPLE_CONFIGURATION",
78                 Schema: "SAMPLE_SCHEMA",
79         }
80
81         svr := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
82                 var d map[string]interface{}
83                 reqBytes, _ := ioutil.ReadAll(r.Body)
84                 defer r.Body.Close()
85
86                 err := json.Unmarshal(reqBytes, &d)
87
88                 ricdms.Logger.Debug("after unmarshal : %+v body: %+v", d, string(reqBytes))
89
90                 if err != nil {
91                         assert.Fail(t, "Not able to parse the request body")
92                 }
93
94                 assert.Equal(t, xApp.Config, d["config-file.json"])
95                 assert.Equal(t, xApp.Schema, d["controls-schema.json"])
96                 fmt.Fprintf(w, "SAMPLE_RESPONSE")
97         }))
98         svr.Listener.Close()
99         svr.Listener, _ = net.Listen("tcp", ricdms.Config.MockServer)
100
101         svr.Start()
102         defer svr.Close()
103
104         resp := rh.OnBoard(xApp)
105         assert.NotEqual(t, nil, resp)
106 }
107
108 func TestGetCharts(t *testing.T) {
109
110         svr := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
111                 ricdms.Logger.Debug("Mock server running")
112                 fmt.Fprintf(w, "SAMPLE_RESPONSE")
113         }))
114         svr.Listener.Close()
115         svr.Listener, _ = net.Listen("tcp", ricdms.Config.MockServer)
116
117         svr.Start()
118         defer svr.Close()
119
120         resp := rh.GetCharts()
121         assert.NotEqual(t, nil, resp)
122
123         successResp := resp.(*charts.GetChartsListOK)
124         assert.Equal(t, "SAMPLE_RESPONSE", successResp.Payload)
125 }
126
127 type HealthCheckerMock struct {
128         mock.Mock
129 }
130
131 func (h HealthCheckerMock) GetStatus() *models.Status {
132         return successStatus
133 }