1+ package me .chanjar .weixin .cp .bean .oa .applydata ;
2+
3+ import me .chanjar .weixin .cp .util .json .WxCpGsonBuilder ;
4+ import org .testng .annotations .Test ;
5+
6+ import java .util .Arrays ;
7+
8+ import static org .testng .Assert .*;
9+
10+ public class ContentValueTipsTest {
11+
12+ @ Test
13+ public void testTipsWithLinks () {
14+ System .out .println ("Testing ContentValue.NewTips structure with Link:" );
15+
16+ // Create a Tips structure with both plain text and link
17+ ContentValue .NewTips tips = new ContentValue .NewTips ();
18+ ContentValue .NewTips .TipsContent tipsContent = new ContentValue .NewTips .TipsContent ();
19+ ContentValue .NewTips .TipsContent .Text text = new ContentValue .NewTips .TipsContent .Text ();
20+
21+ // Create plain text subtext
22+ ContentValue .NewTips .TipsContent .SubText plainSubText = new ContentValue .NewTips .TipsContent .SubText ();
23+ ContentValue .NewTips .TipsContent .SubText .Content plainContent = new ContentValue .NewTips .TipsContent .SubText .Content ();
24+ ContentValue .NewTips .TipsContent .SubText .Content .PlainText plainTextContent =
25+ new ContentValue .NewTips .TipsContent .SubText .Content .PlainText ();
26+ plainTextContent .setContent ("This is plain text. For more info, " );
27+ plainContent .setPlainText (plainTextContent );
28+ plainSubText .setType (1 ); // Type 1 for plain text
29+ plainSubText .setContent (plainContent );
30+
31+ // Create link subtext
32+ ContentValue .NewTips .TipsContent .SubText linkSubText = new ContentValue .NewTips .TipsContent .SubText ();
33+ ContentValue .NewTips .TipsContent .SubText .Content linkContent = new ContentValue .NewTips .TipsContent .SubText .Content ();
34+ ContentValue .NewTips .TipsContent .SubText .Content .Link link =
35+ new ContentValue .NewTips .TipsContent .SubText .Content .Link ();
36+ link .setTitle ("click here" );
37+ link .setUrl ("https://work.weixin.qq.com" );
38+ linkContent .setLink (link );
39+ linkSubText .setType (2 ); // Type 2 for link
40+ linkSubText .setContent (linkContent );
41+
42+ text .setSubText (Arrays .asList (plainSubText , linkSubText ));
43+ tipsContent .setText (text );
44+ tipsContent .setLang ("zh_CN" );
45+
46+ tips .setTipsContent (Arrays .asList (tipsContent ));
47+
48+ // Convert to JSON
49+ String json = WxCpGsonBuilder .create ().toJson (tips );
50+ System .out .println ("Generated JSON:" );
51+ System .out .println (json );
52+
53+ // Try to parse it back
54+ try {
55+ ContentValue .NewTips parsedTips = WxCpGsonBuilder .create ().fromJson (json , ContentValue .NewTips .class );
56+ assertNotNull (parsedTips );
57+ assertNotNull (parsedTips .getTipsContent ());
58+ assertFalse (parsedTips .getTipsContent ().isEmpty ());
59+
60+ ContentValue .NewTips .TipsContent .Text parsedText = parsedTips .getTipsContent ().get (0 ).getText ();
61+ assertNotNull (parsedText );
62+ assertNotNull (parsedText .getSubText ());
63+ assertEquals (parsedText .getSubText ().size (), 2 );
64+
65+ // Verify plain text
66+ assertEquals (parsedText .getSubText ().get (0 ).getType ().intValue (), 1 );
67+ assertEquals (parsedText .getSubText ().get (0 ).getContent ().getPlainText ().getContent (), "This is plain text. For more info, " );
68+
69+ // Verify link
70+ assertEquals (parsedText .getSubText ().get (1 ).getType ().intValue (), 2 );
71+ assertEquals (parsedText .getSubText ().get (1 ).getContent ().getLink ().getTitle (), "click here" );
72+ assertEquals (parsedText .getSubText ().get (1 ).getContent ().getLink ().getUrl (), "https://work.weixin.qq.com" );
73+
74+ System .out .println ("Test passed successfully!" );
75+ } catch (Exception e ) {
76+ System .out .println ("Error parsing: " + e .getMessage ());
77+ e .printStackTrace ();
78+ fail ("Failed to parse JSON: " + e .getMessage ());
79+ }
80+ }
81+ }
0 commit comments