github.com/s7techlab/cckit@v0.10.5/examples/cpaper_asservice/chaincode_test.go (about)

     1  package cpaper_asservice_test
     2  
     3  import (
     4  	"io/ioutil"
     5  	"testing"
     6  
     7  	"github.com/golang/protobuf/ptypes/empty"
     8  	"github.com/hyperledger/fabric/msp"
     9  	. "github.com/onsi/ginkgo"
    10  	. "github.com/onsi/gomega"
    11  
    12  	"github.com/s7techlab/cckit/examples/cpaper_asservice"
    13  	"github.com/s7techlab/cckit/examples/cpaper_asservice/testdata"
    14  	enctest "github.com/s7techlab/cckit/extensions/encryption/testing"
    15  	"github.com/s7techlab/cckit/router"
    16  	testcc "github.com/s7techlab/cckit/testing"
    17  	expectcc "github.com/s7techlab/cckit/testing/expect"
    18  )
    19  
    20  const (
    21  	MspName = "msp"
    22  )
    23  
    24  func TestCommercialPaper(t *testing.T) {
    25  	RegisterFailHandler(Fail)
    26  	RunSpecs(t, "Commercial Paper Suite")
    27  }
    28  
    29  var (
    30  	ccImpl, ccEncImpl *router.Chaincode
    31  	err               error
    32  	mockstub          *testcc.MockStub
    33  	mockstubEnc       *enctest.MockStub
    34  
    35  	identity msp.SigningIdentity
    36  )
    37  
    38  var _ = Describe(`CommercialPaper`, func() {
    39  
    40  	BeforeSuite(func() {
    41  
    42  		ccImpl, err = cpaper_asservice.NewCC()
    43  		Expect(err).NotTo(HaveOccurred())
    44  
    45  		ccEncImpl, err = cpaper_asservice.NewCCEncrypted()
    46  		Expect(err).NotTo(HaveOccurred())
    47  
    48  		mockstub = testcc.NewMockStub(`cpaper_as_service`, ccImpl)
    49  		// all queries/invokes arguments to cc will be encrypted
    50  		mockstubEnc = enctest.NewMockStub(testcc.NewMockStub(`cpaper_as_service_encrypted`, ccEncImpl))
    51  
    52  		identity, err = testcc.IdentityFromFile(MspName, `./testdata/admin.pem`, ioutil.ReadFile)
    53  		Expect(err).NotTo(HaveOccurred())
    54  		// Init chaincode with admin identity
    55  		expectcc.ResponseOk(mockstub.From(identity).Init())
    56  	})
    57  
    58  	Describe("Commercial Paper lifecycle", func() {
    59  		It("Allow issuer to issue new commercial paper", func() {
    60  			expectcc.ResponseOk(mockstub.Invoke(cpaper_asservice.CPaperServiceChaincode_Issue, testdata.Issue1))
    61  
    62  			// Validate event has been emitted with the transaction data
    63  			expectcc.EventStringerEqual(mockstub.ChaincodeEvent, `IssueCommercialPaper`, testdata.Issue1)
    64  		}, 0.1)
    65  
    66  		It("Allow issuer to get commercial paper by composite primary key", func() {
    67  			queryResponse := mockstub.Query(cpaper_asservice.CPaperServiceChaincode_Get, testdata.Id1)
    68  
    69  			paper := expectcc.PayloadIs(queryResponse, &cpaper_asservice.CommercialPaper{}).(*cpaper_asservice.CommercialPaper)
    70  
    71  			Expect(paper.Issuer).To(Equal(testdata.Issue1.Issuer))
    72  			Expect(paper.Owner).To(Equal(testdata.Issue1.Issuer))
    73  			Expect(paper.State).To(Equal(cpaper_asservice.CommercialPaper_STATE_ISSUED))
    74  			Expect(paper.PaperNumber).To(Equal(testdata.Issue1.PaperNumber))
    75  			Expect(paper.FaceValue).To(BeNumerically("==", testdata.Issue1.FaceValue))
    76  		})
    77  
    78  		It("Allow issuer to get commercial paper by unique key", func() {
    79  			queryResponse := mockstub.Query(
    80  				cpaper_asservice.CPaperServiceChaincode_GetByExternalId, testdata.ExternalId1)
    81  
    82  			paper := expectcc.PayloadIs(queryResponse, &cpaper_asservice.CommercialPaper{}).(*cpaper_asservice.CommercialPaper)
    83  
    84  			Expect(paper.Issuer).To(Equal(testdata.Issue1.Issuer))
    85  			Expect(paper.Owner).To(Equal(testdata.Issue1.Issuer))
    86  			Expect(paper.State).To(Equal(cpaper_asservice.CommercialPaper_STATE_ISSUED))
    87  			Expect(paper.PaperNumber).To(Equal(testdata.Issue1.PaperNumber))
    88  			Expect(paper.FaceValue).To(BeNumerically("==", testdata.Issue1.FaceValue))
    89  		})
    90  
    91  		It("Allow issuer to get a list of commercial papers", func() {
    92  			queryResponse := mockstub.Query(cpaper_asservice.CPaperServiceChaincode_List, &empty.Empty{})
    93  
    94  			papers := expectcc.PayloadIs(queryResponse, &cpaper_asservice.CommercialPaperList{}).(*cpaper_asservice.CommercialPaperList)
    95  
    96  			Expect(len(papers.Items)).To(BeNumerically("==", 1))
    97  			Expect(papers.Items[0].Issuer).To(Equal(testdata.Issue1.Issuer))
    98  			Expect(papers.Items[0].Owner).To(Equal(testdata.Issue1.Issuer))
    99  			Expect(papers.Items[0].State).To(Equal(cpaper_asservice.CommercialPaper_STATE_ISSUED))
   100  			Expect(papers.Items[0].PaperNumber).To(Equal(testdata.Issue1.PaperNumber))
   101  			Expect(papers.Items[0].FaceValue).To(BeNumerically("==", testdata.Issue1.FaceValue))
   102  		})
   103  
   104  		It("Allow buyer to buy commercial paper", func() {
   105  			expectcc.ResponseOk(mockstub.Invoke(cpaper_asservice.CPaperServiceChaincode_Buy, testdata.Buy1))
   106  			expectcc.EventStringerEqual(mockstub.ChaincodeEvent, `BuyCommercialPaper`, testdata.Buy1)
   107  
   108  			queryResponse := mockstub.Query(cpaper_asservice.CPaperServiceChaincode_Get, testdata.Id1)
   109  
   110  			paper := expectcc.PayloadIs(queryResponse, &cpaper_asservice.CommercialPaper{}).(*cpaper_asservice.CommercialPaper)
   111  
   112  			Expect(paper.Owner).To(Equal(testdata.Buy1.NewOwner))
   113  			Expect(paper.State).To(Equal(cpaper_asservice.CommercialPaper_STATE_TRADING))
   114  
   115  		})
   116  
   117  		It("Allow buyer to redeem commercial paper", func() {
   118  			expectcc.ResponseOk(mockstub.Invoke(cpaper_asservice.CPaperServiceChaincode_Redeem, testdata.Redeem1))
   119  			expectcc.EventStringerEqual(mockstub.ChaincodeEvent, `RedeemCommercialPaper`, testdata.Redeem1)
   120  
   121  			queryResponse := mockstub.Query(cpaper_asservice.CPaperServiceChaincode_Get, testdata.Id1)
   122  
   123  			paper := expectcc.PayloadIs(queryResponse, &cpaper_asservice.CommercialPaper{}).(*cpaper_asservice.CommercialPaper)
   124  			Expect(paper.Owner).To(Equal(testdata.Issue1.Issuer))
   125  			Expect(paper.State).To(Equal(cpaper_asservice.CommercialPaper_STATE_REDEEMED))
   126  		})
   127  
   128  		It("Allow issuer to delete commercial paper", func() {
   129  			expectcc.ResponseOk(mockstub.Invoke(cpaper_asservice.CPaperServiceChaincode_Delete, testdata.Id1))
   130  
   131  			// Validate there are 0 Commercial Papers in the world state
   132  			queryResponse := mockstub.Query(cpaper_asservice.CPaperServiceChaincode_List, &empty.Empty{})
   133  			papers := expectcc.PayloadIs(queryResponse, &cpaper_asservice.CommercialPaperList{}).(*cpaper_asservice.CommercialPaperList)
   134  
   135  			Expect(len(papers.Items)).To(BeNumerically("==", 0))
   136  		})
   137  	})
   138  
   139  	Describe("Commercial Paper Encrypted lifecycle", func() {
   140  		It("Allow issuer to issue new commercial paper", func() {
   141  
   142  			expectcc.ResponseOk(mockstubEnc.Invoke(cpaper_asservice.CPaperServiceChaincode_Issue, testdata.Issue1))
   143  
   144  			// Validate event has been emitted with the transaction data, and event name and payload is encrypted
   145  			expectcc.EventStringerEqual(mockstubEnc.LastEvent(),
   146  				`IssueCommercialPaper`, testdata.Issue1)
   147  		})
   148  	})
   149  })