package unit import org scalatest FunSuite import nxt import utils TxU

  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
package unit
import org.scalatest.FunSuite
import nxt._
import utils.TxUtils._
import utils.WaitingUtils._
import scala.collection.JavaConversions._
import org.apache.commons.lang.RandomStringUtils
import scala.util.Random
import nxt.crypto.{Crypto, XoredData}
import nxt.DigitalGoodsStore.{Purchase, Goods}
import utils.TxUtils
class DGSSpecification extends FunSuite with NxtBeforeAndAfter with NxtTestingCommons {
case class ListingRequest(name: String, desc: String, tags: String, price: Long, quantity: Int)
private def makeListing(): (Goods, ListingRequest) = {
val name = RandomStringUtils.randomAlphabetic(15)
val desc = RandomStringUtils.randomAlphabetic(50)
val tags = "tag1,tag2"
val price = (Random.nextInt(5) + 1) * Constants.ONE_NXT
val quantity = Random.nextInt(5000) + 10
val req = ListingRequest(name, desc, tags, price, quantity)
val attachment = new Attachment.DigitalGoodsListing(name, desc, tags, quantity, price)
issueTxToGodId(attachment, phrase1)
skipBlocks(BlocksToWait)
(DigitalGoodsStore.getAllGoods.find(_.getName == name).get, req)
}
case class PurchaseRequest(noteText: String, quantity: Long, deadline: Int)
private def makePurchase(goodsId: Long, price: Long): (DigitalGoodsStore.Purchase, PurchaseRequest) = {
val noteText = "this is a note"
val xd = XoredData.encrypt(noteText.getBytes("UTF-8"), Crypto.getPrivateKey(phrase2), Crypto.getPublicKey(phrase1))
val quantity = Random.nextInt(5) + 1
val deadline = Nxt.getBlockchain.getLastBlock.getTimestamp + 86400
val attachment = new Attachment.DigitalGoodsPurchase(goodsId, quantity, price, deadline, xd)
issueTxToGodId(attachment, phrase2)
skipBlocks(BlocksToWait)
val req = PurchaseRequest(noteText, quantity, deadline)
(DigitalGoodsStore.getAllPurchases.filter(_.getGoodsId == goodsId).head, req)
}
test("listing") {
val (g, gl) = makeListing()
def predicate(g: DigitalGoodsStore.Goods, gl: ListingRequest) = g.getSellerId == seller1Id && g.getName == gl.name &&
g.getDescription == gl.desc && g.getPriceNQT == gl.price &&
g.getQuantity == gl.quantity && g.getTags == gl.tags && !g.isDelisted
assert(DigitalGoodsStore.getAllGoods.exists(g => predicate(g, gl)))
}
test("listing - delisting") {
val (g, _) = makeListing()
val id = g.getId
val attachment = new Attachment.DigitalGoodsDelisting(id)
issueTxToGodId(attachment, phrase1)
skipBlocks(BlocksToWait)
assert(DigitalGoodsStore.getGoods(id).isDelisted)
}
test("price change") {
val (g, _) = makeListing()
val id = g.getId
val newPrice = 20 * Constants.ONE_NXT
val attachment = new Attachment.DigitalGoodsPriceChange(id, newPrice)
issueTxToGodId(attachment, phrase1)
skipBlocks(BlocksToWait)
assert(DigitalGoodsStore.getGoods(id).getPriceNQT == newPrice)
}
test("quantity change") {
val (g, gl) = makeListing()
val id = g.getId
val delta1 = 50
val attachment = new Attachment.DigitalGoodsQuantityChange(id, delta1)
issueTxToGodId(attachment, phrase1)
skipBlocks(BlocksToWait)
assert(DigitalGoodsStore.getGoods(id).getQuantity == gl.quantity + delta1)
val delta2 = -2 * (gl.quantity + delta1)
val attachment2 = new Attachment.DigitalGoodsQuantityChange(id, delta2)
issueTxToGodId(attachment2, phrase1)
skipBlocks(BlocksToWait)
assert(DigitalGoodsStore.getGoods(id).getQuantity == 0) //todo: ??
}
test("purchase") {
val (g, _) = makeListing()
val id = g.getId
val price = g.getPriceNQT
val (p, pr) = makePurchase(id, g.getPriceNQT)
val deadline = p.getDeliveryDeadline
val decNote = new String(p.getNote.decrypt(Crypto.getPrivateKey(phrase1), Crypto.getPublicKey(phrase2)), "UTF-8")
assert(p.getBuyerId == buyer1Id && decNote == pr.noteText && p.getPriceNQT == price &&
p.getSellerId == seller1Id && p.getQuantity == pr.quantity && p.getDeliveryDeadline == pr.deadline)
assert(validationExceptionThrown {
val attachment2 = new Attachment.DigitalGoodsPurchase(id, g.getQuantity * 2, price, deadline, p.getNote)
issueTxToGodId(attachment2, phrase2)
})
assert(validationExceptionThrown {
val attachment3 = new Attachment.DigitalGoodsPurchase(id, 2, price, deadline, p.getNote)
issueTx(attachment3, phrase2, seller1Id)
})
}
test("refund") {
val (g, gl) = makeListing()
val (p, pr) = makePurchase(g.getId, g.getPriceNQT)
val xd = XoredData.encrypt(" ".getBytes("UTF-8"), Crypto.getPrivateKey(phrase2), Crypto.getPublicKey(phrase1))
assert(validationExceptionThrown {
issueTxToGodId(new Attachment.DigitalGoodsRefund(p.getId, p.getPriceNQT, xd), phrase2)
})
val attachment = new Attachment.DigitalGoodsRefund(p.getId, p.getPriceNQT, xd)
issueTxToGodId(attachment, phrase1)
skipBlocks(BlocksToWait)
issueTxToGodId(attachment, phrase1)
skipBlocks(BlocksToWait)
val attachment2 = new Attachment.DigitalGoodsDelivery(p.getId, xd, 0)
issueTxToGodId(attachment2, phrase1)
skipBlocks(BlocksToWait)
}
test("feedback") {
val (g, gl) = makeListing()
val (p, pr) = makePurchase(g.getId, g.getPriceNQT)
val xd = XoredData.encrypt(" ".getBytes("UTF-8"), Crypto.getPrivateKey(phrase2), Crypto.getPublicKey(phrase1))
val attachment = new Attachment.DigitalGoodsFeedback(p.getId, xd)
issueTxToGodId(attachment, phrase2)
//todo: finish
}
test("delivery") {
val (g, gl) = makeListing()
val (p, pr) = makePurchase(g.getId, g.getPriceNQT)
assert(DigitalGoodsStore.getPendingPurchase(p.getId) != null)
val xd = XoredData.encrypt(g.getName.getBytes("UTF-8"), Crypto.getPrivateKey(phrase2), Crypto.getPublicKey(phrase1))
assert(validationExceptionThrown {
val attachment = new Attachment.DigitalGoodsDelivery(p.getId, xd, 0)
issueTxToGodId(attachment, phrase2)
})
assert(validationExceptionThrown {
val attachment = new Attachment.DigitalGoodsDelivery(p.getId, xd, p.getQuantity*p.getPriceNQT+1)
issueTxToGodId(attachment, phrase1)
})
assert(validationExceptionThrown {
val attachment = new Attachment.DigitalGoodsDelivery(p.getId+1, xd, p.getPriceNQT)
issueTxToGodId(attachment, phrase1)
})
val attachment = new Attachment.DigitalGoodsDelivery(p.getId, xd, 0)
issueTxToGodId(attachment, phrase1)
skipBlocks(BlocksToWait)
assert(DigitalGoodsStore.getPendingPurchase(p.getId) == null)
}
test("balances consistency"){
skipBlock()
def sellerBalance = Account.getAccount(seller1Id).getBalanceNQT
def buyerBalance = Account.getAccount(buyer1Id).getBalanceNQT
val sellerBalance0 = sellerBalance
val buyerBalance0 = buyerBalance
val (g, gl) = makeListing()
assert(sellerBalance == sellerBalance0 - TxUtils.Fee)
val (p, pr) = makePurchase(g.getId, g.getPriceNQT)
assert(sellerBalance == sellerBalance0 - TxUtils.Fee)
assert(buyerBalance == buyerBalance0 - TxUtils.Fee)
val xd = XoredData.encrypt(g.getName.getBytes("UTF-8"), Crypto.getPrivateKey(phrase2), Crypto.getPublicKey(phrase1))
val attachment2 = new Attachment.DigitalGoodsDelivery(p.getId, xd, 0)
issueTxToGodId(attachment2, phrase1)
skipBlocks(BlocksToWait)
assert(sellerBalance == sellerBalance0 - 2*TxUtils.Fee + p.getPriceNQT*p.getQuantity)
assert(buyerBalance == buyerBalance0 - TxUtils.Fee - p.getPriceNQT*p.getQuantity)
val attachment3 = new Attachment.DigitalGoodsRefund(p.getId, TxUtils.Fee*10, xd)
issueTxToGodId(attachment3, phrase1)
skipBlocks(BlocksToWait)
assert(sellerBalance == sellerBalance0 - 3*TxUtils.Fee + p.getPriceNQT*p.getQuantity - 10*TxUtils.Fee)
assert(buyerBalance == buyerBalance0 - TxUtils.Fee - p.getPriceNQT*p.getQuantity + 10*TxUtils.Fee)
}
}