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/16 01:39:56 UTC

incubator-usergrid git commit: Added comment to management resource about where error codes could be changed. Cleaned up and fixed first two tests of organization resource. Added mapping specific user method to User class. This is a stop gap until we fig

Repository: incubator-usergrid
Updated Branches:
  refs/heads/UG-rest-test-framework-overhaul 792b3e7f0 -> 52d1458a6


Added comment to management resource about where error codes could be changed.
Cleaned up and fixed first two tests of organization resource.
Added mapping specific user method to User class. This is a stop gap until we figure out a better way to get the users back.


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

Branch: refs/heads/UG-rest-test-framework-overhaul
Commit: 52d1458a65b78eb87cb4bd032832009a67ba842c
Parents: 792b3e7
Author: grey <gr...@apigee.com>
Authored: Mon Dec 15 16:39:53 2014 -0800
Committer: grey <gr...@apigee.com>
Committed: Mon Dec 15 16:39:53 2014 -0800

----------------------------------------------------------------------
 .../rest/management/ManagementResource.java     |   2 +
 .../rest/management/OrganizationsIT.java        | 152 ++++++++++---------
 .../endpoints/mgmt/OrganizationResource.java    |   1 +
 .../rest/test/resource2point0/model/User.java   |   7 +-
 4 files changed, 89 insertions(+), 73 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/52d1458a/stack/rest/src/main/java/org/apache/usergrid/rest/management/ManagementResource.java
