Skip to content

Commit

Permalink
test(dataverse): GetResourceGovAddr
Browse files Browse the repository at this point in the history
  • Loading branch information
bdeneux committed Aug 29, 2024
1 parent ae17998 commit 35743bd
Show file tree
Hide file tree
Showing 3 changed files with 190 additions and 25 deletions.
7 changes: 3 additions & 4 deletions dataverse/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package dataverse

import (
"context"
"encoding/json"
"fmt"

cgschema "github.com/axone-protocol/axone-contract-schema/go/cognitarium-schema/v5"
Expand All @@ -21,7 +20,9 @@ type client struct {
cognitariumAddr string
}

func NewDataverseClient(ctx context.Context, dataverseClient dvschema.QueryClient, cognitariumClient cgschema.QueryClient) (Client, error) {
func NewDataverseClient(ctx context.Context,
dataverseClient dvschema.QueryClient,
cognitariumClient cgschema.QueryClient) (Client, error) {
cognitariumAddr, err := getCognitariumAddr(ctx, dataverseClient)
if err != nil {
return nil, fmt.Errorf("failed to get cognitarium address: %w", err)
Expand Down Expand Up @@ -62,8 +63,6 @@ func NewClient(ctx context.Context,

func (c *client) GetResourceGovAddr(_ context.Context, resourceDID string) (string, error) {
query := buildGetResourceGovAddrRequest(resourceDID)
queryB, _ := json.Marshal(query)
fmt.Printf("query : %s", queryB)
response, err := c.cognitariumClient.Select(context.Background(), &cgschema.QueryMsg_Select{Query: query})
if err != nil {
return "", err
Expand Down
152 changes: 149 additions & 3 deletions dataverse/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import (
"fmt"
"testing"

schema "github.com/axone-protocol/axone-contract-schema/go/dataverse-schema/v5"
cgschema "github.com/axone-protocol/axone-contract-schema/go/cognitarium-schema/v5"
dvschema "github.com/axone-protocol/axone-contract-schema/go/dataverse-schema/v5"
"github.com/axone-protocol/axone-sdk/dataverse"
"github.com/axone-protocol/axone-sdk/testutil"
. "github.com/smartystreets/goconvey/convey"
Expand Down Expand Up @@ -43,8 +44,8 @@ func TestClient_NewDataverseClient(t *testing.T) {
mockClient.EXPECT().
Dataverse(gomock.Any(), gomock.Any()).
Return(
&schema.DataverseResponse{
TriplestoreAddress: schema.Addr(test.wantAddr),
&dvschema.DataverseResponse{
TriplestoreAddress: dvschema.Addr(test.wantAddr),
},
test.returnedErr,
).
Expand All @@ -68,3 +69,148 @@ func TestClient_NewDataverseClient(t *testing.T) {
})
}
}

func toAddress[T any](v T) *T {
return &v
}

func TestClient_GetResourceGovAddr(t *testing.T) {
tests := []struct {
name string
resourceDID string
response *cgschema.SelectResponse
responseError error
wantErr error
wantResult string
}{
{
name: "ask for good did response",
resourceDID: "did:key:zQ3shuwMJWYXRi64qiGojsV9bPN6Dtugz5YFM2ESPtkaNxTZ5",
response: &cgschema.SelectResponse{
Head: cgschema.Head{
Vars: []string{"code"},
},
Results: cgschema.Results{
Bindings: []map[string]cgschema.Value{
{
"code": cgschema.Value{
ValueType: cgschema.URI{
Type: "uri",
Value: cgschema.IRI{Full: toAddress(cgschema.IRI_Full("foo"))},
},
},
},
},
},
},
responseError: nil,
wantErr: nil,
wantResult: "foo",
},
{
name: "grpc error",
resourceDID: "did:key:zQ3shuwMJWYXRi64qiGojsV9bPN6Dtugz5YFM2ESPtkaNxTZ5",
response: nil,
responseError: fmt.Errorf("gRPC: connection refused"),
wantErr: fmt.Errorf("gRPC: connection refused"),
wantResult: "",
},
{
name: "invalid variable binding in response",
resourceDID: "did:key:zQ3shuwMJWYXRi64qiGojsV9bPN6Dtugz5YFM2ESPtkaNxTZ5",
response: &cgschema.SelectResponse{
Head: cgschema.Head{
Vars: []string{"code"},
},
Results: cgschema.Results{
Bindings: []map[string]cgschema.Value{
{
"invalid": cgschema.Value{},
},
},
},
},
responseError: nil,
wantErr: fmt.Errorf("could not find governance code"),
wantResult: "",
},
{
name: "no binding in response",
resourceDID: "did:key:zQ3shuwMJWYXRi64qiGojsV9bPN6Dtugz5YFM2ESPtkaNxTZ5",
response: &cgschema.SelectResponse{
Head: cgschema.Head{
Vars: []string{"code"},
},
Results: cgschema.Results{
Bindings: []map[string]cgschema.Value{},
},
},
responseError: nil,
wantErr: fmt.Errorf("could not find governance code"),
wantResult: "",
},
{
name: "invalid value type in response",
resourceDID: "did:key:zQ3shuwMJWYXRi64qiGojsV9bPN6Dtugz5YFM2ESPtkaNxTZ5",
response: &cgschema.SelectResponse{
Head: cgschema.Head{
Vars: []string{"code"},
},
Results: cgschema.Results{
Bindings: []map[string]cgschema.Value{
{
"code": cgschema.Value{
ValueType: cgschema.BlankNode{
Type: "blank_node",
Value: "foo",
},
},
},
},
},
},
responseError: nil,
wantErr: fmt.Errorf("could not decode governance code"),
wantResult: "",
},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
Convey("Given a mocked dataverse client", t, func() {
controller := gomock.NewController(t)
defer controller.Finish()

mockDataverseClient := testutil.NewMockDataverseQueryClient(controller)
mockDataverseClient.EXPECT().
Dataverse(gomock.Any(), gomock.Any()).
Return(&dvschema.DataverseResponse{TriplestoreAddress: "bar"}, nil).
Times(1)

mockCognitarium := testutil.NewMockCognitariumQueryClient(controller)
mockCognitarium.
EXPECT().
Select(gomock.Any(), gomock.Any()).
Return(test.response, test.responseError).
Times(1)

client, err := dataverse.NewDataverseClient(context.Background(), mockDataverseClient, mockCognitarium)
So(err, ShouldBeNil)

Convey("When GetResourceGovAddr is called", func() {
addr, err := client.GetResourceGovAddr(context.Background(), test.resourceDID)

Convey("Then the resource governance address should be returned", func() {
if test.wantErr == nil {
So(err, ShouldBeNil)
So(addr, ShouldEqual, test.wantResult)
} else {
So(err.Error(), ShouldEqual, test.wantErr.Error())
So(addr, ShouldEqual, "")
}
})
})
})
})
}
}
56 changes: 38 additions & 18 deletions dataverse/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,18 @@ package dataverse

import (
"fmt"

cgschema "github.com/axone-protocol/axone-contract-schema/go/cognitarium-schema/v5"
)

const W3IDPrefix = "https://w3id.org/axone/ontology/v4"

//nolint:funlen
func buildGetResourceGovAddrRequest(resource string) cgschema.SelectQuery {
limit := 1
selectVar := cgschema.SelectItem_Variable("code")
codeVar := cgschema.SelectItem_Variable("code")
codeVarOrNodeOrLit := cgschema.VarOrNodeOrLiteral_Variable("code")
credId := cgschema.VarOrNode_Variable("credId")
credIDVar := cgschema.VarOrNode_Variable("credId")
resourceIRI := cgschema.IRI_Full(resource)
govType := cgschema.IRI_Prefixed("gov:GovernanceTextCredential")
claimVar := cgschema.VarOrNode_Variable("claim")
Expand All @@ -31,36 +33,54 @@ func buildGetResourceGovAddrRequest(resource string) cgschema.SelectQuery {
},
Select: []cgschema.SelectItem{
{
Variable: &selectVar,
Variable: &codeVar,
},
},
Where: cgschema.WhereClause{
Bgp: &cgschema.WhereClause_Bgp{
Patterns: []cgschema.TriplePattern{
{
Subject: cgschema.VarOrNode{Variable: &credId},
Predicate: cgschema.VarOrNamedNode{NamedNode: &cgschema.VarOrNamedNode_NamedNode{Full: &VcBodySubject}},
Object: cgschema.VarOrNodeOrLiteral{Node: &cgschema.VarOrNodeOrLiteral_Node{NamedNode: &cgschema.Node_NamedNode{Full: &resourceIRI}}},
Subject: cgschema.VarOrNode{Variable: &credIDVar},
Predicate: cgschema.VarOrNamedNode{
NamedNode: &cgschema.VarOrNamedNode_NamedNode{Full: &VcBodySubject},
},
Object: cgschema.VarOrNodeOrLiteral{
Node: &cgschema.VarOrNodeOrLiteral_Node{
NamedNode: &cgschema.Node_NamedNode{Full: &resourceIRI},
},
},
},
{
Subject: cgschema.VarOrNode{Variable: &credId},
Predicate: cgschema.VarOrNamedNode{NamedNode: &cgschema.VarOrNamedNode_NamedNode{Full: &VcBodyType}},
Object: cgschema.VarOrNodeOrLiteral{Node: &cgschema.VarOrNodeOrLiteral_Node{NamedNode: &cgschema.Node_NamedNode{Prefixed: &govType}}},
Subject: cgschema.VarOrNode{Variable: &credIDVar},
Predicate: cgschema.VarOrNamedNode{
NamedNode: &cgschema.VarOrNamedNode_NamedNode{Full: &VcBodyType},
},
Object: cgschema.VarOrNodeOrLiteral{
Node: &cgschema.VarOrNodeOrLiteral_Node{
NamedNode: &cgschema.Node_NamedNode{Prefixed: &govType},
},
},
},
{
Subject: cgschema.VarOrNode{Variable: &credId},
Predicate: cgschema.VarOrNamedNode{NamedNode: &cgschema.VarOrNamedNode_NamedNode{Full: &VcBodyClaim}},
Object: cgschema.VarOrNodeOrLiteral{Variable: &claimVarOrNode},
Subject: cgschema.VarOrNode{Variable: &credIDVar},
Predicate: cgschema.VarOrNamedNode{
NamedNode: &cgschema.VarOrNamedNode_NamedNode{Full: &VcBodyClaim},
},
Object: cgschema.VarOrNodeOrLiteral{Variable: &claimVarOrNode},
},
{
Subject: cgschema.VarOrNode{Variable: &claimVar},
Predicate: cgschema.VarOrNamedNode{NamedNode: &cgschema.VarOrNamedNode_NamedNode{Prefixed: &isGovernedBy}},
Object: cgschema.VarOrNodeOrLiteral{Variable: &govVarOrNodeOrLit},
Subject: cgschema.VarOrNode{Variable: &claimVar},
Predicate: cgschema.VarOrNamedNode{
NamedNode: &cgschema.VarOrNamedNode_NamedNode{Prefixed: &isGovernedBy},
},
Object: cgschema.VarOrNodeOrLiteral{Variable: &govVarOrNodeOrLit},
},
{
Subject: cgschema.VarOrNode{Variable: &govVar},
Predicate: cgschema.VarOrNamedNode{NamedNode: &cgschema.VarOrNamedNode_NamedNode{Prefixed: &fromGovernance}},
Object: cgschema.VarOrNodeOrLiteral{Variable: &codeVarOrNodeOrLit},
Subject: cgschema.VarOrNode{Variable: &govVar},
Predicate: cgschema.VarOrNamedNode{
NamedNode: &cgschema.VarOrNamedNode_NamedNode{Prefixed: &fromGovernance},
},
Object: cgschema.VarOrNodeOrLiteral{Variable: &codeVarOrNodeOrLit},
},
},
},
Expand Down

0 comments on commit 35743bd

Please sign in to comment.