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

     1  package cpaper_asservice_test
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/golang/protobuf/proto"
     7  	"github.com/golang/protobuf/ptypes/empty"
     8  	. "github.com/onsi/ginkgo"
     9  	. "github.com/onsi/gomega"
    10  
    11  	"github.com/s7techlab/cckit/examples/cpaper_asservice"
    12  	"github.com/s7techlab/cckit/examples/cpaper_asservice/testdata"
    13  	"github.com/s7techlab/cckit/extensions/owner"
    14  	idtestdata "github.com/s7techlab/cckit/identity/testdata"
    15  	"github.com/s7techlab/cckit/router"
    16  	"github.com/s7techlab/cckit/state"
    17  	testcc "github.com/s7techlab/cckit/testing"
    18  	. "github.com/s7techlab/cckit/testing/gomega"
    19  )
    20  
    21  func TestCommercialPaperService(t *testing.T) {
    22  	RegisterFailHandler(Fail)
    23  	RunSpecs(t, "Commercial Paper Suite")
    24  }
    25  
    26  var (
    27  	CPaper = cpaper_asservice.NewService()
    28  
    29  	// service testing util
    30  	cc, ctx = testcc.NewTxHandler(`Commercial paper`)
    31  
    32  	ids = idtestdata.MustSamples(idtestdata.Certificates, idtestdata.DefaultMSP)
    33  	// actors
    34  	Issuer = ids[0]
    35  	Buyer  = ids[1]
    36  )
    37  
    38  var _ = Describe(`Commercial paper service`, func() {
    39  
    40  	It("Allow to init", func() {
    41  		cc.From(Issuer).Init(func(c router.Context) (interface{}, error) {
    42  			return owner.SetFromCreator(c)
    43  		}).Expect().HasError(nil)
    44  	})
    45  
    46  	It("Allow issuer to issue new commercial paper", func() {
    47  		cc.From(Issuer).Tx(func() {
    48  			cc.Expect(CPaper.Issue(ctx, testdata.Issue1)).Is(testdata.CpaperInState1)
    49  		})
    50  	})
    51  
    52  	// Use TxFunc helper - return closure func() {} with some assertions
    53  	It("Disallow issuer to issue same commercial paper", cc.From(Issuer).TxFunc(func() {
    54  		// Expect helper return TxRes
    55  		// we don't check result payload, error only
    56  		cc.Expect(CPaper.Issue(ctx, testdata.Issue1)).HasError(state.ErrKeyAlreadyExists)
    57  	}))
    58  
    59  	It("Allow issuer to get commercial paper by composite primary key", func() {
    60  		cc.Tx(func() {
    61  			// Expect helper, check error is empty and result
    62  			cc.Expect(CPaper.Get(ctx, testdata.Id1)).Is(testdata.CpaperInState1)
    63  		})
    64  	})
    65  
    66  	It("Allow issuer to get commercial paper by unique key", func() {
    67  		cc.Tx(func() {
    68  			// without helpers
    69  			res, err := CPaper.GetByExternalId(ctx, &cpaper_asservice.ExternalId{
    70  				Id: testdata.Issue1.ExternalId,
    71  			})
    72  
    73  			Expect(err).NotTo(HaveOccurred())
    74  			Expect(res).To(StringerEqual(testdata.CpaperInState1))
    75  		})
    76  
    77  	})
    78  
    79  	It("Allow issuer to get a list of commercial papers", func() {
    80  		cc.Tx(func() {
    81  			res, err := CPaper.List(ctx, &empty.Empty{})
    82  
    83  			Expect(err).NotTo(HaveOccurred())
    84  			Expect(res.Items).To(HaveLen(1))
    85  			Expect(res.Items[0]).To(StringerEqual(testdata.CpaperInState1))
    86  		})
    87  	})
    88  
    89  	It("Allow buyer to buy commercial paper", func() {
    90  		cc.From(Buyer).Tx(func() {
    91  			cc.Expect(CPaper.Buy(ctx, testdata.Buy1)).
    92  				// Produce Event - no error and event name and payload check
    93  				ProduceEvent(`BuyCommercialPaper`, testdata.Buy1)
    94  		})
    95  
    96  		newState := proto.Clone(testdata.CpaperInState1).(*cpaper_asservice.CommercialPaper)
    97  		newState.Owner = testdata.Buy1.NewOwner
    98  		newState.State = cpaper_asservice.CommercialPaper_STATE_TRADING
    99  
   100  		cc.Tx(func() {
   101  			cc.Expect(CPaper.Get(ctx, testdata.Id1)).Is(newState)
   102  		})
   103  	})
   104  
   105  	It("Allow buyer to redeem commercial paper", func() {
   106  		// Invoke example
   107  		cc.Invoke(func(c router.Context) (interface{}, error) {
   108  			return CPaper.Redeem(c, testdata.Redeem1)
   109  		}).Expect().ProduceEvent(`RedeemCommercialPaper`, testdata.Redeem1)
   110  
   111  		newState := proto.Clone(testdata.CpaperInState1).(*cpaper_asservice.CommercialPaper)
   112  		newState.State = cpaper_asservice.CommercialPaper_STATE_REDEEMED
   113  
   114  		cc.Invoke(func(c router.Context) (interface{}, error) {
   115  			return CPaper.Get(c, testdata.Id1)
   116  		}).Expect().Is(newState)
   117  	})
   118  
   119  	It("Allow issuer to delete commercial paper", func() {
   120  		cc.From(Issuer).Tx(func() {
   121  			cc.Expect(CPaper.Delete(ctx, testdata.Id1)).HasNoError()
   122  		})
   123  
   124  		cc.Tx(func() {
   125  			res, err := CPaper.List(ctx, &empty.Empty{})
   126  
   127  			Expect(err).NotTo(HaveOccurred())
   128  			Expect(res.Items).To(HaveLen(0))
   129  		})
   130  	})
   131  })