package cn.freemud.service.impl

import cn.freemud.adapter.ShoppingCartConvertAdapter
import cn.freemud.entities.vo.AddShoppingCartGoodsRequestVo
import com.alibaba.fastjson.JSON
import com.freemud.api.assortment.datamanager.entity.vo.AssortmentCustomerInfoVo
import com.freemud.api.assortment.datamanager.manager.customer.AssortmentCustomerInfoManager
import org.junit.runner.RunWith
import org.mockito.Mockito
import org.powermock.api.mockito.PowerMockito
import org.powermock.core.classloader.annotations.PrepareForTest
import org.powermock.modules.junit4.PowerMockRunner
import org.powermock.modules.junit4.PowerMockRunnerDelegate
import org.spockframework.runtime.Sputnik
import spock.lang.Specification

/**
 * @author : xh.Z
 * @email : fisherman0510@163.com
 * @Date : 2022/3/16 15:16
 * @description : 
 */
@RunWith(PowerMockRunner.class)
@PowerMockRunnerDelegate(Sputnik.class)
@PrepareForTest(ShoppingCartConvertAdapter.class)
class ShoppingCartNewServiceImpt2PowerMockTest extends Specification {
    def shoppingCartNewServiceImpl = new ShoppingCartNewServiceImpl()
    def customerInfoManager = PowerMockito.mock(AssortmentCustomerInfoManager)

    void setup() {
        shoppingCartNewServiceImpl.customerInfoManager = customerInfoManager

        // mock静态类
        PowerMockito.mockStatic(ShoppingCartConvertAdapter.class)

    }

    def "测试 /addGoods"() {
        given: "request 请求参数"
        def addShoppingCartGoodsRequestVo = JSON.parseObject("{\"channelType\":\"saas\",\"channel\":1,\"partnerId\":\"2690\",\"appId\":\"wxa1f272347828dae8\",\"version\":\"2.0.68.7\",\"sessionId\":\"1410a547ea1bf178f17032a9e12ac6606db8cc6f\",\"orderType\":1,\"menuType\":\"saas\",\"shopId\":\"QAJIALE001\",\"spuId\":\"215809930343585821\",\"skuId\":\"\",\"qty\":1,\"categoryId\":\"0101\",\"productMaterialList\":[],\"categoryName\":\"单品\",\"productComboList\":[],\"productGroupList\":[],\"receiveId\":\"\",\"bizType\":null}"
                , AddShoppingCartGoodsRequestVo.class)
        and: "模拟第三方响应结果数据"
        def customerInfoVo = JSON.parseObject("{\"accessToken\":\"\",\"alipayUserId\":\"\",\"appId\":\"wx3f7ca0c75b213380\",\"appSecret\":\"\",\"appsecret\":\"\",\"channel\":\"1\",\"customerId\":\"\",\"devBrand\":\"\",\"devModel\":\"\",\"iappId\":\"7\",\"memberId\":\"3614319825989629832\",\"mobile\":\"17327190802\",\"newMember\":false,\"nickName\":\"SWEET SUGA\",\"openId\":\"o98fP4qjUo7xm-nT-JAaVEk9yBqM\",\"openid\":\"o98fP4qjUo7xm-nT-JAaVEk9yBqM\",\"partnerId\":\"2581\",\"photoUrl\":\"https://thirdwx.qlogo.cn/mmopen/vi_32/zahIlicTNSM0IA0qjOryv6s0SzIhlwW9EpQKvL63GbPhBwficMAibDohDEibSCQU0I5KohibIpp0yL57TWRcVKrRH2A/132\",\"registerTime\":\"2021-02-26 14:10:26\",\"sessionId\":\"eac53a53f8ae62def7f40cb1e47a1bd9791d95a8\",\"sessionKey\":\"GCyyapzbBSM4yXh9AduGOw==\",\"session_key\":\"GCyyapzbBSM4yXh9AduGOw==\",\"storeId\":\"\",\"tableNumber\":\"\",\"thirdPartyMemberID\":\"\",\"thirdPartyPartnerID\":\"\",\"unionId\":\"o7cEm68JPFhbrzqb0a0BcQE-dDVA\",\"wxAppId\":\"wx3f7ca0c75b213380\",\"wxAppid\":\"wx3f7ca0c75b213380\"}"
                , AssortmentCustomerInfoVo.class)
        and: "调三方反馈参数 为自己的参数对象"

        customerInfoManager.getCustomerInfoByObject("eac53a53f8ae62def7f40cb1e47a1bd9791d95a8") >> customerInfoVo

        and: "模拟第三方调用"

        Mockito.when(customerInfoManager.getCustomerInfoByObject(Mockito.any())).thenReturn(customerInfoVo)
        and: "静态方法mock"
        PowerMockito.mockStatic(ShoppingCartConvertAdapter.convent2SkuId(addShoppingCartGoodsRequestVo))
        PowerMockito.mockStatic(ShoppingCartConvertAdapter.convent2CartGoods(addShoppingCartGoodsRequestVo))
    }

}
