import (
"net/http"
+ "oransc.org/nonrtric/capifcore/internal/common29122"
discoverapi "oransc.org/nonrtric/capifcore/internal/discoverserviceapi"
-
- "oransc.org/nonrtric/capifcore/internal/publishservice"
+ "oransc.org/nonrtric/capifcore/internal/invokermanagement"
"github.com/labstack/echo/v4"
)
type DiscoverService struct {
- apiRegister publishservice.APIRegister
+ invokerRegister invokermanagement.InvokerRegister
}
-func NewDiscoverService(apiRegister publishservice.APIRegister) *DiscoverService {
+func NewDiscoverService(invokerRegister invokermanagement.InvokerRegister) *DiscoverService {
return &DiscoverService{
- apiRegister: apiRegister,
+ invokerRegister: invokerRegister,
}
}
func (ds *DiscoverService) GetAllServiceAPIs(ctx echo.Context, params discoverapi.GetAllServiceAPIsParams) error {
- allApis := *ds.apiRegister.GetAPIs()
+ allApis := ds.invokerRegister.GetInvokerApiList(params.ApiInvokerId)
+ if allApis == nil {
+ return sendCoreError(ctx, http.StatusNotFound, "Invoker not registered")
+ }
filteredApis := []publishapi.ServiceAPIDescription{}
gatewayDomain := "r1-expo-func-aef"
- for _, api := range allApis {
+ for _, api := range *allApis {
if !matchesFilter(api, params) {
continue
}
return false
}
profiles := *api.AefProfiles
- aefIdMatch := true
- protocolMatch := true
- dataFormatMatch := true
- versionMatch := true
for _, profile := range profiles {
- if filter.AefId != nil {
- aefIdMatch = *filter.AefId == profile.AefId
- }
- if filter.ApiVersion != nil || filter.CommType != nil {
- versionMatch = checkVersionAndCommType(profile, filter.ApiVersion, filter.CommType)
- }
- if filter.Protocol != nil {
- protocolMatch = profile.Protocol != nil && *filter.Protocol == *profile.Protocol
- }
- if filter.DataFormat != nil {
- dataFormatMatch = profile.DataFormat != nil && *filter.DataFormat == *profile.DataFormat
- }
- if aefIdMatch && versionMatch && protocolMatch && dataFormatMatch {
+ if checkAefId(filter, profile) && checkVersionAndCommType(profile, filter) && checkProtocol(filter, profile) && checkDataFormat(filter, profile) {
return true
}
}
return false
}
-func checkVersionAndCommType(profile publishapi.AefProfile, wantedVersion *string, commType *publishapi.CommunicationType) bool {
+func checkAefId(filter discoverapi.GetAllServiceAPIsParams, profile publishapi.AefProfile) bool {
+ if filter.AefId != nil {
+ return *filter.AefId == profile.AefId
+ }
+ return true
+}
+
+func checkVersionAndCommType(profile publishapi.AefProfile, filter discoverapi.GetAllServiceAPIsParams) bool {
match := false
- if wantedVersion != nil {
+ if filter.ApiVersion != nil {
for _, version := range profile.Versions {
- match = checkVersion(version, wantedVersion, commType)
+ match = checkVersion(version, filter.ApiVersion, filter.CommType)
if match {
break
}
}
- } else if commType != nil {
+ } else if filter.CommType != nil {
for _, version := range profile.Versions {
- match = checkCommType(version.Resources, commType)
+ match = checkCommType(version.Resources, filter.CommType)
}
} else {
match = true
return match
}
+func checkProtocol(filter discoverapi.GetAllServiceAPIsParams, profile publishapi.AefProfile) bool {
+ if filter.Protocol != nil {
+ return profile.Protocol != nil && *filter.Protocol == *profile.Protocol
+ }
+ return true
+}
+
+func checkDataFormat(filter discoverapi.GetAllServiceAPIsParams, profile publishapi.AefProfile) bool {
+ if filter.DataFormat != nil {
+ return profile.DataFormat != nil && *filter.DataFormat == *profile.DataFormat
+ }
+ return true
+}
+
func checkVersion(version publishapi.Version, wantedVersion *string, commType *publishapi.CommunicationType) bool {
match := false
if *wantedVersion == version.ApiVersion {
}
return match
}
+
+// This function wraps sending of an error in the Error format, and
+// handling the failure to marshal that.
+func sendCoreError(ctx echo.Context, code int, message string) error {
+ pd := common29122.ProblemDetails{
+ Cause: &message,
+ Status: &code,
+ }
+ err := ctx.JSON(code, pd)
+ return err
+}
"os"
"testing"
+ "oransc.org/nonrtric/capifcore/internal/common29122"
"oransc.org/nonrtric/capifcore/internal/discoverserviceapi"
-
- "oransc.org/nonrtric/capifcore/internal/publishservice"
+ "oransc.org/nonrtric/capifcore/internal/invokermanagement"
+ "oransc.org/nonrtric/capifcore/internal/invokermanagementapi"
"github.com/labstack/echo/v4"
publishapi "oransc.org/nonrtric/capifcore/internal/publishserviceapi"
- "oransc.org/nonrtric/capifcore/internal/publishservice/mocks"
+ "oransc.org/nonrtric/capifcore/internal/invokermanagement/mocks"
"github.com/deepmap/oapi-codegen/pkg/middleware"
"github.com/deepmap/oapi-codegen/pkg/testutil"
getAPI("apiName1", "aefId", "apiCategory", "v1", nil, nil, ""),
getAPI("apiName2", "aefId", "apiCategory", "v1", nil, nil, ""),
}
- apiRegisterMock := mocks.APIRegister{}
- apiRegisterMock.On("GetAPIs").Return(&apiList)
- requestHandler := getEcho(&apiRegisterMock)
+ invokerId := "api_invoker_id"
+ invokerRegisterrMock := getInvokerRegisterMock(invokerId, apiList)
+ requestHandler := getEcho(invokerRegisterrMock)
// Get all APIs, without any filter
- result := testutil.NewRequest().Get("/allServiceAPIs?api-invoker-id=api_invoker_id").Go(t, requestHandler)
+ result := testutil.NewRequest().Get("/allServiceAPIs?api-invoker-id="+invokerId).Go(t, requestHandler)
assert.Equal(t, http.StatusOK, result.Code())
var resultInvoker discoverserviceapi.DiscoveredAPIs
assert.Equal(t, 2, len(*resultInvoker.ServiceAPIDescriptions))
assert.Equal(t, "apiName1", (*resultInvoker.ServiceAPIDescriptions)[0].ApiName)
assert.Equal(t, "apiName2", (*resultInvoker.ServiceAPIDescriptions)[1].ApiName)
- apiRegisterMock.AssertCalled(t, "GetAPIs")
assert.Equal(t, 2, len(*resultInvoker.ServiceAPIDescriptions))
}
+func TestGetAllServiceAPIsWhenMissingProvider(t *testing.T) {
+ invokerId := "unregistered"
+ invokerRegisterrMock := getInvokerRegisterMock(invokerId, nil)
+
+ requestHandler := getEcho(invokerRegisterrMock)
+
+ // Get all APIs, without any filter
+ result := testutil.NewRequest().Get("/allServiceAPIs?api-invoker-id="+invokerId).Go(t, requestHandler)
+
+ assert.Equal(t, http.StatusNotFound, result.Code())
+ var problemDetails common29122.ProblemDetails
+ err := result.UnmarshalBodyToObject(&problemDetails)
+ assert.NoError(t, err, "error unmarshaling response")
+ notFound := http.StatusNotFound
+ assert.Equal(t, ¬Found, problemDetails.Status)
+ errMsg := "Invoker not registered"
+ assert.Equal(t, &errMsg, problemDetails.Cause)
+}
+
func TestFilterApiName(t *testing.T) {
var err error
+ apiName := "apiName1"
apiList := []publishapi.ServiceAPIDescription{
- getAPI("apiName1", "", "", "", nil, nil, ""),
+ getAPI(apiName, "", "", "", nil, nil, ""),
getAPI("apiName2", "", "", "", nil, nil, ""),
}
- apiRegisterMock := mocks.APIRegister{}
- apiRegisterMock.On("GetAPIs").Return(&apiList)
- requestHandler := getEcho(&apiRegisterMock)
+ invokerId := "api_invoker_id"
+ invokerRegisterrMock := getInvokerRegisterMock(invokerId, apiList)
+ requestHandler := getEcho(invokerRegisterrMock)
// Get APIs with filter
- result := testutil.NewRequest().Get("/allServiceAPIs?api-name=apiName1&api-invoker-id=api_invoker_id").Go(t, requestHandler)
+ result := testutil.NewRequest().Get("/allServiceAPIs?api-invoker-id="+invokerId+"&api-name="+apiName).Go(t, requestHandler)
assert.Equal(t, http.StatusOK, result.Code())
var resultInvoker discoverserviceapi.DiscoveredAPIs
func TestFilterAefId(t *testing.T) {
var err error
+ aefId := "aefId"
apiList := []publishapi.ServiceAPIDescription{
- getAPI("apiName1", "aefId", "", "", nil, nil, ""),
+ getAPI("apiName1", aefId, "", "", nil, nil, ""),
getAPI("apiName2", "otherAefId", "", "", nil, nil, ""),
}
- apiRegisterMock := mocks.APIRegister{}
- apiRegisterMock.On("GetAPIs").Return(&apiList)
- requestHandler := getEcho(&apiRegisterMock)
+ invokerId := "api_invoker_id"
+ invokerRegisterrMock := getInvokerRegisterMock(invokerId, apiList)
+ requestHandler := getEcho(invokerRegisterrMock)
// Get APIs with filter
- result := testutil.NewRequest().Get("/allServiceAPIs?api-invoker-id=api_invoker_id&aef-id=aefId").Go(t, requestHandler)
+ result := testutil.NewRequest().Get("/allServiceAPIs?api-invoker-id="+invokerId+"&aef-id="+aefId).Go(t, requestHandler)
assert.Equal(t, http.StatusOK, result.Code())
var resultInvoker discoverserviceapi.DiscoveredAPIs
func TestFilterVersion(t *testing.T) {
var err error
+ version := "v1"
apiList := []publishapi.ServiceAPIDescription{
- getAPI("apiName1", "", "", "v1", nil, nil, ""),
+ getAPI("apiName1", "", "", version, nil, nil, ""),
getAPI("apiName2", "", "", "v2", nil, nil, ""),
}
- apiRegisterMock := mocks.APIRegister{}
- apiRegisterMock.On("GetAPIs").Return(&apiList)
- requestHandler := getEcho(&apiRegisterMock)
+ invokerId := "api_invoker_id"
+ invokerRegisterrMock := getInvokerRegisterMock(invokerId, apiList)
+ requestHandler := getEcho(invokerRegisterrMock)
// Get APIs with filter
- result := testutil.NewRequest().Get("/allServiceAPIs?api-version=v1&api-invoker-id=api_invoker_id").Go(t, requestHandler)
+ result := testutil.NewRequest().Get("/allServiceAPIs?api-invoker-id="+invokerId+"&api-version="+version).Go(t, requestHandler)
assert.Equal(t, http.StatusOK, result.Code())
var resultInvoker discoverserviceapi.DiscoveredAPIs
func TestFilterCommType(t *testing.T) {
var err error
+ commType := publishapi.CommunicationTypeREQUESTRESPONSE
apiList := []publishapi.ServiceAPIDescription{
- getAPI("apiName1", "", "", "", nil, nil, publishapi.CommunicationTypeREQUESTRESPONSE),
+ getAPI("apiName1", "", "", "", nil, nil, commType),
getAPI("apiName2", "", "", "", nil, nil, publishapi.CommunicationTypeSUBSCRIBENOTIFY),
}
- apiRegisterMock := mocks.APIRegister{}
- apiRegisterMock.On("GetAPIs").Return(&apiList)
- requestHandler := getEcho(&apiRegisterMock)
+ invokerId := "api_invoker_id"
+ invokerRegisterrMock := getInvokerRegisterMock(invokerId, apiList)
+ requestHandler := getEcho(invokerRegisterrMock)
// Get APIs with filter
- result := testutil.NewRequest().Get("/allServiceAPIs?api-invoker-id=api_invoker_id&comm-type=REQUEST_RESPONSE").Go(t, requestHandler)
+ result := testutil.NewRequest().Get("/allServiceAPIs?api-invoker-id="+invokerId+"&comm-type="+string(commType)).Go(t, requestHandler)
assert.Equal(t, http.StatusOK, result.Code())
var resultInvoker discoverserviceapi.DiscoveredAPIs
func TestFilterVersionAndCommType(t *testing.T) {
var err error
+ version := "v1"
+ commType := publishapi.CommunicationTypeSUBSCRIBENOTIFY
apiList := []publishapi.ServiceAPIDescription{
- getAPI("apiName1", "", "", "v1", nil, nil, publishapi.CommunicationTypeREQUESTRESPONSE),
- getAPI("apiName2", "", "", "v1", nil, nil, publishapi.CommunicationTypeSUBSCRIBENOTIFY),
- getAPI("apiName3", "", "", "v2", nil, nil, publishapi.CommunicationTypeSUBSCRIBENOTIFY),
+ getAPI("apiName1", "", "", version, nil, nil, publishapi.CommunicationTypeREQUESTRESPONSE),
+ getAPI("apiName2", "", "", version, nil, nil, commType),
+ getAPI("apiName3", "", "", "v2", nil, nil, commType),
}
- apiRegisterMock := mocks.APIRegister{}
- apiRegisterMock.On("GetAPIs").Return(&apiList)
- requestHandler := getEcho(&apiRegisterMock)
+ invokerId := "api_invoker_id"
+ invokerRegisterrMock := getInvokerRegisterMock(invokerId, apiList)
+ requestHandler := getEcho(invokerRegisterrMock)
// Get APIs with filter
- result := testutil.NewRequest().Get("/allServiceAPIs?api-invoker-id=api_invoker_id&api-version=v1&comm-type=SUBSCRIBE_NOTIFY").Go(t, requestHandler)
+ result := testutil.NewRequest().Get("/allServiceAPIs?api-invoker-id="+invokerId+"&api-version="+version+"&comm-type="+string(commType)).Go(t, requestHandler)
assert.Equal(t, http.StatusOK, result.Code())
var resultInvoker discoverserviceapi.DiscoveredAPIs
func TestFilterAPICategory(t *testing.T) {
var err error
+ apiCategory := "apiCategory"
apiList := []publishapi.ServiceAPIDescription{
- getAPI("apiName1", "", "apiCategory", "", nil, nil, ""),
+ getAPI("apiName1", "", apiCategory, "", nil, nil, ""),
getAPI("apiName2", "", "", "", nil, nil, ""),
}
- apiRegisterMock := mocks.APIRegister{}
- apiRegisterMock.On("GetAPIs").Return(&apiList)
- requestHandler := getEcho(&apiRegisterMock)
+ invokerId := "api_invoker_id"
+ invokerRegisterrMock := getInvokerRegisterMock(invokerId, apiList)
+ requestHandler := getEcho(invokerRegisterrMock)
// Get APIs with filter
- result := testutil.NewRequest().Get("/allServiceAPIs?api-invoker-id=api_invoker_id&api-cat=apiCategory").Go(t, requestHandler)
+ result := testutil.NewRequest().Get("/allServiceAPIs?api-invoker-id="+invokerId+"&api-cat="+apiCategory).Go(t, requestHandler)
assert.Equal(t, http.StatusOK, result.Code())
var resultInvoker discoverserviceapi.DiscoveredAPIs
getAPI("apiName1", "", "", "", &protocolHTTP11, nil, ""),
getAPI("apiName2", "", "", "", nil, nil, ""),
}
- apiRegisterMock := mocks.APIRegister{}
- apiRegisterMock.On("GetAPIs").Return(&apiList)
- requestHandler := getEcho(&apiRegisterMock)
+ invokerId := "api_invoker_id"
+ invokerRegisterrMock := getInvokerRegisterMock(invokerId, apiList)
+ requestHandler := getEcho(invokerRegisterrMock)
// Get APIs with filter
- result := testutil.NewRequest().Get("/allServiceAPIs?api-invoker-id=api_invoker_id&protocol=HTTP_1_1").Go(t, requestHandler)
+ result := testutil.NewRequest().Get("/allServiceAPIs?api-invoker-id="+invokerId+"&protocol="+string(protocolHTTP11)).Go(t, requestHandler)
assert.Equal(t, http.StatusOK, result.Code())
var resultInvoker discoverserviceapi.DiscoveredAPIs
getAPI("apiName1", "", "", "", nil, &dataFormatJSON, ""),
getAPI("apiName2", "", "", "", nil, nil, ""),
}
- apiRegisterMock := mocks.APIRegister{}
- apiRegisterMock.On("GetAPIs").Return(&apiList)
- requestHandler := getEcho(&apiRegisterMock)
+ invokerId := "api_invoker_id"
+ invokerRegisterrMock := getInvokerRegisterMock(invokerId, apiList)
+ requestHandler := getEcho(invokerRegisterrMock)
// Get APIs with filter
- result := testutil.NewRequest().Get("/allServiceAPIs?api-invoker-id=api_invoker_id&data-format=JSON").Go(t, requestHandler)
+ result := testutil.NewRequest().Get("/allServiceAPIs?api-invoker-id="+invokerId+"&data-format="+string(dataFormatJSON)).Go(t, requestHandler)
assert.Equal(t, http.StatusOK, result.Code())
var resultInvoker discoverserviceapi.DiscoveredAPIs
assert.Equal(t, "apiName1", (*resultInvoker.ServiceAPIDescriptions)[0].ApiName)
}
-func getEcho(apiRegister publishservice.APIRegister) *echo.Echo {
+func getEcho(invokerManager invokermanagement.InvokerRegister) *echo.Echo {
swagger, err := discoverserviceapi.GetSwagger()
if err != nil {
fmt.Fprintf(os.Stderr, "Error loading swagger spec\n: %s", err)
swagger.Servers = nil
- ds := NewDiscoverService(apiRegister)
+ ds := NewDiscoverService(invokerManager)
e := echo.New()
e.Use(echomiddleware.Logger())
return e
}
+func getInvokerRegisterMock(invokerId string, apisToReturn []publishapi.ServiceAPIDescription) *mocks.InvokerRegister {
+ apiList := invokermanagementapi.APIList(apisToReturn)
+ invokerRegisterrMock := mocks.InvokerRegister{}
+ if apisToReturn != nil {
+ invokerRegisterrMock.On("GetInvokerApiList", invokerId).Return(&apiList)
+ } else {
+ invokerRegisterrMock.On("GetInvokerApiList", invokerId).Return(nil)
+ }
+ return &invokerRegisterrMock
+}
+
func getAPI(apiName, aefId, apiCategory, apiVersion string, protocol *publishapi.Protocol, dataFormat *publishapi.DataFormat, commType publishapi.CommunicationType) publishapi.ServiceAPIDescription {
apiId := "apiId_" + apiName
description := "description"