Skip to content

Instantly share code, notes, and snippets.

@aliwo
Last active October 24, 2021 10:52
Show Gist options
  • Save aliwo/482b6ba636eb263d619ad50a42e7bd05 to your computer and use it in GitHub Desktop.
Save aliwo/482b6ba636eb263d619ad50a42e7bd05 to your computer and use it in GitHub Desktop.
@Getter
@NoArgsConstructor
@Entity
public class User {
@OneToMany(mappedBy = "user", fetch = FetchType.LAZY)
private List<PartyUser> parties = new ArrayList<>();
}
@Getter
@NoArgsConstructor
@Entity
public class Party {
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "leader_id")
private User leader;
@OneToMany(mappedBy = "party", fetch = FetchType.LAZY)
private List<PartyUser> users = new ArrayList<>();
}
@NoArgsConstructor
@Getter
@Entity
@Table(
name="party_user",
uniqueConstraints={
@UniqueConstraint(
columnNames={"user_id","party_id"}
)
}
)
public class PartyUser extends BaseTimeEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@ManyToOne
@JoinColumn(name = "user_id")
private User user;
@ManyToOne
@JoinColumn(name = "party_id")
private Party party;
@Builder
public PartyUser(User user, Party party) {
this.user = user;
this.party = party;
}
}
// test
@SpringBootTest
@Transactional
class PartyServiceTest {
@Test
void 파티장은_강퇴를_할_수_있다() {
// Given: 내가 바로 파티장. you 는 파티원
User me = userRepository.save(
User.builder()
.nickName("test")
.kakaoId("test-kakao-id")
.build()
);
User you = userRepository.save(
User.builder()
.nickName("test2")
.kakaoId("test-kakao-id2")
.build()
);
Party party = partyRepository.save(Party.builder().leader(me).build());
partyUserRepository.save(PartyUser.builder().user(you).party(party).build());
// When: 파티장이 강퇴를 한다. (party user 삭제)
partyUserRepository.deleteByPartyIdAndUserId(party.getId(), you.getId());
// Then: 파티에서 퇴출 완료!
Party resultParty = partyRepository.getById(party.getId());
assertThat(resultParty.getUsers().stream().map(PartyUser::getUser).anyMatch(u -> u.getId().equals(you.getId()))).isFalse();
// 여기서 assert 가 실패합니다! isTrue 가 나옵니다~
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment