You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@usergrid.apache.org by gr...@apache.org on 2014/12/10 01:43:59 UTC

[2/2] incubator-usergrid git commit: Cleaned up some of the old jersey 2 code. Revamped the separation of concerns for files and added in a WebResource so that the appropriate calls can be made.

Cleaned up some of the old jersey 2 code. Revamped the separation of concerns for files and added in a WebResource so that the appropriate calls can be made.


Project: http://git-wip-us.apache.org/repos/asf/incubator-usergrid/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-usergrid/commit/9af4135a
Tree: http://git-wip-us.apache.org/repos/asf/incubator-usergrid/tree/9af4135a
Diff: http://git-wip-us.apache.org/repos/asf/incubator-usergrid/diff/9af4135a

Branch: refs/heads/UG-rest-test-framework-overhaul
Commit: 9af4135ae7799f5da11e74314804bacb29637a15
Parents: c199365
Author: grey <gr...@apigee.com>
Authored: Tue Dec 9 16:43:54 2014 -0800
Committer: grey <gr...@apigee.com>
Committed: Tue Dec 9 16:43:54 2014 -0800

----------------------------------------------------------------------
 .../test/resource2point0/AbstractRestIT.java    | 12 ++-
 .../rest/test/resource2point0/DumbClient.java   | 31 +++++--
 .../rest/test/resource2point0/RestClient.java   | 92 ++++----------------
 .../resource2point0/endpoints/Collection.java   |  2 +-
 .../endpoints/NamedResource.java                |  8 ++
 .../endpoints/OrganizationResource.java         | 18 ++++
 .../resource2point0/endpoints/UrlResource.java  |  8 ++
 .../endpoints/mgmt/ManagementResource.java      |  8 +-
 .../endpoints/mgmt/OrganizationResource.java    | 44 ++++++++++
 9 files changed, 133 insertions(+), 90 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/9af4135a/stack/rest/src/test/java/org/apache/usergrid/rest/test/resource2point0/AbstractRestIT.java
----------------------------------------------------------------------
diff --git a/stack/rest/src/test/java/org/apache/usergrid/rest/test/resource2point0/AbstractRestIT.java b/stack/rest/src/test/java/org/apache/usergrid/rest/test/resource2point0/AbstractRestIT.java
index acf386e..2113cde 100644
--- a/stack/rest/src/test/java/org/apache/usergrid/rest/test/resource2point0/AbstractRestIT.java
+++ b/stack/rest/src/test/java/org/apache/usergrid/rest/test/resource2point0/AbstractRestIT.java
@@ -22,13 +22,12 @@ import java.net.URLClassLoader;
 import java.util.Arrays;
 
 import org.junit.ClassRule;
+import org.junit.Rule;
 
 import org.apache.usergrid.java.client.Client;
 import org.apache.usergrid.rest.ITSetup;
 import org.apache.usergrid.rest.RestITSuite;
 
-import javax.ws.rs.core.Application;
-
 import com.sun.jersey.api.client.config.ClientConfig;
 import com.sun.jersey.api.client.config.DefaultClientConfig;
 import com.sun.jersey.api.json.JSONConfiguration;
@@ -37,11 +36,6 @@ import com.sun.jersey.test.framework.JerseyTest;
 import com.sun.jersey.test.framework.WebAppDescriptor;
 import com.sun.jersey.test.framework.spi.container.TestContainerFactory;
 
