2 // ========================LICENSE_START=================================
5 // Copyright (C) 2022: Nordix Foundation
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
11 // http://www.apache.org/licenses/LICENSE-2.0
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===================================
21 package discoverservice
29 "oransc.org/nonrtric/capifcore/internal/discoverserviceapi"
31 "oransc.org/nonrtric/capifcore/internal/publishservice"
33 "github.com/labstack/echo/v4"
35 publishapi "oransc.org/nonrtric/capifcore/internal/publishserviceapi"
37 "oransc.org/nonrtric/capifcore/internal/publishservice/mocks"
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"
45 func TestGetAllServiceAPIs(t *testing.T) {
48 apiList := []publishapi.ServiceAPIDescription{
49 getAPI("apiName1", "v1"),
50 getAPI("apiName2", "v1"),
52 apiRegisterMock := mocks.APIRegister{}
53 apiRegisterMock.On("GetAPIs").Return(&apiList)
54 requestHandler := getEcho(&apiRegisterMock)
56 // Get all APIs, without any filter
57 result := testutil.NewRequest().Get("/allServiceAPIs?api-invoker-id=api_invoker_id").Go(t, requestHandler)
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")
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)
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")
79 func getEcho(apiRegister publishservice.APIRegister) *echo.Echo {
80 swagger, err := discoverserviceapi.GetSwagger()
82 fmt.Fprintf(os.Stderr, "Error loading swagger spec\n: %s", err)
88 ds := NewDiscoverService(apiRegister)
91 e.Use(echomiddleware.Logger())
92 e.Use(middleware.OapiRequestValidator(swagger))
94 discoverserviceapi.RegisterHandlers(e, ds)
98 func getAPI(apiName, apiVersion string) publishapi.ServiceAPIDescription {
99 apiId := "apiId_" + apiName
101 description := "description"
102 domainName := "domain"
103 var protocol publishapi.Protocol = "HTTP_1_1"
104 return publishapi.ServiceAPIDescription{
107 Description: &description,
108 AefProfiles: &[]publishapi.AefProfile{
111 DomainName: &domainName,
113 Versions: []publishapi.Version{
115 ApiVersion: apiVersion,
116 Resources: &[]publishapi.Resource{
119 CommType: "REQUEST_RESPONSE",
121 Operations: &[]publishapi.Operation{
129 Resources: &[]publishapi.Resource{
132 CommType: "REQUEST_RESPONSE",
134 Operations: &[]publishapi.Operation{