Example #1
0
  /**
   * 单元测试 - 添加菜品信息
   *
   * @throws CategoryNotFoundException
   * @throws DuplicateNameException
   */
  @Test
  public void addDishTest() throws DuplicateNameException, CategoryNotFoundException {
    DishVO dishVO = new DishVO();

    dishVO.setCategoryId(1L);
    dishVO.setMisc("misc");
    dishVO.setName("手撕包菜1");
    dishVO.setPrice(150.0f);
    dishVO.setPicPath("");

    Dish dish = dishService.addDish(dishVO);

    assertNotNull(dish);
    assertTrue(dish.getId() > 0L);
  }
  /**
   * 根据菜品ID查找菜品列表
   *
   * @param categoryId 菜品ID
   * @return 菜品实体列表
   */
  public List<Dish> getDishesByCategoryId(Long categoryId) {
    List<Dish> result = new ArrayList<Dish>();

    Category category = categoryDao.get(categoryId);

    if (null != category) {
      for (Dish dish : category.getDishes()) {
        // REST服务只获取在售的菜品
        if (dish.isOnSell()) {
          result.add(dish);
        }
      }
    }

    return result;
  }
  /**
   * 删除菜品分类
   *
   * @param categoryId 菜品分类id
   * @throws CategoryNotFoundException 菜品分类不存在异常
   */
  @Transactional(
      propagation = Propagation.REQUIRED,
      readOnly = false,
      rollbackFor = Exception.class)
  public void deleteCategory(Long categoryId) throws CategoryNotFoundException {
    // 1. 检查需要删除的菜品分类是否存在
    Category category = this.getCategoryById(categoryId);

    // 2. 将要删除菜品分类下的菜品重置为默认分类
    Category defaultCategory = categoryDao.getCategoryByName(Constants.DEFAULT_CATEGORY);

    if (null == defaultCategory) {
      throw new CategoryNotFoundException(
          MessageUtil.getMessage("category_name_msg", Constants.DEFAULT_CATEGORY));
    } else {
      for (Dish dish : category.getDishes()) {
        dish.setCategory(defaultCategory);
        dishDao.update(dish);
      }
    }

    // 3. 删除菜品分类
    categoryDao.delete(category);
  }
Example #4
0
  /**
   * 单元测试 - 更新菜品信息
   *
   * @throws DishNotFoundException
   * @throws CategoryNotFoundException
   * @throws DuplicateNameException
   */
  @Test
  public void updateDishTest()
      throws DishNotFoundException, CategoryNotFoundException, DuplicateNameException {
    Dish dish = dishService.getDishById(1L);
    DishVO dishVO = new DishVO();

    dishVO.setId(dish.getId());
    dishVO.setCategoryId(dish.getCategory().getId());
    dishVO.setMisc(dish.getMisc());
    dishVO.setName(dish.getName());
    dishVO.setPicPath(dish.getPicPath());
    dishVO.setPrice(dish.getPrice());

    dishService.updateDish(dishVO);
  }
Example #5
0
  /**
   * 单元测试 - 使用错误的菜品分类信息更新菜品
   *
   * @throws DishNotFoundException
   */
  @Test
  public void updateDishWithInvalidCategoryTest() throws DishNotFoundException {
    Dish dish = dishService.getDishById(1L);
    DishVO dishVO = new DishVO();

    dishVO.setId(dish.getId());
    dishVO.setCategoryId(1000L);
    dishVO.setMisc(dish.getMisc());
    dishVO.setName(dish.getName());
    dishVO.setPicPath(dish.getPicPath());
    dishVO.setPrice(dish.getPrice());

    try {
      dishService.updateDish(dishVO);
    } catch (Exception exception) {
      assertEquals(exception.getClass(), CategoryNotFoundException.class);
    }
  }
Example #6
0
  /**
   * 单元测试 - 使用已存在的菜品名更新菜品
   *
   * @throws DishNotFoundException
   */
  @Test
  public void updateDishWithDuplicateNameTest() throws DishNotFoundException {
    String duplicateName = "无油版左公鸡";
    Dish dish = dishService.getDishById(1L);
    DishVO dishVO = new DishVO();

    dishVO.setId(dish.getId());
    dishVO.setCategoryId(dish.getCategory().getId());
    dishVO.setMisc(dish.getMisc());
    dishVO.setName(duplicateName);
    dishVO.setPicPath(dish.getPicPath());
    dishVO.setPrice(dish.getPrice());

    try {
      dishService.updateDish(dishVO);
    } catch (Exception exception) {
      assertEquals(exception.getClass(), DuplicateNameException.class);
    }
  }
Example #7
0
  /**
   * 根据菜品ID查找菜品
   *
   * @param dishId 菜品ID
   * @return 菜品实体
   */
  @GET
  @Path("/{dishId}")
  @Scope("request")
  @Produces(MediaType.APPLICATION_JSON)
  public Map<String, DishVO> getDishById(@PathParam("dishId") Integer dishId) {
    logger.info("[REST_CALL= getDishById, dishId=" + dishId + "]");
    Dish dish = dishService.getDishById(dishId);
    DishVO dishVO = new DishVO();

    if (null != dish) {
      dishVO.setId(dish.getId());
      dishVO.setName(dish.getName());
      dishVO.setPicPath(dish.getPicPath());
      dishVO.setPrice(dish.getPrice());
      dishVO.setOnSell(dish.isOnSell());
    }

    Map<String, DishVO> result = new HashMap<String, DishVO>();
    result.put("dish", dishVO);

    return result;
  }
Example #8
0
  /** 单元测试 - 通过正确的菜品ID获取菜品 */
  @Test
  public void getDishByIdTest() throws DishNotFoundException {
    Dish dish = dishService.getDishById(1L);

    assertEquals(dish.getName(), "手撕包菜");
  }