-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathendpoint_test.go
180 lines (176 loc) · 3.86 KB
/
endpoint_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package main
import (
"bytes"
"encoding/json"
"fmt"
"github.com/taalhach/restcron/job"
"github.com/taalhach/restcron/server"
"io/ioutil"
"log"
"net/http"
"testing"
"time"
)
const (
serverUrl="http://localhost:8080"
)
func TestJob(t *testing.T) {
//to use mock server uncomment following lines but its is recommended to run server separately for clean testing log
//mockServer()
//log.Print("\n\n\n**************************** Starting test *******************\n\n\n")
var id int64
t.Run(fmt.Sprintf("Get_all_jobs"), func(t *testing.T) {
req,err:=http.NewRequest(http.MethodGet,serverUrl+"/",nil)
if err!=nil{
t.Error(err)
return
}
client:=http.Client{}
resp,err:= client.Do(req)
if err!=nil{
t.Error(err)
return
}
if resp.StatusCode!=http.StatusOK{
t.Error("status code: ",resp.StatusCode)
}
})
t.Run(fmt.Sprintf("Create_Job"), func(t *testing.T) {
body,err:=json.Marshal(&job.Job{EndDate:time.Now().Add(time.Minute),StartDate:time.Now(),Frequency:"@every 3s"})
if err!=nil{
t.Error(err)
return
}
req,err:=http.NewRequest(http.MethodPost,serverUrl+"/job",bytes.NewBuffer(body))
if err!=nil{
t.Error(err)
return
}
client:=http.Client{}
resp,err:= client.Do(req)
if err!=nil{
t.Error(err)
return
}
if resp.StatusCode!=http.StatusOK{
t.Error("status code: ",resp.StatusCode)
return
}
body,err=ioutil.ReadAll(resp.Body)
if err!=nil{
t.Error(err)
return
}
var j job.Job
if err:=json.Unmarshal(body,&j); err!=nil{
t.Error(err)
return
}
if j.ID==nil {
t.Error("Job not created",j.ID)
return
}
id=*j.ID
})
t.Run(fmt.Sprintf("Update_Job/%v",id), func(t *testing.T) {
body,err:=json.Marshal(&job.Job{ID:&id,EndDate:time.Now().Add(time.Minute),StartDate:time.Now(),Frequency:"@every 3s"})
if err!=nil{
t.Error(err)
return
}
req,err:=http.NewRequest(http.MethodPatch,serverUrl+"/job",bytes.NewBuffer(body))
if err!=nil{
t.Error(err)
return
}
client:=http.Client{}
resp,err:= client.Do(req)
if err!=nil{
t.Error(err)
return
}
if resp.StatusCode!=http.StatusOK{
t.Error("status code: ",resp.StatusCode)
return
}
body,err=ioutil.ReadAll(resp.Body)
if err!=nil{
t.Error(err)
return
}
var j job.Job
if err:=json.Unmarshal(body,&j); err!=nil{
t.Error(err)
return
}
if j.ID==nil {
t.Error("Job not updated",j.ID)
return
}
})
t.Run(fmt.Sprintf("Get_Job/%v",id), func(t *testing.T) {
req,err:=http.NewRequest(http.MethodGet,serverUrl+fmt.Sprintf("/job/%v",id),nil)
if err!=nil{
t.Error(err)
return
}
client:=http.Client{}
resp,err:= client.Do(req)
if err!=nil{
t.Error(err)
return
}
if resp.StatusCode!=http.StatusOK{
t.Error("status code: ",resp.StatusCode)
}
})
t.Run(fmt.Sprintf("Delete Job/%v",id), func(t *testing.T) {
req,err:=http.NewRequest(http.MethodDelete,serverUrl+fmt.Sprintf("/job/%v",id),nil)
if err!=nil{
t.Error(err)
return
}
client:=http.Client{}
resp,err:= client.Do(req)
if err!=nil{
t.Error(err)
return
}
if resp.StatusCode!=http.StatusOK{
t.Error("status code: ",resp.StatusCode)
return
}
body,err:=ioutil.ReadAll(resp.Body)
if err!=nil{
t.Error(err)
return
}
var j job.Job
if err:=json.Unmarshal(body,&j); err!=nil{
t.Error(err)
return
}
if j.ID==nil {
t.Error(fmt.Sprintf("Job %v was supposed to delete but deleted: %v",id,j.ID))
return
}else if *j.ID!=id || j.IsActive{
t.Error(fmt.Sprintf("Job %v was supposed to delete but status is: %v",id,*j.ID))
return
}
})
}
func mockServer() {
//mock server is started in a go routine
go func() {
cfg,err:=readConfig()
if err!=nil{
log.Fatal(err)
}
s, err := server.NewServer(cfg.Database.Url, cfg.Database.User_name, cfg.Database.Password)
if err != nil {
log.Fatal(err)
}
s.RunServer(cfg.Server.Port)
}()
time.Sleep(3*time.Second)
}