From a20bc89fc6c1101b565b9b13499787f7b3a08c79 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcin=20Wo=C5=BAniak?= Date: Fri, 16 Oct 2020 16:13:40 +0200 Subject: [PATCH 1/2] Added GetCourseGroupsResponse(-Test) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Marcin Woźniak --- .../models/GetCourseGroupsResponse.java | 32 +++++++++++++++++++ .../models/GetCourseGroupResponseTest.java | 24 ++++++++++++++ 2 files changed, 56 insertions(+) create mode 100644 restservice/src/main/java/com/plannaplan/responses/models/GetCourseGroupsResponse.java create mode 100644 restservice/src/test/java/com/plannaplan/responses/models/GetCourseGroupResponseTest.java diff --git a/restservice/src/main/java/com/plannaplan/responses/models/GetCourseGroupsResponse.java b/restservice/src/main/java/com/plannaplan/responses/models/GetCourseGroupsResponse.java new file mode 100644 index 0000000..41387e0 --- /dev/null +++ b/restservice/src/main/java/com/plannaplan/responses/models/GetCourseGroupsResponse.java @@ -0,0 +1,32 @@ +package com.plannaplan.responses.models; + +import java.util.ArrayList; +import java.util.List; + +import com.plannaplan.entities.Groups; +import com.plannaplan.types.GroupType; + +public class GetCourseGroupsResponse { + + private List lectures = new ArrayList<>(); + private List classes = new ArrayList<>(); + + public GetCourseGroupsResponse(List groups ){ + groups.stream().forEach(group -> { + if (group.getType() == GroupType.CLASS) { + this.classes.add(new DefaultGroupResponse(group)); + } else { + this.lectures.add(new DefaultGroupResponse(group)); + } + }); + } + + public List getClasses() { + return this.classes; + } + + public List getLectures() { + return this.lectures; + } + +} \ No newline at end of file diff --git a/restservice/src/test/java/com/plannaplan/responses/models/GetCourseGroupResponseTest.java b/restservice/src/test/java/com/plannaplan/responses/models/GetCourseGroupResponseTest.java new file mode 100644 index 0000000..06bf282 --- /dev/null +++ b/restservice/src/test/java/com/plannaplan/responses/models/GetCourseGroupResponseTest.java @@ -0,0 +1,24 @@ +package com.plannaplan.responses.models; + +import static org.junit.jupiter.api.Assertions.assertTrue; + +import java.util.Arrays; +import java.util.List; + +import com.plannaplan.entities.Groups; +import com.plannaplan.types.WeekDay; + +import org.junit.Test; + +public class GetCourseGroupResponseTest { + + @Test + public void shouldMapListGroupsToResponse() { + final List groups = Arrays.asList(new Groups(32,"A1-1", null, 480, WeekDay.MONDAY, null), new Groups(150,"A1-2", null, 500, WeekDay.THURSDAY, null)); + + final GetCourseGroupsResponse response = new GetCourseGroupsResponse(groups); + assertTrue(response.getClasses().size() == 1); + assertTrue(response.getLectures().size() == 1); + } + +} From 603dadd9c1e973019f09fbebf181aec59110eacc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcin=20Wo=C5=BAniak?= Date: Tue, 20 Oct 2020 19:02:18 +0200 Subject: [PATCH 2/2] Mapping of getCourseGroups MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Marcin Woźniak --- .../controllers/GroupController.java | 7 ++-- .../responses/mappers/GroupsMappers.java | 38 +++++++++++++++++++ .../models/GetCourseGroupsResponse.java | 24 ++++-------- .../responses/mappers/GroupsMappersTest.java | 17 +++++++++ .../models/GetCourseGroupResponseTest.java | 24 ------------ .../models/GetCourseGroupsResponseTest.java | 28 ++++++++++++++ 6 files changed, 95 insertions(+), 43 deletions(-) delete mode 100644 restservice/src/test/java/com/plannaplan/responses/models/GetCourseGroupResponseTest.java create mode 100644 restservice/src/test/java/com/plannaplan/responses/models/GetCourseGroupsResponseTest.java diff --git a/restservice/src/main/java/com/plannaplan/controllers/GroupController.java b/restservice/src/main/java/com/plannaplan/controllers/GroupController.java index 478b6c1..90cbdda 100755 --- a/restservice/src/main/java/com/plannaplan/controllers/GroupController.java +++ b/restservice/src/main/java/com/plannaplan/controllers/GroupController.java @@ -6,6 +6,7 @@ import com.plannaplan.App; import com.plannaplan.entities.Groups; import com.plannaplan.responses.mappers.GroupsMappers; import com.plannaplan.responses.models.DefaultGroupResponse; +import com.plannaplan.responses.models.GetCourseGroupsResponse; import com.plannaplan.services.GroupService; import org.springframework.beans.factory.annotation.Autowired; @@ -25,12 +26,12 @@ public class GroupController { private GroupService groupService; @GetMapping("/getCourseGroups") - public ResponseEntity> getCourses(@RequestParam("id") Long id, + public ResponseEntity> getCourses(@RequestParam("id") Long id, @RequestParam(name = "capacity", defaultValue = "true") Boolean capacity) { List groups = this.groupService.getGroupsByCourse(id); if (capacity) { - return new ResponseEntity<>(GroupsMappers.mapToCapacityResponse(groups), HttpStatus.OK); + return new ResponseEntity<>(GroupsMappers.mapToGetCourseGroupsWithCapacityResponse(groups), HttpStatus.OK); } - return new ResponseEntity<>(GroupsMappers.mapToDefaultResponse(groups), HttpStatus.OK); + return new ResponseEntity<>(GroupsMappers.mapToGetCourseGroupsDefaultResponse(groups), HttpStatus.OK); } } \ No newline at end of file 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 415be62..401a5a1 100755 --- a/restservice/src/main/java/com/plannaplan/responses/mappers/GroupsMappers.java +++ b/restservice/src/main/java/com/plannaplan/responses/mappers/GroupsMappers.java @@ -1,12 +1,15 @@ package com.plannaplan.responses.mappers; +import java.util.ArrayList; import java.util.List; import java.util.Objects; import java.util.stream.Collectors; import com.plannaplan.entities.Groups; import com.plannaplan.responses.models.DefaultGroupResponse; +import com.plannaplan.responses.models.GetCourseGroupsResponse; import com.plannaplan.responses.models.WithCapacityGroupResponse; +import com.plannaplan.types.GroupType; public class GroupsMappers { public static List mapToDefaultResponse(List groups) { @@ -17,4 +20,39 @@ public class GroupsMappers { return groups.stream().filter(Objects::nonNull).map(WithCapacityGroupResponse::new) .collect(Collectors.toList()); } + + public static GetCourseGroupsResponse mapToGetCourseGroupsDefaultResponse (List groups){ + + List lectures = new ArrayList<>(); + List classes = new ArrayList<>(); + + groups.stream().forEach(group -> { + if (group.getType() == GroupType.CLASS) { + classes.add(new DefaultGroupResponse(group)); + } else { + lectures.add(new DefaultGroupResponse(group)); + } + }); + + + return new GetCourseGroupsResponse<>(classes, lectures); + } + + public static GetCourseGroupsResponse mapToGetCourseGroupsWithCapacityResponse (List groups){ + + List lectures = new ArrayList<>(); + List classes = new ArrayList<>(); + + groups.stream().forEach(group -> { + if (group.getType() == GroupType.CLASS) { + classes.add(new WithCapacityGroupResponse(group)); + } else { + lectures.add(new WithCapacityGroupResponse(group)); + } + }); + + + return new GetCourseGroupsResponse<>(classes, lectures); + } + } diff --git a/restservice/src/main/java/com/plannaplan/responses/models/GetCourseGroupsResponse.java b/restservice/src/main/java/com/plannaplan/responses/models/GetCourseGroupsResponse.java index 41387e0..c07bede 100644 --- a/restservice/src/main/java/com/plannaplan/responses/models/GetCourseGroupsResponse.java +++ b/restservice/src/main/java/com/plannaplan/responses/models/GetCourseGroupsResponse.java @@ -3,29 +3,21 @@ package com.plannaplan.responses.models; import java.util.ArrayList; import java.util.List; -import com.plannaplan.entities.Groups; -import com.plannaplan.types.GroupType; +public class GetCourseGroupsResponse { -public class GetCourseGroupsResponse { + private List lectures = new ArrayList<>(); + private List classes = new ArrayList<>(); - private List lectures = new ArrayList<>(); - private List classes = new ArrayList<>(); - - public GetCourseGroupsResponse(List groups ){ - groups.stream().forEach(group -> { - if (group.getType() == GroupType.CLASS) { - this.classes.add(new DefaultGroupResponse(group)); - } else { - this.lectures.add(new DefaultGroupResponse(group)); - } - }); + public GetCourseGroupsResponse(List classes, List lectures ){ + this.lectures = lectures; + this.classes = classes; } - public List getClasses() { + public List getClasses() { return this.classes; } - public List getLectures() { + public List getLectures() { return this.lectures; } 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 3f735a6..cefc561 100755 --- a/restservice/src/test/java/com/plannaplan/responses/mappers/GroupsMappersTest.java +++ b/restservice/src/test/java/com/plannaplan/responses/mappers/GroupsMappersTest.java @@ -1,11 +1,14 @@ package com.plannaplan.responses.mappers; +import static org.junit.Assert.assertTrue; + import java.util.Arrays; import java.util.List; import com.plannaplan.entities.Groups; import com.plannaplan.entities.Lecturer; import com.plannaplan.responses.models.DefaultGroupResponse; +import com.plannaplan.responses.models.GetCourseGroupsResponse; import com.plannaplan.responses.models.WithCapacityGroupResponse; import com.plannaplan.types.WeekDay; @@ -32,4 +35,18 @@ public class GroupsMappersTest { assert (response.get(0) instanceof DefaultGroupResponse); 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"))); + + final GetCourseGroupsResponse response = GroupsMappers.mapToGetCourseGroupsDefaultResponse(groups); + + assertTrue(response.getClasses().size() == 1); + assertTrue(response.getLectures().size() == 1); + + } + } + diff --git a/restservice/src/test/java/com/plannaplan/responses/models/GetCourseGroupResponseTest.java b/restservice/src/test/java/com/plannaplan/responses/models/GetCourseGroupResponseTest.java deleted file mode 100644 index 06bf282..0000000 --- a/restservice/src/test/java/com/plannaplan/responses/models/GetCourseGroupResponseTest.java +++ /dev/null @@ -1,24 +0,0 @@ -package com.plannaplan.responses.models; - -import static org.junit.jupiter.api.Assertions.assertTrue; - -import java.util.Arrays; -import java.util.List; - -import com.plannaplan.entities.Groups; -import com.plannaplan.types.WeekDay; - -import org.junit.Test; - -public class GetCourseGroupResponseTest { - - @Test - public void shouldMapListGroupsToResponse() { - final List groups = Arrays.asList(new Groups(32,"A1-1", null, 480, WeekDay.MONDAY, null), new Groups(150,"A1-2", null, 500, WeekDay.THURSDAY, null)); - - final GetCourseGroupsResponse response = new GetCourseGroupsResponse(groups); - assertTrue(response.getClasses().size() == 1); - assertTrue(response.getLectures().size() == 1); - } - -} diff --git a/restservice/src/test/java/com/plannaplan/responses/models/GetCourseGroupsResponseTest.java b/restservice/src/test/java/com/plannaplan/responses/models/GetCourseGroupsResponseTest.java new file mode 100644 index 0000000..4da18db --- /dev/null +++ b/restservice/src/test/java/com/plannaplan/responses/models/GetCourseGroupsResponseTest.java @@ -0,0 +1,28 @@ +package com.plannaplan.responses.models; + +import static org.junit.Assert.assertTrue; + +import java.util.Arrays; +import java.util.List; + +import com.plannaplan.entities.Groups; + +import org.junit.Test; + +public class GetCourseGroupsResponseTest { + + @Test public void shouldCreateInstance(){ + + final List classes = Arrays.asList(new DefaultGroupResponse(new Groups())); + + final List lectures = Arrays.asList(new DefaultGroupResponse(new Groups())); + + final GetCourseGroupsResponse instance = new GetCourseGroupsResponse<>(classes,lectures); + + assertTrue(instance.getClasses().get(0) instanceof DefaultGroupResponse); + assertTrue(instance.getLectures().get(0) instanceof DefaultGroupResponse); + + } + + +}