-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2e2ac7f
commit 727fc34
Showing
2 changed files
with
102 additions
and
0 deletions.
There are no files selected for viewing
87 changes: 87 additions & 0 deletions
87
src/test/java/bugs/stackoverflow/belisarius/filters/BlacklistedFilterTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
package bugs.stackoverflow.belisarius.filters; | ||
|
||
import java.io.IOException; | ||
|
||
import bugs.stackoverflow.belisarius.models.Post; | ||
import bugs.stackoverflow.belisarius.utils.PostUtils; | ||
import bugs.stackoverflow.belisarius.services.ApiService; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import com.google.gson.JsonObject; | ||
|
||
public class BlacklistedFilterTest { | ||
private final ApiService apiService = new ApiService("stackoverflow"); | ||
|
||
@Test | ||
public void hitTest() throws IOException { | ||
// blacklisted word added after edit | ||
Post post1 = getSamplePost( | ||
"This was my question. PROBLEM SOLVED: do this", | ||
"This is my question. It is quite big.", | ||
"title", | ||
"title", | ||
"edit" | ||
); | ||
|
||
// blacklisted word existed before edit: | ||
Post post2 = getSamplePost( | ||
"Minor edit, This is my question, Minor edit. PrObLEM fIXeD: do this", | ||
"This is my question. PrObLEM fIXeD: do this", | ||
"title", | ||
"title", | ||
"edit" | ||
); | ||
|
||
// blacklisted word inside HTML tag | ||
Post post3 = getSamplePost( | ||
"This was my question. <code>PROBLEM SOLVED</code>: do this", | ||
"This is my question. It is quite big.", | ||
"title", | ||
"title", | ||
"edit" | ||
); | ||
|
||
// more than one blacklisted words | ||
Post post4 = getSamplePost( | ||
"This was my question. problem solved. answer: do this", | ||
"This is my question. It is quite big.", | ||
"title", | ||
"[SOLVED] title", | ||
"problem fixed, approval overriden" | ||
); | ||
|
||
assertEquals(new BlacklistedFilter(0, post1).isHit(), true); | ||
assertEquals(new BlacklistedFilter(0, post2).isHit(), false); | ||
assertEquals(new BlacklistedFilter(0, post3).isHit(), false); | ||
|
||
BlacklistedFilter filter4 = new BlacklistedFilter(0, post4); | ||
assertEquals(filter4.isHit(), true); | ||
// 1 (title) + 1 (edit summary) + 2 (post body) = 4 | ||
assertEquals(filter4.getTotalScore(), 4.0); | ||
} | ||
|
||
private Post getSamplePost( | ||
String body, | ||
String lastBody, | ||
String title, | ||
String lastTitle, | ||
String summary | ||
) throws IOException { | ||
// choosing this post because it is locked | ||
// if a new revision appears, edit .get(2) accordingly | ||
JsonObject json = apiService.getLatestRevisions("2276572", 1) | ||
.get("items").getAsJsonArray() | ||
.get(2).getAsJsonObject(); | ||
|
||
json.addProperty("body", body); | ||
json.addProperty("last_body", lastBody); | ||
json.addProperty("title", title); | ||
json.addProperty("last_title", lastTitle); | ||
json.addProperty("comment", summary); | ||
|
||
return PostUtils.getPost(json, "stackoverflow", "title"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters