package edu.scripps.genewiki.sync;
import static org.junit.Assert.*;
import java.io.File;
import java.nio.charset.Charset;
import java.util.Arrays;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.junit.BeforeClass;
import org.junit.Test;
import com.google.common.io.Files;
import edu.scripps.mwsync.Wiki;
public class TestEditor{
static Wiki source;
static Wiki target;
static MockWiki mockTarget;
static int period;
static String defaultArticle, pre_testMirrored, post_testMirrored, pre_testGWPGene, post_testGWPGene,
pre_testGWPDisease, post_testGWPDisease, pre_testGWP, post_testGWP, pre_testSWL, post_testSWL,
pre_testLinks, post_testLinks, pre_cleanUp, Rs6311, pre_phosph, post_phosph;
static Charset utf8;
static final String TEST_ROOT = "/etc/gwsync/tests/";
@BeforeClass
public static void setUp() throws Exception {
source = null;
target = new Wiki("genewikiplus.org", "");
target.setUsingCompressedRequests(false);
mockTarget = new MockWiki("genewikiplus.org", "");
mockTarget.setUsingCompressedRequests(false);
period = 5;
utf8 = Charset.forName("UTF-8");
defaultArticle = Files.toString(new File(TEST_ROOT+"pln.wiki"), utf8);
pre_testMirrored = defaultArticle;
post_testMirrored = Files.toString(new File(TEST_ROOT+"pln-mirrored.wiki"), utf8);
pre_testGWPGene = Files.toString(new File(TEST_ROOT+"cdk2-gwp-pre.wiki"), utf8);
post_testGWPGene = Files.toString(new File(TEST_ROOT+"cdk2-gwp-post.wiki"), utf8);
pre_testGWPDisease = Files.toString(new File(TEST_ROOT+"diabetes-mellitus-type-2-pre.wiki"), utf8);
post_testGWPDisease = Files.toString(new File(TEST_ROOT+"diabetes-mellitus-type-2-post.wiki"), utf8);
pre_testGWP = Files.toString(new File(TEST_ROOT+"generic-pre.wiki"), utf8);
post_testGWP = Files.toString(new File(TEST_ROOT+"generic-post.wiki"), utf8);
pre_testSWL = defaultArticle;
post_testSWL = Files.toString(new File(TEST_ROOT+"pln-swl.wiki"), utf8);
pre_testLinks = defaultArticle;
post_testLinks = Files.toString(new File(TEST_ROOT+"pln-links.wiki"), utf8);
pre_cleanUp = Files.toString(new File(TEST_ROOT+"cleanupText.wiki"), utf8);
Rs6311 = Files.toString(new File(TEST_ROOT+"Rs6311.wiki"), utf8);
pre_phosph = Files.toString(new File(TEST_ROOT+"phosphBy.wiki"), utf8);
post_phosph = Files.toString(new File(TEST_ROOT+"post_phosphBy.wiki"), utf8);
}
@Test
public void prependMirroredTemplateTest() {
String result = GeneWikiEditor.prependMirroredTemplate(pre_testMirrored);
assertEquals(post_testMirrored, result);
}
@Test
public void appendGWPTemplateTest1() {
String result = GeneWikiEditor.appendGWPTemplate(pre_testGWPGene, "Cyclin-dependent kinase 2", target);
assertEquals(post_testGWPGene, result);
}
@Test
public void appendGWPTemplateTest2() {
String result = GeneWikiEditor.appendGWPTemplate(pre_testGWPDisease, "Diabetes mellitus type 2", target);
assertEquals(post_testGWPDisease, result);
}
@Test
public void appendGWPTemplateTest3() {
String result = GeneWikiEditor.appendGWPTemplate(pre_testGWP, "1995 (year)", target);
assertEquals(post_testGWP, result);
}
@Test
public void convertSWLTemplatesTest() {
String result = GeneWikiEditor.convertSWLTemplates(pre_testSWL, mockTarget, mockTarget);
assertEquals(post_testSWL, result);
}
@Test
public void convertSWLTemplatesTest2() {
String result = GeneWikiEditor.convertSWLTemplates("{{SWL|type=equivalent|target=foo|label=bar}}", source, target);
String result2 = GeneWikiEditor.convertSWLTemplates("{{SWL|type=equivalent|target=foo}}", source, target);
String expected = "[[equivalent URI:=foo]]";
assertEquals(expected, result);
assertEquals(expected, result2);
}
@Test
public void convertSWLTemplatesTest3() {
String result = GeneWikiEditor.convertSWLTemplates(pre_phosph, mockTarget, mockTarget);
assertEquals(post_phosph, result);
}
@Test
public void addSWLDerivedPropertyTest() {
mockTarget.clearEdits();
GeneWikiEditor.addSWLDerivedProperty("phosphorylated_by", mockTarget, mockTarget);
assertEquals("\n[[subproperty of::is_associated_with]]\n", mockTarget.lastEdit);
}
@Test
public void appendDetachedAnnotationsTest() {
String result = GeneWikiEditor.appendDetachedAnnotations("", "Rs6311", "/etc/gwsync/annotations.db", mockTarget, false);
String expected = "\n{{CAnnotationsStart}}\n" +
"* [[is_associated_with_disease::Rheumatoid arthritis]]\n" +
"* [[is_associated_with_disease::Chronic fatigue syndrome]]\n" +
"* [[is_associated_with_disease::Autistic disorder]]\n" +
"* [[is_associated_with_disease::Obesity]]\n" +
"{{CAnnotationsEnd}}\n";
assertEquals(expected, result);
System.out.println(result);
assertEquals(expected, GeneWikiEditor.appendDetachedAnnotations(result, "Rs6311", "/etc/gwsync/annotations.db", mockTarget, false));
}
@Test
public void appendDetachedAnnotationsTest2() {
String result = GeneWikiEditor.appendDetachedAnnotations(Rs6311, mockTarget);
System.out.println("Test result: "+result);
List<String> mustContain = Arrays.asList("[[is_associated_with_disease::Rheumatoid arthritis]]",
"[[is_associated_with_disease::Chronic fatigue syndrome]]",
"[[is_associated_with_disease::Diabetes mellitus]]",
"[[is_associated_with_disease::Obesity]]",
"[[is_associated_with_disease::Autistic disorder]]",
"\n{{CAnnotationsStart}}\n");
for (String required : mustContain) {
assertTrue("Test result does not contain required annotation "+required, result.contains(required));
}
}
@Test
public void fixLinksTest() {
String testLink1 = "[[this_is::already_semantic]]";
String testLink2_pre = "[[this link|does not exist]]";
String testLink2_post = "[[wikipedia:this link|does not exist]]";
String testLink3 = "[[Category:ignore]]";
String testLink4 = "[[Image:just kidding]]";
assertEquals(testLink1, GeneWikiEditor.fixLinks(testLink1, target));
assertEquals(testLink2_post, GeneWikiEditor.fixLinks(testLink2_pre, target));
assertEquals(testLink2_post, GeneWikiEditor.fixLinks(testLink2_post, target));
assertEquals(testLink3, GeneWikiEditor.fixLinks(testLink3, target));
assertEquals(testLink4, GeneWikiEditor.fixLinks(testLink4, target));
}
@Test
public void cleanUpTests() {
String test = "[[wikipedia:File:something [[is_associated_with::something_else]]]] [[is_associated_with::something]]";
String correct = "[[File:something [[something_else]]]] [[is_associated_with::something]]";
assertEquals(correct, GeneWikiEditor.cleanUp(test));
String postCleanUp = GeneWikiEditor.cleanUp(pre_cleanUp);
Matcher GWPFragment = Pattern.compile("\\{\\{GW\\+[^}|]").matcher(postCleanUp);
assertFalse(GWPFragment.find());
assertFalse(postCleanUp.contains("wikipedia:"));
assertTrue(postCleanUp.contains("is_associated_with::"));
assertEquals(postCleanUp, GeneWikiEditor.cleanUp(postCleanUp));
}
@Test
public void removeDiseaseOntologyCategoryMembershipTest() {
String pre = "[[Category:NotDOTerm]]\n[[Category:Adenocarcinoma]]\n[[Category:Benign mesothelioma]]\n";
assertEquals("[[Category:NotDOTerm]]\n", GeneWikiEditor.removeDiseaseOntologyCategoryMembership(pre, "Mesothelin", "/etc/gwsync/annotations.db", mockTarget, true));
}
@Test
public void categorizeDiseasePageTest() {
mockTarget.clearEdits();
assertTrue(GeneWikiEditor.categorizeDiseasePage("Congenital heart defect", mockTarget));
System.out.println(mockTarget.lastEdit);
assertTrue(mockTarget.lastEdit.contains("\n[[Category:Congenital heart defect]]\n{{GW+|disease}}\n"));
}
}
class MockWiki extends Wiki {
private static final long serialVersionUID = 1L;
public String lastEdit;
public MockWiki(String root, String scriptPath) {
super(root, scriptPath);
lastEdit = "";
}
@Override
public String[] getCategories(String title) {
return new String[0];
}
@Override
public boolean[] exists(String... titles) {
boolean[] results = new boolean[titles.length];
for (int i=0; i<titles.length; i++) {
String title = titles[i];
if (!title.startsWith("!")) {
results[i] = false;
} else {
results[i] = true;
}
}
return results;
}
@Override
public void edit(String title, String src, String summary, boolean isMinor) {
lastEdit = src;
}
public void clearEdits() {
lastEdit = "";
}
}