You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@usergrid.apache.org by ro...@apache.org on 2014/12/22 17:54:21 UTC

[15/50] incubator-usergrid git commit: add test abstractions

add test abstractions


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

Branch: refs/heads/two-dot-o
Commit: ed25b255b3be2406138a9bd7b2bc224c194242ab
Parents: 4ddf6ad
Author: Shawn Feldman <sf...@apache.org>
Authored: Tue Dec 16 13:35:01 2014 -0700
Committer: Shawn Feldman <sf...@apache.org>
Committed: Tue Dec 16 13:35:01 2014 -0700

----------------------------------------------------------------------
 .../rest/test/resource2point0/ClientSetup.java  |  2 +-
 .../endpoints/AbstractCollectionResource.java   | 83 ++++++++++++++++++++
 .../endpoints/AbstractEntityResource.java       | 58 ++++++++++++++
 .../endpoints/CollectionResource.java           | 46 ++---------
 .../endpoints/EntityResource.java               | 22 +-----
 .../resource2point0/endpoints/UserResource.java | 40 ++++++++++
 .../endpoints/UsersResource.java                | 49 ++++++++++++
 .../test/resource2point0/model/Application.java |  7 +-
 .../rest/test/resource2point0/model/Entity.java | 15 +++-
 9 files changed, 258 insertions(+), 64 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/ed25b255/stack/rest/src/test/java/org/apache/usergrid/rest/test/resource2point0/ClientSetup.java
