websvc.restkit/test/qa-functional/src/org/netbeans/modules/ws/qaf/rest/CRUDTest.java
author Jaroslav Tulach <jtulach@netbeans.org>
Sun Nov 22 11:35:02 2009 +0100 (10 hours ago)
changeset 154033 fa8b12981306
parent 1510496ce099212066
permissions -rw-r--r--
Merge RP javadoc improvements
     1 /*
     2  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     3  *
     4  * Copyright 1997-2009 Sun Microsystems, Inc. All rights reserved.
     5  *
     6  * The contents of this file are subject to the terms of either the GNU
     7  * General Public License Version 2 only ("GPL") or the Common
     8  * Development and Distribution License("CDDL") (collectively, the
     9  * "License"). You may not use this file except in compliance with the
    10  * License. You can obtain a copy of the License at
    11  * http://www.netbeans.org/cddl-gplv2.html
    12  * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
    13  * specific language governing permissions and limitations under the
    14  * License.  When distributing the software, include this License Header
    15  * Notice in each file and include the License file at
    16  * nbbuild/licenses/CDDL-GPL-2-CP.  Sun designates this
    17  * particular file as subject to the "Classpath" exception as provided
    18  * by Sun in the GPL Version 2 section of the License file that
    19  * accompanied this code. If applicable, add the following below the
    20  * License Header, with the fields enclosed by brackets [] replaced by
    21  * your own identifying information:
    22  * "Portions Copyrighted [year] [name of copyright owner]"
    23  *
    24  * If you wish your version of this file to be governed by only the CDDL
    25  * or only the GPL Version 2, indicate your decision by adding
    26  * "[Contributor] elects to include this software in this distribution
    27  * under the [CDDL or GPL Version 2] license." If you do not indicate a
    28  * single choice of license, a recipient has the option to distribute
    29  * your version of this file under either the CDDL, the GPL Version 2 or
    30  * to extend the choice of license to its licensees as provided above.
    31  * However, if you add GPL Version 2 code and therefore, elected the GPL
    32  * Version 2 license, then the option applies only if the new code is
    33  * made subject to such option by the copyright holder.
    34  *
    35  * Contributor(s):
    36  *
    37  * Portions Copyrighted 2009 Sun Microsystems, Inc.
    38  */
    39 package org.netbeans.modules.ws.qaf.rest;
    40 
    41 import java.io.File;
    42 import java.io.IOException;
    43 import java.util.Arrays;
    44 import java.util.HashSet;
    45 import java.util.Set;
    46 import java.util.logging.Level;
    47 import java.util.logging.Logger;
    48 import junit.framework.Test;
    49 import org.netbeans.jellytools.Bundle;
    50 import org.netbeans.jellytools.NbDialogOperator;
    51 import org.netbeans.jellytools.WizardOperator;
    52 import org.netbeans.jellytools.nodes.Node;
    53 import org.netbeans.jemmy.EventTool;
    54 import org.netbeans.jemmy.operators.JButtonOperator;
    55 import org.netbeans.jemmy.operators.JCheckBoxOperator;
    56 import org.netbeans.jemmy.operators.JComboBoxOperator;
    57 import org.netbeans.jemmy.operators.JListOperator;
    58 import org.netbeans.jemmy.operators.JTreeOperator;
    59 import org.netbeans.junit.NbModuleSuite;
    60 import org.netbeans.modules.ws.qaf.utilities.RestWizardOperator;
    61 import org.openide.filesystems.FileObject;
    62 import org.openide.filesystems.FileUtil;
    63 
    64 /**
    65  * Tests for New REST web services from Entity Classes wizard
    66  *
    67  * Duration of this test suite: aprox. 3min
    68  *
    69  * @author lukas
    70  */
    71 public class CRUDTest extends RestTestBase {
    72 
    73     private static final Logger LOGGER = Logger.getLogger(CRUDTest.class.getName());
    74 
    75     /** Default constructor.
    76      * @param testName name of particular test case
    77      */
    78     public CRUDTest(String name) {
    79         super(name);
    80     }
    81 
    82     protected String getProjectName() {
    83         return "FromEntities"; //NOI18N
    84     }
    85 
    86     protected String getRestPackage() {
    87         return "o.n.m.ws.qaf.rest.crud"; //NOI18N
    88     }
    89 
    90     /**
    91      * Create new web project with entity classes from sample database
    92      * (jdbc/sample), create new RESTful web services from created entities
    93      * and deploy the project
    94      */
    95     public void testRfE() {
    96         copyDBSchema();
    97         //Persistence
    98         String persistenceLabel = Bundle.getStringTrimmed("org.netbeans.modules.j2ee.persistence.ui.resources.Bundle", "Templates/Persistence");
    99         //Entity Classes from Database
   100         String fromDbLabel = Bundle.getStringTrimmed("org.netbeans.modules.j2ee.persistence.wizard.fromdb.Bundle", "Templates/Persistence/RelatedCMP");
   101         createNewFile(getProject(), persistenceLabel, fromDbLabel);
   102         WizardOperator wo = prepareEntityClasses(new WizardOperator(fromDbLabel), true);
   103         //Finish
   104         //new JButtonOperator(wo, 7).pushNoBlock();
   105         //only finish the wizard
   106         wo.finish();
   107         String generationTitle = Bundle.getStringTrimmed("org.netbeans.modules.j2ee.persistence.wizard.fromdb.Bundle", "TXT_EntityClassesGeneration");
   108         waitDialogClosed(generationTitle);
   109         new EventTool().waitNoEvent(1500);
   110 
   111 
   112         //RESTful Web Services from Entity Classes
   113         String restLabel = Bundle.getStringTrimmed("org.netbeans.modules.websvc.rest.wizard.Bundle", "Templates/WebServices/RestServicesFromEntities");
   114         createNewWSFile(getProject(), restLabel);
   115         wo = new RestWizardOperator(restLabel);
   116         //have to wait until "retrieving message dissapers (see also issue 122802)
   117         new EventTool().waitNoEvent(2500);
   118         //Add All >>
   119         String addAllLabel = Bundle.getStringTrimmed("org.netbeans.modules.websvc.rest.wizard.Bundle", "LBL_AddAll");
   120         new JButtonOperator(wo, addAllLabel).pushNoBlock();
   121         wo.next();
   122         //Resource Package
   123         JComboBoxOperator jcbo = new JComboBoxOperator(wo, 2);
   124         jcbo.clearText();
   125         jcbo.typeText(getRestPackage() + ".service"); //NOI18N
   126         //Converter Package
   127         jcbo = new JComboBoxOperator(wo, 1);
   128         jcbo.clearText();
   129         jcbo.typeText(getRestPackage() + ".converter"); //NOI18N
   130         wo.finish();
   131         waitGorGenerationProgress();
   132         Set<File> files = getFiles(getRestPackage() + ".service"); //NOI18N
   133         files.addAll(getFiles(getRestPackage() + ".converter")); //NOI18N
   134         assertEquals("Some files were not generated", 30, files.size()); //NOI18N
   135         checkFiles(files);
   136         //make sure all REST services nodes are visible in project log. view
   137         assertEquals("missing nodes?", 14, getRestNode().getChildren().length);
   138     }
   139 
   140     /**
   141      * Test creation of RESTful web service from an entity class which
   142      * uses property based access. Also tests functionality of the new RESTful
   143      * web service from entity classes wizard (buttons, updating model
   144      * in the wizard)
   145      */
   146     public void testPropAccess() throws IOException {
   147         //copy entity class into a project
   148         FileObject fo = FileUtil.toFileObject(new File(getRestDataDir(), "Person.java.gf")); //NOI18N
   149         FileObject targetDir = getProject().getProjectDirectory().getFileObject("src/java"); //NOI18N
   150         fo.copy(targetDir.createFolder("entity"), "Person", "java"); //NOI18N
   151         try {
   152             Thread.sleep(1500);
   153         } catch (InterruptedException ex) {
   154         }
   155         //RESTful Web Services from Entity Classes
   156         String restLabel = Bundle.getStringTrimmed("org.netbeans.modules.websvc.rest.wizard.Bundle", "Templates/WebServices/RestServicesFromEntities");
   157         createNewWSFile(getProject(), restLabel);
   158         WizardOperator wo = new WizardOperator(restLabel);
   159         //have to wait until "retrieving message dissapers (see also issue 130835)
   160         new EventTool().waitNoEvent(2500);
   161         JListOperator availableEntities = new JListOperator(wo, 1);
   162         JListOperator selectedEntities = new JListOperator(wo, 2);
   163 
   164         //XXX - workaround for: http://www.netbeans.org/issues/show_bug.cgi?id=130835
   165         //Add All >>
   166         String addAllLabel = Bundle.getStringTrimmed("org.netbeans.modules.websvc.rest.wizard.Bundle", "LBL_AddAll");
   167         new JButtonOperator(wo, addAllLabel).push();
   168         //<< Remove All
   169         String removeAllLabel = Bundle.getStringTrimmed("org.netbeans.modules.websvc.rest.wizard.Bundle", "LBL_RemoveAll");
   170         new JButtonOperator(wo, removeAllLabel).push();
   171         //XXX - end
   172 
   173         availableEntities.selectItem("Customer"); //NOI18N
   174         //Add >
   175         String addLabel = Bundle.getStringTrimmed("org.netbeans.modules.websvc.rest.wizard.Bundle", "LBL_Add");
   176         new JButtonOperator(wo, addLabel).push();
   177         assertEquals("add failed in selected", 6, selectedEntities.getModel().getSize()); //NOI18N
   178         assertEquals("add failed in available", 2, availableEntities.getModel().getSize()); //NOI18N
   179         selectedEntities.selectItem("Product"); //NOI18N
   180         //< Remove
   181         String removeLabel = Bundle.getStringTrimmed("org.netbeans.modules.websvc.rest.wizard.Bundle", "LBL_Remove");
   182         new JButtonOperator(wo, removeLabel).push();
   183         assertEquals("remove failed in selected", 5, selectedEntities.getModel().getSize()); //NOI18N
   184         assertEquals("remove failed in available", 3, availableEntities.getModel().getSize()); //NOI18N
   185 //        //<< Remove All
   186 //        String removeAllLabel = Bundle.getStringTrimmed("org.netbeans.modules.websvc.rest.wizard.Bundle", "LBL_RemoveAll");
   187         new JButtonOperator(wo, removeAllLabel).push();
   188         assertEquals("remove all failed in selected", 0, selectedEntities.getModel().getSize()); //NOI18N
   189         assertEquals("remove all failed in available", 8, availableEntities.getModel().getSize()); //NOI18N
   190         availableEntities.selectItem("Person"); //NOI18N
   191         new JButtonOperator(wo, addLabel).push();
   192         assertEquals("add in selected", 1, selectedEntities.getModel().getSize()); //NOI18N
   193         assertEquals("add in available", 7, availableEntities.getModel().getSize()); //NOI18N
   194         wo.next();
   195         wo.finish();
   196         waitGorGenerationProgress();
   197         try {
   198             Thread.sleep(1500);
   199         } catch (InterruptedException ex) {
   200         }
   201         Set<File> files = getFiles("service"); //NOI18N
   202         files.addAll(getFiles("converter")); //NOI18N
   203         assertEquals("Some files were not generated", 6, files.size()); //NOI18N
   204         checkFiles(files);
   205         //make sure all REST services nodes are visible in project log. view
   206         assertEquals("missing nodes?", 16, getRestNode().getChildren().length); //NOI18N
   207     }
   208 
   209     public void testCreateRestClient() throws IOException {
   210         // not display browser on run
   211         // open project properties
   212         getProjectRootNode().properties();
   213         // "Project Properties"
   214         String projectPropertiesTitle = Bundle.getStringTrimmed("org.netbeans.modules.web.project.ui.customizer.Bundle", "LBL_Customizer_Title");
   215         NbDialogOperator propertiesDialogOper = new NbDialogOperator(projectPropertiesTitle);
   216         // select "Run" category
   217         new Node(new JTreeOperator(propertiesDialogOper), "Run").select();
   218         String displayBrowserLabel = Bundle.getStringTrimmed("org.netbeans.modules.web.project.ui.customizer.Bundle", "LBL_CustomizeRun_DisplayBrowser_JCheckBox");
   219         new JCheckBoxOperator(propertiesDialogOper, displayBrowserLabel).setSelected(false);
   220         // confirm properties dialog
   221         propertiesDialogOper.ok();
   222         String testRestActionName = Bundle.getStringTrimmed("org.netbeans.modules.websvc.rest.projects.Bundle", "LBL_TestRestBeansAction_Name");
   223         getProjectRootNode().performPopupAction(testRestActionName);
   224     }
   225 
   226     protected void copyDBSchema() {
   227         //copy dbschema file to the project
   228         FileObject fo = FileUtil.toFileObject(new File(getRestDataDir(), "sampleDB.dbschema")); //NOI18N
   229         FileObject targetDir = getProject().getProjectDirectory().getFileObject("src/conf"); //NOI18N
   230         try {
   231             fo.copy(targetDir, fo.getName(), fo.getExt());
   232         } catch (IOException ex) {
   233             LOGGER.log(Level.WARNING, "db schema not copied", ex);
   234         }
   235     }
   236 
   237     /**
   238      * Go through given from DB wizard and return WizardOperator from the last panel
   239      * of the wizard
   240      *
   241      * @param wo wizard to go through
   242      * @return last step in the wizard
   243      */
   244     protected WizardOperator prepareEntityClasses(WizardOperator wo, boolean createPU) {
   245         //Add all >>
   246         String lbl = Bundle.getStringTrimmed("org.netbeans.modules.websvc.rest.wizard.Bundle", "LBL_AddAll");
   247         JButtonOperator allLbl = new JButtonOperator(wo, lbl);
   248         try {
   249             Thread.sleep(1000);
   250         } catch (InterruptedException ex) {
   251         }
   252         allLbl.pushNoBlock();
   253         try {
   254             Thread.sleep(1000);
   255         } catch (InterruptedException ex) {
   256         }
   257         wo.next();
   258         JComboBoxOperator jcbo = new JComboBoxOperator(wo, 0);
   259         jcbo.clearText();
   260         jcbo.typeText(getRestPackage());
   261         if (createPU) {
   262             //Create persistence unit
   263             String btnLabel = Bundle.getStringTrimmed("org.netbeans.modules.j2ee.persistence.wizard.Bundle", "LBL_CreatePersistenceUnit");
   264             new JButtonOperator(wo, btnLabel).pushNoBlock();
   265             //Create Persistence Unit
   266             String puDlgTitle = Bundle.getStringTrimmed("org.netbeans.modules.j2ee.persistence.wizard.fromdb.Bundle", "LBL_CreatePersistenceUnit");
   267             NbDialogOperator ndo = new NbDialogOperator(puDlgTitle);
   268             //Create
   269             btnLabel = Bundle.getStringTrimmed("org.netbeans.modules.j2ee.persistence.wizard.unit.Bundle", "LBL_Create");
   270             new JButtonOperator(ndo, btnLabel).pushNoBlock();
   271             //end create pu dialog
   272         }
   273         return wo;
   274     }
   275 
   276     protected Set<File> getFiles(String pkg) {
   277         Set<File> files = new HashSet<File>();
   278         File fo = FileUtil.toFile(getProject().getProjectDirectory());
   279         File pkgRoot = new File(fo, "src/java/" + pkg.replace('.', '/') + "/"); //NOI18N
   280         if (pkgRoot.listFiles() != null) {
   281             files.addAll(Arrays.asList(pkgRoot.listFiles()));
   282         }
   283         return files;
   284     }
   285 
   286     protected void waitGorGenerationProgress() {
   287         //Generating RESTful Web Services from Entity Classes
   288         String restGenTitle = Bundle.getStringTrimmed("org.netbeans.modules.websvc.rest.wizard.Bundle", "LBL_RestSevicicesFromEntitiesProgress");
   289         waitDialogClosed(restGenTitle);
   290         // wait classpath scanning finished
   291         waitScanFinished();
   292     }
   293 
   294     /**
   295      * Creates suite from particular test cases. You can define order of testcases here.
   296      */
   297     public static Test suite() {
   298         return NbModuleSuite.create(addServerTests(Server.GLASSFISH_V3, NbModuleSuite.createConfiguration(CRUDTest.class),
   299                 "testRfE", //NOI18N
   300                 "testPropAccess", //NOI18N
   301                 "testDeploy", //NOI18N
   302                 "testCreateRestClient", //NOI18N
   303                 "testUndeploy" //NOI18N
   304                 ).enableModules(".*").clusters(".*")); //NOI18N
   305     }
   306 }