websvc.restkit/test/qa-functional/src/org/netbeans/modules/ws/qaf/rest/CRUDTest.java
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4 * Copyright 1997-2009 Sun Microsystems, Inc. All rights reserved.
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]"
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.
37 * Portions Copyrighted 2009 Sun Microsystems, Inc.
39 package org.netbeans.modules.ws.qaf.rest;
42 import java.io.IOException;
43 import java.util.Arrays;
44 import java.util.HashSet;
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;
65 * Tests for New REST web services from Entity Classes wizard
67 * Duration of this test suite: aprox. 3min
71 public class CRUDTest extends RestTestBase {
73 private static final Logger LOGGER = Logger.getLogger(CRUDTest.class.getName());
75 /** Default constructor.
76 * @param testName name of particular test case
78 public CRUDTest(String name) {
82 protected String getProjectName() {
83 return "FromEntities"; //NOI18N
86 protected String getRestPackage() {
87 return "o.n.m.ws.qaf.rest.crud"; //NOI18N
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
95 public void testRfE() {
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);
104 //new JButtonOperator(wo, 7).pushNoBlock();
105 //only finish the wizard
107 String generationTitle = Bundle.getStringTrimmed("org.netbeans.modules.j2ee.persistence.wizard.fromdb.Bundle", "TXT_EntityClassesGeneration");
108 waitDialogClosed(generationTitle);
109 new EventTool().waitNoEvent(1500);
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);
119 String addAllLabel = Bundle.getStringTrimmed("org.netbeans.modules.websvc.rest.wizard.Bundle", "LBL_AddAll");
120 new JButtonOperator(wo, addAllLabel).pushNoBlock();
123 JComboBoxOperator jcbo = new JComboBoxOperator(wo, 2);
125 jcbo.typeText(getRestPackage() + ".service"); //NOI18N
127 jcbo = new JComboBoxOperator(wo, 1);
129 jcbo.typeText(getRestPackage() + ".converter"); //NOI18N
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
136 //make sure all REST services nodes are visible in project log. view
137 assertEquals("missing nodes?", 14, getRestNode().getChildren().length);
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
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
153 } catch (InterruptedException ex) {
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);
164 //XXX - workaround for: http://www.netbeans.org/issues/show_bug.cgi?id=130835
166 String addAllLabel = Bundle.getStringTrimmed("org.netbeans.modules.websvc.rest.wizard.Bundle", "LBL_AddAll");
167 new JButtonOperator(wo, addAllLabel).push();
169 String removeAllLabel = Bundle.getStringTrimmed("org.netbeans.modules.websvc.rest.wizard.Bundle", "LBL_RemoveAll");
170 new JButtonOperator(wo, removeAllLabel).push();
173 availableEntities.selectItem("Customer"); //NOI18N
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
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
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
196 waitGorGenerationProgress();
199 } catch (InterruptedException ex) {
201 Set<File> files = getFiles("service"); //NOI18N
202 files.addAll(getFiles("converter")); //NOI18N
203 assertEquals("Some files were not generated", 6, files.size()); //NOI18N
205 //make sure all REST services nodes are visible in project log. view
206 assertEquals("missing nodes?", 16, getRestNode().getChildren().length); //NOI18N
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);
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
231 fo.copy(targetDir, fo.getName(), fo.getExt());
232 } catch (IOException ex) {
233 LOGGER.log(Level.WARNING, "db schema not copied", ex);
238 * Go through given from DB wizard and return WizardOperator from the last panel
241 * @param wo wizard to go through
242 * @return last step in the wizard
244 protected WizardOperator prepareEntityClasses(WizardOperator wo, boolean createPU) {
246 String lbl = Bundle.getStringTrimmed("org.netbeans.modules.websvc.rest.wizard.Bundle", "LBL_AddAll");
247 JButtonOperator allLbl = new JButtonOperator(wo, lbl);
250 } catch (InterruptedException ex) {
252 allLbl.pushNoBlock();
255 } catch (InterruptedException ex) {
258 JComboBoxOperator jcbo = new JComboBoxOperator(wo, 0);
260 jcbo.typeText(getRestPackage());
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);
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
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()));
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
295 * Creates suite from particular test cases. You can define order of testcases here.
297 public static Test suite() {
298 return NbModuleSuite.create(addServerTests(Server.GLASSFISH_V3, NbModuleSuite.createConfiguration(CRUDTest.class),
300 "testPropAccess", //NOI18N
301 "testDeploy", //NOI18N
302 "testCreateRestClient", //NOI18N
303 "testUndeploy" //NOI18N
304 ).enableModules(".*").clusters(".*")); //NOI18N