TestActivityHandler.java 9.47 KB
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);
        }
    }
}