Skip to content

Commit 727fc34

Browse files
committed
tests: add more tests
1 parent 2e2ac7f commit 727fc34

File tree

2 files changed

+102
-0
lines changed

2 files changed

+102
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
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+
}

src/test/java/bugs/stackoverflow/belisarius/utils/PostUtilsTest.java

+15
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,21 @@ public void getPostObjectTest() throws IOException {
6565
assertEquals("question", postObject.getPostType());
6666
assertEquals("test", postObject.getTitle());
6767
assertEquals("ABCDE12345", postObject.getPreviousRevisionGuid());
68+
69+
Post locked = belisarius.getPost("2276572");
70+
assertEquals(
71+
locked.getBody(),
72+
"<p>How do I comment a block of lines in YAML?</p>\n"
73+
);
74+
assertEquals(
75+
locked.getLastBody(),
76+
"<p>Does anyone know how to comment a block of lines in yaml?</p>\n"
77+
);
78+
assertEquals(locked.getRevisionGuid(), "ec79a719-0ebf-48ca-ae89-27739738f2b7".toUpperCase());
79+
assertEquals(locked.getPreviousRevisionGuid(), "0db0a737-a330-4625-a537-2bf9e73916a3".toUpperCase());
80+
assertEquals(locked.getComment(), "Active reading [&lt;http://en.wikipedia.org/wiki/YAML&gt;].");
81+
assertEquals(locked.getTitle(), "How do you do block comments in YAML?");
82+
assertEquals(locked.getLastTitle(), "How do you do block comment in yaml?");
6883
}
6984

7085
@Test

0 commit comments

Comments
 (0)