Merge "Improve validation for invoker management"
[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         "net/http"
25         "testing"
26
27         "github.com/deepmap/oapi-codegen/pkg/testutil"
28         "github.com/getkin/kin-openapi/openapi3"
29         "github.com/labstack/echo/v4"
30         "github.com/stretchr/testify/assert"
31         "oransc.org/nonrtric/capifcore/internal/common29122"
32 )
33
34 var e *echo.Echo
35
36 func Test_routing(t *testing.T) {
37         e = getEcho()
38
39         type args struct {
40                 url          string
41                 returnStatus int
42                 method       string
43         }
44         tests := []struct {
45                 name string
46                 args args
47         }{
48                 {
49                         name: "Default path",
50                         args: args{
51                                 url:          "/",
52                                 returnStatus: http.StatusOK,
53                                 method:       "GET",
54                         },
55                 },
56                 {
57                         name: "Provider path",
58                         args: args{
59                                 url:          "/api-provider-management/v1/registrations/provider",
60                                 returnStatus: http.StatusNoContent,
61                                 method:       "DELETE",
62                         },
63                 },
64                 {
65                         name: "Publish path",
66                         args: args{
67                                 url:          "/published-apis/v1/apfId/service-apis/serviceId",
68                                 returnStatus: http.StatusNotFound,
69                                 method:       "GET",
70                         },
71                 },
72                 {
73                         name: "Discover path",
74                         args: args{
75                                 url:          "/service-apis/v1/allServiceAPIs?api-invoker-id=api_invoker_id",
76                                 returnStatus: http.StatusNotFound,
77                                 method:       "GET",
78                         },
79                 },
80                 {
81                         name: "Invoker path",
82                         args: args{
83                                 url:          "/api-invoker-management/v1/onboardedInvokers/invoker",
84                                 returnStatus: http.StatusNoContent,
85                                 method:       "DELETE",
86                         },
87                 },
88                 {
89                         name: "Event path",
90                         args: args{
91                                 url:          "/capif-events/v1/subscriberId/subscriptions/subId",
92                                 returnStatus: http.StatusNoContent,
93                                 method:       "DELETE",
94                         },
95                 },
96                 {
97                         name: "Security path",
98                         args: args{
99                                 url:          "/capif-security/v1/trustedInvokers/apiInvokerId",
100                                 returnStatus: http.StatusNotImplemented,
101                                 method:       "GET",
102                         },
103                 },
104         }
105         for _, tt := range tests {
106                 t.Run(tt.name, func(t *testing.T) {
107                         var result *testutil.CompletedRequest
108                         if tt.args.method == "GET" {
109                                 result = testutil.NewRequest().Get(tt.args.url).Go(t, e)
110                         } else if tt.args.method == "DELETE" {
111                                 result = testutil.NewRequest().Delete(tt.args.url).Go(t, e)
112                         }
113
114                         assert.Equal(t, tt.args.returnStatus, result.Code(), tt.name)
115                 })
116         }
117 }
118
119 func TestGetSwagger(t *testing.T) {
120         e = getEcho()
121
122         type args struct {
123                 apiPath string
124                 apiName string
125         }
126         tests := []struct {
127                 name string
128                 args args
129         }{
130                 {
131                         name: "Provider api",
132                         args: args{
133                                 apiPath: "provider",
134                                 apiName: "Provider",
135                         },
136                 },
137                 {
138                         name: "Publish api",
139                         args: args{
140                                 apiPath: "publish",
141                                 apiName: "Publish",
142                         },
143                 },
144                 {
145                         name: "Invoker api",
146                         args: args{
147                                 apiPath: "invoker",
148                                 apiName: "Invoker",
149                         },
150                 },
151                 {
152                         name: "Events api",
153                         args: args{
154                                 apiPath: "events",
155                                 apiName: "Events",
156                         },
157                 },
158                 {
159                         name: "Discover api",
160                         args: args{
161                                 apiPath: "discover",
162                                 apiName: "Discover",
163                         },
164                 },
165                 {
166                         name: "Security api",
167                         args: args{
168                                 apiPath: "security",
169                                 apiName: "Security",
170                         },
171                 },
172         }
173         for _, tt := range tests {
174                 t.Run(tt.name, func(t *testing.T) {
175                         result := testutil.NewRequest().Get("/swagger/"+tt.args.apiPath).Go(t, e)
176                         assert.Equal(t, http.StatusOK, result.Code())
177                         var swaggerResponse openapi3.T
178                         err := result.UnmarshalJsonToObject(&swaggerResponse)
179                         assert.Nil(t, err)
180                         assert.Contains(t, swaggerResponse.Info.Title, tt.args.apiName)
181                 })
182         }
183         invalidApi := "foobar"
184         result := testutil.NewRequest().Get("/swagger/"+invalidApi).Go(t, e)
185         assert.Equal(t, http.StatusBadRequest, result.Code())
186         var errorResponse common29122.ProblemDetails
187         err := result.UnmarshalJsonToObject(&errorResponse)
188         assert.Nil(t, err)
189         assert.Contains(t, *errorResponse.Cause, "Invalid API")
190         assert.Contains(t, *errorResponse.Cause, invalidApi)
191 }