-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathTestNetflixParser.java
186 lines (147 loc) · 5.38 KB
/
TestNetflixParser.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package fileHandling.in;
import static org.junit.jupiter.api.Assertions.assertAll;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.lang.reflect.Method;
import java.util.Optional;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.junit.platform.commons.support.ReflectionSupport;
import com.github.kilianB.fileHandling.in.NetflixParser;
import com.github.kilianB.model.netflix.NetflixMovie;
import com.github.kilianB.model.netflix.NetflixShowEpisode;
import com.github.kilianB.model.netflix.ViewItem;
/**
*
* @author Kilian
* TODO we don't have fail test cases.
*/
class TestNetflixParser {
static Method parseMethod;
@BeforeAll
static void setUpBeforeClass() throws Exception {
// Cache reflection method
Optional<Method> parseCandidate = ReflectionSupport.findMethod(NetflixParser.class, "parseEntry", String.class,String.class);
if (parseCandidate.isPresent()) {
parseMethod = parseCandidate.get();
} else {
throw new NoSuchMethodException("Could not find parseEntry method in CSVHelper.class");
}
}
@AfterAll
static void tearDownAfterClass() throws Exception {
}
@Nested
@DisplayName("Test Parsing Methods")
class TestCSVParser {
@Nested
class TestSeries {
@Test
void basicSeries() {
String title = "Title";
int season = 1;
String series = "TestSeries";
NetflixShowEpisode show = new NetflixShowEpisode(title, "1.1.2018",season,series);
validateEntry(show, title, season,series);
}
@Test
void defaultSeriesName() {
String inputString = "Lie to Me: Season 3: In the Red";
ViewItem parsedShow = (ViewItem) ReflectionSupport.invokeMethod(parseMethod, null,inputString,"1.1.2018");
validateEntry(parsedShow, "In the Red", 3,"Lie to Me");
}
@Test
void numericalSeriesName() {
String inputString = "Touch: Season 1: 1 + 1 = 3";
ViewItem parsedShow = (ViewItem) ReflectionSupport.invokeMethod(parseMethod, null,inputString,"1.1.2018");
validateEntry(parsedShow, "1 + 1 = 3", 1,"Touch");
}
@Test
void colonsInSeriesName() {
String inputString = "Star Trek: Discovery: Season 1: Context is for Kings";
ViewItem parsedShow = (ViewItem) ReflectionSupport.invokeMethod(parseMethod, null,inputString,"1.1.2018");
validateEntry(parsedShow, "Context is for Kings", 1,"Star Trek: Discovery");
}
@Test
void colonsInEpisodeName() {
String inputString = "The Fresh Prince of Bel-Air: Season 1: Someday Your Prince Will Be in Effect: Part 2";
ViewItem parsedShow = (ViewItem) ReflectionSupport.invokeMethod(parseMethod, null,inputString,"1.1.2018");
validateEntry(parsedShow, "Someday Your Prince Will Be in Effect: Part 2", 1,"The Fresh Prince of Bel-Air");
}
void validateEntry(ViewItem data, String title,int season,String series) {
TestCSVParser.this.validateEntry(NetflixShowEpisode.class, data, title, season,series);
}
}
@Nested
class TestMovies {
@Test
void movieWithoutColon() {
String movieTitle = "The Legend of Tarzan";
ViewItem parsedMovie = (ViewItem) ReflectionSupport.invokeMethod(parseMethod, null,
movieTitle,"ViewDate");
validateEntry(parsedMovie, movieTitle);
}
@Test
void movieWithColon() {
String movieTitle = "Underworld: Awakening";
ViewItem parsedMovie = (ViewItem) ReflectionSupport.invokeMethod(parseMethod, null,
movieTitle,"ViewDate");
validateEntry(parsedMovie, movieTitle);
}
//As expected this will assert to be a show.
@Test
@Disabled
void movieWithTwoColons() {
String movieTitle = "\"Underworld: Awakening : Hello\"";
ViewItem parsedMovie = (ViewItem) ReflectionSupport.invokeMethod(parseMethod, null,
wrapMovieTitle(movieTitle));
validateEntry(parsedMovie, movieTitle);
}
void validateEntry(ViewItem data, String title) {
TestCSVParser.this.validateEntry(NetflixMovie.class, data, title, Integer.MIN_VALUE,null);
}
}
/**
* Helper method to assert if the supplied view item contains the information expected
* @param clazz
* @param data
* @param title
* @param episode
*/
void validateEntry(Class clazz, ViewItem data, String title, int season, String series) {
assertEquals(clazz,data.getClass());
//Assert movie
if(data.getClass().equals(NetflixMovie.class)) {
//@formatter:off
assertAll(
() -> assertFalse(data.isShow()),
() -> assertEquals(title,data.getTitle()),
() -> assertEquals("ViewDate",data.getViewDate())
);
//@formatter:on
}else {
NetflixShowEpisode show = (NetflixShowEpisode) data;
assertAll(
() -> assertTrue(show.isShow()),
() -> assertEquals(title,show.getTitle()),
() -> assertEquals("1.1.2018",show.getViewDate()),
() -> assertEquals(season,show.getSeason()),
() -> assertEquals(series,show.getSeries())
);
}
}
/**
* Wrap the movie title into an object the reflection method can work with
*
* @param movieTitle
*/
Object[] wrapMovieTitle(String movieTitle) {
return new Object[] { new String[] { movieTitle, "ViewDate" } };
}
}
}