1+ package com .back .domain .file .service ;
2+
3+ import com .back .domain .file .config .S3MockConfig ;
4+ import com .back .domain .file .dto .FileUploadResponseDto ;
5+ import com .back .domain .file .entity .AttachmentMapping ;
6+ import com .back .domain .file .entity .EntityType ;
7+ import com .back .domain .file .entity .FileAttachment ;
8+ import com .back .domain .file .repository .AttachmentMappingRepository ;
9+ import com .back .domain .file .repository .FileAttachmentRepository ;
10+ import com .back .domain .user .common .entity .User ;
11+ import com .back .domain .user .common .entity .UserProfile ;
12+ import com .back .domain .user .common .enums .UserStatus ;
13+ import com .back .domain .user .common .repository .UserRepository ;
14+ import io .findify .s3mock .S3Mock ;
15+ import jakarta .persistence .EntityManager ;
16+ import org .junit .jupiter .api .AfterEach ;
17+ import org .junit .jupiter .api .Test ;
18+ import org .springframework .beans .factory .annotation .Autowired ;
19+ import org .springframework .boot .test .context .SpringBootTest ;
20+ import org .springframework .context .annotation .Import ;
21+ import org .springframework .mock .web .MockMultipartFile ;
22+ import org .springframework .security .crypto .password .PasswordEncoder ;
23+ import org .springframework .test .context .ActiveProfiles ;
24+ import org .springframework .transaction .annotation .Transactional ;
25+
26+ import java .time .LocalDate ;
27+ import java .util .List ;
28+
29+ import static org .assertj .core .api .Assertions .assertThat ;
30+
31+ @ Import (S3MockConfig .class )
32+ @ SpringBootTest
33+ @ Transactional
34+ @ ActiveProfiles ("test" )
35+ class AttachmentMappingServiceTest {
36+ @ Autowired
37+ private S3Mock s3Mock ;
38+
39+ @ Autowired
40+ private FileService fileService ;
41+
42+ @ Autowired
43+ private AttachmentMappingService attachmentMappingService ;
44+
45+ @ Autowired
46+ private UserRepository userRepository ;
47+
48+ @ Autowired
49+ private FileAttachmentRepository fileAttachmentRepository ;
50+
51+ @ Autowired
52+ private AttachmentMappingRepository attachmentMappingRepository ;
53+
54+ @ Autowired
55+ private PasswordEncoder passwordEncoder ;
56+
57+ @ AfterEach
58+ public void tearDown () {
59+ s3Mock .stop ();
60+ }
61+
62+ @ Test
63+ void deleteAttachments_success () throws Exception {
64+ // given
65+ User user =
User .
createUser (
"writer" ,
"[email protected] " ,
passwordEncoder .
encode (
"P@ssw0rd!" ));
66+ user .setUserProfile (new UserProfile (user , "홍길동" , null , "소개글" , LocalDate .of (2000 , 1 , 1 ), 1000 ));
67+ user .setUserStatus (UserStatus .ACTIVE );
68+ userRepository .save (user );
69+
70+ String path = "test.png" ;
71+ String contentType = "image/png" ;
72+
73+ MockMultipartFile file = new MockMultipartFile ("test" , path , contentType , "test" .getBytes ());
74+
75+ FileUploadResponseDto res = fileService .uploadFile (file , user .getId ());
76+ FileAttachment fileAttachment = fileAttachmentRepository .findById (res .getAttachmentId ()).orElse (null );
77+
78+ AttachmentMapping attachmentMapping = new AttachmentMapping (fileAttachment , EntityType .POST , 1L );
79+ attachmentMappingRepository .save (attachmentMapping );
80+
81+ // when
82+ attachmentMappingService .deleteAttachments (EntityType .POST , 1L , user .getId ());
83+
84+ // then
85+ assertThat (attachmentMappingRepository .findAllByEntityTypeAndEntityId (EntityType .POST , 1L )
86+ .size ()).isEqualTo (0 );
87+ assertThat (fileAttachmentRepository .findAll ().size ()).isEqualTo (0 );
88+ }
89+
90+ @ Test
91+ void replaceAttachments_success () throws Exception {
92+ // given
93+ User user =
User .
createUser (
"writer" ,
"[email protected] " ,
passwordEncoder .
encode (
"P@ssw0rd!" ));
94+ user .setUserProfile (new UserProfile (user , "홍길동" , null , "소개글" , LocalDate .of (2000 , 1 , 1 ), 1000 ));
95+ user .setUserStatus (UserStatus .ACTIVE );
96+ userRepository .save (user );
97+
98+ // 기존(삭제할) 파일 정보
99+ String path = "test.png" ;
100+ String contentType = "image/png" ;
101+ MockMultipartFile oldFile = new MockMultipartFile ("test" , path , contentType , "test" .getBytes ());
102+ Long oldAttachmentId = fileService .uploadFile (oldFile , user .getId ()).getAttachmentId ();
103+
104+ // 새 파일 정보
105+ String newPath = "newTest.png" ;
106+ MockMultipartFile newFile = new MockMultipartFile ("newTest" , newPath , contentType , "newTest" .getBytes ());
107+ Long newAttachmentId = fileService .uploadFile (newFile , user .getId ()).getAttachmentId ();
108+
109+ FileAttachment fileAttachment = fileAttachmentRepository .findById (oldAttachmentId ).orElse (null );
110+ AttachmentMapping attachmentMapping = new AttachmentMapping (fileAttachment , EntityType .POST , 1L );
111+ attachmentMappingRepository .save (attachmentMapping );
112+
113+ // when
114+ attachmentMappingService .replaceAttachments (EntityType .POST , 1L , user .getId (), List .of (newAttachmentId ));
115+
116+ // then
117+ AttachmentMapping findMapping = attachmentMappingRepository .findByEntityTypeAndEntityId (EntityType .POST , 1L ).orElse (null );
118+ assertThat (findMapping .getFileAttachment ().getId ()).isEqualTo (newAttachmentId );
119+ }
120+
121+ @ Test
122+ void replaceAttachmentsUrl_success () throws Exception {
123+ // given
124+ User user =
User .
createUser (
"writer" ,
"[email protected] " ,
passwordEncoder .
encode (
"P@ssw0rd!" ));
125+ user .setUserProfile (new UserProfile (user , "홍길동" , null , "소개글" , LocalDate .of (2000 , 1 , 1 ), 1000 ));
126+ user .setUserStatus (UserStatus .ACTIVE );
127+ userRepository .save (user );
128+
129+ // 기존(삭제할) 파일 정보
130+ String path = "test.png" ;
131+ String contentType = "image/png" ;
132+ MockMultipartFile oldFile = new MockMultipartFile ("test" , path , contentType , "test" .getBytes ());
133+ Long oldAttachmentId = fileService .uploadFile (oldFile , user .getId ()).getAttachmentId ();
134+
135+ // 새 파일 정보
136+ String newPath = "newTest.png" ;
137+ MockMultipartFile newFile = new MockMultipartFile ("newTest" , newPath , contentType , "newTest" .getBytes ());
138+ Long newAttachmentId = fileService .uploadFile (newFile , user .getId ()).getAttachmentId ();
139+
140+ FileAttachment fileAttachment = fileAttachmentRepository .findById (oldAttachmentId ).orElse (null );
141+ AttachmentMapping attachmentMapping = new AttachmentMapping (fileAttachment , EntityType .POST , 1L );
142+ attachmentMappingRepository .save (attachmentMapping );
143+
144+ // when
145+ String newPublicURL = fileAttachmentRepository
146+ .findById (newAttachmentId )
147+ .orElse (null )
148+ .getPublicURL ();
149+
150+ attachmentMappingService .replaceAttachmentByUrl (EntityType .POST , 1L , user .getId (), newPublicURL );
151+
152+ // then
153+ AttachmentMapping findMapping = attachmentMappingRepository .findByEntityTypeAndEntityId (EntityType .POST , 1L ).orElse (null );
154+ assertThat (findMapping .getFileAttachment ().getId ()).isEqualTo (newAttachmentId );
155+ }
156+
157+ }
0 commit comments