couchdb_test.go 8.63 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/*
Copyright IBM Corp. 2016 All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

		 http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package couchdb

import (
	"encoding/json"
	"fmt"
	"testing"

24
	"github.com/hyperledger/fabric/common/ledger/testutil"
25
	"github.com/hyperledger/fabric/core/ledger/ledgerconfig"
26
	ledgertestutil "github.com/hyperledger/fabric/core/ledger/testutil"
27
28
)

Mari Wade's avatar
Mari Wade committed
29
30
31
//Basic setup to test couch
var connectURL = "localhost:5984"
var badConnectURL = "localhost:5990"
32
var database = "couch_util_testdb"
Mari Wade's avatar
Mari Wade committed
33
34
35
36
37
var username = ""
var password = ""

func cleanup() {
	//create a new connection
38
39
	couchInstance, _ := CreateCouchInstance(connectURL, username, password)
	db, _ := CreateCouchDatabase(*couchInstance, database)
Mari Wade's avatar
Mari Wade committed
40
41
42
43
	//drop the test database
	db.DropDatabase()
}

44
45
46
47
48
49
50
51
52
53
54
55
56
type Asset struct {
	ID        string `json:"_id"`
	Rev       string `json:"_rev"`
	AssetName string `json:"asset_name"`
	Color     string `json:"color"`
	Size      string `json:"size"`
	Owner     string `json:"owner"`
}

var assetJSON = []byte(`{"asset_name":"marble1","color":"blue","size":"35","owner":"jerry"}`)

func TestDBConnectionDef(t *testing.T) {

57
	//call a helper method to load the core.yaml
58
	ledgertestutil.SetupCoreYAMLConfig("./../../../../peer")
59

60
	//create a new connection
61
	_, err := CreateConnectionDefinition(connectURL, "", "")
62
63
64
65
66
67
68
	testutil.AssertNoError(t, err, fmt.Sprintf("Error when trying to create database connection definition"))

}

func TestDBBadConnectionDef(t *testing.T) {

	//create a new connection
69
	_, err := CreateConnectionDefinition("^^^localhost:5984", "", "")
70
71
72
73
74
75
	testutil.AssertError(t, err, fmt.Sprintf("Did not receive error when trying to create database connection definition with a bad hostname"))

}

func TestDBCreateSaveWithoutRevision(t *testing.T) {

76
	if ledgerconfig.IsCouchDBEnabled() == true {
77
78
79
80

		cleanup()
		defer cleanup()

81
82
83
84
		//create a new instance and database object
		couchInstance, err := CreateCouchInstance(connectURL, username, password)
		testutil.AssertNoError(t, err, fmt.Sprintf("Error when trying to create couch instance"))
		db := CouchDatabase{couchInstance: *couchInstance, dbName: database}
85
86
87
88
89
90

		//create a new database
		_, errdb := db.CreateDatabaseIfNotExist()
		testutil.AssertNoError(t, errdb, fmt.Sprintf("Error when trying to create database"))

		//Save the test document
91
		_, saveerr := db.SaveDoc("2", "", assetJSON, nil)
92
93
94
95
96
97
98
		testutil.AssertNoError(t, saveerr, fmt.Sprintf("Error when trying to save a document"))

	}
}

func TestDBBadConnection(t *testing.T) {

99
	if ledgerconfig.IsCouchDBEnabled() == true {
100

101
102
103
104
		//create a new instance and database object
		couchInstance, err := CreateCouchInstance(badConnectURL, username, password)
		testutil.AssertNoError(t, err, fmt.Sprintf("Error when trying to create couch instance"))
		db := CouchDatabase{couchInstance: *couchInstance, dbName: database}
105
106
107
108
109
110

		//create a new database
		_, errdb := db.CreateDatabaseIfNotExist()
		testutil.AssertError(t, errdb, fmt.Sprintf("Error should have been thrown while creating a database with an invalid connecion"))

		//Save the test document
111
		_, saveerr := db.SaveDoc("3", "", assetJSON, nil)
112
113
114
115
116
117
118
119
120
121
122
		testutil.AssertError(t, saveerr, fmt.Sprintf("Error should have been thrown while saving a document with an invalid connecion"))

		//Retrieve the updated test document
		_, _, geterr := db.ReadDoc("3")
		testutil.AssertError(t, geterr, fmt.Sprintf("Error should have been thrown while retrieving a document with an invalid connecion"))

	}
}

func TestDBCreateDatabaseAndPersist(t *testing.T) {

123
	if ledgerconfig.IsCouchDBEnabled() == true {
124
125

		cleanup()
126
		defer cleanup()
127

128
129
130
131
		//create a new instance and database object
		couchInstance, err := CreateCouchInstance(connectURL, username, password)
		testutil.AssertNoError(t, err, fmt.Sprintf("Error when trying to create couch instance"))
		db := CouchDatabase{couchInstance: *couchInstance, dbName: database}
132
133
134
135
136
137
138
139
140
141
142

		//create a new database
		_, errdb := db.CreateDatabaseIfNotExist()
		testutil.AssertNoError(t, errdb, fmt.Sprintf("Error when trying to create database"))

		//Retrieve the info for the new database and make sure the name matches
		dbResp, _, errdb := db.GetDatabaseInfo()
		testutil.AssertNoError(t, errdb, fmt.Sprintf("Error when trying to retrieve database information"))
		testutil.AssertEquals(t, dbResp.DbName, database)

		//Save the test document
143
		_, saveerr := db.SaveDoc("1", "", assetJSON, nil)
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
		testutil.AssertNoError(t, saveerr, fmt.Sprintf("Error when trying to save a document"))

		//Retrieve the test document
		dbGetResp, _, geterr := db.ReadDoc("1")
		testutil.AssertNoError(t, geterr, fmt.Sprintf("Error when trying to retrieve a document"))

		//Unmarshal the document to Asset structure
		assetResp := &Asset{}
		json.Unmarshal(dbGetResp, &assetResp)

		//Verify the owner retrieved matches
		testutil.AssertEquals(t, assetResp.Owner, "jerry")

		//Change owner to bob
		assetResp.Owner = "bob"

		//create a byte array of the JSON
		assetDocUpdated, _ := json.Marshal(assetResp)

		//Save the updated test document
164
		_, saveerr = db.SaveDoc("1", "", assetDocUpdated, nil)
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
		testutil.AssertNoError(t, saveerr, fmt.Sprintf("Error when trying to save the updated document"))

		//Retrieve the updated test document
		dbGetResp, _, geterr = db.ReadDoc("1")
		testutil.AssertNoError(t, geterr, fmt.Sprintf("Error when trying to retrieve a document"))

		//Unmarshal the document to Asset structure
		assetResp = &Asset{}
		json.Unmarshal(dbGetResp, &assetResp)

		//Assert that the update was saved and retrieved
		testutil.AssertEquals(t, assetResp.Owner, "bob")

		//Drop the database
		_, errdbdrop := db.DropDatabase()
		testutil.AssertNoError(t, errdbdrop, fmt.Sprintf("Error dropping database"))

		//Retrieve the info for the new database and make sure the name matches
		_, _, errdbinfo := db.GetDatabaseInfo()
		testutil.AssertError(t, errdbinfo, fmt.Sprintf("Error should have been thrown for missing database"))

	}

}

190
191
func TestDBBadJSON(t *testing.T) {

192
	if ledgerconfig.IsCouchDBEnabled() == true {
193
194

		cleanup()
195
		defer cleanup()
196

197
198
199
200
		//create a new instance and database object
		couchInstance, err := CreateCouchInstance(connectURL, username, password)
		testutil.AssertNoError(t, err, fmt.Sprintf("Error when trying to create couch instance"))
		db := CouchDatabase{couchInstance: *couchInstance, dbName: database}
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222

		//create a new database
		_, errdb := db.CreateDatabaseIfNotExist()
		testutil.AssertNoError(t, errdb, fmt.Sprintf("Error when trying to create database"))

		//Retrieve the info for the new database and make sure the name matches
		dbResp, _, errdb := db.GetDatabaseInfo()
		testutil.AssertNoError(t, errdb, fmt.Sprintf("Error when trying to retrieve database information"))
		testutil.AssertEquals(t, dbResp.DbName, database)

		badJSON := []byte(`{"asset_name"}`)

		//Save the test document
		_, saveerr := db.SaveDoc("1", "", badJSON, nil)
		testutil.AssertError(t, saveerr, fmt.Sprintf("Error should have been thrown for a bad JSON"))

	}

}

func TestDBSaveAttachment(t *testing.T) {

223
	if ledgerconfig.IsCouchDBEnabled() == true {
224
225
226
227
228
229
230
231
232
233
234
235
236

		cleanup()
		defer cleanup()
		byteText := []byte(`This is a test document.  This is only a test`)

		attachment := Attachment{}
		attachment.AttachmentBytes = byteText
		attachment.ContentType = "text/plain"
		attachment.Name = "valueBytes"

		attachments := []Attachment{}
		attachments = append(attachments, attachment)

237
238
239
240
		//create a new instance and database object
		couchInstance, err := CreateCouchInstance(connectURL, username, password)
		testutil.AssertNoError(t, err, fmt.Sprintf("Error when trying to create couch instance"))
		db := CouchDatabase{couchInstance: *couchInstance, dbName: database}
241
242
243
244
245
246
247
248
249
250

		//create a new database
		_, errdb := db.CreateDatabaseIfNotExist()
		testutil.AssertNoError(t, errdb, fmt.Sprintf("Error when trying to create database"))

		//Save the test document
		_, saveerr := db.SaveDoc("10", "", nil, attachments)
		testutil.AssertNoError(t, saveerr, fmt.Sprintf("Error when trying to save a document"))

		//Attempt to retrieve the updated test document with attachments
251
		returnDoc, _, geterr2 := db.ReadDoc("10")
252
253
		testutil.AssertNoError(t, geterr2, fmt.Sprintf("Error when trying to retrieve a document with attachment"))

254
255
256
		//Test to see that the result from CouchDB matches the initial text
		testutil.AssertEquals(t, string(returnDoc), string(byteText))

257
258
	}
}