먼저 기존의코드를 리팩토링을 해보겠다.
Account.java 리팩토링
package me.weekbelt.studyolle.domain;
@Builder @AllArgsConstructor @NoArgsConstructor
@Getter @Setter @EqualsAndHashCode(of = "id")
@Entity
public class Account {
// .........
@ManyToMany
private Set<Tag> tags = new HashSet<>();
// .........
}
AccountService.java 리팩토링
package me.weekbelt.studyolle.account;
@Transactional
@RequiredArgsConstructor
@Service
public class AccountService implements UserDetailsService {
// ...........
public Account processNewAccount(SignUpForm signUpForm) {
Account newAccount = saveNewAccount(signUpForm);
sendSignUpConfirmEmail(newAccount);
return newAccount;
}
private Account saveNewAccount(@Valid SignUpForm signUpForm) {
signUpForm.setPassword(passwordEncoder.encode(signUpForm.getPassword()));
Account account = modelMapper.map(signUpForm, Account.class);
account.generateEmailCheckToken();
return accountRepository.save(account);
}
// ............
}
테스트 코드
package me.weekbelt.studyolle.settings;
@Transactional
@SpringBootTest
@AutoConfigureMockMvc
class SettingsControllerTest {
// .................
@Autowired
ObjectMapper objectMapper;
@Autowired
TagRepository tagRepository;
@Autowired
AccountService accountService;
// .................
@WithAccount("joohyuk")
@DisplayName("계정의 태그 수정 폼")
@Test
public void updateTagForm() throws Exception {
mockMvc.perform(get("/settings/tags"))
.andExpect(view().name("settings/tags"))
.andExpect(model().attributeExists("account"))
.andExpect(model().attributeExists("whitelist"))
.andExpect(model().attributeExists("tags"));
}
@WithAccount("joohyuk")
@DisplayName("계정에 태그 추가")
@Test
public void addTag() throws Exception {
TagForm tagForm = new TagForm();
tagForm.setTagTitle("newTag");
mockMvc.perform(post("/settings/tags/add")
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(tagForm))
.with(csrf()))
.andExpect(status().isOk());
Tag newTag = tagRepository.findByTitle("newTag").get();
assertThat(newTag).isNotNull();
Account joohyuk = accountRepository.findByNickname("joohyuk");
// 만약 @Transactional이 없다면 account는 detached 상태
assertThat(joohyuk.getTags().contains(newTag))
.isTrue();
}
@WithAccount("joohyuk")
@DisplayName("계정에 태그 삭제")
@Test
public void removeTag() throws Exception {
Account joohyuk = accountRepository.findByNickname("joohyuk");
Tag newTag = tagRepository.save(Tag.builder().title("newTag").build());
accountService.addTag(joohyuk, newTag);
assertThat(joohyuk.getTags().contains(newTag)).isTrue();
TagForm tagForm = new TagForm();
tagForm.setTagTitle("newTag");
mockMvc.perform(post("/settings/tags/remove")
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(tagForm))
.with(csrf()))
.andExpect(status().isOk());
assertThat(joohyuk.getTags().contains(newTag)).isFalse();
}
}
참고: https://www.inflearn.com/course/%EC%8A%A4%ED%94%84%EB%A7%81-JPA-%EC%9B%B9%EC%95%B1#
'스프링과 JPA 기반 웹 어플리케이션 개발 > 2부(관심 주제와 지역 정보)' 카테고리의 다른 글
40. 지역 정보 추가 삭제 (0) | 2020.04.23 |
---|---|
39. 지역 도메인 (0) | 2020.04.23 |
37. 관심 주제 자동완성 (0) | 2020.04.22 |
36. 관심 주제 삭제 (0) | 2020.04.22 |
35. 관심 주제 조회 (0) | 2020.04.22 |