From 082b30133c5774933cfa64dafbbdf1968dacdd0c Mon Sep 17 00:00:00 2001 From: BuildTools Date: Mon, 30 Nov 2020 13:10:40 +0100 Subject: [PATCH] mapToGetCourseGroupsDefaultResponse overloaded --- .../responses/mappers/GroupsMappers.java | 22 ++- .../responses/mappers/GroupsMappersTest.java | 167 ++++++++++-------- 2 files changed, 114 insertions(+), 75 deletions(-) diff --git a/restservice/src/main/java/com/plannaplan/responses/mappers/GroupsMappers.java b/restservice/src/main/java/com/plannaplan/responses/mappers/GroupsMappers.java index 016b9f0..4144fd2 100755 --- a/restservice/src/main/java/com/plannaplan/responses/mappers/GroupsMappers.java +++ b/restservice/src/main/java/com/plannaplan/responses/mappers/GroupsMappers.java @@ -33,9 +33,6 @@ public class GroupsMappers { public static List mapToCapacityResponse(List groups, HashMap taken) { - // return - // groups.stream().filter(Objects::nonNull).map(GroupWithCapacityResponse::new) - // .collect(Collectors.toList()); return groups.stream().filter(Objects::nonNull).map(new Function() { @Override public GroupWithCapacityResponse apply(Groups p) { @@ -53,22 +50,35 @@ public class GroupsMappers { } public static CourseWithGroupsResponse mapToGetCourseGroupsDefaultResponse( - List groups) { + List groups, HashMap taken) { List lectures = new ArrayList<>(); List classes = new ArrayList<>(); groups.stream().forEach(group -> { if (group.getType() == GroupType.CLASS) { - classes.add(new GroupDefaultResponse(group)); + if (taken != null) { + classes.add(new GroupDefaultResponse(group, taken.get(group.getId()))); + } else { + classes.add(new GroupDefaultResponse(group)); + } } else { - lectures.add(new GroupDefaultResponse(group)); + if (taken != null) { + lectures.add(new GroupDefaultResponse(group, taken.get(group.getId()))); + } else { + lectures.add(new GroupDefaultResponse(group)); + } } }); return new CourseWithGroupsResponse<>(classes, lectures); } + public static CourseWithGroupsResponse mapToGetCourseGroupsDefaultResponse( + List groups) { + return GroupsMappers.mapToGetCourseGroupsDefaultResponse(groups, null); + } + public static CourseWithGroupsResponse mapToGetCourseGroupsWithCapacityResponse( List groups) { diff --git a/restservice/src/test/java/com/plannaplan/responses/mappers/GroupsMappersTest.java b/restservice/src/test/java/com/plannaplan/responses/mappers/GroupsMappersTest.java index 3ef598f..d3311de 100755 --- a/restservice/src/test/java/com/plannaplan/responses/mappers/GroupsMappersTest.java +++ b/restservice/src/test/java/com/plannaplan/responses/mappers/GroupsMappersTest.java @@ -17,93 +17,122 @@ import com.plannaplan.types.WeekDay; import org.junit.Test; public class GroupsMappersTest { - @Test - public void shouldMapToResponseIncludingCapacity() { - final List gropus = Arrays.asList( - new Groups(42, "A4-1", null, 520, WeekDay.MONDAY, new Lecturer("krul.", "Wladyslaw", "Potocki"))); - final List response = GroupsMappers.mapToCapacityResponse(gropus); + @Test + public void shouldMapToResponseIncludingCapacity() { + final List gropus = Arrays.asList(new Groups(42, "A4-1", null, 520, WeekDay.MONDAY, + new Lecturer("krul.", "Wladyslaw", "Potocki"))); + final List response = GroupsMappers.mapToCapacityResponse(gropus); - assert (response.get(0).getCapacity() == 42); - assert (response.get(0) instanceof GroupWithCapacityResponse); - assert (response.size() == 1); - } + assert (response.get(0).getCapacity() == 42); + assert (response.get(0) instanceof GroupWithCapacityResponse); + assert (response.size() == 1); + } - @Test - public void shouldMapToResponseWiothoutCapacity() { - final List gropus = Arrays.asList( - new Groups(42, "A4-1", null, 520, WeekDay.MONDAY, new Lecturer("krul.", "Wladyslaw", "Potocki"))); - final List response = GroupsMappers.mapToDefaultResponse(gropus); + @Test + public void shouldMapToResponseWiothoutCapacity() { + final List gropus = Arrays.asList(new Groups(42, "A4-1", null, 520, WeekDay.MONDAY, + new Lecturer("krul.", "Wladyslaw", "Potocki"))); + final List response = GroupsMappers.mapToDefaultResponse(gropus); - assert (response.get(0) instanceof GroupDefaultResponse); - assert (response.size() == 1); - } + assert (response.get(0) instanceof GroupDefaultResponse); + assert (response.size() == 1); + } - @Test - public void shouldMapToGetCourseGroupsResponse() { - final List groups = Arrays.asList( - new Groups(150, "A4-1", null, 520, WeekDay.MONDAY, new Lecturer("krul.", "Wladyslaw", "Potocki")), - new Groups(24, "A4-1", null, 520, WeekDay.MONDAY, new Lecturer("krul.", "Wladyslaw", "Potocki"))); + @Test + public void shouldMapToGetCourseGroupsResponse() { + final List groups = Arrays.asList( + new Groups(150, "A4-1", null, 520, WeekDay.MONDAY, + new Lecturer("krul.", "Wladyslaw", "Potocki")), + new Groups(24, "A4-1", null, 520, WeekDay.MONDAY, + new Lecturer("krul.", "Wladyslaw", "Potocki"))); - final CourseWithGroupsResponse response = GroupsMappers - .mapToGetCourseGroupsDefaultResponse(groups); + final CourseWithGroupsResponse response = GroupsMappers + .mapToGetCourseGroupsDefaultResponse(groups); - assertTrue(response.getClasses().size() == 1); - assertTrue(response.getLectures().size() == 1); + assertTrue(response.getClasses().size() == 1); + assertTrue(response.getLectures().size() == 1); - } + } - @Test - public void shouldMapToDefaultResponseWithTakenPlace() - throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException { - final Field reader = Groups.class.getDeclaredField("id"); - reader.setAccessible(true); + @Test + public void shouldMapToGetCourseGroupsResponseWithTakenPlaces() throws NoSuchFieldException, SecurityException, + IllegalArgumentException, IllegalAccessException { + final Field reader = Groups.class.getDeclaredField("id"); + reader.setAccessible(true); - final Groups group1 = new Groups(150, "A4-1", null, 520, WeekDay.MONDAY, - new Lecturer("krul.", "Wladyslaw", "Potocki")); - final Groups group2 = new Groups(24, "A4-1", null, 520, WeekDay.MONDAY, - new Lecturer("krul.", "Wladyslaw", "Potocki")); + final List groups = Arrays.asList( + new Groups(150, "A4-1", null, 520, WeekDay.MONDAY, + new Lecturer("krul.", "Wladyslaw", "Potocki")), + new Groups(24, "A4-1", null, 520, WeekDay.MONDAY, + new Lecturer("krul.", "Wladyslaw", "Potocki"))); + reader.set(groups.get(0), Long.valueOf(0)); + reader.set(groups.get(1), Long.valueOf(1)); - reader.set(group1, Long.valueOf(0)); - reader.set(group2, Long.valueOf(1)); + final HashMap placeMap = new HashMap<>(); + placeMap.put(Long.valueOf(0), 0); + placeMap.put(Long.valueOf(1), 21); - final HashMap placeMap = new HashMap<>(); - placeMap.put(Long.valueOf(0), 5); - placeMap.put(Long.valueOf(1), 56); + final CourseWithGroupsResponse response = GroupsMappers + .mapToGetCourseGroupsDefaultResponse(groups, placeMap); - final List response = GroupsMappers.mapToDefaultResponse(List.of(group1, group2), - placeMap); + assertTrue(response.getClasses().size() == 1); + assertTrue(response.getLectures().size() == 1); + assertTrue(response.getLectures().get(0).getTakenPlaces() == 0); + assertTrue(response.getClasses().get(0).getTakenPlaces() == 21); + } - assertTrue(response.size() == 2); - assertTrue(response.get(0).getTakenPlaces() == 5); - assertTrue(response.get(1).getTakenPlaces() == 56); - } + @Test + public void shouldMapToDefaultResponseWithTakenPlace() throws NoSuchFieldException, SecurityException, + IllegalArgumentException, IllegalAccessException { + final Field reader = Groups.class.getDeclaredField("id"); + reader.setAccessible(true); - @Test - public void shouldMapToCapacityResponseWithTakenPlace() - throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException { - final Field reader = Groups.class.getDeclaredField("id"); - reader.setAccessible(true); + final Groups group1 = new Groups(150, "A4-1", null, 520, WeekDay.MONDAY, + new Lecturer("krul.", "Wladyslaw", "Potocki")); + final Groups group2 = new Groups(24, "A4-1", null, 520, WeekDay.MONDAY, + new Lecturer("krul.", "Wladyslaw", "Potocki")); - final Groups group1 = new Groups(150, "A4-1", null, 520, WeekDay.MONDAY, - new Lecturer("krul.", "Wladyslaw", "Potocki")); - final Groups group2 = new Groups(24, "A4-1", null, 520, WeekDay.MONDAY, - new Lecturer("krul.", "Wladyslaw", "Potocki")); + reader.set(group1, Long.valueOf(0)); + reader.set(group2, Long.valueOf(1)); - reader.set(group1, Long.valueOf(0)); - reader.set(group2, Long.valueOf(1)); + final HashMap placeMap = new HashMap<>(); + placeMap.put(Long.valueOf(0), 5); + placeMap.put(Long.valueOf(1), 56); - final HashMap placeMap = new HashMap<>(); - placeMap.put(Long.valueOf(0), 5); - placeMap.put(Long.valueOf(1), 56); + final List response = GroupsMappers.mapToDefaultResponse(List.of(group1, group2), + placeMap); - final List response = GroupsMappers.mapToCapacityResponse(List.of(group1, group2), - placeMap); + assertTrue(response.size() == 2); + assertTrue(response.get(0).getTakenPlaces() == 5); + assertTrue(response.get(1).getTakenPlaces() == 56); + } - assertTrue(response.size() == 2); - assertTrue(response.get(0).getTakenPlaces() == 5); - assertTrue(response.get(0).getCapacity() == 150); - assertTrue(response.get(1).getTakenPlaces() == 56); - assertTrue(response.get(1).getCapacity() == 24); - } + @Test + public void shouldMapToCapacityResponseWithTakenPlace() throws NoSuchFieldException, SecurityException, + IllegalArgumentException, IllegalAccessException { + final Field reader = Groups.class.getDeclaredField("id"); + reader.setAccessible(true); + + final Groups group1 = new Groups(150, "A4-1", null, 520, WeekDay.MONDAY, + new Lecturer("krul.", "Wladyslaw", "Potocki")); + final Groups group2 = new Groups(24, "A4-1", null, 520, WeekDay.MONDAY, + new Lecturer("krul.", "Wladyslaw", "Potocki")); + + reader.set(group1, Long.valueOf(0)); + reader.set(group2, Long.valueOf(1)); + + final HashMap placeMap = new HashMap<>(); + placeMap.put(Long.valueOf(0), 5); + placeMap.put(Long.valueOf(1), 56); + + final List response = GroupsMappers + .mapToCapacityResponse(List.of(group1, group2), placeMap); + + assertTrue(response.size() == 2); + assertTrue(response.get(0).getTakenPlaces() == 5); + assertTrue(response.get(0).getCapacity() == 150); + assertTrue(response.get(1).getTakenPlaces() == 56); + assertTrue(response.get(1).getCapacity() == 24); + } }