|
1 | 1 | package api |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "encoding/json" |
| 5 | + "os" |
| 6 | + "path/filepath" |
4 | 7 | "testing" |
5 | 8 |
|
6 | 9 | "github.com/aep-dev/aep-lib-go/pkg/openapi" |
7 | 10 | "github.com/stretchr/testify/assert" |
| 11 | + "github.com/stretchr/testify/require" |
| 12 | + "gopkg.in/yaml.v3" |
8 | 13 | ) |
9 | 14 |
|
10 | 15 | var basicOpenAPI = &openapi.OpenAPI{ |
@@ -568,3 +573,68 @@ func TestGetAPI(t *testing.T) { |
568 | 573 | }) |
569 | 574 | } |
570 | 575 | } |
| 576 | + |
| 577 | +func TestParseBookstoreYAMLDirectly(t *testing.T) { |
| 578 | + // Construct the path relative to the test file's location |
| 579 | + yamlPath := filepath.Join("..", "..", "examples", "resource-definitions", "bookstore.yaml") |
| 580 | + |
| 581 | + // Read the YAML file |
| 582 | + yamlData, err := os.ReadFile(yamlPath) |
| 583 | + require.NoError(t, err, "Failed to read bookstore.yaml") |
| 584 | + require.NotEmpty(t, yamlData, "bookstore.yaml is empty") |
| 585 | + |
| 586 | + // Unmarshal YAML into a generic interface{} |
| 587 | + var genericYamlData interface{} |
| 588 | + err = yaml.Unmarshal(yamlData, &genericYamlData) |
| 589 | + require.NoError(t, err, "Failed to unmarshal YAML into generic interface") |
| 590 | + |
| 591 | + // Marshal the generic interface{} to JSON |
| 592 | + jsonData, err := json.Marshal(genericYamlData) |
| 593 | + require.NoError(t, err, "Failed to marshal generic interface to JSON") |
| 594 | + require.NotEmpty(t, jsonData, "Resulting JSON data is empty") |
| 595 | + |
| 596 | + // Attempt to unmarshal the JSON directly into api.API |
| 597 | + var apiResult API |
| 598 | + err = json.Unmarshal(jsonData, &apiResult) |
| 599 | + require.NoError(t, err, "Failed to unmarshal JSON into api.API struct") |
| 600 | + |
| 601 | + // Assert basic fields that might match (like Name, ServerURL, Contact) |
| 602 | + assert.Equal(t, "bookstore.example.com", apiResult.Name, "API Name should be populated if field names match") |
| 603 | + assert.Equal(t, "http://localhost:8081", apiResult.ServerURL, "API ServerURL should be populated based on json tag") |
| 604 | + if assert.NotNil(t, apiResult.Contact, "Contact might be populated if fields match") { |
| 605 | + assert.Equal(t, "API support", apiResult.Contact.Name) |
| 606 | + assert.Equal(t, "aepsupport@aep.dev", apiResult.Contact.Email) |
| 607 | + } |
| 608 | + |
| 609 | + // Assert that Resources map IS populated correctly |
| 610 | + assert.NotEmpty(t, apiResult.Resources, "Resources map should be populated") |
| 611 | + assert.Contains(t, apiResult.Resources, "publisher", "Resources map should contain 'publisher'") |
| 612 | + assert.Contains(t, apiResult.Resources, "book", "Resources map should contain 'book'") |
| 613 | + assert.Contains(t, apiResult.Resources, "book-edition", "Resources map should contain 'book-edition'") |
| 614 | + assert.Contains(t, apiResult.Resources, "isbn", "Resources map should contain 'isbn'") |
| 615 | + |
| 616 | + // Check some details of a resource |
| 617 | + publisherResource := apiResult.Resources["publisher"] |
| 618 | + assert.NotNil(t, publisherResource, "'publisher' resource should not be nil") |
| 619 | + assert.Equal(t, "publisher", publisherResource.Singular) |
| 620 | + assert.Equal(t, "publishers", publisherResource.Plural) |
| 621 | + assert.NotNil(t, publisherResource.Schema, "'publisher' resource schema should not be nil") |
| 622 | + assert.Equal(t, "object", publisherResource.Schema.Type) |
| 623 | + assert.Contains(t, publisherResource.Schema.Properties, "description") |
| 624 | + assert.Equal(t, "string", publisherResource.Schema.Properties["description"].Type) |
| 625 | + assert.NotNil(t, publisherResource.Methods.List, "'publisher' should have List method") |
| 626 | + assert.True(t, publisherResource.Methods.List.SupportsFilter) |
| 627 | + |
| 628 | + // Check book resource details, including custom method |
| 629 | + bookResource := apiResult.Resources["book"] |
| 630 | + assert.NotNil(t, bookResource, "'book' resource should not be nil") |
| 631 | + assert.Equal(t, "book", bookResource.Singular) |
| 632 | + assert.Equal(t, "books", bookResource.Plural) |
| 633 | + assert.NotNil(t, bookResource.Schema, "'book' resource schema should not be nil") |
| 634 | + assert.Contains(t, bookResource.Schema.Properties, "isbn") |
| 635 | + assert.Equal(t, "array", bookResource.Schema.Properties["isbn"].Type) |
| 636 | + assert.NotNil(t, bookResource.Methods.List, "'book' should have List method") |
| 637 | + assert.True(t, bookResource.Methods.List.HasUnreachableResources) |
| 638 | + assert.Len(t, bookResource.CustomMethods, 1, "'book' should have 1 custom method") |
| 639 | + assert.Equal(t, "archive", bookResource.CustomMethods[0].Name) |
| 640 | +} |
0 commit comments