f174fe31dd9d8e7b9094197f82f290691240e958
[nonrtric/plt/sme.git] / capifcore / 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/internal/common29122"
37 )
38
39 var e *echo.Echo
40
41 func Test_routing(t *testing.T) {
42         e = getEcho()
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 = getEcho()
126
127         type args struct {
128                 apiPath string
129                 apiName string
130         }
131         tests := []struct {
132                 name string
133                 args args
134         }{
135                 {
136                         name: "Provider api",
137                         args: args{
138                                 apiPath: "provider",
139                                 apiName: "Provider",
140                         },
141                 },
142                 {
143                         name: "Publish api",
144                         args: args{
145                                 apiPath: "publish",
146                                 apiName: "Publish",
147                         },
148                 },
149                 {
150                         name: "Invoker api",
151                         args: args{
152                                 apiPath: "invoker",
153                                 apiName: "Invoker",
154                         },
155                 },
156                 {
157                         name: "Events api",
158                         args: args{
159                                 apiPath: "events",
160                                 apiName: "Events",
161                         },
162                 },
163                 {
164                         name: "Discover api",
165                         args: args{
166                                 apiPath: "discover",
167                                 apiName: "Discover",
168                         },
169                 },
170                 {
171                         name: "Security api",
172                         args: args{
173                                 apiPath: "security",
174                                 apiName: "Security",
175                         },
176                 },
177         }
178         for _, tt := range tests {
179                 t.Run(tt.name, func(t *testing.T) {
180                         result := testutil.NewRequest().Get("/swagger/"+tt.args.apiPath).Go(t, e)
181                         assert.Equal(t, http.StatusOK, result.Code())
182                         var swaggerResponse openapi3.T
183                         err := result.UnmarshalJsonToObject(&swaggerResponse)
184                         assert.Nil(t, err)
185                         assert.Contains(t, swaggerResponse.Info.Title, tt.args.apiName)
186                 })
187         }
188         invalidApi := "foobar"
189         result := testutil.NewRequest().Get("/swagger/"+invalidApi).Go(t, e)
190         assert.Equal(t, http.StatusBadRequest, result.Code())
191         var errorResponse common29122.ProblemDetails
192         err := result.UnmarshalJsonToObject(&errorResponse)
193         assert.Nil(t, err)
194         assert.Contains(t, *errorResponse.Cause, "Invalid API")
195         assert.Contains(t, *errorResponse.Cause, invalidApi)
196 }
197
198 func TestHTTPSServer(t *testing.T) {
199         e = getEcho()
200         var port = 44333
201         go startHttpsWebServer(e, 44333, "certs/cert.pem", "certs/key.pem") //"certs/test/cert.pem", "certs/test/key.pem"
202
203         time.Sleep(100 * time.Millisecond)
204
205         tr := &http.Transport{
206                 TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
207         }
208
209         client := &http.Client{Transport: tr}
210         res, err := client.Get(fmt.Sprintf("https://localhost:%d", port))
211         if err != nil {
212                 t.Fatal(err)
213         }
214
215         defer res.Body.Close()
216         assert.Equal(t, res.StatusCode, res.StatusCode)
217
218         body, err := io.ReadAll(res.Body)
219         if err != nil {
220                 t.Fatal(err)
221         }
222
223         expected := []byte("Hello, World!")
224         assert.Equal(t, expected, body)
225 }