diff --git a/.idea/misc.xml b/.idea/misc.xml
index 164b970..02642ab 100644
--- a/.idea/misc.xml
+++ b/.idea/misc.xml
@@ -13,7 +13,7 @@
-
+
\ No newline at end of file
diff --git a/.idea/modules/flashy_main.iml b/.idea/modules/flashy_main.iml
index 3d7e5d1..1d3ef90 100644
--- a/.idea/modules/flashy_main.iml
+++ b/.idea/modules/flashy_main.iml
@@ -1,5 +1,10 @@
+
+
+
+
+
diff --git a/.idea/modules/flashy_test.iml b/.idea/modules/flashy_test.iml
index 3c5b73a..7aa96a5 100644
--- a/.idea/modules/flashy_test.iml
+++ b/.idea/modules/flashy_test.iml
@@ -15,6 +15,10 @@
+
+
+
+
@@ -31,11 +35,19 @@
+
+
+
+
+
+
+
+
+
-
@@ -61,23 +73,28 @@
-
+
+
+
+
-
+
+
+
\ No newline at end of file
diff --git a/.idea/vcs.xml b/.idea/vcs.xml
index 94a25f7..d175698 100644
--- a/.idea/vcs.xml
+++ b/.idea/vcs.xml
@@ -2,5 +2,7 @@
+
+
\ No newline at end of file
diff --git a/build.gradle b/build.gradle
index c89d069..45aaedc 100644
--- a/build.gradle
+++ b/build.gradle
@@ -27,5 +27,10 @@ dependencies {
compile 'org.springframework.boot:spring-boot-starter-thymeleaf'
compile 'com.h2database:h2'
compile 'org.eclipse.mylyn.github:org.eclipse.egit.github.core:2.1.5'
+
+ testCompile 'org.springframework.boot:spring-boot-starter-test'
+ testCompile 'org.springframework.security:spring-security-test'
+ testCompile 'com.github.springtestdbunit:spring-test-dbunit:1.3.0'
+ testCompile 'org.dbunit:dbunit:2.5.2'
testCompile group: 'junit', name: 'junit', version: '4.11'
}
diff --git a/src/main/java/com/teamtreehouse/flashy/bootstrap/DatabaseLoader.java b/src/main/java/com/teamtreehouse/flashy/bootstrap/DatabaseLoader.java
index 239bb00..db2d0bb 100644
--- a/src/main/java/com/teamtreehouse/flashy/bootstrap/DatabaseLoader.java
+++ b/src/main/java/com/teamtreehouse/flashy/bootstrap/DatabaseLoader.java
@@ -26,6 +26,10 @@ public void run(ApplicationArguments args) throws Exception {
cards.add(new FlashCard("JRE", "Java Runtime Environment"));
cards.add(new FlashCard("JCL", "Java Class Library"));
cards.add(new FlashCard("JVM", "Java Virtual Machine"));
+ cards.add(new FlashCard("JNDI", "Java Naming and Directory Interface"));
+ cards.add(new FlashCard("JDBC", "Java Database Connectivity"));
+ cards.add(new FlashCard("AJAX", "Asynchronous JavaScript and XML"));
+ cards.add(new FlashCard("JSON", "JavaScript Object Notation"));
flashCardRepository.save(cards);
}
diff --git a/src/main/java/com/teamtreehouse/flashy/controllers/IndexController.java b/src/main/java/com/teamtreehouse/flashy/controllers/IndexController.java
index f127754..79d476d 100644
--- a/src/main/java/com/teamtreehouse/flashy/controllers/IndexController.java
+++ b/src/main/java/com/teamtreehouse/flashy/controllers/IndexController.java
@@ -12,6 +12,7 @@
@Controller
public class IndexController {
+ public static final int AMOUNT_TO_SHOW = 3;
private FlashCardService flashCardService;
@Autowired
@@ -22,7 +23,7 @@ public void setFlashCardService(FlashCardService flashCardService) {
@RequestMapping("/")
public String index(Model model) {
StringBuilder ctaBuilder = new StringBuilder();
- List cards = flashCardService.getRandomFlashCards(5);
+ List cards = flashCardService.getRandomFlashCards(AMOUNT_TO_SHOW);
ctaBuilder.append("Refresh your memory about ");
for (FlashCard card : cards) {
ctaBuilder.append(card.getTerm());
@@ -30,10 +31,12 @@ public String index(Model model) {
ctaBuilder.append(", ");
}
}
- ctaBuilder.append(" and ");
Long totalCount = flashCardService.getCurrentCount();
- ctaBuilder.append(totalCount);
- ctaBuilder.append(" more");
+ if (totalCount > AMOUNT_TO_SHOW) {
+ ctaBuilder.append(" and ");
+ ctaBuilder.append(totalCount - AMOUNT_TO_SHOW);
+ ctaBuilder.append(" more");
+ }
model.addAttribute("cta", ctaBuilder.toString());
model.addAttribute("flashCardCount", totalCount);
return "index";
diff --git a/src/main/java/com/teamtreehouse/flashy/domain/FlashCard.java b/src/main/java/com/teamtreehouse/flashy/domain/FlashCard.java
index 8592a63..36ae398 100644
--- a/src/main/java/com/teamtreehouse/flashy/domain/FlashCard.java
+++ b/src/main/java/com/teamtreehouse/flashy/domain/FlashCard.java
@@ -14,7 +14,7 @@ public class FlashCard {
private String term;
private String definition;
- protected FlashCard() {
+ public FlashCard() {
id = null;
}
diff --git a/src/main/java/com/teamtreehouse/flashy/services/FlashCardServiceImpl.java b/src/main/java/com/teamtreehouse/flashy/services/FlashCardServiceImpl.java
index 5b660e9..7f1c3bf 100644
--- a/src/main/java/com/teamtreehouse/flashy/services/FlashCardServiceImpl.java
+++ b/src/main/java/com/teamtreehouse/flashy/services/FlashCardServiceImpl.java
@@ -2,15 +2,10 @@
import com.teamtreehouse.flashy.domain.FlashCard;
import com.teamtreehouse.flashy.repositories.FlashCardRepository;
-
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
-import java.util.Collection;
-import java.util.Collections;
-import java.util.List;
-import java.util.Map;
-import java.util.Random;
+import java.util.*;
import static java.util.stream.Collectors.toList;
@@ -52,22 +47,19 @@ public FlashCard getNextUnseenFlashCard(Collection seenIds) {
@Override
public FlashCard getNextFlashCardBasedOnViews(Map idToViewCounts) {
FlashCard card = getNextUnseenFlashCard(idToViewCounts.keySet());
- if (card != null) {
- return card;
- }
- Long leastViewedId = null;
- for (Map.Entry entry : idToViewCounts.entrySet()) {
- if (leastViewedId == null) {
- leastViewedId = entry.getKey();
- continue;
- }
- Long lowestScore = idToViewCounts.get(leastViewedId);
- if (entry.getValue() >= lowestScore) {
- break;
- }
- leastViewedId = entry.getKey();
+ if (card == null) {
+ card = getLeastViewedFlashCard(idToViewCounts);
}
- return flashCardRepository.findOne(leastViewedId);
+ return card;
+ }
+
+ public FlashCard getLeastViewedFlashCard(Map idToViewCounts) {
+ List> entries = new ArrayList<>(idToViewCounts.entrySet());
+ Collections.shuffle(entries);
+ return entries.stream()
+ .min(Comparator.comparing(Map.Entry::getValue))
+ .map(entry -> flashCardRepository.findOne(entry.getKey()))
+ .orElseThrow(IllegalArgumentException::new);
}
@Override
diff --git a/src/main/resources/templates/index.html b/src/main/resources/templates/index.html
index dcc669c..ca5fb4f 100644
--- a/src/main/resources/templates/index.html
+++ b/src/main/resources/templates/index.html
@@ -1,18 +1,18 @@
-
+
Flashy
-
-
-
-
+
+
+
+
diff --git a/src/test/java/com/teamtreehouse/flashy/services/FlashCardServiceImplTest.java b/src/test/java/com/teamtreehouse/flashy/services/FlashCardServiceImplTest.java
new file mode 100644
index 0000000..b6242b7
--- /dev/null
+++ b/src/test/java/com/teamtreehouse/flashy/services/FlashCardServiceImplTest.java
@@ -0,0 +1,48 @@
+package com.teamtreehouse.flashy.services;
+
+import com.teamtreehouse.flashy.domain.FlashCard;
+import com.teamtreehouse.flashy.repositories.FlashCardRepository;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.InjectMocks;
+import org.mockito.Mock;
+import org.mockito.runners.MockitoJUnitRunner;
+
+import java.util.Arrays;
+import java.util.List;
+
+import static org.hamcrest.Matchers.instanceOf;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertThat;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+@RunWith(MockitoJUnitRunner.class)
+public class FlashCardServiceImplTest {
+
+ @Mock
+ private FlashCardRepository repository;
+
+ @InjectMocks
+ private FlashCardService service = new FlashCardServiceImpl();
+
+ @Test
+ public void getRandomFlashCards_ShouldReturnTwo() throws Exception {
+ List flashCards = Arrays.asList(
+ new FlashCard(),
+ new FlashCard()
+ );
+
+ when(repository.findAll()).thenReturn(flashCards);
+
+ assertEquals("getRandomFlashCards should return two favorites", 2, service.getRandomFlashCards(2).size());
+ verify(repository).findAll();
+ }
+
+ @Test
+ public void findById_ShouldReturnOne() throws Exception {
+ when(repository.findOne(1L)).thenReturn(new FlashCard());
+ assertThat(service.getFlashCardById(1L), instanceOf(FlashCard.class));
+ }
+
+}
\ No newline at end of file