|
| 1 | +package cn.leancloud; |
| 2 | + |
| 3 | +import cn.leancloud.core.LeanCloud; |
| 4 | +import io.reactivex.Observer; |
| 5 | +import io.reactivex.disposables.Disposable; |
| 6 | +import junit.framework.Test; |
| 7 | +import junit.framework.TestCase; |
| 8 | +import junit.framework.TestSuite; |
| 9 | + |
| 10 | +import java.util.ArrayList; |
| 11 | +import java.util.List; |
| 12 | +import java.util.concurrent.CountDownLatch; |
| 13 | + |
| 14 | +public class LCLeaderboardTests extends TestCase { |
| 15 | + private boolean testSucceed = false; |
| 16 | + private CountDownLatch latch = null; |
| 17 | + public LCLeaderboardTests(String name) { |
| 18 | + super(name); |
| 19 | + Configure.initializeWithApp("dY107uv8CYXDIve1V9", |
| 20 | + "hOx6DFyU9pXmZDDbNL4piUFto2LJGYp9UcfJFcaQ", |
| 21 | + "dy107uv8.cloud.tds1.tapapis.cn"); |
| 22 | + LeanCloud.setLogLevel(LCLogger.Level.DEBUG); |
| 23 | + } |
| 24 | + |
| 25 | + public static Test suite() { |
| 26 | + return new TestSuite(LCLeaderboardTests.class); |
| 27 | + } |
| 28 | + |
| 29 | + @Override |
| 30 | + protected void setUp() throws Exception { |
| 31 | + testSucceed = false; |
| 32 | + latch = new CountDownLatch(1); |
| 33 | + } |
| 34 | + |
| 35 | + @Override |
| 36 | + protected void tearDown() throws Exception { |
| 37 | + ; |
| 38 | + } |
| 39 | + |
| 40 | + public void testResultParse() throws Exception { |
| 41 | + LCLeaderboard leaderboard = LCLeaderboard.createWithoutData("LearderBoard_Test"); |
| 42 | + int skip = 0; |
| 43 | + int limit = 10; |
| 44 | + |
| 45 | + List<String> selectMemberKeys=new ArrayList<>(); |
| 46 | + selectMemberKeys.add("username"); |
| 47 | + List<String> includeStatistics=new ArrayList<>(); |
| 48 | + includeStatistics.add("LeaderBoard_Test_Also"); |
| 49 | + |
| 50 | + leaderboard.getResults(skip, limit, selectMemberKeys, includeStatistics) |
| 51 | + .subscribe(new Observer<LCLeaderboardResult>() { |
| 52 | + @Override |
| 53 | + public void onSubscribe(Disposable disposable) { |
| 54 | + } |
| 55 | + @Override |
| 56 | + public void onNext(LCLeaderboardResult leaderboardResult) { |
| 57 | + // rankings是类的成员变量,用于其他方法访问 |
| 58 | + // 获取的到的数值请见文档末尾 |
| 59 | + List<LCRanking> rankings = leaderboardResult.getResults(); |
| 60 | + // X获取的结果为Null |
| 61 | + List<LCStatistic> X = rankings.get(0).getStatistics(); |
| 62 | + // process rankings |
| 63 | + // 调用获取成功处理... |
| 64 | + if (null != X) { |
| 65 | + for (LCStatistic statistic: X) { |
| 66 | + System.out.println(statistic.toString()); |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + // ... |
| 71 | + testSucceed = true; |
| 72 | + latch.countDown(); |
| 73 | + } |
| 74 | + @Override |
| 75 | + public void onError(Throwable throwable) { |
| 76 | + // handle error |
| 77 | + // 调用获取失败处理... |
| 78 | + // ... |
| 79 | + latch.countDown(); |
| 80 | + } |
| 81 | + @Override |
| 82 | + public void onComplete() { |
| 83 | + } |
| 84 | + }); |
| 85 | + latch.await(); |
| 86 | + assertTrue(testSucceed); |
| 87 | + } |
| 88 | + |
| 89 | + public void testUserGroupResultParse() throws Exception { |
| 90 | + LCLeaderboard leaderboard = LCLeaderboard.createWithoutData("LearderBoard_Test"); |
| 91 | + List<String> userIds=new ArrayList<>(); |
| 92 | + userIds.add("LeaderBoard_Test_1"); |
| 93 | + userIds.add("LeaderBoard_Test_Also"); |
| 94 | + |
| 95 | + leaderboard.queryGroupStatistics(userIds) |
| 96 | + .subscribe(new Observer<LCStatisticResult>() { |
| 97 | + @Override |
| 98 | + public void onSubscribe(Disposable disposable) { |
| 99 | + } |
| 100 | + @Override |
| 101 | + public void onNext(LCStatisticResult leaderboardResult) { |
| 102 | + // X获取的结果为Null |
| 103 | + List<LCStatistic> X = leaderboardResult.getResults(); |
| 104 | + // process rankings |
| 105 | + // 调用获取成功处理... |
| 106 | + if (null != X) { |
| 107 | + for (LCStatistic statistic: X) { |
| 108 | + System.out.println(statistic.toString()); |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + // ... |
| 113 | + testSucceed = true; |
| 114 | + latch.countDown(); |
| 115 | + } |
| 116 | + @Override |
| 117 | + public void onError(Throwable throwable) { |
| 118 | + // handle error |
| 119 | + // 调用获取失败处理... |
| 120 | + // ... |
| 121 | + latch.countDown(); |
| 122 | + } |
| 123 | + @Override |
| 124 | + public void onComplete() { |
| 125 | + } |
| 126 | + }); |
| 127 | + latch.await(); |
| 128 | + assertTrue(testSucceed); |
| 129 | + } |
| 130 | + |
| 131 | + public void testObjectGroupResultParse() throws Exception { |
| 132 | + LCLeaderboard leaderboard = LCLeaderboard.createWithoutData("LearderBoard_Test", LCLeaderboard.MEMBER_TYPE_OBJECT); |
| 133 | + List<String> targetIds=new ArrayList<>(); |
| 134 | + targetIds.add("LeaderBoard_Test_1"); |
| 135 | + targetIds.add("LeaderBoard_Test_Also"); |
| 136 | + |
| 137 | + leaderboard.queryGroupStatistics(targetIds) |
| 138 | + .subscribe(new Observer<LCStatisticResult>() { |
| 139 | + @Override |
| 140 | + public void onSubscribe(Disposable disposable) { |
| 141 | + } |
| 142 | + @Override |
| 143 | + public void onNext(LCStatisticResult leaderboardResult) { |
| 144 | + // X获取的结果为Null |
| 145 | + List<LCStatistic> X = leaderboardResult.getResults(); |
| 146 | + // process rankings |
| 147 | + // 调用获取成功处理... |
| 148 | + if (null != X) { |
| 149 | + for (LCStatistic statistic: X) { |
| 150 | + System.out.println(statistic.toString()); |
| 151 | + } |
| 152 | + } |
| 153 | + |
| 154 | + // ... |
| 155 | + testSucceed = true; |
| 156 | + latch.countDown(); |
| 157 | + } |
| 158 | + @Override |
| 159 | + public void onError(Throwable throwable) { |
| 160 | + // handle error |
| 161 | + // 调用获取失败处理... |
| 162 | + // ... |
| 163 | + latch.countDown(); |
| 164 | + } |
| 165 | + @Override |
| 166 | + public void onComplete() { |
| 167 | + } |
| 168 | + }); |
| 169 | + latch.await(); |
| 170 | + assertTrue(testSucceed); |
| 171 | + } |
| 172 | + |
| 173 | + public void testEntityGroupResultParse() throws Exception { |
| 174 | + LCLeaderboard leaderboard = LCLeaderboard.createWithoutData("LearderBoard_Test", LCLeaderboard.MEMBER_TYPE_ENTITY); |
| 175 | + List<String> targetIds=new ArrayList<>(); |
| 176 | + targetIds.add("LeaderBoard_Test_1"); |
| 177 | + targetIds.add("LeaderBoard_Test_Also"); |
| 178 | + |
| 179 | + leaderboard.queryGroupStatistics(targetIds) |
| 180 | + .subscribe(new Observer<LCStatisticResult>() { |
| 181 | + @Override |
| 182 | + public void onSubscribe(Disposable disposable) { |
| 183 | + } |
| 184 | + @Override |
| 185 | + public void onNext(LCStatisticResult leaderboardResult) { |
| 186 | + // X获取的结果为Null |
| 187 | + List<LCStatistic> X = leaderboardResult.getResults(); |
| 188 | + // process rankings |
| 189 | + // 调用获取成功处理... |
| 190 | + if (null != X) { |
| 191 | + for (LCStatistic statistic: X) { |
| 192 | + System.out.println(statistic.toString()); |
| 193 | + } |
| 194 | + } |
| 195 | + |
| 196 | + // ... |
| 197 | + testSucceed = true; |
| 198 | + latch.countDown(); |
| 199 | + } |
| 200 | + @Override |
| 201 | + public void onError(Throwable throwable) { |
| 202 | + // handle error |
| 203 | + // 调用获取失败处理... |
| 204 | + // ... |
| 205 | + System.out.println(throwable); |
| 206 | + latch.countDown(); |
| 207 | + } |
| 208 | + @Override |
| 209 | + public void onComplete() { |
| 210 | + } |
| 211 | + }); |
| 212 | + latch.await(); |
| 213 | + assertTrue(testSucceed); |
| 214 | + } |
| 215 | +} |
0 commit comments