This is an automated email from the ASF dual-hosted git repository.

mwalch pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/accumulo.git


The following commit(s) were added to refs/heads/master by this push:
     new b6f5bc1  Unit test and validation for client properties (#795)
b6f5bc1 is described below

commit b6f5bc1406b6d23cfaa5a0fe1c64b6f4f719c454
Author: Mike Walch <mwa...@apache.org>
AuthorDate: Wed Dec 5 12:06:16 2018 -0500

    Unit test and validation for client properties (#795)
---
 .../core/clientImpl/AccumuloClientImpl.java        |  3 ++
 .../apache/accumulo/core/conf/ClientProperty.java  | 23 +++++++++
 .../accumulo/core/client/ClientPropertiesTest.java | 55 ++++++++++++++++++++++
 3 files changed, 81 insertions(+)

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 2db723f..8657119 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
@@ -301,8 +301,10 @@ public class AccumuloClientImpl implements AccumuloClient {
 
     private ClientInfo getClientInfo() {
       if (token != null) {
+        ClientProperty.validate(properties, false);
         return new ClientInfoImpl(properties, token);
       }
+      ClientProperty.validate(properties);
       return new ClientInfoImpl(properties);
     }
 
@@ -323,6 +325,7 @@ public class AccumuloClientImpl implements AccumuloClient {
     }
 
     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 219906a..546db89 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 enum ClientProperty {
     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 0000000..a83eefc
--- /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
+    }
+  }
+}

Reply via email to