Skip to content

Commit

Permalink
friendship test with friend request scenario (#37)
Browse files Browse the repository at this point in the history
  • Loading branch information
bviz6542 authored Jun 24, 2024
1 parent 0abe4c9 commit 5254f08
Show file tree
Hide file tree
Showing 12 changed files with 63 additions and 68 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.example.swiftgathering_server.controller;

import com.example.swiftgathering_server.dto.FriendRequestInputDto;
import com.example.swiftgathering_server.dto.FriendRequestCreateDto;
import com.example.swiftgathering_server.dto.FriendRequestOutputDto;
import com.example.swiftgathering_server.dto.FriendRequestUpdateDto;
import com.example.swiftgathering_server.service.FriendRequestService;
Expand All @@ -18,8 +18,8 @@ public class FriendRequestController {
private final FriendRequestService friendRequestService;

@PostMapping
public ResponseEntity<Void> sendFriendRequest(@PathVariable Long memberId, @RequestBody FriendRequestInputDto friendRequestDto) {
friendRequestService.sendFriendRequest(memberId, friendRequestDto);
public ResponseEntity<Void> sendFriendRequest(@PathVariable Long memberId, @RequestBody FriendRequestCreateDto friendRequestCreateDto) {
friendRequestService.sendFriendRequest(memberId, friendRequestCreateDto);
return ResponseEntity.ok().build();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package com.example.swiftgathering_server.dto;

import lombok.AllArgsConstructor;
import lombok.Getter;

@Getter
public class FriendRequestInputDto {
@AllArgsConstructor
public class FriendRequestCreateDto {
private Long receiverId;
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package com.example.swiftgathering_server.dto;

import lombok.AllArgsConstructor;
import lombok.Getter;

@Getter
@AllArgsConstructor
public class FriendRequestUpdateDto {
private Long requestId;
private Boolean isAccepted;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package com.example.swiftgathering_server.dto;

import lombok.AllArgsConstructor;
import lombok.Getter;

@Getter
@AllArgsConstructor
public class RegisterDto {
private String loginId;
private String loginUsername;
private String loginPassword;
private String name;
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@

@Getter
public class ResignDto {
private String loginId;
private String loginUsername;
private String loginPassword;
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ public Optional<Member> findOne(Long id) {
return Optional.ofNullable(member);
}

public Optional<Member> findByLoginId(String loginId) {
return em.createQuery("select m from Member m where m.loginUsername = :loginId", Member.class)
.setParameter("loginId", loginId)
public Optional<Member> findByLoginUsername(String loginUsername) {
return em.createQuery("select m from Member m where m.loginUsername = :loginUsername", Member.class)
.setParameter("loginUsername", loginUsername)
.getResultList()
.stream()
.findAny();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
@RequiredArgsConstructor
public class AuthService {
private final MemberRepository memberRepository;
Expand All @@ -33,7 +35,7 @@ public LoginOutputDto authenticateAndGenerateToken(LoginInputDto loginInputDto)

String token = jwtUtil.createJwt(username, role, 60 * 60 * 1000L);

Long memberId = memberRepository.findByLoginId(username)
Long memberId = memberRepository.findByLoginUsername(username)
.orElseThrow(() -> new RuntimeException("Invalid login ID or password."))
.getId();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,18 @@
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
@RequiredArgsConstructor
public class CustomUserDetailsService implements UserDetailsService {

private final MemberRepository memberRepository;

@Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
Member member = memberRepository.findByLoginId(username)
Member member = memberRepository.findByLoginUsername(username)
.orElseThrow(() -> new AuthenticationException("Invalid login ID or password."));
return new CustomUserDetails(member);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import com.example.swiftgathering_server.domain.FriendRequest;
import com.example.swiftgathering_server.domain.Friendship;
import com.example.swiftgathering_server.domain.Member;
import com.example.swiftgathering_server.dto.FriendRequestInputDto;
import com.example.swiftgathering_server.dto.FriendRequestCreateDto;
import com.example.swiftgathering_server.dto.FriendRequestOutputDto;
import com.example.swiftgathering_server.dto.FriendRequestUpdateDto;
import com.example.swiftgathering_server.repository.FriendRequestRepository;
Expand All @@ -25,8 +25,8 @@ public class FriendRequestService {
private final MemberRepository memberRepository;
private final FriendshipRepository friendshipRepository;

public void sendFriendRequest(Long memberId, FriendRequestInputDto friendRequestDto) {
Long receiverId = friendRequestDto.getReceiverId();
public Long sendFriendRequest(Long memberId, FriendRequestCreateDto friendRequestCreateDto) {
Long receiverId = friendRequestCreateDto.getReceiverId();
Member sender = memberRepository.findOne(memberId)
.orElseThrow(() -> new IllegalArgumentException("No member found with ID: " + memberId));
Member receiver = memberRepository.findOne(receiverId)
Expand All @@ -36,7 +36,7 @@ public void sendFriendRequest(Long memberId, FriendRequestInputDto friendRequest
.receiverMember(receiver)
.requestStatus(FriendRequest.RequestStatus.PENDING)
.build();
friendRequestRepository.save(friendRequest);
return friendRequestRepository.save(friendRequest);
}

@Transactional(readOnly = true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ public class GatheringSessionService {

private final GatheringSessionRepository gatheringSessionRepository;
private final FlagLocationRepository flagLocationRepository;
// private final RabbitAdmin rabbitAdmin;
private final AmqpTemplate amqpTemplate;

public void createSession(CreateSessionRequestDto requestDto) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package com.example.swiftgathering_server.service;

import com.example.swiftgathering_server.domain.Member;
import com.example.swiftgathering_server.dto.LoginInputDto;
import com.example.swiftgathering_server.dto.MyInfoDto;
import com.example.swiftgathering_server.dto.RegisterDto;
import com.example.swiftgathering_server.dto.ResignDto;
import com.example.swiftgathering_server.repository.MemberRepository;
Expand All @@ -22,35 +20,26 @@ public class MemberService {
private final BCryptPasswordEncoder bCryptPasswordEncoder;

public Long register(RegisterDto registerDto) {
memberRepository.findByLoginId(registerDto.getLoginId())
memberRepository.findByLoginUsername(registerDto.getLoginUsername())
.ifPresent(m -> {
throw new EntityExistsException("Login ID already in use: " + registerDto.getLoginId());
throw new EntityExistsException("Login ID already in use: " + registerDto.getLoginUsername());
});

String encodedPassword = bCryptPasswordEncoder.encode(registerDto.getLoginPassword());
Member member = Member.builder()
.loginUsername(registerDto.getLoginId())
.loginUsername(registerDto.getLoginUsername())
.loginPassword(encodedPassword)
.name(registerDto.getName())
.build();
return memberRepository.save(member);
}

public void resign(ResignDto resignDto) {
Member member = memberRepository.findByLoginId(resignDto.getLoginId())
Member member = memberRepository.findByLoginUsername(resignDto.getLoginUsername())
.orElseThrow(() -> new AuthenticationException("Invalid login ID or password."));
if (!bCryptPasswordEncoder.matches(resignDto.getLoginPassword(), member.getLoginPassword())) {
throw new AuthenticationException("Invalid login ID or password.");
}
memberRepository.remove(member);
}

public MyInfoDto verify(LoginInputDto loginDto) {
Member member = memberRepository.findByLoginId(loginDto.getLoginUsername())
.orElseThrow(() -> new AuthenticationException("Invalid login ID or password."));
if (!bCryptPasswordEncoder.matches(loginDto.getLoginPassword(), member.getLoginPassword())) {
throw new AuthenticationException("Invalid login ID or password.");
}
return new MyInfoDto(member.getId());
}
}
71 changes: 34 additions & 37 deletions src/test/java/com/example/swiftgathering_server/FriendshipTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,15 @@

import com.example.swiftgathering_server.domain.Friendship;
import com.example.swiftgathering_server.domain.Member;
import com.example.swiftgathering_server.dto.FriendInfoDto;
import com.example.swiftgathering_server.dto.FriendRequestCreateDto;
import com.example.swiftgathering_server.dto.FriendRequestUpdateDto;
import com.example.swiftgathering_server.dto.RegisterDto;
import com.example.swiftgathering_server.repository.FriendshipRepository;
import com.example.swiftgathering_server.repository.MemberRepository;
import com.example.swiftgathering_server.service.FriendRequestService;
import com.example.swiftgathering_server.service.FriendshipService;
import com.example.swiftgathering_server.service.MemberService;
import jakarta.persistence.EntityManager;
import jakarta.transaction.Transactional;
import org.junit.jupiter.api.Assertions;
Expand All @@ -13,64 +20,54 @@
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit.jupiter.SpringExtension;

import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@ExtendWith(SpringExtension.class)
@SpringBootTest
@Transactional
public class FriendshipTest {

@Autowired
private FriendshipRepository friendshipRepository;
private MemberService memberService;

@Autowired
private MemberRepository memberRepository;
private FriendRequestService friendRequestService;

@Autowired
private FriendshipService friendshipService;

@Autowired
private EntityManager em;

@Test
public void 친구목록() {
// given
Member member1 = Member.builder()
.loginId("111")
.loginPassword("11111")
.name("firsty")
.build();
Member member2 = Member.builder()
.loginId("222")
.loginPassword("22222")
.name("secondy")
.build();
Member member3 = Member.builder()
.loginId("333")
.loginPassword("33333")
.name("thirdy")
.build();
Long member1Id = memberRepository.save(member1);
Long member2Id = memberRepository.save(member2);
Long member3Id = memberRepository.save(member3);
RegisterDto registerDto0 = new RegisterDto("member0", "asdasdf", "mem0");
Long member0Id = memberService.register(registerDto0);
RegisterDto registerDto1 = new RegisterDto("member1", "asdasdf", "mem1");
Long member1Id = memberService.register(registerDto1);
RegisterDto registerDto2 = new RegisterDto("member2", "asdasdf", "mem2");
Long member2Id = memberService.register(registerDto2);
em.flush();

// when
Friendship friendshipOf1And2 = Friendship.builder()
.olderMember(member1)
.youngerMember(member2)
.build();
Friendship friendshipOf2And3 = Friendship.builder()
.olderMember(member2)
.youngerMember(member3)
.build();
friendshipRepository.save(friendshipOf1And2);
friendshipRepository.save(friendshipOf2And3);
em.flush();
FriendRequestCreateDto friendRequestCreateDto0to1 = new FriendRequestCreateDto(member1Id);
Long request0to1Id = friendRequestService.sendFriendRequest(member0Id, friendRequestCreateDto0to1);
FriendRequestCreateDto friendRequestCreateDto0to2 = new FriendRequestCreateDto(member2Id);
Long request0to2Id = friendRequestService.sendFriendRequest(member0Id, friendRequestCreateDto0to2);

FriendRequestUpdateDto friendRequestUpdateDto1to0 = new FriendRequestUpdateDto(request0to1Id, true);
friendRequestService.updateFriendRequestStatus(member1Id, friendRequestUpdateDto1to0);
FriendRequestUpdateDto friendRequestUpdateDto2to0 = new FriendRequestUpdateDto(request0to2Id, false);
friendRequestService.updateFriendRequestStatus(member2Id, friendRequestUpdateDto2to0);

// then
List<Long> fetchedFriends = friendshipRepository.findAllFriendsOfUser(member2)
.stream()
.map(Member::getId)
.toList();
List<Long> expectedFriends = List.of(member1Id, member3Id);
Assertions.assertArrayEquals(expectedFriends.toArray(), fetchedFriends.toArray());
List<Long> resultIds = friendshipService.findAllFriendsOfUser(member0Id)
.stream().map(FriendInfoDto::getId).collect(Collectors.toList());
List<Long> expectedIds = Arrays.asList(new Long[]{member1Id});
Assertions.assertEquals(expectedIds, resultIds);
}
}

0 comments on commit 5254f08

Please sign in to comment.