// Publish a new API.
func (ps *PublishService) PostApfIdServiceApis(ctx echo.Context, apfId string) error {
var newServiceAPIDescription publishapi.ServiceAPIDescription
+ errorMsg := "Unable to register the service due to: %s "
err := ctx.Bind(&newServiceAPIDescription)
if err != nil {
- return sendCoreError(ctx, http.StatusBadRequest, "Invalid format for service "+apfId)
+ return sendCoreError(ctx, http.StatusBadRequest, fmt.Sprintf(errorMsg, "invalid format for service "+apfId))
}
+ if err := newServiceAPIDescription.Validate(); err != nil {
+ return sendCoreError(ctx, http.StatusBadRequest, fmt.Sprintf(errorMsg, err))
+ }
ps.lock.Lock()
defer ps.lock.Unlock()
registeredFuncs := ps.serviceRegister.GetAefsForPublisher(apfId)
for _, profile := range *newServiceAPIDescription.AefProfiles {
if !slices.Contains(registeredFuncs, profile.AefId) {
- return sendCoreError(ctx, http.StatusNotFound, fmt.Sprintf("Function %s not registered", profile.AefId))
+ return sendCoreError(ctx, http.StatusNotFound, fmt.Sprintf(errorMsg, fmt.Sprintf("function %s not registered", profile.AefId)))
}
}
}
func (ps *PublishService) checkProfilesRegistered(apfId string, updatedProfiles []publishapi.AefProfile) error {
-
registeredFuncs := ps.serviceRegister.GetAefsForPublisher(apfId)
for _, profile := range updatedProfiles {
if !slices.Contains(registeredFuncs, profile.AefId) {
}
}
return nil
-
}
// This function wraps sending of an error in the Error format, and
err := result.UnmarshalBodyToObject(&resultService)
assert.NoError(t, err, "error unmarshaling response")
newApiId := "api_id_" + apiName
- assert.Equal(t, *resultService.ApiId, newApiId)
+ assert.Equal(t, newApiId, *resultService.ApiId)
assert.Equal(t, "http://example.com/"+apfId+"/service-apis/"+*resultService.ApiId, result.Recorder.Header().Get(echo.HeaderLocation))
newServiceDescription.ApiId = &newApiId
wantedAPILIst := []publishapi.ServiceAPIDescription{newServiceDescription}
assert.Len(t, (*resultService.AefProfiles), 1)
assert.False(t, serviceUnderTest.IsAPIPublished("aefId", "path"))
+}
+
+func TestPublishInvalidService(t *testing.T) {
+ _, _, requestHandler := getEcho(nil, nil)
+ newServiceDescription := getServiceAPIDescription("aefId", " ", "description")
+
+ // Publish a service
+ result := testutil.NewRequest().Post("/apfId/service-apis").WithJsonBody(newServiceDescription).Go(t, requestHandler)
+
+ assert.Equal(t, http.StatusBadRequest, result.Code())
+ var resultError common29122.ProblemDetails
+ err := result.UnmarshalBodyToObject(&resultError)
+ assert.NoError(t, err, "error unmarshaling response")
+ assert.Contains(t, *resultError.Cause, "missing")
+ assert.Contains(t, *resultError.Cause, "apiName")
+ assert.Equal(t, http.StatusBadRequest, *resultError.Status)
+
}
func getEcho(serviceRegister providermanagement.ServiceRegister, helmManager helmmanagement.HelmManager) (*PublishService, chan eventsapi.EventNotification, *echo.Echo) {
swagger, err := publishapi.GetSwagger()
--- /dev/null
+package publishserviceapi
+
+import (
+ "errors"
+ //"fmt"
+ "strings"
+)
+
+func (sd ServiceAPIDescription) Validate() error {
+ if len(strings.TrimSpace(sd.ApiName)) == 0 {
+ return errors.New("ServiceAPIDescription missing required apiName")
+ }
+ return nil
+}
--- /dev/null
+package publishserviceapi
+
+import (
+ "testing"
+
+ "github.com/stretchr/testify/assert"
+)
+
+func TestValidate(t *testing.T) {
+ serviceDescriptionUnderTest := ServiceAPIDescription{}
+ err := serviceDescriptionUnderTest.Validate()
+ if assert.Error(t, err) {
+ assert.Contains(t, err.Error(), "missing")
+ assert.Contains(t, err.Error(), "apiName")
+ }
+
+ serviceDescriptionUnderTest.ApiName = "apiName"
+ err = serviceDescriptionUnderTest.Validate()
+ assert.Nil(t, err)
+
+}