From 3dd587c0888c044bf1335d234e9b60a75efe3984 Mon Sep 17 00:00:00 2001
From: Christoph Thelen <christoph.thelen@mni.thm.de>
Date: Wed, 25 Feb 2015 11:45:51 +0100
Subject: [PATCH] Fix #14670: Do not include questions without correct answers

---
 .../de/thm/arsnova/domain/CourseScore.java    |  3 +
 .../QuestionBasedLearningProgressTest.java    | 74 +++++++++++++++++++
 .../de/thm/arsnova/domain/package-info.java   | 18 +++++
 .../de/thm/arsnova/entities/TestUser.java     | 28 +++++++
 .../arsnova/services/FeedbackServiceTest.java | 11 +--
 5 files changed, 124 insertions(+), 10 deletions(-)
 create mode 100644 src/test/java/de/thm/arsnova/domain/QuestionBasedLearningProgressTest.java
 create mode 100644 src/test/java/de/thm/arsnova/domain/package-info.java
 create mode 100644 src/test/java/de/thm/arsnova/entities/TestUser.java

diff --git a/src/main/java/de/thm/arsnova/domain/CourseScore.java b/src/main/java/de/thm/arsnova/domain/CourseScore.java
index f57e5629..a5ab28b6 100644
--- a/src/main/java/de/thm/arsnova/domain/CourseScore.java
+++ b/src/main/java/de/thm/arsnova/domain/CourseScore.java
@@ -30,6 +30,9 @@ public class CourseScore implements Iterable<QuestionScore> {
 	private final Map<String, QuestionScore> scores = new HashMap<String, QuestionScore>();
 
 	public void add(String questionId, int questionScore) {
+		if (questionScore == 0) {
+			return;
+		}
 		scores.put(questionId, new QuestionScore(questionId, questionScore));
 	}
 
diff --git a/src/test/java/de/thm/arsnova/domain/QuestionBasedLearningProgressTest.java b/src/test/java/de/thm/arsnova/domain/QuestionBasedLearningProgressTest.java
new file mode 100644
index 00000000..941d50c9
--- /dev/null
+++ b/src/test/java/de/thm/arsnova/domain/QuestionBasedLearningProgressTest.java
@@ -0,0 +1,74 @@
+/*
+ * This file is part of ARSnova Backend.
+ * Copyright (C) 2012-2015 The ARSnova Team
+ *
+ * ARSnova Backend is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * ARSnova Backend is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+package de.thm.arsnova.domain;
+
+import static org.mockito.Mockito.*;
+import static org.junit.Assert.*;
+
+import java.util.AbstractMap.SimpleEntry;
+
+import org.junit.Test;
+
+import de.thm.arsnova.dao.IDatabaseDao;
+import de.thm.arsnova.entities.TestUser;
+import de.thm.arsnova.entities.User;
+
+public class QuestionBasedLearningProgressTest {
+
+	/**
+	 * Questions without "correct" answers should have a value of zero
+	 */
+	@Test
+	public void shouldIgnoreQuestionsWithoutCorrectAnswers() {
+		final int questionMaxValue = 0;
+		final int userScore = 0;
+		User user = new TestUser("username");
+		CourseScore courseScore = new CourseScore();
+		courseScore.add("question-id", questionMaxValue);
+		courseScore.add("question-id", user.getUsername(), userScore);
+
+		IDatabaseDao db = mock(IDatabaseDao.class);
+		when(db.getLearningProgress(null)).thenReturn(courseScore);
+		LearningProgress lp = new QuestionBasedLearningProgress(db);
+
+		SimpleEntry<Integer, Integer> expected = new SimpleEntry<Integer, Integer>(0, 0);
+		SimpleEntry<Integer, Integer> actual = lp.getMyProgress(null, user);
+
+		assertEquals(expected, actual);
+	}
+
+	@Test
+	public void shouldIgnoreQuestionsWithoutCorrectAnswersInQuestionCount() {
+		User user = new TestUser("username");
+		CourseScore courseScore = new CourseScore();
+		courseScore.add("question-without-correct-answers", 0);
+		courseScore.add("question-with-correct-answers", 50);
+		courseScore.add("question-without-correct-answers", user.getUsername(), 0);
+		courseScore.add("question-with-correct-answers", user.getUsername(), 50);
+
+		IDatabaseDao db = mock(IDatabaseDao.class);
+		when(db.getLearningProgress(null)).thenReturn(courseScore);
+		LearningProgress lp = new QuestionBasedLearningProgress(db);
+
+		SimpleEntry<Integer, Integer> expected = new SimpleEntry<Integer, Integer>(100, 100);
+		SimpleEntry<Integer, Integer> actual = lp.getMyProgress(null, user);
+
+		assertEquals(expected, actual);
+	}
+
+}
diff --git a/src/test/java/de/thm/arsnova/domain/package-info.java b/src/test/java/de/thm/arsnova/domain/package-info.java
new file mode 100644
index 00000000..375154f3
--- /dev/null
+++ b/src/test/java/de/thm/arsnova/domain/package-info.java
@@ -0,0 +1,18 @@
+/*
+ * This file is part of ARSnova Backend.
+ * Copyright (C) 2012-2015 The ARSnova Team
+ *
+ * ARSnova Backend is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * ARSnova Backend is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+package de.thm.arsnova.domain;
\ No newline at end of file
diff --git a/src/test/java/de/thm/arsnova/entities/TestUser.java b/src/test/java/de/thm/arsnova/entities/TestUser.java
new file mode 100644
index 00000000..ffacb933
--- /dev/null
+++ b/src/test/java/de/thm/arsnova/entities/TestUser.java
@@ -0,0 +1,28 @@
+/*
+ * This file is part of ARSnova Backend.
+ * Copyright (C) 2012-2015 The ARSnova Team
+ *
+ * ARSnova Backend is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * ARSnova Backend is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+package de.thm.arsnova.entities;
+
+import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
+
+public class TestUser extends User {
+	private static final long serialVersionUID = 1L;
+
+	public TestUser(String username) {
+		super( new UsernamePasswordAuthenticationToken(username, "secret") );
+	}
+}
\ No newline at end of file
diff --git a/src/test/java/de/thm/arsnova/services/FeedbackServiceTest.java b/src/test/java/de/thm/arsnova/services/FeedbackServiceTest.java
index f5af00e4..a7ba86f7 100644
--- a/src/test/java/de/thm/arsnova/services/FeedbackServiceTest.java
+++ b/src/test/java/de/thm/arsnova/services/FeedbackServiceTest.java
@@ -26,13 +26,12 @@ import org.junit.Before;
 import org.junit.Test;
 import org.junit.runner.RunWith;
 import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
 import org.springframework.test.context.ActiveProfiles;
 import org.springframework.test.context.ContextConfiguration;
 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
 
 import de.thm.arsnova.dao.StubDatabaseDao;
-import de.thm.arsnova.entities.User;
+import de.thm.arsnova.entities.TestUser;
 import de.thm.arsnova.exceptions.NoContentException;
 import de.thm.arsnova.exceptions.NotFoundException;
 
@@ -163,12 +162,4 @@ public class FeedbackServiceTest {
 		userService.setUserAuthenticated(true);
 		assertEquals(2.1904, feedbackService.getAverageFeedback("18273645"), 0.001);
 	}
-
-	public static class TestUser extends User {
-		private static final long serialVersionUID = 1L;
-
-		public TestUser(String username) {
-			super( new UsernamePasswordAuthenticationToken(username, "secret") );
-		}
-	}
 }
\ No newline at end of file
-- 
GitLab