NONRTRIC-946: Add support for Kong routes
[nonrtric/plt/sme.git] / servicemanager / main_test.go
1 // -
2 //   ========================LICENSE_START=================================
3 //   O-RAN-SC
4 //   %%
5 //   Copyright (C) 2023-2024: OpenInfra Foundation Europe
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         "os"
26         "testing"
27
28         "github.com/deepmap/oapi-codegen/pkg/testutil"
29         "github.com/getkin/kin-openapi/openapi3"
30         "github.com/labstack/echo/v4"
31         "github.com/stretchr/testify/assert"
32         log "github.com/sirupsen/logrus"
33
34         "oransc.org/nonrtric/servicemanager/internal/common29122"
35         "oransc.org/nonrtric/servicemanager/internal/envreader"
36 )
37
38 var e *echo.Echo
39 var myPorts map [string]int
40
41 func TestMain(m *testing.M) {
42     // Init code to run before tests
43         myEnv, myPorts, err := envreader.ReadDotEnv()
44         if err != nil {
45                 log.Fatal("error loading environment file")
46                 return
47         }
48
49         e, err = getEcho(myEnv, myPorts)
50         if err != nil {
51                 log.Fatal("getEcho fatal error")
52                 return
53         }
54     
55     // Run tests
56     exitVal := m.Run()
57     
58     // Finalization code to run after tests
59
60     // Exit with exit value from tests
61     os.Exit(exitVal)
62 }
63
64
65 func Test_routing(t *testing.T) {
66         type args struct {
67                 url          string
68                 returnStatus int
69                 method       string
70         }
71         tests := []struct {
72                 name string
73                 args args
74         }{
75                 {
76                         name: "Default path",
77                         args: args{
78                                 url:          "/",
79                                 returnStatus: http.StatusOK,
80                                 method:       "GET",
81                         },
82                 },
83                 {
84                         name: "Provider path",
85                         args: args{
86                                 url:          "/api-provider-management/v1/registrations/provider",
87                                 returnStatus: http.StatusNoContent,
88                                 method:       "DELETE",
89                         },
90                 },
91                 {
92                         name: "Publish path",
93                         args: args{
94                                 url:          "/published-apis/v1/apfId/service-apis/serviceId",
95                                 returnStatus: http.StatusNotFound,
96                                 method:       "GET",
97                         },
98                 },
99                 {
100                         name: "Discover path",
101                         args: args{
102                                 url:          "/service-apis/v1/allServiceAPIs?api-invoker-id=api_invoker_id",
103                                 returnStatus: http.StatusNotFound,
104                                 method:       "GET",
105                         },
106                 },
107                 {
108                         name: "Invoker path",
109                         args: args{
110                                 url:          "/api-invoker-management/v1/onboardedInvokers/invoker",
111                                 returnStatus: http.StatusNoContent,
112                                 method:       "DELETE",
113                         },
114                 },
115         }
116         for _, tt := range tests {
117                 t.Run(tt.name, func(t *testing.T) {
118                         var result *testutil.CompletedRequest
119                         if tt.args.method == "GET" {
120                                 result = testutil.NewRequest().Get(tt.args.url).Go(t, e)
121                         } else if tt.args.method == "DELETE" {
122                                 result = testutil.NewRequest().Delete(tt.args.url).Go(t, e)
123                         }
124
125                         assert.Equal(t, tt.args.returnStatus, result.Code(), tt.name)
126                 })
127         }
128 }
129
130 func TestGetSwagger(t *testing.T) {
131         type args struct {
132                 apiPath string
133                 apiName string
134         }
135         tests := []struct {
136                 name string
137                 args args
138         }{
139                 {
140                         name: "Provider api",
141                         args: args{
142                                 apiPath: "provider",
143                                 apiName: "Provider",
144                         },
145                 },
146                 {
147                         name: "Publish api",
148                         args: args{
149                                 apiPath: "publish",
150                                 apiName: "Publish",
151                         },
152                 },
153                 {
154                         name: "Invoker api",
155                         args: args{
156                                 apiPath: "invoker",
157                                 apiName: "Invoker",
158                         },
159                 },
160                 {
161                         name: "Discover api",
162                         args: args{
163                                 apiPath: "discover",
164                                 apiName: "Discover",
165                         },
166                 },
167         }
168         for _, tt := range tests {
169                 t.Run(tt.name, func(t *testing.T) {
170                         result := testutil.NewRequest().Get("/swagger/"+tt.args.apiPath).Go(t, e)
171                         assert.Equal(t, http.StatusOK, result.Code())
172                         var swaggerResponse openapi3.T
173                         err := result.UnmarshalJsonToObject(&swaggerResponse)
174                         assert.Nil(t, err)
175                         assert.Contains(t, swaggerResponse.Info.Title, tt.args.apiName)
176                 })
177         }
178         invalidApi := "foobar"
179         result := testutil.NewRequest().Get("/swagger/"+invalidApi).Go(t, e)
180         assert.Equal(t, http.StatusBadRequest, result.Code())
181         var errorResponse common29122.ProblemDetails
182         err := result.UnmarshalJsonToObject(&errorResponse)
183         assert.Nil(t, err)
184         assert.Contains(t, *errorResponse.Cause, "Invalid API")
185         assert.Contains(t, *errorResponse.Cause, invalidApi)
186 }