Java Reference
In-Depth Information
The test starts off by initializing the server's database by calling
populateDB()
. Like
ex-
10_2
, the client interacts with the
StoreResource
JAX-RS service to obtain links to all the
services in the system.
System
.
out
.
println
(
"** Buy an iPhone for Bill Burke"
);
System
.
out
.
println
();
System
.
out
.
println
(
"** First see if Bill Burke exists as a customer"
);
Customers custs
=
client
.
target
(
customers
)
.
queryParam
(
"firstName"
,
"Bill"
)
.
queryParam
(
"lastName"
,
"Burke"
)
.
request
().
get
(
Customers
.
class
);
Customer customer
=
null
null
;
iif
(
custs
.
getCustomers
().
size
() >
0
)
{
System
.
out
.
println
(
"- Found a Bill Burke in the database, using that"
);
customer
=
custs
.
getCustomers
().
iterator
().
next
();
}
else
else
{
System
.
out
.
println
(
"- Cound not find a Bill Burke in the database,
creating one."
);
customer
=
new
new
Customer
();
customer
.
setFirstName
(
"Bill"
);
customer
.
setLastName
(
"Burke"
);
customer
.
setStreet
(
"222 Dartmouth Street"
);
customer
.
setCity
(
"Boston"
);
customer
.
setState
(
"MA"
);
customer
.
setZip
(
"02115"
);
customer
.
setCountry
(
"USA"
);
response
=
client
.
target
(
customers
)
.
request
()
.
post
(
Entity
.
xml
(
customer
));
Assert
.
assertEquals
(
201
,
response
.
getStatus
());
URI uri
=
response
.
getLocation
();
response
.
close
();
customer
=
client
.
target
(
uri
).
request
().
get
(
Customer
.
class
);
}
The client code checks to see if the customer “Bill Burke” already exists. If that customer
doesn't exist, it is created within the customer database.
System
.
out
.
println
();
System
.
out
.
println
(
"Search for iPhone in the Product database"
);
Products prods
=
client
.
target
(
products
)
.
queryParam
(
"name"
,
"iPhone"
)