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
|
// Copyright Verizon Media. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
// document command tests
// Author: bratseth
package cmd
import (
"io/ioutil"
"strconv"
"testing"
"github.com/stretchr/testify/assert"
"github.com/vespa-engine/vespa/util"
"github.com/vespa-engine/vespa/vespa"
)
func TestDocumentPostWithIdArg(t *testing.T) {
assertDocumentPost([]string{"document", "post", "id:mynamespace:music::head", "testdata/A-Head-Full-of-Dreams.json"},
"id:mynamespace:music::head", "testdata/A-Head-Full-of-Dreams.json", t)
}
func TestDocumentPostWithIdInDocument(t *testing.T) {
assertDocumentPost([]string{"document", "post", "testdata/A-Head-Full-of-Dreams-With-Id.json"},
"id:mynamespace:music::head", "testdata/A-Head-Full-of-Dreams-With-Id.json", t)
}
func TestDocumentPostWithIdInDocumentShortForm(t *testing.T) {
assertDocumentPost([]string{"document", "testdata/A-Head-Full-of-Dreams-With-Id.json"},
"id:mynamespace:music::head", "testdata/A-Head-Full-of-Dreams-With-Id.json", t)
}
func TestDocumentPostWithIdAsPutInDocument(t *testing.T) {
assertDocumentPost([]string{"document", "post", "testdata/A-Head-Full-of-Dreams-With-Put.json"},
"id:mynamespace:music::head", "testdata/A-Head-Full-of-Dreams-With-Put.json", t)
}
func TestDocumentIdNotSpecified(t *testing.T) {
arguments := []string{"document", "post", "testdata/A-Head-Full-of-Dreams.json"}
client := &mockHttpClient{}
assert.Equal(t,
"No document id given neither as argument or an 'id' key in the json file\n",
executeCommand(t, client, arguments, []string{}))
}
func TestDocumentPostDocumentError(t *testing.T) {
assertDocumentError(t, 401, "Document error")
}
func TestDocumentPostServerError(t *testing.T) {
assertDocumentServerError(t, 501, "Server error")
}
func assertDocumentPost(arguments []string, documentId string, jsonFile string, t *testing.T) {
client := &mockHttpClient{}
assert.Equal(t,
documentId+"\n",
executeCommand(t, client, arguments, []string{}))
target := getTarget(documentContext).document
expectedPath, _ := vespa.IdToUrlPath(documentId)
assert.Equal(t, target+"/document/v1/"+expectedPath, client.lastRequest.URL.String())
assert.Equal(t, "application/json", client.lastRequest.Header.Get("Content-Type"))
assert.Equal(t, "POST", client.lastRequest.Method)
fileContent, _ := ioutil.ReadFile(jsonFile)
assert.Equal(t, string(fileContent), util.ReaderToString(client.lastRequest.Body))
}
func assertDocumentPostShortForm(documentId string, jsonFile string, t *testing.T) {
client := &mockHttpClient{}
assert.Equal(t,
"Success\n",
executeCommand(t, client, []string{"document", jsonFile}, []string{}))
target := getTarget(documentContext).document
assert.Equal(t, target+"/document/v1/"+documentId, client.lastRequest.URL.String())
}
func assertDocumentError(t *testing.T, status int, errorMessage string) {
client := &mockHttpClient{nextStatus: status, nextBody: errorMessage}
assert.Equal(t,
"Invalid document (Status "+strconv.Itoa(status)+"):\n"+errorMessage+"\n",
executeCommand(t, client, []string{"document", "post",
"id:mynamespace:music::head",
"testdata/A-Head-Full-of-Dreams.json"}, []string{}))
}
func assertDocumentServerError(t *testing.T, status int, errorMessage string) {
client := &mockHttpClient{nextStatus: status, nextBody: errorMessage}
assert.Equal(t,
"Error from container (document api) at 127.0.0.1:8080 (Status "+strconv.Itoa(status)+"):\n"+errorMessage+"\n",
executeCommand(t, client, []string{"document", "post",
"id:mynamespace:music::head",
"testdata/A-Head-Full-of-Dreams.json"}, []string{}))
}
|