Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support struct arguments with all optional fields #156

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions contractapi/contract_chaincode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,19 @@ func (tx *txHandler) Handler() {
// do nothing
}

type optionalFieldArg struct {
OptionalOne string `json:"optionalOne" metadata:",optional"`
OptionalTwo string `json:"optionalTwo" metadata:",optional"`
}

type optionalFieldsContract struct {
Contract
}

func (oc *optionalFieldsContract) TxFunction(arg *optionalFieldArg) (*optionalFieldArg, error) {
return arg, nil
}

func testContractChaincodeContractMatchesContract(t *testing.T, actual contractChaincodeContract, expected contractChaincodeContract) {
t.Helper()

Expand Down Expand Up @@ -644,6 +657,11 @@ func TestNewChaincode(t *testing.T) {
require.False(t, ok, "should not include ignored function")
}

func TestNewChaincodeOptionalFields(t *testing.T) {
_, err := NewChaincode(new(optionalFieldsContract))
require.NoError(t, err)
}

func TestStart(t *testing.T) {
mc := new(myContract)

Expand Down
2 changes: 1 addition & 1 deletion contractapi/mock_chaincodeStubInterface_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion metadata/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ type ContractMetadata struct {
type ObjectMetadata struct {
ID string `json:"$id"`
Properties map[string]spec.Schema `json:"properties"`
Required []string `json:"required"`
Required []string `json:"required,omitempty"`
AdditionalProperties bool `json:"additionalProperties"`
}

Expand Down
147 changes: 85 additions & 62 deletions metadata/metadata_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (

"github.com/go-openapi/spec"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/xeipuuv/gojsonschema"
)

Expand Down Expand Up @@ -339,75 +340,97 @@ func validateCompiledSchema(t *testing.T, propName string, propValue interface{}
}

func TestReadMetadataFile(t *testing.T) {
ContractMetaNumberOfCalls = 0
var metadata ContractChaincodeMetadata
var err error

oldOsHelper := osAbs

osAbs = osExcTestStr{}
metadata, err = ReadMetadataFile()
assert.EqualError(t, err, "failed to read metadata from file. Could not find location of executable. some error", "should error when cannot read file due to exec error")
assert.Equal(t, ContractChaincodeMetadata{}, metadata, "should return blank metadata when cannot read file due to exec error")

osAbs = osStatTestStr{}
metadata, err = ReadMetadataFile()
assert.EqualError(t, err, "failed to read metadata from file. Metadata file does not exist", "should error when cannot read file due to stat error")
assert.Equal(t, ContractChaincodeMetadata{}, metadata, "should return blank metadata when cannot read file due to stat error")

osAbs = osStatTestStrContractMeta{}
metadata, err = ReadMetadataFile()
assert.Equal(t, ContractMetaNumberOfCalls, 2, "Should check contract-metadata directory if META-INF doesn't contain metadata.json file")
assert.Contains(t, err.Error(), "failed to read metadata from file. Could not read file", "should error when cannot read file due to read error")
assert.Equal(t, ContractChaincodeMetadata{}, metadata, "should return blank metadata when cannot read file due to read error")
ContractMetaNumberOfCalls = 0

oldIoUtilHelper := ioutilAbs
osAbs = osWorkTestStr{}

ioutilAbs = ioUtilReadFileTestStr{}
metadata, err = ReadMetadataFile()
assert.Contains(t, err.Error(), "failed to read metadata from file. Could not read file", "should error when cannot read file due to read error")
assert.Equal(t, ContractChaincodeMetadata{}, metadata, "should return blank metadata when cannot read file due to read error")

ioutilAbs = ioUtilWorkTestStr{}
metadata, err = ReadMetadataFile()
assert.NoError(t, err, "should not return error when can read file")
metadataBytes := []byte("{\"info\":{\"title\":\"my contract\",\"version\":\"0.0.1\"},\"contracts\":{},\"components\":{}}")
expectedContractChaincodeMetadata := ContractChaincodeMetadata{}
err = json.Unmarshal(metadataBytes, &expectedContractChaincodeMetadata)
assert.NoError(t, err, "json unmarshal")
assert.Equal(t, expectedContractChaincodeMetadata, metadata, "should return contract metadata that was in the file")

osAbs = osWorkTestStrContractMeta{}
metadata, err = ReadMetadataFile()
assert.Equal(t, ContractMetaNumberOfCalls, 2, "Should check contract-metadata directory if META-INF doesn't contain metadata.json file")
assert.Nil(t, err, "should not return error when can read file")
assert.Equal(t, expectedContractChaincodeMetadata, metadata, "should return contract metadata that was in the file")
ContractMetaNumberOfCalls = 0

ioutilAbs = oldIoUtilHelper
osAbs = oldOsHelper
metadataBytes := []byte("{\"info\":{\"title\":\"my contract\",\"version\":\"0.0.1\"},\"contracts\":{},\"components\":{}}")
require.NoError(t, json.Unmarshal(metadataBytes, &expectedContractChaincodeMetadata))

t.Run("Exec error reading metadata file", func(t *testing.T) {
fakeOS(t, osExcTestStr{})

metadata, err := ReadMetadataFile()
assert.EqualError(t, err, "failed to read metadata from file. Could not find location of executable. some error", "should error when cannot read file due to exec error")
assert.Equal(t, ContractChaincodeMetadata{}, metadata, "should return blank metadata when cannot read file due to exec error")
})

t.Run("Stat error reading metadata file", func(t *testing.T) {
fakeOS(t, osStatTestStr{})

metadata, err := ReadMetadataFile()
assert.EqualError(t, err, "failed to read metadata from file. Metadata file does not exist", "should error when cannot read file due to stat error")
assert.Equal(t, ContractChaincodeMetadata{}, metadata, "should return blank metadata when cannot read file due to stat error")
})

t.Run("Error reading metadata file from contract-metadata directory", func(t *testing.T) {
fakeOS(t, osStatTestStrContractMeta{})
ContractMetaNumberOfCalls = 0

metadata, err := ReadMetadataFile()
assert.Equal(t, ContractMetaNumberOfCalls, 2, "Should check contract-metadata directory if META-INF doesn't contain metadata.json file")
assert.Contains(t, err.Error(), "failed to read metadata from file. Could not read file", "should error when cannot read file due to read error")
assert.Equal(t, ContractChaincodeMetadata{}, metadata, "should return blank metadata when cannot read file due to read error")
})

t.Run("Returns blank metadata on file read error", func(t *testing.T) {
fakeOS(t, osWorkTestStr{})
fakeIOUtil(t, ioUtilReadFileTestStr{})

metadata, err := ReadMetadataFile()
assert.Contains(t, err.Error(), "failed to read metadata from file. Could not read file", "should error when cannot read file due to read error")
assert.Equal(t, ContractChaincodeMetadata{}, metadata, "should return blank metadata when cannot read file due to read error")
})

t.Run("Returns contract metadata from META-INF", func(t *testing.T) {
fakeOS(t, osWorkTestStr{})
fakeIOUtil(t, ioUtilWorkTestStr{})

metadata, err := ReadMetadataFile()
assert.NoError(t, err, "should not return error when can read file")

assert.Equal(t, expectedContractChaincodeMetadata, metadata, "should return contract metadata that was in the file")
})

t.Run("Returns contract metadata from contract-metadata", func(t *testing.T) {
fakeOS(t, osWorkTestStrContractMeta{})
fakeIOUtil(t, ioUtilWorkTestStr{})
ContractMetaNumberOfCalls = 0

metadata, err := ReadMetadataFile()
assert.Equal(t, ContractMetaNumberOfCalls, 2, "Should check contract-metadata directory if META-INF doesn't contain metadata.json file")
assert.Nil(t, err, "should not return error when can read file")
assert.Equal(t, expectedContractChaincodeMetadata, metadata, "should return contract metadata that was in the file")
})
}

func TestValidateAgainstSchema(t *testing.T) {
var err error
fakeOS(t, osWorkTestStr{})
fakeIOUtil(t, ioUtilWorkTestStr{})

oldIoUtilHelper := ioutilAbs
oldOsHelper := osAbs
osAbs = osWorkTestStr{}
t.Run("Error on empty metadata", func(t *testing.T) {
err := ValidateAgainstSchema(ContractChaincodeMetadata{})
assert.EqualError(t, err, "cannot use metadata. Metadata did not match schema:\n1. (root): info is required\n2. contracts: Invalid type. Expected: object, given: null", "should error when metadata given does not match schema")
})

metadata := ContractChaincodeMetadata{}
t.Run("Valid metadata", func(t *testing.T) {
metadata, err := ReadMetadataFile()
require.NoError(t, err)

ioutilAbs = ioUtilWorkTestStr{}

err = ValidateAgainstSchema(metadata)
assert.EqualError(t, err, "cannot use metadata. Metadata did not match schema:\n1. (root): info is required\n2. contracts: Invalid type. Expected: object, given: null", "should error when metadata given does not match schema")
err = ValidateAgainstSchema(metadata)
assert.NoError(t, err, "should not error for valid metadata")
})
}

metadata, _ = ReadMetadataFile()
err = ValidateAgainstSchema(metadata)
assert.Nil(t, err, "should not error for valid metadata")
func fakeOS(t *testing.T, fake osInterface) {
previous := osAbs
t.Cleanup(func() {
osAbs = previous
})
osAbs = fake
}

ioutilAbs = oldIoUtilHelper
osAbs = oldOsHelper
func fakeIOUtil(t *testing.T, fake ioutilInterface) {
previous := ioutilAbs
t.Cleanup(func() {
ioutilAbs = previous
})
ioutilAbs = fake
}