----------------------------------------------------------------------
diff --git a/stack/rest/src/main/java/org/apache/usergrid/rest/management/ManagementResource.java b/stack/rest/src/main/java/org/apache/usergrid/rest/management/ManagementResource.java
index d66d6a8..a5ff3e3 100644
--- a/stack/rest/src/main/java/org/apache/usergrid/rest/management/ManagementResource.java
+++ b/stack/rest/src/main/java/org/apache/usergrid/rest/management/ManagementResource.java
@@ -247,6 +247,8 @@ public class ManagementResource extends AbstractContextResource {
             }
 
             if ( user == null ) {
+                //TODO: this could be fixed to return the reason why a user is null. In some cases the USER is not found
+                //so a 404 would be more appropriate etc...
                 OAuthResponse response =
                         OAuthResponse.errorResponse( SC_BAD_REQUEST ).setError( OAuthError.TokenResponse.INVALID_GRANT )
                                      .setErrorDescription( errorDescription ).buildJSONMessage();

http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/52d1458a/stack/rest/src/test/java/org/apache/usergrid/rest/management/OrganizationsIT.java
----------------------------------------------------------------------
diff --git a/stack/rest/src/test/java/org/apache/usergrid/rest/management/OrganizationsIT.java b/stack/rest/src/test/java/org/apache/usergrid/rest/management/OrganizationsIT.java
index cff42d0..c9f9f11 100644
--- a/stack/rest/src/test/java/org/apache/usergrid/rest/management/OrganizationsIT.java
+++ b/stack/rest/src/test/java/org/apache/usergrid/rest/management/OrganizationsIT.java
@@ -33,7 +33,6 @@ import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 import org.apache.usergrid.persistence.EntityManager;
-import org.apache.usergrid.persistence.entities.User;
 import org.apache.usergrid.persistence.index.utils.UUIDUtils;
 import org.apache.usergrid.rest.TestContextSetup;
 import org.apache.usergrid.rest.management.organizations.OrganizationsResource;
@@ -41,6 +40,7 @@ import org.apache.usergrid.rest.test.resource2point0.AbstractRestIT;
 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.Token;
+import org.apache.usergrid.rest.test.resource2point0.model.User;
 
 import com.fasterxml.jackson.databind.JsonNode;
 import com.sun.jersey.api.client.ClientResponse;
@@ -53,6 +53,7 @@ import static junit.framework.Assert.fail;
 import static org.apache.usergrid.utils.MapUtils.hashMap;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
 import static org.junit.Assert.assertTrue;
 
 
@@ -68,7 +69,6 @@ public class OrganizationsIT extends AbstractRestIT {
 
     /**
      * Tests that a Organization and Owner can be created and that they persist properties and default permissions.
-     * @throws Exception
      */
 
     @Test
@@ -80,88 +80,96 @@ public class OrganizationsIT extends AbstractRestIT {
         String orgName = username;
         String email = username + "@usergrid.com";
 
-        Map<String, Object> organizationProperties = new HashMap<String, Object>();
-        organizationProperties.put( "securityLevel", 5 );
-        organizationProperties.put( "company", "Apigee" );
-
-
-        Organization organization = new Organization(orgName,username,email,name,password,organizationProperties);
         //TODO:seperate entity properties from organization properties.
-        //organization.addProperty( "" ).addProperty(  )
+        Map<String, Object> userProperties = new HashMap<String, Object>();
+        userProperties.put( "company", "Apigee" );
 
-        Organization orgOwner = clientSetup.getRestClient().management().orgs().post( organization );
+        //Create organization
+        Organization organization = new Organization( orgName, username, email, name, password, userProperties );
 
-        assertNotNull( orgOwner );
+        //Get back organization response
+        Organization organizationResponse = clientSetup.getRestClient().management().orgs().post( organization );
 
-       // orgOwner = clientSetup.getRestClient().management().orgs().get();
+        assertNotNull( organizationResponse );
 
-        Token token = new Token( "password",username,password );
-        Token tokenBack = clientSetup.getRestClient().management().token().post(token);
 
-        assertNotNull( tokenBack );
+        //Creates token
+        Token token =
+                clientSetup.getRestClient().management().token().post( new Token( "password", username, password ) );
+
+        assertNotNull( token );
 
         assertNotNull( clientSetup.getRestClient().getContext().getToken() );
 
-        Organization org = clientSetup.getRestClient().management().orgs().organization(orgName).get();
+        Organization returnedOrg = clientSetup.getRestClient().management().orgs().organization( orgName ).get();
+
+        //TODO: clean this up
+        User returnedUser =
+                new User().mapSpecificUserResponse( ( Map<String, Object> ) returnedOrg.get( "users" ), username );
+        assertTrue( returnedOrg != null && returnedOrg.getName().equals( orgName ) );
 
-        assertTrue(org != null && org.getName().equals(orgName));
+        assertEquals( "Apigee", returnedUser.getProperties().get( "company" ) );
     }
 
-//
-//    @Test
-//    public void testCreateDuplicateOrgName() throws Exception {
-//
-//        // create organization with name
-//        String timeuuid = UUIDUtils.newTimeUUID().toString();
-//        Map<String, String> payload =
-//                hashMap( "email", "create-duplicate-org" + timeuuid + "@mockserver.com" ).map( "password", "password" )
-//                                                                                         .map( "organization",
-//                                                                                                 "create-duplicate-orgname-org"
-//                                                                                                         + timeuuid );
-//        JsonNode node = mapper.readTree(
-//                resource().path( "/management/organizations" ).accept( MediaType.APPLICATION_JSON )
-//                          .type( MediaType.APPLICATION_JSON_TYPE ).post( String.class, payload ) );
-//
-//        logNode( node );
-//        assertNotNull( node );
-//
-//        refreshIndex( "create-duplicate-orgname-org" + timeuuid, "dummy" );
-//
-//        // create another org with that same name, but a different user
-//        payload = hashMap( "email", "create-duplicate-org2@mockserver.com" ).map( "username", "create-dupe-orgname2" )
-//                                                                            .map( "password", "password" )
-//                                                                            .map( "organization",
-//                                                                                    "create-duplicate-orgname-org"
-//                                                                                            + timeuuid );
-//        try {
-//            node = mapper.readTree( resource().path( "/management/organizations" ).accept( MediaType.APPLICATION_JSON )
-//                                              .type( MediaType.APPLICATION_JSON_TYPE ).post( String.class, payload ) );
-//        }
-//        catch ( Exception ex ) {
-//        }
-//
-//        refreshIndex( "create-duplicate-orgname-org" + timeuuid, "dummy" );
-//
-//        // now attempt to login as the user for the second organization
-//        payload = hashMap( "grant_type", "password" ).map( "username", "create-dupe-orgname2" )
-//                                                     .map( "password", "password" );
-//        try {
-//            node = mapper.readTree( resource().path( "/management/token" ).accept( MediaType.APPLICATION_JSON )
-//                                              .type( MediaType.APPLICATION_JSON_TYPE ).post( String.class, payload ) );
-//            fail( "Should not have created user" );
-//        }
-//        catch ( Exception ex ) {
-//        }
-//        logNode( node );
-//
-//        refreshIndex( "create-duplicate-orgname-org" + timeuuid, "dummy" );
-//
-//        payload = hashMap( "username", "create-duplicate-org@mockserver.com" ).map( "grant_type", "password" )
-//                                                                              .map( "password", "password" );
-//        node = mapper.readTree( resource().path( "/management/token" ).accept( MediaType.APPLICATION_JSON )
-//                                          .type( MediaType.APPLICATION_JSON_TYPE ).post( String.class, payload ) );
-//        logNode( node );
-//    }
+
+    /**
+     * Creates a organization with an owner, then attempts to create an organization with the same name ( making sure it
+     * fails) When it fails it verifies that the original is still intact.
+     * @throws Exception
+     */
+    @Test
+    public void testCreateDuplicateOrgName() throws Exception {
+
+        // create organization with name
+        String username = "testCreateDuplicateOrgName" + UUIDUtils.newTimeUUID();
+        String name = username;
+        String password = "password";
+        String orgName = username;
+        String email = username + "@usergrid.com";
+
+
+        //Create organization
+        Organization orgPayload = new Organization( orgName, username, email, name, password, null );
+
+        Organization orgCreatedResponse = clientSetup.getRestClient().management().orgs().post( orgPayload );
+
+
+        assertNotNull( orgCreatedResponse );
+
+        //TODO: Check to see if we still need to refresh the index.
+
+        Organization orgTestDuplicatePayload =
+                new Organization( orgName, username + "test", email + "test", name + "test", password, null );
+        Organization orgTestDuplicateResponse = null;
+        try {
+            orgTestDuplicateResponse = clientSetup.getRestClient().management().orgs().post( orgTestDuplicatePayload );
+        }
+        catch ( Exception ex ) {
+            //TODO: add a check here to make sure proper error message was returned
+        }
+
+        //refreshIndex( "create-duplicate-orgname-org" + timeuuid, "dummy" );
+
+        // now attempt to login as the user for the second organization
+
+        Token tokenPayload = new Token( "password", username + "test", password );
+        Token tokenError = null;
+        try {
+            tokenError = clientSetup.getRestClient().management().token().post( tokenPayload );
+            fail( "Should not have created user" );
+        }
+        catch ( UniformInterfaceException ex ) {
+            //TODO: Should throw a 404 not a 400.
+            System.out.println();
+        }
+
+        assertNull( tokenError );
+
+        tokenPayload = new Token( "password", username, password );
+        Token tokenReturned = clientSetup.getRestClient().management().token().post( tokenPayload );
+
+        assertNotNull( tokenReturned );
+    }
 //
 //
 //    @Test

http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/52d1458a/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
index 0b6de42..01cafcf 100644
--- 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
@@ -26,6 +26,7 @@ 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.model.Organization;
+import org.apache.usergrid.rest.test.resource2point0.model.User;
 import org.apache.usergrid.rest.test.resource2point0.state.ClientContext;
 
 

http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/52d1458a/stack/rest/src/test/java/org/apache/usergrid/rest/test/resource2point0/model/User.java
----------------------------------------------------------------------
diff --git a/stack/rest/src/test/java/org/apache/usergrid/rest/test/resource2point0/model/User.java b/stack/rest/src/test/java/org/apache/usergrid/rest/test/resource2point0/model/User.java
index 5f5fe6e..4445bff 100644
--- a/stack/rest/src/test/java/org/apache/usergrid/rest/test/resource2point0/model/User.java
+++ b/stack/rest/src/test/java/org/apache/usergrid/rest/test/resource2point0/model/User.java
@@ -31,6 +31,8 @@ import com.fasterxml.jackson.databind.JsonNode;
  */
 public class User extends Entity {
 
+
+    public User(){}
     /**
      * This could also be a user
      * @param response
@@ -88,6 +90,9 @@ public class User extends Entity {
         return UUID.fromString( (String) get("uuid") );
     }
 
-
+    public User mapSpecificUserResponse(Map<String,Object> map,String username){
+        putAll((Map<String, Object>) map.get(username));
+        return this;
+    }
 
 }