@Before
 public void initTest() {
   coupon = new Coupon();
   coupon.setCode(DEFAULT_CODE);
   coupon.setPassword(DEFAULT_PASSWORD);
   coupon.setCategory(DEFAULT_CATEGORY);
   coupon.setStatus(STATUS.Unused);
 }
  @Test
  @Transactional
  public void updateCoupon() throws Exception {
    // Initialize the database
    couponRepository.saveAndFlush(coupon);

    int databaseSizeBeforeUpdate = couponRepository.findAll().size();

    // Update the coupon
    coupon.setCode(UPDATED_CODE);
    coupon.setPassword(UPDATED_PASSWORD);
    coupon.setCategory(UPDATED_CATEGORY);
    coupon.setStatus(STATUS.Unused);
    restCouponMockMvc
        .perform(
            put("/api/coupons")
                .contentType(TestUtil.APPLICATION_JSON_UTF8)
                .content(TestUtil.convertObjectToJsonBytes(coupon)))
        .andExpect(status().isOk());

    // Validate the Coupon in the database
    List<Coupon> coupons = couponRepository.findAll();
    assertThat(coupons).hasSize(databaseSizeBeforeUpdate);
    Coupon testCoupon = coupons.get(coupons.size() - 1);
    assertThat(testCoupon.getCode()).isEqualTo(UPDATED_CODE);
    assertThat(testCoupon.getPassword()).isEqualTo(UPDATED_PASSWORD);
    assertThat(testCoupon.getCategory()).isEqualTo(UPDATED_CATEGORY);
    assertThat(testCoupon.getStatus()).isEqualTo(UPDATED_STATUS);
  }
  @Test
  @Transactional
  public void getCoupon() throws Exception {
    // Initialize the database
    couponRepository.saveAndFlush(coupon);

    // Get the coupon
    restCouponMockMvc
        .perform(get("/api/coupons/{id}", coupon.getId()))
        .andExpect(status().isOk())
        .andExpect(content().contentType(MediaType.APPLICATION_JSON))
        .andExpect(jsonPath("$.id").value(coupon.getId().intValue()))
        .andExpect(jsonPath("$.code").value(DEFAULT_CODE.toString()))
        .andExpect(jsonPath("$.password").value(DEFAULT_PASSWORD.toString()))
        .andExpect(jsonPath("$.category").value(DEFAULT_CATEGORY.intValue()))
        .andExpect(jsonPath("$.status").value(DEFAULT_STATUS.toString()));
  }
  /** 输入用户信息得到提货码 */
  @RequestMapping("/register")
  public String register(HttpServletRequest request, Model model) throws Exception {

    String mobile = request.getParameter("username");
    String openId = request.getParameter("openid");

    logger.info("mobile: " + mobile + " openId: " + openId);
    /* 判断是否已被注册 */
    if (!cUserService.verfiyMobileAndOpenId(mobile, openId))
      /* 已被注册 */
      return "fail";
    else {
      /* 没被注册,绑定一个提货码 */
      Coupon coupon = cUserService.registerActivity(mobile, openId);
      model.addAttribute("coupon", coupon.getCode());
      return "succeed";
    }
  }
  /** 查询赠卷? (查询赠卷按钮连接的url) */
  @RequestMapping("/query")
  public String query(HttpServletRequest request, Model model) throws Exception {
    /* 通过code得到登录用户信息 */
    String code = request.getParameter("code");

    WxMpOAuth2AccessToken wxMpOAuth2AccessToken =
        WeChatConnector.getMpService().oauth2getAccessToken(code);
    WxMpUser wxMpUser =
        WeChatConnector.getMpService().oauth2getUserInfo(wxMpOAuth2AccessToken, null);

    String openId = wxMpUser.getOpenId();

    Coupon coupon = cUserService.queryActivity(openId);
    if (null == coupon) return "fail";
    else {
      model.addAttribute("coupon", coupon.getCode());
      return "succeed";
    }
  }
  /** 得到提货码 */
  @RequestMapping("/coupon")
  public String getCoupon(HttpServletRequest request, Model model) throws Exception {

    String openId = request.getParameter("openid");
    logger.debug("获取提货码 openId:" + openId);
    /* 判断是否已被注册 */
    CUser user = cUserService.findOneByOpenId(openId);
    if (user != null) {
      /* 已被注册 */
      logger.debug("------userId:" + user.getId());
      model.addAttribute("msg", "您已领取过提货码:");
      model.addAttribute("coupon", user.getCoupons().get(0).getCode());
      return "1436858491142/success";
    } else {
      /* 没被注册,绑定一个提货码 */
      Coupon coupon = cUserService.registerActivity("" + System.currentTimeMillis(), openId);
      logger.debug("------coupon:" + coupon.getCode());
      model.addAttribute("msg", "恭喜,您获得一枚提取码:");
      model.addAttribute("coupon", coupon.getCode());
      return "1436858491142/success";
    }
  }
  @Test
  @Transactional
  public void createCoupon() throws Exception {
    int databaseSizeBeforeCreate = couponRepository.findAll().size();

    // Create the Coupon
    restCouponMockMvc
        .perform(
            post("/api/coupons")
                .contentType(TestUtil.APPLICATION_JSON_UTF8)
                .content(TestUtil.convertObjectToJsonBytes(coupon)))
        .andExpect(status().isCreated());

    // Validate the Coupon in the database
    List<Coupon> coupons = couponRepository.findAll();
    assertThat(coupons).hasSize(databaseSizeBeforeCreate + 1);
    Coupon testCoupon = coupons.get(coupons.size() - 1);
    assertThat(testCoupon.getCode()).isEqualTo(DEFAULT_CODE);
    assertThat(testCoupon.getPassword()).isEqualTo(DEFAULT_PASSWORD);
    assertThat(testCoupon.getCategory()).isEqualTo(DEFAULT_CATEGORY);
    assertThat(testCoupon.getStatus()).isEqualTo(DEFAULT_STATUS);
  }
  @Test
  @Transactional
  public void deleteCoupon() throws Exception {
    // Initialize the database
    couponRepository.saveAndFlush(coupon);

    int databaseSizeBeforeDelete = couponRepository.findAll().size();

    // Get the coupon
    restCouponMockMvc
        .perform(delete("/api/coupons/{id}", coupon.getId()).accept(TestUtil.APPLICATION_JSON_UTF8))
        .andExpect(status().isOk());

    // Validate the database is empty
    List<Coupon> coupons = couponRepository.findAll();
    assertThat(coupons).hasSize(databaseSizeBeforeDelete - 1);
  }
  @Test
  @Transactional
  public void checkCodeIsRequired() throws Exception {
    // Validate the database is empty
    assertThat(couponRepository.findAll()).hasSize(0);
    // set the field null
    coupon.setCode(null);

    // Create the Coupon, which fails.
    restCouponMockMvc
        .perform(
            post("/api/coupons")
                .contentType(TestUtil.APPLICATION_JSON_UTF8)
                .content(TestUtil.convertObjectToJsonBytes(coupon)))
        .andExpect(status().isBadRequest());

    // Validate the database is still empty
    List<Coupon> coupons = couponRepository.findAll();
    assertThat(coupons).hasSize(0);
  }