From 593084aeba7356c456b4423762b1a147c140e588 Mon Sep 17 00:00:00 2001 From: BuildTools Date: Mon, 30 Nov 2020 12:07:18 +0100 Subject: [PATCH] Group default response places overload --- .../responses/mappers/GroupsMappers.java | 17 +++++++- .../responses/mappers/GroupsMappersTest.java | 43 ++++++++++++++++--- 2 files changed, 52 insertions(+), 8 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 7a43668..f5607c5 100755 --- a/restservice/src/main/java/com/plannaplan/responses/mappers/GroupsMappers.java +++ b/restservice/src/main/java/com/plannaplan/responses/mappers/GroupsMappers.java @@ -1,8 +1,10 @@ package com.plannaplan.responses.mappers; import java.util.ArrayList; +import java.util.HashMap; import java.util.List; import java.util.Objects; +import java.util.function.Function; import java.util.stream.Collectors; import com.plannaplan.entities.Groups; @@ -12,8 +14,21 @@ import com.plannaplan.responses.models.GroupWithCapacityResponse; import com.plannaplan.types.GroupType; public class GroupsMappers { + public static List mapToDefaultResponse(List groups, HashMap taken) { + return groups.stream().filter(Objects::nonNull).map(new Function() { + @Override + public GroupDefaultResponse apply(Groups p) { + if (taken != null) { + return new GroupDefaultResponse(p, taken.get(p.getId())); + } else { + return new GroupDefaultResponse(p); + } + } + }).collect(Collectors.toList()); + } + public static List mapToDefaultResponse(List groups) { - return groups.stream().filter(Objects::nonNull).map(GroupDefaultResponse::new).collect(Collectors.toList()); + return GroupsMappers.mapToDefaultResponse(groups, null); } public static List mapToCapacityResponse(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 e41fe0a..d9c4de3 100755 --- a/restservice/src/test/java/com/plannaplan/responses/mappers/GroupsMappersTest.java +++ b/restservice/src/test/java/com/plannaplan/responses/mappers/GroupsMappersTest.java @@ -2,7 +2,9 @@ package com.plannaplan.responses.mappers; import static org.junit.Assert.assertTrue; +import java.lang.reflect.Field; import java.util.Arrays; +import java.util.HashMap; import java.util.List; import com.plannaplan.entities.Groups; @@ -37,16 +39,43 @@ public class GroupsMappersTest { } @Test - public void shouldMapToGetCourseGroupsResponse(){ + 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); + 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); 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); + + 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.mapToDefaultResponse(List.of(group1, group2), + placeMap); + + assertTrue(response.size() == 2); + assertTrue(response.get(0).getTakenPlaces() == 5); + assertTrue(response.get(1).getTakenPlaces() == 56); + } + +}