Adding tests for 3DB, currently 2/4 corrects on PanelTest
authorArnaldo <arnaud.gucciardi@gmail.com>
Mon, 15 Mar 2021 11:56:45 +0000 (12:56 +0100)
committerArnaldo <arnaud.gucciardi@gmail.com>
Mon, 15 Mar 2021 11:56:45 +0000 (12:56 +0100)
src/jalview/fts/service/pdb/PDBFTSRestClient.java
src/jalview/fts/service/threedbeacons/TDBeaconsFTSPanel.java
src/jalview/fts/service/threedbeacons/TDBeaconsFTSRestClient.java
test/jalview/fts/service/pdb/PDBFTSPanelTest.java
test/jalview/fts/threedbeacons/TDBeaconsFTSRestClientTest.java [new file with mode: 0644]
test/jalview/fts/threedbeacons/TDBeaconsPanelTest.java [new file with mode: 0644]

index 22ed591..c18ad48 100644 (file)
@@ -165,7 +165,7 @@ public class PDBFTSRestClient extends FTSRestClient
 
       URI uri = webResource.getURI();
 
-      // System.out.println(uri);
+      System.out.println(uri);
 
       // Execute the REST request
       ClientResponse clientResponse = webResource
@@ -176,7 +176,7 @@ public class PDBFTSRestClient extends FTSRestClient
       Map<String, Object> jsonObj = null;
       String responseString = null;
 
-      // System.out.println("query >>>>>>> " + pdbRestRequest.toString());
+      System.out.println("query >>>>>>> " + pdbRestRequest.toString());
 
       // Check the response status and report exception if one occurs
       int responseStatus = clientResponse.getStatus();
index 21034ac..0d79d9b 100644 (file)
@@ -1,9 +1,18 @@
 package jalview.fts.service.threedbeacons;
 
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.InputStreamReader;
+import java.net.HttpURLConnection;
+import java.net.MalformedURLException;
+import java.net.URL;
 import java.util.HashMap;
 import java.util.HashSet;
 import java.util.Map;
 
+import org.json.JSONArray;
+import org.json.JSONObject;
+
 import jalview.fts.api.FTSDataColumnI;
 import jalview.fts.api.FTSRestClientI;
 import jalview.fts.core.FTSRestRequest;
@@ -17,7 +26,7 @@ import jalview.util.MessageManager;
 public class TDBeaconsFTSPanel extends GFTSPanel
 {       
   private static String defaultFTSFrameTitle = MessageManager
-          .getString("label.threedb_sequence_fetcher");
+          .getString("label.pdb_sequence_fetcher");
 
   private static Map<String, Integer> tempUserPrefs = new HashMap<>();
 
@@ -25,6 +34,8 @@ public class TDBeaconsFTSPanel extends GFTSPanel
 
   private static final String THREEDB_AUTOSEARCH = "FTS.THREEDB.AUTOSEARCH";
   
+  private static HttpURLConnection connection;
+  
   public TDBeaconsFTSPanel(SequenceFetcher fetcher)
   {
     super(fetcher);
@@ -33,6 +44,70 @@ public class TDBeaconsFTSPanel extends GFTSPanel
     this.progressIndicator = (fetcher == null) ? null
             : fetcher.getProgressIndicator();
   }
+  
+  public int TDBeaconsFTSPanel2(String result)
+  {
+    int seqlength = executeParse(result);
+    return seqlength;
+  }
+  
+  public static int executeParse(String query) {
+    BufferedReader reader;
+    String line;
+    StringBuffer responseContent = new StringBuffer();
+    try {
+      URL url = new URL("https://wwwdev.ebi.ac.uk/pdbe/pdbe-kb/3dbeacons-hub-api/uniprot/summary/" + query + ".json");
+      connection = (HttpURLConnection) url.openConnection();
+      connection.setRequestMethod("GET");
+      connection.setConnectTimeout(5000); // <=>5seconds
+      connection.setReadTimeout(5000);
+      // Status check up, 200 = connection succesful
+      int status = connection.getResponseCode();
+      // System.out.println(status);
+
+      if (status > 299) {
+              reader = new BufferedReader(new InputStreamReader(connection.getErrorStream()));
+              while ((line = reader.readLine()) != null) {
+                      responseContent.append(line);
+              }
+              reader.close();
+      } else {
+              reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
+              while ((line = reader.readLine()) != null) {
+                      responseContent.append(line);
+              }
+              reader.close();
+      }
+      //System.out.println(responseContent.toString());
+      int seq = parse(responseContent.toString());
+      return seq;
+
+      } catch (MalformedURLException e) {
+        e.printStackTrace();
+      } catch (IOException e) {
+        e.printStackTrace();
+      } finally {
+      connection.disconnect();
+      }
+      return (Integer) null;
+      
+  }
+  
+  public static int parse(String jsonString) {
+    JSONObject entry = new JSONObject(jsonString);
+    System.out.println(entry);
+    int length = entry.getJSONObject("uniprot_entry").getInt("sequence_length");
+    String  ac = entry.getJSONObject("uniprot_entry").getString("ac");
+    
+    JSONArray structures = entry.getJSONArray("structures");
+    for (int i=0 ; i < structures.length() ; i++) {
+            String id = structures.getJSONObject(i).getString("model_identifier");
+            System.out.println("identifier num " + i + " : " + id);
+    }
+    System.out.println("length : " + length + "; access name : " + ac);
+    return length;
+    
+}
 
   @Override
   public void searchAction(boolean isFreshSearch)
@@ -209,5 +284,9 @@ public class TDBeaconsFTSPanel extends GFTSPanel
     System.out.println("No help implemented yet.");
     
   }
