websvc.restkit/test/qa-functional/src/org/netbeans/modules/ws/qaf/rest/PatternsTest.java
author Jaroslav Tulach <jtulach@netbeans.org>
Sun Nov 22 11:35:02 2009 +0100 (10 hours ago)
changeset 154033 fa8b12981306
parent 1256745bff76b18629
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.util.HashSet;
    43 import java.util.Set;
    44 import junit.framework.Test;
    45 import org.netbeans.jellytools.Bundle;
    46 import org.netbeans.jellytools.EditorOperator;
    47 import org.netbeans.jellytools.NbDialogOperator;
    48 import org.netbeans.jellytools.WizardOperator;
    49 import org.netbeans.jellytools.nodes.Node;
    50 import org.netbeans.jemmy.operators.JButtonOperator;
    51 import org.netbeans.jemmy.operators.JComboBoxOperator;
    52 import org.netbeans.jemmy.operators.JRadioButtonOperator;
    53 import org.netbeans.jemmy.operators.JTextFieldOperator;
    54 import org.netbeans.junit.NbModuleSuite;
    55 import org.netbeans.modules.ws.qaf.utilities.RestWizardOperator;
    56 import org.openide.filesystems.FileObject;
    57 import org.openide.filesystems.FileUtil;
    58 
    59 /**
    60  * Tests for New REST from Patterns wizard
    61  *
    62  * @author lukas
    63  */
    64 public class PatternsTest extends RestTestBase {
    65 
    66     private enum Pattern {
    67 
    68         CcContainerItem,
    69         ContainerItem,
    70         Singleton;
    71 
    72         @Override
    73         public String toString() {
    74             switch (this) {
    75                 case Singleton:
    76                     //Singleton
    77                     return Bundle.getStringTrimmed("org.netbeans.modules.websvc.rest.wizard.Bundle", "LBL_SingletonResource");
    78                 case ContainerItem:
    79                     //Container-Item
    80                     return Bundle.getStringTrimmed("org.netbeans.modules.websvc.rest.wizard.Bundle", "LBL_ContainerItem");
    81                 case CcContainerItem:
    82                     //Client-Controlled Container-Item
    83                     return Bundle.getStringTrimmed("org.netbeans.modules.websvc.rest.wizard.Bundle", "LBL_ClientControl");
    84             }
    85             throw new AssertionError("Unknown type: " + this); //NOI18N
    86         }
    87 
    88         /**
    89          * Method for getting correct index of the container resource class select
    90          * button in the new RESTful web service from patterns wizard for given type
    91          * of the resource
    92          *
    93          * @return index of the container resource class select button
    94          */
    95         public int getRepresentationClassSelectIndex() {
    96             switch (this) {
    97                 case Singleton:
    98                     return 1;
    99                 case ContainerItem:
   100                 case CcContainerItem:
   101                     return 3;
   102             }
   103             throw new AssertionError("Unknown type: " + this); //NOI18N
   104         }
   105 
   106         /**
   107          * Method for getting correct index of the container resource representation
   108          * class select button in the new RESTful web service from patterns wizard
   109          * for given type of the resource
   110          *
   111          * @return index of the container resource representation class select button
   112          */
   113         public int getContainerRepresentationClassSelectIndex() {
   114             switch (this) {
   115                 case Singleton:
   116                     return -1;
   117                 case ContainerItem:
   118                 case CcContainerItem:
   119                     return 4;
   120             }
   121             throw new AssertionError("Unknown type: " + this); //NOI18N
   122         }
   123     }
   124 
   125     /**
   126      * Def constructor.
   127      *
   128      * @param testName name of particular test case
   129      */
   130     public PatternsTest(String name) {
   131         super(name);
   132     }
   133 
   134     @Override
   135     public String getProjectName() {
   136         return "FromPatterns"; //NOI18N
   137     }
   138 
   139     protected String getRestPackage() {
   140         return "o.n.m.ws.qaf.rest.patterns"; //NOI18N
   141     }
   142 
   143     /**
   144      * Test default setting for Singleton pattern
   145      */
   146     public void testSingletonDef() {
   147         Set<File> files = createWsFromPatterns(null, Pattern.Singleton, null);
   148     }
   149 
   150     /**
   151      * Test application/json mime setting for Singleton pattern
   152      */
   153     public void testSingleton1() {
   154         String name = "Singleton1"; //NOI18N
   155         Set<File> files = createWsFromPatterns(name, Pattern.Singleton, MimeType.APPLICATION_JSON);
   156     }
   157 
   158     /**
   159      * Test text/plain mime setting for Singleton pattern
   160      */
   161     public void testSingleton2() {
   162         String name = "Singleton2"; //NOI18N
   163         Set<File> files = createWsFromPatterns(name, Pattern.Singleton, MimeType.TEXT_PLAIN);
   164     }
   165 
   166     /**
   167      * Test text/html mime setting for Singleton pattern
   168      */
   169     public void testSingleton3() {
   170         String name = "Singleton3"; //NOI18N
   171         Set<File> files = createWsFromPatterns(name, Pattern.Singleton, MimeType.TEXT_HTML);
   172     }
   173 
   174     /**
   175      * Test default setting for Container Item pattern
   176      */
   177     public void testContainerIDef() {
   178         Set<File> files = createWsFromPatterns(null, Pattern.ContainerItem, null);
   179     }
   180 
   181     /**
   182      * Test application/json mime setting for Container Item pattern
   183      */
   184     public void testContainerI1() {
   185         String name = "CI1"; //NOI18N
   186         Set<File> files = createWsFromPatterns(name, Pattern.ContainerItem, MimeType.APPLICATION_JSON);
   187     }
   188 
   189     /**
   190      * Test text/plain mime setting for Container Item pattern
   191      */
   192     public void testContainerI2() {
   193         String name = "CI2"; //NOI18N
   194         Set<File> files = createWsFromPatterns(name, Pattern.ContainerItem, MimeType.TEXT_PLAIN);
   195     }
   196 
   197     /**
   198      * Test text/html mime setting for Container Item pattern
   199      */
   200     public void testContainerI3() {
   201         String name = "CI3"; //NOI18N
   202         Set<File> files = createWsFromPatterns(name, Pattern.ContainerItem, MimeType.TEXT_HTML);
   203     }
   204 
   205     /**
   206      * Test default setting for Client Controlled Container Item pattern
   207      */
   208     public void testCcContainerIDef() {
   209         String name = "Item1"; //NOI18N
   210         Set<File> files = createWsFromPatterns(null, Pattern.CcContainerItem, null);
   211     }
   212 
   213     /**
   214      * Test application/json mime setting for Client Controlled Container Item pattern
   215      */
   216     public void testCcContainerI1() {
   217         String name = "CcCI1"; //NOI18N
   218         Set<File> files = createWsFromPatterns(name, Pattern.CcContainerItem, MimeType.APPLICATION_JSON);
   219     }
   220 
   221     /**
   222      * Test text/plain mime setting for Client Controlled Container Item pattern
   223      */
   224     public void testCcContainerI2() {
   225         String name = "CcCI2"; //NOI18N
   226         Set<File> files = createWsFromPatterns(name, Pattern.CcContainerItem, MimeType.TEXT_PLAIN);
   227     }
   228 
   229     /**
   230      * Test text/html mime setting for Client Controlled Container Item pattern
   231      */
   232     public void testCcContainerI3() {
   233         String name = "CcCI3"; //NOI18N
   234         Set<File> files = createWsFromPatterns(name, Pattern.CcContainerItem, MimeType.TEXT_HTML);
   235     }
   236 
   237     /**
   238      * Make sure all REST services nodes are visible in project log. view
   239      */
   240     public void testNodes() {
   241         Node restNode = getRestNode();
   242         assertEquals("missing nodes?", 20, restNode.getChildren().length); //NOI18N
   243         restNode.tree().clickOnPath(restNode.getTreePath(), 2);
   244         assertTrue("Node not collapsed", restNode.isCollapsed());
   245     }
   246 
   247     private Set<File> createWsFromPatterns(String name, Pattern pattern, MimeType mimeType) {
   248         //RESTful Web Services from Patterns
   249         String patternsTypeName = Bundle.getStringTrimmed("org.netbeans.modules.websvc.rest.wizard.Bundle", "Templates/WebServices/RestServicesFromPatterns");
   250         createNewWSFile(getProject(), patternsTypeName);
   251         WizardOperator wo = new WizardOperator(patternsTypeName);
   252         new JRadioButtonOperator(wo, pattern.ordinal()).clickMouse();
   253         wo.next();
   254         wo = new RestWizardOperator(patternsTypeName);
   255         //set resource package
   256         JComboBoxOperator jcbo = new JComboBoxOperator(wo, new Pkg());
   257         jcbo.clickMouse();
   258         jcbo.clearText();
   259         jcbo.typeText(getRestPackage());
   260         if (name != null) {
   261             //we're not using Defs when name != null !!!
   262             //set resource class name
   263             JTextFieldOperator jtfo = new JTextFieldOperator(wo, new ClsName());
   264             jtfo.clearText();
   265             jtfo.typeText(name + "Cl"); //NOI18N
   266             //set mimeType
   267             if (mimeType != null) {
   268                 jcbo = new JComboBoxOperator(wo, new Mime());
   269                 jcbo.selectItem(mimeType.toString());
   270             }
   271             //set resource representation class
   272             if (MimeType.APPLICATION_JSON.equals(mimeType)) {
   273                 jtfo = new JTextFieldOperator(wo, new RCls());
   274                 jtfo.clearText();
   275                 jtfo.typeText("org.codehaus.jettison.json.JSONString"); //NOI18N
   276             } else if (MimeType.TEXT_PLAIN.equals(mimeType)) {
   277                 new JButtonOperator(wo, pattern.getRepresentationClassSelectIndex()).pushNoBlock();
   278                 //"Find Type"
   279                 String fTypeLbl = Bundle.getStringTrimmed("org.netbeans.modules.java.source.ui.Bundle", "DLG_FindType");
   280                 NbDialogOperator nbo = new NbDialogOperator(fTypeLbl);
   281                 new JTextFieldOperator(nbo, 0).typeText("Level"); //NOI18N
   282                 nbo.ok();
   283             }
   284             if (Pattern.Singleton.equals(pattern)) {
   285                 //set resource Path
   286                 jtfo = new JTextFieldOperator(wo, new Path());
   287                 jtfo.clearText();
   288                 jtfo.typeText(name + "URI"); //NOI18N
   289             } else {
   290                 //set resource Path
   291                 jtfo = new JTextFieldOperator(wo, new Path());
   292                 jtfo.clearText();
   293                 jtfo.typeText("{" + name + "URI}"); //NOI18N
   294                 //set container resource class name
   295                 jtfo = new JTextFieldOperator(wo, new CClsName());
   296                 jtfo.clearText();
   297                 jtfo.typeText(name + "CClass"); //NOI18N
   298                 //set container resource Path
   299                 jtfo = new JTextFieldOperator(wo, new CPath());
   300                 jtfo.clearText();
   301                 jtfo.typeText("/" + name + "ContainerURI"); //NOI18N
   302                 //set container resource representation class
   303                 if (MimeType.APPLICATION_JSON.equals(mimeType)) {
   304                     jtfo = new JTextFieldOperator(wo, new CRCls());
   305                     jtfo.clearText();
   306                     jtfo.typeText("org.codehaus.jettison.json.JSONObject"); //NOI18N
   307                 } else if (MimeType.TEXT_PLAIN.equals(mimeType)) {
   308                     new JButtonOperator(wo, pattern.getContainerRepresentationClassSelectIndex()).pushNoBlock();
   309                     //"Find Type"
   310                     String fTypeLbl = Bundle.getStringTrimmed("org.netbeans.modules.java.source.ui.Bundle", "DLG_FindType");
   311                     NbDialogOperator nbo = new NbDialogOperator(fTypeLbl);
   312                     new JTextFieldOperator(nbo, 0).typeText("Preferences"); //NOI18N
   313                     nbo.ok();
   314                 }
   315             }
   316         }
   317         wo.finish();
   318         String progressDialogTitle = Bundle.getStringTrimmed("org.netbeans.modules.websvc.rest.wizard.Bundle", "LBL_RestServicesFromPatternsProgress");
   319         waitDialogClosed(progressDialogTitle);
   320         Set<File> createdFiles = new HashSet<File>();
   321         switch (pattern) {
   322             case Singleton:
   323                 if (name != null) {
   324                     createdFiles.add(getFileFromProject(name + "Cl")); //NOI18N
   325                 } else {
   326                     createdFiles.add(getFileFromProject("GenericResource")); //NOI18N
   327                 }
   328                 break;
   329             case ContainerItem:
   330                 if (name != null) {
   331                     createdFiles.add(getFileFromProject(name + "Cl")); //NOI18N
   332                     createdFiles.add(getFileFromProject(name + "CClass")); //NOI18N
   333                 } else {
   334                     createdFiles.add(getFileFromProject("ItemResource")); //NOI18N
   335                     createdFiles.add(getFileFromProject("ItemsResource")); //NOI18N
   336                 }
   337                 break;
   338             case CcContainerItem:
   339                 if (name != null) {
   340                     createdFiles.add(getFileFromProject(name + "Cl")); //NOI18N
   341                     createdFiles.add(getFileFromProject(name + "CClass")); //NOI18N
   342                 } else {
   343                     createdFiles.add(getFileFromProject("ItemResource_1")); //NOI18N
   344                     createdFiles.add(getFileFromProject("ItemsResource_1")); //NOI18N
   345                 }
   346                 break;
   347         }
   348         closeCreatedFiles(createdFiles);
   349         checkFiles(createdFiles);
   350         return createdFiles;
   351     }
   352 
   353     private File getFileFromProject(String fileName) {
   354         FileObject fo = getProject().getProjectDirectory().getFileObject("src/java"); //NOI18N
   355         String location = getRestPackage().replace('.', '/') + "/" + fileName + ".java"; //NOI18N
   356         FileObject file = fo.getFileObject(location);
   357         assertNotNull(fileName + " not found at " + FileUtil.toFile(fo).getAbsolutePath() + File.separator + location, file); //NOI18N
   358         return FileUtil.toFile(file);
   359     }
   360 
   361     private void closeCreatedFiles(Set<File> files) {
   362         for (File f : files) {
   363             EditorOperator eo = new EditorOperator(f.getName());
   364             eo.close();
   365         }
   366     }
   367 
   368     /**
   369      * Creates suite from particular test cases. You can define order of testcases here.
   370      */
   371     public static Test suite() {
   372         return NbModuleSuite.create(addServerTests(Server.GLASSFISH_V3, NbModuleSuite.createConfiguration(PatternsTest.class),
   373                 "testSingletonDef", //NOI18N
   374                 "testContainerIDef", //NOI18N
   375                 "testCcContainerIDef", //NOI18N
   376                 "testSingleton1", //NOI18N
   377                 "testCcContainerI1", //NOI18N
   378                 "testSingleton2", //NOI18N
   379                 "testContainerI1", //NOI18N
   380                 "testContainerI2", //NOI18N
   381                 "testSingleton3", //NOI18N
   382                 "testContainerI3", //NOI18N
   383                 "testCcContainerI2", //NOI18N
   384                 "testCcContainerI3", //NOI18N
   385                 "testNodes", //NOI18N
   386                 "testDeploy", //NOI18N
   387                 "testUndeploy" //NOI18N
   388                 ).enableModules(".*").clusters(".*")); //NOI18N
   389     }
   390 
   391     static class Pkg extends JComponentByLabelFinder {
   392 
   393         public Pkg() {
   394             //Resource Package:
   395             super(Bundle.getStringTrimmed("org.netbeans.modules.websvc.rest.wizard.Bundle", "LBL_Package"));
   396         }
   397     }
   398 
   399     static class ClsName extends JComponentByLabelFinder {
   400 
   401         public ClsName() {
   402             //Class Name:
   403             super(Bundle.getStringTrimmed("org.netbeans.modules.websvc.rest.wizard.Bundle", "LBL_ClassName"));
   404         }
   405     }
   406 
   407     static class Mime extends JComponentByLabelFinder {
   408 
   409         public Mime() {
   410             //MIME Type:
   411             super(Bundle.getStringTrimmed("org.netbeans.modules.websvc.rest.wizard.Bundle", "LBL_MimeType"));
   412         }
   413     }
   414 
   415     static class RCls extends JComponentByLabelFinder {
   416 
   417         public RCls() {
   418             //Representation Class:
   419             super(Bundle.getStringTrimmed("org.netbeans.modules.websvc.rest.wizard.Bundle", "LBL_RepresentationClass"));
   420         }
   421     }
   422 
   423     static class Path extends JComponentByLabelFinder {
   424 
   425         public Path() {
   426             //Path:
   427             super(Bundle.getStringTrimmed("org.netbeans.modules.websvc.rest.wizard.Bundle", "LBL_UriTemplate"));
   428         }
   429     }
   430 
   431     static class CClsName extends JComponentByLabelFinder {
   432 
   433         public CClsName() {
   434             //Container Class Name:
   435             super(Bundle.getStringTrimmed("org.netbeans.modules.websvc.rest.wizard.Bundle", "LBL_ContainerClass"));
   436         }
   437     }
   438 
   439     static class CPath extends JComponentByLabelFinder {
   440 
   441         public CPath() {
   442             //Container Path:
   443             super(Bundle.getStringTrimmed("org.netbeans.modules.websvc.rest.wizard.Bundle", "LBL_ContainerUriTemplate"));
   444         }
   445     }
   446 
   447     static class CRCls extends JComponentByLabelFinder {
   448 
   449         public CRCls() {
   450             //Container Representation Class:
   451             super(Bundle.getStringTrimmed("org.netbeans.modules.websvc.rest.wizard.Bundle", "LBL_ContainerRepresentationClass"));
   452         }
   453     }
   454 
   455     static class Loc extends JComponentByLabelFinder {
   456 
   457         public Loc() {
   458             //Location:
   459             super(Bundle.getStringTrimmed("org.netbeans.modules.websvc.rest.wizard.Bundle", "LBL_SrcLocation"));
   460         }
   461     }
   462 }