----------------------------------------------------------------------
diff --git a/stack/rest/src/test/java/org/apache/usergrid/rest/test/resource2point0/ClientSetup.java b/stack/rest/src/test/java/org/apache/usergrid/rest/test/resource2point0/ClientSetup.java
index 3d5c637..0b1c860 100644
--- a/stack/rest/src/test/java/org/apache/usergrid/rest/test/resource2point0/ClientSetup.java
+++ b/stack/rest/src/test/java/org/apache/usergrid/rest/test/resource2point0/ClientSetup.java
@@ -88,7 +88,7 @@ public class ClientSetup implements TestRule {
 
         username = "user_"+name + UUIDUtils.newTimeUUID();
         orgName = "org_"+name+UUIDUtils.newTimeUUID();
-        orgName = "app_"+name+UUIDUtils.newTimeUUID();
+        appName = "app_"+name+UUIDUtils.newTimeUUID();
 
         organization = restClient.management().orgs().post(new Organization( orgName,username,username+"@usergrid.com",username,username, null  ));
 

http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/ed25b255/stack/rest/src/test/java/org/apache/usergrid/rest/test/resource2point0/endpoints/AbstractCollectionResource.java
----------------------------------------------------------------------
diff --git a/stack/rest/src/test/java/org/apache/usergrid/rest/test/resource2point0/endpoints/AbstractCollectionResource.java b/stack/rest/src/test/java/org/apache/usergrid/rest/test/resource2point0/endpoints/AbstractCollectionResource.java
new file mode 100644
index 0000000..7e5afa3
--- /dev/null
+++ b/stack/rest/src/test/java/org/apache/usergrid/rest/test/resource2point0/endpoints/AbstractCollectionResource.java
@@ -0,0 +1,83 @@
+/*
+ *
+ *  * Licensed to the Apache Software Foundation (ASF) under one or more
+ *  *  contributor license agreements.  The ASF licenses this file to You
+ *  * under the Apache License, Version 2.0 (the "License"); you may not
+ *  * use this file except in compliance with the License.
+ *  * You may obtain a copy of the License at
+ *  *
+ *  *     http://www.apache.org/licenses/LICENSE-2.0
+ *  *
+ *  * Unless required by applicable law or agreed to in writing, software
+ *  * distributed under the License is distributed on an "AS IS" BASIS,
+ *  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  * See the License for the specific language governing permissions and
+ *  * limitations under the License.  For additional information regarding
+ *  * copyright in this work, please see the NOTICE file in the top level
+ *  * directory of this distribution.
+ *
+ */
+
+package org.apache.usergrid.rest.test.resource2point0.endpoints;
+
+import com.sun.jersey.api.client.WebResource;
+import org.apache.usergrid.rest.test.resource2point0.model.ApiResponse;
+import org.apache.usergrid.rest.test.resource2point0.model.Entity;
+import org.apache.usergrid.rest.test.resource2point0.model.QueryParameters;
+import org.apache.usergrid.rest.test.resource2point0.state.ClientContext;
+
+import javax.ws.rs.core.MediaType;
+
+/**
+ * Classy class class.
+ */
+public abstract class AbstractCollectionResource<T,K> extends NamedResource {
+    public AbstractCollectionResource(String name, ClientContext context, UrlResource parent) {
+        super(name, context, parent);
+    }
+
+    public K entity(final String identifier){
+        return instantiateK(identifier, context, this);
+    }
+
+    /**
+     * Get a list of entities
+     * @return
+     */
+    public ApiResponse get( final QueryParameters parameters){
+        return get(parameters,true);
+    }
+    /**
+     * Get a list of entities
+     * @return
+     */
+    public ApiResponse get(final QueryParameters parameters, final boolean useToken){
+        WebResource resource  = getResource(useToken);
+        addParametersToResource(getResource(), parameters);
+        return resource.type( MediaType.APPLICATION_JSON_TYPE ).accept(MediaType.APPLICATION_JSON)
+                .get(ApiResponse.class);
+    }
+
+    /**
+     * Post the entity to the users collection
+     * @param entity
+     * @return
+     */
+    public T post(final T entity){
+        return instantiateT(getResource(true).post(ApiResponse.class, entity));
+    }
+
+    /**
+     * Put the entity to the users collection
+     * @param entity
+     * @return
+     */
+    public T put(final T entity){
+        return instantiateT(getResource(true).put(ApiResponse.class, entity));
+    }
+
+    protected abstract T instantiateT(ApiResponse response);
+
+    protected abstract K instantiateK(String identifier, ClientContext context, UrlResource parent);
+
+}

http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/ed25b255/stack/rest/src/test/java/org/apache/usergrid/rest/test/resource2point0/endpoints/AbstractEntityResource.java
----------------------------------------------------------------------
diff --git a/stack/rest/src/test/java/org/apache/usergrid/rest/test/resource2point0/endpoints/AbstractEntityResource.java b/stack/rest/src/test/java/org/apache/usergrid/rest/test/resource2point0/endpoints/AbstractEntityResource.java
new file mode 100644
index 0000000..28f20a9
--- /dev/null
+++ b/stack/rest/src/test/java/org/apache/usergrid/rest/test/resource2point0/endpoints/AbstractEntityResource.java
@@ -0,0 +1,58 @@
+/*
+ *
+ *  * Licensed to the Apache Software Foundation (ASF) under one or more
+ *  *  contributor license agreements.  The ASF licenses this file to You
+ *  * under the Apache License, Version 2.0 (the "License"); you may not
+ *  * use this file except in compliance with the License.
+ *  * You may obtain a copy of the License at
+ *  *
+ *  *     http://www.apache.org/licenses/LICENSE-2.0
+ *  *
+ *  * Unless required by applicable law or agreed to in writing, software
+ *  * distributed under the License is distributed on an "AS IS" BASIS,
+ *  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  * See the License for the specific language governing permissions and
+ *  * limitations under the License.  For additional information regarding
+ *  * copyright in this work, please see the NOTICE file in the top level
+ *  * directory of this distribution.
+ *
+ */
+
+package org.apache.usergrid.rest.test.resource2point0.endpoints;
+
+import com.sun.jersey.api.client.WebResource;
+import org.apache.usergrid.rest.test.resource2point0.model.ApiResponse;
+import org.apache.usergrid.rest.test.resource2point0.state.ClientContext;
+
+import javax.ws.rs.core.MediaType;
+
+/**
+ * Classy class class.
+ */
+public abstract class AbstractEntityResource<T> extends NamedResource {
+
+    public AbstractEntityResource(String identifier, ClientContext context, UrlResource parent) {
+        super(identifier, context, parent);
+    }
+
+    public T get() {
+        WebResource resource = getResource(true);
+        ApiResponse response = resource.type(MediaType.APPLICATION_JSON_TYPE).accept(MediaType.APPLICATION_JSON)
+                .get(ApiResponse.class);
+        return instantiateT(response);
+    }
+
+    public ApiResponse post(final T entity) {
+        WebResource resource = getResource(true);
+        return resource.type(MediaType.APPLICATION_JSON_TYPE).accept(MediaType.APPLICATION_JSON)
+                .post(ApiResponse.class, entity);
+    }
+
+    public ApiResponse put(final T entity) {
+        WebResource resource = getResource(true);
+        return resource.type(MediaType.APPLICATION_JSON_TYPE).accept(MediaType.APPLICATION_JSON)
+                .put(ApiResponse.class, entity);
+    }
+
+    protected abstract T instantiateT(ApiResponse response);
+}

http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/ed25b255/stack/rest/src/test/java/org/apache/usergrid/rest/test/resource2point0/endpoints/CollectionResource.java
----------------------------------------------------------------------
diff --git a/stack/rest/src/test/java/org/apache/usergrid/rest/test/resource2point0/endpoints/CollectionResource.java b/stack/rest/src/test/java/org/apache/usergrid/rest/test/resource2point0/endpoints/CollectionResource.java
index 87f0098..695d677 100644
--- a/stack/rest/src/test/java/org/apache/usergrid/rest/test/resource2point0/endpoints/CollectionResource.java
+++ b/stack/rest/src/test/java/org/apache/usergrid/rest/test/resource2point0/endpoints/CollectionResource.java
@@ -36,55 +36,25 @@ import javax.ws.rs.core.MediaType;
  * Holds POST,PUT,GET,DELETE methods for Collections. Models the rest endpoints for the different ways
  * to get an entity out of UG.
  */
-public  class CollectionResource extends NamedResource {
+public  class CollectionResource extends AbstractCollectionResource<Entity,EntityResource> {
 
 
     public CollectionResource(final String name, final ClientContext context, final UrlResource parent) {
         super( name, context, parent );
     }
 
-    public EntityResource entity(final String identifier){
-        return new EntityResource( identifier, context, this );
-    }
 
-    /**
-     * Get a list of entities
-     * @return
-     */
-    public ApiResponse get( final QueryParameters parameters){
-       return get(parameters,true);
-    }
-    /**
-     * Get a list of entities
-     * @return
-     */
-    public ApiResponse get(final QueryParameters parameters, final boolean useToken){
-        WebResource resource  = getResource(useToken);
-        addParametersToResource(getResource(), parameters);
-        return resource.type( MediaType.APPLICATION_JSON_TYPE ).accept(MediaType.APPLICATION_JSON)
-                .get(ApiResponse.class);
+    @Override
+    protected Entity instantiateT(ApiResponse response) {
+        Entity entity = new Entity(response);
+        return entity;
     }
 
-    /**
-     * Post the entity to the users collection
-     * @param entity
-     * @return
-     */
-    public ApiResponse post(final Entity entity){
-        return getResource(true).post(ApiResponse.class,entity);
-    }
+    @Override
+    protected EntityResource instantiateK(String identifier, ClientContext context, UrlResource parent) {
+        return new EntityResource( identifier, context, this );
 
-    /**
-     * Put the entity to the users collection
-     * @param entity
-     * @return
-     */
-    public ApiResponse put(final Entity entity){
-        return getResource(true).post(ApiResponse.class,entity);
     }
 
 
-
-
-
 }

http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/ed25b255/stack/rest/src/test/java/org/apache/usergrid/rest/test/resource2point0/endpoints/EntityResource.java
----------------------------------------------------------------------
diff --git a/stack/rest/src/test/java/org/apache/usergrid/rest/test/resource2point0/endpoints/EntityResource.java b/stack/rest/src/test/java/org/apache/usergrid/rest/test/resource2point0/endpoints/EntityResource.java
index fb4e5f8..f432d9b 100644
--- a/stack/rest/src/test/java/org/apache/usergrid/rest/test/resource2point0/endpoints/EntityResource.java
+++ b/stack/rest/src/test/java/org/apache/usergrid/rest/test/resource2point0/endpoints/EntityResource.java
@@ -31,27 +31,13 @@ import javax.ws.rs.core.MediaType;
 /**
  * Classy class class.
  */
-public class EntityResource extends NamedResource {
+public class EntityResource extends AbstractEntityResource<Entity> {
     public EntityResource(String name, ClientContext context, UrlResource parent) {
         super(name, context, parent);
     }
 
-
-    public ApiResponse get(){
-        WebResource resource  =getResource(true);
-        return resource.type( MediaType.APPLICATION_JSON_TYPE ).accept(MediaType.APPLICATION_JSON)
-                .get(ApiResponse.class);
-    }
-
-    public ApiResponse post(final Entity entity){
-        WebResource resource  =getResource(true);
-        return resource.type( MediaType.APPLICATION_JSON_TYPE ).accept(MediaType.APPLICATION_JSON)
-                .post(ApiResponse.class,entity);
-    }
-
-    public ApiResponse put(final Entity entity){
-        WebResource resource  =getResource(true);
-        return resource.type( MediaType.APPLICATION_JSON_TYPE ).accept(MediaType.APPLICATION_JSON)
-                .put(ApiResponse.class,entity);
+    @Override
+    protected Entity instantiateT(ApiResponse response) {
+        return new Entity(response);
     }
 }

http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/ed25b255/stack/rest/src/test/java/org/apache/usergrid/rest/test/resource2point0/endpoints/UserResource.java
----------------------------------------------------------------------
diff --git a/stack/rest/src/test/java/org/apache/usergrid/rest/test/resource2point0/endpoints/UserResource.java b/stack/rest/src/test/java/org/apache/usergrid/rest/test/resource2point0/endpoints/UserResource.java
new file mode 100644
index 0000000..22b47ce
--- /dev/null
+++ b/stack/rest/src/test/java/org/apache/usergrid/rest/test/resource2point0/endpoints/UserResource.java
@@ -0,0 +1,40 @@
+/*
+ *
+ *  * Licensed to the Apache Software Foundation (ASF) under one or more
+ *  *  contributor license agreements.  The ASF licenses this file to You
+ *  * under the Apache License, Version 2.0 (the "License"); you may not
+ *  * use this file except in compliance with the License.
+ *  * You may obtain a copy of the License at
+ *  *
+ *  *     http://www.apache.org/licenses/LICENSE-2.0
+ *  *
+ *  * Unless required by applicable law or agreed to in writing, software
+ *  * distributed under the License is distributed on an "AS IS" BASIS,
+ *  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  * See the License for the specific language governing permissions and
+ *  * limitations under the License.  For additional information regarding
+ *  * copyright in this work, please see the NOTICE file in the top level
+ *  * directory of this distribution.
+ *
+ */
+
+package org.apache.usergrid.rest.test.resource2point0.endpoints;
+
+import org.apache.usergrid.rest.test.resource2point0.model.ApiResponse;
+import org.apache.usergrid.rest.test.resource2point0.model.Entity;
+import org.apache.usergrid.rest.test.resource2point0.model.User;
+import org.apache.usergrid.rest.test.resource2point0.state.ClientContext;
+
+/**
+ * Classy class class.
+ */
+public class UserResource extends AbstractEntityResource<User>  {
+    public UserResource(String name, ClientContext context, UrlResource parent) {
+        super(name, context, parent);
+    }
+
+    @Override
+    protected User instantiateT(ApiResponse response) {
+        return new User(response);
+    }
+}

http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/ed25b255/stack/rest/src/test/java/org/apache/usergrid/rest/test/resource2point0/endpoints/UsersResource.java
----------------------------------------------------------------------
diff --git a/stack/rest/src/test/java/org/apache/usergrid/rest/test/resource2point0/endpoints/UsersResource.java b/stack/rest/src/test/java/org/apache/usergrid/rest/test/resource2point0/endpoints/UsersResource.java
new file mode 100644
index 0000000..4eb39b9
--- /dev/null
+++ b/stack/rest/src/test/java/org/apache/usergrid/rest/test/resource2point0/endpoints/UsersResource.java
@@ -0,0 +1,49 @@
+/*
+ *
+ *  * Licensed to the Apache Software Foundation (ASF) under one or more
+ *  *  contributor license agreements.  The ASF licenses this file to You
+ *  * under the Apache License, Version 2.0 (the "License"); you may not
+ *  * use this file except in compliance with the License.
+ *  * You may obtain a copy of the License at
+ *  *
+ *  *     http://www.apache.org/licenses/LICENSE-2.0
+ *  *
+ *  * Unless required by applicable law or agreed to in writing, software
+ *  * distributed under the License is distributed on an "AS IS" BASIS,
+ *  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  * See the License for the specific language governing permissions and
+ *  * limitations under the License.  For additional information regarding
+ *  * copyright in this work, please see the NOTICE file in the top level
+ *  * directory of this distribution.
+ *
+ */
+
+package org.apache.usergrid.rest.test.resource2point0.endpoints;
+
+import org.apache.usergrid.rest.test.resource2point0.model.ApiResponse;
+import org.apache.usergrid.rest.test.resource2point0.model.Organization;
+import org.apache.usergrid.rest.test.resource2point0.model.User;
+import org.apache.usergrid.rest.test.resource2point0.state.ClientContext;
+
+import javax.ws.rs.core.MediaType;
+import java.util.Map;
+
+/**
+ * Classy class class.
+ */
+public class UsersResource extends AbstractCollectionResource<User,UserResource> {
+
+    public UsersResource( ClientContext context, UrlResource parent) {
+        super("users", context, parent);
+    }
+
+    @Override
+    protected User instantiateT(ApiResponse response) {
+        return new User(response);
+    }
+
+    @Override
+    protected UserResource instantiateK(String name, ClientContext context, UrlResource parent) {
+        return new UserResource(name,context,parent);
+    }
+}

http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/ed25b255/stack/rest/src/test/java/org/apache/usergrid/rest/test/resource2point0/model/Application.java
----------------------------------------------------------------------
diff --git a/stack/rest/src/test/java/org/apache/usergrid/rest/test/resource2point0/model/Application.java b/stack/rest/src/test/java/org/apache/usergrid/rest/test/resource2point0/model/Application.java
index 16e4718..75566f2 100644
--- a/stack/rest/src/test/java/org/apache/usergrid/rest/test/resource2point0/model/Application.java
+++ b/stack/rest/src/test/java/org/apache/usergrid/rest/test/resource2point0/model/Application.java
@@ -21,6 +21,7 @@
 package org.apache.usergrid.rest.test.resource2point0.model;
 
 import java.util.List;
+import java.util.Map;
 
 /**
  * Classy class class.
@@ -33,10 +34,6 @@ public class Application extends Entity {
     }
 
     public Application(ApiResponse response){
-        if(response.getEntities() !=null &&  response.getEntities().size()>=1){
-            List<Entity>  entities =  response.getEntities();
-            Entity entity = entities.get(0);
-            this.putAll(entity.dynamic_properties);
-        }
+        super(response);
     }
 }

http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/ed25b255/stack/rest/src/test/java/org/apache/usergrid/rest/test/resource2point0/model/Entity.java
----------------------------------------------------------------------
diff --git a/stack/rest/src/test/java/org/apache/usergrid/rest/test/resource2point0/model/Entity.java b/stack/rest/src/test/java/org/apache/usergrid/rest/test/resource2point0/model/Entity.java
index 23d7f38..77218f3 100644
--- a/stack/rest/src/test/java/org/apache/usergrid/rest/test/resource2point0/model/Entity.java
+++ b/stack/rest/src/test/java/org/apache/usergrid/rest/test/resource2point0/model/Entity.java
@@ -51,12 +51,23 @@ public class Entity implements Serializable, Map<String,Object> {
 
     private CollectionResource targetResource;
 
+    public Entity(){}
+
+    public Entity(ApiResponse response){
+        if(response.getEntities() !=null &&  response.getEntities().size()>=1){
+            List<Entity>  entities =  response.getEntities();
+            Map<String,Object> entity = entities.get(0);
+            this.putAll(entity);
+        }
+    }
+
     /**
      * Performs deep copy on entity passed in and save over what we currently have
      */
     public void save(){
-        List<Entity> response = targetResource.put(this).getEntities();
-        Entity entity = response.get(0);
+        targetResource.put(this);
+        ApiResponse response = targetResource.entity(this.getName()).get();
+        Entity entity = new Entity(response);
         this.dynamic_properties.putAll(entity.getDynamicProperties());
     }