Removed redundant load method Finished V2 tests and refactor. Issue proven in tests which fail.
Project: http://git-wip-us.apache.org/repos/asf/incubator-usergrid/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-usergrid/commit/8beab448 Tree: http://git-wip-us.apache.org/repos/asf/incubator-usergrid/tree/8beab448 Diff: http://git-wip-us.apache.org/repos/asf/incubator-usergrid/diff/8beab448 Branch: refs/heads/USERGRID-250-buffer-size-fix Commit: 8beab4489672e403462ecdc9bfe6f3061954c076 Parents: 2bd1c95 Author: Todd Nine <[email protected]> Authored: Wed Nov 19 17:37:44 2014 -0700 Committer: Todd Nine <[email protected]> Committed: Wed Nov 19 17:37:44 2014 -0700 ---------------------------------------------------------------------- .../mvcc/MvccEntitySerializationStrategy.java | 12 --- .../mvcc/stage/delete/MarkCommit.java | 2 +- .../serialization/impl/EntityRepairImpl.java | 2 +- .../MvccEntitySerializationStrategyImpl.java | 30 ------ ...vccEntitySerializationStrategyProxyImpl.java | 9 -- .../collection/EntityCollectionManagerIT.java | 28 +---- .../serialization/EntityRepairImplTest.java | 2 +- ...ccEntitySerializationSTrategyV2FixTests.java | 82 ++++++++++++++ ...MvccEntitySerializationStrategyImplTest.java | 17 +-- ...cEntitySerializationStrategyProxyV2Test.java | 2 +- ...ccEntitySerializationStrategyV2ImplTest.java | 4 +- .../collection/util/EntityHelper.java | 106 +++++++++++++++++++ 12 files changed, 208 insertions(+), 88 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/8beab448/stack/corepersistence/collection/src/main/java/org/apache/usergrid/persistence/collection/mvcc/MvccEntitySerializationStrategy.java ---------------------------------------------------------------------- diff --git a/stack/corepersistence/collection/src/main/java/org/apache/usergrid/persistence/collection/mvcc/MvccEntitySerializationStrategy.java b/stack/corepersistence/collection/src/main/java/org/apache/usergrid/persistence/collection/mvcc/MvccEntitySerializationStrategy.java index 93288af..faa76a3 100644 --- a/stack/corepersistence/collection/src/main/java/org/apache/usergrid/persistence/collection/mvcc/MvccEntitySerializationStrategy.java +++ b/stack/corepersistence/collection/src/main/java/org/apache/usergrid/persistence/collection/mvcc/MvccEntitySerializationStrategy.java @@ -56,18 +56,6 @@ public interface MvccEntitySerializationStrategy extends Migration { */ public EntitySet load( CollectionScope scope, Collection<Id> entityIds, UUID maxVersion); - /** - * Load a list, from highest to lowest of the entity with versions <= version up to maxSize elements - * - * @param context The context to persist the entity into - * @param entityId The entity id to load - * @param version The max version to seek from. I.E a stored version >= this argument - * @param fetchSize The maximum size to return. If you receive this size, there may be more versions to load. - * - * @return A list of entities up to max size ordered from max(UUID)=> min(UUID). The return value should be null - * safe and return an empty list when there are no matches - */ - public Iterator<MvccEntity> load( CollectionScope context, Id entityId, UUID version, int fetchSize ); /** * Load a historical list of entities, from highest to lowest of the entity with versions <= version up to maxSize elements http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/8beab448/stack/corepersistence/collection/src/main/java/org/apache/usergrid/persistence/collection/mvcc/stage/delete/MarkCommit.java ---------------------------------------------------------------------- diff --git a/stack/corepersistence/collection/src/main/java/org/apache/usergrid/persistence/collection/mvcc/stage/delete/MarkCommit.java b/stack/corepersistence/collection/src/main/java/org/apache/usergrid/persistence/collection/mvcc/stage/delete/MarkCommit.java index 5bcb9f8..fab904d 100644 --- a/stack/corepersistence/collection/src/main/java/org/apache/usergrid/persistence/collection/mvcc/stage/delete/MarkCommit.java +++ b/stack/corepersistence/collection/src/main/java/org/apache/usergrid/persistence/collection/mvcc/stage/delete/MarkCommit.java @@ -129,7 +129,7 @@ public class MarkCommit implements Action1<CollectionIoEvent<MvccEntity>> { @Override protected Iterator<MvccEntity> getIterator() { Iterator<MvccEntity> entities = - entityStrat.load( collectionScope, entityId, entity.getVersion(), 100 ); + entityStrat.loadHistory( collectionScope, entityId, entity.getVersion(), 100 ); return entities; } http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/8beab448/stack/corepersistence/collection/src/main/java/org/apache/usergrid/persistence/collection/serialization/impl/EntityRepairImpl.java ---------------------------------------------------------------------- diff --git a/stack/corepersistence/collection/src/main/java/org/apache/usergrid/persistence/collection/serialization/impl/EntityRepairImpl.java b/stack/corepersistence/collection/src/main/java/org/apache/usergrid/persistence/collection/serialization/impl/EntityRepairImpl.java index a94c688..a01ffdb 100644 --- a/stack/corepersistence/collection/src/main/java/org/apache/usergrid/persistence/collection/serialization/impl/EntityRepairImpl.java +++ b/stack/corepersistence/collection/src/main/java/org/apache/usergrid/persistence/collection/serialization/impl/EntityRepairImpl.java @@ -69,7 +69,7 @@ public class EntityRepairImpl implements EntityRepair { partialEntities.add( targetEntity ); final Iterator<MvccEntity> results = mvccEntitySerializationStrategy - .load( collectionScope, targetEntity.getId(), targetEntity.getVersion(), + .loadHistory( collectionScope, targetEntity.getId(), targetEntity.getVersion(), serializationFig.getBufferSize() ); http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/8beab448/stack/corepersistence/collection/src/main/java/org/apache/usergrid/persistence/collection/serialization/impl/MvccEntitySerializationStrategyImpl.java ---------------------------------------------------------------------- diff --git a/stack/corepersistence/collection/src/main/java/org/apache/usergrid/persistence/collection/serialization/impl/MvccEntitySerializationStrategyImpl.java b/stack/corepersistence/collection/src/main/java/org/apache/usergrid/persistence/collection/serialization/impl/MvccEntitySerializationStrategyImpl.java index 1ec027f..c964b68 100644 --- a/stack/corepersistence/collection/src/main/java/org/apache/usergrid/persistence/collection/serialization/impl/MvccEntitySerializationStrategyImpl.java +++ b/stack/corepersistence/collection/src/main/java/org/apache/usergrid/persistence/collection/serialization/impl/MvccEntitySerializationStrategyImpl.java @@ -209,36 +209,6 @@ public abstract class MvccEntitySerializationStrategyImpl implements MvccEntityS @Override - public Iterator<MvccEntity> load( final CollectionScope collectionScope, final Id entityId, final UUID version, - final int fetchSize ) { - - Preconditions.checkNotNull( collectionScope, "collectionScope is required" ); - Preconditions.checkNotNull( entityId, "entity id is required" ); - Preconditions.checkNotNull( version, "version is required" ); - Preconditions.checkArgument( fetchSize > 0, "max Size must be greater than 0" ); - - - final Id applicationId = collectionScope.getApplication(); - final Id ownerId = collectionScope.getOwner(); - final String collectionName = collectionScope.getName(); - - final CollectionPrefixedKey<Id> collectionPrefixedKey = - new CollectionPrefixedKey<>( collectionName, ownerId, entityId ); - - - final ScopedRowKey<CollectionPrefixedKey<Id>> rowKey = - ScopedRowKey.fromKey( applicationId, collectionPrefixedKey ); - - - RowQuery<ScopedRowKey<CollectionPrefixedKey<Id>>, UUID> query = - keyspace.prepareQuery( CF_ENTITY_DATA ).getKey( rowKey ) - .withColumnRange( version, null, false, fetchSize ); - - return new ColumnNameIterator( query, new MvccColumnParser( entityId, entityJsonSerializer ), false ); - } - - - @Override public Iterator<MvccEntity> loadHistory( final CollectionScope collectionScope, final Id entityId, final UUID version, final int fetchSize ) { http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/8beab448/stack/corepersistence/collection/src/main/java/org/apache/usergrid/persistence/collection/serialization/impl/MvccEntitySerializationStrategyProxyImpl.java ---------------------------------------------------------------------- diff --git a/stack/corepersistence/collection/src/main/java/org/apache/usergrid/persistence/collection/serialization/impl/MvccEntitySerializationStrategyProxyImpl.java b/stack/corepersistence/collection/src/main/java/org/apache/usergrid/persistence/collection/serialization/impl/MvccEntitySerializationStrategyProxyImpl.java index a9e01b1..f5cf642 100644 --- a/stack/corepersistence/collection/src/main/java/org/apache/usergrid/persistence/collection/serialization/impl/MvccEntitySerializationStrategyProxyImpl.java +++ b/stack/corepersistence/collection/src/main/java/org/apache/usergrid/persistence/collection/serialization/impl/MvccEntitySerializationStrategyProxyImpl.java @@ -95,15 +95,6 @@ public class MvccEntitySerializationStrategyProxyImpl implements MvccEntitySeria } - @Override - public Iterator<MvccEntity> load( final CollectionScope context, final Id entityId, final UUID version, - final int fetchSize ) { - if ( isOldVersion() ) { - return previous.load( context, entityId, version, fetchSize ); - } - - return current.load( context, entityId, version, fetchSize ); - } @Override http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/8beab448/stack/corepersistence/collection/src/test/java/org/apache/usergrid/persistence/collection/EntityCollectionManagerIT.java ---------------------------------------------------------------------- diff --git a/stack/corepersistence/collection/src/test/java/org/apache/usergrid/persistence/collection/EntityCollectionManagerIT.java b/stack/corepersistence/collection/src/test/java/org/apache/usergrid/persistence/collection/EntityCollectionManagerIT.java index 2d18675..db5e3a4 100644 --- a/stack/corepersistence/collection/src/test/java/org/apache/usergrid/persistence/collection/EntityCollectionManagerIT.java +++ b/stack/corepersistence/collection/src/test/java/org/apache/usergrid/persistence/collection/EntityCollectionManagerIT.java @@ -28,6 +28,7 @@ import org.junit.Test; import org.junit.runner.RunWith; import org.apache.usergrid.persistence.collection.exception.WriteUniqueVerifyException; +import org.apache.usergrid.persistence.collection.util.EntityHelper; import org.apache.usergrid.persistence.core.guice.MigrationManagerRule; import org.apache.usergrid.persistence.collection.guice.TestCollectionModule; import org.apache.usergrid.persistence.collection.impl.CollectionScopeImpl; @@ -43,7 +44,6 @@ import org.apache.usergrid.persistence.model.field.BooleanField; import org.apache.usergrid.persistence.model.field.Field; import org.apache.usergrid.persistence.model.field.IntegerField; import org.apache.usergrid.persistence.model.field.StringField; -import org.apache.usergrid.persistence.model.util.UUIDGenerator; import com.fasterxml.uuid.UUIDComparator; import com.google.inject.Inject; @@ -713,29 +713,8 @@ public class EntityCollectionManagerIT { public void largeEntityWriteRead() { final int setSize = 65535 * 2; - int currentLength = 0; - - final Entity entity = new Entity( new SimpleId( "test" ) ); - - //generate a really large string value - StringBuilder builder = new StringBuilder(); - - for ( int i = 0; i < 100; i++ ) { - builder.append( UUIDGenerator.newTimeUUID().toString() ); - } - - final String value = builder.toString(); - - - //loop until our size is beyond the set size - for ( int i = 0; currentLength < setSize; i++ ) { - final String key = "newStringField" + i; - - entity.setField( new StringField( key, value ) ); - - currentLength += key.length() + value.length(); - } + final Entity entity = EntityHelper.generateEntity( setSize ); //now we have one massive, entity, save it and retrieve it. CollectionScope context = @@ -751,6 +730,7 @@ public class EntityCollectionManagerIT { //now load it final Entity loaded = manager.load( entity.getId() ).toBlocking().last(); - assertEquals( entity, loaded ); + + EntityHelper.verifySame( entity, loaded ); } } http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/8beab448/stack/corepersistence/collection/src/test/java/org/apache/usergrid/persistence/collection/serialization/EntityRepairImplTest.java ---------------------------------------------------------------------- diff --git a/stack/corepersistence/collection/src/test/java/org/apache/usergrid/persistence/collection/serialization/EntityRepairImplTest.java b/stack/corepersistence/collection/src/test/java/org/apache/usergrid/persistence/collection/serialization/EntityRepairImplTest.java index 27f39dc..4326e10 100644 --- a/stack/corepersistence/collection/src/test/java/org/apache/usergrid/persistence/collection/serialization/EntityRepairImplTest.java +++ b/stack/corepersistence/collection/src/test/java/org/apache/usergrid/persistence/collection/serialization/EntityRepairImplTest.java @@ -101,7 +101,7 @@ public class EntityRepairImplTest { //mock up returning when( mvccEntitySerializationStrategy - .load( scope, simpleId, v3.getVersion(), serializationFig.getBufferSize() ) ) + .loadHistory( scope, simpleId, v3.getVersion(), serializationFig.getBufferSize() ) ) .thenReturn( Arrays.<MvccEntity>asList( v3, v2, v1 ).iterator() ); http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/8beab448/stack/corepersistence/collection/src/test/java/org/apache/usergrid/persistence/collection/serialization/impl/MvccEntitySerializationSTrategyV2FixTests.java ---------------------------------------------------------------------- diff --git a/stack/corepersistence/collection/src/test/java/org/apache/usergrid/persistence/collection/serialization/impl/MvccEntitySerializationSTrategyV2FixTests.java b/stack/corepersistence/collection/src/test/java/org/apache/usergrid/persistence/collection/serialization/impl/MvccEntitySerializationSTrategyV2FixTests.java new file mode 100644 index 0000000..293922e --- /dev/null +++ b/stack/corepersistence/collection/src/test/java/org/apache/usergrid/persistence/collection/serialization/impl/MvccEntitySerializationSTrategyV2FixTests.java @@ -0,0 +1,82 @@ +/* + * 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.persistence.collection.serialization.impl; + + +import java.util.Iterator; +import java.util.UUID; + +import org.junit.Test; + +import org.apache.usergrid.persistence.collection.CollectionScope; +import org.apache.usergrid.persistence.collection.MvccEntity; +import org.apache.usergrid.persistence.collection.impl.CollectionScopeImpl; +import org.apache.usergrid.persistence.collection.mvcc.entity.impl.MvccEntityImpl; +import org.apache.usergrid.persistence.collection.util.EntityHelper; +import org.apache.usergrid.persistence.model.entity.Entity; +import org.apache.usergrid.persistence.model.entity.Id; +import org.apache.usergrid.persistence.model.entity.SimpleId; +import org.apache.usergrid.persistence.model.util.UUIDGenerator; + +import com.netflix.astyanax.connectionpool.exceptions.ConnectionException; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + + +public abstract class MvccEntitySerializationSTrategyV2FixTests extends MvccEntitySerializationStrategyImplTest { + + + /** + * Tests an entity with more than 65535 bytes worth of data is successfully stored and retrieved + */ + @Test + public void largeEntityWriteRead() throws ConnectionException { + final int setSize = 65535 * 2; + + final Entity entity = EntityHelper.generateEntity( setSize ); + + //now we have one massive, entity, save it and retrieve it. + CollectionScope context = + new CollectionScopeImpl( new SimpleId( "organization" ), new SimpleId( "test" ), "test" ); + + + final Id simpleId = new SimpleId( "test" ); + final UUID version = UUIDGenerator.newTimeUUID(); + final MvccEntity.Status status = MvccEntity.Status.COMPLETE; + + final MvccEntity mvccEntity = new MvccEntityImpl( simpleId, version, status, entity ); + + + getMvccEntitySerializationStrategy().write( context, mvccEntity ).execute(); + + //now load it + final Iterator<MvccEntity> loaded = + getMvccEntitySerializationStrategy().loadHistory( context, entity.getId(), version, 100 ); + + assertTrue( loaded.hasNext() ); + + final MvccEntity loadedEntity = loaded.next(); + + assertEquals( "The loaded entity should match the stored entity", mvccEntity, loadedEntity ); + + EntityHelper.verifySame( entity, loadedEntity.getEntity().get() ); + } +} http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/8beab448/stack/corepersistence/collection/src/test/java/org/apache/usergrid/persistence/collection/serialization/impl/MvccEntitySerializationStrategyImplTest.java ---------------------------------------------------------------------- diff --git a/stack/corepersistence/collection/src/test/java/org/apache/usergrid/persistence/collection/serialization/impl/MvccEntitySerializationStrategyImplTest.java b/stack/corepersistence/collection/src/test/java/org/apache/usergrid/persistence/collection/serialization/impl/MvccEntitySerializationStrategyImplTest.java index 4b5ea7a..bc7c783 100644 --- a/stack/corepersistence/collection/src/test/java/org/apache/usergrid/persistence/collection/serialization/impl/MvccEntitySerializationStrategyImplTest.java +++ b/stack/corepersistence/collection/src/test/java/org/apache/usergrid/persistence/collection/serialization/impl/MvccEntitySerializationStrategyImplTest.java @@ -402,7 +402,7 @@ public abstract class MvccEntitySerializationStrategyImplTest { //now ask for up to 10 versions from the current version, we should get cleared, v2, v1 UUID current = UUIDGenerator.newTimeUUID(); - Iterator<MvccEntity> entities = serializationStrategy.load( context, id, current, 3 ); + Iterator<MvccEntity> entities = serializationStrategy.loadHistory( context, id, current, 3 ); MvccEntity first = entities.next(); assertEquals( clearedV3, first); @@ -422,7 +422,7 @@ public abstract class MvccEntitySerializationStrategyImplTest { serializationStrategy.delete( context, id , version1 ).execute(); serializationStrategy.delete( context, id , version2 ).execute(); - entities = serializationStrategy.load( context, id, current, 3 ); + entities = serializationStrategy.loadHistory( context, id, current, 3 ); first = entities.next(); assertEquals( clearedV3, first ); @@ -432,7 +432,7 @@ public abstract class MvccEntitySerializationStrategyImplTest { serializationStrategy.delete( context, id , version3 ).execute(); - entities = serializationStrategy.load( context, id, current, 3 ); + entities = serializationStrategy.loadHistory( context, id, current, 3 ); Assert.assertTrue( !entities.hasNext()); } @@ -639,7 +639,7 @@ public abstract class MvccEntitySerializationStrategyImplTest { @Test(expected = NullPointerException.class) public void loadListParamContext() throws ConnectionException { - serializationStrategy.load( null, new SimpleId( "test" ), UUIDGenerator.newTimeUUID(), 1 ); + serializationStrategy.loadHistory( null, new SimpleId( "test" ), UUIDGenerator.newTimeUUID(), 1 ); } @@ -647,8 +647,8 @@ public abstract class MvccEntitySerializationStrategyImplTest { public void loadListParamEntityId() throws ConnectionException { serializationStrategy - .load( new CollectionScopeImpl(new SimpleId( "organization" ), new SimpleId( "test" ), "test" ), null, UUIDGenerator.newTimeUUID(), - 1 ); + .loadHistory( new CollectionScopeImpl( new SimpleId( "organization" ), new SimpleId( "test" ), "test" ), + null, UUIDGenerator.newTimeUUID(), 1 ); } @@ -656,14 +656,15 @@ public abstract class MvccEntitySerializationStrategyImplTest { public void loadListParamVersion() throws ConnectionException { serializationStrategy - .load( new CollectionScopeImpl(new SimpleId( "organization" ), new SimpleId( "test" ), "test" ), new SimpleId( "test" ), null, 1 ); + .loadHistory( new CollectionScopeImpl( new SimpleId( "organization" ), new SimpleId( "test" ), "test" ), + new SimpleId( "test" ), null, 1 ); } @Test(expected = IllegalArgumentException.class) public void loadListParamSize() throws ConnectionException { - serializationStrategy.load( new CollectionScopeImpl(new SimpleId( "organization" ), new SimpleId( "test" ), "test" ), new SimpleId( "test" ), + serializationStrategy.loadHistory( new CollectionScopeImpl(new SimpleId( "organization" ), new SimpleId( "test" ), "test" ), new SimpleId( "test" ), UUIDGenerator.newTimeUUID(), 0 ); } http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/8beab448/stack/corepersistence/collection/src/test/java/org/apache/usergrid/persistence/collection/serialization/impl/MvccEntitySerializationStrategyProxyV2Test.java ---------------------------------------------------------------------- diff --git a/stack/corepersistence/collection/src/test/java/org/apache/usergrid/persistence/collection/serialization/impl/MvccEntitySerializationStrategyProxyV2Test.java b/stack/corepersistence/collection/src/test/java/org/apache/usergrid/persistence/collection/serialization/impl/MvccEntitySerializationStrategyProxyV2Test.java index 19b2cae..02eccb1 100644 --- a/stack/corepersistence/collection/src/test/java/org/apache/usergrid/persistence/collection/serialization/impl/MvccEntitySerializationStrategyProxyV2Test.java +++ b/stack/corepersistence/collection/src/test/java/org/apache/usergrid/persistence/collection/serialization/impl/MvccEntitySerializationStrategyProxyV2Test.java @@ -36,7 +36,7 @@ import com.google.inject.Inject; @RunWith( ITRunner.class ) @UseModules( TestCollectionModule.class ) -public class MvccEntitySerializationStrategyProxyV2Test extends MvccEntitySerializationStrategyImplTest { +public class MvccEntitySerializationStrategyProxyV2Test extends MvccEntitySerializationSTrategyV2FixTests { @Inject @ProxyImpl http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/8beab448/stack/corepersistence/collection/src/test/java/org/apache/usergrid/persistence/collection/serialization/impl/MvccEntitySerializationStrategyV2ImplTest.java ---------------------------------------------------------------------- diff --git a/stack/corepersistence/collection/src/test/java/org/apache/usergrid/persistence/collection/serialization/impl/MvccEntitySerializationStrategyV2ImplTest.java b/stack/corepersistence/collection/src/test/java/org/apache/usergrid/persistence/collection/serialization/impl/MvccEntitySerializationStrategyV2ImplTest.java index 260bcf2..012eeff 100644 --- a/stack/corepersistence/collection/src/test/java/org/apache/usergrid/persistence/collection/serialization/impl/MvccEntitySerializationStrategyV2ImplTest.java +++ b/stack/corepersistence/collection/src/test/java/org/apache/usergrid/persistence/collection/serialization/impl/MvccEntitySerializationStrategyV2ImplTest.java @@ -34,7 +34,7 @@ import com.google.inject.Inject; @RunWith( ITRunner.class ) @UseModules( TestCollectionModule.class ) -public class MvccEntitySerializationStrategyV2ImplTest extends MvccEntitySerializationStrategyImplTest { +public class MvccEntitySerializationStrategyV2ImplTest extends MvccEntitySerializationSTrategyV2FixTests { @Inject @CurrentImpl @@ -44,4 +44,6 @@ public class MvccEntitySerializationStrategyV2ImplTest extends MvccEntitySeriali protected MvccEntitySerializationStrategy getMvccEntitySerializationStrategy() { return serializationStrategy; } + + } http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/8beab448/stack/corepersistence/collection/src/test/java/org/apache/usergrid/persistence/collection/util/EntityHelper.java ---------------------------------------------------------------------- diff --git a/stack/corepersistence/collection/src/test/java/org/apache/usergrid/persistence/collection/util/EntityHelper.java b/stack/corepersistence/collection/src/test/java/org/apache/usergrid/persistence/collection/util/EntityHelper.java new file mode 100644 index 0000000..9de546d --- /dev/null +++ b/stack/corepersistence/collection/src/test/java/org/apache/usergrid/persistence/collection/util/EntityHelper.java @@ -0,0 +1,106 @@ +/* + * 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.persistence.collection.util; + + +import java.util.Collection; +import java.util.HashMap; +import java.util.Map; + +import org.apache.usergrid.persistence.model.entity.Entity; +import org.apache.usergrid.persistence.model.entity.SimpleId; +import org.apache.usergrid.persistence.model.field.Field; +import org.apache.usergrid.persistence.model.field.StringField; +import org.apache.usergrid.persistence.model.util.UUIDGenerator; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; + + +/** + * Utilities for generating test data. + */ +public class EntityHelper { + + /** + * Generates an entity with all string fields to have at least the minSize + * number of characters in the field names + field values + * @param minSize + * @return + */ + public static Entity generateEntity(final int minSize){ + int currentLength = 0; + + final Entity entity = new Entity( new SimpleId( "test" ) ); + + //generate a really large string value + StringBuilder builder = new StringBuilder(); + + for ( int i = 0; i < 100; i++ ) { + builder.append( UUIDGenerator.newTimeUUID().toString() ); + } + + final String value = builder.toString(); + + + //loop until our size is beyond the set size + for ( int i = 0; currentLength < minSize; i++ ) { + final String key = "newStringField" + i; + + entity.setField( new StringField( key, value ) ); + + currentLength += key.length() + value.length(); + } + + return entity; + } + + + /** + * Verify that all fields in the expected are present in the returned, and have the same values + * via .equals. Does not recurse on object values. Also verifies there are no additional fields + * in the returned entity + * + * @param expected + * @param returned + */ + public static void verifySame(final Entity expected, final Entity returned){ + + //perform object equals + assertEquals("Expected same entity equality", expected, returned); + + final Collection<Field> expectedFields = expected.getFields(); + + final Map<String, Field> returnedFields = new HashMap<>(returned.getFieldMap()); + + for(Field expectedField: expectedFields){ + + final Field returnedField = returnedFields.get( expectedField.getName() ); + + assertNotNull("Field " + expectedField.getName() + " exists in returned entity", returnedField ); + + assertEquals("Field values should match", expectedField.getValue(), returnedField.getValue()); + + returnedFields.remove( expectedField.getName() ); + } + + assertEquals("There are no additional fields in the returned entity", 0, returnedFields.size()); + } +}
