visualweb.websvcmgr/test/unit/src/org/netbeans/modules/websvc/saas/model/SaasServicesModelTest.java
changeset 3213 c85dcf71e424
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/visualweb.websvcmgr/test/unit/src/org/netbeans/modules/websvc/saas/model/SaasServicesModelTest.java	Wed Mar 23 16:50:27 2011 -0400
     1.3 @@ -0,0 +1,186 @@
     1.4 +/*
     1.5 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     1.6 + *
     1.7 + * Copyright 1997-2010 Oracle and/or its affiliates. All rights reserved.
     1.8 + *
     1.9 + * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
    1.10 + * Other names may be trademarks of their respective owners.
    1.11 + *
    1.12 + * The contents of this file are subject to the terms of either the GNU
    1.13 + * General Public License Version 2 only ("GPL") or the Common
    1.14 + * Development and Distribution License("CDDL") (collectively, the
    1.15 + * "License"). You may not use this file except in compliance with the
    1.16 + * License. You can obtain a copy of the License at
    1.17 + * http://www.netbeans.org/cddl-gplv2.html
    1.18 + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
    1.19 + * specific language governing permissions and limitations under the
    1.20 + * License.  When distributing the software, include this License Header
    1.21 + * Notice in each file and include the License file at
    1.22 + * nbbuild/licenses/CDDL-GPL-2-CP.  Oracle designates this
    1.23 + * particular file as subject to the "Classpath" exception as provided
    1.24 + * by Oracle in the GPL Version 2 section of the License file that
    1.25 + * accompanied this code. If applicable, add the following below the
    1.26 + * License Header, with the fields enclosed by brackets [] replaced by
    1.27 + * your own identifying information:
    1.28 + * "Portions Copyrighted [year] [name of copyright owner]"
    1.29 + * 
    1.30 + * If you wish your version of this file to be governed by only the CDDL
    1.31 + * or only the GPL Version 2, indicate your decision by adding
    1.32 + * "[Contributor] elects to include this software in this distribution
    1.33 + * under the [CDDL or GPL Version 2] license." If you do not indicate a
    1.34 + * single choice of license, a recipient has the option to distribute
    1.35 + * your version of this file under either the CDDL, the GPL Version 2 or
    1.36 + * to extend the choice of license to its licensees as provided above.
    1.37 + * However, if you add GPL Version 2 code and therefore, elected the GPL
    1.38 + * Version 2 license, then the option applies only if the new code is
    1.39 + * made subject to such option by the copyright holder.
    1.40 + * 
    1.41 + * Contributor(s):
    1.42 + * 
    1.43 + * Portions Copyrighted 2008 Sun Microsystems, Inc.
    1.44 + */
    1.45 +
    1.46 +package org.netbeans.modules.websvc.saas.model;
    1.47 +
    1.48 +import org.netbeans.junit.NbTestCase;
    1.49 +import org.netbeans.modules.websvc.saas.spi.websvcmgr.WsdlData;
    1.50 +import org.netbeans.modules.websvc.saas.util.SetupUtil;
    1.51 +import org.openide.filesystems.FileObject;
    1.52 +import org.openide.filesystems.FileUtil;
    1.53 +import java.io.OutputStream;
    1.54 +import org.netbeans.modules.websvc.saas.util.SaasUtil;
    1.55 +
    1.56 +/**
    1.57 + *
    1.58 + * @author nam
    1.59 + */
    1.60 +public class SaasServicesModelTest extends NbTestCase {
    1.61 +    
    1.62 +    public static void resetSaasServicesModel() {
    1.63 +        SaasServicesModel.getInstance().reset();
    1.64 +    }
    1.65 +    
    1.66 +    public static void setWsdlData(WsdlSaas saas, WsdlData data) {
    1.67 +        saas.setWsdlData(data);
    1.68 +    }
    1.69 +    
    1.70 +    public SaasServicesModelTest(String testName) {
    1.71 +        super(testName);
    1.72 +    }            
    1.73 +
    1.74 +    @Override
    1.75 +    protected void setUp() throws Exception {
    1.76 +        SetupUtil.commonSetUp(super.getWorkDir());
    1.77 +    }
    1.78 +
    1.79 +    @Override
    1.80 +    protected void tearDown() throws Exception {
    1.81 +        SetupUtil.commonTearDown();
    1.82 +    }
    1.83 +
    1.84 +    public void testLoading() throws Exception {
    1.85 +
    1.86 +        SaasServicesModel instance = SaasServicesModel.getInstance();
    1.87 +        assertEquals("Zillow", instance.getGroups().get(1).getName());
    1.88 +        //No Sub-group for now
    1.89 +        //SaasGroup group = instance.getGroups().get(0).getChildGroup("Videos");
    1.90 +        //assertNotNull(group);
    1.91 +        SaasGroup group = instance.getGroups().get(1);
    1.92 +        WadlSaas service = (WadlSaas) group.getServices().get(0);
    1.93 +        assertEquals("Real Estate Service", service.getDisplayName());
    1.94 +        assertNotNull(service.getWadlModel());
    1.95 +
    1.96 +        SetupUtil.commonTearDown();
    1.97 +    }
    1.98 +    
    1.99 +    public void testAddGroup() throws Exception {
   1.100 +
   1.101 +        SaasServicesModel instance = SaasServicesModel.getInstance();
   1.102 +        instance.createGroup(instance.getRootGroup(), "groupA");
   1.103 +        SaasGroup added = instance.getRootGroup().getChildGroup("groupA");
   1.104 +        assertEquals("groupA", added.getName());
   1.105 +        instance.createGroup(added, "child1");
   1.106 +        SaasGroup child2 = instance.createGroup(added, "child2");
   1.107 +        instance.createGroup(child2, "grandChild");
   1.108 +        
   1.109 +        instance.reset();
   1.110 +        instance.initRootGroup();
   1.111 +        
   1.112 +        SaasGroup reloaded = instance.getRootGroup().getChildGroup("groupA");
   1.113 +        assertEquals("groupA", reloaded.getName());
   1.114 +        assertEquals(2, reloaded.getChildrenGroups().size());
   1.115 +        assertEquals("child1", reloaded.getChildGroup("child1").getName());
   1.116 +        assertEquals("grandChild", reloaded.getChildGroup("child2").getChildGroup("grandChild").getName());
   1.117 +
   1.118 +        SetupUtil.commonTearDown();
   1.119 +    }
   1.120 +
   1.121 +    public void testAddToPrePackagedGroup() throws Exception {
   1.122 +
   1.123 +        System.out.println(getWorkDirPath());
   1.124 +        SaasServicesModel instance = SaasServicesModel.getInstance();
   1.125 +        SaasGroup delicious = instance.getTopGroup("Delicious");
   1.126 +        assertFalse(delicious.isUserDefined());
   1.127 +        assertNotNull(delicious.getChildService("Bookmarking Service"));
   1.128 +        instance.createGroup(delicious, "myDelicious");
   1.129 +        assertTrue(delicious.getChildGroup("myDelicious").isUserDefined());
   1.130 +        assertNotNull(delicious.getChildService("Bookmarking Service"));
   1.131 +        
   1.132 +        instance.reset();
   1.133 +        instance.initRootGroup();
   1.134 +        
   1.135 +        delicious = instance.getTopGroup("Delicious");
   1.136 +        assertFalse(delicious.isUserDefined());
   1.137 +        assertNotNull(delicious.getChildGroup("myDelicious"));
   1.138 +        assertTrue(delicious.getChildGroup("myDelicious").isUserDefined());
   1.139 +        assertNotNull(delicious.getChildService("Bookmarking Service"));
   1.140 +
   1.141 +    }
   1.142 +
   1.143 +    private FileObject setupLocalWadl() throws Exception {
   1.144 +        FileObject workDir = FileUtil.toFileObject(getWorkDir());
   1.145 +        FileObject wadl = workDir.getFileObject("application.wadl.xml");
   1.146 +        if (wadl == null) {
   1.147 +            wadl = workDir.createData("application.wadl.xml");
   1.148 +        }
   1.149 +        OutputStream out = wadl.getOutputStream();
   1.150 +        try {
   1.151 +            FileUtil.copy(getClass().getResourceAsStream("application.wadl"), out);
   1.152 +        } finally {
   1.153 +            out.close();
   1.154 +        }
   1.155 +        return wadl;
   1.156 +    }
   1.157 +
   1.158 +    public void testAddWadlService() throws Exception {
   1.159 +        FileObject wadl = setupLocalWadl();
   1.160 +        
   1.161 +        String url = wadl.getURL().toExternalForm();
   1.162 +        SaasServicesModel instance = SaasServicesModel.getInstance();
   1.163 +        SaasGroup delicious = instance.getTopGroup("Delicious");
   1.164 +        SaasGroup myDelicious = instance.createGroup(delicious, "myDelicious");
   1.165 +        WadlSaas saas = (WadlSaas) instance.createSaasService(myDelicious, url, null);
   1.166 +        assertEquals(1, saas.getResources().size());
   1.167 +    }
   1.168 +
   1.169 +    public void testRemoveGroupWithWadlService() throws Exception {
   1.170 +        FileObject wadl = setupLocalWadl();
   1.171 +        
   1.172 +        String url = wadl.getURL().toExternalForm();
   1.173 +        SaasServicesModel instance = SaasServicesModel.getInstance();
   1.174 +        SaasGroup delicious = instance.getTopGroup("Delicious");
   1.175 +        SaasGroup myDelicious = instance.createGroup(delicious, "myDelicious");
   1.176 +        instance.createSaasService(myDelicious, url, null);
   1.177 +        
   1.178 +        instance.reset();
   1.179 +        
   1.180 +        SaasGroup g = instance.getTopGroup("Delicious").getChildGroup("myDelicious");
   1.181 +        WadlSaas saas = (WadlSaas)g.getServices().get(0);
   1.182 +        assertEquals(1, saas.getResources().get(0).getMethods().size());
   1.183 +        assertNotNull(instance.getWebServiceHome().getFileObject(SaasUtil.toValidJavaName(saas.getDisplayName())));
   1.184 +        
   1.185 +        instance.removeGroup(g);
   1.186 +        assertNull(instance.getWebServiceHome().getFileObject(SaasUtil.toValidJavaName(saas.getDisplayName())));
   1.187 +        assertNull(instance.getTopGroup("Delicious").getChildGroup("myDelicious"));
   1.188 +    }
   1.189 +}