-
+  
+  public static String decodeSearchTerm(String enteredText) {
+    // no multiple query support yet
+    return enteredText;
+  }
 }
index 967fb2d..1ee7f37 100644 (file)
@@ -43,7 +43,8 @@ public class TDBeaconsFTSRestClient extends FTSRestClient
     try
     {
       // Removed wantedFields, sortParam & facetPivot from PDBFTSRClient
-      
+      String wantedFields = getDataColumnsFieldsAsCommaDelimitedString(
+              tdbRestRequest.getWantedFields());
       int responseSize = (tdbRestRequest.getResponseSize() == 0)
               ? getDefaultResponsePageSize()
               : tdbRestRequest.getResponseSize();
@@ -68,7 +69,8 @@ public class TDBeaconsFTSRestClient extends FTSRestClient
         clientResponseClass = ClientResponse.class;
       }
       WebResource webResource;
-      webResource = client.resource(DEFAULT_THREEDBEACONS_DOMAIN);
+      webResource = client.resource(DEFAULT_THREEDBEACONS_DOMAIN)
+              .queryParam("P",query);
       URI uri = webResource.getURI();
         
       System.out.println(uri);
index 224a712..187a30e 100644 (file)
@@ -69,7 +69,7 @@ public class PDBFTSPanelTest
     String expectedString = "1xyz OR text:2xyz OR text:3xyz";
     String outcome = PDBFTSPanel.decodeSearchTerm("1xyz:A;2xyz;3xyz",
             "text");
-    // System.out.println("1 >>>>>>>>>>> " + outcome);
+    System.out.println("1 >>>>>>>>>>> " + outcome);
     assertEquals(expectedString, outcome);
 
     expectedString = "1xyz";
