"oransc.org/nonrtric/capifcore/internal/common29122"
invokerapi "oransc.org/nonrtric/capifcore/internal/invokermanagementapi"
-
"oransc.org/nonrtric/capifcore/internal/publishservice"
- "github.com/labstack/echo/v4"
+ echo "github.com/labstack/echo/v4"
)
//go:generate mockery --name InvokerRegister
// Creates a new individual API Invoker profile.
func (im *InvokerManager) PostOnboardedInvokers(ctx echo.Context) error {
- var newInvoker invokerapi.APIInvokerEnrolmentDetails
errMsg := "Unable to onboard invoker due to %s"
- if err := ctx.Bind(&newInvoker); err != nil {
- return sendCoreError(ctx, http.StatusBadRequest, fmt.Sprintf(errMsg, "invalid format for invoker"))
+
+ newInvoker, err := getInvokerFromRequest(ctx)
+ if err != nil {
+ return sendCoreError(ctx, http.StatusBadRequest, fmt.Sprintf(errMsg, err))
}
- if err := im.isInvokerOnboarded(newInvoker); err != nil {
+ if err = im.isInvokerOnboarded(newInvoker); err != nil {
return sendCoreError(ctx, http.StatusForbidden, fmt.Sprintf(errMsg, err))
}
- if err := im.validateInvoker(newInvoker, ctx); err != nil {
+ if err = im.validateInvoker(newInvoker, ctx); err != nil {
return sendCoreError(ctx, http.StatusBadRequest, fmt.Sprintf(errMsg, err))
}
uri := ctx.Request().Host + ctx.Request().URL.String()
ctx.Response().Header().Set(echo.HeaderLocation, ctx.Scheme()+`://`+path.Join(uri, *newInvoker.ApiInvokerId))
- err := ctx.JSON(http.StatusCreated, newInvoker)
+
+ err = ctx.JSON(http.StatusCreated, newInvoker)
if err != nil {
// Something really bad happened, tell Echo that our handler failed
return err
delete(im.onboardedInvokers, onboardingId)
}
+func getInvokerFromRequest(ctx echo.Context) (invokerapi.APIInvokerEnrolmentDetails, error) {
+ var invoker invokerapi.APIInvokerEnrolmentDetails
+ if err := ctx.Bind(&invoker); err != nil {
+ return invokerapi.APIInvokerEnrolmentDetails{}, fmt.Errorf("invalid format for invoker")
+ }
+ return invoker, nil
+}
+
// Updates an individual API invoker details.
func (im *InvokerManager) PutOnboardedInvokersOnboardingId(ctx echo.Context, onboardingId string) error {
- var invoker invokerapi.APIInvokerEnrolmentDetails
errMsg := "Unable to update invoker due to %s"
- if err := ctx.Bind(&invoker); err != nil {
- return sendCoreError(ctx, http.StatusBadRequest, fmt.Sprintf(errMsg, "invalid format for invoker"))
+
+ newInvoker, err := getInvokerFromRequest(ctx)
+ if err != nil {
+ return sendCoreError(ctx, http.StatusBadRequest, fmt.Sprintf(errMsg, err))
}
- if onboardingId != *invoker.ApiInvokerId {
- return sendCoreError(ctx, http.StatusBadRequest, fmt.Sprintf(errMsg, "ApiInvokerId not matching"))
+ // Additional validation for PUT
+ if (newInvoker.ApiInvokerId == nil) || (*newInvoker.ApiInvokerId != onboardingId) {
+ errMismatch := "APIInvokerEnrolmentDetails ApiInvokerId doesn't match path parameter"
+ return sendCoreError(ctx, http.StatusBadRequest, fmt.Sprintf(errMsg, errMismatch))
}
- if err := im.validateInvoker(invoker, ctx); err != nil {
+ if err := im.validateInvoker(newInvoker, ctx); err != nil {
return sendCoreError(ctx, http.StatusBadRequest, fmt.Sprintf(errMsg, err))
}
if _, ok := im.onboardedInvokers[onboardingId]; ok {
- im.updateInvoker(invoker)
+ im.updateInvoker(newInvoker)
} else {
return sendCoreError(ctx, http.StatusNotFound, "The invoker to update has not been onboarded")
}
- err := ctx.JSON(http.StatusOK, invoker)
+ err = ctx.JSON(http.StatusOK, newInvoker)
if err != nil {
// Something really bad happened, tell Echo that our handler failed
return err
}
}
+func TestFailedUpdateInvoker(t *testing.T) {
+ publishRegisterMock := publishmocks.PublishRegister{}
+ publishRegisterMock.On("GetAllPublishedServices").Return([]publishserviceapi.ServiceAPIDescription{})
+ serviceUnderTest, _, requestHandler := getEcho(&publishRegisterMock, nil)
+
+ invokerInfo := "invoker a"
+ invokerId := "api_invoker_id_" + strings.Replace(invokerInfo, " ", "_", 1)
+
+ // Attempt to update with an invoker without the ApiInvokerId provided in the parameter body. We should get 400 with problem details.
+ invalidInvoker := getInvoker(invokerInfo)
+ serviceUnderTest.onboardedInvokers[invokerId] = invalidInvoker
+
+ result := testutil.NewRequest().Put("/onboardedInvokers/"+invokerId).WithJsonBody(invalidInvoker).Go(t, requestHandler)
+ assert.Equal(t, http.StatusBadRequest, result.Code())
+
+ var problemDetails common29122.ProblemDetails
+ err := result.UnmarshalBodyToObject(&problemDetails)
+ assert.NoError(t, err, "error unmarshaling response")
+ assert.Equal(t, http.StatusBadRequest, *problemDetails.Status)
+
+ assert.Contains(t, *problemDetails.Cause, "APIInvokerEnrolmentDetails ApiInvokerId doesn't match path parameter")
+}
+
func TestUpdateInvoker(t *testing.T) {
publishRegisterMock := publishmocks.PublishRegister{}
publishRegisterMock.On("GetAllPublishedServices").Return([]publishserviceapi.ServiceAPIDescription{})
err = result.UnmarshalBodyToObject(&problemDetails)
assert.NoError(t, err, "error unmarshaling response")
assert.Equal(t, http.StatusBadRequest, *problemDetails.Status)
- assert.Contains(t, *problemDetails.Cause, "not matching")
- assert.Contains(t, *problemDetails.Cause, "ApiInvokerId")
+ assert.Contains(t, *problemDetails.Cause, "APIInvokerEnrolmentDetails ApiInvokerId doesn't match path parameter")
// Update an invoker that has not been onboarded, should get 404 with problem details
missingId := "1"