public static JSONArray toJSONArray() { JSONArray jsonArray = new JSONArray(); for (Position position : positions) { Map<String, Object> map = new HashMap<String, Object>(); map.put("item_id", position.getItem().getId()); map.put("quantity", position.getQuantity()); if (position.getOptions().size() > 0) { JSONArray options = new JSONArray(); for (Option option : position.getOptions()) { Map<String, Object> m = new HashMap<String, Object>(); m.put("id", option.getId()); options.put(new JSONObject(m)); } map.put("options", options); } if (position.getToppings().size() > 0) { JSONArray toppings = new JSONArray(); for (Topping topping : position.getToppings()) { Map<String, Object> m = new HashMap<String, Object>(); m.put("id", topping.getId()); toppings.put(new JSONObject(m)); } map.put("toppings", toppings); } JSONObject jsonObject = new JSONObject(map); jsonArray.put(jsonObject); } return jsonArray; }
@Test /** Test reconstituting a MenuMessage */ public void testReconstituteMenu() { ArrayList<String> representation = new ArrayList<String>(); representation.add("2"); representation.add("Item type:slice, Menu id:1, description:Slice (thin crust), price:1.75"); representation.add("Item type:slice, Menu id:2, description:Slice (thick crust), price:1.75"); representation.add( "Item type:slice, Menu id:3, description:Slice (Sicilian crust), price:2.25"); representation.add( "Item type:specialty pie, Menu id:4, small:12.50, medium:14.50, large:17.75, " + "description:Tibetan delight with hallucenogenic mushrooms, yak butter, and turnips"); representation.add( "Item type:specialty pie, Menu id:5, small:15.50, medium:20.00, large:24.00, " + "description:Hawaiin luau pie with pineapple, pork meat, hibiscus flowers, and sea salt"); representation.add(""); // empty line separating menu items from toppings representation.add("topping id:1, description:Extra Cheese, price:0.75"); representation.add("topping id:2, description:Anchovies, price:1.25"); representation.add("topping id:3, description:Pepperoni, price:1.00 "); representation.add("END"); PizzaMessage msg = PizzaMessage.reconstitute(representation); assertTrue("reconstituted message is a Menu ", msg instanceof MenuMessage); MenuMessage menu = (MenuMessage) msg; ArrayList<MenuItem> menuItems = menu.getMenuItemList(); assertEquals("number of menu items", 5, menuItems.size()); MenuItem item0 = menuItems.get(0); assertTrue("first menu item is a slice", item0 instanceof SliceMenuItem); SliceMenuItem slice0 = (SliceMenuItem) item0; assertEquals("first menu item id", 1, slice0.getMenuItemId()); assertEquals("first menu item description", "Slice (thin crust)", slice0.getItemDescription()); assertEquals("first menu item price", 1.75, slice0.getBasePrice(), 0.01); MenuItem item1 = menuItems.get(1); assertTrue("second menu item is a slice", item1 instanceof SliceMenuItem); SliceMenuItem slice1 = (SliceMenuItem) item1; assertEquals("second menu item id", 2, slice1.getMenuItemId()); assertEquals( "second menu item description", "Slice (thick crust)", slice1.getItemDescription()); assertEquals("second menu item price", 1.75, slice1.getBasePrice(), 0.01); MenuItem item2 = menuItems.get(2); assertTrue("third menu item is a slice", item2 instanceof SliceMenuItem); SliceMenuItem slice2 = (SliceMenuItem) item2; assertEquals("third menu item id", 3, slice2.getMenuItemId()); assertEquals( "third menu item description", "Slice (Sicilian crust)", slice2.getItemDescription()); assertEquals("third menu item price", 2.25, slice2.getBasePrice(), 0.01); MenuItem item3 = menuItems.get(3); assertTrue("fourth menu item is a specialty pie", item3 instanceof SpecialtyPizzaMenuItem); SpecialtyPizzaMenuItem pie1 = (SpecialtyPizzaMenuItem) item3; assertEquals("fourth menu item id", 4, pie1.getMenuItemId()); assertEquals( "fourth menu item description", "Tibetan delight with hallucenogenic mushrooms, yak butter, and turnips", pie1.getItemDescription()); assertEquals("fourth menu item small price", 12.50, pie1.getSmallPrice(), 0.01); assertEquals("fourth menu item medium price", 14.50, pie1.getMediumPrice(), 0.01); assertEquals("fourth menu item large price", 17.75, pie1.getLargePrice(), 0.01); MenuItem item4 = menuItems.get(4); assertTrue("fifth menu item is a specialty pie", item4 instanceof SpecialtyPizzaMenuItem); SpecialtyPizzaMenuItem pie2 = (SpecialtyPizzaMenuItem) item4; assertEquals("fifth menu item id", 5, pie2.getMenuItemId()); assertEquals( "fifth menu item description", "Hawaiin luau pie with pineapple, pork meat, hibiscus flowers, and sea salt", pie2.getItemDescription()); assertEquals("fifth menu item small price", 15.50, pie2.getSmallPrice(), 0.01); assertEquals("fifth menu item medium price", 20.00, pie2.getMediumPrice(), 0.01); assertEquals("fitth menu item large price", 24.00, pie2.getLargePrice(), 0.01); /* * Check toppings */ ArrayList<Topping> toppings = menu.getToppingList(); assertEquals("number of toppings", 3, toppings.size()); Topping topping0 = toppings.get(0); assertEquals("first topping id", 1, topping0.getId()); assertEquals("first topping description", "Extra Cheese", topping0.getDescription()); assertEquals("first topping price", 0.75, topping0.getPrice(), 0.01); Topping topping1 = toppings.get(1); assertEquals("second topping id", 2, topping1.getId()); assertEquals("second topping description", "Anchovies", topping1.getDescription()); assertEquals("second topping price", 1.25, topping1.getPrice(), 0.01); Topping topping2 = toppings.get(2); assertEquals("third topping id", 3, topping2.getId()); assertEquals("third topping description", "Pepperoni", topping2.getDescription()); assertEquals("third topping price", 1.00, topping2.getPrice(), 0.01); }