<commit_before>/** Copyright (c) 2012 Memorial Sloan-Kettering Cancer Center.
**
** This library is free software; you can redistribute it and/or modify it
** under the terms of the GNU Lesser General Public License as published
** by the Free Software Foundation; either version 2.1 of the License, or
** any later version.
**
** This library is distributed in the hope that it will be useful, but
** WITHOUT ANY WARRANTY, WITHOUT EVEN THE IMPLIED WARRANTY OF
** MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.  The software and
** documentation provided hereunder is on an "as is" basis, and
** Memorial Sloan-Kettering Cancer Center 
** has no obligations to provide maintenance, support,
** updates, enhancements or modifications.  In no event shall
** Memorial Sloan-Kettering Cancer Center
** be liable to any party for direct, indirect, special,
** incidental or consequential damages, including lost profits, arising
** out of the use of this software and its documentation, even if
** Memorial Sloan-Kettering Cancer Center 
** has been advised of the possibility of such damage.  See
** the GNU Lesser General Public License for more details.
**
** You should have received a copy of the GNU Lesser General Public License
** along with this library; if not, write to the Free Software Foundation,
** Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
**/

package org.mskcc.cbio.cgds.servlet;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashSet;
import junit.framework.TestCase;
import org.mskcc.cbio.cgds.dao.*;
import org.mskcc.cbio.cgds.model.*;
import org.mskcc.cbio.cgds.scripts.ImportTypesOfCancers;
import org.mskcc.cbio.cgds.scripts.ResetDatabase;
import org.mskcc.cbio.cgds.util.NullHttpServletRequest;
import org.mskcc.cbio.cgds.util.ProgressMonitor;
import org.mskcc.cbio.cgds.util.WebserviceParserUtils;

/**
 * JUnit test for WebService class.
 */
public class TestWebService extends TestCase {

   private CancerStudy publicCancerStudy;
   private CancerStudy privateCancerStudy1;
   private CancerStudy privateCancerStudy2;
   private User user1;
   private User user2;
   private String cleartextPwd;
   private GeneticProfile privateGeneticProfile;
   private GeneticProfile publicGeneticProfile;
   
