NONRTRIC-946: Servicemanager - mock kong and capif as library
[nonrtric/plt/sme.git] / capifcore / cmd / main_test.go
1 // -
2 //   ========================LICENSE_START=================================
3 //   O-RAN-SC
4 //   %%
5 //   Copyright (C) 2022: Nordix Foundation. All rights reserved.
6 //   Copyright (C) 2023 OpenInfra Foundation Europe. All rights reserved.
7 //   %%
8 //   Licensed under the Apache License, Version 2.0 (the "License");
9 //   you may not use this file except in compliance with the License.
10 //   You may obtain a copy of the License at
11 //
12 //        http://www.apache.org/licenses/LICENSE-2.0
13 //
14 //   Unless required by applicable law or agreed to in writing, software
15 //   distributed under the License is distributed on an "AS IS" BASIS,
16 //   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 //   See the License for the specific language governing permissions and
18 //   limitations under the License.
19 //   ========================LICENSE_END===================================
20 //
21
22 package main
23
24 import (
25         "crypto/tls"
26         "fmt"
27         "io"
28         "net/http"
29         "testing"
30         "time"
31
32         "github.com/deepmap/oapi-codegen/pkg/testutil"
33         "github.com/getkin/kin-openapi/openapi3"
34         "github.com/labstack/echo/v4"
35         "github.com/stretchr/testify/assert"
36         "oransc.org/nonrtric/capifcore"
37         "oransc.org/nonrtric/capifcore/internal/common29122"
38 )
39
40 func Test_routing(t *testing.T) {
41         e := echo.New()
42         capifcore.RegisterHandlers(e, nil, nil)
43
44         type args struct {
45                 url          string
46                 returnStatus int
47                 method       string
48         }
49         tests := []struct {
50                 name string
51                 args args
52         }{
53                 {
54                         name: "Default path",
55                         args: args{
56                                 url:          "/",
57                                 returnStatus: http.StatusOK,
58                                 method:       "GET",
59                         },
60                 },
61                 {
62                         name: "Provider path",
63                         args: args{
64                                 url:          "/api-provider-management/v1/registrations/provider",
65                                 returnStatus: http.StatusNoContent,
66                                 method:       "DELETE",
67                         },
68                 },
69                 {
70                         name: "Publish path",
71                         args: args{
72                                 url:          "/published-apis/v1/apfId/service-apis/serviceId",
73                                 returnStatus: http.StatusNotFound,
74                                 method:       "GET",
75                         },
76                 },
77                 {
78                         name: "Discover path",
79                         args: args{
80                                 url:          "/service-apis/v1/allServiceAPIs?api-invoker-id=api_invoker_id",
81                                 returnStatus: http.StatusNotFound,
82                                 method:       "GET",
83                         },
84                 },
85                 {
86                         name: "Invoker path",
87                         args: args{
88                                 url:          "/api-invoker-management/v1/onboardedInvokers/invoker",
89                                 returnStatus: http.StatusNoContent,
90                                 method:       "DELETE",
91                         },
92                 },
93                 {
94                         name: "Event path",
95                         args: args{
96                                 url:          "/capif-events/v1/subscriberId/subscriptions/subId",
97                                 returnStatus: http.StatusNoContent,
98                                 method:       "DELETE",
99                         },
100                 },
101                 {
102                         name: "Security path",
103                         args: args{
104                                 url:          "/capif-security/v1/trustedInvokers/apiInvokerId",
105                                 returnStatus: http.StatusNotFound,
106                                 method:       "GET",
107                         },
108                 },
109         }
110         for _, tt := range tests {
111                 t.Run(tt.name, func(t *testing.T) {
112                         var result *testutil.CompletedRequest
113                         if tt.args.method == "GET" {
114                                 result = testutil.NewRequest().Get(tt.args.url).Go(t, e)
115                         } else if tt.args.method == "DELETE" {
116                                 result = testutil.NewRequest().Delete(tt.args.url).Go(t, e)
117                         }
118
119                         assert.Equal(t, tt.args.returnStatus, result.Code(), tt.name)
120                 })
121         }
122 }
123
124 func TestGetSwagger(t *testing.T) {
125         e := echo.New()
126         capifcore.RegisterHandlers(e, nil, nil)
127
128         type args struct {
129                 apiPath string
130                 apiName string
131         }
132         tests := []struct {
133                 name string
134                 args args
135         }{
136                 {
137                         name: "Provider api",
138                         args: args{
139                                 apiPath: "provider",
140                                 apiName: "Provider",
141                         },
142                 },
143                 {
144                         name: "Publish api",
145                         args: args{
146                                 apiPath: "publish",
147                                 apiName: "Publish",
148                         },
149                 },
150                 {
151                         name: "Invoker api",
152                         args: args{
153                                 apiPath: "invoker",
154                                 apiName: "Invoker",
155                         },
156                 },
157                 {
158                         name: "Events api",
159                         args: args{
160                                 apiPath: "events",
161                                 apiName: "Events",
162                         },
163                 },
164                 {
165                         name: "Discover api",
166                         args: args{
167                                 apiPath: "discover",
168                                 apiName: "Discover",
169                         },
170                 },
171                 {
172                         name: "Security api",
173                         args: args{
174                                 apiPath: "security",
175                                 apiName: "Security",
176                         },
177                 },
178         }
179         for _, tt := range tests {
180                 t.Run(tt.name, func(t *testing.T) {
181                         result := testutil.NewRequest().Get("/swagger/"+tt.args.apiPath).Go(t, e)
182                         assert.Equal(t, http.StatusOK, result.Code())
183                         var swaggerResponse openapi3.T
184                         err := result.UnmarshalJsonToObject(&swaggerResponse)
185                         assert.Nil(t, err)
186                         assert.Contains(t, swaggerResponse.Info.Title, tt.args.apiName)
187                 })
188         }
189         invalidApi := "foobar"
190         result := testutil.NewRequest().Get("/swagger/"+invalidApi).Go(t, e)
191         assert.Equal(t, http.StatusBadRequest, result.Code())
192         var errorResponse common29122.ProblemDetails
193         err := result.UnmarshalJsonToObject(&errorResponse)
194         assert.Nil(t, err)
195         assert.Contains(t, *errorResponse.Cause, "Invalid API")
196         assert.Contains(t, *errorResponse.Cause, invalidApi)
197 }
198
199 func TestHTTPSServer(t *testing.T) {
200         e := echo.New()
201         capifcore.RegisterHandlers(e, nil, nil)
202
203         var port = 44333
204         go startHttpsWebServer(e, 44333, "../certs/cert.pem", "../certs/key.pem") //"certs/test/cert.pem", "certs/test/key.pem"
205
206         time.Sleep(100 * time.Millisecond)
207
208         tr := &http.Transport{
209                 TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
210         }
211
212         client := &http.Client{Transport: tr}
213         res, err := client.Get(fmt.Sprintf("https://localhost:%d", port))
214         if err != nil {
215                 t.Fatal(err)
216         }
217
218         defer res.Body.Close()
219         assert.Equal(t, res.StatusCode, res.StatusCode)
220
221         body, err := io.ReadAll(res.Body)
222         if err != nil {
223                 t.Fatal(err)
224         }
225
226         expected := []byte("Hello, World!")
227         assert.Equal(t, expected, body)
228 }