99import genum .shared .DTO .response .ResponseDetails ;
1010import genum .shared .constant .Gender ;
1111import genum .shared .security .CustomUserDetails ;
12+ import lombok .extern .slf4j .Slf4j ;
1213import org .junit .jupiter .api .BeforeAll ;
1314import org .junit .jupiter .api .BeforeEach ;
1415import org .junit .jupiter .api .Test ;
3031import org .springframework .security .core .context .SecurityContextImpl ;
3132import org .springframework .test .web .servlet .MockMvc ;
3233
33- import java .io .IOException ;
3434import java .nio .charset .StandardCharsets ;
3535import java .time .LocalDateTime ;
3636import java .util .List ;
4545@ SpringBootTest
4646@ AutoConfigureMockMvc (addFilters = false )
4747@ AutoConfigureJsonTesters
48+ @ Slf4j
4849public class LearningIT extends BaseDatabaseIntegration {
4950
5051 @ Autowired
@@ -97,20 +98,22 @@ void shouldGetAllUsersUploadedCoursesIfAuthorized() throws Exception {
9798 userDetails .getUserReferenceId (),
9899 "Course Description" ,
99100 23000 );
101+ existingCourse .setUploaderId (userDetails .getUserReferenceId ());
100102
101103 existingCourse = mongoTemplate .save (existingCourse , "course" );
102104
103105 mockMvc .perform (
104- get ("/api/learn/my-course" )
106+ get ("/api/learn/course/ my-course" )
105107 .with (user (userDetails ))
106108 ).andExpect (status ().isOk ());
107109
108- mongoTemplate .remove (existingCourse , "course" );
110+ var deleteResult = mongoTemplate .remove (existingCourse , "course" );
111+ assertThat (deleteResult .wasAcknowledged ()).isTrue ();
109112 }
110113 @ Test
111114 void shouldReturnUnauthorizedIfNotAuthorized () throws Exception {
112115 mockMvc .perform (
113- get ("/api/learn/my-course" )
116+ get ("/api/learn/course/ my-course" )
114117 ).andExpect (status ().isUnauthorized ());
115118 }
116119
@@ -129,7 +132,8 @@ void givenExistingCourse_ShouldReturnCourse() throws Exception {
129132 get ("/api/learn/course/%s" .formatted (existingCourse .getReferenceId ()))
130133 )
131134 .andExpect (status ().isOk ());
132- mongoTemplate .remove (existingCourse );
135+ var deleteResult = mongoTemplate .remove (existingCourse , "course" );
136+ assertThat (deleteResult .wasAcknowledged ()).isTrue ();
133137 }
134138 @ Test
135139 void givenNonExistentCourse_ShouldReturnNotFound () throws Exception {
@@ -160,7 +164,8 @@ void givenExistingCourseAndUserIsEnrolled_ShouldReturnTrue() throws Exception {
160164 boolean isAuthorized = jacksonTesterForEnrolled .parseObject (result .getContentAsString (StandardCharsets .UTF_8 )).getData ();
161165
162166 assertThat (isAuthorized ).isTrue ();
163- mongoTemplate .remove (existingCourse , "course" );
167+ var deleteResult = mongoTemplate .remove (existingCourse , "course" );
168+ assertThat (deleteResult .wasAcknowledged ()).isTrue ();
164169 }
165170 @ Test
166171 void givenExistingCourseAndUserNotEnrolled_ShouldReturnFalse () throws Exception {
@@ -180,7 +185,8 @@ void givenExistingCourseAndUserNotEnrolled_ShouldReturnFalse() throws Exception
180185 boolean isAuthorized = jacksonTesterForEnrolled .parseObject (result .getContentAsString (StandardCharsets .UTF_8 )).getData ();
181186 assertThat (isAuthorized ).isFalse ();
182187
183- mongoTemplate .remove (existingCourse , "course" );
188+ var deleteResult = mongoTemplate .remove (existingCourse , "course" );
189+ assertThat (deleteResult .wasAcknowledged ()).isTrue ();
184190 }
185191
186192 /* ===================================== Tests for creating course ============================================== */
@@ -257,8 +263,11 @@ void shouldGetLessonsPertainingToAParticularCourse() throws Exception {
257263 assertThat (jsonResult .getData ().content ().isEmpty ()).isFalse ();
258264 assertThat (jsonResult .getData ().content ().size ()).isEqualTo (3 );
259265
260- mongoTemplate .remove (Query .query (Criteria .where ("courseId" ).is (existingCourse .getReferenceId ())),"lesson" );
261- mongoTemplate .remove (existingCourse ,"course" );
266+ var deleteResultForLesson = mongoTemplate .remove (Query .query (Criteria .where ("courseId" ).is (existingCourse .getReferenceId ())),"lesson" );
267+ var deleteResult = mongoTemplate .remove (existingCourse , "course" );
268+ assertThat (deleteResult .wasAcknowledged ()).isTrue ();
269+ assertThat (deleteResultForLesson .wasAcknowledged ()).isTrue ();
270+
262271 }
263272
264273 /* =========================================== Test for getting lesson by lessonId ============================== */
@@ -286,7 +295,8 @@ void shouldGetLessonByTheLessonId() throws Exception {
286295 assertThat (existingLesson .getTitle ()).isEqualTo (responseData .title ());
287296 assertThat (existingLesson .getReferenceId ()).isEqualTo (responseData .lessonId ());
288297
289- mongoTemplate .remove (existingLesson , "lesson" );
298+ var deleteResult = mongoTemplate .remove (existingLesson , "lesson" );
299+ assertThat (deleteResult .wasAcknowledged ()).isTrue ();
290300
291301 }
292302
@@ -334,7 +344,8 @@ void shouldUploadLesson_GivenAValidRequest() throws Exception{
334344 assertThat (response .title ()).isEqualTo (lessonUploadRequest .title ());
335345
336346
337- mongoTemplate .remove (existingCourse ,"course" );
347+ var deleteResult = mongoTemplate .remove (existingCourse , "course" );
348+ assertThat (deleteResult .wasAcknowledged ()).isTrue ();
338349
339350 }
340351
@@ -359,9 +370,10 @@ void shouldReturn400ForAnInValidRequest() throws Exception{
359370 .contentType (MediaType .APPLICATION_JSON )
360371 .content (jsonLessonUploadRequest .write (lessonUploadRequest ).getJson ())
361372 )
362- .andExpect (status ().isCreated ());
373+ .andExpect (status ().isBadRequest ());
363374
364- mongoTemplate .remove (existingCourse ,"course" );
375+ var deleteResult = mongoTemplate .remove (existingCourse , "course" );
376+ assertThat (deleteResult .wasAcknowledged ()).isTrue ();
365377
366378 }
367379
@@ -424,8 +436,11 @@ void givenExistingCourseAndLesson_ShouldUpdateLesson () throws Exception {
424436 assertThat (existingLesson .getReferenceId ()).isEqualTo (response .lessonId ());
425437
426438
427- mongoTemplate .remove (existingCourse , "course" );
428- mongoTemplate .remove (existingLesson , "lesson" );
439+ var deleteCourseResult = mongoTemplate .remove (existingCourse , "course" );
440+ assertThat (deleteCourseResult .wasAcknowledged ()).isTrue ();
441+
442+ var deleteLessonResult = mongoTemplate .remove (existingLesson , "lesson" );
443+ assertThat (deleteLessonResult .wasAcknowledged ()).isTrue ();
429444
430445
431446 }
@@ -465,8 +480,10 @@ void givenExistingCourseAndLessonButWithEmptyOrBlankRequestFields_ShouldNotUpdat
465480 assertThat (existingLesson .getReferenceId ()).isEqualTo (response .lessonId ());
466481
467482
468- mongoTemplate .remove (existingCourse , "course" );
469- mongoTemplate .remove (existingLesson , "lesson" );
483+ var deleteCourseResult = mongoTemplate .remove (existingCourse , "course" );
484+ assertThat (deleteCourseResult .wasAcknowledged ()).isTrue ();
485+ var deleteLessonResult = mongoTemplate .remove (existingLesson , "lesson" );
486+ assertThat (deleteLessonResult .wasAcknowledged ()).isTrue ();
470487
471488
472489 }
@@ -519,8 +536,10 @@ void shouldUploadVideoForAParticularLesson() throws Exception{
519536 ).andExpect (status ().isCreated ());
520537
521538
522- mongoTemplate .remove (existingCourse , "course" );
523- mongoTemplate .remove (existingLesson , "lesson" );
539+ var deleteCourseResult = mongoTemplate .remove (existingCourse , "course" );
540+ assertThat (deleteCourseResult .wasAcknowledged ()).isTrue ();
541+ var deleteLessonResult = mongoTemplate .remove (existingLesson , "lesson" );
542+ assertThat (deleteLessonResult .wasAcknowledged ()).isTrue ();
524543
525544 }
526545
0 commit comments