Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Repositry test #9

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions .idea/modules/flashy_main.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 20 additions & 3 deletions .idea/modules/flashy_test.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

@Controller
public class IndexController {
public static final int AMOUNT_TO_SHOW = 3;
private FlashCardService flashCardService;

@Autowired
Expand All @@ -22,18 +23,20 @@ public void setFlashCardService(FlashCardService flashCardService) {
@RequestMapping("/")
public String index(Model model) {
StringBuilder ctaBuilder = new StringBuilder();
List<FlashCard> cards = flashCardService.getRandomFlashCards(5);
List<FlashCard> cards = flashCardService.getRandomFlashCards(AMOUNT_TO_SHOW);
ctaBuilder.append("Refresh your memory about ");
for (FlashCard card : cards) {
ctaBuilder.append(card.getTerm());
if (card != cards.get(cards.size() - 1)) {
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";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class FlashCard {
private String term;
private String definition;

protected FlashCard() {
public FlashCard() {
id = null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -52,22 +47,19 @@ public FlashCard getNextUnseenFlashCard(Collection<Long> seenIds) {
@Override
public FlashCard getNextFlashCardBasedOnViews(Map<Long, Long> idToViewCounts) {
FlashCard card = getNextUnseenFlashCard(idToViewCounts.keySet());
if (card != null) {
return card;
}
Long leastViewedId = null;
for (Map.Entry<Long, Long> 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<Long, Long> idToViewCounts) {
List<Map.Entry<Long, Long>> 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
Expand Down
12 changes: 6 additions & 6 deletions src/main/resources/templates/index.html
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="utf-8" />
<meta charset="utf-8"/>
<title>Flashy</title>
<meta name="description" content="A Flash Card game about Java terms" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link href="https://fonts.googleapis.com/css?family=Roboto|Suez+One" rel="stylesheet" />
<link rel="stylesheet" href="/css/global.css" />
<meta name="description" content="A Flash Card game about Java terms"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<link href="https://fonts.googleapis.com/css?family=Roboto|Suez+One" rel="stylesheet"/>
<link rel="stylesheet" href="/css/global.css"/>
</head>
<body class="index">
<div class="bounds">
<a href="/flashcard/next" class="card">
<h1 class="logo">Flashy</h1>
<p id="call-to-action" th:text="${cta}" />
<p id="call-to-action" th:text="${cta}"></p>
<div class="button button-primary">Get Started!</div>
</a>
</div>
Expand Down
Original file line number Diff line number Diff line change
@@ -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<FlashCard> 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));
}

}