Add generated code
[nonrtric/plt/sme.git] / internal / discoverservice / discoverservice_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 discoverservice
22
23 import (
24         "fmt"
25         "net/http"
26         "os"
27         "testing"
28
29         "oransc.org/nonrtric/sme/internal/discoverserviceapi"
30
31         "oransc.org/nonrtric/sme/internal/publishservice"
32
33         "github.com/labstack/echo/v4"
34
35         publishapi "oransc.org/nonrtric/sme/internal/publishserviceapi"
36
37         "oransc.org/nonrtric/sme/internal/publishservice/mocks"
38
39         "github.com/deepmap/oapi-codegen/pkg/middleware"
40         "github.com/deepmap/oapi-codegen/pkg/testutil"
41         echomiddleware "github.com/labstack/echo/v4/middleware"
42         "github.com/stretchr/testify/assert"
43 )
44
45 func TestGetAllServiceAPIs(t *testing.T) {
46         var err error
47
48         apiList := []publishapi.ServiceAPIDescription{
49                 getAPI("apiName1", "v1"),
50                 getAPI("apiName2", "v1"),
51         }
52         apiRegisterMock := mocks.APIRegister{}
53         apiRegisterMock.On("GetAPIs").Return(&apiList)
54         requestHandler := getEcho(&apiRegisterMock)
55
56         // Get all APIs, without any filter
57         result := testutil.NewRequest().Get("/allServiceAPIs?api-invoker-id=api_invoker_id").Go(t, requestHandler)
58
59         assert.Equal(t, http.StatusOK, result.Code())
60         var resultInvoker discoverserviceapi.DiscoveredAPIs
61         err = result.UnmarshalBodyToObject(&resultInvoker)
62         assert.NoError(t, err, "error unmarshaling response")
63         assert.Equal(t, 2, len(*resultInvoker.ServiceAPIDescriptions))
64         assert.Equal(t, "apiName1", (*resultInvoker.ServiceAPIDescriptions)[0].ApiName)
65         assert.Equal(t, "apiName2", (*resultInvoker.ServiceAPIDescriptions)[1].ApiName)
66         apiRegisterMock.AssertCalled(t, "GetAPIs")
67
68         // Get APIs with filter
69         result = testutil.NewRequest().Get("/allServiceAPIs?api-name=apiName1&api-version=v1&api-invoker-id=api_invoker_id").Go(t, requestHandler)
70
71         assert.Equal(t, http.StatusOK, result.Code())
72         err = result.UnmarshalBodyToObject(&resultInvoker)
73         assert.NoError(t, err, "error unmarshaling response")
74         assert.Equal(t, 1, len(*resultInvoker.ServiceAPIDescriptions))
75         assert.Equal(t, "apiName1", (*resultInvoker.ServiceAPIDescriptions)[0].ApiName)
76         apiRegisterMock.AssertCalled(t, "GetAPIs")
77 }
78
79 func getEcho(apiRegister publishservice.APIRegister) *echo.Echo {
80         swagger, err := discoverserviceapi.GetSwagger()
81         if err != nil {
82                 fmt.Fprintf(os.Stderr, "Error loading swagger spec\n: %s", err)
83                 os.Exit(1)
84         }
85
86         swagger.Servers = nil
87
88         ds := NewDiscoverService(apiRegister)
89
90         e := echo.New()
91         e.Use(echomiddleware.Logger())
92         e.Use(middleware.OapiRequestValidator(swagger))
93
94         discoverserviceapi.RegisterHandlers(e, ds)
95         return e
96 }
97
98 func getAPI(apiName, apiVersion string) publishapi.ServiceAPIDescription {
99         apiId := "apiId_" + apiName
100         aefId := "aefId"
101         description := "description"
102         domainName := "domain"
103         var protocol publishapi.Protocol = "HTTP_1_1"
104         return publishapi.ServiceAPIDescription{
105                 ApiId:       &apiId,
106                 ApiName:     apiName,
107                 Description: &description,
108                 AefProfiles: &[]publishapi.AefProfile{
109                         {
110                                 AefId:      aefId,
111                                 DomainName: &domainName,
112                                 Protocol:   &protocol,
113                                 Versions: []publishapi.Version{
114                                         {
115                                                 ApiVersion: apiVersion,
116                                                 Resources: &[]publishapi.Resource{
117                                                         {
118                                                                 ResourceName: "app",
119                                                                 CommType:     "REQUEST_RESPONSE",
120                                                                 Uri:          "uri",
121                                                                 Operations: &[]publishapi.Operation{
122                                                                         "POST",
123                                                                 },
124                                                         },
125                                                 },
126                                         },
127                                         {
128                                                 ApiVersion: "v2",
129                                                 Resources: &[]publishapi.Resource{
130                                                         {
131                                                                 ResourceName: "app",
132                                                                 CommType:     "REQUEST_RESPONSE",
133                                                                 Uri:          "uri",
134                                                                 Operations: &[]publishapi.Operation{
135                                                                         "POST",
136                                                                 },
137                                                         },
138                                                 },
139                                         },
140                                 },
141                         },
142                 },
143         }
144 }