type InvokerRegister interface {
IsInvokerRegistered(invokerId string) bool
VerifyInvokerSecret(invokerId, secret string) bool
+ GetInvokerApiList(invokerId string) *invokerapi.APIList
}
type InvokerManager struct {
return verified
}
+func (im *InvokerManager) GetInvokerApiList(invokerId string) *invokerapi.APIList {
+ invoker, ok := im.onboardedInvokers[invokerId]
+ if ok {
+ return invoker.ApiList
+ }
+ return nil
+}
+
func (im *InvokerManager) PostOnboardedInvokers(ctx echo.Context) error {
var newInvoker invokerapi.APIInvokerEnrolmentDetails
err := ctx.Bind(&newInvoker)
return sendCoreError(ctx, http.StatusBadRequest, "Invalid format for invoker")
}
- shouldReturn, coreError := im.validateInvoker(newInvoker, ctx)
- if shouldReturn {
+ coreError := im.validateInvoker(newInvoker, ctx)
+ if coreError != nil {
return coreError
}
return sendCoreError(ctx, http.StatusBadRequest, "Invoker ApiInvokerId not matching")
}
- shouldReturn, coreError := im.validateInvoker(invoker, ctx)
- if shouldReturn {
+ coreError := im.validateInvoker(invoker, ctx)
+ if coreError != nil {
return coreError
}
return ctx.NoContent(http.StatusNotImplemented)
}
-func (im *InvokerManager) validateInvoker(invoker invokerapi.APIInvokerEnrolmentDetails, ctx echo.Context) (bool, error) {
+func (im *InvokerManager) validateInvoker(invoker invokerapi.APIInvokerEnrolmentDetails, ctx echo.Context) error {
if invoker.NotificationDestination == "" {
- return true, sendCoreError(ctx, http.StatusBadRequest, "Invoker missing required NotificationDestination")
+ return sendCoreError(ctx, http.StatusBadRequest, "Invoker missing required NotificationDestination")
}
if invoker.OnboardingInformation.ApiInvokerPublicKey == "" {
- return true, sendCoreError(ctx, http.StatusBadRequest, "Invoker missing required OnboardingInformation.ApiInvokerPublicKey")
+ return sendCoreError(ctx, http.StatusBadRequest, "Invoker missing required OnboardingInformation.ApiInvokerPublicKey")
}
if !im.areAPIsRegistered(invoker.ApiList) {
- return true, sendCoreError(ctx, http.StatusBadRequest, "Some APIs needed by invoker are not registered")
+ return sendCoreError(ctx, http.StatusBadRequest, "Some APIs needed by invoker are not registered")
}
- return false, nil
+ return nil
}
func (im *InvokerManager) areAPIsRegistered(apis *invokerapi.APIList) bool {
"fmt"
"net/http"
"os"
+ "path"
"testing"
"oransc.org/nonrtric/capifcore/internal/invokermanagementapi"
)
func TestOnboardInvoker(t *testing.T) {
- var err error
- apiId := "apiId"
- aefId := "aefId"
apiRegisterMock := publishmocks.APIRegister{}
apiRegisterMock.On("AreAPIsRegistered", mock.Anything).Return(true)
invokerUnderTest, requestHandler := getEcho(&apiRegisterMock)
- description := "description"
- domainName := "domain"
- var protocol publishserviceapi.Protocol = "HTTP_1_1"
+ aefProfiles := []publishserviceapi.AefProfile{
+ getAefProfile("aefId"),
+ }
+ apiId := "apiId"
var apiList invokermanagementapi.APIList = []publishserviceapi.ServiceAPIDescription{
{
ApiId: &apiId,
- ApiName: "api",
- Description: &description,
- AefProfiles: &[]publishserviceapi.AefProfile{
- {
- AefId: aefId,
- DomainName: &domainName,
- Protocol: &protocol,
- Versions: []publishserviceapi.Version{
- {
- ApiVersion: "v1",
- Resources: &[]publishserviceapi.Resource{
- {
- ResourceName: "app",
- CommType: "REQUEST_RESPONSE",
- Uri: "uri",
- Operations: &[]publishserviceapi.Operation{
- "POST",
- },
- },
- },
- },
- },
- },
- },
- },
- }
- invokerInfo := "invoker a"
- newInvoker := invokermanagementapi.APIInvokerEnrolmentDetails{
- ApiInvokerInformation: &invokerInfo,
- NotificationDestination: "url",
- OnboardingInformation: invokermanagementapi.OnboardingInformation{
- ApiInvokerPublicKey: "key",
+ AefProfiles: &aefProfiles,
},
- ApiList: &apiList,
}
+ newInvoker := getInvoker("invoker a", apiList)
// Onboard a valid invoker
result := testutil.NewRequest().Post("/onboardedInvokers").WithJsonBody(newInvoker).Go(t, requestHandler)
assert.Equal(t, http.StatusCreated, result.Code())
var resultInvoker invokermanagementapi.APIInvokerEnrolmentDetails
- err = result.UnmarshalBodyToObject(&resultInvoker)
+ err := result.UnmarshalBodyToObject(&resultInvoker)
assert.NoError(t, err, "error unmarshaling response")
assert.Equal(t, "api_invoker_id_invoker_a", *resultInvoker.ApiInvokerId)
assert.Equal(t, newInvoker.NotificationDestination, resultInvoker.NotificationDestination)
}
func TestDeleteInvoker(t *testing.T) {
- _, requestHandler := getEcho(nil)
+ invokerUnderTest, requestHandler := getEcho(nil)
newInvoker := invokermanagementapi.APIInvokerEnrolmentDetails{
NotificationDestination: "url",
// Onboard an invoker
result := testutil.NewRequest().Post("/onboardedInvokers").WithJsonBody(newInvoker).Go(t, requestHandler)
- var resultInvoker invokermanagementapi.APIInvokerEnrolmentDetails
- result.UnmarshalBodyToObject(&resultInvoker)
invokerUrl := result.Recorder.Header().Get(echo.HeaderLocation)
+ assert.True(t, invokerUnderTest.IsInvokerRegistered(path.Base(invokerUrl)))
// Delete the invoker
result = testutil.NewRequest().Delete(invokerUrl).Go(t, requestHandler)
assert.Equal(t, http.StatusNoContent, result.Code())
+ assert.False(t, invokerUnderTest.IsInvokerRegistered(path.Base(invokerUrl)))
}
func TestUpdateInvoker(t *testing.T) {
assert.Equal(t, ¬Found, problemDetails.Status)
errMsg = "The invoker to update has not been onboarded"
assert.Equal(t, &errMsg, problemDetails.Cause)
+}
+
+func TestGetInvokerApiList(t *testing.T) {
+ apiRegisterMock := publishmocks.APIRegister{}
+ apiRegisterMock.On("AreAPIsRegistered", mock.Anything).Return(true)
+ invokerUnderTest, requestHandler := getEcho(&apiRegisterMock)
+
+ // Onboard two invokers
+ aefProfiles := []publishserviceapi.AefProfile{
+ getAefProfile("aefId"),
+ }
+ apiId := "apiId"
+ var apiList invokermanagementapi.APIList = []publishserviceapi.ServiceAPIDescription{
+ {
+ ApiId: &apiId,
+ AefProfiles: &aefProfiles,
+ },
+ }
+ newInvoker := getInvoker("invoker a", apiList)
+ testutil.NewRequest().Post("/onboardedInvokers").WithJsonBody(newInvoker).Go(t, requestHandler)
+ aefProfiles = []publishserviceapi.AefProfile{
+ getAefProfile("aefId2"),
+ }
+ apiId2 := "apiId2"
+ apiList = []publishserviceapi.ServiceAPIDescription{
+ {
+ ApiId: &apiId2,
+ AefProfiles: &aefProfiles,
+ },
+ }
+ newInvoker = getInvoker("invoker b", apiList)
+ testutil.NewRequest().Post("/onboardedInvokers").WithJsonBody(newInvoker).Go(t, requestHandler)
+ wantedApiList := invokerUnderTest.GetInvokerApiList("api_invoker_id_invoker_a")
+ assert.NotNil(t, wantedApiList)
+ assert.Equal(t, apiId, *(*wantedApiList)[0].ApiId)
}
func getEcho(apiRegister publishservice.APIRegister) (*InvokerManager, *echo.Echo) {
invokermanagementapi.RegisterHandlers(e, im)
return im, e
}
+
+func getAefProfile(aefId string) publishserviceapi.AefProfile {
+ return publishserviceapi.AefProfile{
+ AefId: aefId,
+ Versions: []publishserviceapi.Version{
+ {
+ Resources: &[]publishserviceapi.Resource{
+ {
+ CommType: "REQUEST_RESPONSE",
+ },
+ },
+ },
+ },
+ }
+}
+
+func getInvoker(invokerInfo string, apiList invokermanagementapi.APIList) invokermanagementapi.APIInvokerEnrolmentDetails {
+ newInvoker := invokermanagementapi.APIInvokerEnrolmentDetails{
+ ApiInvokerInformation: &invokerInfo,
+ NotificationDestination: "url",
+ OnboardingInformation: invokermanagementapi.OnboardingInformation{
+ ApiInvokerPublicKey: "key",
+ },
+ ApiList: &apiList,
+ }
+ return newInvoker
+}