diff --git a/test/jalview/fts/threedbeacons/TDBeaconsFTSRestClientTest.java b/test/jalview/fts/threedbeacons/TDBeaconsFTSRestClientTest.java
new file mode 100644 (file)
index 0000000..be76222
--- /dev/null
@@ -0,0 +1,76 @@
+package jalview.fts.threedbeacons;
+
+import static org.testng.AssertJUnit.assertTrue;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.testng.Assert;
+import org.testng.annotations.AfterMethod;
+import org.testng.annotations.BeforeClass;
+import org.testng.annotations.BeforeMethod;
+import org.testng.annotations.Test;
+
+import jalview.fts.api.FTSDataColumnI;
+import jalview.fts.core.FTSRestRequest;
+import jalview.fts.core.FTSRestResponse;
+import jalview.fts.service.threedbeacons.TDBeaconsFTSRestClient;
+import jalview.gui.JvOptionPane;
+
+public class TDBeaconsFTSRestClientTest
+{
+  @BeforeClass(alwaysRun = true)
+  public void setUpJvOptionPane()
+  {
+    JvOptionPane.setInteractiveMode(false);
+    JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION);
+  }
+
+  @BeforeMethod(alwaysRun = true)
+  public void setUp() throws Exception
+  {
+  }
+
+  @AfterMethod(alwaysRun = true)
+  public void tearDown() throws Exception
+  {
+  }
+
+  @Test(groups = { "External", "Network" })
+  
+  public void executeRequestTest()
+  {
+    List<FTSDataColumnI> wantedFields = new ArrayList<FTSDataColumnI>();
+    try
+    {
+      wantedFields.add(TDBeaconsFTSRestClient.getInstance()
+              .getDataColumnByNameOrCode("uniprot_entry"));
+    } catch (Exception e1)
+    {
+      e1.printStackTrace();
+    }
+    System.out.println(wantedFields);
+
+    FTSRestRequest request = new FTSRestRequest();
+    //request.setAllowEmptySeq(false);
+    //request.setResponseSize(100);
+    request.setSearchTerm("01308.json");
+    //request.setWantedFields(wantedFields);
+    System.out.println(request.toString());
+
+    FTSRestResponse response;
+    try
+    {
+      response = TDBeaconsFTSRestClient.getInstance().executeRequest(request);
+    } catch (Exception e)
+    {
+      e.printStackTrace();
+      Assert.fail("Couldn't execute webservice call!");
+      return;
+    }
+    //assertTrue(response.getNumberOfItemsFound() > 99);
+    assertTrue(response.getSearchSummary() != null);
+    assertTrue(response.getSearchSummary().size() > 99);
+  }
+
+}
diff --git a/test/jalview/fts/threedbeacons/TDBeaconsPanelTest.java b/test/jalview/fts/threedbeacons/TDBeaconsPanelTest.java
new file mode 100644 (file)
index 0000000..1397944
--- /dev/null
@@ -0,0 +1,68 @@
+package jalview.fts.threedbeacons;
+
+
+import static org.testng.AssertJUnit.assertEquals;
+import static org.testng.AssertJUnit.assertTrue;
+
+import jalview.fts.service.pdb.PDBFTSPanel;
+import jalview.fts.service.threedbeacons.TDBeaconsFTSPanel;
+import jalview.gui.JvOptionPane;
+
+import javax.swing.JComboBox;
+import javax.swing.JInternalFrame;
+
+import org.testng.annotations.AfterMethod;
+import org.testng.annotations.BeforeClass;
+import org.testng.annotations.BeforeMethod;
+import org.testng.annotations.Test;
+
+import junit.extensions.PA;
+
+
+public class TDBeaconsPanelTest
+{
+  @BeforeClass(alwaysRun = true)
+  public void setUpJvOptionPane()
+  {
+    JvOptionPane.setInteractiveMode(false);
+    JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION);
+  }
+
+  @BeforeMethod(alwaysRun = true)
+  public void setUp() throws Exception
+  {
+  }
+
+  @AfterMethod(alwaysRun = true)
+  public void tearDown() throws Exception
+  {
+  }
+
+  @Test(groups = { "Functional" })
+  public void populateCmbSearchTargetOptionsTest()
+  {
+    TDBeaconsFTSPanel searchPanel = new TDBeaconsFTSPanel(null);
+    assertTrue(searchPanel.getCmbSearchTarget().getItemCount() > 0);
+    searchPanel.populateCmbSearchTargetOptions();
+  }
+  
+  @Test
+  public void getFTSframeTitleTest() {
+    TDBeaconsFTSPanel searchPanel = new TDBeaconsFTSPanel(null);
+    System.out.println(searchPanel.getFTSFrameTitle());
+  }
+  
+  @Test
+  public void testgetUNIPROTid() {
+    String outcome = TDBeaconsFTSPanel.decodeSearchTerm("P01308");
+    System.out.println(outcome);
+  }
+  
+  @Test
+  public void queryTest() {
+    int outcome = TDBeaconsFTSPanel.executeParse("P01308");
+    //System.out.println("query outcome :" + outcome);
+    int expected_length = 110;
+    assertEquals(outcome, expected_length);
+  }
+}