View Javadoc
1   package com.foxinmy.weixin4j.mp.test;
2   
3   import java.util.ArrayList;
4   import java.util.List;
5   
6   import org.junit.Assert;
7   import org.junit.Before;
8   import org.junit.Test;
9   
10  import com.foxinmy.weixin4j.exception.WeixinException;
11  import com.foxinmy.weixin4j.http.weixin.ApiResult;
12  import com.foxinmy.weixin4j.model.Button;
13  import com.foxinmy.weixin4j.mp.api.MenuApi;
14  import com.foxinmy.weixin4j.mp.model.Menu;
15  import com.foxinmy.weixin4j.mp.model.MenuMatchRule;
16  import com.foxinmy.weixin4j.mp.type.ClientPlatformType;
17  import com.foxinmy.weixin4j.type.ButtonType;
18  
19  /**
20   * 自定义菜单测试
21   * 
22   * @className MenuTest
23   * @author jinyu(foxinmy@gmail.com)
24   * @date 2014年4月10日
25   * @since JDK 1.6
26   */
27  public class MenuTest extends TokenTest {
28  
29  	private MenuApi menuApi;
30  	private List<Button> buttons;
31  
32  	@Before
33  	public void init() {
34  		menuApi = new MenuApi(tokenManager);
35  	}
36  
37  	@Test
38  	public void create() throws WeixinException {
39  		buttons = new ArrayList<Button>();
40  		String domain = "http://wx.jdxg.doubimeizhi.com";
41  		buttons.add(new Button("立即下单", domain, ButtonType.view));
42  
43  		//buttons.add(new Button("个人中心", domain + "/user", ButtonType.view));
44  
45  		Button button = new Button("小哥介绍", domain, ButtonType.view);
46  		button.pushSub(new Button("小哥介绍", "http://x.eqxiu.com/s/89oy462U",
47  				ButtonType.view));
48  		button.pushSub(new Button("小哥官网", "http://www.jdxiaoge.com",
49  				ButtonType.view));
50  		button.pushSub(new Button("兴趣部落",
51  				"http://buluo.qq.com/p/barindex.html?from=share&bid=282651",
52  				ButtonType.view));
53  		button.pushSub(new Button("服务流程", "FLOW", ButtonType.click));
54  		button.pushSub(new Button("在线客服", "KF", ButtonType.click));
55  		//buttons.add(button);
56  
57  		ApiResult result = menuApi.createMenu(buttons);
58  		Assert.assertEquals("0", result.getReturnCode());
59  	}
60  
61  	@Test
62  	public void create1() throws WeixinException {
63  		buttons = new ArrayList<Button>();
64  
65  		Button b1 = new Button("我要订餐", "ORDERING", ButtonType.click);
66  		buttons.add(b1);
67  		Button b2 = new Button("查询订单", "http://www.lendocean.com/order/list",
68  				ButtonType.view);
69  		buttons.add(b2);
70  		Button b3 = new Button("最新资讯", "NEWS", ButtonType.click);
71  		buttons.add(b3);
72  		ApiResult result = menuApi.createMenu(buttons);
73  		Assert.assertEquals("0", result.getReturnCode());
74  	}
75  
76  	@Test
77  	public void get() throws WeixinException {
78  		buttons = menuApi.getMenu();
79  		for (Button btn : buttons) {
80  			System.out.println(btn);
81  		}
82  		Assert.assertEquals(3, buttons.size());
83  		// Button [name=我的门店, type=view,
84  		// content=http://dianzhang.canyi.net/setting/index, subs=[]]
85  		// Button [name=每日签到, type=click, content=CHECKIN, subs=[]]
86  		// Button [name=今日订单, type=null, content=null, subs=[Button [name=今日订单,
87  		// type=view, content=http://dianzhang.canyi.net/order/index, subs=[]],
88  		// Button [name=营业统计, type=view,
89  		// content=http://dianzhang.canyi.net/stats/index, subs=[]]]]
90  
91  	}
92  
93  	@Test
94  	public void delete() throws WeixinException {
95  		ApiResult result = menuApi.deleteMenu();
96  		Assert.assertEquals("0", result.getReturnCode());
97  	}
98  
99  	@Test
100 	public void testCustom() throws WeixinException {
101 		buttons = new ArrayList<Button>();
102 		buttons.add(new Button("only for iphone", "iphone", ButtonType.click));
103 		MenuMatchRule matchRule = new MenuMatchRule();
104 		matchRule.platform(ClientPlatformType.IOS);
105 		Assert.assertNotNull(menuApi.createCustomMenu(buttons, matchRule));
106 	}
107 
108 	@Test
109 	public void testGetAllMenus() throws WeixinException {
110 		List<Menu> menus = menuApi.getAllMenu();
111 		System.err.println(menus);
112 	}
113 
114 	@Test
115 	public void testMatchMenu() throws WeixinException {
116 		List<Button> buttons = menuApi.matchCustomMenu("paihuaing");
117 		System.err.println(buttons);
118 	}
119 }