-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain_integration_test.go
More file actions
224 lines (201 loc) · 6.54 KB
/
main_integration_test.go
File metadata and controls
224 lines (201 loc) · 6.54 KB
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
package main
import (
. "AppMetadataAPIServerGo/model"
"encoding/json"
"fmt"
"github.com/stretchr/testify/assert"
"gopkg.in/yaml.v2"
"io"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"reflect"
"strings"
"testing"
)
var validInputsDirectory = "./_test_data/_valid_inputs"
var invalidInputsDirectory = "./_test_data/_invalid_inputs"
var allAppMetadatas = getAppMetadataFromPayloads(getPayloadsFromFile(validInputsDirectory))
func TestIntegration(t *testing.T){
validPayloads := getPayloadsFromFile(validInputsDirectory)
invalidPayloads := getPayloadsFromFile(invalidInputsDirectory)
testServer := httptest.NewServer(initServer())
defer testServer.Close()
endpoint := fmt.Sprintf("%s/%s", testServer.URL, MetadataEndpoint)
testPostValidData(t, validPayloads, endpoint)
testPostInvalidData(t, invalidPayloads, endpoint)
testGetByTitleVersionAPI(t, endpoint)
testQueryAPI(t,endpoint)
testDeleteAPI(t, validPayloads, endpoint)
}
func testQueryAPI(t *testing.T, endpoint string) {
queries:= []string{
"license=Apache-2.0",
"title=mit&page=0",
"version=1.0.1&pageSize=abc",
"maintainerName=firstmaintainer+app1&website=https://website2.com",
"wrongKey=nothing",
"title=Valid+App+1&page=xyz",
"",
"pageSize=3&page=2",
"pageSize=2",
"license=Apache-2.0&pageSize=2&page=2",
"pageSize=0",
}
expectedResults := [][]AppMetadata{
allAppMetadatas,
{},
{allAppMetadatas[1], allAppMetadatas[2]},
{allAppMetadatas[2] },
{},
{allAppMetadatas[0], allAppMetadatas[1] },
allAppMetadatas,
{allAppMetadatas[3]},
{allAppMetadatas[0], allAppMetadatas[1]},
{allAppMetadatas[2], allAppMetadatas[3]},
{},
}
for i,query :=range queries{
validateQueryResults(t, endpoint+"?"+query, expectedResults[i])
}
}
func validateQueryResults(t *testing.T, queryURI string, expectedResult []AppMetadata){
res,err:=http.Get(queryURI)
if err!=nil{
t.Fatalf("Something wrong with Query URI %s, Error: %s",queryURI, err)
}
assert.Equal(t, res.StatusCode, 200)
actual:= getAppMetadataSliceFromResponse(res)
assert.ElementsMatch(t, expectedResult, actual)
}
func testGetByTitleVersionAPI(t *testing.T, endpoint string) {
queries:= []string{
"/Valid App 1/0.0.1/",
"/Valid App 1/1.0.1/",
"/Valid App 2/1.0.1/",
"/Valid App 2/1.0.2/",
}
for i, query:= range queries{
res,err:=http.Get(endpoint+query)
if err!=nil{
t.Fatalf("Something wrong with integration test: %s", err)
}
if res.StatusCode!=200{
t.Errorf("Failed to get response from %s. Expected status code = 200, actual: %d", query, res.StatusCode)
}
returnedAppMetadata := getAppMetadataFromResponse(res)
expectedAppMetadata := allAppMetadatas[i]
if !reflect.DeepEqual(expectedAppMetadata, returnedAppMetadata){
t.Errorf("expected metatdata not equal returned metadata. Expected: %+v, returned: %+v", expectedAppMetadata, returnedAppMetadata)
}
}
res,err:=http.Get(endpoint+"/not/there/")
if err!=nil{
t.Fatalf("Something wrong with integration test: %s", err)
}
if res.StatusCode!=404{
t.Errorf("Expected status code = 404, actual: %d", res.StatusCode)
}
}
func testPostInvalidData(t *testing.T, invalidPayloads []string, endpoint string) {
for _, invalidPayload := range invalidPayloads {
res, err := http.Post(endpoint, "text/plain", strings.NewReader(invalidPayload))
if err != nil {
t.Fatalf("Something wrong with integration test: %s", err)
}
if res.StatusCode != 400 {
t.Errorf("Failed to reject invalid payloads.Expected status code = 400, actual: %d, payload:\n %s", res.StatusCode, invalidPayload)
}
}
}
func testPostValidData(t *testing.T, validPayloads []string, endpoint string) {
for _, validPayload := range validPayloads {
res, err := http.Post(endpoint, "text/plain", strings.NewReader(validPayload))
if err != nil {
t.Fatalf("Something wrong with integration test: %s", err)
}
if res.StatusCode != 201 {
responseContent, _ := io.ReadAll(res.Body)
t.Errorf("Expected status code = 201, actual: %d, body: %s", res.StatusCode, responseContent)
}
}
res, err := http.Post(endpoint, "text/plain", strings.NewReader(validPayloads[0]))
if err != nil {
t.Fatalf("Something wrong with integration test: %s", err)
}
if res.StatusCode != 400 {
t.Errorf("Failed to reject duplicate payloads.Expected status code = 400, actual: %d, payload:\n %s", res.StatusCode, validPayloads[0])
}
}
func testDeleteAPI(t *testing.T, validPayloads []string, endpoint string) {
for _, validPayload := range validPayloads {
http.Post(endpoint, "text/plain", strings.NewReader(validPayload))
}
toDelete:="/Valid App 1/0.0.1/"
notToDelete := []string{
"/Valid App 1/1.0.1/",
"/Valid App 2/1.0.1/",
"/Valid App 2/1.0.2/",
}
request, _:=http.NewRequest(http.MethodDelete, endpoint+toDelete,nil)
//before delete
validateQueryResults(t, endpoint+"?title=Valid+App+1", []AppMetadata{allAppMetadatas[0],allAppMetadatas[1]})
res,err:=http.Get(endpoint+toDelete)
assert.Equal(t, getAppMetadataFromResponse(res), allAppMetadatas[0])
//do delete
client:=&http.Client{}
res,err = client.Do(request)
if err!=nil{
t.Errorf("Failed to handle Delete request. Error: %s", err)
}
//verify
assert.Equal(t, res.StatusCode, 200)
assert.Equal(t, getAppMetadataFromResponse(res),allAppMetadatas[0])
res,err=http.Get(endpoint+toDelete)
assert.Equal(t, res.StatusCode, 404)
for _, remains := range notToDelete{
res,err=http.Get(endpoint+remains)
assert.Equal(t, res.StatusCode, 200)
}
validateQueryResults(t, endpoint+"?title=Valid+App+1", []AppMetadata{allAppMetadatas[1]})
}
func getAppMetadataFromResponse(res *http.Response) AppMetadata{
responseContent, _ := io.ReadAll(res.Body)
appMetadata := AppMetadata{}
json.Unmarshal(responseContent, &appMetadata)
return appMetadata
}
func getAppMetadataSliceFromResponse(res *http.Response) []AppMetadata{
responseContent, _ := io.ReadAll(res.Body)
metadatas:= []AppMetadata{}
json.Unmarshal(responseContent, &metadatas)
return metadatas
}
func getPayloadsFromFile(directory string) []string{
var payloads []string
err := filepath.Walk(directory, func(path string, info os.FileInfo, err error) error {
fmt.Printf("walking: %s", path)
if !info.IsDir(){
content, err := os.ReadFile(path)
if err!=nil{
return err
}
payloads = append(payloads, string(content))
}
return nil
})
if err != nil {
panic(err)
}
return payloads
}
func getAppMetadataFromPayloads(payloads []string) []AppMetadata{
var appMetadatas []AppMetadata
for _, payload := range payloads{
parsed := AppMetadata{}
yaml.Unmarshal([]byte(payload), &parsed)
appMetadatas = append(appMetadatas, parsed)
}
return appMetadatas
}