1 package xpetstore.services.petstore.test; 2 3 import java.util.Date ; 4 import java.util.HashMap ; 5 import java.util.Iterator ; 6 7 import javax.naming.InitialContext ; 8 9 import javax.ejb.FinderException ; 10 11 import junit.framework.TestCase; 12 13 import xpetstore.domain.catalog.ejb.Category; 14 import xpetstore.domain.catalog.ejb.Item; 15 import xpetstore.domain.catalog.ejb.Product; 16 import xpetstore.domain.customer.ejb.Customer; 17 import xpetstore.domain.order.ejb.OrderItem; 18 import xpetstore.domain.order.ejb.Order; 19 import xpetstore.domain.signon.ejb.Account; 20 21 import xpetstore.services.petstore.ejb.Petstore; 22 23 import xpetstore.util.Page; 24 25 26 29 public class PetstoreTest 30 extends TestCase 31 { 32 34 private static Petstore _petstore; 35 36 38 public PetstoreTest( String arg0 ) 39 { 40 super( arg0 ); 41 } 42 43 45 public void testAuthenticate( ) 49 { 50 boolean result; 51 52 try 53 { 54 result = _petstore.authenticate( "user1", "password1" ); 55 assertEquals( "Unable to authenticate 'user1'", true, result ); 56 } 57 catch ( Exception e ) 58 { 59 fail( "Unable to authenticate. Error=" + e.toString( ) ); 60 } 61 } 62 63 public void testCreateCustomer( ) 67 { 68 try 69 { 70 String userId = "CST-" + ( System.currentTimeMillis( ) % 100000 ); 71 Account act0 = new Account( userId, "password" ); 72 Customer cst0 = new Customer( null, "firstname", "lastname", userId + "@foo.com", "123-456", "en", "street1.1", "street1.2", "city", "CA", "A1A-1A1", "US", "111-111-111", "Visa", "01-11" ); 73 74 String id = _petstore.createCustomer( cst0 ); 76 Customer cst = _petstore.getCustomer( id ); 77 Account act = cst.getAccount( ); 78 79 assertEquals( "firstname", "firstname", cst.getFirstname( ) ); 80 assertEquals( "lastname", "lastname", cst.getLastname( ) ); 81 assertEquals( "telephone", "123-456", cst.getTelephone( ) ); 82 assertEquals( "email", userId + "@foo.com", cst.getEmail( ) ); 83 assertEquals( "language", "en", cst.getLanguage( ) ); 84 assertEquals( "street1", "street1.1", cst.getStreet1( ) ); 85 assertEquals( "street2", "street1.2", cst.getStreet2( ) ); 86 assertEquals( "city", "city", cst.getCity( ) ); 87 assertEquals( "zipcode", "A1A-1A1", cst.getZipcode( ) ); 88 assertEquals( "state", "CA", cst.getState( ) ); 89 assertEquals( "country", "US", cst.getCountry( ) ); 90 assertEquals( "creditCardType", "Visa", cst.getCreditCardType( ) ); 91 assertEquals( "creditCardNumber", "111-111-111", cst.getCreditCardNumber( ) ); 92 assertEquals( "creditCardExpiryDate", "01-11", cst.getCreditCardExpiryDate( ) ); 93 assertEquals( "account.userId", userId, act.getUserId( ) ); 94 assertEquals( "account.password", "password", act.getPassword( ) ); 95 } 96 catch ( Exception e ) 97 { 98 fail( "Unable to create the user. Error=" + e.toString( ) ); 99 } 100 } 101 102 public void testUpdateCustomer( ) 103 { 104 try 105 { 106 String userId = "CST-" + ( System.currentTimeMillis( ) % 1000000 ); 107 Account act0 = new Account( userId, "password" ); 108 Customer cst0 = new Customer( null, "firstnam", "lastnam", userId + "@foo.com", "222-22", "f", "street2.", "street2.", "city", "O", "A1A-1A1", "C", "111-111-111", "Visa", "01-21" ); 109 110 String id = _petstore.createCustomer( cst0 ); 112 113 Account act1 = new Account( userId, "password1" ); 114 Customer cst1 = new Customer( id, "firstname2", "lastname2", userId + "-2@foo.com", "222-222", "fr", "street2.1", "street2.2", "city2", "ON", "A2A-2A2", "CA", "222-222-222", "Amex", "02-22" ); 115 cst1.setAccount( act1 ); 116 117 _petstore.updateCustomer( cst1 ); 119 120 Customer cst = _petstore.getCustomer( id ); 121 Account act = cst.getAccount( ); 122 123 assertEquals( "firstname", "firstname2", cst.getFirstname( ) ); 125 assertEquals( "lastname", "lastname2", cst.getLastname( ) ); 126 assertEquals( "telephone", "222-222", cst.getTelephone( ) ); 127 assertEquals( "email", userId + "-2@foo.com", cst.getEmail( ) ); 128 assertEquals( "language", "fr", cst.getLanguage( ) ); 129 assertEquals( "street1", "street2.1", cst.getStreet1( ) ); 130 assertEquals( "street2", "street2.2", cst.getStreet2( ) ); 131 assertEquals( "city", "city2", cst.getCity( ) ); 132 assertEquals( "zipcode", "A2A-2A2", cst.getZipcode( ) ); 133 assertEquals( "state", "ON", cst.getState( ) ); 134 assertEquals( "country", "CA", cst.getCountry( ) ); 135 assertEquals( "creditCardType", "Amex", cst.getCreditCardType( ) ); 136 assertEquals( "creditCardNumber", "222-222-222", cst.getCreditCardNumber( ) ); 137 assertEquals( "creditCardExpiryDate", "02-22", cst.getCreditCardExpiryDate( ) ); 138 assertEquals( "account.userId", userId, act.getUserId( ) ); 139 assertEquals( "account.password", "password1", act.getPassword( ) ); 140 } 141 catch ( Exception e ) 142 { 143 fail( "Unable to create the customer. Error=" + e.toString( ) ); 144 } 145 } 146 147 public void testGetCategory( ) 151 { 152 try 153 { 154 Category cat = _petstore.getCategory( "FISH" ); 155 assertNotNull( "category[FISH] not found", cat ); 156 assertEquals( "categoryId", "FISH", cat.getCategoryId( ) ); 157 assertEquals( "name", "Fish", cat.getName( ) ); 158 assertEquals( "description", "description of FISH", cat.getDescription( ) ); 159 } 160 catch ( Exception e ) 161 { 162 fail( "Error=" + e.toString( ) ); 163 } 164 } 165 166 public void testGetCategories( ) 167 { 168 try 169 { 170 Page pg = _petstore.getCategories( 0, 10 ); 171 assertNotNull( "page", pg ); 172 assertEquals( "page.size", 5, pg.getSize( ) ); 173 } 174 catch ( Exception e ) 175 { 176 fail( "Error=" + e.toString( ) ); 177 } 178 } 179 180 public void testGetProduct( ) 184 { 185 try 186 { 187 Product prod = _petstore.getProduct( "K9-DL-01" ); 188 assertNotNull( "product[K9-DL-01] not found", prod ); 189 assertEquals( "productId", "K9-DL-01", prod.getProductId( ) ); 190 assertEquals( "name", "Dalmation", prod.getName( ) ); 191 assertEquals( "description", "Great dog for a fire station", prod.getDescription( ) ); 192 } 193 catch ( Exception e ) 194 { 195 fail( "Error=" + e.toString( ) ); 196 } 197 } 198 199 public void testGetProducts( ) 200 { 201 try 202 { 203 Page pg = _petstore.getProducts( "FISH", 0, 10 ); 204 assertNotNull( "page", pg ); 205 assertEquals( "page.size", 4, pg.getSize( ) ); 206 } 207 catch ( Exception e ) 208 { 209 fail( "Error=" + e.toString( ) ); 210 } 211 } 212 213 public void testSearchProducts( ) 214 { 215 try 216 { 217 Page pg = _petstore.searchProducts( "fish", 0, 10 ); 218 assertNotNull( "page", pg ); 219 assertEquals( "page.size", 4, pg.getSize( ) ); 220 } 221 catch ( Exception e ) 222 { 223 fail( "Error=" + e.toString( ) ); 224 } 225 } 226 227 public void testGetItem( ) 231 { 232 try 233 { 234 Item item = _petstore.getItem( "EST-1" ); 235 assertNotNull( "Item[EST-1] not found", item ); 236 assertEquals( "productId", "EST-1", item.getItemId( ) ); 237 assertEquals( "name", "Large", item.getDescription( ) ); 238 assertEquals( "listPrice", 16.50, item.getListPrice( ), 0 ); 239 assertEquals( "unitCost", 10.00, item.getUnitCost( ), 0 ); 240 assertEquals( "itemPath", "fish1.jpg", item.getImagePath( ) ); 241 } 242 catch ( Exception e ) 243 { 244 fail( "Error=" + e.toString( ) ); 245 } 246 } 247 248 public void testGetItems( ) 249 { 250 try 251 { 252 Page pg = _petstore.getItems( "FI-SW-02", 0, 10 ); 253 assertNotNull( "page", pg ); 254 assertEquals( "page.size", 2, pg.getSize( ) ); 255 } 256 catch ( Exception e ) 257 { 258 fail( "Error=" + e.toString( ) ); 259 } 260 } 261 262 public void testOrder( ) 266 { 267 try 268 { 269 HashMap map = new HashMap ( ); 271 map.put( "EST-1", new Integer ( 10 ) ); 272 map.put( "EST-10", new Integer ( 1 ) ); 273 274 String userId = "user1"; 276 277 Date now = new Date ( ); 279 Integer orderUId = _petstore.createOrder( userId, now, map ); 280 Order order = _petstore.getOrder( orderUId ); 281 282 assertNotNull( "order=null", order ); 284 assertEquals( "order.orderUID", orderUId, order.getOrderUId( ) ); 285 assertNotNull( "order.date=null", order.getOrderDate( ) ); 286 assertEquals( "order.date", now.getTime( ) / 100000, order.getOrderDate( ).getTime( ) / 100000, 1000 ); 287 assertEquals( "street1", "street1.1", order.getStreet1( ) ); 288 assertEquals( "street2", "street1.2", order.getStreet2( ) ); 289 assertEquals( "city", "city1", order.getCity( ) ); 290 assertEquals( "state", "ST1", order.getState( ) ); 291 assertEquals( "zipcode", "A1B-1C1", order.getZipcode( ) ); 292 assertEquals( "country", "US", order.getCountry( ) ); 293 assertEquals( "creditCardType", "Visa", order.getCreditCardType( ) ); 294 assertEquals( "creditCardNumber", "111-111-111", order.getCreditCardNumber( ) ); 295 assertEquals( "creditCardExpiryDate", "01-11", order.getCreditCardExpiryDate( ) ); 296 assertNotNull( "order.status=null", order.getStatus( ) ); 297 } 298 catch ( Exception e ) 299 { 300 fail( "Error=" + e.toString( ) ); 301 } 302 } 303 304 public void testOrderItems( ) 305 { 306 try 307 { 308 HashMap map = new HashMap ( ); 310 map.put( "EST-1", new Integer ( 10 ) ); 311 map.put( "EST-10", new Integer ( 1 ) ); 312 313 String userId = "user1"; 315 316 Date now = new Date ( ); 318 Integer orderUId = _petstore.createOrder( userId, now, map ); 319 320 Page page = _petstore.getOrderItems( orderUId, 0, Integer.MAX_VALUE ); 322 assertEquals( "page.size", 2, page.getSize( ) ); 323 324 Iterator it = page.getList( ).iterator( ); 325 326 for ( int i = 0; it.hasNext( ); ) 327 { 328 String prefix = "order#" + i; 329 OrderItem value = ( OrderItem ) it.next( ); 330 Item item = value.getItem( ); 331 332 assertNotNull( prefix + ".item", value.getItem( ) ); 333 334 if ( "EST-1".equals( item.getItemId( ) ) ) 335 { 336 assertEquals( prefix + ".quantity", 10, value.getQuantity( ) ); 337 assertEquals( prefix + ".unitPrice", 16.5, value.getUnitPrice( ), 0 ); 338 } 339 else if ( "EST-10".equals( item.getItemId( ) ) ) 340 { 341 assertEquals( prefix + ".quantity", 1, value.getQuantity( ) ); 342 assertEquals( prefix + ".unitPrice", 28.5, value.getUnitPrice( ), 0 ); 343 } 344 else 345 { 346 fail( prefix + ".id is invalid: " + item.getItemId( ) ); 347 } 348 } 349 } 350 catch ( Exception e ) 351 { 352 fail( "Error=" + e.toString( ) ); 353 } 354 } 355 356 public void testMyOrders( ) 357 { 358 try 359 { 360 HashMap map = new HashMap ( ); 362 map.put( "EST-1", new Integer ( 10 ) ); 363 map.put( "EST-10", new Integer ( 1 ) ); 364 365 String userId = "CST-" + ( System.currentTimeMillis( ) % 1000000 ); 367 Account act0 = new Account( userId, "password" ); 368 Customer cst0 = new Customer( null, "firstname", "lastname", userId + "@localdomain", "123-456", "en", "street1.1", "street1.2", "city", "CA", "A1A-1A1", "US", "111-111-111", "Visa", "01-01-11" ); 369 cst0.setAccount( act0 ); 370 _petstore.createCustomer( cst0 ); 371 372 Date now = new Date ( ); 374 _petstore.createOrder( userId, now, map ); 375 376 _petstore.createOrder( userId, now, map ); 377 378 Page orders = _petstore.getCustomerOrders( userId, 0, 10 ); 379 assertNotNull( "customer[" + userId + "] - orders=null", orders ); 380 assertEquals( "customer[" + userId + "] - orders.size", 1, orders.getSize( ) ); 381 } 382 catch ( Exception e ) 383 { 384 fail( "Error=" + e.toString( ) ); 385 } 386 } 387 388 protected void setUp( ) 392 { 393 if ( _petstore == null ) 394 { 395 try 396 { 397 System.out.println( "Creating the Petstore object" ); 398 _petstore = (Petstore)new InitialContext ().lookup("Petstore"); 399 } 400 catch ( Exception e ) 401 { 402 e.printStackTrace( ); 403 fail( "Unable to create the Petstore object. Error=" + e.toString( ) ); 404 } 405 } 406 } 407 } 408 | Popular Tags |