Authored by wujiexiang

添加单元测试

... ... @@ -22,6 +22,11 @@ public class PlatformServiceFeeDefinition {
//费用规则 按照threshold从小到大排序
private List<FeeRule> rules = new ArrayList<>();
public void setRules(List<FeeRule> rules) {
this.rules = rules;
sortRules(Comparator.comparing(FeeRule::getThreshold).reversed());
}
/**
* 排序
*
... ...
package com.yohoufo.order;
import org.mockito.Mockito;
import java.lang.reflect.Field;
public class MockFieldElement {
private Object bean;
public MockFieldElement(Object bean) {
this.bean = bean;
}
public <T> T mock(String fieldName, Class<T> mockClass) {
// 要模拟的类
T mockObject = Mockito.mock(mockClass);
// 指定反射类
Class<?> clazz = bean.getClass();
// 获得指定类的属性
Field field = null;
try {
field = clazz.getDeclaredField(fieldName);
// 值为 true 则指示反射的对象在使用时应该取消 Java 语言访问检查。
// 值为 false 则指示反射的对象应该实施 Java 语言访问检查。
// 默认 false
field.setAccessible(true);
// 更改私有属性的值
field.set(bean, mockObject);
} catch (NoSuchFieldException | IllegalAccessException e) {
e.printStackTrace();
}
return mockObject;
}
}
... ...
package com.yohoufo.order.sellerstat;
import com.google.common.collect.Lists;
import com.yohobuy.ufo.model.order.common.EntrySellerType;
import com.yohobuy.ufo.model.order.constants.SkupType;
import com.yohoufo.dal.order.SellerOrderStatsResultMapper;
import com.yohoufo.dal.order.model.SellerOrderStatsResult;
import com.yohoufo.order.BaseWebTest;
import com.yohoufo.order.MockFieldElement;
import com.yohoufo.order.model.bo.PlatformServiceFeeDefinition;
import com.yohoufo.order.model.bo.SellerPlatformServiceFee;
import com.yohoufo.order.service.stats.StatsUnit;
import com.yohoufo.order.model.bo.SellerServiceFeeRuleDefinition;
import com.yohoufo.order.service.cache.SellerServiceFeeRuleCacheService;
import com.yohoufo.order.service.stats.*;
import com.yohoufo.order.service.stats.impl.SellerOrderStatsConfiguration;
import com.yohoufo.order.service.stats.impl.SellerOrderStatsConfigurationManager;
import com.yohoufo.order.service.support.SellerPlatformServiceFeeSupport;
import org.apache.commons.lang3.tuple.Pair;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.mockito.Mockito;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.LinkedList;
import java.util.List;
/**
* 卖家订单统计
* 卖家订单统计
*/
public class SellerOrderQuantityStatsTest extends BaseWebTest {
public class SellerOrderQuantityStatsTest {
private SellerPlatformServiceFeeSupport mainInstance = new SellerPlatformServiceFeeSupport();
@Autowired
private SellerPlatformServiceFeeSupport sellerPlatformServiceFeeSupport;
SellerOrderStatsConfigurationManager statsConfigurationManager;
@Autowired
private SellerOrderStatsResultMapper sellerOrderStatsResultMapper;
private SellerServiceFeeRuleCacheService sellerServiceFeeRuleCacheService;
int uid = 500031116;
String processName = "orderProcessor";
String statCode = "orderStat";
@Before
public void setup() throws IOException {
MockFieldElement mainInstanceElement = new MockFieldElement(mainInstance);
statsConfigurationManager = mainInstanceElement.mock("statsConfigurationManager", SellerOrderStatsConfigurationManager.class);
sellerServiceFeeRuleCacheService = mainInstanceElement.mock("sellerServiceFeeRuleCacheService", SellerServiceFeeRuleCacheService.class);
}
/**
* 当前周期的服务费比例查询
*/
@Test
public void test_currentPeriod() {
public void test_currentPeriod_for_instock() {
SellerOrderStatsConfiguration sellerOrderStatsConfiguration = new SellerOrderStatsConfiguration();
sellerOrderStatsConfiguration.setStatsCode(statCode);
sellerOrderStatsConfiguration.setStatsProcessorName(processName);
sellerOrderStatsConfiguration.setSkupTypes(Lists.newArrayList(SkupType.IN_STOCK));
Mockito.when(statsConfigurationManager.getStatsConfig(Mockito.any())).thenReturn(sellerOrderStatsConfiguration);
Mockito.when(statsConfigurationManager.getStatsProcessor(processName)).thenReturn(new StatsProcessor() {
@Override
public String getName() {
return processName;
}
@Override
public void stat(StatsEntry statsEntry, StatsConfiguration statsConfiguration) {
//准备数据
Pair<Integer, Integer> integerIntegerPair = StatsUnit.MONTH.currentPeriodTimeTuple(1, 1);
SellerOrderStatsResult result = new SellerOrderStatsResult();
result.setUid(uid);
result.setEnterType(EntrySellerType.COMMON.getCode());
result.setStatsCode("OrderQuantityStats");
result.setQuantity(10);
result.setBeginTime(integerIntegerPair.getKey());
result.setEndTime(integerIntegerPair.getValue());
sellerOrderStatsResultMapper.insert(result);
}
SellerPlatformServiceFee sellerPlatformServiceFee = sellerPlatformServiceFeeSupport.currentPeriod(uid, SkupType.IN_STOCK);
@Override
public void updateResult(Object o) {
}
@Override
public Object getResult(StatsEntry statsEntry, StatsConfiguration statsConfiguration) {
SellerOrderStatsResult result = new SellerOrderStatsResult();
result.setEnterType(EntrySellerType.COMMON.getCode());
result.setStatsCode(statCode);
result.setQuantity(10);
return result;
}
});
Mockito.when(sellerServiceFeeRuleCacheService.getRuleDefinitions()).thenReturn(buildRules());
SellerPlatformServiceFee sellerPlatformServiceFee = mainInstance.currentPeriod(uid, SkupType.IN_STOCK);
Assert.assertTrue(sellerPlatformServiceFee != null);
//0.05 最大比例
Assert.assertTrue(sellerPlatformServiceFee.getFeeRate().doubleValue() < 0.05);
Assert.assertEquals(sellerPlatformServiceFee.getFeeRate().doubleValue(), 0.025, 0);
}
@Test
public void test_currentPeriod_feeRate_is_null_for_advance() {
BigDecimal feeRate = sellerPlatformServiceFeeSupport.getPlatformServiceFeeRate(uid, SkupType.ADVANCE);
Mockito.when(statsConfigurationManager.getStatsConfig(Mockito.any())).thenReturn(SellerOrderStatsConfiguration.emptyConfiguration);
Mockito.when(statsConfigurationManager.getStatsProcessor(SellerOrderStatsConfiguration.emptyConfiguration.getStatsProcessorName())).thenReturn(new StatsProcessor() {
@Override
public String getName() {
return processName;
}
@Override
public void stat(StatsEntry statsEntry, StatsConfiguration statsConfiguration) {
}
@Override
public void updateResult(Object o) {
}
@Override
public Object getResult(StatsEntry statsEntry, StatsConfiguration statsConfiguration) {
SellerOrderStatsResult result = new SellerOrderStatsResult();
result.setEnterType(EntrySellerType.COMMON.getCode());
result.setStatsCode(SellerOrderStatsConfiguration.emptyConfiguration.getStatsCode());
result.setQuantity(10);
return result;
}
});
Mockito.when(sellerServiceFeeRuleCacheService.getRuleDefinitions()).thenReturn(buildRules());
BigDecimal feeRate = mainInstance.getPlatformServiceFeeRate(uid, SkupType.ADVANCE);
Assert.assertTrue(feeRate == null);
}
private List<SellerServiceFeeRuleDefinition> buildRules() {
List<SellerServiceFeeRuleDefinition> rules = new LinkedList<>();
rules.add(buildNotEntryRule());
rules.add(buildCommonRule());
rules.add(buildSuperRule());
return rules;
}
private SellerServiceFeeRuleDefinition buildNotEntryRule() {
PlatformServiceFeeDefinition psfd = new PlatformServiceFeeDefinition();
PlatformServiceFeeDefinition.FeeRule one = new PlatformServiceFeeDefinition.FeeRule();
one.setRate(BigDecimal.valueOf(0.055));
one.setThreshold(0);
PlatformServiceFeeDefinition.FeeRule two = new PlatformServiceFeeDefinition.FeeRule();
two.setRate(BigDecimal.valueOf(0.045));
two.setThreshold(10);
PlatformServiceFeeDefinition.FeeRule three = new PlatformServiceFeeDefinition.FeeRule();
three.setRate(BigDecimal.valueOf(0.035));
three.setThreshold(0);
psfd.setRules(Lists.newArrayList(one, two, three));
return SellerServiceFeeRuleDefinition.builder().serviceFeeDefinition(psfd).statsCode(statCode).enterType(EntrySellerType.NOT_ENTRY.getCode()).build();
}
private SellerServiceFeeRuleDefinition buildCommonRule() {
PlatformServiceFeeDefinition psfd = new PlatformServiceFeeDefinition();
PlatformServiceFeeDefinition.FeeRule one = new PlatformServiceFeeDefinition.FeeRule();
one.setRate(BigDecimal.valueOf(0.035));
one.setThreshold(0);
PlatformServiceFeeDefinition.FeeRule two = new PlatformServiceFeeDefinition.FeeRule();
two.setRate(BigDecimal.valueOf(0.025));
two.setThreshold(10);
PlatformServiceFeeDefinition.FeeRule three = new PlatformServiceFeeDefinition.FeeRule();
three.setRate(BigDecimal.valueOf(0.015));
three.setThreshold(20);
psfd.setRules(Lists.newArrayList(one, two, three));
return SellerServiceFeeRuleDefinition.builder().serviceFeeDefinition(psfd).statsCode(statCode).enterType(EntrySellerType.COMMON.getCode()).build();
}
private SellerServiceFeeRuleDefinition buildSuperRule() {
PlatformServiceFeeDefinition psfd = new PlatformServiceFeeDefinition();
PlatformServiceFeeDefinition.FeeRule one = new PlatformServiceFeeDefinition.FeeRule();
one.setRate(BigDecimal.valueOf(0.025));
one.setThreshold(0);
PlatformServiceFeeDefinition.FeeRule two = new PlatformServiceFeeDefinition.FeeRule();
two.setRate(BigDecimal.valueOf(0.015));
two.setThreshold(10);
PlatformServiceFeeDefinition.FeeRule three = new PlatformServiceFeeDefinition.FeeRule();
three.setRate(BigDecimal.valueOf(0.005));
three.setThreshold(0);
psfd.setRules(Lists.newArrayList(one, two, three));
return SellerServiceFeeRuleDefinition.builder().serviceFeeDefinition(psfd).statsCode(statCode).enterType(EntrySellerType.COMMON.getCode()).build();
}
}
... ...