Add Security Service to diagrams
[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: "Security path",
90                         args: args{
91                                 url:          "/capif-security/v1/trustedInvokers/apiInvokerId",
92                                 returnStatus: http.StatusNotImplemented,
93                                 method:       "GET",
94                         },
95                 },
96         }
97         for _, tt := range tests {
98                 t.Run(tt.name, func(t *testing.T) {
99                         var result *testutil.CompletedRequest
100                         if tt.args.method == "GET" {
101                                 result = testutil.NewRequest().Get(tt.args.url).Go(t, e)
102                         } else if tt.args.method == "DELETE" {
103                                 result = testutil.NewRequest().Delete(tt.args.url).Go(t, e)
104                         }
105
106                         assert.Equal(t, tt.args.returnStatus, result.Code(), tt.name)
107                 })
108         }
109 }
110
111 func TestGetSwagger(t *testing.T) {
112         e = getEcho()
113
114         type args struct {
115                 apiPath string
116                 apiName string
117         }
118         tests := []struct {
119                 name string
120                 args args
121         }{
122                 {
123                         name: "Provider api",
124                         args: args{
125                                 apiPath: "provider",
126                                 apiName: "Provider",
127                         },
128                 },
129                 {
130                         name: "Publish api",
131                         args: args{
132                                 apiPath: "publish",
133                                 apiName: "Publish",
134                         },
135                 },
136                 {
137                         name: "Invoker api",
138                         args: args{
139                                 apiPath: "invoker",
140                                 apiName: "Invoker",
141                         },
142                 },
143                 {
144                         name: "Discover api",
145                         args: args{
146                                 apiPath: "discover",
147                                 apiName: "Discover",
148                         },
149                 },
150                 {
151                         name: "Security api",
152                         args: args{
153                                 apiPath: "security",
154                                 apiName: "Security",
155                         },
156                 },
157         }
158         for _, tt := range tests {
159                 t.Run(tt.name, func(t *testing.T) {
160                         result := testutil.NewRequest().Get("/swagger/"+tt.args.apiPath).Go(t, e)
161                         assert.Equal(t, http.StatusOK, result.Code())
162                         var swaggerResponse openapi3.T
163                         err := result.UnmarshalJsonToObject(&swaggerResponse)
164                         assert.Nil(t, err)
165                         assert.Contains(t, swaggerResponse.Info.Title, tt.args.apiName)
166                 })
167         }
168         invalidApi := "foobar"
169         result := testutil.NewRequest().Get("/swagger/"+invalidApi).Go(t, e)
170         assert.Equal(t, http.StatusBadRequest, result.Code())
171         var errorResponse common29122.ProblemDetails
172         err := result.UnmarshalJsonToObject(&errorResponse)
173         assert.Nil(t, err)
174         assert.Contains(t, *errorResponse.Cause, "Invalid API")
175         assert.Contains(t, *errorResponse.Cause, invalidApi)
176 }