-
-//import com.sun.jersey.api.json.JSONConfiguration;
-//import com.sun.jersey.test.framework.WebAppDescriptor;
-
-
 public class AbstractRestIT extends JerseyTest {
 
     private static ClientConfig clientConfig = new DefaultClientConfig();
@@ -50,6 +44,10 @@ public class AbstractRestIT extends JerseyTest {
 
     @ClassRule
     public static ITSetup setup = new ITSetup( RestITSuite.cassandraResource );
+//
+//    TODO: Allow the client to be setup seperately
+//    @Rule
+//    public ClientSetup clientSetup = new ClientSetup(my url);
 
     protected static final AppDescriptor descriptor;
 

http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/9af4135a/stack/rest/src/test/java/org/apache/usergrid/rest/test/resource2point0/DumbClient.java
----------------------------------------------------------------------
diff --git a/stack/rest/src/test/java/org/apache/usergrid/rest/test/resource2point0/DumbClient.java b/stack/rest/src/test/java/org/apache/usergrid/rest/test/resource2point0/DumbClient.java
index d818a7f..5f247d0 100644
--- a/stack/rest/src/test/java/org/apache/usergrid/rest/test/resource2point0/DumbClient.java
+++ b/stack/rest/src/test/java/org/apache/usergrid/rest/test/resource2point0/DumbClient.java
@@ -16,6 +16,9 @@
  */
 package org.apache.usergrid.rest.test.resource2point0;
 
+import java.util.Map;
+import java.util.UUID;
+
 import org.junit.Ignore;
 import org.junit.Rule;
 import org.junit.Test;
@@ -23,6 +26,7 @@ import org.junit.Test;
 import org.apache.catalina.startup.Tomcat;
 
 import org.apache.usergrid.TomcatMain;
+import org.apache.usergrid.persistence.index.utils.UUIDUtils;
 import org.apache.usergrid.rest.TomcatResource;
 import org.apache.usergrid.rest.test.resource2point0.endpoints.ApplicationResource;
 import org.apache.usergrid.rest.test.resource2point0.endpoints.Collection;
@@ -30,6 +34,7 @@ import org.apache.usergrid.rest.test.resource2point0.endpoints.OrganizationResou
 import org.apache.usergrid.rest.test.resource2point0.endpoints.RootResource;
 import org.apache.usergrid.rest.test.resource2point0.model.Entity;
 import org.apache.usergrid.rest.test.resource2point0.model.EntityResponse;
+import org.apache.usergrid.utils.MapUtils;
 
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNotNull;
@@ -51,21 +56,35 @@ public class DumbClient extends AbstractRestIT {
     @Test
     public void stuff(){
 
+        String name = "stuff"+ UUIDUtils.newTimeUUID();
+        c
+
         //EntityResponse itr  =  client.org( "test" ).getApp( "test" ).users().getEntityResponse();
-        OrganizationResource organizationResource = client.org( "borg" );
-        assertNotNull( organizationResource );
-        assertEquals( getBaseURI().toString()+"borg",client.getPath());
+//        OrganizationResource organizationResource = client.org( "borg" );
+//        assertNotNull( organizationResource );
+//        assertEquals( getBaseURI().toString()+"borg",client.getPath());
+//
+//
+//        ApplicationResource applicationResource = client.org( "morg" ).getApp( "app" );
+//        assertNotNull( applicationResource );
+//        assertEquals( getBaseURI().toString()+"borg/morg",client.getPath());
 
 
-        ApplicationResource applicationResource = client.org( "morg" ).getApp( "app" );
-        assertNotNull( applicationResource );
-        assertEquals( getBaseURI().toString()+"borg/morg",client.getPath());
+        //can't post an org to an organization endpoint you derp. I think it has to be to management.
+        //client.post(mapOrganization(name,name,name+"@apigee.com",name,name  ));
 
         //        for(Entity entity: itr){
 //
 //        }
     }
 
+    public Map<String,String> mapOrganization(String orgName, String username, String email, String name, String password){
+
+        return MapUtils.hashMap( "organization", orgName ).map( "username", username )
+                       .map( "email", email ).map( "name", name )
+                       .map( "password", password);
+    }
+
     @Ignore
     public void stateful(){
 

http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/9af4135a/stack/rest/src/test/java/org/apache/usergrid/rest/test/resource2point0/RestClient.java
----------------------------------------------------------------------
diff --git a/stack/rest/src/test/java/org/apache/usergrid/rest/test/resource2point0/RestClient.java b/stack/rest/src/test/java/org/apache/usergrid/rest/test/resource2point0/RestClient.java
index 4072c95..4c41a0a 100644
--- a/stack/rest/src/test/java/org/apache/usergrid/rest/test/resource2point0/RestClient.java
+++ b/stack/rest/src/test/java/org/apache/usergrid/rest/test/resource2point0/RestClient.java
@@ -16,69 +16,54 @@
  */
 package org.apache.usergrid.rest.test.resource2point0;
 
-import java.io.IOException;
-import java.net.URI;
-import org.junit.ClassRule;
-import org.junit.rules.TestRule;
-import org.junit.runner.Description;
-import org.junit.runners.model.Statement;
-
-import org.apache.usergrid.persistence.index.utils.UUIDUtils;
-import org.apache.usergrid.rest.ITSetup;
-import org.apache.usergrid.rest.RestITSuite;
 import org.apache.usergrid.rest.test.resource2point0.endpoints.mgmt.ManagementResource;
 import org.apache.usergrid.rest.test.resource2point0.endpoints.OrganizationResource;
 import org.apache.usergrid.rest.test.resource2point0.endpoints.UrlResource;
 import org.apache.usergrid.rest.test.resource2point0.state.ClientContext;
-import org.apache.usergrid.rest.test.security.TestAdminUser;
 
 import com.sun.jersey.api.client.Client;
 import com.sun.jersey.api.client.WebResource;
 import com.sun.jersey.api.client.config.ClientConfig;
 import com.sun.jersey.api.client.config.DefaultClientConfig;
-import com.sun.jersey.test.framework.JerseyTest;
 
 
 /**
- * Extends the JerseyTest framework because this is the client that we are going to be using to interact with tomcat
+ * This REST client was made to be able to send calls to any backend system that accepts calls. To do this It needs to
+ * work independently of the existing REST test framework.
  */
-public class RestClient implements UrlResource,TestRule {
-
-//    @ClassRule
-//    public static ITSetup setup = new ITSetup( RestITSuite.cassandraResource );
+public class RestClient implements UrlResource {
 
     private final String serverUrl;
     private final ClientContext context;
-    WebResource resource;
-    ClientConfig config = new DefaultClientConfig(  );//new ClientConfig();
+
+    public WebResource resource;
+    ClientConfig config = new DefaultClientConfig();
     Client client = Client.create( config );
 
-//This should work independantly of test frameowkr. Need to be able to pull the WebResource Url and not have to integrate the webresource into the Endpoints/Client.
-    //This uses jeresy to create the client. Initialize the client with the webresource, and then the CLIENT calls the root resource.
-    //
-    //after initialization of the client htne use it to build our path using our resources.
-    //Just keep checking in early and checkin often.
 
+    /**
+     *
+     * @param serverUrl
+     */
     public RestClient( final String serverUrl ) {
         this.serverUrl = serverUrl;
         this.context = new ClientContext();
         resource = client.resource( serverUrl );
         resource.path( serverUrl );
-
-        //maybe the problem here is with the jaxrs version not having the correct dependencies or methods.
-       // webTarget = client.target( serverUrl );
-        //webTarget = webTarget.path( serverUrl );
     }
 
 
     /**
      * TODO: should this method return the base path or the total path we have built?
-     * @return
      */
     @Override
     public String getPath() {
-        return resource.getURI().toString(); //webResource.getUri().toString();
-        //return serverUrl;
+        return resource.getURI().toString();
+    }
+
+    @Override
+    public WebResource getResource() {
+        return client.resource( serverUrl );
     }
 
 
@@ -94,13 +79,11 @@ public class RestClient implements UrlResource,TestRule {
      * Get hte organization resource
      */
     public OrganizationResource org( final String orgName ) {
-        OrganizationResource organizationResource = new OrganizationResource( orgName, context,  this );
-        resource = resource.path( organizationResource.getPath() );
-        //webTarget = webTarget.path( organizationResource.getPath()); This worked really well, shame it couldn't be used.
-        return new OrganizationResource( orgName, context,  this );
+        return new OrganizationResource( orgName, context, this );
     }
 
-//todo:fix this method for the client.
+
+    //todo:fix this method for the client.
     public void loginAdminUser( final String username, final String password ) {
         //Post isn't implemented yet, but using the method below we should be able to get a superuser password as well.
         //final String token = management().token().post(username, password);
@@ -108,42 +91,5 @@ public class RestClient implements UrlResource,TestRule {
         //context.setToken( token );
     }
 
-    //TODO: maybe take out the below methods to be a seperate class? Follow solid principles? Single responsiblitiy. This is currently
-    //taking on the responsibility of both the
-
-    @Override
-    public Statement apply( Statement base, Description description ) {
-        return statement( base, description );
-    }
-
-    private Statement statement( final Statement base, final Description description ) {
-        return new Statement() {
-            @Override
-            public void evaluate() throws Throwable {
-                before( description );
-                try {
-                    base.evaluate();
-                }
-                finally {
-                    cleanup();
-                }
-            }
-        };
-    }
-    protected void cleanup() {
-        // might want to do something here later
-    }
 
-    //TODO: look over this logic
-    protected void before( Description description ) throws IOException {
-//        String testClass = description.getTestClass().getName();
-//        String methodName = description.getMethodName();
-//        String name = testClass + "." + methodName;
-//
-//        TestAdminUser testAdmin = new TestAdminUser( name+ UUIDUtils.newTimeUUID(),
-//                name + "@usergrid.com"+UUIDUtils.newTimeUUID(),
-//                name + "@usergrid.com"+UUIDUtils.newTimeUUID() );
-//        withOrg( name+ UUIDUtils.newTimeUUID() ).withApp( methodName + UUIDUtils.newTimeUUID() ).withUser(
-//                testAdmin ).initAll();
-    }
 }

http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/9af4135a/stack/rest/src/test/java/org/apache/usergrid/rest/test/resource2point0/endpoints/Collection.java
----------------------------------------------------------------------
diff --git a/stack/rest/src/test/java/org/apache/usergrid/rest/test/resource2point0/endpoints/Collection.java b/stack/rest/src/test/java/org/apache/usergrid/rest/test/resource2point0/endpoints/Collection.java
index 6b31cb6..a51a07c 100644
--- a/stack/rest/src/test/java/org/apache/usergrid/rest/test/resource2point0/endpoints/Collection.java
+++ b/stack/rest/src/test/java/org/apache/usergrid/rest/test/resource2point0/endpoints/Collection.java
@@ -44,7 +44,7 @@ public class Collection extends NamedResource {
      * @return
      */
     public ApiResponse get(final Optional<String> cursor){
-        return null;
+        return getResource().get( ApiResponse.class );
     }
 
 

http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/9af4135a/stack/rest/src/test/java/org/apache/usergrid/rest/test/resource2point0/endpoints/NamedResource.java
----------------------------------------------------------------------
diff --git a/stack/rest/src/test/java/org/apache/usergrid/rest/test/resource2point0/endpoints/NamedResource.java b/stack/rest/src/test/java/org/apache/usergrid/rest/test/resource2point0/endpoints/NamedResource.java
index 32232e0..5f58ba6 100644
--- a/stack/rest/src/test/java/org/apache/usergrid/rest/test/resource2point0/endpoints/NamedResource.java
+++ b/stack/rest/src/test/java/org/apache/usergrid/rest/test/resource2point0/endpoints/NamedResource.java
@@ -19,6 +19,8 @@ package org.apache.usergrid.rest.test.resource2point0.endpoints;
 
 import org.apache.usergrid.rest.test.resource2point0.state.ClientContext;
 
+import com.sun.jersey.api.client.WebResource;
+
 
 /**
  * Base class that is extended by named endpoints.
@@ -45,4 +47,10 @@ public class NamedResource implements UrlResource {
     public String getPath() {
         return name;
     }
+
+
+    @Override
+    public WebResource getResource() {
+        return parent.getResource().path( getPath() );
+    }
 }

http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/9af4135a/stack/rest/src/test/java/org/apache/usergrid/rest/test/resource2point0/endpoints/OrganizationResource.java
----------------------------------------------------------------------
diff --git a/stack/rest/src/test/java/org/apache/usergrid/rest/test/resource2point0/endpoints/OrganizationResource.java b/stack/rest/src/test/java/org/apache/usergrid/rest/test/resource2point0/endpoints/OrganizationResource.java
index 210fd4f..703a534 100644
--- a/stack/rest/src/test/java/org/apache/usergrid/rest/test/resource2point0/endpoints/OrganizationResource.java
+++ b/stack/rest/src/test/java/org/apache/usergrid/rest/test/resource2point0/endpoints/OrganizationResource.java
@@ -17,7 +17,17 @@
 package org.apache.usergrid.rest.test.resource2point0.endpoints;
 
 
+import java.util.Map;
+
+import javax.ws.rs.core.MediaType;
+
+import org.apache.usergrid.rest.test.resource2point0.model.ApiResponse;
 import org.apache.usergrid.rest.test.resource2point0.state.ClientContext;
+import org.apache.usergrid.utils.MapUtils;
+
+import com.sun.jersey.api.client.Client;
+import com.sun.jersey.api.client.UniformInterfaceException;
+import com.sun.jersey.api.client.WebResource;
 
 
 /**
@@ -34,4 +44,12 @@ public class OrganizationResource extends NamedResource {
     public ApplicationResource getApp(final String app){
         return new ApplicationResource( app, context ,this );
     }
+
+    public void post(Map<String,String> organization){
+
+        getResource().type( MediaType.APPLICATION_JSON_TYPE ).accept( MediaType.APPLICATION_JSON )
+                                                   .post( ApiResponse.class, organization );
+    }
+
+
 }

http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/9af4135a/stack/rest/src/test/java/org/apache/usergrid/rest/test/resource2point0/endpoints/UrlResource.java
----------------------------------------------------------------------
diff --git a/stack/rest/src/test/java/org/apache/usergrid/rest/test/resource2point0/endpoints/UrlResource.java b/stack/rest/src/test/java/org/apache/usergrid/rest/test/resource2point0/endpoints/UrlResource.java
index 6c48c30..70d48c1 100644
--- a/stack/rest/src/test/java/org/apache/usergrid/rest/test/resource2point0/endpoints/UrlResource.java
+++ b/stack/rest/src/test/java/org/apache/usergrid/rest/test/resource2point0/endpoints/UrlResource.java
@@ -19,6 +19,8 @@ package org.apache.usergrid.rest.test.resource2point0.endpoints;
 
 import org.apache.usergrid.rest.test.resource2point0.state.ClientContext;
 
+import com.sun.jersey.api.client.WebResource;
+
 
 /**
  * Interface that returns the path that is currently being pointed to.
@@ -31,5 +33,11 @@ public interface UrlResource {
      */
     public String getPath();
 
+    /**
+     * Get the resource
+     * @return
+     */
+    public WebResource getResource();
+
 
 }

http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/9af4135a/stack/rest/src/test/java/org/apache/usergrid/rest/test/resource2point0/endpoints/mgmt/ManagementResource.java
----------------------------------------------------------------------
diff --git a/stack/rest/src/test/java/org/apache/usergrid/rest/test/resource2point0/endpoints/mgmt/ManagementResource.java b/stack/rest/src/test/java/org/apache/usergrid/rest/test/resource2point0/endpoints/mgmt/ManagementResource.java
index 147ff44..846d761 100644
--- a/stack/rest/src/test/java/org/apache/usergrid/rest/test/resource2point0/endpoints/mgmt/ManagementResource.java
+++ b/stack/rest/src/test/java/org/apache/usergrid/rest/test/resource2point0/endpoints/mgmt/ManagementResource.java
@@ -17,9 +17,7 @@
 package org.apache.usergrid.rest.test.resource2point0.endpoints.mgmt;
 
 
-import org.apache.usergrid.rest.test.resource2point0.endpoints.NamedResource;
-import org.apache.usergrid.rest.test.resource2point0.endpoints.TokenResource;
-import org.apache.usergrid.rest.test.resource2point0.endpoints.UrlResource;
+import org.apache.usergrid.rest.test.resource2point0.endpoints.*;
 import org.apache.usergrid.rest.test.resource2point0.state.ClientContext;
 
 
@@ -35,4 +33,8 @@ public class ManagementResource extends NamedResource {
         return new TokenResource( context, this );
     }
 
+    public OrganizationResource orgs(String name) {
+        return new OrganizationResource( name ,context,this );
+    }
+
 }
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/9af4135a/stack/rest/src/test/java/org/apache/usergrid/rest/test/resource2point0/endpoints/mgmt/OrganizationResource.java
----------------------------------------------------------------------
diff --git a/stack/rest/src/test/java/org/apache/usergrid/rest/test/resource2point0/endpoints/mgmt/OrganizationResource.java b/stack/rest/src/test/java/org/apache/usergrid/rest/test/resource2point0/endpoints/mgmt/OrganizationResource.java
new file mode 100644
index 0000000..92cc60e
--- /dev/null
+++ b/stack/rest/src/test/java/org/apache/usergrid/rest/test/resource2point0/endpoints/mgmt/OrganizationResource.java
@@ -0,0 +1,44 @@
+package org.apache.usergrid.rest.test.resource2point0.endpoints.mgmt;
+
+
+/**
+ * Created by ApigeeCorporation on 12/9/14.
+ */
+
+import java.util.Map;
+
+import javax.ws.rs.core.MediaType;
+
+import org.apache.usergrid.rest.test.resource2point0.endpoints.ApplicationResource;
+import org.apache.usergrid.rest.test.resource2point0.endpoints.NamedResource;
+import org.apache.usergrid.rest.test.resource2point0.endpoints.UrlResource;
+import org.apache.usergrid.rest.test.resource2point0.model.ApiResponse;
+import org.apache.usergrid.rest.test.resource2point0.state.ClientContext;
+
+
+/**
+ * This is for the Management endpoint.
+ * Holds the information required for building and chaining organization objects to applications.
+ * Should also contain the GET,PUT,POST,DELETE methods of functioning in here.
+ */
+public class OrganizationResource extends NamedResource {
+
+//TODO: need to find a way to integrate having the orgs/<org_name> into the same endpoint.
+    //maybe I could append the orgs to the end of the parent
+    public OrganizationResource( final String name, final ClientContext context, final UrlResource parent ) {
+        super( name, context, parent. );
+    }
+
+    //TODO: change this so that it reflects the management endpoint
+//    public ApplicationResource getApp(final String app){
+//        return new ApplicationResource( app, context ,this );
+//    }
+
+    public void post(Map<String,String> organization){
+
+        getResource().type( MediaType.APPLICATION_JSON_TYPE ).accept( MediaType.APPLICATION_JSON )
+                     .post( ApiResponse.class, organization );
+    }
+
+
+}