Skip to content
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
6 changes: 5 additions & 1 deletion .classpath
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,16 @@
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/5"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER">
<attributes>
<attribute name="module" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="output" path="target/classes"/>
</classpath>
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
.metadata
doc/
bin/
tmp/
*.tmp
Expand Down
65 changes: 51 additions & 14 deletions src/main/java/edu/mills/cs180a/wordui/model/SampleData.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,23 @@
import edu.mills.cs180a.wordnik.client.model.WordOfTheDay;
import javafx.collections.ObservableList;


public class SampleData {
/**
* String key for the count.
*/
@VisibleForTesting
protected static final String FREQ_COUNT_KEY = "count";
/**
* String key for the frequency.
*/
@VisibleForTesting
protected static final String FREQ_YEAR_KEY = "year";
private static final int FREQ_YEAR = 2012;
private static ApiClient client; // set in fillSampleData()

private static int getFrequencyFromSummary(FrequencySummary fs, int year) {
List<Object> freqObjects = fs.getFrequency();
// freqObjects is a List<Map> [{"year" = "2012", "count" = 179}] for "Java"

if (freqObjects instanceof List) {
List<Object> maps = (List<Object>) freqObjects;
for (Object map : maps) {
Expand All @@ -43,34 +48,43 @@ private static int getFrequencyFromSummary(FrequencySummary fs, int year) {
}

// TODO: Move to spring-swagger-wordnik-client

@VisibleForTesting
protected static int getFrequencyByYear(WordApi wordApi, String word, int year) {
FrequencySummary fs = wordApi.getWordFrequency(word, "false", year, year);
return getFrequencyFromSummary(fs, year);
}

private static WordRecord buildWordRecord(String word, Map<Object, Object> definition) {
WordApi wordApi = client.buildClient(WordApi.class);
private static WordRecord buildWordRecord(String word, Map<Object, Object> definition,
WordApi wordApi) {
return new WordRecord(
word,
getFrequencyByYear(wordApi, word, FREQ_YEAR),
definition.get("text").toString());
}

/**
* Gets a word of the day object.
*
* @param wordsApi the words API
* @return the word of the day
*/
@VisibleForTesting
protected static WordOfTheDay getWordOfTheDay(WordsApi wordsApi) {
return wordsApi.getWordOfTheDay();
}

/**
* Adds word records to a passed list.
*
* @param backingList the list of word records

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wouldn't include type information (that the list contains word records) since that is shown automatically in generated javadoc as part of the signature.

*/
public static void fillSampleData(ObservableList<WordRecord> backingList) {
try {
client = ApiClientHelper.getApiClient();
WordsApi wordsApi = client.buildClient(WordsApi.class);
WordOfTheDay word = wordsApi.getWordOfTheDay();
List<Object> definitions = word.getDefinitions();
if (definitions != null && !definitions.isEmpty()) {
Object definition = definitions.get(0);
if (definition instanceof Map) {
@SuppressWarnings("unchecked")
Map<Object, Object> definitionAsMap = (Map<Object, Object>) definition;
backingList.add(buildWordRecord(word.getWord(), definitionAsMap));
}
}
WordApi wordApi = client.buildClient(WordApi.class);
addWordOfTheDay(backingList, wordsApi, wordApi);
} catch (IOException e) {
System.err.println("Unable to get API key.");
}
Expand All @@ -82,4 +96,27 @@ public static void fillSampleData(ObservableList<WordRecord> backingList) {
backingList.add(new WordRecord("random",
794, "Having no specific pattern, purpose, or objective"));
}

/**
* Adds the word to the passed list.
*
* @param backingList the list of word records
* @param wordsApi the words API
* @param wordApi the word API
*/
@VisibleForTesting
protected static void addWordOfTheDay(ObservableList<WordRecord> backingList,
WordsApi wordsApi, WordApi wordApi) {
WordOfTheDay word = getWordOfTheDay(wordsApi);

List<Object> definitions = word.getDefinitions();
if (definitions != null && !definitions.isEmpty()) {
Object definition = definitions.get(0);
if (definition instanceof Map) {
@SuppressWarnings("unchecked")
Map<Object, Object> definitionAsMap = (Map<Object, Object>) definition;
backingList.add(buildWordRecord(word.getWord(), definitionAsMap, wordApi));
}
}
}
}
73 changes: 65 additions & 8 deletions src/test/java/edu/mills/cs180a/wordui/model/SampleDataTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,38 +5,95 @@
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
import edu.mills.cs180a.wordnik.client.api.WordApi;
import edu.mills.cs180a.wordnik.client.api.WordsApi;
import edu.mills.cs180a.wordnik.client.model.FrequencySummary;
import edu.mills.cs180a.wordnik.client.model.WordOfTheDay;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;


class SampleDataTest {
private final FrequencySummary mockFS = mock(FrequencySummary.class);
private final WordApi mockWordApi = mock(WordApi.class);
private final WordsApi mockWordsApi = mock(WordsApi.class);
private static final WordOfTheDay TODAYS_WORD = mock(WordOfTheDay.class);
private static final String A_WORD = "jingle";
private static final Map<String, FrequencySummary> FREQS_MAP = Map.of(
"apple",
makeFreqSummary(List.of(
makeMap(2000, 339),
makeMap(2001, 464))),
"orange",
makeFreqSummary(List.of(
makeMap(2000, 774),
makeMap(2001, 941))),
A_WORD,
makeFreqSummary(List.of(
makeMap(2020, 187))));
private static final String TODAYS_DEF = "def for jingle";
private static final List<Object> WORD_DEFS = List.of(TODAYS_DEF);
private static final List<Object> DEF_LIST = List.of(Map.of("text", TODAYS_DEF));
private static final List<Object> DEF_LIST_MAP = List.of(Map.of(
"text", WORD_DEFS.get(0)));



@BeforeEach
void setup() {
List<Object> freqObjects = List.of(
// frequencies for "apple"
makeMap(2000, 339),
makeMap(2001, 464));
when(mockFS.getFrequency())
.thenReturn(freqObjects);
when(mockWordApi.getWordFrequency(anyString(), anyString(), anyInt(), anyInt()))
.thenReturn(mockFS);
.thenAnswer(invocation -> FREQS_MAP.get(invocation.getArgument(0)));
when(mockWordsApi.getWordOfTheDay())
.thenReturn(TODAYS_WORD);
when(TODAYS_WORD.getWord())
.thenReturn(A_WORD);
when(TODAYS_WORD.getDefinitions())
.thenReturn(DEF_LIST_MAP);
}

@Test
void addWordOfTheDay_EqualsWordRecord_MockWordsObject() {
List<WordRecord> testList = new ArrayList<>();
ObservableList<WordRecord> testListRecord = FXCollections.observableList(testList);
SampleData.addWordOfTheDay(testListRecord, mockWordsApi, mockWordApi);

assertEquals("jingle", TODAYS_WORD.getWord());
assertEquals(DEF_LIST, TODAYS_WORD.getDefinitions());
assertEquals(1, testListRecord.size());
assertEquals(187, SampleData.getFrequencyByYear(mockWordApi, TODAYS_WORD.getWord(), 2020));
}

private static FrequencySummary makeFreqSummary(List<Object> freqs) {
FrequencySummary fs = mock(FrequencySummary.class);
when(fs.getFrequency())
.thenReturn(freqs);
return fs;
}

private static Map<Object, Object> makeMap(int year, int count) {
return Map.of(SampleData.FREQ_YEAR_KEY, String.valueOf(year),
SampleData.FREQ_COUNT_KEY, count);
}

@Test
void getWordOfTheDay_EqualsWordRecord_MockWordsObject() {
assertEquals("jingle", TODAYS_WORD.getWord());

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you misunderstood my comment. Your code was right before this commit when it called SampleData.getWordOfTheDay(). As it is now written, this does not test getWordOfTheDay() because it does not call it.

assertEquals(DEF_LIST, TODAYS_WORD.getDefinitions());
assertEquals(187, SampleData.getFrequencyByYear(mockWordApi, TODAYS_WORD.getWord(), 2020));
}

@ParameterizedTest
@CsvSource({"apple,2000,339", "apple,2001,464", "apple,2020,0"})
@CsvSource({"apple,2000,339", "apple,2001,464", "apple,2020,0",
"orange,2000,774", "orange,2001,941", "orange,2050,0"})
void testGetFrequencyFromSummary(String word, int year, int count) {
assertEquals(count, SampleData.getFrequencyByYear(mockWordApi, word, year));
}

}