You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@accumulo.apache.org by GitBox <gi...@apache.org> on 2018/12/05 17:06:18 UTC

[GitHub] mikewalch closed pull request #795: Unit test and validation for client properties

mikewalch closed pull request #795: Unit test and validation for client properties
URL: https://github.com/apache/accumulo/pull/795
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/core/src/main/java/org/apache/accumulo/core/clientImpl/AccumuloClientImpl.java b/core/src/main/java/org/apache/accumulo/core/clientImpl/AccumuloClientImpl.java
index fc9cfde91b..5bc2487f97 100644
--- a/core/src/main/java/org/apache/accumulo/core/clientImpl/AccumuloClientImpl.java
+++ b/core/src/main/java/org/apache/accumulo/core/clientImpl/AccumuloClientImpl.java
@@ -302,8 +302,10 @@ public ClientBuilderImpl(Function<ClientBuilderImpl, T> builderFunction) {
 
     private ClientInfo getClientInfo() {
       if (token != null) {
+        ClientProperty.validate(properties, false);
         return new ClientInfoImpl(properties, token);
       }
+      ClientProperty.validate(properties);
       return new ClientInfoImpl(properties);
     }
 
@@ -324,6 +326,7 @@ public static AccumuloClient buildClient(ClientBuilderImpl<AccumuloClient> cbi)
     }
 
     public static Properties buildProps(ClientBuilderImpl<Properties> cbi) {
+      ClientProperty.validate(cbi.properties);
       return cbi.properties;
     }
 
diff --git a/core/src/main/java/org/apache/accumulo/core/conf/ClientProperty.java b/core/src/main/java/org/apache/accumulo/core/conf/ClientProperty.java
index 219906a77f..546db891a8 100644
--- a/core/src/main/java/org/apache/accumulo/core/conf/ClientProperty.java
+++ b/core/src/main/java/org/apache/accumulo/core/conf/ClientProperty.java
@@ -302,4 +302,27 @@ public static void setAuthenticationToken(Properties properties, AuthenticationT
     properties.setProperty(ClientProperty.AUTH_TYPE.getKey(), token.getClass().getName());
     properties.setProperty(ClientProperty.AUTH_TOKEN.getKey(), encodeToken(token));
   }
+
+  public static void validateProperty(Properties properties, ClientProperty prop) {
+    if (!properties.containsKey(prop.getKey()) || prop.getValue(properties).isEmpty()) {
+      throw new IllegalArgumentException(prop.getKey() + " is not set");
+    }
+  }
+
+  public static void validate(Properties properties, boolean validateToken) {
+    validateProperty(properties, ClientProperty.INSTANCE_NAME);
+    validateProperty(properties, ClientProperty.INSTANCE_ZOOKEEPERS);
+    validateProperty(properties, ClientProperty.AUTH_TYPE);
+    validateProperty(properties, ClientProperty.AUTH_PRINCIPAL);
+    if (validateToken) {
+      validateProperty(properties, ClientProperty.AUTH_TOKEN);
+    }
+  }
+
+  /**
+   * @throws IllegalArgumentException if Properties does not contain all required
+   */
+  public static void validate(Properties properties) {
+    validate(properties, true);
+  }
 }
diff --git a/core/src/test/java/org/apache/accumulo/core/client/ClientPropertiesTest.java b/core/src/test/java/org/apache/accumulo/core/client/ClientPropertiesTest.java
new file mode 100644
index 0000000000..a83eefc03a
--- /dev/null
+++ b/core/src/test/java/org/apache/accumulo/core/client/ClientPropertiesTest.java
@@ -0,0 +1,55 @@
+/*
+ * 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.accumulo.core.client;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.fail;
+
+import java.nio.file.Paths;
+import java.util.Properties;
+
+import org.apache.accumulo.core.conf.ClientProperty;
+import org.junit.Test;
+
+public class ClientPropertiesTest {
+
+  @Test
+  public void testBasic() {
+    Properties props1 = Accumulo.newClientProperties().to("inst1", "zoo1")
+        .as("user1", "pass1").build();
+    assertEquals("inst1", ClientProperty.INSTANCE_NAME.getValue(props1));
+    assertEquals("zoo1", ClientProperty.INSTANCE_ZOOKEEPERS.getValue(props1));
+    assertEquals("user1", ClientProperty.AUTH_PRINCIPAL.getValue(props1));
+    assertEquals("password", ClientProperty.AUTH_TYPE.getValue(props1));
+    assertEquals("pass1", ClientProperty.AUTH_TOKEN.getValue(props1));
+
+    Properties props2 = Accumulo.newClientProperties().from(props1).as("user2", Paths.get("/path2")).build();
+    assertEquals("inst1", ClientProperty.INSTANCE_NAME.getValue(props1));
+    assertEquals("zoo1", ClientProperty.INSTANCE_ZOOKEEPERS.getValue(props1));
+    assertEquals("user2", ClientProperty.AUTH_PRINCIPAL.getValue(props1));
+    assertEquals("kerberos", ClientProperty.AUTH_TYPE.getValue(props1));
+    assertEquals("/path2", ClientProperty.AUTH_TOKEN.getValue(props1));
+
+    props2.remove(ClientProperty.AUTH_PRINCIPAL.getKey());
+    try {
+      ClientProperty.validate(props2);
+      fail();
+    } catch (IllegalArgumentException e) {
+      // expected
+    }
+  }
+}


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services