@@ -49,9 +49,6 @@ class FileServiceTest {
4949 @ Autowired
5050 private PasswordEncoder passwordEncoder ;
5151
52- @ Autowired
53- private PostRepository postRepository ;
54-
5552 @ AfterEach
5653 public void tearDown () {
5754 s3Mock .stop ();
@@ -65,19 +62,17 @@ void uploadFile() {
6562 user .setUserStatus (UserStatus .ACTIVE );
6663 userRepository .save (user );
6764
68- Post post = new Post (user , "제목" , "내용" , null );
69- postRepository .save (post );
70-
7165 String path = "test.png" ;
7266 String contentType = "image/png" ;
7367 String dirName = "test" ;
7468
7569 MockMultipartFile file = new MockMultipartFile ("test" , path , contentType , "test" .getBytes ());
7670
7771 // when
78- FileUploadResponseDto res = fileService .uploadFile (file , EntityType . POST , post . getId (), user .getId ());
72+ FileUploadResponseDto res = fileService .uploadFile (file , user .getId ());
7973
8074 // then
75+ assertThat (res .getAttachmentId ()).isPositive ();
8176 assertThat (res .getImageUrl ()).contains (path );
8277 assertThat (res .getImageUrl ()).contains (dirName );
8378 }
@@ -90,17 +85,14 @@ void readFile() {
9085 user .setUserStatus (UserStatus .ACTIVE );
9186 userRepository .save (user );
9287
93- Post post = new Post (user , "제목" , "내용" , null );
94- postRepository .save (post );
95-
9688 String path = "test.png" ;
9789 String contentType = "image/png" ;
9890 String dirName = "test" ;
9991 MockMultipartFile file = new MockMultipartFile ("test" , path , contentType , "test" .getBytes ());
100- fileService .uploadFile (file , EntityType . POST , post . getId (), user . getId () );
92+ Long attachmentId = fileService .uploadFile (file , user . getId ()). getAttachmentId ( );
10193
10294 //when
103- FileReadResponseDto res = fileService .getFile (EntityType . POST , post . getId () );
95+ FileReadResponseDto res = fileService .getFile (attachmentId );
10496
10597 // then
10698 assertThat (res .getImageUrl ()).contains (path );
@@ -115,24 +107,20 @@ void updateFile() {
115107 user .setUserStatus (UserStatus .ACTIVE );
116108 userRepository .save (user );
117109
118- Post post = new Post (user , "제목" , "내용" , null );
119- postRepository .save (post );
120-
121110 // 기존(삭제할) 파일 정보
122111 String path = "test.png" ;
123112 String contentType = "image/png" ;
124- String dirName = "test" ;
125113 MockMultipartFile oldFile = new MockMultipartFile ("test" , path , contentType , "test" .getBytes ());
126- fileService .uploadFile (oldFile , EntityType . POST , post . getId (), user . getId () );
114+ Long attachmentId = fileService .uploadFile (oldFile , user . getId ()). getAttachmentId ( );
127115
128116 // 새 파일 정보
129117 String newPath = "newTest.png" ;
130118 String newDirName = "newTest" ;
131119 MockMultipartFile newFile = new MockMultipartFile ("newTest" , newPath , contentType , "newTest" .getBytes ());
132120
133121 // when
134- fileService .updateFile (newFile , EntityType . POST , post . getId () , user .getId ());
135- FileReadResponseDto res = fileService .getFile (EntityType . POST , post . getId () );
122+ fileService .updateFile (attachmentId , newFile , user .getId ());
123+ FileReadResponseDto res = fileService .getFile (attachmentId );
136124
137125 // then
138126 assertThat (res .getImageUrl ()).contains (newPath );
@@ -147,23 +135,21 @@ void deleteFile() {
147135 user .setUserStatus (UserStatus .ACTIVE );
148136 userRepository .save (user );
149137
150- Post post = new Post (user , "제목" , "내용" , null );
151- postRepository .save (post );
152-
153138 // 기존(삭제할) 파일 정보
154139 String path = "test.png" ;
155140 String contentType = "image/png" ;
156141 String dirName = "test" ;
157142 MockMultipartFile oldFile = new MockMultipartFile ("test" , path , contentType , "test" .getBytes ());
158- fileService .uploadFile (oldFile , EntityType .POST , post .getId (), user .getId ());
143+ Long attachmentId = fileService .uploadFile (oldFile , user .getId ()).getAttachmentId ();
144+
159145
160146 // when
161- fileService .deleteFile (EntityType . POST , post . getId () , user .getId ());
147+ fileService .deleteFile (attachmentId , user .getId ());
162148 CustomException exception = assertThrows (CustomException .class , () -> {
163- fileService .getFile (EntityType . POST , post . getId () );
149+ fileService .getFile (attachmentId );
164150 });
165151
166152 // then
167- assertEquals (ErrorCode .ATTACHMENT_MAPPING_NOT_FOUND , exception .getErrorCode ());
153+ assertEquals (ErrorCode .FILE_NOT_FOUND , exception .getErrorCode ());
168154 }
169155}
0 commit comments