|
1 | 1 | package NextLevel.demo.query.make; |
2 | 2 |
|
| 3 | +import NextLevel.demo.funding.entity.FreeFundingEntity; |
| 4 | +import NextLevel.demo.funding.entity.OptionFundingEntity; |
| 5 | +import NextLevel.demo.funding.repository.FreeFundingRepository; |
| 6 | +import NextLevel.demo.funding.repository.OptionFundingRepository; |
| 7 | +import NextLevel.demo.funding.service.CouponService; |
| 8 | +import NextLevel.demo.img.service.ImgPath; |
3 | 9 | import NextLevel.demo.img.service.ImgServiceImpl; |
4 | 10 | import NextLevel.demo.project.project.dto.request.CreateProjectDto; |
5 | 11 | import NextLevel.demo.project.project.service.ProjectService; |
|
10 | 16 | import NextLevel.demo.user.service.LoginService; |
11 | 17 | import NextLevel.demo.user.service.UserValidateService; |
12 | 18 | import NextLevel.demo.util.StringUtil; |
13 | | -import org.junit.jupiter.api.Disabled; |
| 19 | +import NextLevel.demo.funding.dto.request.RequestFreeFundingDto; |
| 20 | +import NextLevel.demo.funding.dto.request.RequestOptionFundingDto; |
| 21 | +import NextLevel.demo.funding.service.FundingService; |
| 22 | +import NextLevel.demo.option.OptionEntity; |
| 23 | +import NextLevel.demo.option.OptionRepository; |
| 24 | +import NextLevel.demo.project.project.entity.ProjectEntity; |
| 25 | +import NextLevel.demo.project.project.repository.ProjectRepository; |
| 26 | +import NextLevel.demo.user.entity.UserDetailEntity; |
| 27 | +import NextLevel.demo.user.entity.UserEntity; |
| 28 | +import java.util.Set; |
| 29 | +import java.util.stream.LongStream; |
| 30 | +import org.junit.jupiter.api.AfterEach; |
| 31 | +import org.junit.jupiter.api.MethodOrderer; |
| 32 | +import org.junit.jupiter.api.Order; |
14 | 33 | import org.junit.jupiter.api.Test; |
| 34 | +import org.junit.jupiter.api.TestMethodOrder; |
15 | 35 | import org.junit.jupiter.api.extension.ExtendWith; |
16 | 36 | import org.mockito.Mock; |
17 | 37 | import org.mockito.MockedStatic; |
|
25 | 45 | import org.springframework.security.crypto.password.PasswordEncoder; |
26 | 46 | import org.springframework.test.annotation.Rollback; |
27 | 47 | import org.springframework.test.context.ActiveProfiles; |
28 | | -import org.springframework.transaction.annotation.Transactional; |
| 48 | +import org.springframework.transaction.PlatformTransactionManager; |
| 49 | +import org.springframework.transaction.support.TransactionTemplate; |
29 | 50 | import org.springframework.web.multipart.MultipartFile; |
30 | 51 |
|
31 | 52 | import java.nio.file.Files; |
32 | 53 | import java.nio.file.Path; |
33 | 54 | import java.nio.file.Paths; |
34 | 55 | import java.time.LocalDate; |
35 | | -import java.util.ArrayList; |
36 | 56 | import java.util.List; |
37 | 57 |
|
38 | 58 | @SpringBootTest |
39 | 59 | @ActiveProfiles("query-test") |
40 | 60 | @AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE) |
41 | 61 | @ExtendWith(MockitoExtension.class) |
42 | | -@Disabled |
| 62 | +@TestMethodOrder(MethodOrderer.OrderAnnotation.class) |
43 | 63 | public class MakeTestQuery { |
44 | 64 |
|
45 | 65 | private static final int userCount = 100; |
46 | 66 | private static final int projectCount = 1000; |
47 | 67 |
|
48 | 68 | private LoginService loginService; |
49 | 69 | private ProjectService projectService; |
| 70 | + private UserRepository userRepository; |
| 71 | + private ProjectRepository projectRepository; |
| 72 | + private OptionRepository optionRepository; |
| 73 | + private FundingService fundingService; |
| 74 | + @Autowired |
| 75 | + private OptionFundingRepository optionFundingRepository; |
| 76 | + @Autowired |
| 77 | + private FreeFundingRepository freeFundingRepository; |
50 | 78 |
|
51 | | -// public MakeTestQuery( |
52 | | -// @Autowired UserRepository userRepository, |
53 | | -// @Autowired UserDetailRepository userDetailRepository, |
54 | | -// @Autowired PasswordEncoder passwordEncoder, |
55 | | -// @Autowired UserValidateService userValidateService, |
56 | | -// @Autowired ImgServiceImpl imgService, |
57 | | -// @Mock EmailService emailService, |
58 | | -// |
59 | | -// @Autowired ProjectService projectService |
60 | | -// ) { |
61 | | -// loginService = new LoginService( |
62 | | -// userRepository, |
63 | | -// userDetailRepository, |
64 | | -// emailService, |
65 | | -// imgService, |
66 | | -// passwordEncoder, |
67 | | -// userValidateService |
68 | | -// ); |
69 | | -// this.projectService = projectService; |
70 | | -// mockInit(); |
71 | | -// } |
72 | | -// |
73 | | -// private void mockInit() { |
74 | | -// // String Util.getFormattedNumber -> just return number |
75 | | -// MockedStatic<StringUtil> stringUtilStatic = Mockito.mockStatic(StringUtil.class); |
76 | | -// stringUtilStatic.when(() -> StringUtil.getFormattedNumber(Mockito.anyString(), Mockito.anyString())).thenAnswer(invocation -> invocation.getArgument(0)); |
77 | | -// stringUtilStatic.when(()->StringUtil.toLocalDate(Mockito.anyString())).thenReturn(LocalDate.of(2029, 10, 10)); |
78 | | -// MockedStatic<java.nio.file.Files> fileStatic = Mockito.mockStatic(Files.class); |
79 | | -// fileStatic.when(()->Files.write(Mockito.any(), Mockito.any(byte[].class))).thenReturn(Paths.get("uri")); |
80 | | -// fileStatic.when(()->Files.delete(Mockito.any(Path.class))).thenAnswer((Answer<Void>) invocation -> null); |
81 | | -// } |
82 | | -// |
83 | | -// @Test |
84 | | -// @Transactional |
85 | | -// @Rollback(false) |
86 | | -// public void makeUser100() { |
87 | | -// for(int i = 0; i < userCount; i++) { |
88 | | -// loginService.register(randomUserDto(i), new ArrayList<>()); |
89 | | -// } |
90 | | -// } |
91 | | -// |
92 | | -// private RequestUserCreateDto randomUserDto(int count){ |
93 | | -// RequestUserCreateDto dto = RequestUserCreateDto |
94 | | -// .builder() |
95 | | -// .name("user" + count) |
96 | | -// .nickName("nickname" + count) |
97 | | -// |
98 | | -// .email("email"+count) |
99 | | -// .address("address" + count) |
100 | | -// |
101 | | -// .number("number" + count) |
102 | | -// .areaNumber("areaNumber" + count) |
103 | | -// |
104 | | -// .password("passwrod" + count) |
105 | | -// .build(); |
106 | | -// dto.setKey("key"); |
107 | | -// dto.setImg(new MockMultipartFile("user_img"+count, "img".getBytes())); |
108 | | -// return dto; |
109 | | -// } |
110 | | -// |
111 | | -// @Test |
112 | | -// @Transactional |
113 | | -// @Rollback(false) |
114 | | -// public void makeProject1000() { |
115 | | -// for(int i = 0; i < projectCount; i++) { |
116 | | -// projectService.save(randomProjectDto(i, userCount) , new ArrayList<>()); |
117 | | -// } |
118 | | -// } |
119 | | -// |
120 | | -// private CreateProjectDto randomProjectDto(int count, int userCount) { |
121 | | -// CreateProjectDto dto = new CreateProjectDto(); |
122 | | -// dto.setUserId(Long.valueOf(count % userCount + 1)); // 0이 포함되면 안됨 |
123 | | -// dto.setGoal(Long.valueOf(count % 10 + 1) * 1000); // 1000 ~ 10000 원 |
124 | | -// dto.setStartAt(null); // 오늘로 알아서 처리 |
125 | | -// dto.setExpiredAt("mocked"); // 언젠가... |
126 | | -// dto.setContent("content" + count); |
127 | | -// dto.setTitle("title" + count); |
128 | | -// dto.setTitleImg(Mockito.mock(MockMultipartFile.class, "title_img" + count)); |
129 | | -// dto.setImgs(List.of(new MultipartFile[]{ |
130 | | -// new MockMultipartFile("project"+count+"_img1"+count, "img".getBytes()), |
131 | | -// new MockMultipartFile("project"+count+"_img2"+count, "img".getBytes()), |
132 | | -// new MockMultipartFile("project"+count+"_img3"+count, "img".getBytes()) |
133 | | -// })); |
134 | | -// dto.setTags(List.of(new Long[] { |
135 | | -// Long.valueOf(count % 4 + 1), |
136 | | -// Long.valueOf(count % 4 + 2), |
137 | | -// Long.valueOf(count % 4 + 3) |
138 | | -// })); |
139 | | -// System.out.println(dto.toString()); |
140 | | -// return dto; |
141 | | -// } |
| 79 | + private final TransactionTemplate transactionTemplate; |
| 80 | + |
| 81 | + private MockedStatic<StringUtil> stringUtilStatic; |
| 82 | + private MockedStatic<Files> filesStatic; |
| 83 | + |
| 84 | + public MakeTestQuery( |
| 85 | + @Autowired UserRepository userRepository, |
| 86 | + @Autowired UserDetailRepository userDetailRepository, |
| 87 | + @Autowired PasswordEncoder passwordEncoder, |
| 88 | + @Autowired UserValidateService userValidateService, |
| 89 | + @Autowired ImgServiceImpl imgService, |
| 90 | + @Mock EmailService emailService, |
| 91 | + @Autowired CouponService couponService, |
| 92 | + |
| 93 | + @Autowired ProjectService projectService, |
| 94 | + @Autowired ProjectRepository projectRepository, |
| 95 | + @Autowired OptionRepository optionRepository, |
| 96 | + @Autowired FundingService fundingService, |
| 97 | + |
| 98 | + @Autowired |
| 99 | + PlatformTransactionManager txManager |
| 100 | + ) { |
| 101 | + loginService = new LoginService( |
| 102 | + userRepository, |
| 103 | + userDetailRepository, |
| 104 | + emailService, |
| 105 | + imgService, |
| 106 | + passwordEncoder, |
| 107 | + userValidateService, |
| 108 | + couponService |
| 109 | + ); |
| 110 | + this.projectService = projectService; |
| 111 | + this.userRepository = userRepository; |
| 112 | + this.projectRepository = projectRepository; |
| 113 | + this.optionRepository = optionRepository; |
| 114 | + this.fundingService = fundingService; |
| 115 | + |
| 116 | + this.transactionTemplate = new TransactionTemplate(txManager); |
| 117 | + |
| 118 | + mockInit(); |
| 119 | + } |
| 120 | + |
| 121 | + private void mockInit() { |
| 122 | + stringUtilStatic = Mockito.mockStatic(StringUtil.class); |
| 123 | + stringUtilStatic |
| 124 | + .when(() -> StringUtil.getFormattedNumber(Mockito.anyString(), Mockito.anyString())) |
| 125 | + .thenAnswer(invocation -> invocation.getArgument(0)); |
| 126 | + |
| 127 | + stringUtilStatic |
| 128 | + .when(() -> StringUtil.toLocalDate(Mockito.anyString())) |
| 129 | + .thenReturn(LocalDate.of(2029, 10, 10)); |
| 130 | + |
| 131 | + filesStatic = Mockito.mockStatic(Files.class); |
| 132 | + filesStatic |
| 133 | + .when(() -> Files.write(Mockito.any(), Mockito.any(byte[].class))) |
| 134 | + .thenReturn(Paths.get("uri")); |
| 135 | + |
| 136 | + filesStatic |
| 137 | + .when(() -> Files.delete(Mockito.any(Path.class))) |
| 138 | + .thenAnswer(invocation -> null); |
| 139 | + } |
| 140 | + |
| 141 | + @AfterEach |
| 142 | + void tearDown() { |
| 143 | + if (stringUtilStatic != null) stringUtilStatic.close(); |
| 144 | + if (filesStatic != null) filesStatic.close(); |
| 145 | + } |
| 146 | + |
| 147 | + @Test |
| 148 | + @Rollback(false) |
| 149 | + @Order(1) |
| 150 | + public void makeUser100() { |
| 151 | + for(int i = 0; i < userCount; i++) { |
| 152 | + int userId = i; |
| 153 | + transactionTemplate.executeWithoutResult(status -> { |
| 154 | + UserDetailEntity detail = loginService.register(randomUserDto(userId), new ImgPath()); |
| 155 | + UserEntity user = detail.getUser(); |
| 156 | + user.updatePoint(100000); |
| 157 | + userRepository.save(user); |
| 158 | + }); |
| 159 | + } |
| 160 | + } |
| 161 | + |
| 162 | + private RequestUserCreateDto randomUserDto(int count){ |
| 163 | + RequestUserCreateDto dto = RequestUserCreateDto |
| 164 | + .builder() |
| 165 | + .name("user" + count) |
| 166 | + .nickName("nickname" + count) |
| 167 | + .email("email"+count) |
| 168 | + .address("address" + count) |
| 169 | + .number("number" + count) |
| 170 | + .areaNumber("areaNumber" + count) |
| 171 | + .password("passwrod" + count) |
| 172 | + .build(); |
| 173 | + dto.setKey("key"); |
| 174 | + dto.setImg(new MockMultipartFile("user_img"+count, "img".getBytes())); |
| 175 | + return dto; |
| 176 | + } |
| 177 | + |
| 178 | + @Test |
| 179 | + @Rollback(false) |
| 180 | + @Order(2) |
| 181 | + public void makeProject1000() { |
| 182 | + for(int i = 0; i < projectCount; i++) { |
| 183 | + int projectId = i; |
| 184 | + transactionTemplate.executeWithoutResult(status -> { |
| 185 | + CreateProjectDto dto = randomProjectDto(projectId, userCount); |
| 186 | + ProjectEntity newProject = projectService.save(dto, new ImgPath()); |
| 187 | + |
| 188 | + makeOptions(newProject, List.of(100,200,300)); |
| 189 | + }); |
| 190 | + } |
| 191 | + } |
| 192 | + |
| 193 | + private CreateProjectDto randomProjectDto(int count, int userCount) { |
| 194 | + CreateProjectDto dto = new CreateProjectDto(); |
| 195 | + dto.setUserId(Long.valueOf(count % userCount + 1)); |
| 196 | + dto.setGoal(Long.valueOf(count % 10 + 1) * 1000); |
| 197 | + dto.setStartAt(null); |
| 198 | + dto.setExpiredAt("mocked"); |
| 199 | + dto.setContent("content" + count); |
| 200 | + dto.setTitle("title" + count); |
| 201 | + dto.setTitleImg(Mockito.mock(MockMultipartFile.class, "title_img" + count)); |
| 202 | + dto.setImgs(List.of(new MultipartFile[]{ |
| 203 | + new MockMultipartFile("project"+count+"_img1"+count, "img".getBytes()), |
| 204 | + new MockMultipartFile("project"+count+"_img2"+count, "img".getBytes()), |
| 205 | + new MockMultipartFile("project"+count+"_img3"+count, "img".getBytes()) |
| 206 | + })); |
| 207 | + dto.setTags(List.of( |
| 208 | + Long.valueOf(count % 4 + 1), |
| 209 | + Long.valueOf(count % 4 + 2), |
| 210 | + Long.valueOf(count % 4 + 3) |
| 211 | + )); |
| 212 | + return dto; |
| 213 | + } |
| 214 | + |
| 215 | + private void makeOptions(ProjectEntity projectEntity, List<Integer> priceList) { |
| 216 | + List<OptionEntity> optionList = priceList.stream().map(price -> |
| 217 | + OptionEntity.builder() |
| 218 | + .project(projectEntity) |
| 219 | + .price(price) |
| 220 | + .description(price + " Option") |
| 221 | + .build() |
| 222 | + ).toList(); |
| 223 | + optionRepository.saveAll(optionList); |
| 224 | + } |
| 225 | + |
| 226 | + @Test |
| 227 | + @Rollback(false) |
| 228 | + @Order(3) |
| 229 | + public void makeFunding() { |
| 230 | + List<ProjectEntity> allProjects = projectRepository.findAll(); |
| 231 | + for(ProjectEntity project : allProjects) { |
| 232 | + transactionTemplate.executeWithoutResult(status -> { |
| 233 | + List<Long> funderIdList = LongStream.range(project.getUser().getId(), project.getUser().getId()+20).boxed().toList(); |
| 234 | + List<UserEntity> funderList = userRepository.findAllById(funderIdList); |
| 235 | + |
| 236 | + List<OptionEntity> optionList = optionRepository.findByProjectId(project.getId()); |
| 237 | + |
| 238 | + makeFunding(project, optionList, funderList); |
| 239 | + }); |
| 240 | + } |
| 241 | + } |
| 242 | + |
| 243 | + private void makeFunding(ProjectEntity projectEntity, List<OptionEntity> optionList, List<UserEntity> funderList) { |
| 244 | + List<FreeFundingEntity> freeFundingEntityList = funderList.stream().map(funder-> |
| 245 | + FreeFundingEntity.builder() |
| 246 | + .price(500L) |
| 247 | + .project(projectEntity) |
| 248 | + .user(funder) |
| 249 | + .build() |
| 250 | + ).toList(); |
| 251 | + freeFundingRepository.saveAll(freeFundingEntityList); |
| 252 | + |
| 253 | + List<OptionFundingEntity> optionFundingList = funderList.stream().map(funder -> |
| 254 | + OptionFundingEntity.builder() |
| 255 | + .user(funder) |
| 256 | + .count(1) |
| 257 | + .option(optionList.get(funder.getId().intValue() % 3)) |
| 258 | + .build() |
| 259 | + ).toList(); |
| 260 | + optionFundingRepository.saveAll(optionFundingList); |
| 261 | + } |
142 | 262 |
|
143 | 263 | } |
| 264 | + |
0 commit comments