   public void testWebService() throws Exception {
      setUpDBMS();
      
      WebService webService = new WebService();

      // null request
      NullHttpServletRequest aNullHttpServletRequest = new NullHttpServletRequest();
      NullHttpServletResponse aNullHttpServletResponse = new NullHttpServletResponse();
      webService.processClient( aNullHttpServletRequest, aNullHttpServletResponse );
      assertTrue( aNullHttpServletResponse.getOutput().contains("# CGDS Kernel:  Data served up fresh at") );
      
      checkRequest( mkStringArray( WebService.CMD, "getTypesOfCancer" ), 
               mkStringArray( "type_of_cancer_id\tname", "BLCA\tBladder Urothelial Carcinoma" ) );

      // bad command
      checkRequest( mkStringArray( WebService.CMD, "badCommand" ), "Error: 'badCommand' not a valid command." );
      
      /* TBD: Recoded when we provide granualar access
      // public studies
      String[] publicStudies = mkStringArray( "cancer_study_id\tname\tdescription", 
               studyLine( publicCancerStudy ) );
      checkRequest( mkStringArray( WebService.CMD, "getCancerStudies" ), publicStudies );
      */
      
      // no cancer_study_id for "getGeneticProfiles" 
      checkRequest( mkStringArray( WebService.CMD, "getGeneticProfiles" ), 
            mkStringArray( "Error: " + "No cancer study (cancer_study_id), or genetic profile (genetic_profile_id) " +
                              "or case list or (case_list) case set (case_set_id) provided by request. " +
                              "Please reformulate request." ) );
      checkRequest( mkStringArray( WebService.CMD, "getGeneticProfiles",
              WebService.CANCER_STUDY_ID, CancerStudy.NO_SUCH_STUDY +"" ),
            mkStringArray( "Error: " + "Problem when identifying a cancer study for the request." ) );

      // getGeneticProfiles for public study
      String[] publicGenePro = mkStringArray(
         "genetic_profile_id\tgenetic_profile_name\tgenetic_profile_description\t" +
            "cancer_study_id\tgenetic_alteration_type\tshow_profile_in_analysis_tab",
         "stableIdpublic\tprofileName\tprofileDescription\t3\tCOPY_NUMBER_ALTERATION\ttrue");  
      checkRequest( mkStringArray( 
            WebService.CMD, "getGeneticProfiles", 
            WebService.CANCER_STUDY_ID, publicCancerStudy.getCancerStudyStableId()),
            publicGenePro );

      /* TBD: Recoded when we provide granualar access
      // with bad key
      checkRequest( mkStringArray( 
            WebService.CMD, "getGeneticProfiles", 
            WebService.SECRET_KEY, "not_good_key", 
            WebService.CANCER_STUDY_ID, publicCancerStudy.getCancerStudyStableId()),
            publicGenePro );
      
      // missing email address for private study
      String deniedError = "Error: User cannot access the cancer study called 'name'." +
      "Please provide credentials to access private data.";
      checkRequest( mkStringArray( WebService.CMD, "getGeneticProfiles", WebService.CANCER_STUDY_ID, "study1" ),
               mkStringArray( deniedError ) );
      
      // denied for "getGeneticProfiles"; no key 
      checkRequest( mkStringArray( 
            WebService.CMD, "getGeneticProfiles", 
            WebService.CANCER_STUDY_ID, "study1",
            WebService.EMAIL_ADDRESS, user1.getEmail() 
            ), 
            mkStringArray( deniedError ) );
      
      // denied for "getGeneticProfiles"; bad key 
      checkRequest( mkStringArray( 
            WebService.CMD, "getGeneticProfiles", 
            WebService.SECRET_KEY, "not_good_key", 
            WebService.CANCER_STUDY_ID, "study1",
            WebService.EMAIL_ADDRESS, user1.getEmail() 
            ), 
            mkStringArray( deniedError ) );

      // getGeneticProfiles works for private study 
      String[] privateGenePro1 = mkStringArray( "genetic_profile_id\tgenetic_profile_name\tgenetic_profile_description"
            + "\tcancer_study_id\tgenetic_alteration_type\tshow_profile_in_analysis_tab",
      "stableIdPrivate\tprofileName\tprofileDescription\t1\tCOPY_NUMBER_ALTERATION\ttrue");  
      checkRequest( mkStringArray( 
            WebService.CMD, "getGeneticProfiles", 
            WebService.SECRET_KEY, cleartextPwd, 
            WebService.CANCER_STUDY_ID, "study1", 
            WebService.EMAIL_ADDRESS, user1.getEmail() 
            ), 
            mkStringArray( privateGenePro1 ) );
      */
   }
   
   private void checkRequest( String[] requestFields, String... responseLines ) throws IOException{
      checkRequest( false, requestFields, responseLines );
   }
   
   /**
    * check a request
    * @param debug            if true, print servlet's output
    * @param requestFields    array embedded with name, value pairs for the httpRequest 
    * @param responseLines    array of expected responses
    * @throws IOException
    */
   private void checkRequest( boolean debug, String[] requestFields, String... responseLines ) throws IOException{
      WebService webService = new WebService();
      NullHttpServletRequest aNullHttpServletRequest = new NullHttpServletRequest();
      NullHttpServletResponse aNullHttpServletResponse = new NullHttpServletResponse();
      for( int i=0; i<requestFields.length; i += 2 ){
         aNullHttpServletRequest.setParameter( requestFields[i], requestFields[i+1] );
      }

      webService.processClient( aNullHttpServletRequest, aNullHttpServletResponse );
      assertTrue( aNullHttpServletResponse.getOutput().contains("# CGDS Kernel:  Data served up fresh at") );
      if( debug ){
         System.out.println( "\nResponse says:\n" + aNullHttpServletResponse.getOutput() );
      }
      String[] lines = aNullHttpServletResponse.getOutput().split("\n");
      for( int i=0; i<responseLines.length; i++ ){
          System.out.println(responseLines[i] + " : " +  lines[i+1]);
          assertEquals( responseLines[i], lines[i+1]);
      }
   }
   
