-
Notifications
You must be signed in to change notification settings - Fork 0
/
e2e_test.go
151 lines (133 loc) · 4.58 KB
/
e2e_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
package main
import (
"encoding/json"
"fmt"
"io"
"net/http"
"strconv"
"testing"
"time"
"github.com/srik007/sensor-api/domain/entity"
"github.com/srik007/sensor-api/domain/valueObjects"
"github.com/srik007/sensor-api/interfaces"
"github.com/stretchr/testify/assert"
)
const BASE_URL = "http://localhost:8080/api/v1"
func TestAverageTrasnparencyInTheGroup(t *testing.T) {
var averageTransparency interfaces.AverageTransparencyResponse
response, err := http.Get(BASE_URL + "/group/Group1/transparency")
body, err := io.ReadAll(response.Body)
err = json.Unmarshal(body, &averageTransparency)
if err != nil {
fmt.Println("Error unmarshalling JSON:", err)
return
}
assert.NoError(t, err)
defer response.Body.Close()
assert.Equal(t, http.StatusOK, response.StatusCode)
assert.Equal(t, averageTransparency.GroupName, "Group1")
assert.Equal(t, averageTransparency.Transparency, float64(62.5))
}
func TestAverageTemparatureInTheGroup(t *testing.T) {
var averageTemparature interfaces.AverageTemparatureResponse
response, err := http.Get(BASE_URL + "/group/Group2/temparature")
body, err := io.ReadAll(response.Body)
err = json.Unmarshal(body, &averageTemparature)
if err != nil {
fmt.Println("Error unmarshalling JSON:", err)
return
}
assert.NoError(t, err)
defer response.Body.Close()
assert.Equal(t, http.StatusOK, response.StatusCode)
assert.Equal(t, averageTemparature.Temparature, float64(250))
}
func TestTotalSpeciesInTheGroup(t *testing.T) {
var species entity.Species
response, err := http.Get(BASE_URL + "/group/Group2/species")
body, err := io.ReadAll(response.Body)
err = json.Unmarshal(body, &species)
if err != nil {
fmt.Println("Error unmarshalling JSON:", err)
return
}
assert.NoError(t, err)
defer response.Body.Close()
assert.Equal(t, http.StatusOK, response.StatusCode)
assert.Equal(t, len(species), 4)
}
func TestTopNSpeciesInTheGroup(t *testing.T) {
var species entity.Species
response, err := http.Get(BASE_URL + "/group/Group2/species/top/2")
body, err := io.ReadAll(response.Body)
err = json.Unmarshal(body, &species)
if err != nil {
fmt.Println("Error unmarshalling JSON:", err)
return
}
assert.NoError(t, err)
defer response.Body.Close()
assert.Equal(t, http.StatusOK, response.StatusCode)
assert.Equal(t, len(species), 2)
assert.Equal(t, 10, species[0].Count)
}
func TestTopNSpeciesInTheGroupBetween(t *testing.T) {
var species entity.Species
response, err := http.Get(BASE_URL + "/group/Group2/species/top/2?from=1700137279&till=1700137279")
body, err := io.ReadAll(response.Body)
err = json.Unmarshal(body, &species)
if err != nil {
fmt.Println("Error unmarshalling JSON:", err)
return
}
assert.NoError(t, err)
defer response.Body.Close()
assert.Equal(t, http.StatusOK, response.StatusCode)
assert.Equal(t, len(species), 0)
}
func TestMaxTemparatureInsideAGroup(t *testing.T) {
var maxTemparature valueObjects.Temparature
response, err := http.Get(BASE_URL + "/region/temparature/max?xMin=0&xMax=20&yMin=0&yMax=25&zMin=0&zMax=150")
body, err := io.ReadAll(response.Body)
err = json.Unmarshal(body, &maxTemparature)
if err != nil {
fmt.Println("Error unmarshalling JSON:", err)
return
}
assert.NoError(t, err)
defer response.Body.Close()
assert.Equal(t, http.StatusOK, response.StatusCode)
assert.Equal(t, float64(300), maxTemparature.Value)
assert.Equal(t, "Celsius", maxTemparature.Scale)
}
func TestMinTemparatureInsideAGroup(t *testing.T) {
var minTemperature valueObjects.Temparature
response, err := http.Get(BASE_URL + "/region/temparature/min?xMin=0&xMax=20&yMin=0&yMax=25&zMin=0&zMax=150")
body, err := io.ReadAll(response.Body)
err = json.Unmarshal(body, &minTemperature)
if err != nil {
fmt.Println("Error unmarshalling JSON:", err)
return
}
assert.NoError(t, err)
defer response.Body.Close()
assert.Equal(t, http.StatusOK, response.StatusCode)
assert.Equal(t, float64(200), minTemperature.Value)
assert.Equal(t, "Celsius", minTemperature.Scale)
}
func TestAverageTemparatureDetectedBySensor(t *testing.T) {
var averageTemparature valueObjects.Temparature
currentTimestamp := int64(time.Now().Unix())
response, err := http.Get(BASE_URL + "/sensor/Group1_1/temparature/average?from=1697438904&till=" + strconv.FormatInt(currentTimestamp, 10))
body, err := io.ReadAll(response.Body)
err = json.Unmarshal(body, &averageTemparature)
if err != nil {
fmt.Println("Error unmarshalling JSON:", err)
return
}
assert.NoError(t, err)
defer response.Body.Close()
assert.Equal(t, http.StatusOK, response.StatusCode)
assert.Equal(t, float64(200), averageTemparature.Value)
assert.Equal(t, "Celsius", averageTemparature.Scale)
}