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