From b99fdea2e09060b6a7a42ab96546a6ea588f4aaf Mon Sep 17 00:00:00 2001
From: uhensler <urs.hensler@frentix.com>
Date: Wed, 17 Jun 2020 08:21:27 +0200
Subject: [PATCH] OO-4630: Respect maximum participants if counting free
 appointments

---
 .../appointments/manager/AppointmentDAO.java  | 17 ++++++--
 .../manager/AppointmentDAOTest.java           | 39 ++++++++++++-------
 2 files changed, 39 insertions(+), 17 deletions(-)

diff --git a/src/main/java/org/olat/course/nodes/appointments/manager/AppointmentDAO.java b/src/main/java/org/olat/course/nodes/appointments/manager/AppointmentDAO.java
index f322a28fd33..371f068a5d2 100644
--- a/src/main/java/org/olat/course/nodes/appointments/manager/AppointmentDAO.java
+++ b/src/main/java/org/olat/course/nodes/appointments/manager/AppointmentDAO.java
@@ -186,9 +186,20 @@ class AppointmentDAO {
 				.map(t -> (Long)t.get(0))
 				.collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));
 	}
-
-	private Predicate<? super Tuple> filterFreeOnly(boolean freeOnly) {
-		return t -> freeOnly? t.get(2) == null || ((Integer)t.get(2)).intValue() < ((Long)t.get(3)).intValue(): true;
+	
+	private Predicate<Tuple> filterFreeOnly(boolean freeOnly) {
+		return t -> { 
+			if (freeOnly) {
+				// no max participants
+				if (t.get(2) == null) {
+					return true;
+				}
+				Integer maxParticipants = (Integer)t.get(2);
+				Long count = (Long)t.get(3);
+				return count.intValue() < maxParticipants.intValue();
+			}
+			return true; 
+		};
 	}
 	
 	Long loadAppointmentCount(AppointmentSearchParams params) {
diff --git a/src/test/java/org/olat/course/nodes/appointments/manager/AppointmentDAOTest.java b/src/test/java/org/olat/course/nodes/appointments/manager/AppointmentDAOTest.java
index 6ce8cbfad43..3a54cc10660 100644
--- a/src/test/java/org/olat/course/nodes/appointments/manager/AppointmentDAOTest.java
+++ b/src/test/java/org/olat/course/nodes/appointments/manager/AppointmentDAOTest.java
@@ -276,16 +276,30 @@ public class AppointmentDAOTest extends OlatTestCase {
 		RepositoryEntry entry = JunitTestHelper.createAndPersistRepositoryEntry();
 		Identity identity = JunitTestHelper.createAndPersistIdentityAsUser(random());
 		String subIdent = JunitTestHelper.random();
-		Topic topic1 = topicDao.createTopic(entry, subIdent);
-		Topic topic2 = topicDao.createTopic(entry, subIdent);
-		Appointment appointment11 = sut.createAppointment(topic1);
-		sut.createAppointment(topic1);
-		Appointment appointment21 = sut.createAppointment(topic2);
-		appointment21.setMaxParticipations(1);
-		sut.saveAppointment(appointment21);
-		participationDao.createParticipation(appointment11, identity);
-		participationDao.createParticipation(appointment11, identity);
-		participationDao.createParticipation(appointment21, identity);
+		Topic topic = topicDao.createTopic(entry, subIdent);
+
+		Appointment noLimitNoParticipations = sut.createAppointment(topic);
+		Appointment noLimitWithParticipations = sut.createAppointment(topic);
+		Appointment limitNoParticipations = sut.createAppointment(topic);
+		Appointment limitFull = sut.createAppointment(topic);
+		Appointment limitNotFull = sut.createAppointment(topic);
+		noLimitNoParticipations.setDetails("noLimitNoParticipations");
+		sut.saveAppointment(noLimitNoParticipations);
+		noLimitWithParticipations.setDetails("noLimitWithParticipations");
+		sut.saveAppointment(noLimitWithParticipations);
+		limitNoParticipations.setDetails("limitNoParticipations");
+		limitNoParticipations.setMaxParticipations(2);
+		sut.saveAppointment(limitNoParticipations);
+		limitFull.setDetails("limitFull");
+		limitFull.setMaxParticipations(1);
+		sut.saveAppointment(limitFull);
+		limitNotFull.setDetails("limitNotFull");
+		limitNotFull.setMaxParticipations(3);
+		sut.saveAppointment(limitNotFull);
+		participationDao.createParticipation(noLimitWithParticipations, identity);
+		participationDao.createParticipation(noLimitWithParticipations, identity);
+		participationDao.createParticipation(limitFull, identity);
+		participationDao.createParticipation(limitNotFull, identity);
 		dbInstance.commitAndCloseSession();
 		
 		AppointmentSearchParams params = new AppointmentSearchParams();
@@ -293,10 +307,7 @@ public class AppointmentDAOTest extends OlatTestCase {
 		params.setSubIdent(subIdent);
 		Map<Long, Long> appointmentCountByTopic = sut.loadTopicKeyToAppointmentCount(params, true);
 		
-		SoftAssertions softly = new SoftAssertions();
-		softly.assertThat(appointmentCountByTopic.get(topic1.getKey())).isEqualTo(2);
-		softly.assertThat(appointmentCountByTopic.get(topic2.getKey())).isNull();
-		softly.assertAll();
+		assertThat(appointmentCountByTopic.get(topic.getKey())).isEqualTo(4);
 	}
 	
 	@Test
-- 
GitLab