code.vegaprotocol.io/vega@v0.79.0/core/datasource/spec/adaptors/json_test.go (about)

     1  // Copyright (C) 2023 Gobalsky Labs Limited
     2  //
     3  // This program is free software: you can redistribute it and/or modify
     4  // it under the terms of the GNU Affero General Public License as
     5  // published by the Free Software Foundation, either version 3 of the
     6  // License, or (at your option) any later version.
     7  //
     8  // This program is distributed in the hope that it will be useful,
     9  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    10  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    11  // GNU Affero General Public License for more details.
    12  //
    13  // You should have received a copy of the GNU Affero General Public License
    14  // along with this program.  If not, see <http://www.gnu.org/licenses/>.
    15  
    16  package adaptors_test
    17  
    18  import (
    19  	"encoding/hex"
    20  	"encoding/json"
    21  	"fmt"
    22  	"testing"
    23  
    24  	"code.vegaprotocol.io/vega/core/datasource/spec/adaptors"
    25  	"code.vegaprotocol.io/vega/libs/crypto"
    26  	datapb "code.vegaprotocol.io/vega/protos/vega/data/v1"
    27  
    28  	"github.com/stretchr/testify/assert"
    29  	"github.com/stretchr/testify/require"
    30  )
    31  
    32  func TestJSONAdaptor(t *testing.T) {
    33  	t.Run("Normalising incompatible data fails", testJSONAdaptorNormalisingIncompatibleDataFails)
    34  	t.Run("Normalising compatible and valid data succeeds", testJSONAdaptorNormalisingCompatibleAndValidDataSucceeds)
    35  }
    36  
    37  func testJSONAdaptorNormalisingIncompatibleDataFails(t *testing.T) {
    38  	// given
    39  	pubKeyB := []byte("0xdeadbeef")
    40  	pubKey := crypto.NewPublicKey(hex.EncodeToString(pubKeyB), pubKeyB)
    41  	rawData, _ := json.Marshal(struct {
    42  		Prices       string
    43  		MarketNumber uint
    44  	}{
    45  		Prices:       "42",
    46  		MarketNumber: 1337,
    47  	})
    48  
    49  	// when
    50  	normalisedData, err := adaptors.NewJSONAdaptor().Normalise(pubKey, rawData)
    51  
    52  	// then
    53  	assert.Error(t, err)
    54  	assert.Nil(t, normalisedData)
    55  }
    56  
    57  func testJSONAdaptorNormalisingCompatibleAndValidDataSucceeds(t *testing.T) {
    58  	pubKeyB := &datapb.Signer_PubKey{
    59  		PubKey: &datapb.PubKey{
    60  			Key: "0xdeadbeef",
    61  		},
    62  	}
    63  
    64  	hexPubKey := hex.EncodeToString([]byte(pubKeyB.PubKey.Key))
    65  	pubKey := crypto.NewPublicKey(hexPubKey, []byte(pubKeyB.PubKey.Key))
    66  	oracleData := map[string]string{
    67  		"BTC": "37371.725",
    68  		"ETH": "1412.67",
    69  	}
    70  	rawData, _ := json.Marshal(oracleData)
    71  
    72  	// when
    73  	normalisedData, err := adaptors.NewJSONAdaptor().Normalise(pubKey, rawData)
    74  
    75  	// then
    76  	require.NoError(t, err)
    77  	assert.NotNil(t, normalisedData)
    78  	assert.Equal(t, fmt.Sprintf("signerPubKey(pubKey(%s))", hexPubKey), normalisedData.Signers[0].Signer.String())
    79  	assert.Equal(t, oracleData["BTC"], normalisedData.Data["BTC"])
    80  	assert.Equal(t, oracleData["ETH"], normalisedData.Data["ETH"])
    81  }