TestCoupenHelpOpenHandler.java 3.44 KB
package com.zhaoonline.coupen.handler;


import com.zhaoonline.coupen.bean.*;
import com.zhaoonline.coupen.dispatcher.CoupenDispatchHandler;
import com.zhaoonline.coupen.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.List;

/**
 * Created by ZhaoOnline<br/>
 * User: yangyoupeng<br/>
 * Date: 2016/12/16<br/>
 * Time: 11:19<br/>
 * Description:please descript you class
 */
@RunWith(MockitoJUnitRunner.class)
public class TestCoupenHelpOpenHandler {
    private CoupenDispatchHandler coupenDispatcher = Mockito.mock(CoupenDispatchHandler.class);
    private UserCoupenMappingHandler userCoupenMappingHandler= Mockito.mock(UserCoupenMappingHandler.class);

    /**
     *
     */
    @Test
    public void testProcessRequest(){
        HelpOpenCoupenSeed  seed =new HelpOpenCoupenSeed();
        seed.setSeedID("testSeedID123456");
        List<CommonCoupenSeed> mockedSeedList=new ArrayList<CommonCoupenSeed>();
        mockedSeedList.add(seed);

        OwnerUser owner=new OwnerUser();
        owner.setOwnerUserID("testOwner1");
        CommonCoupenEntity coupenEntity=new CommonCoupenEntity();
        coupenEntity.setOwnerUser(owner);
        coupenEntity.setCoupenID("testCoupenID1234556");
        coupenEntity.setAmount(new BigDecimal(880));
        coupenEntity.setSeed(seed);

        OwnerUser owner2=new OwnerUser();
        owner2.setOwnerUserID("testOwner2");

        CommonCoupenEntity coupenEntity2=new CommonCoupenEntity();
        coupenEntity2.setCoupenID("testCoupenID1234552");
        coupenEntity2.setAmount(new BigDecimal(880));
        coupenEntity2.setSeed(seed);

        OwnerUser helper=new OwnerUser();
        helper.setOwnerUserID("testHelper1");

        List<CommonCoupenEntity> mockCoupenEntityList=new ArrayList<CommonCoupenEntity>();
        mockCoupenEntityList.add(coupenEntity);
        mockCoupenEntityList.add(coupenEntity2);



        Mockito.when(coupenDispatcher.dispacthCoupenEntityUsingSeed(Mockito.any(CommonCoupenSeed.class))).thenReturn(mockCoupenEntityList);

        Mockito.when(coupenDispatcher.dispatchCoupenSeedUsingActivity(Mockito.any(Activity.class))).thenReturn(mockedSeedList);


        Mockito.when(userCoupenMappingHandler.mappingCoupen2User(Mockito.any(CommonCoupenEntity.class),Mockito.any(OwnerUser.class)))
                .then(new Answer<CommonCoupenEntity>() {
                    @Override
                    public CommonCoupenEntity answer(InvocationOnMock invocation) throws Throwable {
                        Object[]  args=invocation.getArguments();
                        CommonCoupenEntity coupen= (CommonCoupenEntity) args[0];
                        OwnerUser owner= (OwnerUser) args[1];
                        coupen.setOwnerUser(owner);
                        return coupen;
                    }
                });

        CoupenHelpOpenHandler helpOpenHandler = new CoupenHelpOpenHandler(coupenDispatcher,userCoupenMappingHandler);


        CommonCoupenEntity helperGetNewCoupenAfterHelpOpen=helpOpenHandler.processRequest(coupenEntity,helper);


        OwnerUser ownerAfterHelpOpen = helperGetNewCoupenAfterHelpOpen.getOwnerUser();

        Assert.assertEquals("testHelper1",ownerAfterHelpOpen.getOwnerUserID());

    }
}