Skip to content

Commit

Permalink
Merge pull request #60 from Familing/develop
Browse files Browse the repository at this point in the history
애정 카드 테스트 코드 깨짐 해결 및 가족 중복 생성 예외 처리 추가
  • Loading branch information
JunHyeong-99 authored Aug 29, 2024
2 parents 82c152c + c5eb369 commit 49510b5
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 306 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
import com.pinu.familing.domain.family.repository.FamilyRepository;
import com.pinu.familing.domain.user.entity.User;
import com.pinu.familing.domain.user.repository.UserRepository;
import com.pinu.familing.domain.user.service.UserService;
import com.pinu.familing.global.error.CustomException;
import com.pinu.familing.global.error.ExceptionCode;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
Expand All @@ -23,24 +25,36 @@ public class FamilyService {
private final UserRepository userRepository;
private final FamilyRepository familyRepository;
private final ChatService chatService;
private final UserService userService;


//가족 만들기
@Transactional
public FamilyDto registerNewFamily(String username, String familyName) {
String validCode = validFamilyCode(FamilyCodeHandler.createCode(username));

User user = userRepository.findByUsername(username)
.orElseThrow(() -> new CustomException(USER_NOT_FOUND));

if (user.hasFamily()) {
throw new CustomException(ALREADY_HAVE_FAMILY);
}

Family family = Family.builder()
.familyName(familyName)
.code(validCode)
.build();

Family savefamily = familyRepository.save(family);
User user = userRepository.findByUsername(username)
.orElseThrow(() -> new CustomException(USER_NOT_FOUND));

// 가족 등록할 때 가족 채팅방 가동 생성
chatService.makeChatRoom(user, validCode);
return FamilyDto.fromEntity(savefamily);

FamilyDto familyDto = FamilyDto.fromEntity(savefamily);

userService.addFamilyToUser(username, familyDto.code());

return familyDto;
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public record LovecardLogResponse(Long id,
LovecardResponse lovecardResponse,
LocalDate localDate
) {
private record LovecardUser(String usernaem,
private record LovecardUser(String username,
String nickname) {

public LovecardUser(User user) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,8 @@ public class Lovecard {
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String image;

public Lovecard(String image) {
this.image = image;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ public void registerFamily(Family family) {
family.addMember();
}

public boolean hasFamily() {
return this.family != null;
}

public void registerChatRoom(ChatRoom chatRoom) {
this.chatRoom = chatRoom;
}
Expand Down
28 changes: 18 additions & 10 deletions familing/src/main/resources/data.sql
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,34 @@ VALUES ('일반형', 2900, '일반형, 기본 기능이고 상태보기 기능
INSERT INTO subscription (name, price, explanation)
VALUES ('프리미엄형', 3900, '프리미엄 구독입니다. 모든 기능을 무제한으로 제공합니다.');

INSERT INTO status (text)
VALUES ('공부 중');
INSERT INTO status (text)
VALUES ('노는 중');
INSERT INTO status (text)
VALUES ('쉬는 중');

INSERT INTO snapshot_title (title)
VALUES ('테스트용1');
VALUES ('가장 잘나왔다고 생각하는 셀카');
INSERT INTO snapshot_title (title)
VALUES ('테스트용2');
VALUES ('오늘의 점심메뉴');
INSERT INTO snapshot_title (title)
VALUES ('테스트용3');
VALUES ('내 갤러리의 4번째 사진');
INSERT INTO snapshot_title (title)
VALUES ('테스트용4');
VALUES ('지금 내가 앉아있는 곳');
INSERT INTO snapshot_title (title)
VALUES ('테스트용5');
VALUES ('지금 보이는 파란색 물건');
INSERT INTO snapshot_title (title)
VALUES ('테스트용6');
VALUES ('오늘의 출근길/퇴근길');
INSERT INTO snapshot_title (title)
VALUES ('테스트용7');
VALUES ('내가 지내는 장소');
INSERT INTO snapshot_title (title)
VALUES ('테스트용8');
VALUES ('내가 좋아하는 간식');
INSERT INTO snapshot_title (title)
VALUES ('테스트용9');
VALUES ('좋아하는 음악');
INSERT INTO snapshot_title (title)
VALUES ('테스트용10');
VALUES ('나의 휴식 공간');


INSERT INTO lovecard (image)
VALUES ('이미지1');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import com.pinu.familing.domain.user.entity.User;
import com.pinu.familing.domain.user.repository.UserRepository;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
Expand All @@ -23,8 +24,11 @@
import org.springframework.data.domain.Pageable;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

import static org.assertj.core.api.Assertions.assertThat;

@Transactional
class LovecardServiceTest extends IntegrationTestSupport {

@Autowired
Expand All @@ -40,65 +44,37 @@ class LovecardServiceTest extends IntegrationTestSupport {
@AfterEach
void tearDown() {
lovecardLogRepository.deleteAll();
lovecardRepository.deleteAll();
userRepository.deleteAll();

}

@BeforeEach
void setUp() {
Lovecard lovecard1 = new Lovecard("이미지1");
Lovecard lovecard2 = new Lovecard("이미지2");
Lovecard lovecard3 = new Lovecard("이미지3");
Lovecard lovecard4 = new Lovecard("이미지4");
lovecardRepository.save(lovecard1);
lovecardRepository.save(lovecard2);
lovecardRepository.save(lovecard3);
lovecardRepository.save(lovecard4);
}

@Test
@DisplayName("애정카드 페이지 보이는지 확인")
void getLovecardPage() {
//given
Pageable pageable = PageRequest.of(0, 12);
Pageable pageable = PageRequest.of(0, 4);
//when
Page<?> page = lovecardService.getLovecardPage(pageable);

//then
assertThat(page.getContent()).isNotNull();

CustomPage customPage = new CustomPage(page);
System.out.println("customPage = " + customPage);
}


@Test
@DisplayName("특정 가족과 주고 받은 애정카드 정상 조회 테스트")
void getLovecardByFamilyLogPage() throws JsonProcessingException {
//given
User receiver = userRepository.save(User.builder()
.username("user1")
.realname("서현")
.nickname("동생")
.build());

User sender = userRepository.save(User.builder()
.username("user2")
.realname("서경")
.nickname("언니")
.build());

Lovecard lovecard = lovecardRepository.findById(1L).get();

Pageable pageable = PageRequest.of(0, 12);

lovecardLogRepository.save(LovecardLog.builder().lovecard(lovecard).receiver(receiver).sender(sender).build());

//then
Page<LovecardLogResponse> lovecardLogResponses = lovecardService.getLovecardByFamilyLogPage(receiver.getUsername(), sender.getUsername(), pageable);

System.out.println("lovecardLogResponses = " + lovecardLogResponses);

// Create ObjectMapper instance
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.registerModule(new JavaTimeModule()); // Register JavaTimeModule
objectMapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS); // Disable writing dates as timestamps
objectMapper.enable(SerializationFeature.INDENT_OUTPUT); // For pretty-printing

// Convert to JSON string
String json = objectMapper.writeValueAsString(lovecardLogResponses);

// Print JSON string
System.out.println("lovecardLogResponses (JSON) = " + json);
}

@Test
@Transactional
@DisplayName("원하는 가족에게 애정카드 보내주는 기능 정상 작동")
Expand Down
Loading

0 comments on commit 49510b5

Please sign in to comment.