1
+ package bugs .stackoverflow .belisarius .filters ;
2
+
3
+ import java .io .IOException ;
4
+
5
+ import bugs .stackoverflow .belisarius .models .Post ;
6
+ import bugs .stackoverflow .belisarius .utils .PostUtils ;
7
+ import bugs .stackoverflow .belisarius .services .ApiService ;
8
+
9
+ import static org .junit .jupiter .api .Assertions .assertEquals ;
10
+
11
+ import org .junit .jupiter .api .Test ;
12
+
13
+ import com .google .gson .JsonObject ;
14
+
15
+ public class BlacklistedFilterTest {
16
+ private final ApiService apiService = new ApiService ("stackoverflow" );
17
+
18
+ @ Test
19
+ public void hitTest () throws IOException {
20
+ // blacklisted word added after edit
21
+ Post post1 = getSamplePost (
22
+ "This was my question. PROBLEM SOLVED: do this" ,
23
+ "This is my question. It is quite big." ,
24
+ "title" ,
25
+ "title" ,
26
+ "edit"
27
+ );
28
+
29
+ // blacklisted word existed before edit:
30
+ Post post2 = getSamplePost (
31
+ "Minor edit, This is my question, Minor edit. PrObLEM fIXeD: do this" ,
32
+ "This is my question. PrObLEM fIXeD: do this" ,
33
+ "title" ,
34
+ "title" ,
35
+ "edit"
36
+ );
37
+
38
+ // blacklisted word inside HTML tag
39
+ Post post3 = getSamplePost (
40
+ "This was my question. <code>PROBLEM SOLVED</code>: do this" ,
41
+ "This is my question. It is quite big." ,
42
+ "title" ,
43
+ "title" ,
44
+ "edit"
45
+ );
46
+
47
+ // more than one blacklisted words
48
+ Post post4 = getSamplePost (
49
+ "This was my question. problem solved. answer: do this" ,
50
+ "This is my question. It is quite big." ,
51
+ "title" ,
52
+ "[SOLVED] title" ,
53
+ "problem fixed, approval overriden"
54
+ );
55
+
56
+ assertEquals (new BlacklistedFilter (0 , post1 ).isHit (), true );
57
+ assertEquals (new BlacklistedFilter (0 , post2 ).isHit (), false );
58
+ assertEquals (new BlacklistedFilter (0 , post3 ).isHit (), false );
59
+
60
+ BlacklistedFilter filter4 = new BlacklistedFilter (0 , post4 );
61
+ assertEquals (filter4 .isHit (), true );
62
+ // 1 (title) + 1 (edit summary) + 2 (post body) = 4
63
+ assertEquals (filter4 .getTotalScore (), 4.0 );
64
+ }
65
+
66
+ private Post getSamplePost (
67
+ String body ,
68
+ String lastBody ,
69
+ String title ,
70
+ String lastTitle ,
71
+ String summary
72
+ ) throws IOException {
73
+ // choosing this post because it is locked
74
+ // if a new revision appears, edit .get(2) accordingly
75
+ JsonObject json = apiService .getLatestRevisions ("2276572" , 1 )
76
+ .get ("items" ).getAsJsonArray ()
77
+ .get (2 ).getAsJsonObject ();
78
+
79
+ json .addProperty ("body" , body );
80
+ json .addProperty ("last_body" , lastBody );
81
+ json .addProperty ("title" , title );
82
+ json .addProperty ("last_title" , lastTitle );
83
+ json .addProperty ("comment" , summary );
84
+
85
+ return PostUtils .getPost (json , "stackoverflow" , "title" );
86
+ }
87
+ }
0 commit comments