Skip to content

Commit

Permalink
Catalog extension tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tschaub committed May 17, 2024
1 parent 9d3c3b8 commit 2107d9a
Showing 1 changed file with 134 additions and 0 deletions.
134 changes: 134 additions & 0 deletions catalog_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ package stac_test

import (
"encoding/json"
"regexp"
"testing"

"github.com/mitchellh/mapstructure"
"github.com/planetlabs/go-stac"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -38,3 +40,135 @@ func TestCatalogMarshal(t *testing.T) {

assert.JSONEq(t, expected, string(data))
}

const (
extensionAlias = "test-catalog-extension"
extensionUri = "https://example.com/test-catalog-extension/v1.0.0/schema.json"
extensionPattern = `https://example.com/test-catalog-extension/v1\..*/schema.json`
)

type CatalogExtension struct {
RequiredNum float64 `json:"required_num"`
OptionalBool *bool `json:"optional_bool,omitempty"`
}

var _ stac.Extension = (*CatalogExtension)(nil)

func (*CatalogExtension) URI() string {
return extensionUri
}

func (e *CatalogExtension) Encode(catalogMap map[string]any) error {
extendedProps := map[string]any{}
encoder, err := mapstructure.NewDecoder(&mapstructure.DecoderConfig{
TagName: "json",
Result: &extendedProps,
})
if err != nil {
return err
}
if err := encoder.Decode(e); err != nil {
return err
}
catalogMap[extensionAlias] = extendedProps
return nil
}

func (e *CatalogExtension) Decode(catalogMap map[string]any) error {
extendedProps, present := catalogMap[extensionAlias]
if !present {
return nil
}

decoder, err := mapstructure.NewDecoder(&mapstructure.DecoderConfig{
TagName: "json",
Result: e,
})
if err != nil {
return err
}
return decoder.Decode(extendedProps)
}

func TestExtendedCatalogMarshal(t *testing.T) {
stac.RegisterCatalogExtension(
regexp.MustCompile(extensionPattern),
func() stac.Extension {
return &CatalogExtension{}
},
)

catalog := &stac.Catalog{
Description: "Test catalog with extension",
Id: "catalog-id",
Extensions: []stac.Extension{
&CatalogExtension{
RequiredNum: 42,
},
},
Links: []*stac.Link{},
Version: "1.2.3",
}

data, err := json.Marshal(catalog)
require.NoError(t, err)

expected := `{
"type": "Catalog",
"description": "Test catalog with extension",
"id": "catalog-id",
"test-catalog-extension": {
"required_num": 42
},
"links": [],
"stac_extensions": [
"https://example.com/test-catalog-extension/v1.0.0/schema.json"
],
"stac_version": "1.2.3"
}`

assert.JSONEq(t, expected, string(data))
}

func TestExtendedCatalogUnmarshal(t *testing.T) {
stac.RegisterCatalogExtension(
regexp.MustCompile(extensionPattern),
func() stac.Extension {
return &CatalogExtension{}
},
)

data := []byte(`{
"type": "Catalog",
"description": "Test catalog with extension",
"id": "catalog-id",
"test-catalog-extension": {
"required_num": 100,
"optional_bool": true
},
"links": [],
"stac_extensions": [
"https://example.com/test-catalog-extension/v1.0.0/schema.json"
],
"stac_version": "1.2.3"
}`)

catalog := &stac.Catalog{}
require.NoError(t, json.Unmarshal(data, catalog))

b := true
expected := &stac.Catalog{
Description: "Test catalog with extension",
Id: "catalog-id",
Extensions: []stac.Extension{
&CatalogExtension{
RequiredNum: 100,
OptionalBool: &b,
},
},
Links: []*stac.Link{},
Version: "1.2.3",
}

assert.Equal(t, expected, catalog)
}

0 comments on commit 2107d9a

Please sign in to comment.