http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/3993f081/stack/rest/src/test/java/org/apache/usergrid/rest/applications/queries/MatrixQueryTests.java ---------------------------------------------------------------------- diff --git a/stack/rest/src/test/java/org/apache/usergrid/rest/applications/queries/MatrixQueryTests.java b/stack/rest/src/test/java/org/apache/usergrid/rest/applications/queries/MatrixQueryTests.java new file mode 100644 index 0000000..25bc33b --- /dev/null +++ b/stack/rest/src/test/java/org/apache/usergrid/rest/applications/queries/MatrixQueryTests.java @@ -0,0 +1,202 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * 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. + */ +package org.apache.usergrid.rest.applications.queries; + +import java.util.Map; +import java.util.UUID; +import org.apache.usergrid.rest.AbstractRestIT; +import org.apache.usergrid.rest.TestContextSetup; +import org.apache.usergrid.rest.test.resource.CustomCollection; +import org.apache.usergrid.rest.test.resource.app.UsersCollection; +import static org.apache.usergrid.utils.MapUtils.hashMap; +import org.junit.Rule; +import org.junit.Test; + + +public class MatrixQueryTests extends AbstractRestIT { + + @Rule + public TestContextSetup context = new TestContextSetup( this ); + + + @Test + public void simpleMatrix() throws Exception { + + /** + * Create 3 users which we will use for sub searching + */ + UsersCollection users = context.users(); + + Map user1 = + hashMap( "username", "user1" ).map( "email", "[email protected]" ).map( "fullname", "Bob Smith" ); + + users.create( user1 ); + + + Map user2 = + hashMap( "username", "user2" ).map( "email", "[email protected]" ).map( "fullname", "Fred Smith" ); + + users.create( user2 ); + + + Map user3 = hashMap( "username", "user3" ).map( "email", "[email protected]" ) + .map( "fullname", "Frank Grimes" ); + + users.create( user3 ); + + + //now create 4 restaurants + + CustomCollection restaurants = context.collection( "restaurants" ); + + + Map restaurant1 = hashMap( "name", "Old Major" ); + + UUID restaurant1Id = getEntityId( restaurants.create( restaurant1 ), 0 ); + + Map restaurant2 = hashMap( "name", "tag" ); + + UUID restaurant2Id = getEntityId( restaurants.create( restaurant2 ), 0 ); + + Map restaurant3 = hashMap( "name", "Squeaky Bean" ); + + UUID restaurant3Id = getEntityId( restaurants.create( restaurant3 ), 0 ); + + Map restaurant4 = hashMap( "name", "Lola" ); + + UUID restaurant4Id = getEntityId( restaurants.create( restaurant4 ), 0 ); + + + //now like our 3 users + + + //user 1 likes old major + users.user( "user1" ).connection( "likes" ).entity( restaurant1Id ).post(); + + users.user( "user1" ).connection( "likes" ).entity( restaurant2Id ).post(); + + + //user 2 likes tag and squeaky bean + users.user( "user2" ).connection( "likes" ).entity( restaurant2Id ).post(); + + users.user( "user2" ).connection( "likes" ).entity( restaurant3Id ).post(); + + //user 3 likes Lola (it shouldn't appear in the results) + + users.user( "user3" ).connection( "likes" ).entity( restaurant4Id ).post(); + + + //now query with matrix params + + +// JsonNode testGetUsers = context.collection( "users" ).get().get( "entities" ); +// +// JsonNode likesNode = +// context.collection( "users" ).entity( "user1" ).connection( "likes" ).get().get( "entities" ); +// +// +// JsonNode queryResponse = context.collection( "users" ).withMatrix( +// hashMap( "ql", "where fullname contains 'Smith'" ).map( "limit", "1000" ) ).connection( "likes" ).get(); +// +// assertEquals( "Old Major", getEntityName( queryResponse, 0 ) ); +// +// assertEquals( "tag", getEntityName( queryResponse, 1 ) ); +// +// assertEquals( "Squeaky Bean", getEntityName( queryResponse, 2 ) ); +// +// /** +// * No additional elements in the response +// */ +// assertNull( getEntity( queryResponse, 3 ) ); + } + + +// @Test +// public void largeRootElements() { +// +// +// // create 4 restaurants +// +// CustomCollection restaurants = context.collection( "restaurants" ); +// +// +// Map restaurant1 = hashMap( "name", "Old Major" ); +// +// UUID restaurant1Id = getEntityId( restaurants.create( restaurant1 ), 0 ); +// +// Map restaurant2 = hashMap( "name", "tag" ); +// +// UUID restaurant2Id = getEntityId( restaurants.create( restaurant2 ), 0 ); +// +// Map restaurant3 = hashMap( "name", "Squeaky Bean" ); +// +// UUID restaurant3Id = getEntityId( restaurants.create( restaurant3 ), 0 ); +// +// +// /** +// * Create 3 users which we will use for sub searching +// */ +// UsersCollection users = context.users(); +// +// +// int max = 1000; +// int count = ( int ) (max * 1.1); +// +// for ( int i = 0; i < count; i++ ) { +// +// String username = "user" + i; +// String email = username + "@usergrid.com"; +// +// Map user1 = hashMap( "username", username ).map( "email", email ).map( "fullname", i + " Smith" ); +// +// users.create( user1 ); +// +// /** +// * Change our links every other time. This way we should get all 3 +// */ +// +// if ( i % 2 == 0 ) { +// users.user( username ).connection( "likes" ).entity( restaurant1Id ).post(); +// +// users.user( username ).connection( "likes" ).entity( restaurant2Id ).post(); +// } +// else { +// +// users.user( username ).connection( "likes" ).entity( restaurant2Id ).post(); +// +// users.user( username ).connection( "likes" ).entity( restaurant3Id ).post(); +// } +// } +// +// +// +// //set our limit to 1k. We should get only 3 results, but this should run +// JsonNode queryResponse = context.collection( "users" ).withMatrix( +// hashMap( "ql", "where fullname contains 'Smith'" ).map( "limit", "1000" ) ).connection( "likes" ).get(); +// +// assertEquals( "Old Major", getEntityName( queryResponse, 0 ) ); +// +// assertEquals( "tag", getEntityName( queryResponse, 1 ) ); +// +// assertEquals( "Squeaky Bean", getEntityName( queryResponse, 2 ) ); +// +// /** +// * No additional elements in the response +// */ +// assertNull( getEntity( queryResponse, 3 ) ); +// } +}
http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/3993f081/stack/rest/src/test/java/org/apache/usergrid/rest/applications/queries/OrderByTest.java ---------------------------------------------------------------------- diff --git a/stack/rest/src/test/java/org/apache/usergrid/rest/applications/queries/OrderByTest.java b/stack/rest/src/test/java/org/apache/usergrid/rest/applications/queries/OrderByTest.java new file mode 100644 index 0000000..481f753 --- /dev/null +++ b/stack/rest/src/test/java/org/apache/usergrid/rest/applications/queries/OrderByTest.java @@ -0,0 +1,172 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * 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. + */ +package org.apache.usergrid.rest.applications.queries; + + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import com.fasterxml.jackson.databind.JsonNode; +import java.io.IOException; +import org.junit.Rule; +import org.junit.Test; +import org.apache.usergrid.rest.AbstractRestIT; +import org.apache.usergrid.rest.TestContextSetup; +import org.apache.usergrid.rest.test.resource.CustomCollection; + +import static org.junit.Assert.assertEquals; +import static org.apache.usergrid.utils.MapUtils.hashMap; + + +/** + * // TODO: Document this + * + * @author ApigeeCorporation + * @since 4.0 + */ +public class OrderByTest extends AbstractRestIT { + + @Rule + public TestContextSetup context = new TestContextSetup( this ); + + + @Test + // USERGRID-1400 + public void orderByShouldNotAffectResults() throws IOException { + + CustomCollection activities = context.collection( "activities" ); + + long created = 0; + Map actor = hashMap( "displayName", "Erin" ); + Map props = new HashMap(); + props.put( "actor", actor ); + props.put( "verb", "go" ); + props.put( "content", "bragh" ); + for ( int i = 0; i < 20; i++ ) { + props.put( "ordinal", i ); + JsonNode activity = activities.create( props ); + if ( i == 5 ) { + created = activity.findValue( "created" ).longValue(); + } + } + + refreshIndex(context.getOrgName(), context.getAppName()); + + String query = "select * where created > " + created; + JsonNode node = activities.withQuery( query ).get(); + assertEquals( 10, node.get( "entities" ).size() ); + + query = query + " order by created desc"; + node = activities.withQuery( query ).get(); + assertEquals( 10, node.get( "entities" ).size() ); + } + + + @Test + // USERGRID-1520 + public void orderByComesBeforeLimitResult() throws IOException { + + CustomCollection activities = context.collection( "activities" ); + + Map actor = hashMap( "displayName", "Erin" ); + Map props = new HashMap(); + int checkResultsNum = 0; + + props.put( "actor", actor ); + props.put( "verb", "go" ); + props.put( "content", "bragh" ); + + for ( int i = 0; i < 20; i++ ) { + props.put( "ordinal", i ); + JsonNode activity = activities.create( props ); + } + + refreshIndex(context.getOrgName(), context.getAppName()); + + String query = "select * where created > " + 1 + " order by created desc"; + + JsonNode incorrectNode = activities.withQuery( query ).withLimit( 5 ).get(); + + assertEquals( 5, incorrectNode.get( "entities" ).size() ); + + while ( checkResultsNum < 5 ) { + assertEquals( activities.entityIndex( query, checkResultsNum ), + activities.entityIndexLimit( query, 5, checkResultsNum ) ); + checkResultsNum++; + } + } + + /* + * public JsonNode entityIndex(JsonNode container, int index) { return + * container.get("entities").get(index); } + */ + + + @Test + // USERGRID-1521 + public void orderByReturnCorrectResults() throws IOException { + + CustomCollection activities = context.collection( "activities" ); + + int size = 200; + + Map<String, String> actor = hashMap( "displayName", "Erin" ); + Map<String, Object> props = new HashMap<String, Object>(); + + props.put( "actor", actor ); + props.put( "verb", "go" ); + props.put( "content", "bragh" ); + + List<JsonNode> activites = new ArrayList<JsonNode>( size ); + + for ( int i = 0; i < size; i++ ) { + props.put( "ordinal", i ); + JsonNode activity = activities.create( props ).get( "entities" ).get( 0 ); + activites.add( activity ); + } + + refreshIndex(context.getOrgName(), context.getAppName()); + + long lastCreated = activites.get( activites.size() - 1 ).get( "created" ).asLong(); + + String errorQuery = String.format( "select * where created <= %d order by created desc", lastCreated ); + String cursor = null; + int index = size - 1; + + do { + JsonNode response = activities.withQuery( errorQuery ).get(); + JsonNode cursorNode = response.get( "cursor" ); + + cursor = cursorNode != null ? cursorNode.asText() : null; + + JsonNode entities = response.get( "entities" ); + + int returnSize = entities.size(); + + for ( int i = 0; i < returnSize; i++, index-- ) { + assertEquals( activites.get( index ), entities.get( i ) ); + } + + activities = activities.withCursor( cursor ); + } + while ( cursor != null && cursor.length() > 0 ); + + assertEquals( "Paged to last result", -1, index ); + } +} http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/3993f081/stack/rest/src/test/java/org/apache/usergrid/rest/applications/users/ActivityResourceIT.java ---------------------------------------------------------------------- diff --git a/stack/rest/src/test/java/org/apache/usergrid/rest/applications/users/ActivityResourceIT.java b/stack/rest/src/test/java/org/apache/usergrid/rest/applications/users/ActivityResourceIT.java deleted file mode 100644 index 0a9b23d..0000000 --- a/stack/rest/src/test/java/org/apache/usergrid/rest/applications/users/ActivityResourceIT.java +++ /dev/null @@ -1,188 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * 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. - */ -package org.apache.usergrid.rest.applications.users; - - -import org.junit.Before; -import org.junit.Test; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.apache.usergrid.cassandra.Concurrent; -import org.apache.usergrid.java.client.Client.Query; -import org.apache.usergrid.java.client.entities.Entity; -import org.apache.usergrid.java.client.entities.User; -import org.apache.usergrid.java.client.response.ApiResponse; -import org.apache.usergrid.rest.AbstractRestIT; -import org.apache.usergrid.utils.UUIDUtils; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; - - -/** @author tnine */ -@Concurrent() -public class ActivityResourceIT extends AbstractRestIT { - private static Logger log = LoggerFactory.getLogger( ActivityResourceIT.class ); - - private static final String GROUP = "testGroup"; - - private static final String USER = "edanuff"; - - private static boolean groupCreated = false; - - - public ActivityResourceIT() throws Exception { - - } - - - @Before - public void setupGroup() { - if ( groupCreated ) { - return; - } - - client.createGroup( GROUP ); - - refreshIndex("test-organization", "test-app"); - - groupCreated = true; - } - - - @Test - public void postNullActivityToGroup() { - - boolean fail = false; - try { - ApiResponse groupActivity = client.postGroupActivity( GROUP, null ); - fail = (groupActivity.getError() != null); - - } - catch ( Exception e ) { - fail = true; - } - assertTrue( fail ); - } - - - @Test - public void postGroupActivity() { - - // don't populate the user, it will use the currently authenticated user. - - String activityTitle = "testTitle" + UUIDUtils.newTimeUUID(); - String activityDesc = "testActivity" + UUIDUtils.newTimeUUID(); - - client.postGroupActivity( GROUP, "POST", - activityTitle, activityDesc, "testCategory", null, null, null, null, null ); - - refreshIndex("test-organization", "test-app"); - - Query results = client.queryActivityFeedForGroup( GROUP ); - - ApiResponse response = results.getResponse(); - - Entity result = response.getEntities().get( 0 ); - - assertEquals( "POST", result.getProperties().get( "verb" ).asText() ); - assertEquals( activityTitle, result.getProperties().get( "title" ).asText() ); - assertEquals( activityDesc, result.getProperties().get( "content" ).asText() ); - - // now pull the activity directly, we should find it - - results = client.queryActivity(); - - response = results.getResponse(); - - result = response.getEntities().get( 0 ); - - assertEquals( "POST", result.getProperties().get( "verb" ).asText() ); - assertEquals( activityTitle, result.getProperties().get( "title" ).asText() ); - assertEquals( activityDesc, result.getProperties().get( "content" ).asText() ); - } - - - @Test - public void postUserActivity() { - - // don't populate the user, it will use the currently authenticated - // user. - - User current = client.getLoggedInUser(); - - String activityTitle = "testTitle" + UUIDUtils.newTimeUUID(); - String activityDesc = "testActivity" + UUIDUtils.newTimeUUID(); - - client.postUserActivity( "POST", activityTitle, activityDesc, "testCategory", current, null, null, null, null ); - - refreshIndex("test-organization", "test-app"); - - Query results = client.queryActivityFeedForUser( USER ); - - ApiResponse response = results.getResponse(); - - Entity result = response.getEntities().get( 0 ); - - assertEquals( "POST", result.getProperties().get( "verb" ).asText() ); - assertEquals( activityTitle, result.getProperties().get( "title" ).asText() ); - assertEquals( activityDesc, result.getProperties().get( "content" ).asText() ); - assertEquals( current.getUuid().toString(), result.getProperties().get( "actor" ).get( "uuid" ).asText() ); - - // now pull the activity directly, we should find it - - results = client.queryActivity(); - - response = results.getResponse(); - - result = response.getEntities().get( 0 ); - - assertEquals( "POST", result.getProperties().get( "verb" ).asText() ); - assertEquals( activityTitle, result.getProperties().get( "title" ).asText() ); - assertEquals( activityDesc, result.getProperties().get( "content" ).asText() ); - } - - - @Test - public void postActivity() { - - // don't populate the user, it will use the currently authenticated - // user. - - User current = client.getLoggedInUser(); - - String activityTitle = "testTitle" + UUIDUtils.newTimeUUID(); - String activityDesc = "testActivity" + UUIDUtils.newTimeUUID(); - - client.postActivity( "POST", activityTitle, activityDesc, "testCategory", current, null, null, null, null ); - - refreshIndex("test-organization", "test-app"); - - Query results = client.queryActivity(); - - ApiResponse response = results.getResponse(); - - Entity result = response.getEntities().get( 0 ); - - assertEquals( "POST", result.getProperties().get( "verb" ).asText() ); - assertEquals( activityTitle, result.getProperties().get( "title" ).asText() ); - assertEquals( activityDesc, result.getProperties().get( "content" ).asText() ); - - //ACTOR isn't coming back, why? - assertEquals( current.getUuid().toString(), result.getProperties().get( "actor" ).get( "uuid" ).asText() ); - } -} http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/3993f081/stack/rest/src/test/java/org/apache/usergrid/rest/applications/users/CollectionsResourceIT.java ---------------------------------------------------------------------- diff --git a/stack/rest/src/test/java/org/apache/usergrid/rest/applications/users/CollectionsResourceIT.java b/stack/rest/src/test/java/org/apache/usergrid/rest/applications/users/CollectionsResourceIT.java deleted file mode 100644 index 0027f2e..0000000 --- a/stack/rest/src/test/java/org/apache/usergrid/rest/applications/users/CollectionsResourceIT.java +++ /dev/null @@ -1,205 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * 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. - */ -package org.apache.usergrid.rest.applications.users; - - -import java.util.HashMap; -import java.util.Map; -import java.util.UUID; - -import javax.ws.rs.core.MediaType; - -import com.fasterxml.jackson.databind.JsonNode; -import org.junit.Assert; -import org.junit.Test; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.apache.usergrid.cassandra.Concurrent; -import org.apache.usergrid.rest.AbstractRestIT; -import org.apache.usergrid.utils.UUIDUtils; - -import com.sun.jersey.api.client.UniformInterfaceException; -import java.io.IOException; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertNull; -import static org.apache.usergrid.utils.MapUtils.hashMap; - - -/** - * @author zznate - * @author tnine - */ -@Concurrent() -public class CollectionsResourceIT extends AbstractRestIT { - - private static Logger log = LoggerFactory.getLogger( CollectionsResourceIT.class ); - - - @Test - public void postToBadPath() throws IOException { - Map<String, String> payload = hashMap( "name", "Austin" ).map( "state", "TX" ); - JsonNode node = null; - try { - node = mapper.readTree( resource().path( "/test-organization/test-organization/test-app/cities" ) - .queryParam( "access_token", access_token ).accept( MediaType.APPLICATION_JSON ) - .type( MediaType.APPLICATION_JSON_TYPE ).post( String.class, payload )); - } - catch ( UniformInterfaceException e ) { - assertEquals( "Should receive a 400 Not Found", 400, e.getResponse().getStatus() ); - } - } - - - @Test - public void postToEmptyCollection() throws IOException { - Map<String, String> payload = new HashMap<String, String>(); - - JsonNode node = mapper.readTree( resource().path( "/test-organization/test-app/cities" ).queryParam( "access_token", access_token ) - .accept( MediaType.APPLICATION_JSON ).type( MediaType.APPLICATION_JSON_TYPE ) - .post( String.class, payload )); - assertNull( getEntity( node, 0 ) ); - assertNull( node.get( "count" ) ); - } - - - /** - * emails with "me" in them are causing errors. Test we can post to a colleciton after creating a user with this - * email - * <p/> - * USERGRID-689 - */ - @Test - public void permissionWithMeInString() throws Exception { - // user is created get a token - createUser( "[email protected]", "[email protected]", "secret", "Sumeet Agarwal" ); - refreshIndex("test-organization", "test-app"); - - String token = userToken( "[email protected]", "secret" ); - - - //create a permission with the path "me" in it - Map<String, String> data = new HashMap<String, String>(); - - data.put( "permission", "get,post,put,delete:/users/[email protected]/**" ); - - String path = "/test-organization/test-app/users/[email protected]/permissions"; - JsonNode posted = mapper.readTree( resource().path( path ).queryParam( "access_token", token ).accept( MediaType.APPLICATION_JSON ) - .type( MediaType.APPLICATION_JSON_TYPE ).post( String.class, data )); - - - //now post data - data = new HashMap<String, String>(); - - data.put( "name", "profile-sumeet" ); - data.put( "firstname", "sumeet" ); - data.put( "lastname", "agarwal" ); - data.put( "mobile", "122" ); - - - posted = mapper.readTree( resource().path( "/test-organization/test-app/nestprofiles" ).queryParam( "access_token", token ) - .accept( MediaType.APPLICATION_JSON ).type( MediaType.APPLICATION_JSON_TYPE ) - .post( String.class, data )); - - refreshIndex("test-organization", "test-app"); - - JsonNode response = mapper.readTree( resource().path( "/test-organization/test-app/nestprofiles" ).queryParam( "access_token", token ) - .accept( MediaType.APPLICATION_JSON ).type( MediaType.APPLICATION_JSON_TYPE ) - .get( String.class )); - - assertNotNull( getEntity( response, 0 ) ); - assertNotNull( response.get( "count" ) ); - } - - - @Test - public void stringWithSpaces() throws IOException { - Map<String, String> payload = hashMap( "summaryOverview", "My Summary" ).map( "caltype", "personal" ); - - JsonNode node = mapper.readTree( resource().path( "/test-organization/test-app/calendarlists" ) - .queryParam( "access_token", access_token ).accept( MediaType.APPLICATION_JSON ) - .type( MediaType.APPLICATION_JSON_TYPE ).post( String.class, payload )); - - - UUID id = getEntityId( node, 0 ); - - //post a second entity - - - payload = hashMap( "summaryOverview", "Your Summary" ).map( "caltype", "personal" ); - - node = mapper.readTree( resource().path( "/test-organization/test-app/calendarlists" ).queryParam( "access_token", access_token ) - .accept( MediaType.APPLICATION_JSON ).type( MediaType.APPLICATION_JSON_TYPE ) - .post( String.class, payload )); - - - refreshIndex("test-organization", "test-app"); - - //query for the first entity - - String query = "summaryOverview = 'My Summary'"; - - - JsonNode queryResponse = mapper.readTree( resource().path( "/test-organization/test-app/calendarlists" ) - .queryParam( "access_token", access_token ).queryParam( "ql", query ) - .accept( MediaType.APPLICATION_JSON ).type( MediaType.APPLICATION_JSON_TYPE ).get( String.class )); - - - UUID returnedId = getEntityId( queryResponse, 0 ); - - assertEquals( id, returnedId ); - - assertEquals( 1, queryResponse.get( "entities" ).size() ); - } - - - /** - * Test to verify "name property returns twice in AppServices response" is fixed. - * https://apigeesc.atlassian.net/browse/USERGRID-2318 - */ - @Test - public void testNoDuplicateFields() throws Exception { - - { - // create an "app_user" object with name fred - Map<String, String> payload = hashMap( "type", "app_user" ).map( "name", "fred" ); - - JsonNode node = mapper.readTree( resource().path( "/test-organization/test-app/app_users" ) - .queryParam( "access_token", access_token ).accept( MediaType.APPLICATION_JSON ) - .type( MediaType.APPLICATION_JSON_TYPE ).post( String.class, payload )); - - String uuidString = node.get( "entities" ).get( 0 ).get( "uuid" ).asText(); - UUID entityId = UUIDUtils.tryGetUUID( uuidString ); - Assert.assertNotNull( entityId ); - } - - refreshIndex("test-organization", "test-app"); - - { - // check REST API response for duplicate name property - // have to look at raw response data, Jackson will remove dups - String s = resource().path( "/test-organization/test-app/app_users/fred" ) - .queryParam( "access_token", access_token ).accept( MediaType.APPLICATION_JSON ) - .type( MediaType.APPLICATION_JSON_TYPE ).get( String.class ); - - int firstFred = s.indexOf( "fred" ); - int secondFred = s.indexOf( "fred", firstFred + 4 ); - Assert.assertEquals( "Should not be more than one name property", -1, secondFred ); - } - } -} http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/3993f081/stack/rest/src/test/java/org/apache/usergrid/rest/applications/users/ConnectionResourceTest.java ---------------------------------------------------------------------- diff --git a/stack/rest/src/test/java/org/apache/usergrid/rest/applications/users/ConnectionResourceTest.java b/stack/rest/src/test/java/org/apache/usergrid/rest/applications/users/ConnectionResourceTest.java deleted file mode 100644 index 5ab5ccd..0000000 --- a/stack/rest/src/test/java/org/apache/usergrid/rest/applications/users/ConnectionResourceTest.java +++ /dev/null @@ -1,271 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * 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. - */ -package org.apache.usergrid.rest.applications.users; - - -import java.util.LinkedHashMap; -import java.util.Map; -import java.util.UUID; - -import javax.ws.rs.core.MediaType; - -import com.fasterxml.jackson.databind.JsonNode; -import org.junit.Rule; -import org.junit.Test; -import org.apache.usergrid.rest.AbstractRestIT; -import org.apache.usergrid.rest.TestContextSetup; -import org.apache.usergrid.rest.test.resource.CustomCollection; - -import com.sun.jersey.api.client.ClientResponse; -import com.sun.jersey.api.client.UniformInterfaceException; -import java.io.IOException; - -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; -import static org.apache.usergrid.utils.MapUtils.hashMap; - - -/** - * // TODO: Document this - * - * @author ApigeeCorporation - * @since 4.0 - */ -public class ConnectionResourceTest extends AbstractRestIT { - @Rule - public TestContextSetup context = new TestContextSetup( this ); - - - @Test - public void connectionsQueryTest() throws IOException { - - - CustomCollection activities = context.collection( "peeps" ); - - Map stuff = hashMap( "type", "chicken" ); - - activities.create( stuff ); - - - Map<String, Object> payload = new LinkedHashMap<String, Object>(); - payload.put( "username", "todd" ); - - Map<String, Object> objectOfDesire = new LinkedHashMap<String, Object>(); - objectOfDesire.put( "codingmunchies", "doritoes" ); - - resource().path( "/test-organization/test-app/users" ).queryParam( "access_token", access_token ) - .accept( MediaType.APPLICATION_JSON ).type( MediaType.APPLICATION_JSON_TYPE ) - .post( String.class, payload ); - - payload.put( "username", "scott" ); - - - resource().path( "/test-organization/test-app/users" ).queryParam( "access_token", access_token ) - .accept( MediaType.APPLICATION_JSON ).type( MediaType.APPLICATION_JSON_TYPE ) - .post( String.class, payload ); - /*finish setting up the two users */ - - - refreshIndex("test-organization", "test-app"); - - ClientResponse toddWant = resource().path( "/test-organization/test-app/users/todd/likes/peeps" ) - .queryParam( "access_token", access_token ).accept( MediaType.TEXT_HTML ) - .type( MediaType.APPLICATION_JSON_TYPE ).post( ClientResponse.class, objectOfDesire ); - - assertEquals( 200, toddWant.getStatus() ); - - refreshIndex("test-organization", "test-app"); - - JsonNode node = mapper.readTree( resource().path( "/test-organization/test-app/peeps" ).queryParam( "access_token", access_token ) - .accept( MediaType.APPLICATION_JSON ).type( MediaType.APPLICATION_JSON_TYPE ) - .get( String.class )); - - String uuid = node.get( "entities" ).get( 0 ).get( "uuid" ).textValue(); - - - try { - node = mapper.readTree( resource().path( "/test-organization/test-app/users/scott/likes/" + uuid ) - .queryParam( "access_token", access_token ).accept( MediaType.APPLICATION_JSON ) - .type( MediaType.APPLICATION_JSON_TYPE ).get( String.class )); - assert ( false ); - } - catch ( UniformInterfaceException uie ) { - assertEquals( 404, uie.getResponse().getClientResponseStatus().getStatusCode() ); - } - } - - - @Test - public void connectionsLoopbackTest() throws IOException { - - CustomCollection things = context.collection( "things" ); - - UUID thing1Id = getEntityId( things.create( hashMap( "name", "thing1" ) ), 0 ); - - UUID thing2Id = getEntityId( things.create( hashMap( "name", "thing2" ) ), 0 ); - - - refreshIndex(context.getOrgName(), context.getAppName()); - - //create the connection - things.entity( thing1Id ).connection( "likes" ).entity( thing2Id ).post(); - - - refreshIndex(context.getOrgName(), context.getAppName()); - - //test we have the "likes" in our connection meta data response - - JsonNode response = things.entity( "thing1" ).get(); - - String url = getEntity( response, 0 ).get( "metadata" ).get( "connections" ).get( "likes" ).asText(); - - - assertNotNull( "Connection url returned in entity", url ); - - //trim off the start / - url = url.substring( 1 ); - - - //now that we know the URl is correct, follow it - - response = context.collection( url ).get(); - - UUID returnedUUID = getEntityId( response, 0 ); - - assertEquals( thing2Id, returnedUUID ); - - - //now follow the loopback, which should be pointers to the other entity - - url = getEntity( response, 0 ).get( "metadata" ).get( "connecting" ).get( "likes" ).asText(); - - assertNotNull( "Incoming edge URL provited", url ); - - //trim off the start / - url = url.substring( 1 ); - - //now we should get thing1 from the loopback url - - response = context.collection( url ).get(); - - UUID returned = getEntityId( response, 0 ); - - assertEquals( "Should point to thing1 as an incoming entity connection", thing1Id, returned ); - } - - - @Test - public void connectionsUUIDTest() throws IOException { - - CustomCollection things = context.collection( "things" ); - - UUID thing1Id = getEntityId( things.create( hashMap( "name", "thing1" ) ), 0 ); - - UUID thing2Id = getEntityId( things.create( hashMap( "name", "thing2" ) ), 0 ); - - - refreshIndex(context.getOrgName(), context.getAppName()); - - //create the connection - things.entity( thing1Id ).connection( "likes" ).entity( thing2Id ).post(); - - - refreshIndex(context.getOrgName(), context.getAppName()); - - //test we have the "likes" in our connection meta data response - - JsonNode response = things.entity( "thing1" ).get(); - - String url = getEntity( response, 0 ).get( "metadata" ).get( "connections" ).get( "likes" ).asText(); - - - assertNotNull( "Connection url returned in entity", url ); - - //trim off the start / - url = url.substring( 1 ); - - - //now that we know the URl is correct, follow it - - response = context.collection( url ).get(); - - UUID returnedUUID = getEntityId( response, 0 ); - - assertEquals( thing2Id, returnedUUID ); - - //get on the collection works, now get it directly by uuid - - //now we should get thing1 from the loopback url - - response = things.entity( thing1Id ).connection( "likes" ).entity( thing2Id ).get(); - - UUID returned = getEntityId( response, 0 ); - - assertEquals( "Should point to thing2 as an entity connection", thing2Id, returned ); - } - - @Test //USERGRID-3011 - public void connectionsDeleteSecondEntityInConnectionTest() throws IOException { - - CustomCollection things = context.collection( "things" ); - - UUID thing1Id = getEntityId( things.create( hashMap( "name", "thing1" ) ), 0 ); - - UUID thing2Id = getEntityId( things.create( hashMap( "name", "thing2" ) ), 0 ); - - refreshIndex(context.getOrgName(), context.getAppName()); - - //create the connection - things.entity( thing1Id ).connection( "likes" ).entity( thing2Id ).post(); - - JsonNode response = things.entity( "thing2" ).delete(); - - refreshIndex(context.getOrgName(), context.getAppName()); - - JsonNode node = things.entity ( "thing2" ).get(); - - assertNull(node); - - } - - @Test //USERGRID-3011 - public void connectionsDeleteFirstEntityInConnectionTest() throws IOException { - - CustomCollection things = context.collection( "things" ); - - UUID thing1Id = getEntityId( things.create( hashMap( "name", "thing1" ) ), 0 ); - - UUID thing2Id = getEntityId( things.create( hashMap( "name", "thing2" ) ), 0 ); - - refreshIndex(context.getOrgName(), context.getAppName()); - - //create the connection - things.entity( thing1Id ).connection( "likes" ).entity( thing2Id ).post(); - - JsonNode response = things.entity( "thing1" ).delete(); - - refreshIndex(context.getOrgName(), context.getAppName()); - - JsonNode node = things.entity ( "thing1" ).get(); - - assertNull(node); - - } - - -} http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/3993f081/stack/rest/src/test/java/org/apache/usergrid/rest/applications/users/GroupResourceIT.java ---------------------------------------------------------------------- diff --git a/stack/rest/src/test/java/org/apache/usergrid/rest/applications/users/GroupResourceIT.java b/stack/rest/src/test/java/org/apache/usergrid/rest/applications/users/GroupResourceIT.java deleted file mode 100644 index 6278067..0000000 --- a/stack/rest/src/test/java/org/apache/usergrid/rest/applications/users/GroupResourceIT.java +++ /dev/null @@ -1,295 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * 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. - */ -package org.apache.usergrid.rest.applications.users; - - -import java.util.UUID; - -import javax.ws.rs.core.MediaType; - -import com.fasterxml.jackson.databind.JsonNode; -import java.io.IOException; -import org.junit.Before; -import org.junit.Test; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.apache.usergrid.cassandra.Concurrent; -import org.apache.usergrid.java.client.Client.Query; -import org.apache.usergrid.java.client.response.ApiResponse; -import org.apache.usergrid.rest.AbstractRestIT; -import org.apache.usergrid.utils.UUIDUtils; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertTrue; - - -/** @author tnine */ -@Concurrent() -public class GroupResourceIT extends AbstractRestIT { - private static Logger log = LoggerFactory.getLogger( GroupResourceIT.class ); - - private static final String GROUP = "testGroup"; - - private static final String USER = "edanuff"; - - private static boolean groupCreated = false; - - - public GroupResourceIT() throws Exception { - - } - - - @Before - public void setupGroup() { - if ( groupCreated ) { - return; - } - - try { - client.createGroup( GROUP ); - groupCreated = true; - } - catch ( Exception e ) { - log.error( "Error creating group " + GROUP, e ); - } - refreshIndex("test-organization", "test-app"); - - } - - - @Test - public void failGroupNameValidation() { - - ApiResponse response = client.createGroup( "groupName/withslash" ); - assertNull( response.getError() ); - - refreshIndex("test-organization", "test-app"); - - { - boolean failed = false; - try { - ApiResponse groupResponse = client.createGroup( "groupName withspace" ); - failed = groupResponse.getError() != null; - } catch ( Exception e ) { - failed = true; - } - assertTrue( failed ); - } - } - - - @Test - public void postGroupActivity() { - - // don't populate the user, it will use the currently authenticated - // user. - - UUID id = UUIDUtils.newTimeUUID(); - - String groupPath = "groupPath" + id; - String groupTitle = "groupTitle " + id; - String groupName = "groupName" + id; - - ApiResponse response = client.createGroup( groupPath, groupTitle, groupName ); - - assertNull( "Error was: " + response.getErrorDescription(), response.getError() ); - - refreshIndex("test-organization", "test-app"); - - UUID newId = response.getEntities().get( 0 ).getUuid(); - - Query results = client.queryGroups( String.format( "name='%s'", groupName ) ); - - response = results.getResponse(); - - UUID entityId = response.getEntities().get( 0 ).getUuid(); - - assertEquals( newId, entityId ); - - results = client.queryGroups( String.format( "title='%s'", groupTitle ) ); - - response = results.getResponse(); - - entityId = response.getEntities().get( 0 ).getUuid(); - - assertEquals( newId, entityId ); - - results = client.queryGroups( String.format( "title contains '%s'", id ) ); - - response = results.getResponse(); - - entityId = response.getEntities().get( 0 ).getUuid(); - - assertEquals( newId, entityId ); - - results = client.queryGroups( String.format( "path='%s'", groupPath ) ); - - response = results.getResponse(); - - entityId = response.getEntities().get( 0 ).getUuid(); - - assertEquals( newId, entityId ); - } - - - @Test - public void addRemovePermission() throws IOException { - - UUID id = UUIDUtils.newTimeUUID(); - - String groupName = "groupname" + id; - - ApiResponse response = client.createGroup( groupName ); - assertNull( "Error was: " + response.getErrorDescription(), response.getError() ); - - refreshIndex("test-organization", "test-app"); - - UUID createdId = response.getEntities().get( 0 ).getUuid(); - - // add Permission - - String json = "{\"permission\":\"delete:/test\"}"; - JsonNode node = mapper.readTree( resource().path( "/test-organization/test-app/groups/" + createdId + "/permissions" ) - .queryParam( "access_token", access_token ).accept( MediaType.APPLICATION_JSON ) - .type( MediaType.APPLICATION_JSON_TYPE ).post( String.class, json )); - - // check it - assertNull( node.get( "errors" ) ); - assertEquals( node.get( "data" ).get( 0 ).asText(), "delete:/test" ); - - refreshIndex("test-organization", "test-app"); - - node = mapper.readTree( resource().path( "/test-organization/test-app/groups/" + createdId + "/permissions" ) - .queryParam( "access_token", access_token ).accept( MediaType.APPLICATION_JSON ) - .type( MediaType.APPLICATION_JSON_TYPE ).get( String.class )); - assertNull( node.get( "errors" ) ); - assertEquals( node.get( "data" ).get( 0 ).asText(), "delete:/test" ); - - - // remove Permission - - node = mapper.readTree( resource().path( "/test-organization/test-app/groups/" + createdId + "/permissions" ) - .queryParam( "access_token", access_token ).queryParam( "permission", "delete%3A%2Ftest" ) - .accept( MediaType.APPLICATION_JSON ).type( MediaType.APPLICATION_JSON_TYPE ).delete( String.class )); - - // check it - assertNull( node.get( "errors" ) ); - assertTrue( node.get( "data" ).size() == 0 ); - - refreshIndex("test-organization", "test-app"); - - node = mapper.readTree( resource().path( "/test-organization/test-app/groups/" + createdId + "/permissions" ) - .queryParam( "access_token", access_token ).accept( MediaType.APPLICATION_JSON ) - .type( MediaType.APPLICATION_JSON_TYPE ).get( String.class )); - assertNull( node.get( "errors" ) ); - assertTrue( node.get( "data" ).size() == 0 ); - } - - - @Test - public void addRemoveRole() throws IOException { - - UUID id = UUIDUtils.newTimeUUID(); - - String groupName = "groupname" + id; - String roleName = "rolename" + id; - - ApiResponse response = client.createGroup( groupName ); - assertNull( "Error was: " + response.getErrorDescription(), response.getError() ); - - UUID createdId = response.getEntities().get( 0 ).getUuid(); - - refreshIndex("test-organization", "test-app"); - - // create Role - - String json = "{\"title\":\"" + roleName + "\",\"name\":\"" + roleName + "\"}"; - JsonNode node = mapper.readTree( resource().path( "/test-organization/test-app/roles" ).queryParam( "access_token", access_token ) - .accept( MediaType.APPLICATION_JSON ).type( MediaType.APPLICATION_JSON_TYPE ) - .post( String.class, json )); - - // check it - assertNull( node.get( "errors" ) ); - - - refreshIndex("test-organization", "test-app"); - - // add Role - - node = mapper.readTree( resource().path( "/test-organization/test-app/groups/" + createdId + "/roles/" + roleName ) - .queryParam( "access_token", access_token ).accept( MediaType.APPLICATION_JSON ) - .type( MediaType.APPLICATION_JSON_TYPE ).post( String.class )); - - refreshIndex("test-organization", "test-app"); - - // check it - assertNull( node.get( "errors" ) ); - assertEquals( node.get( "entities" ).get( 0 ).get( "name" ).asText(), roleName ); - - node = mapper.readTree( resource().path( "/test-organization/test-app/groups/" + createdId + "/roles" ) - .queryParam( "access_token", access_token ).accept( MediaType.APPLICATION_JSON ) - .type( MediaType.APPLICATION_JSON_TYPE ).get( String.class )); - assertNull( node.get( "errors" ) ); - assertEquals( node.get( "entities" ).get( 0 ).get( "name" ).asText(), roleName ); - - // check root roles - node = mapper.readTree( resource().path( "/test-organization/test-app/roles" ).queryParam( "access_token", access_token ) - .accept( MediaType.APPLICATION_JSON ).type( MediaType.APPLICATION_JSON_TYPE ).get( String.class )); - assertNull( node.get( "errors" ) ); - assertTrue( node.get( "entities" ).findValuesAsText( "name" ).contains( roleName ) ); - - refreshIndex("test-organization", "test-app"); - - // remove Role - - node = mapper.readTree( resource().path( "/test-organization/test-app/groups/" + createdId + "/roles/" + roleName ) - .queryParam( "access_token", access_token ).accept( MediaType.APPLICATION_JSON ) - .type( MediaType.APPLICATION_JSON_TYPE ).delete( String.class )); - assertNull( node.get( "errors" ) ); - - refreshIndex("test-organization", "test-app"); - - node = mapper.readTree( resource().path( "/test-organization/test-app/groups/" + createdId + "/roles" ) - .queryParam( "access_token", access_token ).accept( MediaType.APPLICATION_JSON ) - .type( MediaType.APPLICATION_JSON_TYPE ).get( String.class )); - assertNull( node.get( "errors" ) ); - assertTrue( node.get( "entities" ).size() == 0 ); - - // check root roles - role should remain - node = mapper.readTree( resource().path( "/test-organization/test-app/roles" ).queryParam( "access_token", access_token ) - .accept( MediaType.APPLICATION_JSON ).type( MediaType.APPLICATION_JSON_TYPE ).get( String.class )); - assertNull( node.get( "errors" ) ); - assertTrue( node.get( "entities" ).findValuesAsText( "name" ).contains( roleName ) ); - - // now kill the root role - node = mapper.readTree( resource().path( "/test-organization/test-app/roles/" + roleName ) - .queryParam( "access_token", access_token ).accept( MediaType.APPLICATION_JSON ) - .type( MediaType.APPLICATION_JSON_TYPE ).delete( String.class )); - assertNull( node.get( "errors" ) ); - - refreshIndex("test-organization", "test-app"); - - // now it should be gone - node = mapper.readTree( resource().path( "/test-organization/test-app/roles" ).queryParam( "access_token", access_token ) - .accept( MediaType.APPLICATION_JSON ).type( MediaType.APPLICATION_JSON_TYPE ).get( String.class )); - assertNull( node.get( "errors" ) ); - assertFalse( node.get( "entities" ).findValuesAsText( "name" ).contains( roleName ) ); - } -} http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/3993f081/stack/rest/src/test/java/org/apache/usergrid/rest/applications/users/MatrixQueryTests.java ---------------------------------------------------------------------- diff --git a/stack/rest/src/test/java/org/apache/usergrid/rest/applications/users/MatrixQueryTests.java b/stack/rest/src/test/java/org/apache/usergrid/rest/applications/users/MatrixQueryTests.java deleted file mode 100644 index 475c8fb..0000000 --- a/stack/rest/src/test/java/org/apache/usergrid/rest/applications/users/MatrixQueryTests.java +++ /dev/null @@ -1,202 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * 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. - */ -package org.apache.usergrid.rest.applications.users; - -import java.util.Map; -import java.util.UUID; -import org.apache.usergrid.rest.AbstractRestIT; -import org.apache.usergrid.rest.TestContextSetup; -import org.apache.usergrid.rest.test.resource.CustomCollection; -import org.apache.usergrid.rest.test.resource.app.UsersCollection; -import static org.apache.usergrid.utils.MapUtils.hashMap; -import org.junit.Rule; -import org.junit.Test; - - -public class MatrixQueryTests extends AbstractRestIT { - - @Rule - public TestContextSetup context = new TestContextSetup( this ); - - - @Test - public void simpleMatrix() throws Exception { - - /** - * Create 3 users which we will use for sub searching - */ - UsersCollection users = context.users(); - - Map user1 = - hashMap( "username", "user1" ).map( "email", "[email protected]" ).map( "fullname", "Bob Smith" ); - - users.create( user1 ); - - - Map user2 = - hashMap( "username", "user2" ).map( "email", "[email protected]" ).map( "fullname", "Fred Smith" ); - - users.create( user2 ); - - - Map user3 = hashMap( "username", "user3" ).map( "email", "[email protected]" ) - .map( "fullname", "Frank Grimes" ); - - users.create( user3 ); - - - //now create 4 restaurants - - CustomCollection restaurants = context.collection( "restaurants" ); - - - Map restaurant1 = hashMap( "name", "Old Major" ); - - UUID restaurant1Id = getEntityId( restaurants.create( restaurant1 ), 0 ); - - Map restaurant2 = hashMap( "name", "tag" ); - - UUID restaurant2Id = getEntityId( restaurants.create( restaurant2 ), 0 ); - - Map restaurant3 = hashMap( "name", "Squeaky Bean" ); - - UUID restaurant3Id = getEntityId( restaurants.create( restaurant3 ), 0 ); - - Map restaurant4 = hashMap( "name", "Lola" ); - - UUID restaurant4Id = getEntityId( restaurants.create( restaurant4 ), 0 ); - - - //now like our 3 users - - - //user 1 likes old major - users.user( "user1" ).connection( "likes" ).entity( restaurant1Id ).post(); - - users.user( "user1" ).connection( "likes" ).entity( restaurant2Id ).post(); - - - //user 2 likes tag and squeaky bean - users.user( "user2" ).connection( "likes" ).entity( restaurant2Id ).post(); - - users.user( "user2" ).connection( "likes" ).entity( restaurant3Id ).post(); - - //user 3 likes Lola (it shouldn't appear in the results) - - users.user( "user3" ).connection( "likes" ).entity( restaurant4Id ).post(); - - - //now query with matrix params - - -// JsonNode testGetUsers = context.collection( "users" ).get().get( "entities" ); -// -// JsonNode likesNode = -// context.collection( "users" ).entity( "user1" ).connection( "likes" ).get().get( "entities" ); -// -// -// JsonNode queryResponse = context.collection( "users" ).withMatrix( -// hashMap( "ql", "where fullname contains 'Smith'" ).map( "limit", "1000" ) ).connection( "likes" ).get(); -// -// assertEquals( "Old Major", getEntityName( queryResponse, 0 ) ); -// -// assertEquals( "tag", getEntityName( queryResponse, 1 ) ); -// -// assertEquals( "Squeaky Bean", getEntityName( queryResponse, 2 ) ); -// -// /** -// * No additional elements in the response -// */ -// assertNull( getEntity( queryResponse, 3 ) ); - } - - -// @Test -// public void largeRootElements() { -// -// -// // create 4 restaurants -// -// CustomCollection restaurants = context.collection( "restaurants" ); -// -// -// Map restaurant1 = hashMap( "name", "Old Major" ); -// -// UUID restaurant1Id = getEntityId( restaurants.create( restaurant1 ), 0 ); -// -// Map restaurant2 = hashMap( "name", "tag" ); -// -// UUID restaurant2Id = getEntityId( restaurants.create( restaurant2 ), 0 ); -// -// Map restaurant3 = hashMap( "name", "Squeaky Bean" ); -// -// UUID restaurant3Id = getEntityId( restaurants.create( restaurant3 ), 0 ); -// -// -// /** -// * Create 3 users which we will use for sub searching -// */ -// UsersCollection users = context.users(); -// -// -// int max = 1000; -// int count = ( int ) (max * 1.1); -// -// for ( int i = 0; i < count; i++ ) { -// -// String username = "user" + i; -// String email = username + "@usergrid.com"; -// -// Map user1 = hashMap( "username", username ).map( "email", email ).map( "fullname", i + " Smith" ); -// -// users.create( user1 ); -// -// /** -// * Change our links every other time. This way we should get all 3 -// */ -// -// if ( i % 2 == 0 ) { -// users.user( username ).connection( "likes" ).entity( restaurant1Id ).post(); -// -// users.user( username ).connection( "likes" ).entity( restaurant2Id ).post(); -// } -// else { -// -// users.user( username ).connection( "likes" ).entity( restaurant2Id ).post(); -// -// users.user( username ).connection( "likes" ).entity( restaurant3Id ).post(); -// } -// } -// -// -// -// //set our limit to 1k. We should get only 3 results, but this should run -// JsonNode queryResponse = context.collection( "users" ).withMatrix( -// hashMap( "ql", "where fullname contains 'Smith'" ).map( "limit", "1000" ) ).connection( "likes" ).get(); -// -// assertEquals( "Old Major", getEntityName( queryResponse, 0 ) ); -// -// assertEquals( "tag", getEntityName( queryResponse, 1 ) ); -// -// assertEquals( "Squeaky Bean", getEntityName( queryResponse, 2 ) ); -// -// /** -// * No additional elements in the response -// */ -// assertNull( getEntity( queryResponse, 3 ) ); -// } -} http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/3993f081/stack/rest/src/test/java/org/apache/usergrid/rest/applications/users/OwnershipResourceIT.java ---------------------------------------------------------------------- diff --git a/stack/rest/src/test/java/org/apache/usergrid/rest/applications/users/OwnershipResourceIT.java b/stack/rest/src/test/java/org/apache/usergrid/rest/applications/users/OwnershipResourceIT.java deleted file mode 100644 index d11f473..0000000 --- a/stack/rest/src/test/java/org/apache/usergrid/rest/applications/users/OwnershipResourceIT.java +++ /dev/null @@ -1,379 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * 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. - */ -package org.apache.usergrid.rest.applications.users; - - -import com.fasterxml.jackson.databind.JsonNode; -import java.io.IOException; -import org.junit.Rule; -import org.junit.Test; -import org.apache.usergrid.cassandra.Concurrent; -import org.apache.usergrid.rest.AbstractRestIT; -import org.apache.usergrid.rest.TestContextSetup; -import org.apache.usergrid.rest.test.resource.Connection; -import org.apache.usergrid.rest.test.resource.CustomCollection; -import org.apache.usergrid.rest.test.resource.app.queue.DevicesCollection; -import org.apache.usergrid.rest.test.security.TestAppUser; -import org.apache.usergrid.rest.test.security.TestUser; -import org.apache.usergrid.utils.MapUtils; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; - - -/** - * - */ -@Concurrent() -public class OwnershipResourceIT extends AbstractRestIT { - - @Rule - public TestContextSetup context = new TestContextSetup( this ); - - - @Test - public void meVerify() throws Exception { - - context.clearUser(); - - String email = "[email protected]"; - TestUser user1 = new TestAppUser( email, "password", email ).create( context ); - refreshIndex(context.getOrgName(), context.getAppName()); - user1.login( context ).makeActive( context ); - - refreshIndex(context.getOrgName(), context.getAppName()); - - String token = user1.getToken(); - JsonNode userNode = context.application().users().user( "me" ).get(); - assertNotNull( userNode ); - - String uuid = userNode.get( "entities" ).get( 0 ).get( "uuid" ).textValue(); - assertNotNull( uuid ); - - setup.getMgmtSvc().revokeAccessTokenForAppUser( token ); - - refreshIndex(context.getOrgName(), context.getAppName()); - - try { - context.application().users().user( "me" ).get(); - fail(); - } - catch ( Exception ex ) { - ex.printStackTrace(); - assertTrue( ex.getMessage().contains( "401" ) ); - } - } - - - @Test - public void contextualPathOwnership() throws IOException { - - // anonymous user - context.clearUser(); - - refreshIndex(context.getOrgName(), context.getAppName()); - - TestUser user1 = new TestAppUser( "[email protected]", "password", "[email protected]" ).create( context ); - refreshIndex(context.getOrgName(), context.getAppName()); - user1.login( context ); - user1.makeActive( context ); - - refreshIndex(context.getOrgName(), context.getAppName()); - - // create device 1 on user1 devices - context.application().users().user( "me" ).devices() - .create( MapUtils.hashMap( "name", "device1" ).map( "number", "5551112222" ) ); - - // anonymous user - context.clearUser(); - refreshIndex(context.getOrgName(), context.getAppName()); - - // create device 2 on user 2 - TestUser user2 = new TestAppUser( "[email protected]", "password", "[email protected]" ).create( context ); - refreshIndex(context.getOrgName(), context.getAppName()); - user2.login( context ); - user2.makeActive( context ); - - refreshIndex(context.getOrgName(), context.getAppName()); - - context.application().users().user( "me" ).devices() - .create( MapUtils.hashMap( "name", "device2" ).map( "number", "5552223333" ) ); - - refreshIndex(context.getOrgName(), context.getAppName()); - - // now query on user 1. - - DevicesCollection devices = context.withUser( user1 ).application().users().user( "me" ).devices(); - - JsonNode data = devices.device( "device1" ).get(); - assertNotNull( data ); - assertEquals( "device1", getEntity( data, 0 ).get( "name" ).asText() ); - - // check we can't see device2 - data = devices.device( "device2" ).get(); - assertNull( data ); - - // do a collection load, make sure we're not loading device 2 - data = devices.get(); - - assertEquals( "device1", getEntity( data, 0 ).get( "name" ).asText() ); - assertNull( getEntity( data, 1 ) ); - - // log in as user 2 and check it - devices = context.withUser( user2 ).application().users().user( "me" ).devices(); - - data = devices.device( "device2" ).get(); - assertNotNull( data ); - assertEquals( "device2", getEntity( data, 0 ).get( "name" ).asText() ); - - // check we can't see device1 - data = devices.device( "device1" ).get(); - assertNull( data ); - - // do a collection load, make sure we're not loading device 1 - data = devices.get(); - - assertEquals( "device2", getEntity( data, 0 ).get( "name" ).asText() ); - assertNull( getEntity( data, 1 ) ); - - // we should see both devices when loaded from the root application - - // test for user 1 - - devices = context.withUser( user1 ).application().devices(); - data = devices.device( "device1" ).get(); - - assertNotNull( data ); - assertEquals( "device1", getEntity( data, 0 ).get( "name" ).asText() ); - - data = devices.device( "device2" ).get(); - - assertNotNull( data ); - assertEquals( "device2", getEntity( data, 0 ).get( "name" ).asText() ); - - // test for user 2 - data = context.withUser( user2 ).application().devices().device( "device1" ).get(); - - assertNotNull( data ); - assertEquals( "device1", getEntity( data, 0 ).get( "name" ).asText() ); - - data = devices.device( "device2" ).get(); - - assertNotNull( data ); - assertEquals( "device2", getEntity( data, 0 ).get( "name" ).asText() ); - } - - - @Test - public void contextualConnectionOwnership() throws IOException { - - // anonymous user - context.clearUser(); - - refreshIndex(context.getOrgName(), context.getAppName()); - - String email = "[email protected]"; - TestUser user1 = new TestAppUser( email, "password", email ).create( context ); - refreshIndex(context.getOrgName(), context.getAppName()); - user1.login( context ).makeActive( context ); - - // create a 4peaks restaurant - JsonNode data = context.application() - .collection( "restaurants" ).create( MapUtils.hashMap( "name", "4peaks" ) ); - - refreshIndex(context.getOrgName(), context.getAppName()); - - // create our connection - data = context.application().users().user( "me" ) - .connection( "likes" ).collection( "restaurants" ).entity( "4peaks" ).post(); - - refreshIndex(context.getOrgName(), context.getAppName()); - - String peaksId = getEntity( data, 0 ).get( "uuid" ).asText(); - - // anonymous user - context.clearUser(); - - // create a restaurant and link it to user 2 - email = "[email protected]"; - TestUser user2 = new TestAppUser( email, "password", email ).create( context ); - refreshIndex(context.getOrgName(), context.getAppName()); - - user2.login( context ).makeActive( context ); - refreshIndex(context.getOrgName(), context.getAppName()); - - data = context.application().collection( "restaurants" ) - .create( MapUtils.hashMap( "name", "arrogantbutcher" ) ); - refreshIndex(context.getOrgName(), context.getAppName()); - - data = context.application().users().user( "me" ).connection( "likes" ).collection( "restaurants" ) - .entity( "arrogantbutcher" ).post(); - refreshIndex(context.getOrgName(), context.getAppName()); - - String arrogantButcherId = getEntity( data, 0 ).get( "uuid" ).asText(); - - // now query on user 1. - - CustomCollection likeRestaurants = - context.withUser( user1 ).application().users().user( "me" ).connection( "likes" ) - .collection( "restaurants" ); - refreshIndex(context.getOrgName(), context.getAppName()); - - // check we can get it via id - data = likeRestaurants.entity( peaksId ).get(); - assertNotNull( data ); - assertEquals( "4peaks", getEntity( data, 0 ).get( "name" ).asText() ); - - // check we can get it by name - data = likeRestaurants.entity( "4peaks" ).get(); - assertNotNull( data ); - assertEquals( "4peaks", getEntity( data, 0 ).get( "name" ).asText() ); - - // check we can't see arrogantbutcher by name or id - data = likeRestaurants.entity( "arrogantbutcher" ).get(); - assertNull( data ); - - data = likeRestaurants.entity( arrogantButcherId ).get(); - assertNull( data ); - - // do a collection load, make sure we're not entities we shouldn't see - data = likeRestaurants.get(); - - assertEquals( "4peaks", getEntity( data, 0 ).get( "name" ).asText() ); - assertNull( getEntity( data, 1 ) ); - - // log in as user 2 and check it - likeRestaurants = context.withUser( user2 ).application().users().user( "me" ).connection( "likes" ) - .collection( "restaurants" ); - - data = likeRestaurants.entity( arrogantButcherId ).get(); - assertNotNull( data ); - assertEquals( "arrogantbutcher", getEntity( data, 0 ).get( "name" ).asText() ); - - data = likeRestaurants.entity( "arrogantbutcher" ).get(); - assertNotNull( data ); - assertEquals( "arrogantbutcher", getEntity( data, 0 ).get( "name" ).asText() ); - - // check we can't see 4peaks - data = likeRestaurants.entity( "4peaks" ).get(); - assertNull( data ); - - data = likeRestaurants.entity( peaksId ).get(); - assertNull( data ); - - // do a collection load, make sure we're not loading device 1 - data = likeRestaurants.get(); - - assertEquals( "arrogantbutcher", getEntity( data, 0 ).get( "name" ).asText() ); - assertNull( getEntity( data, 1 ) ); - - // we should see both devices when loaded from the root application - - // test for user 1 - - CustomCollection restaurants = context.withUser( user1 ).application().collection( "restaurants" ); - data = restaurants.entity( "4peaks" ).get(); - - assertNotNull( data ); - assertEquals( "4peaks", getEntity( data, 0 ).get( "name" ).asText() ); - - data = restaurants.entity( "arrogantbutcher" ).get(); - - assertNotNull( data ); - assertEquals( "arrogantbutcher", getEntity( data, 0 ).get( "name" ).asText() ); - - // test for user 2 - restaurants = context.withUser( user1 ).application().collection( "restaurants" ); - data = restaurants.entity( "4peaks" ).get(); - - assertNotNull( data ); - assertEquals( "4peaks", getEntity( data, 0 ).get( "name" ).asText() ); - - data = restaurants.entity( "arrogantbutcher" ).get(); - - assertNotNull( data ); - assertEquals( "arrogantbutcher", getEntity( data, 0 ).get( "name" ).asText() ); - } - - - @Test - public void contextualConnectionOwnershipGuestAccess() throws IOException { - - //set up full GET,PUT,POST,DELETE access for guests - context.application().collection( "roles" ).entity( "guest" ).collection( "permissions" ) - .create( MapUtils.hashMap( "permission", "get,put,post,delete:/**" ) ); - - - // anonymous user - context.clearUser(); - - - JsonNode city = context.application().collection( "cities" ).create( MapUtils.hashMap( "name", "tempe" ) ); - - refreshIndex(context.getOrgName(), context.getAppName()); - - String cityId = getEntity( city, 0 ).get( "uuid" ).asText(); - - // create a 4peaks restaurant - JsonNode data = context.application().collection( "cities" ).entity( "tempe" ).connection( "likes" ) - .collection( "restaurants" ).create( MapUtils.hashMap( "name", "4peaks" ) ); - - String peaksId = getEntity( data, 0 ).get( "uuid" ).asText(); - - data = context.application().collection( "cities" ).entity( "tempe" ).connection( "likes" ) - .collection( "restaurants" ).create( MapUtils.hashMap( "name", "arrogantbutcher" ) ); - - String arrogantButcherId = getEntity( data, 0 ).get( "uuid" ).asText(); - - // now query on user 1. - - Connection likeRestaurants = - context.application().collection( "cities" ).entity( "tempe" ).connection( "likes" ); - - refreshIndex(context.getOrgName(), context.getAppName()); - - // check we can get it via id with no collection name - data = likeRestaurants.entity( peaksId ).get(); - assertNotNull( data ); - assertEquals( "4peaks", getEntity( data, 0 ).get( "name" ).asText() ); - - data = likeRestaurants.entity( arrogantButcherId ).get(); - assertEquals( "arrogantbutcher", getEntity( data, 0 ).get( "name" ).asText() ); - - // check we can get it via id with a collection name - data = likeRestaurants.collection( "restaurants" ).entity( peaksId ).get(); - assertNotNull( data ); - assertEquals( "4peaks", getEntity( data, 0 ).get( "name" ).asText() ); - - data = likeRestaurants.collection( "restaurants" ).entity( arrogantButcherId ).get(); - assertEquals( "arrogantbutcher", getEntity( data, 0 ).get( "name" ).asText() ); - - // do a delete either token should work - data = likeRestaurants.collection( "restaurants" ).entity( peaksId ).delete(); - - assertNotNull( data ); - assertEquals( "4peaks", getEntity( data, 0 ).get( "name" ).asText() ); - - data = likeRestaurants.collection( "restaurants" ).entity( arrogantButcherId ).delete(); - - assertNotNull( data ); - assertEquals( "arrogantbutcher", getEntity( data, 0 ).get( "name" ).asText() ); - } -}
