diff --git a/libraries-apache-commons/src/test/java/com/baeldung/commons/beanutils/CourseServiceUnitTest.java b/libraries-apache-commons/src/test/java/com/baeldung/commons/beanutils/CourseServiceUnitTest.java index 0b1eeed5d1af..b4522f466088 100644 --- a/libraries-apache-commons/src/test/java/com/baeldung/commons/beanutils/CourseServiceUnitTest.java +++ b/libraries-apache-commons/src/test/java/com/baeldung/commons/beanutils/CourseServiceUnitTest.java @@ -2,16 +2,64 @@ import static org.assertj.core.api.Assertions.assertThat; import static org.junit.jupiter.api.Assertions.assertEquals; - +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNull; +import org.junit.jupiter.api.BeforeEach; +import org.apache.commons.beanutils.PropertyUtils; import java.lang.reflect.InvocationTargetException; import java.util.Arrays; import java.util.List; - + import org.junit.Assert; import org.junit.Test; public class CourseServiceUnitTest { + private static final String STUDENT_ID = "01"; + + @Test + public void givenCourse_whenGettingSimplePropertyValueUsingPropertyUtil_thenValueReturned() throws IllegalAccessException, InvocationTargetException, NoSuchMethodException { + Course course = new Course(); + String name = "Computer Science"; + List codes = Arrays.asList("CS101","CS102"); + CourseService.setValues(course, name, codes); + + // Use getSimpleProperty to retrieve the 'name' property from the course bean + String courseName = (String) PropertyUtils.getSimpleProperty(course, "name"); + + assertEquals("Computer Science", courseName); + } + + @Test + public void givenCourse_whenGettingIndexedPropertyValueUsingPropertyUtil_thenValueReturned() throws IllegalAccessException, InvocationTargetException, NoSuchMethodException { + Course course = new Course(); + String name = "Computer Science"; + List codes = Arrays.asList("CS101","CS102"); + CourseService.setValues(course, name, codes); + // Use getIndexedProperty to retrieve the element at index 1 from the 'codes' list + String secondCode = (String) PropertyUtils.getIndexedProperty(course, "codes[1]"); + + assertEquals("CS102", secondCode); + } + + @Test + public void givenCourse_whenGettingMappedPropertyValueUsingPropertyUtil_thenValueReturned() throws IllegalAccessException, InvocationTargetException, NoSuchMethodException { + Course course = new Course(); + String name = "Computer Science"; + List codes = Arrays.asList("CS101","CS102"); + CourseService.setValues(course, name, codes); + + // 1. Create and set a Student + Student student = new Student(); + student.setName("John Doe"); + CourseService.setMappedValue(course, STUDENT_ID, student); + // Use getMappedProperty to retrieve the value associated with the key '01' + // from the 'enrolledStudent' map + Student enrolledStudent = (Student) PropertyUtils.getMappedProperty(course, "enrolledStudent(" + STUDENT_ID + ")"); + + assertEquals("John Doe", enrolledStudent.getName()); + } + @Test public void givenCourse_whenSetValuesUsingPropertyUtil_thenReturnSetValues() throws IllegalAccessException, InvocationTargetException, NoSuchMethodException { Course course = new Course(); @@ -48,7 +96,7 @@ public void givenCopyProperties_whenCopyCourseToCourseEntity_thenCopyPropertyWit CourseService.copyProperties(course, courseEntity); Assert.assertNotNull(course.getName()); - Assert.assertNotNull(courseEntity.getName()); + Assert.assertNotNull(courseEntity.getName()); Assert.assertEquals(course.getName(), courseEntity.getName()); Assert.assertEquals(course.getCodes(), courseEntity.getCodes()); Assert.assertNull(courseEntity.getStudent("ST-1"));