Skip to content

Commit

Permalink
✨ ModelMapper 설정 (#19)
Browse files Browse the repository at this point in the history
* ✏️ Rename NangmanLetterTests

* ✨ NangmanLetterRepository 테스트

* ✨ ModelMapper 설정
  • Loading branch information
u-genuine authored Jan 19, 2024
1 parent 99c9758 commit 0888719
Show file tree
Hide file tree
Showing 6 changed files with 112 additions and 40 deletions.
3 changes: 3 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ dependencies {
implementation 'org.springdoc:springdoc-openapi-ui:1.6.15'
implementation 'io.springfox:springfox-swagger2:2.9.2'
implementation 'io.springfox:springfox-swagger-ui:2.9.2'

// model mapper
implementation 'org.modelmapper:modelmapper:3.1.0'
}

tasks.named('test') {
Expand Down
21 changes: 21 additions & 0 deletions src/main/java/aromanticcat/umcproject/config/AppConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package aromanticcat.umcproject.config;

import org.modelmapper.ModelMapper;
import org.modelmapper.convention.MatchingStrategies;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class AppConfig {

@Bean
public ModelMapper getMapper(){
ModelMapper modelMapper = new ModelMapper();
modelMapper.getConfiguration()
.setFieldMatchingEnabled(true)
.setFieldAccessLevel(org.modelmapper.config.Configuration.AccessLevel.PRIVATE)
.setMatchingStrategy(MatchingStrategies.STRICT);

return modelMapper;
}
}
1 change: 0 additions & 1 deletion src/main/java/aromanticcat/umcproject/entity/Member.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
@Builder
@AllArgsConstructor
@NoArgsConstructor
@ToString
public class Member extends BaseEntity {

@Id
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,8 @@ public class NangmanLetter extends BaseEntity {
@JoinColumn(name = "member_id")
@ManyToOne(fetch = FetchType.LAZY)
private Member member;

public void change(Boolean has_response){
this.has_response = has_response;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package aromanticcat.umcproject.repository;

import aromanticcat.umcproject.entity.Member;
import aromanticcat.umcproject.entity.NangmanLetter;
import org.hibernate.Hibernate;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import javax.transaction.Transactional;
import java.util.Optional;

import static org.junit.jupiter.api.Assertions.assertNotNull;


@SpringBootTest
@Transactional
public class NangmanLetterRepositoryTests {

@Autowired
private NangmanLetterRepository nangmanLetterRepository;

@Autowired
private MemberRepository memberRepository;


@Test
public void testInsertNangmanLetter(){

Member member1 = Member.builder()
.email("[email protected]")
.nickname("member1")
.coin(0)
.build();
memberRepository.save(member1);

NangmanLetter nangmanLetter = NangmanLetter.builder()
.is_public(false)
.sender_nickname("낭만적인 고양이")
.content("제 고민을 들어주세요")
.member(member1)
.build();
NangmanLetter result = nangmanLetterRepository.save(nangmanLetter);

System.out.println(result);
}

@Test
public void testSelectById(){
Long id = 3L;

Optional<NangmanLetter> result = nangmanLetterRepository.findById(id);
NangmanLetter nangmanLetter = result.orElseThrow();

// 프록시 초기화
Hibernate.initialize(nangmanLetter.getMember().getNangmanLetters());

// 영속성 컨텍스트에서 엔티티를 가져오는 것을 확인
assertNotNull(nangmanLetter.getId());

// 출력
System.out.println(nangmanLetter);
}

@Test
public void testUpdateHasResponse(){
Long id = 1L;

Optional<NangmanLetter> result = nangmanLetterRepository.findById(id);

NangmanLetter nangmanLetter = result.orElseThrow();

nangmanLetter.change(true);

nangmanLetterRepository.save(nangmanLetter);
}

@Test
public void testDeleteNangmanLetter(){
Long id = 1L;

nangmanLetterRepository.deleteById(id);
}
}

This file was deleted.

0 comments on commit 0888719

Please sign in to comment.