Skip to content

Commit

Permalink
Merge pull request #2 from mollie/feature/support-all-apis
Browse files Browse the repository at this point in the history
Feature/support all apis
  • Loading branch information
subzerobo authored Dec 17, 2024
2 parents 4572c44 + 1c62ceb commit 3fc483a
Show file tree
Hide file tree
Showing 23 changed files with 3,622 additions and 191 deletions.
32 changes: 28 additions & 4 deletions apis/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func (api *AdminAPI) CreateGlobalRule(ctx context.Context, rule models.Rule, lev
url := fmt.Sprintf("%s/admin/rules", api.Client.BaseURL)

// Prepare the request body
body := models.CreateUpdateGlobalRuleRequest{
body := models.CreateUpdateRuleRequest{
RuleType: rule,
Config: level,
}
Expand Down Expand Up @@ -81,7 +81,7 @@ func (api *AdminAPI) GetGlobalRule(ctx context.Context, rule models.Rule) (model
return "", err
}

var globalRule models.GlobalRuleResponse
var globalRule models.RuleResponse
if err := handleResponse(resp, http.StatusOK, &globalRule); err != nil {
return "", err
}
Expand All @@ -96,7 +96,7 @@ func (api *AdminAPI) UpdateGlobalRule(ctx context.Context, rule models.Rule, lev
url := fmt.Sprintf("%s/admin/rules/%s", api.Client.BaseURL, rule)

// Prepare the request body
body := models.CreateUpdateGlobalRuleRequest{
body := models.CreateUpdateRuleRequest{
RuleType: rule,
Config: level,
}
Expand All @@ -105,7 +105,7 @@ func (api *AdminAPI) UpdateGlobalRule(ctx context.Context, rule models.Rule, lev
return err
}

var globalRule models.GlobalRuleResponse
var globalRule models.RuleResponse
if err := handleResponse(resp, http.StatusOK, &globalRule); err != nil {
return err
}
Expand All @@ -126,6 +126,30 @@ func (api *AdminAPI) DeleteGlobalRule(ctx context.Context, rule models.Rule) err
return handleResponse(resp, http.StatusNoContent, nil)
}

// ListArtifactTypes Gets a list of all the currently configured artifact types (if any).
// GET admin/config/artifactTypes
// See https://www.apicur.io/registry/docs/apicurio-registry/3.0.x/assets-attachments/registry-rest-api.htm#tag/Artifact-Type/operation/listArtifactTypes
func (api *AdminAPI) ListArtifactTypes(ctx context.Context) ([]models.ArtifactType, error) {
url := fmt.Sprintf("%s/admin/config/artifactTypes", api.Client.BaseURL)
resp, err := api.executeRequest(ctx, http.MethodGet, url, nil)
if err != nil {
return nil, err
}

var artifactTypesResponse []models.ArtifactTypeResponse
if err := handleResponse(resp, http.StatusOK, &artifactTypesResponse); err != nil {
return nil, err
}

var artifactTypes []models.ArtifactType
for _, item := range artifactTypesResponse {
artifactTypes = append(artifactTypes, item.Name)
}

return artifactTypes, nil

}

// executeRequest handles the creation and execution of an HTTP request.
func (api *AdminAPI) executeRequest(ctx context.Context, method, url string, body interface{}) (*http.Response, error) {
var reqBody []byte
Expand Down
181 changes: 173 additions & 8 deletions apis/admin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,15 @@ const (
TitleInternalServerError = "Internal server error"
TitleNotFound = "Not found"
TitleConflict = "Conflict"
TitleMethodNotAllowed = "Method Not allowed"
)

func TestRulesAPI_ListGlobalRules(t *testing.T) {
func setupAdminAPIClient() *apis.AdminAPI {
apiClient := setupHTTPClient()
return apis.NewAdminAPI(apiClient)
}

func TestAdminAPI_ListGlobalRules(t *testing.T) {
t.Run("Success", func(t *testing.T) {
mockReferences := []models.Rule{models.RuleValidity, models.RuleCompatibility}
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
Expand Down Expand Up @@ -68,7 +74,7 @@ func TestRulesAPI_ListGlobalRules(t *testing.T) {
})
}

func TestRulesAPI_CreateGlobalRule(t *testing.T) {
func TestAdminAPI_CreateGlobalRule(t *testing.T) {
t.Run("Success", func(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
assert.Contains(t, r.URL.Path, "/admin/rules")
Expand Down Expand Up @@ -158,7 +164,7 @@ func TestRulesAPI_CreateGlobalRule(t *testing.T) {
})
}

func TestRulesAPI_DeleteAllGlobalRule(t *testing.T) {
func TestAdminAPI_DeleteAllGlobalRule(t *testing.T) {
t.Run("Success", func(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
assert.Contains(t, r.URL.Path, "/admin/rules")
Expand Down Expand Up @@ -200,9 +206,9 @@ func TestRulesAPI_DeleteAllGlobalRule(t *testing.T) {
})
}

func TestRulesAPI_GetGlobalRule(t *testing.T) {
func TestAdminAPI_GetGlobalRule(t *testing.T) {
t.Run("Success", func(t *testing.T) {
mockResponse := models.GlobalRuleResponse{
mockResponse := models.RuleResponse{
RuleType: models.RuleValidity,
Config: models.ValidityLevelFull,
}
Expand Down Expand Up @@ -276,9 +282,9 @@ func TestRulesAPI_GetGlobalRule(t *testing.T) {
})
}

func TestRulesAPI_UpdateGlobalRule(t *testing.T) {
func TestAdminAPI_UpdateGlobalRule(t *testing.T) {
t.Run("Success", func(t *testing.T) {
mockResponse := models.GlobalRuleResponse{
mockResponse := models.RuleResponse{
RuleType: models.RuleValidity,
Config: models.ValidityLevelFull,
}
Expand Down Expand Up @@ -349,7 +355,7 @@ func TestRulesAPI_UpdateGlobalRule(t *testing.T) {

}

func TestRulesAPI_DeleteGlobalRule(t *testing.T) {
func TestAdminAPI_DeleteGlobalRule(t *testing.T) {
t.Run("Success", func(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
assert.Contains(t, r.URL.Path, "/admin/rules/")
Expand Down Expand Up @@ -414,3 +420,162 @@ func TestRulesAPI_DeleteGlobalRule(t *testing.T) {
assert.Equal(t, TitleInternalServerError, apiErr.Title)
})
}

func TestAdminAPI_ListArtifactTypes(t *testing.T) {
t.Run("Success", func(t *testing.T) {
mockReferences := []models.ArtifactTypeResponse{
{Name: models.Avro},
{Name: models.Protobuf},
}
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
assert.Contains(t, r.URL.Path, "admin/config/artifactTypes")
assert.Equal(t, http.MethodGet, r.Method)

w.WriteHeader(http.StatusOK)
err := json.NewEncoder(w).Encode(mockReferences)
assert.NoError(t, err)
}))
defer server.Close()

mockClient := &client.Client{BaseURL: server.URL, HTTPClient: server.Client()}
api := apis.NewAdminAPI(mockClient)

result, err := api.ListArtifactTypes(context.Background())
assert.NoError(t, err)
assert.NotNil(t, result)
assert.Len(t, result, 2)
})

}

/***********************/
/***** Integration *****/
/***********************/

func TestAdminAPI_Rules_Integration(t *testing.T) {
if testing.Short() {
t.Skip("skipping integration test")
}

ctx := context.Background()
adminAPI := setupAdminAPIClient()

// Test ListGlobalRules
t.Run("ListGlobalRulesToGetEmptyResults", func(t *testing.T) {
// Delete all rules
err := adminAPI.DeleteAllGlobalRule(ctx)
assert.NoError(t, err)

rules, err := adminAPI.ListGlobalRules(ctx)
assert.NoError(t, err)
assert.NotNil(t, rules)
})

// Test CreateGlobalRule
t.Run("CreateGlobalValidityRuleAndCheckIfItApplied", func(t *testing.T) {
// Delete all rules
err := adminAPI.DeleteAllGlobalRule(ctx)
assert.NoError(t, err)

// Create a new rule
err = adminAPI.CreateGlobalRule(ctx, models.RuleValidity, models.ValidityLevelFull)
assert.NoError(t, err)

// Verify rule creation
rules, err := adminAPI.ListGlobalRules(ctx)
assert.NoError(t, err)
assert.Contains(t, rules, models.RuleValidity)
})

// Test GetGlobalRule
t.Run("GetGlobalRule", func(t *testing.T) {
// Delete all rules
err := adminAPI.DeleteAllGlobalRule(ctx)
assert.NoError(t, err)

// Create a new rule
err = adminAPI.CreateGlobalRule(ctx, models.RuleValidity, models.ValidityLevelFull)
assert.NoError(t, err)

level, err := adminAPI.GetGlobalRule(ctx, models.RuleValidity)
assert.NoError(t, err)
assert.Equal(t, models.ValidityLevelFull, level)
})

// Test UpdateGlobalRule
t.Run("UpdateGlobalRule", func(t *testing.T) {
// Delete all rules
err := adminAPI.DeleteAllGlobalRule(ctx)
assert.NoError(t, err)

// Create a new rule
err = adminAPI.CreateGlobalRule(ctx, models.RuleValidity, models.ValidityLevelFull)
assert.NoError(t, err)

// Update the rule
err = adminAPI.UpdateGlobalRule(ctx, models.RuleValidity, models.ValidityLevelSyntaxOnly)
assert.NoError(t, err)

// Verify rule update
level, err := adminAPI.GetGlobalRule(ctx, models.RuleValidity)
assert.NoError(t, err)
assert.Equal(t, models.ValidityLevelSyntaxOnly, level)
})

// Test DeleteGlobalRule
t.Run("DeleteGlobalRuleAndDeleteAllGlobalRules", func(t *testing.T) {
// Delete all rules
err := adminAPI.DeleteAllGlobalRule(ctx)
assert.NoError(t, err)

// Create a new rules
err = adminAPI.CreateGlobalRule(ctx, models.RuleValidity, models.ValidityLevelFull)
assert.NoError(t, err)

err = adminAPI.CreateGlobalRule(ctx, models.RuleCompatibility, models.CompatibilityLevelFull)
assert.NoError(t, err)

err = adminAPI.CreateGlobalRule(ctx, models.RuleIntegrity, models.IntegrityLevelFull)
assert.NoError(t, err)

// Delete the validity rule
err = adminAPI.DeleteGlobalRule(ctx, models.RuleValidity)
assert.NoError(t, err)

// Verify rule deletion
rules, err := adminAPI.ListGlobalRules(ctx)
assert.NoError(t, err)
assert.Equal(t, 2, len(rules))
assert.NotContains(t, rules, models.RuleValidity)

// Delete all rules again
// Delete all rules
err = adminAPI.DeleteAllGlobalRule(ctx)
assert.NoError(t, err)

// Verify all rules are deleted
rules, err = adminAPI.ListGlobalRules(ctx)
assert.NoError(t, err)
assert.Empty(t, rules)

})

// List Artifact Types
t.Run("ListArtifactTypes", func(t *testing.T) {
list, err := adminAPI.ListArtifactTypes(ctx)
assert.NoError(t, err)
assert.NotNil(t, list)
assert.NotEmpty(t, list)
assert.Contains(t, list, models.Protobuf)
assert.Contains(t, list, models.OpenAPI)
assert.Contains(t, list, models.AsyncAPI)
assert.Contains(t, list, models.Json)
assert.Contains(t, list, models.Avro)
assert.Contains(t, list, models.GraphQL)
assert.Contains(t, list, models.KConnect)
assert.Contains(t, list, models.WSDL)
assert.Contains(t, list, models.XSD)
assert.Contains(t, list, models.XML)

})
}
Loading

0 comments on commit 3fc483a

Please sign in to comment.