提交 | 用户 | 时间
|
e7c126
|
1 |
package com.iailab.framework.test.core.util; |
H |
2 |
|
|
3 |
import cn.hutool.core.date.LocalDateTimeUtil; |
|
4 |
import cn.hutool.core.util.ArrayUtil; |
|
5 |
import cn.hutool.core.util.RandomUtil; |
|
6 |
import cn.hutool.core.util.StrUtil; |
|
7 |
import com.iailab.framework.common.enums.CommonStatusEnum; |
|
8 |
import uk.co.jemos.podam.api.PodamFactory; |
|
9 |
import uk.co.jemos.podam.api.PodamFactoryImpl; |
|
10 |
|
|
11 |
import java.lang.reflect.Type; |
|
12 |
import java.time.LocalDateTime; |
|
13 |
import java.util.Arrays; |
|
14 |
import java.util.Date; |
|
15 |
import java.util.List; |
|
16 |
import java.util.Set; |
|
17 |
import java.util.function.Consumer; |
|
18 |
import java.util.stream.Collectors; |
|
19 |
import java.util.stream.Stream; |
|
20 |
|
|
21 |
/** |
|
22 |
* 随机工具类 |
|
23 |
* |
|
24 |
* @author iailab |
|
25 |
*/ |
|
26 |
public class RandomUtils { |
|
27 |
|
|
28 |
private static final int RANDOM_STRING_LENGTH = 10; |
|
29 |
|
|
30 |
private static final int TINYINT_MAX = 127; |
|
31 |
|
|
32 |
private static final int RANDOM_DATE_MAX = 30; |
|
33 |
|
|
34 |
private static final int RANDOM_COLLECTION_LENGTH = 5; |
|
35 |
|
|
36 |
private static final PodamFactory PODAM_FACTORY = new PodamFactoryImpl(); |
|
37 |
|
|
38 |
static { |
|
39 |
// 字符串 |
|
40 |
PODAM_FACTORY.getStrategy().addOrReplaceTypeManufacturer(String.class, |
|
41 |
(dataProviderStrategy, attributeMetadata, map) -> randomString()); |
|
42 |
// Integer |
|
43 |
PODAM_FACTORY.getStrategy().addOrReplaceTypeManufacturer(Integer.class, (dataProviderStrategy, attributeMetadata, map) -> { |
|
44 |
// 如果是 status 的字段,返回 0 或 1 |
|
45 |
if ("status".equals(attributeMetadata.getAttributeName())) { |
|
46 |
return RandomUtil.randomEle(CommonStatusEnum.values()).getStatus(); |
|
47 |
} |
|
48 |
// 如果是 type、status 结尾的字段,返回 tinyint 范围 |
|
49 |
if (StrUtil.endWithAnyIgnoreCase(attributeMetadata.getAttributeName(), |
|
50 |
"type", "status", "category", "scope", "result")) { |
|
51 |
return RandomUtil.randomInt(0, TINYINT_MAX + 1); |
|
52 |
} |
|
53 |
return RandomUtil.randomInt(); |
|
54 |
}); |
|
55 |
// LocalDateTime |
|
56 |
PODAM_FACTORY.getStrategy().addOrReplaceTypeManufacturer(LocalDateTime.class, |
|
57 |
(dataProviderStrategy, attributeMetadata, map) -> randomLocalDateTime()); |
|
58 |
// Boolean |
|
59 |
PODAM_FACTORY.getStrategy().addOrReplaceTypeManufacturer(Boolean.class, (dataProviderStrategy, attributeMetadata, map) -> { |
|
60 |
// 如果是 deleted 的字段,返回非删除 |
|
61 |
if ("deleted".equals(attributeMetadata.getAttributeName())) { |
|
62 |
return false; |
|
63 |
} |
|
64 |
return RandomUtil.randomBoolean(); |
|
65 |
}); |
|
66 |
} |
|
67 |
|
|
68 |
public static String randomString() { |
|
69 |
return RandomUtil.randomString(RANDOM_STRING_LENGTH); |
|
70 |
} |
|
71 |
|
|
72 |
public static Long randomLongId() { |
|
73 |
return RandomUtil.randomLong(0, Long.MAX_VALUE); |
|
74 |
} |
|
75 |
|
|
76 |
public static Integer randomInteger() { |
|
77 |
return RandomUtil.randomInt(0, Integer.MAX_VALUE); |
|
78 |
} |
|
79 |
|
|
80 |
public static Date randomDate() { |
|
81 |
return RandomUtil.randomDay(0, RANDOM_DATE_MAX); |
|
82 |
} |
|
83 |
|
|
84 |
public static LocalDateTime randomLocalDateTime() { |
|
85 |
// 设置 Nano 为零的原因,避免 MySQL、H2 存储不到时间戳 |
|
86 |
return LocalDateTimeUtil.of(randomDate()).withNano(0); |
|
87 |
} |
|
88 |
|
|
89 |
public static Short randomShort() { |
|
90 |
return (short) RandomUtil.randomInt(0, Short.MAX_VALUE); |
|
91 |
} |
|
92 |
|
|
93 |
public static <T> Set<T> randomSet(Class<T> clazz) { |
|
94 |
return Stream.iterate(0, i -> i).limit(RandomUtil.randomInt(1, RANDOM_COLLECTION_LENGTH)) |
|
95 |
.map(i -> randomPojo(clazz)).collect(Collectors.toSet()); |
|
96 |
} |
|
97 |
|
|
98 |
public static Integer randomCommonStatus() { |
|
99 |
return RandomUtil.randomEle(CommonStatusEnum.values()).getStatus(); |
|
100 |
} |
|
101 |
|
|
102 |
public static String randomEmail() { |
|
103 |
return randomString() + "@qq.com"; |
|
104 |
} |
|
105 |
|
|
106 |
public static String randomURL() { |
|
107 |
return "https://www.baidu.com/" + randomString(); |
|
108 |
} |
|
109 |
|
|
110 |
@SafeVarargs |
|
111 |
public static <T> T randomPojo(Class<T> clazz, Consumer<T>... consumers) { |
|
112 |
T pojo = PODAM_FACTORY.manufacturePojo(clazz); |
|
113 |
// 非空时,回调逻辑。通过它,可以实现 Pojo 的进一步处理 |
|
114 |
if (ArrayUtil.isNotEmpty(consumers)) { |
|
115 |
Arrays.stream(consumers).forEach(consumer -> consumer.accept(pojo)); |
|
116 |
} |
|
117 |
return pojo; |
|
118 |
} |
|
119 |
|
|
120 |
@SafeVarargs |
|
121 |
public static <T> T randomPojo(Class<T> clazz, Type type, Consumer<T>... consumers) { |
|
122 |
T pojo = PODAM_FACTORY.manufacturePojo(clazz, type); |
|
123 |
// 非空时,回调逻辑。通过它,可以实现 Pojo 的进一步处理 |
|
124 |
if (ArrayUtil.isNotEmpty(consumers)) { |
|
125 |
Arrays.stream(consumers).forEach(consumer -> consumer.accept(pojo)); |
|
126 |
} |
|
127 |
return pojo; |
|
128 |
} |
|
129 |
|
|
130 |
@SafeVarargs |
|
131 |
public static <T> List<T> randomPojoList(Class<T> clazz, Consumer<T>... consumers) { |
|
132 |
int size = RandomUtil.randomInt(1, RANDOM_COLLECTION_LENGTH); |
|
133 |
return Stream.iterate(0, i -> i).limit(size).map(o -> randomPojo(clazz, consumers)) |
|
134 |
.collect(Collectors.toList()); |
|
135 |
} |
|
136 |
|
|
137 |
} |