Skip to content

Commit 10dd2a0

Browse files
committed
added comments before eeach test methods
1 parent 8a05aa1 commit 10dd2a0

File tree

7 files changed

+312
-9
lines changed

7 files changed

+312
-9
lines changed

pom.xml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,20 @@
6161
<groupId>org.springframework.boot</groupId>
6262
<artifactId>spring-boot-starter-test</artifactId>
6363
<scope>test</scope>
64+
<exclusions>
65+
<exclusion>
66+
<groupId>junit</groupId>
67+
<artifactId>junit</artifactId>
68+
</exclusion>
69+
</exclusions>
70+
</dependency>
71+
72+
<!-- JUnit 4 for testing -->
73+
<dependency>
74+
<groupId>junit</groupId>
75+
<artifactId>junit</artifactId>
76+
<version>4.13.2</version>
77+
<scope>test</scope>
6478
</dependency>
6579

6680
<!-- https://mvnrepository.com/artifact/org.mockito/mockito-all -->

src/test/java/com/sopromadze/blogapi/controller/AlbumControllerTest.java

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,12 @@ public class AlbumControllerTest {
6969
private User user;
7070
private Photo photo;
7171

72+
/**
73+
* Test setup method
74+
* Initializes common test data and mock configurations used across all test methods.
75+
* Creates sample UserPrincipal, User, AlbumRequest, and AlbumResponse objects
76+
* that will be used for mocking service responses and testing controller behavior.
77+
*/
7278
@Before
7379
public void setUp() {
7480
mockMvc = MockMvcBuilders.standaloneSetup(albumController).build();
@@ -99,6 +105,10 @@ public void setUp() {
99105
album.setPhoto(Arrays.asList(photo));
100106
}
101107

108+
/**
109+
* Test successful retrieval of all albums with pagination
110+
* Verifies that the getAllAlbums endpoint returns a paginated response with correct data
111+
*/
102112
@Test
103113
public void testGetAllAlbums_Success() throws Exception {
104114
PagedResponse<AlbumResponse> pagedResponse = new PagedResponse<>(
@@ -115,6 +125,10 @@ public void testGetAllAlbums_Success() throws Exception {
115125
verify(albumService, times(1)).getAllAlbums(anyInt(), anyInt());
116126
}
117127

128+
/**
129+
* Test successful album creation
130+
* Verifies that a new album can be created with valid data and returns the created album
131+
*/
118132
@Test
119133
public void testAddAlbum_Success() throws Exception {
120134
when(albumService.addAlbum(any(AlbumRequest.class), any(UserPrincipal.class)))
@@ -130,6 +144,10 @@ public void testAddAlbum_Success() throws Exception {
130144
verify(albumService, times(1)).addAlbum(any(AlbumRequest.class), any(UserPrincipal.class));
131145
}
132146

147+
/**
148+
* Test album creation without authentication
149+
* Verifies behavior when attempting to create an album without proper authentication
150+
*/
133151
@Test
134152
public void testAddAlbum_Unauthorized() throws Exception {
135153
// Note: Security filters are disabled in test setup, so this returns 200 instead of 403
@@ -142,6 +160,10 @@ public void testAddAlbum_Unauthorized() throws Exception {
142160
.andExpect(status().isCreated());
143161
}
144162

163+
/**
164+
* Test album creation with invalid data
165+
* Verifies that the system properly handles invalid album data (empty title)
166+
*/
145167
@Test
146168
public void testAddAlbum_InvalidData() throws Exception {
147169
// Note: No validation annotations on AlbumRequest, so empty title is accepted
@@ -160,6 +182,10 @@ public void testAddAlbum_InvalidData() throws Exception {
160182
verify(albumService, times(1)).addAlbum(any(AlbumRequest.class), any(UserPrincipal.class));
161183
}
162184

185+
/**
186+
* Test successful retrieval of a specific album
187+
* Verifies that an album can be retrieved by its ID with correct data
188+
*/
163189
@Test
164190
public void testGetAlbum_Success() throws Exception {
165191
when(albumService.getAlbum(anyLong())).thenReturn(new ResponseEntity<>(album, HttpStatus.OK));
@@ -173,6 +199,10 @@ public void testGetAlbum_Success() throws Exception {
173199
verify(albumService, times(1)).getAlbum(anyLong());
174200
}
175201

202+
/**
203+
* Test retrieval of non-existent album
204+
* Verifies that the system returns 404 when trying to retrieve an album that doesn't exist
205+
*/
176206
@Test
177207
public void testGetAlbum_NonExistentAlbum() throws Exception {
178208
when(albumService.getAlbum(anyLong())).thenThrow(new ResourceNotFoundException("Album", "id", 999L));
@@ -184,6 +214,10 @@ public void testGetAlbum_NonExistentAlbum() throws Exception {
184214
verify(albumService, times(1)).getAlbum(anyLong());
185215
}
186216

217+
/**
218+
* Test successful album update
219+
* Verifies that an existing album can be updated with new data
220+
*/
187221
@Test
188222
public void testUpdateAlbum_Success() throws Exception {
189223
AlbumRequest updateRequest = new AlbumRequest();
@@ -206,6 +240,10 @@ public void testUpdateAlbum_Success() throws Exception {
206240
verify(albumService, times(1)).updateAlbum(anyLong(), any(AlbumRequest.class), any(UserPrincipal.class));
207241
}
208242

243+
/**
244+
* Test album update without authentication
245+
* Verifies behavior when attempting to update an album without proper authentication
246+
*/
209247
@Test
210248
public void testUpdateAlbum_Unauthorized() throws Exception {
211249
// Note: Security filters are disabled in test setup, so this returns 200 instead of 403
@@ -221,6 +259,10 @@ public void testUpdateAlbum_Unauthorized() throws Exception {
221259
.andExpect(status().isOk());
222260
}
223261

262+
/**
263+
* Test album update with invalid data
264+
* Verifies that the system properly handles invalid update data (empty title)
265+
*/
224266
@Test
225267
public void testUpdateAlbum_InvalidData() throws Exception {
226268
// Note: No validation annotations on AlbumRequest, so empty title is accepted
@@ -239,6 +281,10 @@ public void testUpdateAlbum_InvalidData() throws Exception {
239281
verify(albumService, times(1)).updateAlbum(anyLong(), any(AlbumRequest.class), any(UserPrincipal.class));
240282
}
241283

284+
/**
285+
* Test successful album deletion
286+
* Verifies that an album can be deleted and returns a success response
287+
*/
242288
@Test
243289
public void testDeleteAlbum_Success() throws Exception {
244290
ApiResponse apiResponse = new ApiResponse(true, "Album deleted successfully");
@@ -256,6 +302,10 @@ public void testDeleteAlbum_Success() throws Exception {
256302
verify(albumService, times(1)).deleteAlbum(anyLong(), any(UserPrincipal.class));
257303
}
258304

305+
/**
306+
* Test album deletion without authentication
307+
* Verifies behavior when attempting to delete an album without proper authentication
308+
*/
259309
@Test
260310
public void testDeleteAlbum_Unauthorized() throws Exception {
261311
// Note: Security filters are disabled in test setup, so this returns 200 instead of 403
@@ -271,6 +321,10 @@ public void testDeleteAlbum_Unauthorized() throws Exception {
271321
.andExpect(jsonPath("$.message").value("Album deleted successfully"));
272322
}
273323

324+
/**
325+
* Test deletion of non-existent album
326+
* Verifies that the system returns 404 when trying to delete an album that doesn't exist
327+
*/
274328
@Test
275329
public void testDeleteAlbum_NonExistentAlbum() throws Exception {
276330
when(albumService.deleteAlbum(anyLong(), any(UserPrincipal.class)))
@@ -284,6 +338,10 @@ public void testDeleteAlbum_NonExistentAlbum() throws Exception {
284338
verify(albumService, times(1)).deleteAlbum(anyLong(), any(UserPrincipal.class));
285339
}
286340

341+
/**
342+
* Test successful retrieval of all photos by album
343+
* Verifies that all photos belonging to a specific album can be retrieved with pagination
344+
*/
287345
@Test
288346
public void testGetAllPhotosByAlbum_Success() throws Exception {
289347
PhotoResponse photoResponse = new PhotoResponse(1L, "Test Photo", "http://example.com/photo.jpg", "http://example.com/photo-thumb.jpg", 1L);

src/test/java/com/sopromadze/blogapi/controller/AuthControllerTest.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,12 @@ public class AuthControllerTest {
7676
private Role userRole;
7777
private Authentication authentication;
7878

79+
/**
80+
* Test setup method
81+
* Initializes common test data and mock configurations used across all test methods.
82+
* Creates sample LoginRequest, SignUpRequest, JwtAuthenticationResponse, UserIdentityAvailability,
83+
* and UserProfile objects that will be used for testing authentication and user management functionality.
84+
*/
7985
@Before
8086
public void setUp() {
8187
signUpRequest = new SignUpRequest();
@@ -98,6 +104,10 @@ public void setUp() {
98104
authentication = new UsernamePasswordAuthenticationToken("venkat", "password123");
99105
}
100106

107+
/**
108+
* Test successful user registration
109+
* Verifies that a new user can be registered with valid data and returns the created user
110+
*/
101111
@Test
102112
public void testRegisterUser_Success() throws Exception {
103113
given(userRepository.existsByUsername(anyString())).willReturn(false);
@@ -113,6 +123,10 @@ public void testRegisterUser_Success() throws Exception {
113123
.andExpect(status().isCreated());
114124
}
115125

126+
/**
127+
* Test user registration with already taken username
128+
* Verifies that the system properly rejects registration when username is already taken
129+
*/
116130
@Test
117131
public void testRegisterUser_UsernameAlreadyTaken() throws Exception {
118132
given(userRepository.existsByUsername(anyString())).willReturn(true);
@@ -123,6 +137,10 @@ public void testRegisterUser_UsernameAlreadyTaken() throws Exception {
123137
.andExpect(status().isBadRequest());
124138
}
125139

140+
/**
141+
* Test user registration with already taken email
142+
* Verifies that the system properly rejects registration when email is already taken
143+
*/
126144
@Test
127145
public void testRegisterUser_EmailAlreadyTaken() throws Exception {
128146
given(userRepository.existsByUsername(anyString())).willReturn(false);
@@ -134,6 +152,10 @@ public void testRegisterUser_EmailAlreadyTaken() throws Exception {
134152
.andExpect(status().isBadRequest());
135153
}
136154

155+
/**
156+
* Test user registration with invalid data
157+
* Verifies that the system properly validates and rejects invalid registration data
158+
*/
137159
@Test
138160
public void testRegisterUser_InvalidData() throws Exception {
139161
SignUpRequest invalidRequest = new SignUpRequest();
@@ -149,6 +171,10 @@ public void testRegisterUser_InvalidData() throws Exception {
149171
.andExpect(status().isBadRequest());
150172
}
151173

174+
/**
175+
* Test successful user authentication
176+
* Verifies that a user can authenticate with valid credentials and receive a JWT token
177+
*/
152178
@Test
153179
public void testAuthenticateUser_Success() throws Exception {
154180
given(authenticationManager.authenticate(any(UsernamePasswordAuthenticationToken.class)))
@@ -161,6 +187,10 @@ public void testAuthenticateUser_Success() throws Exception {
161187
.andExpect(status().isOk());
162188
}
163189

190+
/**
191+
* Test user authentication with invalid credentials
192+
* Verifies that the system properly rejects authentication with wrong username/password
193+
*/
164194
@Test
165195
public void testAuthenticateUser_InvalidCredentials() throws Exception {
166196
given(authenticationManager.authenticate(any(UsernamePasswordAuthenticationToken.class)))
@@ -172,6 +202,10 @@ public void testAuthenticateUser_InvalidCredentials() throws Exception {
172202
.andExpect(status().isUnauthorized());
173203
}
174204

205+
/**
206+
* Test user authentication with invalid data
207+
* Verifies that the system properly validates and rejects invalid authentication data
208+
*/
175209
@Test
176210
public void testAuthenticateUser_InvalidData() throws Exception {
177211
LoginRequest invalidRequest = new LoginRequest();

src/test/java/com/sopromadze/blogapi/controller/CommentControllerTest.java

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,13 @@
1313
import com.sopromadze.blogapi.security.UserPrincipal;
1414
import com.sopromadze.blogapi.service.CommentService;
1515
import com.sopromadze.blogapi.exception.ResourceNotFoundException;
16-
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
1716
import org.junit.Before;
1817
import org.junit.Test;
1918
import org.junit.runner.RunWith;
2019
import org.mockito.InjectMocks;
2120
import org.mockito.Mock;
2221
import org.mockito.junit.MockitoJUnitRunner;
22+
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
2323
import org.springframework.http.MediaType;
2424
import org.springframework.test.web.servlet.MockMvc;
2525
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
@@ -60,6 +60,12 @@ public class CommentControllerTest {
6060
private Post post;
6161
private User user;
6262

63+
/**
64+
* Test setup method
65+
* Initializes common test data and mock configurations used across all test methods.
66+
* Creates sample UserPrincipal, User, Post, CommentRequest, and Comment objects
67+
* that will be used for mocking service responses and testing controller behavior.
68+
*/
6369
@Before
6470
public void setUp() {
6571
mockMvc = MockMvcBuilders.standaloneSetup(commentController).build();
@@ -91,6 +97,10 @@ public void setUp() {
9197
comment.setUser(user);
9298
}
9399

100+
/**
101+
* Test successful comment addition to a post
102+
* Verifies that a comment can be added to a specific post with valid data
103+
*/
94104
@Test
95105
public void testAddComment_Success() throws Exception {
96106
when(commentService.addComment(any(CommentRequest.class), anyLong(), any(UserPrincipal.class))).thenReturn(comment);
@@ -108,6 +118,10 @@ public void testAddComment_Success() throws Exception {
108118
}
109119

110120

121+
/**
122+
* Test comment addition with invalid data
123+
* Verifies that the system properly validates and rejects invalid comment data (too short body)
124+
*/
111125
@Test
112126
public void testAddComment_InvalidData() throws Exception {
113127
CommentRequest invalidRequest = new CommentRequest();
@@ -122,6 +136,10 @@ public void testAddComment_InvalidData() throws Exception {
122136
verify(commentService, never()).addComment(any(CommentRequest.class), anyLong(), any(UserPrincipal.class));
123137
}
124138

139+
/**
140+
* Test successful retrieval of all comments for a post
141+
* Verifies that all comments for a specific post can be retrieved with pagination
142+
*/
125143
@Test
126144
public void testGetAllComments_Success() throws Exception {
127145
PagedResponse<Comment> pagedResponse = new PagedResponse<>(
@@ -139,6 +157,10 @@ public void testGetAllComments_Success() throws Exception {
139157
verify(commentService, times(1)).getAllComments(anyLong(), anyInt(), anyInt());
140158
}
141159

160+
/**
161+
* Test successful retrieval of a specific comment
162+
* Verifies that a comment can be retrieved by its ID with correct data
163+
*/
142164
@Test
143165
public void testGetComment_Success() throws Exception {
144166
when(commentService.getComment(anyLong(), anyLong())).thenReturn(comment);
@@ -153,6 +175,10 @@ public void testGetComment_Success() throws Exception {
153175
verify(commentService, times(1)).getComment(anyLong(), anyLong());
154176
}
155177

178+
/**
179+
* Test retrieval of non-existent comment
180+
* Verifies that the system returns 404 when trying to retrieve a comment that doesn't exist
181+
*/
156182
@Test
157183
public void testGetComment_NonExistentComment() throws Exception {
158184
when(commentService.getComment(anyLong(), anyLong())).thenThrow(new ResourceNotFoundException("Comment", "id", 999L));
@@ -164,6 +190,10 @@ public void testGetComment_NonExistentComment() throws Exception {
164190
verify(commentService, times(1)).getComment(anyLong(), anyLong());
165191
}
166192

193+
/**
194+
* Test successful comment update
195+
* Verifies that an existing comment can be updated with new data
196+
*/
167197
@Test
168198
public void testUpdateComment_Success() throws Exception {
169199
CommentRequest updateRequest = new CommentRequest();
@@ -190,6 +220,10 @@ public void testUpdateComment_Success() throws Exception {
190220
}
191221

192222

223+
/**
224+
* Test comment update with invalid data
225+
* Verifies that the system properly validates and rejects invalid update data (too short body)
226+
*/
193227
@Test
194228
public void testUpdateComment_InvalidData() throws Exception {
195229
CommentRequest invalidRequest = new CommentRequest();
@@ -204,6 +238,10 @@ public void testUpdateComment_InvalidData() throws Exception {
204238
verify(commentService, never()).updateComment(anyLong(), anyLong(), any(CommentRequest.class), any(UserPrincipal.class));
205239
}
206240

241+
/**
242+
* Test successful comment deletion
243+
* Verifies that a comment can be deleted and returns a success response
244+
*/
207245
@Test
208246
public void testDeleteComment_Success() throws Exception {
209247
ApiResponse apiResponse = new ApiResponse(true, "Comment deleted successfully");
@@ -221,6 +259,10 @@ public void testDeleteComment_Success() throws Exception {
221259
}
222260

223261

262+
/**
263+
* Test deletion of non-existent comment
264+
* Verifies that the system returns 404 when trying to delete a comment that doesn't exist
265+
*/
224266
@Test
225267
public void testDeleteComment_NonExistentComment() throws Exception {
226268
when(commentService.deleteComment(anyLong(), anyLong(), any(UserPrincipal.class)))

0 commit comments

Comments
 (0)