   private String[] mkStringArray( String... requestFields ){
      return requestFields;
   }
   
   private String studyLine( CancerStudy cancerStudy ){
      return cancerStudy.getCancerStudyStableId() + "\t" + cancerStudy.getName()
              + "\t" + cancerStudy.getDescription();
   }

   public void testGetCancerStudyIDs() throws Exception {

      setUpDBMS();
      HashSet<String> studies;
      NullHttpServletRequest aNullHttpServletRequest = new NullHttpServletRequest();
      studies = WebserviceParserUtils.getCancerStudyIDs(aNullHttpServletRequest);
      assertEquals( 0, studies.size() );
      
      // example getGeneticProfiles request      
      aNullHttpServletRequest.setParameter(WebService.CANCER_STUDY_ID, "HI");
      studies = WebserviceParserUtils.getCancerStudyIDs(aNullHttpServletRequest);
      assertEquals( null, studies );
      aNullHttpServletRequest.setParameter(WebService.CANCER_STUDY_ID, "33");
      studies = WebserviceParserUtils.getCancerStudyIDs(aNullHttpServletRequest);
      assertEquals( null, studies );
      aNullHttpServletRequest.setParameter(WebService.CANCER_STUDY_ID, "study1");
      studies = WebserviceParserUtils.getCancerStudyIDs(aNullHttpServletRequest);<commit_msg>removed a sout statements out of the tests :|<commit_after>/** Copyright (c) 2012 Memorial Sloan-Kettering Cancer Center.
**
** This library is free software; you can redistribute it and/or modify it
** under the terms of the GNU Lesser General Public License as published
** by the Free Software Foundation; either version 2.1 of the License, or
** any later version.
**
** This library is distributed in the hope that it will be useful, but
** WITHOUT ANY WARRANTY, WITHOUT EVEN THE IMPLIED WARRANTY OF
** MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.  The software and
** documentation provided hereunder is on an "as is" basis, and
** Memorial Sloan-Kettering Cancer Center 
** has no obligations to provide maintenance, support,
** updates, enhancements or modifications.  In no event shall
** Memorial Sloan-Kettering Cancer Center
** be liable to any party for direct, indirect, special,
** incidental or consequential damages, including lost profits, arising
** out of the use of this software and its documentation, even if
** Memorial Sloan-Kettering Cancer Center 
** has been advised of the possibility of such damage.  See
** the GNU Lesser General Public License for more details.
**
** You should have received a copy of the GNU Lesser General Public License
** along with this library; if not, write to the Free Software Foundation,
** Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
**/

package org.mskcc.cbio.cgds.servlet;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashSet;
import junit.framework.TestCase;
import org.mskcc.cbio.cgds.dao.*;
import org.mskcc.cbio.cgds.model.*;
import org.mskcc.cbio.cgds.scripts.ImportTypesOfCancers;
import org.mskcc.cbio.cgds.scripts.ResetDatabase;
import org.mskcc.cbio.cgds.util.NullHttpServletRequest;
import org.mskcc.cbio.cgds.util.ProgressMonitor;
import org.mskcc.cbio.cgds.util.WebserviceParserUtils;

/**
 * JUnit test for WebService class.
 */
public class TestWebService extends TestCase {

   private CancerStudy publicCancerStudy;
   private CancerStudy privateCancerStudy1;
   private CancerStudy privateCancerStudy2;
   private User user1;
   private User user2;
   private String cleartextPwd;
   private GeneticProfile privateGeneticProfile;
   private GeneticProfile publicGeneticProfile;
   
   public void testWebService() throws Exception {
      setUpDBMS();
      
      WebService webService = new WebService();

      // null request
      NullHttpServletRequest aNullHttpServletRequest = new NullHttpServletRequest();
      NullHttpServletResponse aNullHttpServletResponse = new NullHttpServletResponse();
      webService.processClient( aNullHttpServletRequest, aNullHttpServletResponse );
      assertTrue( aNullHttpServletResponse.getOutput().contains("# CGDS Kernel:  Data served up fresh at") );
      
      checkRequest( mkStringArray( WebService.CMD, "getTypesOfCancer" ), 
               mkStringArray( "type_of_cancer_id\tname", "BLCA\tBladder Urothelial Carcinoma" ) );

      // bad command
      checkRequest( mkStringArray( WebService.CMD, "badCommand" ), "Error: 'badCommand' not a valid command." );
      
      /* TBD: Recoded when we provide granualar access
      // public studies
      String[] publicStudies = mkStringArray( "cancer_study_id\tname\tdescription", 
               studyLine( publicCancerStudy ) );
      checkRequest( mkStringArray( WebService.CMD, "getCancerStudies" ), publicStudies );
      */
      
      // no cancer_study_id for "getGeneticProfiles" 
      checkRequest( mkStringArray( WebService.CMD, "getGeneticProfiles" ), 
            mkStringArray( "Error: " + "No cancer study (cancer_study_id), or genetic profile (genetic_profile_id) " +
                              "or case list or (case_list) case set (case_set_id) provided by request. " +
                              "Please reformulate request." ) );
      checkRequest( mkStringArray( WebService.CMD, "getGeneticProfiles",
              WebService.CANCER_STUDY_ID, CancerStudy.NO_SUCH_STUDY +"" ),
            mkStringArray( "Error: " + "Problem when identifying a cancer study for the request." ) );

      // getGeneticProfiles for public study
      String[] publicGenePro = mkStringArray(
         "genetic_profile_id\tgenetic_profile_name\tgenetic_profile_description\t" +
            "cancer_study_id\tgenetic_alteration_type\tshow_profile_in_analysis_tab",
         "stableIdpublic\tprofileName\tprofileDescription\t3\tCOPY_NUMBER_ALTERATION\ttrue");  
      checkRequest( mkStringArray( 
            WebService.CMD, "getGeneticProfiles", 
            WebService.CANCER_STUDY_ID, publicCancerStudy.getCancerStudyStableId()),
            publicGenePro );

      /* TBD: Recoded when we provide granualar access
      // with bad key
      checkRequest( mkStringArray( 
            WebService.CMD, "getGeneticProfiles", 
            WebService.SECRET_KEY, "not_good_key", 
            WebService.CANCER_STUDY_ID, publicCancerStudy.getCancerStudyStableId()),
            publicGenePro );
      
      // missing email address for private study
      String deniedError = "Error: User cannot access the cancer study called 'name'." +
      "Please provide credentials to access private data.";
      checkRequest( mkStringArray( WebService.CMD, "getGeneticProfiles", WebService.CANCER_STUDY_ID, "study1" ),
               mkStringArray( deniedError ) );
      
      // denied for "getGeneticProfiles"; no key 
      checkRequest( mkStringArray( 
            WebService.CMD, "getGeneticProfiles", 
            WebService.CANCER_STUDY_ID, "study1",
            WebService.EMAIL_ADDRESS, user1.getEmail() 
            ), 
            mkStringArray( deniedError ) );
      
      // denied for "getGeneticProfiles"; bad key 
      checkRequest( mkStringArray( 
            WebService.CMD, "getGeneticProfiles", 
            WebService.SECRET_KEY, "not_good_key", 
            WebService.CANCER_STUDY_ID, "study1",
            WebService.EMAIL_ADDRESS, user1.getEmail() 
            ), 
            mkStringArray( deniedError ) );

      // getGeneticProfiles works for private study 
      String[] privateGenePro1 = mkStringArray( "genetic_profile_id\tgenetic_profile_name\tgenetic_profile_description"
            + "\tcancer_study_id\tgenetic_alteration_type\tshow_profile_in_analysis_tab",
      "stableIdPrivate\tprofileName\tprofileDescription\t1\tCOPY_NUMBER_ALTERATION\ttrue");  
      checkRequest( mkStringArray( 
            WebService.CMD, "getGeneticProfiles", 
            WebService.SECRET_KEY, cleartextPwd, 
            WebService.CANCER_STUDY_ID, "study1", 
            WebService.EMAIL_ADDRESS, user1.getEmail() 
            ), 
            mkStringArray( privateGenePro1 ) );
      */
   }
   
   private void checkRequest( String[] requestFields, String... responseLines ) throws IOException{
      checkRequest( false, requestFields, responseLines );
   }
   
   /**
    * check a request
    * @param debug            if true, print servlet's output
    * @param requestFields    array embedded with name, value pairs for the httpRequest 
    * @param responseLines    array of expected responses
    * @throws IOException
    */
   private void checkRequest( boolean debug, String[] requestFields, String... responseLines ) throws IOException{
      WebService webService = new WebService();
      NullHttpServletRequest aNullHttpServletRequest = new NullHttpServletRequest();
      NullHttpServletResponse aNullHttpServletResponse = new NullHttpServletResponse();
      for( int i=0; i<requestFields.length; i += 2 ){
         aNullHttpServletRequest.setParameter( requestFields[i], requestFields[i+1] );
      }

      webService.processClient( aNullHttpServletRequest, aNullHttpServletResponse );
      assertTrue( aNullHttpServletResponse.getOutput().contains("# CGDS Kernel:  Data served up fresh at") );
      if( debug ){
         System.out.println( "\nResponse says:\n" + aNullHttpServletResponse.getOutput() );
      }
      String[] lines = aNullHttpServletResponse.getOutput().split("\n");
      for( int i=0; i<responseLines.length; i++ ){
          assertEquals( responseLines[i], lines[i+1]);
      }
   }
   
   private String[] mkStringArray( String... requestFields ){
      return requestFields;
   }
   
   private String studyLine( CancerStudy cancerStudy ){
      return cancerStudy.getCancerStudyStableId() + "\t" + cancerStudy.getName()
              + "\t" + cancerStudy.getDescription();
   }

   public void testGetCancerStudyIDs() throws Exception {

      setUpDBMS();
      HashSet<String> studies;
      NullHttpServletRequest aNullHttpServletRequest = new NullHttpServletRequest();
      studies = WebserviceParserUtils.getCancerStudyIDs(aNullHttpServletRequest);
      assertEquals( 0, studies.size() );
      
      // example getGeneticProfiles request      
      aNullHttpServletRequest.setParameter(WebService.CANCER_STUDY_ID, "HI");
      studies = WebserviceParserUtils.getCancerStudyIDs(aNullHttpServletRequest);
      assertEquals( null, studies );
      aNullHttpServletRequest.setParameter(WebService.CANCER_STUDY_ID, "33");
      studies = WebserviceParserUtils.getCancerStudyIDs(aNullHttpServletRequest);
      assertEquals( null, studies );
      aNullHttpServletRequest.setParameter(WebService.CANCER_STUDY_ID, "study1");
      studies = WebserviceParserUtils.getCancerStudyIDs(aNullHttpServletRequest);
{'fraction_non_alphanumeric': 0.08715644820295983, 'fraction_numerical': 0.004704016913319239, 'mean_word_length': 3.3727755951005314, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1792509', 'n_tokens_mistral': 5325, 'n_tokens_neox': 5176, 'n_words': 1337}