TestActivityHandler.java
9.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
package com.zhaoonline.coupon.handler;
import com.zhaoonline.coupon.bean.Activity;
import com.zhaoonline.coupon.bean.CommonCouponEntity;
import com.zhaoonline.coupon.bean.CommonCouponSeed;
import com.zhaoonline.coupon.bean.OwnerUser;
import com.zhaoonline.coupon.dispatcher.CoupenDispatchHandler;
import com.zhaoonline.coupon.dispatcher.UserCoupenMappingHandler;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.runners.MockitoJUnitRunner;
import org.mockito.stubbing.Answer;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* Created by ZhaoOnline<br/>
* User: yangyoupeng<br/>
* Date: 2016/12/16<br/>
* Time: 11:18<br/>
* Description:please descript you class
*/
@RunWith(MockitoJUnitRunner.class)
public class TestActivityHandler {
private CoupenDispatchHandler coupenDispatcher = Mockito.mock(CoupenDispatchHandler.class);
private UserCoupenMappingHandler userCoupenMappingHandler= Mockito.mock(UserCoupenMappingHandler.class);
/**
* 同一个用户在同一个Activity中只允许抢一个红包.
* 一个CoupenSeed、2个CoupenEntity
*/
@Test
public void testSameUserOnlyGetOneCoupenIntheSameActivity(){
Activity testActivity= new Activity();
testActivity.setActivityID(1);
CommonCouponSeed seed =new CommonCouponSeed();
seed.setSeedID("testSeedID123456");
seed.setActivity(testActivity);
List<CommonCouponSeed> mockedSeedList=new ArrayList<CommonCouponSeed>();
mockedSeedList.add(seed);
CommonCouponEntity coupenEntity=new CommonCouponEntity();
coupenEntity.setSeed(seed);
coupenEntity.setCoupenID("testCoupenID1234556");
coupenEntity.setAmount(new BigDecimal(880));
CommonCouponEntity coupenEntity2=new CommonCouponEntity();
coupenEntity2.setSeed(seed);
coupenEntity2.setCoupenID("testCoupenID1234552");
coupenEntity2.setAmount(new BigDecimal(880));
List<CommonCouponEntity> mockCoupenEntityList=new ArrayList<CommonCouponEntity>();
mockCoupenEntityList.add(coupenEntity);
mockCoupenEntityList.add(coupenEntity2);
Mockito.when(coupenDispatcher.dispacthCoupenEntityUsingSeed(Mockito.any(CommonCouponSeed.class))).thenReturn(mockCoupenEntityList);
Mockito.when(coupenDispatcher.dispatchCoupenSeedUsingActivity(Mockito.any(Activity.class))).thenReturn(mockedSeedList);
Mockito.when(userCoupenMappingHandler.mappingCoupen2User(Mockito.any(CommonCouponEntity.class),Mockito.any(OwnerUser.class)))
.then(new Answer<CommonCouponEntity>() {
int invokeCount=0;
@Override
public CommonCouponEntity answer(InvocationOnMock invocation) throws Throwable {
//如果invoke>1 调用多次,表示对于同一个用户在同一个活动中进行了多次领取。
if(invokeCount>=1){
return null;
}
invokeCount=invokeCount+1;
Object[] args=invocation.getArguments();
CommonCouponEntity coupen= (CommonCouponEntity) args[0];
OwnerUser owner= (OwnerUser) args[1];
coupen.setOwnerUser(owner);
return coupen;
}
});
OwnerUser owner=new OwnerUser();
owner.setOwnerUserID("testOwner1");
ActivityHandler activityHandler = new ActivityHandler(coupenDispatcher,userCoupenMappingHandler);
CommonCouponEntity coupenOfUserFirstDraw=activityHandler.processRequest(testActivity,owner);
Assert.assertNotNull(coupenOfUserFirstDraw);
Assert.assertEquals("testOwner1",coupenOfUserFirstDraw.getOwnerUser().getOwnerUserID());
CommonCouponEntity coupenOfUserSecondDraw=activityHandler.processRequest(testActivity,owner);
Assert.assertNull(coupenOfUserSecondDraw);
CommonCouponEntity coupenOfUserThirdDraw=activityHandler.processRequest(testActivity,owner);
Assert.assertNull(coupenOfUserThirdDraw);
}
/**
*一个Activity
* 2个CoupenSeed
* 每个CoupenSeed有两个CoupenEntity
*/
@Test
public void testNoEnoughCoupen(){
Activity testActivity= new Activity();
testActivity.setActivityID(1);
String seedID1="testSeedID123456";
String seedID2="testSeedID123457";
CommonCouponSeed seed =new CommonCouponSeed();
seed.setSeedID(seedID1);
seed.setActivity(testActivity);
CommonCouponSeed seed2 =new CommonCouponSeed();
seed2.setSeedID(seedID2);
seed2.setActivity(testActivity);
List<CommonCouponSeed> mockedSeedList=new ArrayList<CommonCouponSeed>();
mockedSeedList.add(seed);
mockedSeedList.add(seed2);
CommonCouponEntity coupenEntity1=new CommonCouponEntity();
coupenEntity1.setSeed(seed);
coupenEntity1.setCoupenID("testCoupenID1234551");
coupenEntity1.setAmount(new BigDecimal(880));
CommonCouponEntity coupenEntity2=new CommonCouponEntity();
coupenEntity2.setSeed(seed);
coupenEntity2.setCoupenID("testCoupenID1234552");
coupenEntity2.setAmount(new BigDecimal(880));
CommonCouponEntity coupenEntity3=new CommonCouponEntity();
coupenEntity3.setSeed(seed2);
coupenEntity3.setCoupenID("testCoupenID1234553");
coupenEntity3.setAmount(new BigDecimal(580));
CommonCouponEntity coupenEntity4=new CommonCouponEntity();
coupenEntity4.setSeed(seed2);
coupenEntity4.setCoupenID("testCoupenID1234554");
coupenEntity4.setAmount(new BigDecimal(580));
List<CommonCouponEntity> mockCoupenEntityList=new ArrayList<CommonCouponEntity>();
mockCoupenEntityList.add(coupenEntity1);
mockCoupenEntityList.add(coupenEntity2);
List<CommonCouponEntity> mockCoupenEntityList2=new ArrayList<CommonCouponEntity>();
mockCoupenEntityList2.add(coupenEntity3);
mockCoupenEntityList2.add(coupenEntity4);
Map<String,List<CommonCouponEntity>> seedCoupenMap=new HashMap<String,List<CommonCouponEntity>>();
seedCoupenMap.put(seedID1,mockCoupenEntityList);
seedCoupenMap.put(seedID2,mockCoupenEntityList2);
//
Mockito.when(coupenDispatcher.dispacthCoupenEntityUsingSeed(Mockito.any(CommonCouponSeed.class)))
.then(new Answer<List<CommonCouponEntity>>() {
Map<String,Integer> invokerCountMap=new HashMap<String, Integer>();
@Override
public List<CommonCouponEntity> answer(InvocationOnMock invocation) throws Throwable {
Object[] args=invocation.getArguments();
CommonCouponSeed coupenSeed= (CommonCouponSeed) args[0];
List<CommonCouponEntity> mockCoupenEntityList = seedCoupenMap.get(coupenSeed.getSeedID());
Integer invokerCount= invokerCountMap.get(coupenSeed.getSeedID());
if(invokerCount == null){
invokerCountMap.put(coupenSeed.getSeedID(),new Integer(0));
}else{
if(invokerCount >=1){
for(CommonCouponEntity coupen :mockCoupenEntityList){
coupen.setOwnerUser(new OwnerUser());
}
}
invokerCountMap.put(coupenSeed.getSeedID(),new Integer(invokerCount.intValue()+1));
}
return mockCoupenEntityList;
}
});
Mockito.when(coupenDispatcher.dispatchCoupenSeedUsingActivity(Mockito.any(Activity.class))).thenReturn(mockedSeedList);
Mockito.when(userCoupenMappingHandler.mappingCoupen2User(Mockito.any(CommonCouponEntity.class),Mockito.any(OwnerUser.class)))
.then(new Answer<CommonCouponEntity>() {
@Override
public CommonCouponEntity answer(InvocationOnMock invocation) throws Throwable {
Object[] args=invocation.getArguments();
CommonCouponEntity coupen= (CommonCouponEntity) args[0];
OwnerUser owner= (OwnerUser) args[1];
coupen.setOwnerUser(owner);
return coupen;
}
});
ActivityHandler activityHandler = new ActivityHandler(coupenDispatcher,userCoupenMappingHandler);
int userCount=4;
for(int i=0;i<userCount;i++){
OwnerUser owner=new OwnerUser();
owner.setOwnerUserID("testOwner"+i);
CommonCouponEntity coupenOfUserFirstDraw=activityHandler.processRequest(testActivity,owner);
Assert.assertNotNull(coupenOfUserFirstDraw);
Assert.assertEquals(owner.getOwnerUserID(),coupenOfUserFirstDraw.getOwnerUser().getOwnerUserID());
}
//更多的用来来抢有限的红包
for(int i=5;i<10;i++){
OwnerUser owner=new OwnerUser();
owner.setOwnerUserID("testOwner"+i);
CommonCouponEntity coupenOfUserFirstDraw=activityHandler.processRequest(testActivity,owner);
Assert.assertNull(coupenOfUserFirstDraw);
}
}
}