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

[16/19] incubator-usergrid git commit: Added value validation tests

Added value validation tests


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

Branch: refs/heads/two-dot-o
Commit: 3e625b1e9160c384111d39bb3a740c6bf978a4a9
Parents: fb90ae7
Author: Todd Nine <tn...@apigee.com>
Authored: Fri Nov 21 13:00:36 2014 -0700
Committer: Todd Nine <tn...@apigee.com>
Committed: Fri Nov 21 13:00:36 2014 -0700

----------------------------------------------------------------------
 .../corepersistence/CpEntityManager.java        |   2 +-
 .../corepersistence/util/CpNamingUtils.java     |   4 +-
 .../serialization/SerializationFig.java         |   6 +-
 .../serialization/impl/SerializationModule.java |   4 +
 .../serialization/impl/SettingsValidation.java  |  58 ++++++++
 .../impl/SettingsValidationTest.java            | 131 +++++++++++++++++++
 6 files changed, 199 insertions(+), 6 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/3e625b1e/stack/core/src/main/java/org/apache/usergrid/corepersistence/CpEntityManager.java
----------------------------------------------------------------------
diff --git a/stack/core/src/main/java/org/apache/usergrid/corepersistence/CpEntityManager.java b/stack/core/src/main/java/org/apache/usergrid/corepersistence/CpEntityManager.java
index e6b8bce..b2d854b 100644
--- a/stack/core/src/main/java/org/apache/usergrid/corepersistence/CpEntityManager.java
+++ b/stack/core/src/main/java/org/apache/usergrid/corepersistence/CpEntityManager.java
@@ -583,7 +583,7 @@ public class CpEntityManager implements EntityManager {
 
     private Observable deleteAsync( EntityRef entityRef ) throws Exception {
 
-        CollectionScope collectionScope =getCollectionScopeNameFromEntityType(applicationScope.getApplication(),  entityRef.getType()  );
+        CollectionScope collectionScope = getCollectionScopeNameFromEntityType(applicationScope.getApplication(), entityRef.getType()  );
 
         EntityCollectionManager ecm = managerCache.getEntityCollectionManager( collectionScope );
 

http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/3e625b1e/stack/core/src/main/java/org/apache/usergrid/corepersistence/util/CpNamingUtils.java
----------------------------------------------------------------------
diff --git a/stack/core/src/main/java/org/apache/usergrid/corepersistence/util/CpNamingUtils.java b/stack/core/src/main/java/org/apache/usergrid/corepersistence/util/CpNamingUtils.java
index 684b6e0..0d7b6ff 100644
--- a/stack/core/src/main/java/org/apache/usergrid/corepersistence/util/CpNamingUtils.java
+++ b/stack/core/src/main/java/org/apache/usergrid/corepersistence/util/CpNamingUtils.java
@@ -79,9 +79,7 @@ public class CpNamingUtils {
      * @return The collectionScope
      */
     public static CollectionScope getCollectionScopeNameFromEntityType(final Id applicationId, final String type){
-       return
-                       new CollectionScopeImpl( applicationId, applicationId,
-                               getCollectionScopeNameFromEntityType( type ) );
+       return  new CollectionScopeImpl( applicationId, applicationId, getCollectionScopeNameFromEntityType( type ) );
     }
 
     /**

http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/3e625b1e/stack/corepersistence/collection/src/main/java/org/apache/usergrid/persistence/collection/serialization/SerializationFig.java
----------------------------------------------------------------------
diff --git a/stack/corepersistence/collection/src/main/java/org/apache/usergrid/persistence/collection/serialization/SerializationFig.java b/stack/corepersistence/collection/src/main/java/org/apache/usergrid/persistence/collection/serialization/SerializationFig.java
index 0687312..9fb04d1 100644
--- a/stack/corepersistence/collection/src/main/java/org/apache/usergrid/persistence/collection/serialization/SerializationFig.java
+++ b/stack/corepersistence/collection/src/main/java/org/apache/usergrid/persistence/collection/serialization/SerializationFig.java
@@ -13,6 +13,8 @@ import org.safehaus.guicyfig.Key;
 @FigSingleton
 public interface SerializationFig extends GuicyFig {
 
+    String COLLECTION_MAX_ENTITY_SIZE = "collection.max.entity.size";
+
     /**
      * Time to live timeout in seconds.
      *
@@ -58,7 +60,7 @@ public interface SerializationFig extends GuicyFig {
     int getTaskPoolQueueSize();
 
     /**
-     * The maximum amount of entities we can load at any one time
+     * The maximum amount of entities we can load in a single request
      */
     @Key( "collection.max.load.size" )
     @Default( "100" )
@@ -71,7 +73,7 @@ public interface SerializationFig extends GuicyFig {
      *
      * (15mb thrift buffer * .9) / 100 (default max load size)
      */
-    @Key( "collection.max.entity.size" )
+    @Key( COLLECTION_MAX_ENTITY_SIZE )
     @Default( "141557" )
     int getMaxEntitySize();
 

http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/3e625b1e/stack/corepersistence/collection/src/main/java/org/apache/usergrid/persistence/collection/serialization/impl/SerializationModule.java
----------------------------------------------------------------------
diff --git a/stack/corepersistence/collection/src/main/java/org/apache/usergrid/persistence/collection/serialization/impl/SerializationModule.java b/stack/corepersistence/collection/src/main/java/org/apache/usergrid/persistence/collection/serialization/impl/SerializationModule.java
index eb9c374..33a092d 100644
--- a/stack/corepersistence/collection/src/main/java/org/apache/usergrid/persistence/collection/serialization/impl/SerializationModule.java
+++ b/stack/corepersistence/collection/src/main/java/org/apache/usergrid/persistence/collection/serialization/impl/SerializationModule.java
@@ -59,5 +59,9 @@ public class SerializationModule extends AbstractModule {
         uriBinder.addBinding().to( Key.get( MvccEntitySerializationStrategy.class, CurrentImpl.class ) );
         uriBinder.addBinding().to( Key.get( MvccLogEntrySerializationStrategy.class ) );
         uriBinder.addBinding().to( Key.get( UniqueValueSerializationStrategy.class ) );
+
+
+        //bind our settings as an eager singleton so it's checked on startup
+        bind(SettingsValidation.class).asEagerSingleton();
     }
 }

http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/3e625b1e/stack/corepersistence/collection/src/main/java/org/apache/usergrid/persistence/collection/serialization/impl/SettingsValidation.java
----------------------------------------------------------------------
diff --git a/stack/corepersistence/collection/src/main/java/org/apache/usergrid/persistence/collection/serialization/impl/SettingsValidation.java b/stack/corepersistence/collection/src/main/java/org/apache/usergrid/persistence/collection/serialization/impl/SettingsValidation.java
new file mode 100644
index 0000000..6715cfb
--- /dev/null
+++ b/stack/corepersistence/collection/src/main/java/org/apache/usergrid/persistence/collection/serialization/impl/SettingsValidation.java
@@ -0,0 +1,58 @@
+/*
+ * 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 org.apache.usergrid.persistence.collection.serialization.SerializationFig;
+import org.apache.usergrid.persistence.core.astyanax.CassandraFig;
+
+import com.google.common.base.Preconditions;
+import com.google.inject.Inject;
+import com.google.inject.Singleton;
+
+
+/**
+ *
+ * This element validates our SerializationFig and CassandraFig are correct for transport values
+ * TODO, this feels like a hack (Even though it's legal in GUICE)  When we have more time, we should look at using
+ * something like visitors and SPI to perform validation
+ */
+@Singleton
+public class SettingsValidation {
+
+
+    @Inject
+    public SettingsValidation( final CassandraFig cassandraFig, final SerializationFig serializationFig ) {
+        final int thriftBufferSize = cassandraFig.getThriftBufferSize();
+
+        Preconditions.checkArgument( thriftBufferSize > 0, CassandraFig.THRIFT_TRANSPORT_SIZE + " must be > than 0"  );
+
+        final int usableThriftBufferSize = ( int ) (thriftBufferSize*.9);
+
+        final int maxEntitySize = serializationFig.getMaxEntitySize();
+
+        Preconditions.checkArgument( maxEntitySize > 0, CassandraFig.THRIFT_TRANSPORT_SIZE + " must be > than 0"  );
+
+        Preconditions.checkArgument(usableThriftBufferSize >= maxEntitySize, "You cannot set the max entity size to more than the thrift buffer size * .9.  Maximum usable thrift size is " + usableThriftBufferSize + " and max entity size is " + maxEntitySize);
+
+    }
+
+
+}

http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/3e625b1e/stack/corepersistence/collection/src/test/java/org/apache/usergrid/persistence/collection/serialization/impl/SettingsValidationTest.java
----------------------------------------------------------------------
diff --git a/stack/corepersistence/collection/src/test/java/org/apache/usergrid/persistence/collection/serialization/impl/SettingsValidationTest.java b/stack/corepersistence/collection/src/test/java/org/apache/usergrid/persistence/collection/serialization/impl/SettingsValidationTest.java
new file mode 100644
index 0000000..807bc4e
--- /dev/null
+++ b/stack/corepersistence/collection/src/test/java/org/apache/usergrid/persistence/collection/serialization/impl/SettingsValidationTest.java
@@ -0,0 +1,131 @@
+/*
+ * 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 org.junit.Test;
+
+import org.apache.usergrid.persistence.collection.serialization.SerializationFig;
+import org.apache.usergrid.persistence.core.astyanax.CassandraFig;
+
+import static org.junit.Assert.*;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+
+/**
+ * Performs basic unit tests on our settings validation
+ */
+public class SettingsValidationTest {
+
+    /**
+     * Tests that when we're within range, it passes
+     */
+    @Test
+    public void withinBounds(){
+        CassandraFig cassandraFig = mock(CassandraFig.class);
+
+        final int thriftSize = 15728640;
+
+        final int usableThriftSize = ( int ) (thriftSize * .9);
+
+        when(cassandraFig.getThriftBufferSize()).thenReturn( thriftSize );
+
+
+        SerializationFig serializationFig = mock(SerializationFig.class);
+
+        when(serializationFig.getMaxEntitySize()).thenReturn( usableThriftSize );
+
+        new  SettingsValidation( cassandraFig, serializationFig);
+
+        when(serializationFig.getMaxEntitySize()).thenReturn( usableThriftSize -1  );
+
+        new  SettingsValidation( cassandraFig, serializationFig);
+
+    }
+
+
+    /**
+     * Tests that when we're within range, it passes
+     */
+    @Test(expected = IllegalArgumentException.class)
+    public void outOfBounds(){
+        CassandraFig cassandraFig = mock(CassandraFig.class);
+
+        final int thriftSize = 15728640;
+
+        final int usableThriftSize = ( int ) (thriftSize * .9);
+
+        when(cassandraFig.getThriftBufferSize()).thenReturn( thriftSize );
+
+
+        SerializationFig serializationFig = mock(SerializationFig.class);
+
+        when(serializationFig.getMaxEntitySize()).thenReturn( usableThriftSize+1 );
+
+        new  SettingsValidation( cassandraFig, serializationFig);
+
+
+
+    }
+
+
+
+    /**
+     * Tests that when we're within range, it passes
+     */
+    @Test(expected = IllegalArgumentException.class)
+    public void zeroBufferSize(){
+        CassandraFig cassandraFig = mock(CassandraFig.class);
+
+        final int thriftSize = 0;
+
+        when(cassandraFig.getThriftBufferSize()).thenReturn( thriftSize );
+
+        SerializationFig serializationFig = mock(SerializationFig.class);
+
+        new  SettingsValidation( cassandraFig, serializationFig);
+
+
+
+    }
+
+    /**
+         * Tests that when we're within range, it passes
+         */
+        @Test(expected = IllegalArgumentException.class)
+        public void zeroEntitySize(){
+            CassandraFig cassandraFig = mock(CassandraFig.class);
+
+            final int thriftSize = 15728640;
+
+            when(cassandraFig.getThriftBufferSize()).thenReturn( thriftSize );
+
+
+            SerializationFig serializationFig = mock(SerializationFig.class);
+
+            when(serializationFig.getMaxEntitySize()).thenReturn( 0 );
+
+            new  SettingsValidation( cassandraFig, serializationFig);
+
+
+
+        }
+}