X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=test%2Fjalview%2Fws%2Fsifts%2FSiftsClientTest.java;h=b50bafff15ac7fbcd2440dbb1c4df602b1d49d35;hb=207b06de28859536277608ad94897eaa526b1279;hp=9141bad941942d524b2c452d91e166eda6e68025;hpb=63117948459c9ee4748cb1ebbcea963bdf26796c;p=jalview.git diff --git a/test/jalview/ws/sifts/SiftsClientTest.java b/test/jalview/ws/sifts/SiftsClientTest.java index 9141bad..b50baff 100644 --- a/test/jalview/ws/sifts/SiftsClientTest.java +++ b/test/jalview/ws/sifts/SiftsClientTest.java @@ -20,23 +20,33 @@ */ package jalview.ws.sifts; +import static org.testng.Assert.assertEquals; +import static org.testng.Assert.assertTrue; + import jalview.api.DBRefEntryI; +import jalview.bin.Cache; import jalview.datamodel.DBRefEntry; import jalview.datamodel.DBRefSource; import jalview.datamodel.Sequence; import jalview.datamodel.SequenceI; -import jalview.io.AppletFormatAdapter; +import jalview.gui.JvOptionPane; +import jalview.io.DataSourceType; import jalview.structure.StructureMapping; +import jalview.structure.StructureMappingClient.StructureMappingException; import jalview.xml.binding.sifts.Entry.Entity; import java.io.File; import java.io.IOException; import java.util.ArrayList; +import java.util.Arrays; import java.util.HashMap; +import java.util.Iterator; +import java.util.Map; import org.testng.Assert; import org.testng.FileAssert; import org.testng.annotations.AfterTest; +import org.testng.annotations.BeforeClass; import org.testng.annotations.BeforeTest; import org.testng.annotations.Test; @@ -46,6 +56,13 @@ import MCview.PDBfile; public class SiftsClientTest { + @BeforeClass(alwaysRun = true) + public void setUpJvOptionPane() + { + JvOptionPane.setInteractiveMode(false); + JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION); + } + public static final String DEFAULT_SIFTS_DOWNLOAD_DIR = System .getProperty("user.home") + File.separatorChar @@ -67,7 +84,7 @@ public class SiftsClientTest @BeforeTest(alwaysRun = true) public void populateExpectedMapping() throws SiftsException - { + { expectedMapping.put(51, new int[] { 1, 2 }); expectedMapping.put(52, new int[] { 2, 7 }); expectedMapping.put(53, new int[] { 3, 12 }); @@ -165,11 +182,13 @@ public class SiftsClientTest expectedMapping.put(145, new int[] { 95, 714 }); expectedMapping.put(146, new int[] { 96, 722 }); expectedMapping.put(147, new int[] { 97, 729 }); - } - + } + @BeforeTest(alwaysRun = true) - public void setUpSiftsClient() throws SiftsException + public void setUpSiftsClient() throws SiftsException, IOException { + // read test props before manipulating config + Cache.loadProperties("test/jalview/io/testProps.jvprops"); // SIFTs entries are updated weekly - so use saved SIFTs file to enforce // test reproducibility new SiftsSettings(); @@ -179,15 +198,9 @@ public class SiftsClientTest SiftsSettings.setCacheThresholdInDays("2"); SiftsSettings.setFailSafePIDThreshold("70"); PDBfile pdbFile; - try - { - pdbFile = new PDBfile(false, false, false, "test/jalview/io/" - + testPDBId + ".pdb", AppletFormatAdapter.FILE); - siftsClient = new SiftsClient(pdbFile); - } catch (Exception e) - { - e.printStackTrace(); - } + pdbFile = new PDBfile(false, false, false, "test/jalview/io/" + + testPDBId + ".pdb", DataSourceType.FILE); + siftsClient = new SiftsClient(pdbFile); } @AfterTest(alwaysRun = true) @@ -196,45 +209,56 @@ public class SiftsClientTest siftsClient = null; } - @Test(groups = { "Functional" }) - public void getSIFTsFileTest() throws SiftsException + @Test(groups = { "Network" }) + public void getSIFTsFileTest() throws SiftsException, IOException { File siftsFile; - try + siftsFile = SiftsClient.downloadSiftsFile(testPDBId); + FileAssert.assertFile(siftsFile); + long t1 = siftsFile.lastModified(); + + // re-read file should be returned from cache + siftsFile = SiftsClient.downloadSiftsFile(testPDBId); + FileAssert.assertFile(siftsFile); + long t2 = siftsFile.lastModified(); + assertEquals(t1, t2); + + /* + * force fetch by having 0 expiry of cache + * also wait one second, because file timestamp does not + * give millisecond resolution :-( + */ + synchronized (this) { - siftsFile = SiftsClient.downloadSiftsFile(testPDBId); - FileAssert.assertFile(siftsFile); - // test for SIFTs file caching - SiftsSettings.setCacheThresholdInDays("0"); - siftsFile = SiftsClient.getSiftsFile(testPDBId); - FileAssert.assertFile(siftsFile); - SiftsSettings.setCacheThresholdInDays("2"); - } catch (IOException e) - { - e.printStackTrace(); + try + { + wait(1000); + } catch (InterruptedException e) + { + } } + SiftsSettings.setCacheThresholdInDays("0"); + siftsFile = SiftsClient.getSiftsFile(testPDBId); + FileAssert.assertFile(siftsFile); + long t3 = siftsFile.lastModified(); + assertTrue(t3 > t2, "file timestamp unchanged at " + t3); + + SiftsSettings.setCacheThresholdInDays("2"); } - @Test(groups = { "Functional" }) - public void downloadSiftsFileTest() throws SiftsException + @Test(groups = { "Network" }) + public void downloadSiftsFileTest() throws SiftsException, IOException { // Assert that file isn't yet downloaded - if already downloaded, assert it // is deleted Assert.assertTrue(SiftsClient.deleteSiftsFileByPDBId(testPDBId)); File siftsFile; - try - { - siftsFile = SiftsClient.downloadSiftsFile(testPDBId); - FileAssert.assertFile(siftsFile); - SiftsClient.downloadSiftsFile(testPDBId); - } catch (IOException e) - { - e.printStackTrace(); - } + siftsFile = SiftsClient.downloadSiftsFile(testPDBId); + FileAssert.assertFile(siftsFile); + SiftsClient.downloadSiftsFile(testPDBId); } - - @Test(groups = { "Functional" }) + @Test(groups = { "Network" }) public void getAllMappingAccessionTest() { Assert.assertNotNull(siftsClient); @@ -242,7 +266,7 @@ public class SiftsClientTest Assert.assertTrue(siftsClient.getAllMappingAccession().size() > 1); } - @Test(groups = { "Functional" }) + @Test(groups = { "Network" }) public void getGreedyMappingTest() { Assert.assertNotNull(siftsClient); @@ -251,19 +275,41 @@ public class SiftsClientTest // TODO delete when auto-fetching of DBRefEntry is implemented DBRefEntry dbRef = new DBRefEntry("uniprot", "", "P00221"); - dbRef.setStartRes(1); - dbRef.setEndRes(147); testSeq.addDBRef(dbRef); // testSeq.setSourceDBRef(dbRef); try { HashMap actualMapping = siftsClient.getGreedyMapping( - "A", testSeq, - null); + "A", testSeq, null); Assert.assertEquals(testSeq.getStart(), 1); Assert.assertEquals(testSeq.getEnd(), 147); - Assert.assertEquals(actualMapping, expectedMapping); +<<<<<<< HEAD + // Can't do Assert.assertEquals(actualMapping, expectedMapping); + // because this fails in our version of TestNG + Assert.assertEquals(actualMapping.size(), expectedMapping.size()); + Iterator> it = expectedMapping.entrySet() + .iterator(); + while (it.hasNext()) + { + Map.Entry pair = it.next(); + Assert.assertTrue(actualMapping.containsKey(pair.getKey())); + Assert.assertEquals(actualMapping.get(pair.getKey()), + pair.getValue()); + } + +======= + // Assert.assertEquals(actualMapping, expectedMapping); + Assert.assertEquals(actualMapping.size(), expectedMapping.size()); + // Test key set + Assert.assertEquals(actualMapping.keySet(), expectedMapping.keySet()); + // Test entry set + for (int key : expectedMapping.keySet()) + { + Assert.assertTrue(Arrays.equals(expectedMapping.get(key), + actualMapping.get(key))); + } +>>>>>>> f80180a53bf16dc72ecdd4ace0f70c83cb0d274a } catch (Exception e) { e.printStackTrace(); @@ -271,7 +317,7 @@ public class SiftsClientTest } } - @Test(groups = { "Functional" }) + @Test(groups = { "Network" }) private void getAtomIndexTest() { ArrayList atoms = new ArrayList(); @@ -286,73 +332,60 @@ public class SiftsClientTest } @Test( - groups = { "Functional" }, + groups = { "Network" }, expectedExceptions = IllegalArgumentException.class) private void getAtomIndexNullTest() { siftsClient.getAtomIndex(1, null); } - @Test(groups = { "Functional" }) + @Test(groups = { "Network" }) private void padWithGapsTest() { } @Test( - groups = { "Functional" }, - expectedExceptions = SiftsException.class) +groups = { "Network" }, + expectedExceptions = StructureMappingException.class) private void populateAtomPositionsNullTest1() - throws IllegalArgumentException, SiftsException + throws IllegalArgumentException, StructureMappingException { - siftsClient.populateAtomPositions(null, null); + siftsClient.populateAtomPositions(null, null); } @Test( - groups = { "Functional" }, - expectedExceptions = SiftsException.class) +groups = { "Network" }, + expectedExceptions = StructureMappingException.class) private void populateAtomPositionsNullTest2() - throws IllegalArgumentException, SiftsException + throws IllegalArgumentException, StructureMappingException { siftsClient.populateAtomPositions("A", null); } - @Test(groups = { "Functional" }) - public void getValidSourceDBRefTest() + @Test(groups = { "Network" }) + public void getValidSourceDBRefTest() throws SiftsException { - try - { - DBRefEntryI actualValidSrcDBRef = siftsClient - .getValidSourceDBRef(testSeq); - DBRefEntryI expectedDBRef = new DBRefEntry(); - expectedDBRef.setSource(DBRefSource.UNIPROT); - expectedDBRef.setAccessionId("P00221"); - expectedDBRef.setStartRes(1); - expectedDBRef.setEndRes(147); - expectedDBRef.setVersion(""); - Assert.assertEquals(actualValidSrcDBRef, expectedDBRef); - } catch (Exception e) - { - } + DBRefEntryI actualValidSrcDBRef = siftsClient + .getValidSourceDBRef(testSeq); + DBRefEntryI expectedDBRef = new DBRefEntry(); + expectedDBRef.setSource(DBRefSource.UNIPROT); + expectedDBRef.setAccessionId("P00221"); + expectedDBRef.setVersion(""); + Assert.assertEquals(actualValidSrcDBRef, expectedDBRef); } @Test( - groups = { "Functional" }, +groups = { "Network" }, expectedExceptions = SiftsException.class) public void getValidSourceDBRefExceptionTest() throws SiftsException { - SequenceI invalidTestSeq = new Sequence("testSeq", "ABCDEFGH"); - try - { - siftsClient.getValidSourceDBRef(invalidTestSeq); - } catch (SiftsException e) - { - throw new SiftsException(e.getMessage()); - } + SequenceI invalidTestSeq = new Sequence("testSeq", "ABCDEFGH"); + siftsClient.getValidSourceDBRef(invalidTestSeq); } @Test( - groups = { "Functional" }, +groups = { "Network" }, expectedExceptions = SiftsException.class) public void getValidSourceDBRefExceptionXTest() throws SiftsException { @@ -360,60 +393,74 @@ public class SiftsClientTest DBRefEntry invalidDBRef = new DBRefEntry(); invalidDBRef.setAccessionId("BLAR"); invalidTestSeq.addDBRef(invalidDBRef); - try - { - siftsClient.getValidSourceDBRef(invalidTestSeq); - } catch (SiftsException e) - { - throw new SiftsException(e.getMessage()); - } - + siftsClient.getValidSourceDBRef(invalidTestSeq); } - @Test(groups = { "Functional" }) + @Test(groups = { "Network" }) public void isValidDBRefEntryTest() { DBRefEntryI validDBRef = new DBRefEntry(); validDBRef.setSource(DBRefSource.UNIPROT); validDBRef.setAccessionId("P00221"); - validDBRef.setStartRes(1); - validDBRef.setEndRes(147); validDBRef.setVersion(""); Assert.assertTrue(siftsClient.isValidDBRefEntry(validDBRef)); } - @Test(groups = { "Functional" }) + @Test(groups = { "Network" }) public void getSiftsStructureMappingTest() + throws StructureMappingException, Exception { - try + Assert.assertTrue(SiftsSettings.isMapWithSifts()); + StructureMapping strucMapping = siftsClient.getStructureMapping( + testSeq, testPDBId, "A"); + String expectedMappingOutput = "\nSequence ⟷ Structure mapping details\n" + + "Method: SIFTS\n\n" + + "P00221 : 51 - 147 Maps to \n" + + "1A70|A : 1 - 97\n\n" + + "P00221 AAYKVTLVTPTGNVEFQCPDDVYILDAAEEEGIDLPYSCRAGSCSSCAGKLKTGSLNQDDQSFLD\n" + + " |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||\n" + + "1A70|A AAYKVTLVTPTGNVEFQCPDDVYILDAAEEEGIDLPYSCRAGSCSSCAGKLKTGSLNQDDQSFLD\n\n" + + + "P00221 DDQIDEGWVLTCAAYPVSDVTIETHKEEELTA\n" + + " |||||||||||||||||||||||||| |||||\n" + + "1A70|A DDQIDEGWVLTCAAYPVSDVTIETHKKEELTA\n\n" + + + "Length of alignment = 97\n" + "Percentage ID = 98.97\n"; + + Assert.assertEquals(strucMapping.getMappingDetailsOutput(), + expectedMappingOutput); + +<<<<<<< HEAD + // Can't do Assert.assertEquals(strucMapping.getMapping(), expectedMapping); + // because this fails in our version of TestNG + Assert.assertEquals(strucMapping.getMapping().size(), + expectedMapping.size()); + Iterator> it = expectedMapping.entrySet() + .iterator(); + while (it.hasNext()) { - Assert.assertTrue(SiftsSettings.isMapWithSifts()); - StructureMapping strucMapping = siftsClient.getSiftsStructureMapping( - testSeq, testPDBId, "A"); - String expectedMappingOutput = "\nSequence ⟷ Structure mapping details\n" - + "Method: SIFTS\n\n" - + "P00221 : 1 - 97 Maps to \n" - + "1A70|A : 51 - 147\n\n" - + "P00221 AAYKVTLVTPTGNVEFQCPDDVYILDAAEEEGIDLPYSCRAGSCSSCAGKLKTGSLNQDDQSFLD\n" - + " |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||\n" - + "1A70|A AAYKVTLVTPTGNVEFQCPDDVYILDAAEEEGIDLPYSCRAGSCSSCAGKLKTGSLNQDDQSFLD\n\n" - - + "P00221 DDQIDEGWVLTCAAYPVSDVTIETHKEEELTA\n" - + " |||||||||||||||||||||||||| |||||\n" - + "1A70|A DDQIDEGWVLTCAAYPVSDVTIETHKKEELTA\n\n" + - - "Length of alignment = 97\n" + "Percentage ID = 98.97\n"; - - Assert.assertEquals(strucMapping.getMappingDetailsOutput(), - expectedMappingOutput); - Assert.assertEquals(strucMapping.getMapping(), expectedMapping); - } catch (SiftsException e) + Map.Entry pair = it.next(); + Assert.assertTrue(strucMapping.getMapping() + .containsKey(pair.getKey())); + Assert.assertEquals(strucMapping.getMapping().get(pair.getKey()), + pair.getValue()); +======= + // Assert.assertEquals(strucMapping.getMapping(), expectedMapping); + Assert.assertEquals(strucMapping.getMapping().size(), + expectedMapping.size()); + // Test key set + Assert.assertEquals(strucMapping.getMapping().keySet(), + expectedMapping.keySet()); + // Test entry set + for (int key : expectedMapping.keySet()) { - e.printStackTrace(); + Assert.assertTrue(Arrays.equals(expectedMapping.get(key), + strucMapping.getMapping().get(key))); +>>>>>>> f80180a53bf16dc72ecdd4ace0f70c83cb0d274a } } - @Test(groups = { "Functional" }) + @Test(groups = { "Network" }) public void getEntityCountTest() { int actualEntityCount = siftsClient.getEntityCount(); @@ -421,7 +468,7 @@ public class SiftsClientTest Assert.assertEquals(actualEntityCount, 1); } - @Test(groups = { "Functional" }) + @Test(groups = { "Network" }) public void getDbAccessionIdTest() { String actualDbAccId = siftsClient.getDbAccessionId(); @@ -429,7 +476,7 @@ public class SiftsClientTest Assert.assertEquals(actualDbAccId, "1a70"); } - @Test(groups = { "Functional" }) + @Test(groups = { "Network" }) public void getDbCoordSysTest() { String actualDbCoordSys = siftsClient.getDbCoordSys(); @@ -437,7 +484,7 @@ public class SiftsClientTest Assert.assertEquals(actualDbCoordSys, "PDBe"); } - @Test(groups = { "Functional" }) + @Test(groups = { "Network" }) public void getDbSourceTest() { String actualDbSource = siftsClient.getDbSource(); @@ -445,7 +492,7 @@ public class SiftsClientTest Assert.assertEquals(actualDbSource, "PDBe"); } - @Test(groups = { "Functional" }) + @Test(groups = { "Network" }) public void getDbVersionTest() { String actualDbVersion = siftsClient.getDbVersion(); @@ -453,20 +500,15 @@ public class SiftsClientTest Assert.assertEquals(actualDbVersion, "2.0"); } - @Test(groups = { "Functional" }) - public void getEntityByMostOptimalMatchedIdTest() + @Test(groups = { "Network" }) + public void getEntityByMostOptimalMatchedIdTest1() throws IOException, + SiftsException { SiftsClient siftsClientX = null; PDBfile pdbFile; - try - { - pdbFile = new PDBfile(false, false, false, "test/jalview/io/2nq2" - + ".pdb", AppletFormatAdapter.FILE); - siftsClientX = new SiftsClient(pdbFile); - } catch (Exception e) - { - e.printStackTrace(); - } + pdbFile = new PDBfile(false, false, false, "test/jalview/io/2nq2" + + ".pdb", DataSourceType.FILE); + siftsClientX = new SiftsClient(pdbFile); Entity entityA = siftsClientX.getEntityByMostOptimalMatchedId("A"); Assert.assertEquals(entityA.getEntityId(), "A"); Entity entityB = siftsClientX.getEntityByMostOptimalMatchedId("B"); @@ -477,4 +519,29 @@ public class SiftsClientTest Assert.assertEquals(entityD.getEntityId(), "D"); } + + @Test(groups = { "Network" }) + public void getEntityByMostOptimalMatchedIdTest2() throws IOException, + SiftsException + { + // This test is for a SIFTS file in which entity A should map to chain P for + // the given PDB Id. All the other chains shouldn't be mapped as there are + // no SIFTS entity records for them. + SiftsClient siftsClientX = null; + PDBfile pdbFile; + pdbFile = new PDBfile(false, false, false, "test/jalview/io/3ucu.cif", + DataSourceType.FILE); + siftsClientX = new SiftsClient(pdbFile); + Entity entityA = siftsClientX.getEntityByMostOptimalMatchedId("P"); + Entity entityP = siftsClientX.getEntityByMostOptimalMatchedId("A"); + Entity entityR = siftsClientX.getEntityByMostOptimalMatchedId("R"); + Assert.assertEquals(entityA.getEntityId(), "A"); + Assert.assertNotEquals(entityR, "A"); + Assert.assertNotEquals(entityP, "A"); + Assert.assertNotEquals(entityR, "R"); + Assert.assertNotEquals(entityP, "P"); + Assert.assertNull(entityR); + Assert.assertNull(entityP); + + } }