Skip to content

Commit 74bdb39

Browse files
committed
added tests
1 parent 5fb3ea2 commit 74bdb39

File tree

5 files changed

+153
-21
lines changed

5 files changed

+153
-21
lines changed

event.json

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"specversion" : "0.3",
3+
"type" : "com.example.event",
4+
"source" : "https://github.com/mchmarny/dapr-starter",
5+
"id" : "C234-1234-1234",
6+
"time" : "2020-06-14T15:14:08Z",
7+
"extension1" : "test",
8+
"extension2" : 5,
9+
"datacontenttype" : "application/json",
10+
"data" : {
11+
"message": "hello"
12+
}
13+
}

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,6 @@ require (
77
github.com/gin-gonic/gin v1.6.3
88
github.com/mchmarny/gcputil v0.3.3
99
github.com/mchmarny/godapr v0.8.1
10+
github.com/stretchr/testify v1.5.1
1011
go.opencensus.io v0.22.4-0.20200403004729-46dfec7deb6e
1112
)

handler.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@ package main
33
import (
44
"encoding/json"
55
"net/http"
6+
"time"
67

78
"github.com/gin-gonic/gin"
9+
dapr "github.com/mchmarny/godapr/v1"
810

911
ce "github.com/cloudevents/sdk-go/v2"
1012
)
@@ -29,6 +31,25 @@ type SimpleMessage struct {
2931
Message string `json:"message"`
3032
}
3133

