Authored by wujiexiang

添加单元测试

@@ -22,6 +22,11 @@ public class PlatformServiceFeeDefinition { @@ -22,6 +22,11 @@ public class PlatformServiceFeeDefinition {
22 //费用规则 按照threshold从小到大排序 22 //费用规则 按照threshold从小到大排序
23 private List<FeeRule> rules = new ArrayList<>(); 23 private List<FeeRule> rules = new ArrayList<>();
24 24
  25 + public void setRules(List<FeeRule> rules) {
  26 + this.rules = rules;
  27 + sortRules(Comparator.comparing(FeeRule::getThreshold).reversed());
  28 + }
  29 +
25 /** 30 /**
26 * 排序 31 * 排序
27 * 32 *
  1 +package com.yohoufo.order;
  2 +
  3 +import org.mockito.Mockito;
  4 +
  5 +import java.lang.reflect.Field;
  6 +
  7 +public class MockFieldElement {
  8 + private Object bean;
  9 +
  10 + public MockFieldElement(Object bean) {
  11 + this.bean = bean;
  12 + }
  13 +
  14 + public <T> T mock(String fieldName, Class<T> mockClass) {
  15 + // 要模拟的类
  16 + T mockObject = Mockito.mock(mockClass);
  17 + // 指定反射类
  18 + Class<?> clazz = bean.getClass();
  19 + // 获得指定类的属性
  20 + Field field = null;
  21 + try {
  22 + field = clazz.getDeclaredField(fieldName);
  23 + // 值为 true 则指示反射的对象在使用时应该取消 Java 语言访问检查。
  24 + // 值为 false 则指示反射的对象应该实施 Java 语言访问检查。
  25 + // 默认 false
  26 + field.setAccessible(true);
  27 + // 更改私有属性的值
  28 + field.set(bean, mockObject);
  29 + } catch (NoSuchFieldException | IllegalAccessException e) {
  30 + e.printStackTrace();
  31 + }
  32 + return mockObject;
  33 + }
  34 +}
1 package com.yohoufo.order.sellerstat; 1 package com.yohoufo.order.sellerstat;
2 2
  3 +import com.google.common.collect.Lists;
3 import com.yohobuy.ufo.model.order.common.EntrySellerType; 4 import com.yohobuy.ufo.model.order.common.EntrySellerType;
4 import com.yohobuy.ufo.model.order.constants.SkupType; 5 import com.yohobuy.ufo.model.order.constants.SkupType;
5 -import com.yohoufo.dal.order.SellerOrderStatsResultMapper;  
6 import com.yohoufo.dal.order.model.SellerOrderStatsResult; 6 import com.yohoufo.dal.order.model.SellerOrderStatsResult;
7 -import com.yohoufo.order.BaseWebTest; 7 +import com.yohoufo.order.MockFieldElement;
  8 +import com.yohoufo.order.model.bo.PlatformServiceFeeDefinition;
8 import com.yohoufo.order.model.bo.SellerPlatformServiceFee; 9 import com.yohoufo.order.model.bo.SellerPlatformServiceFee;
9 -import com.yohoufo.order.service.stats.StatsUnit; 10 +import com.yohoufo.order.model.bo.SellerServiceFeeRuleDefinition;
  11 +import com.yohoufo.order.service.cache.SellerServiceFeeRuleCacheService;
  12 +import com.yohoufo.order.service.stats.*;
  13 +import com.yohoufo.order.service.stats.impl.SellerOrderStatsConfiguration;
  14 +import com.yohoufo.order.service.stats.impl.SellerOrderStatsConfigurationManager;
10 import com.yohoufo.order.service.support.SellerPlatformServiceFeeSupport; 15 import com.yohoufo.order.service.support.SellerPlatformServiceFeeSupport;
11 -import org.apache.commons.lang3.tuple.Pair;  
12 import org.junit.Assert; 16 import org.junit.Assert;
  17 +import org.junit.Before;
13 import org.junit.Test; 18 import org.junit.Test;
14 -import org.springframework.beans.factory.annotation.Autowired;  
15 - 19 +import org.mockito.Mockito;
  20 +import java.io.IOException;
16 import java.math.BigDecimal; 21 import java.math.BigDecimal;
  22 +import java.util.LinkedList;
  23 +import java.util.List;
17 24
18 /** 25 /**
19 - * 卖家订单统计 26 + * 卖家订单统计
20 */ 27 */
21 -public class SellerOrderQuantityStatsTest extends BaseWebTest { 28 +public class SellerOrderQuantityStatsTest {
  29 +
  30 + private SellerPlatformServiceFeeSupport mainInstance = new SellerPlatformServiceFeeSupport();
22 31
23 - @Autowired  
24 - private SellerPlatformServiceFeeSupport sellerPlatformServiceFeeSupport; 32 + SellerOrderStatsConfigurationManager statsConfigurationManager;
25 33
26 - @Autowired  
27 - private SellerOrderStatsResultMapper sellerOrderStatsResultMapper; 34 + private SellerServiceFeeRuleCacheService sellerServiceFeeRuleCacheService;
28 35
29 int uid = 500031116; 36 int uid = 500031116;
30 37
  38 + String processName = "orderProcessor";
  39 +
  40 + String statCode = "orderStat";
  41 +
  42 + @Before
  43 + public void setup() throws IOException {
  44 + MockFieldElement mainInstanceElement = new MockFieldElement(mainInstance);
  45 + statsConfigurationManager = mainInstanceElement.mock("statsConfigurationManager", SellerOrderStatsConfigurationManager.class);
  46 + sellerServiceFeeRuleCacheService = mainInstanceElement.mock("sellerServiceFeeRuleCacheService", SellerServiceFeeRuleCacheService.class);
  47 + }
  48 +
31 /** 49 /**
32 * 当前周期的服务费比例查询 50 * 当前周期的服务费比例查询
33 */ 51 */
34 @Test 52 @Test
35 - public void test_currentPeriod() { 53 + public void test_currentPeriod_for_instock() {
  54 +
  55 + SellerOrderStatsConfiguration sellerOrderStatsConfiguration = new SellerOrderStatsConfiguration();
  56 + sellerOrderStatsConfiguration.setStatsCode(statCode);
  57 + sellerOrderStatsConfiguration.setStatsProcessorName(processName);
  58 + sellerOrderStatsConfiguration.setSkupTypes(Lists.newArrayList(SkupType.IN_STOCK));
  59 +
  60 + Mockito.when(statsConfigurationManager.getStatsConfig(Mockito.any())).thenReturn(sellerOrderStatsConfiguration);
  61 +
  62 + Mockito.when(statsConfigurationManager.getStatsProcessor(processName)).thenReturn(new StatsProcessor() {
  63 +
  64 + @Override
  65 + public String getName() {
  66 + return processName;
  67 + }
  68 +
  69 + @Override
  70 + public void stat(StatsEntry statsEntry, StatsConfiguration statsConfiguration) {
36 71
37 - //准备数据  
38 - Pair<Integer, Integer> integerIntegerPair = StatsUnit.MONTH.currentPeriodTimeTuple(1, 1);  
39 - SellerOrderStatsResult result = new SellerOrderStatsResult();  
40 - result.setUid(uid);  
41 - result.setEnterType(EntrySellerType.COMMON.getCode());  
42 - result.setStatsCode("OrderQuantityStats");  
43 - result.setQuantity(10);  
44 - result.setBeginTime(integerIntegerPair.getKey());  
45 - result.setEndTime(integerIntegerPair.getValue());  
46 - sellerOrderStatsResultMapper.insert(result); 72 + }
47 73
48 - SellerPlatformServiceFee sellerPlatformServiceFee = sellerPlatformServiceFeeSupport.currentPeriod(uid, SkupType.IN_STOCK); 74 + @Override
  75 + public void updateResult(Object o) {
  76 +
  77 + }
  78 +
  79 + @Override
  80 + public Object getResult(StatsEntry statsEntry, StatsConfiguration statsConfiguration) {
  81 + SellerOrderStatsResult result = new SellerOrderStatsResult();
  82 + result.setEnterType(EntrySellerType.COMMON.getCode());
  83 + result.setStatsCode(statCode);
  84 + result.setQuantity(10);
  85 + return result;
  86 + }
  87 + });
  88 + Mockito.when(sellerServiceFeeRuleCacheService.getRuleDefinitions()).thenReturn(buildRules());
  89 +
  90 + SellerPlatformServiceFee sellerPlatformServiceFee = mainInstance.currentPeriod(uid, SkupType.IN_STOCK);
49 91
50 Assert.assertTrue(sellerPlatformServiceFee != null); 92 Assert.assertTrue(sellerPlatformServiceFee != null);
51 93
52 //0.05 最大比例 94 //0.05 最大比例
53 - Assert.assertTrue(sellerPlatformServiceFee.getFeeRate().doubleValue() < 0.05); 95 + Assert.assertEquals(sellerPlatformServiceFee.getFeeRate().doubleValue(), 0.025, 0);
54 } 96 }
55 97
56 @Test 98 @Test
57 public void test_currentPeriod_feeRate_is_null_for_advance() { 99 public void test_currentPeriod_feeRate_is_null_for_advance() {
58 - BigDecimal feeRate = sellerPlatformServiceFeeSupport.getPlatformServiceFeeRate(uid, SkupType.ADVANCE); 100 +
  101 + Mockito.when(statsConfigurationManager.getStatsConfig(Mockito.any())).thenReturn(SellerOrderStatsConfiguration.emptyConfiguration);
  102 +
  103 + Mockito.when(statsConfigurationManager.getStatsProcessor(SellerOrderStatsConfiguration.emptyConfiguration.getStatsProcessorName())).thenReturn(new StatsProcessor() {
  104 +
  105 + @Override
  106 + public String getName() {
  107 + return processName;
  108 + }
  109 +
  110 + @Override
  111 + public void stat(StatsEntry statsEntry, StatsConfiguration statsConfiguration) {
  112 +
  113 + }
  114 +
  115 + @Override
  116 + public void updateResult(Object o) {
  117 +
  118 + }
  119 +
  120 + @Override
  121 + public Object getResult(StatsEntry statsEntry, StatsConfiguration statsConfiguration) {
  122 + SellerOrderStatsResult result = new SellerOrderStatsResult();
  123 + result.setEnterType(EntrySellerType.COMMON.getCode());
  124 + result.setStatsCode(SellerOrderStatsConfiguration.emptyConfiguration.getStatsCode());
  125 + result.setQuantity(10);
  126 + return result;
  127 + }
  128 + });
  129 + Mockito.when(sellerServiceFeeRuleCacheService.getRuleDefinitions()).thenReturn(buildRules());
  130 +
  131 + BigDecimal feeRate = mainInstance.getPlatformServiceFeeRate(uid, SkupType.ADVANCE);
  132 +
59 Assert.assertTrue(feeRate == null); 133 Assert.assertTrue(feeRate == null);
60 134
61 } 135 }
  136 +
  137 + private List<SellerServiceFeeRuleDefinition> buildRules() {
  138 + List<SellerServiceFeeRuleDefinition> rules = new LinkedList<>();
  139 + rules.add(buildNotEntryRule());
  140 + rules.add(buildCommonRule());
  141 + rules.add(buildSuperRule());
  142 + return rules;
  143 + }
  144 +
  145 + private SellerServiceFeeRuleDefinition buildNotEntryRule() {
  146 + PlatformServiceFeeDefinition psfd = new PlatformServiceFeeDefinition();
  147 + PlatformServiceFeeDefinition.FeeRule one = new PlatformServiceFeeDefinition.FeeRule();
  148 + one.setRate(BigDecimal.valueOf(0.055));
  149 + one.setThreshold(0);
  150 +
  151 + PlatformServiceFeeDefinition.FeeRule two = new PlatformServiceFeeDefinition.FeeRule();
  152 + two.setRate(BigDecimal.valueOf(0.045));
  153 + two.setThreshold(10);
  154 +
  155 + PlatformServiceFeeDefinition.FeeRule three = new PlatformServiceFeeDefinition.FeeRule();
  156 + three.setRate(BigDecimal.valueOf(0.035));
  157 + three.setThreshold(0);
  158 +
  159 + psfd.setRules(Lists.newArrayList(one, two, three));
  160 +
  161 + return SellerServiceFeeRuleDefinition.builder().serviceFeeDefinition(psfd).statsCode(statCode).enterType(EntrySellerType.NOT_ENTRY.getCode()).build();
  162 + }
  163 +
  164 + private SellerServiceFeeRuleDefinition buildCommonRule() {
  165 + PlatformServiceFeeDefinition psfd = new PlatformServiceFeeDefinition();
  166 + PlatformServiceFeeDefinition.FeeRule one = new PlatformServiceFeeDefinition.FeeRule();
  167 + one.setRate(BigDecimal.valueOf(0.035));
  168 + one.setThreshold(0);
  169 +
  170 + PlatformServiceFeeDefinition.FeeRule two = new PlatformServiceFeeDefinition.FeeRule();
  171 + two.setRate(BigDecimal.valueOf(0.025));
  172 + two.setThreshold(10);
  173 +
  174 + PlatformServiceFeeDefinition.FeeRule three = new PlatformServiceFeeDefinition.FeeRule();
  175 + three.setRate(BigDecimal.valueOf(0.015));
  176 + three.setThreshold(20);
  177 +
  178 + psfd.setRules(Lists.newArrayList(one, two, three));
  179 +
  180 + return SellerServiceFeeRuleDefinition.builder().serviceFeeDefinition(psfd).statsCode(statCode).enterType(EntrySellerType.COMMON.getCode()).build();
  181 + }
  182 +
  183 + private SellerServiceFeeRuleDefinition buildSuperRule() {
  184 + PlatformServiceFeeDefinition psfd = new PlatformServiceFeeDefinition();
  185 + PlatformServiceFeeDefinition.FeeRule one = new PlatformServiceFeeDefinition.FeeRule();
  186 + one.setRate(BigDecimal.valueOf(0.025));
  187 + one.setThreshold(0);
  188 +
  189 + PlatformServiceFeeDefinition.FeeRule two = new PlatformServiceFeeDefinition.FeeRule();
  190 + two.setRate(BigDecimal.valueOf(0.015));
  191 + two.setThreshold(10);
  192 +
  193 + PlatformServiceFeeDefinition.FeeRule three = new PlatformServiceFeeDefinition.FeeRule();
  194 + three.setRate(BigDecimal.valueOf(0.005));
  195 + three.setThreshold(0);
  196 +
  197 + psfd.setRules(Lists.newArrayList(one, two, three));
  198 +
  199 + return SellerServiceFeeRuleDefinition.builder().serviceFeeDefinition(psfd).statsCode(statCode).enterType(EntrySellerType.COMMON.getCode()).build();
  200 + }
62 } 201 }