@@ -2,11 +2,14 @@ package main
22
33import (
44 "context"
5+ "errors"
56 "os"
67 "strings"
78 "testing"
89 "time"
910
11+ "gofr.dev/pkg/gofr"
12+ "gofr.dev/pkg/gofr/container"
1013 "gofr.dev/pkg/gofr/datasource/pubsub/kafka"
1114 "gofr.dev/pkg/gofr/logging"
1215 "gofr.dev/pkg/gofr/testutil"
@@ -49,10 +52,10 @@ func TestExampleSubscriber(t *testing.T) {
4952 testutil .NewServerConfigs (t )
5053
5154 go main ()
52- time .Sleep (time .Second * 1 ) // Giving some time to start the server
55+ time .Sleep (time .Second * 30 ) // Giving some time to start the server
5356
5457 initializeTest (t )
55- time .Sleep (time .Second * 20 ) // Giving some time to publish events
58+ time .Sleep (time .Second * 5 ) // Giving some time to publish events
5659 })
5760
5861 testCases := []struct {
@@ -75,3 +78,114 @@ func TestExampleSubscriber(t *testing.T) {
7578 }
7679 }
7780}
81+
82+ type errorRequest struct {}
83+
84+ func (e * errorRequest ) Context () context.Context {
85+ return context .Background ()
86+ }
87+
88+ func (e * errorRequest ) Bind (v interface {}) error { return errors .New ("bind error" ) }
89+ func (e * errorRequest ) Param (key string ) string { return "" }
90+ func (e * errorRequest ) PathParam (key string ) string { return "" }
91+ func (e * errorRequest ) HostName () string { return "" }
92+ func (e * errorRequest ) Params (key string ) []string { return nil }
93+
94+ func TestProductSubscribe_BindError (t * testing.T ) {
95+ mockContainer , _ := container .NewMockContainer (t )
96+
97+ ctx := & gofr.Context {
98+ Request : & errorRequest {},
99+ Container : mockContainer ,
100+ ContextLogger : * logging .NewContextLogger (context .Background (), mockContainer .Logger ),
101+ }
102+
103+ err := productHandler (ctx )
104+
105+ if err != nil {
106+ t .Errorf ("Expected nil error, got %v" , err )
107+ }
108+ }
109+
110+ func TestOrderSubscribe_BindError (t * testing.T ) {
111+ mockContainer , _ := container .NewMockContainer (t )
112+
113+ ctx := & gofr.Context {
114+ Request : & errorRequest {},
115+ Container : mockContainer ,
116+ ContextLogger : * logging .NewContextLogger (context .Background (), mockContainer .Logger ),
117+ }
118+
119+ err := orderHandler (ctx )
120+ if err != nil {
121+ t .Errorf ("Expected nil error, got %v" , err )
122+ }
123+ }
124+
125+ type successProductRequest struct {}
126+
127+ func (r * successProductRequest ) Context () context.Context {
128+ return context .Background ()
129+ }
130+
131+ func (r * successProductRequest ) Bind (v interface {}) error {
132+ p := v .(* struct {
133+ ProductId string `json:"productId"`
134+ Price string `json:"price"`
135+ })
136+ p .ProductId = "123"
137+ p .Price = "599"
138+ return nil
139+ }
140+
141+ func (r * successProductRequest ) Param (string ) string { return "" }
142+ func (r * successProductRequest ) PathParam (string ) string { return "" }
143+ func (r * successProductRequest ) HostName () string { return "" }
144+ func (r * successProductRequest ) Params (string ) []string { return nil }
145+
146+ func TestProductHandler_Success (t * testing.T ) {
147+ mockContainer , _ := container .NewMockContainer (t )
148+ ctx := & gofr.Context {
149+ Request : & successProductRequest {},
150+ Container : mockContainer ,
151+ ContextLogger : * logging .NewContextLogger (context .Background (), mockContainer .Logger ),
152+ }
153+
154+ err := productHandler (ctx )
155+ if err != nil {
156+ t .Errorf ("Expected nil error, got %v" , err )
157+ }
158+ }
159+
160+ type successOrderRequest struct {}
161+
162+ func (r * successOrderRequest ) Context () context.Context {
163+ return context .Background ()
164+ }
165+
166+ func (r * successOrderRequest ) Bind (v interface {}) error {
167+ o := v .(* struct {
168+ OrderId string `json:"orderId"`
169+ Status string `json:"status"`
170+ })
171+ o .OrderId = "456"
172+ o .Status = "pending"
173+ return nil
174+ }
175+ func (r * successOrderRequest ) Param (string ) string { return "" }
176+ func (r * successOrderRequest ) PathParam (string ) string { return "" }
177+ func (r * successOrderRequest ) HostName () string { return "" }
178+ func (r * successOrderRequest ) Params (string ) []string { return nil }
179+
180+ func TestOrderHandler_Success (t * testing.T ) {
181+ mockContainer , _ := container .NewMockContainer (t )
182+ ctx := & gofr.Context {
183+ Request : & successOrderRequest {},
184+ Container : mockContainer ,
185+ ContextLogger : * logging .NewContextLogger (context .Background (), mockContainer .Logger ),
186+ }
187+ err := orderHandler (ctx )
188+ if err != nil {
189+ t .Errorf ("Expected nil error, got %v" , err )
190+ }
191+ }
0 commit comments