34+
func defaultHandler(c *gin.Context) {
35+
c.JSON(http.StatusOK, gin.H{
36+
"release": AppVersion,
37+
"request_on": time.Now(),
38+
"request_from": c.Request.RemoteAddr,
39+
})
40+
}
41+
42+
func subscriptionHandler(c *gin.Context) {
43+
subscriptions := []dapr.Subscription{
44+
{
45+
Topic: topicName,
46+
Route: "/events",
47+
},
48+
}
49+
logger.Printf("subscription topics: %v", subscriptions)
50+
c.JSON(http.StatusOK, subscriptions)
51+
}
52+
3253
func eventHandler(c *gin.Context) {
3354
ctx := getTraceContext(c)
3455
e := ce.NewEvent()

handler_test.go

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
package main
2+
3+
import (
4+
"bytes"
5+
"encoding/json"
6+
"io/ioutil"
7+
"net/http"
8+
"net/http/httptest"
9+
"testing"
10+
11+
"github.com/gin-gonic/gin"
12+
dapr "github.com/mchmarny/godapr/v1"
13+
"github.com/stretchr/testify/assert"
14+
"go.opencensus.io/trace"
15+
)
16+
17+
func TestDefaultHandler(t *testing.T) {
18+
daprClient = GetTestClient()
19+
gin.SetMode(gin.DebugMode)
20+
21+
r := gin.New()
22+
r.Use(gin.Recovery())
23+
r.Use(Options)
24+
r.GET("/", defaultHandler)
25+
w := httptest.NewRecorder()
26+
27+
req, _ := http.NewRequest("GET", "/", nil)
28+
req.Header.Set("Content-Type", "application/json")
29+
30+
r.ServeHTTP(w, req)
31+
assert.Equal(t, http.StatusOK, w.Code)
32+
}
33+
34+
func TestSubscriptionHandler(t *testing.T) {
35+
daprClient = GetTestClient()
36+
gin.SetMode(gin.DebugMode)
37+
38+
r := gin.New()
39+
r.Use(gin.Recovery())
40+
r.Use(Options)
41+
r.GET("/", subscriptionHandler)
42+
w := httptest.NewRecorder()
43+
44+
req, _ := http.NewRequest("GET", "/", nil)
45+
req.Header.Set("Content-Type", "application/json")
46+
47+
r.ServeHTTP(w, req)
48+
assert.Equal(t, http.StatusOK, w.Code)
49+
50+
content, err := ioutil.ReadAll(w.Body)
51+
assert.Nil(t, err)
52+
53+
var subs []dapr.Subscription
54+
err = json.Unmarshal(content, &subs)
55+
assert.Nil(t, err)
56+
assert.Lenf(t, subs, 1, "minimum 1 subscription required, got: %v", subs)
57+
}
58+
59+
func TestEventHandler(t *testing.T) {
60+
daprClient = GetTestClient()
61+
gin.SetMode(gin.DebugMode)
62+
63+
r := gin.New()
64+
r.Use(gin.Recovery())
65+
r.Use(Options)
66+
r.POST("/", eventHandler)
67+
w := httptest.NewRecorder()
68+
69+
data, err := ioutil.ReadFile("./event.json")
70+
assert.Nil(t, err)
71+
72+
req, _ := http.NewRequest("POST", "/", bytes.NewBuffer(data))
73+
req.Header.Set("Content-Type", "application/json")
74+
75+
r.ServeHTTP(w, req)
76+
assert.Equal(t, http.StatusOK, w.Code)
77+
}
78+
79+
func GetTestClient() *TestClient {
80+
return &TestClient{}
81+
}
82+
83+
var (
84+
// test test client against local interace
85+
_ = Client(&TestClient{})
86+
)
87+
88+
type TestClient struct {
89+
}
90+
91+
func (c *TestClient) GetState(ctx trace.SpanContext, store, key string) ([]byte, error) {
92+
return []byte(`{ "message": "hello" }`), nil
93+
}
94+
func (c *TestClient) SaveState(ctx trace.SpanContext, store, key string, data interface{}) error {
95+
return nil
96+
}
97+
func (c *TestClient) DeleteState(ctx trace.SpanContext, store, key string) error {
98+
return nil
99+
}

main.go

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"net"
66
"net/http"
77
"os"
8-
"time"
98

109
"github.com/gin-gonic/gin"
1110
"github.com/mchmarny/gcputil/env"
@@ -26,39 +25,31 @@ var (
2625
topicName = env.MustGetEnvVar("TOPIC_NAME", "events")
2726
storeName = env.MustGetEnvVar("STORE_NAME", "store")
2827

29-
daprClient = dapr.NewClient()
28+
// dapr
29+
daprClient Client
30+
31+
// test client against local interace
32+
_ = Client(dapr.NewClient())
3033
)
3134

3235
func main() {
3336
gin.SetMode(gin.ReleaseMode)
3437

38+
// wire actual Dapr client
39+
daprClient = dapr.NewClient()
40+
3541
// router
3642
r := gin.New()
3743
r.Use(gin.Recovery())
3844
r.Use(Options)
3945

40-
// root route
41-
r.GET("/", func(c *gin.Context) {
42-
c.JSON(http.StatusOK, gin.H{
43-
"release": AppVersion,
44-
"request_on": time.Now(),
45-
"request_from": c.Request.RemoteAddr,
46-
})
47-
})
48-
4946
// pubsub
50-
r.GET("/dapr/subscribe", func(c *gin.Context) {
51-
subscriptions := []dapr.Subscription{
52-
{
53-
Topic: topicName,
54-
Route: "/events",
55-
},
56-
}
57-
logger.Printf("subscription topics: %v", subscriptions)
58-
c.JSON(http.StatusOK, subscriptions)
59-
})
47+
r.GET("/dapr/subscribe", subscriptionHandler)
6048
r.POST("/events", eventHandler)
6149

50+
// default route
51+
r.Any("/", defaultHandler)
52+
6253
// server
6354
hostPort := net.JoinHostPort("0.0.0.0", servicePort)
6455
logger.Printf("Server (%s) starting: %s \n", AppVersion, hostPort)
@@ -96,3 +87,10 @@ func getTraceContext(c *gin.Context) trace.SpanContext {
9687

9788
return ctx
9889
}
90+
91+
// Client is the minimal client support for testing
92+
type Client interface {
93+
GetState(ctx trace.SpanContext, store, key string) ([]byte, error)
94+
SaveState(ctx trace.SpanContext, store, key string, data interface{}) error
95+
DeleteState(ctx trace.SpanContext, store, key string) error
96+
}

0 commit comments

Comments
 (0)