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