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

nreich pushed a commit to branch feature/GEODE-3781
in repository https://gitbox.apache.org/repos/asf/geode.git


The following commit(s) were added to refs/heads/feature/GEODE-3781 by this 
push:
     new fa12dfc  Feature/geode 4021 (#1131)
fa12dfc is described below

commit fa12dfc8b3ffcd91cc68a4d24d8390a1148f8be3
Author: Nick Reich <[email protected]>
AuthorDate: Wed Dec 6 14:31:35 2017 -0800

    Feature/geode 4021 (#1131)
    
    * GEODE-4021: Add Gfsh command to describe JDBC connection configurations
---
 .../internal/cli/DescribeConnectionCommand.java    | 102 +++++++++++++++
 .../internal/cli/DescribeConnectionFunction.java   |  66 ++++++++++
 .../org.springframework.shell.core.CommandMarker   |   1 +
 .../cli/DescribeConnectionCommandDUnitTest.java    | 106 ++++++++++++++++
 .../DescribeConnectionCommandIntegrationTest.java  | 122 ++++++++++++++++++
 .../cli/DescribeConnectionFunctionTest.java        | 138 +++++++++++++++++++++
 6 files changed, 535 insertions(+)

diff --git 
a/geode-connectors/src/main/java/org/apache/geode/connectors/jdbc/internal/cli/DescribeConnectionCommand.java
 
b/geode-connectors/src/main/java/org/apache/geode/connectors/jdbc/internal/cli/DescribeConnectionCommand.java
new file mode 100644
index 0000000..48ba3d4
--- /dev/null
+++ 
b/geode-connectors/src/main/java/org/apache/geode/connectors/jdbc/internal/cli/DescribeConnectionCommand.java
@@ -0,0 +1,102 @@
+/*
+ * 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.geode.connectors.jdbc.internal.cli;
+
+import static 
org.apache.geode.connectors.jdbc.internal.cli.CreateConnectionCommand.CREATE_CONNECTION__NAME;
+import static 
org.apache.geode.connectors.jdbc.internal.cli.CreateConnectionCommand.CREATE_CONNECTION__PASSWORD;
+import static 
org.apache.geode.connectors.jdbc.internal.cli.CreateConnectionCommand.CREATE_CONNECTION__URL;
+
+import java.util.List;
+import java.util.Properties;
+import java.util.Set;
+
+import org.springframework.shell.core.annotation.CliCommand;
+import org.springframework.shell.core.annotation.CliOption;
+
+import org.apache.geode.cache.execute.ResultCollector;
+import org.apache.geode.connectors.jdbc.internal.ConnectionConfiguration;
+import org.apache.geode.distributed.DistributedMember;
+import org.apache.geode.management.cli.CliMetaData;
+import org.apache.geode.management.cli.Result;
+import org.apache.geode.management.internal.cli.commands.GfshCommand;
+import org.apache.geode.management.internal.cli.i18n.CliStrings;
+import org.apache.geode.management.internal.cli.result.CompositeResultData;
+import org.apache.geode.management.internal.cli.result.ResultBuilder;
+import org.apache.geode.management.internal.security.ResourceOperation;
+import org.apache.geode.security.ResourcePermission;
+
+public class DescribeConnectionCommand implements GfshCommand {
+
+  static final String DESCRIBE_CONNECTION = "describe jdbc-connection";
+  static final String DESCRIBE_CONNECTION__HELP = "Describe/Remove the 
specified jdbc connection.";
+  static final String DESCRIBE_CONNECTION__NAME = "name";
+  static final String DESCRIBE_CONNECTION__NAME__HELP =
+      "Name of the jdbc connection to be described.";
+
+  static final String OBSCURED_PASSWORD = "********";
+  static final String RESULT_SECTION_NAME = "ConnectionDescription";
+
+  @CliCommand(value = DESCRIBE_CONNECTION, help = DESCRIBE_CONNECTION__HELP)
+  @CliMetaData(relatedTopic = CliStrings.DEFAULT_TOPIC_GEODE)
+  @ResourceOperation(resource = ResourcePermission.Resource.CLUSTER,
+      operation = ResourcePermission.Operation.MANAGE)
+  public Result describeConnection(@CliOption(key = DESCRIBE_CONNECTION__NAME, 
mandatory = true,
+      help = DESCRIBE_CONNECTION__NAME__HELP) String name) {
+
+    // input
+    Set<DistributedMember> members = getMembers(null, null);
+    if (members.isEmpty()) {
+      return 
ResultBuilder.createUserErrorResult(CliStrings.NO_MEMBERS_FOUND_MESSAGE);
+    }
+    DistributedMember targetMember = members.iterator().next();
+
+    // action
+    ResultCollector<ConnectionConfiguration, List<ConnectionConfiguration>> 
resultCollector =
+        execute(new DescribeConnectionFunction(), name, targetMember);
+
+    // output
+    ConnectionConfiguration config = resultCollector.getResult().get(0);
+    if (config == null) {
+      return ResultBuilder.createInfoResult(String.format("Connection named 
'%s' not found", name));
+    }
+
+    CompositeResultData resultData = ResultBuilder.createCompositeResultData();
+    fillResultData(config, resultData);
+    return ResultBuilder.buildResult(resultData);
+  }
+
+  ResultCollector<ConnectionConfiguration, List<ConnectionConfiguration>> 
execute(
+      DescribeConnectionFunction function, String connectionName, 
DistributedMember targetMember) {
+    return (ResultCollector<ConnectionConfiguration, 
List<ConnectionConfiguration>>) executeFunction(
+        function, connectionName, targetMember);
+  }
+
+  private void fillResultData(ConnectionConfiguration config, 
CompositeResultData resultData) {
+    CompositeResultData.SectionResultData sectionResult =
+        resultData.addSection(RESULT_SECTION_NAME);
+
+    sectionResult.addSeparator('-');
+    sectionResult.addData(CREATE_CONNECTION__NAME, config.getName());
+    sectionResult.addData(CREATE_CONNECTION__URL, config.getUrl());
+    Properties parameters = config.getConnectionProperties();
+    for (String parameterName : parameters.stringPropertyNames()) {
+      if (parameterName.equals(CREATE_CONNECTION__PASSWORD)) {
+        sectionResult.addData(parameterName, OBSCURED_PASSWORD);
+      } else {
+        sectionResult.addData(parameterName, 
parameters.getProperty(parameterName));
+      }
+    }
+  }
+}
diff --git 
a/geode-connectors/src/main/java/org/apache/geode/connectors/jdbc/internal/cli/DescribeConnectionFunction.java
 
b/geode-connectors/src/main/java/org/apache/geode/connectors/jdbc/internal/cli/DescribeConnectionFunction.java
new file mode 100644
index 0000000..584e4ff
--- /dev/null
+++ 
b/geode-connectors/src/main/java/org/apache/geode/connectors/jdbc/internal/cli/DescribeConnectionFunction.java
@@ -0,0 +1,66 @@
+/*
+ * 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.geode.connectors.jdbc.internal.cli;
+
+import org.apache.geode.cache.execute.Function;
+import org.apache.geode.cache.execute.FunctionContext;
+import org.apache.geode.connectors.jdbc.internal.ConnectionConfiguration;
+import org.apache.geode.connectors.jdbc.internal.InternalJdbcConnectorService;
+import org.apache.geode.internal.InternalEntity;
+
+public class DescribeConnectionFunction implements Function<String>, 
InternalEntity {
+
+  private static final String ID = DescribeConnectionFunction.class.getName();
+
+  private final FunctionContextArgumentProvider argumentProvider;
+  private final ExceptionHandler exceptionHandler;
+
+  DescribeConnectionFunction() {
+    this(new FunctionContextArgumentProvider(), new ExceptionHandler());
+  }
+
+  private DescribeConnectionFunction(FunctionContextArgumentProvider 
jdbcCommandFunctionContext,
+      ExceptionHandler exceptionHandler) {
+    this.argumentProvider = jdbcCommandFunctionContext;
+    this.exceptionHandler = exceptionHandler;
+  }
+
+  @Override
+  public boolean isHA() {
+    return false;
+  }
+
+  @Override
+  public String getId() {
+    return ID;
+  }
+
+  @Override
+  public void execute(FunctionContext<String> context) {
+    try {
+      // input
+      InternalJdbcConnectorService service = 
argumentProvider.getJdbcConnectorService(context);
+      String connectionName = context.getArguments();
+
+      // action
+      ConnectionConfiguration config = 
service.getConnectionConfig(connectionName);
+
+      // output
+      context.getResultSender().lastResult(config);
+    } catch (Exception e) {
+      exceptionHandler.handleException(context, e);
+    }
+  }
+}
diff --git 
a/geode-connectors/src/main/resources/META-INF/services/org.springframework.shell.core.CommandMarker
 
b/geode-connectors/src/main/resources/META-INF/services/org.springframework.shell.core.CommandMarker
index 60e3207..8d75a83 100644
--- 
a/geode-connectors/src/main/resources/META-INF/services/org.springframework.shell.core.CommandMarker
+++ 
b/geode-connectors/src/main/resources/META-INF/services/org.springframework.shell.core.CommandMarker
@@ -18,3 +18,4 @@
 org.apache.geode.connectors.jdbc.internal.cli.CreateConnectionCommand
 org.apache.geode.connectors.jdbc.internal.cli.DestroyConnectionCommand
 org.apache.geode.connectors.jdbc.internal.cli.ListConnectionCommand
+org.apache.geode.connectors.jdbc.internal.cli.DescribeConnectionCommand
diff --git 
a/geode-connectors/src/test/java/org/apache/geode/connectors/jdbc/internal/cli/DescribeConnectionCommandDUnitTest.java
 
b/geode-connectors/src/test/java/org/apache/geode/connectors/jdbc/internal/cli/DescribeConnectionCommandDUnitTest.java
new file mode 100644
index 0000000..81abe74
--- /dev/null
+++ 
b/geode-connectors/src/test/java/org/apache/geode/connectors/jdbc/internal/cli/DescribeConnectionCommandDUnitTest.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.geode.connectors.jdbc.internal.cli;
+
+import static 
org.apache.geode.connectors.jdbc.internal.cli.DescribeConnectionCommand.DESCRIBE_CONNECTION;
+import static 
org.apache.geode.connectors.jdbc.internal.cli.DescribeConnectionCommand.DESCRIBE_CONNECTION__NAME;
+import static org.assertj.core.api.Assertions.assertThat;
+
+import java.io.Serializable;
+
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.experimental.categories.Category;
+
+import org.apache.geode.connectors.jdbc.internal.ConnectionConfigBuilder;
+import 
org.apache.geode.connectors.jdbc.internal.ConnectionConfigExistsException;
+import org.apache.geode.connectors.jdbc.internal.InternalJdbcConnectorService;
+import org.apache.geode.internal.cache.InternalCache;
+import org.apache.geode.management.internal.cli.util.CommandStringBuilder;
+import org.apache.geode.test.dunit.rules.LocatorServerStartupRule;
+import org.apache.geode.test.dunit.rules.MemberVM;
+import org.apache.geode.test.junit.assertions.CommandResultAssert;
+import org.apache.geode.test.junit.categories.DistributedTest;
+import org.apache.geode.test.junit.rules.GfshCommandRule;
+import org.apache.geode.test.junit.rules.serializable.SerializableTestName;
+
+@Category(DistributedTest.class)
+public class DescribeConnectionCommandDUnitTest implements Serializable {
+
+  private static final String CONNECTION_NAME = "connectionName";
+
+  @Rule
+  public transient GfshCommandRule gfsh = new GfshCommandRule();
+
+  @Rule
+  public LocatorServerStartupRule startupRule = new LocatorServerStartupRule();
+
+  @Rule
+  public SerializableTestName testName = new SerializableTestName();
+
+  private MemberVM server;
+
+  @Before
+  public void before() throws Exception {
+    MemberVM locator = startupRule.startLocatorVM(0);
+    server = startupRule.startServerVM(1, locator.getPort());
+
+    gfsh.connectAndVerify(locator);
+  }
+
+  @Test
+  public void describesExistingConnection() {
+    server.invoke(this::createConnection);
+    CommandStringBuilder csb = new CommandStringBuilder(DESCRIBE_CONNECTION)
+        .addOption(DESCRIBE_CONNECTION__NAME, CONNECTION_NAME);
+
+    CommandResultAssert commandResultAssert = 
gfsh.executeAndAssertThat(csb.toString());
+
+    commandResultAssert.statusIsSuccess();
+    commandResultAssert.containsKeyValuePair("name", CONNECTION_NAME);
+    commandResultAssert.containsKeyValuePair("url", "myUrl");
+    commandResultAssert.containsKeyValuePair("user", "username");
+    commandResultAssert.containsKeyValuePair("password", 
"\\*\\*\\*\\*\\*\\*\\*\\*");
+    commandResultAssert.containsKeyValuePair("key1", "value1");
+    commandResultAssert.containsKeyValuePair("key2", "value2");
+  }
+
+  @Test
+  public void reportsNoConfigurationFound() {
+    CommandStringBuilder csb = new CommandStringBuilder(DESCRIBE_CONNECTION)
+        .addOption(DESCRIBE_CONNECTION__NAME, "nonExisting");
+
+    CommandResultAssert commandResultAssert = 
gfsh.executeAndAssertThat(csb.toString());
+
+    commandResultAssert.statusIsSuccess();
+    commandResultAssert
+        .containsOutput(String.format("Connection named '%s' not found", 
"nonExisting"));
+  }
+
+  private void createConnection() throws ConnectionConfigExistsException {
+    InternalCache cache = LocatorServerStartupRule.getCache();
+    InternalJdbcConnectorService service = 
cache.getService(InternalJdbcConnectorService.class);
+
+    String[] params = new String[] {"key1:value1", "key2:value2"};
+    service.createConnectionConfig(
+        new 
ConnectionConfigBuilder().withName(CONNECTION_NAME).withUrl("myUrl")
+            
.withUser("username").withPassword("secret").withParameters(params).build());
+
+    assertThat(service.getConnectionConfig(CONNECTION_NAME)).isNotNull();
+  }
+
+
+}
diff --git 
a/geode-connectors/src/test/java/org/apache/geode/connectors/jdbc/internal/cli/DescribeConnectionCommandIntegrationTest.java
 
b/geode-connectors/src/test/java/org/apache/geode/connectors/jdbc/internal/cli/DescribeConnectionCommandIntegrationTest.java
new file mode 100644
index 0000000..771ace7
--- /dev/null
+++ 
b/geode-connectors/src/test/java/org/apache/geode/connectors/jdbc/internal/cli/DescribeConnectionCommandIntegrationTest.java
@@ -0,0 +1,122 @@
+/*
+ * 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.geode.connectors.jdbc.internal.cli;
+
+import static 
org.apache.geode.connectors.jdbc.internal.cli.CreateConnectionCommand.CREATE_CONNECTION__NAME;
+import static 
org.apache.geode.connectors.jdbc.internal.cli.CreateConnectionCommand.CREATE_CONNECTION__PASSWORD;
+import static 
org.apache.geode.connectors.jdbc.internal.cli.CreateConnectionCommand.CREATE_CONNECTION__URL;
+import static 
org.apache.geode.connectors.jdbc.internal.cli.DescribeConnectionCommand.OBSCURED_PASSWORD;
+import static 
org.apache.geode.connectors.jdbc.internal.cli.DescribeConnectionCommand.RESULT_SECTION_NAME;
+import static 
org.apache.geode.distributed.ConfigurationProperties.ENABLE_CLUSTER_CONFIGURATION;
+import static 
org.apache.geode.management.internal.cli.result.AbstractResultData.SECTION_DATA_ACCESSOR;
+import static org.assertj.core.api.Assertions.assertThat;
+
+import java.util.Properties;
+
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.experimental.categories.Category;
+
+import org.apache.geode.cache.CacheFactory;
+import org.apache.geode.connectors.jdbc.internal.ConnectionConfigBuilder;
+import org.apache.geode.connectors.jdbc.internal.ConnectionConfiguration;
+import org.apache.geode.connectors.jdbc.internal.InternalJdbcConnectorService;
+import org.apache.geode.internal.cache.InternalCache;
+import org.apache.geode.management.cli.Result;
+import org.apache.geode.management.internal.cli.json.GfJsonObject;
+import org.apache.geode.management.internal.cli.result.CommandResult;
+import org.apache.geode.test.junit.categories.IntegrationTest;
+
+@Category(IntegrationTest.class)
+public class DescribeConnectionCommandIntegrationTest {
+  private static final String CONNECTION = "connection";
+
+  private InternalCache cache;
+  private InternalJdbcConnectorService service;
+  private ConnectionConfiguration connectionConfig;
+  private DescribeConnectionCommand command;
+
+  @Before
+  public void setup() {
+    String[] params = new String[] {"param1:value1", "param2:value2"};
+
+    connectionConfig = new 
ConnectionConfigBuilder().withName(CONNECTION).withUrl("myUrl")
+        
.withUser("username").withPassword("secret").withParameters(params).build();
+
+    cache = (InternalCache) new 
CacheFactory().set(ENABLE_CLUSTER_CONFIGURATION, "true").create();
+    service = cache.getService(InternalJdbcConnectorService.class);
+
+    command = new DescribeConnectionCommand();
+  }
+
+  @After
+  public void tearDown() {
+    cache.close();
+  }
+
+  @Test
+  public void displaysNoConnectionFoundMessageWhenConfigurationDoesNotExist() {
+    String notExistingConnectionName = "non existing";
+    Result result = command.describeConnection(notExistingConnectionName);
+
+    assertThat(result.getStatus()).isSameAs(Result.Status.OK);
+    CommandResult commandResult = (CommandResult) result;
+    String tableContent = commandResult.getTableContent().toString();
+    assertThat(tableContent)
+        .contains("Connection named '" + notExistingConnectionName + "' not 
found");
+  }
+
+  @Test
+  public void displaysConnectionInformationWhenConfigurationExists() throws 
Exception {
+    service.createConnectionConfig(connectionConfig);
+    Result result = command.describeConnection(CONNECTION);
+
+    assertThat(result.getStatus()).isSameAs(Result.Status.OK);
+    CommandResult commandResult = (CommandResult) result;
+    GfJsonObject sectionContent = commandResult.getTableContent()
+        .getJSONObject(SECTION_DATA_ACCESSOR + "-" + RESULT_SECTION_NAME);
+
+    
assertThat(sectionContent.get(CREATE_CONNECTION__NAME)).isEqualTo(connectionConfig.getName());
+    
assertThat(sectionContent.get(CREATE_CONNECTION__URL)).isEqualTo(connectionConfig.getUrl());
+
+    Properties connectionProperties = 
connectionConfig.getConnectionProperties();
+    for (String parameterName : connectionProperties.stringPropertyNames()) {
+      if (parameterName.equals(CREATE_CONNECTION__PASSWORD)) {
+        
assertThat(sectionContent.get(parameterName)).isEqualTo(OBSCURED_PASSWORD);
+      } else {
+        assertThat(sectionContent.get(parameterName))
+            .isEqualTo(connectionProperties.getProperty(parameterName));
+      }
+    }
+  }
+
+  @Test
+  public void doesNotDisplayParametersWithNoValue() throws Exception {
+    connectionConfig = new 
ConnectionConfigBuilder().withName(CONNECTION).withUrl("myUrl").build();
+
+    service.createConnectionConfig(connectionConfig);
+    Result result = command.describeConnection(CONNECTION);
+
+    assertThat(result.getStatus()).isSameAs(Result.Status.OK);
+    CommandResult commandResult = (CommandResult) result;
+    GfJsonObject sectionContent = commandResult.getTableContent()
+        .getJSONObject(SECTION_DATA_ACCESSOR + "-" + RESULT_SECTION_NAME);
+
+    
assertThat(sectionContent.get(CREATE_CONNECTION__NAME)).isEqualTo(connectionConfig.getName());
+    
assertThat(sectionContent.get(CREATE_CONNECTION__URL)).isEqualTo(connectionConfig.getUrl());
+    assertThat(sectionContent.get(CREATE_CONNECTION__PASSWORD)).isNull();
+  }
+}
diff --git 
a/geode-connectors/src/test/java/org/apache/geode/connectors/jdbc/internal/cli/DescribeConnectionFunctionTest.java
 
b/geode-connectors/src/test/java/org/apache/geode/connectors/jdbc/internal/cli/DescribeConnectionFunctionTest.java
new file mode 100644
index 0000000..c877349
--- /dev/null
+++ 
b/geode-connectors/src/test/java/org/apache/geode/connectors/jdbc/internal/cli/DescribeConnectionFunctionTest.java
@@ -0,0 +1,138 @@
+/*
+ * 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.geode.connectors.jdbc.internal.cli;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.ArgumentMatchers.eq;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+import java.io.Serializable;
+
+import org.apache.commons.lang.SerializationUtils;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.experimental.categories.Category;
+import org.mockito.ArgumentCaptor;
+
+import org.apache.geode.cache.execute.FunctionContext;
+import org.apache.geode.cache.execute.ResultSender;
+import org.apache.geode.connectors.jdbc.internal.ConnectionConfiguration;
+import org.apache.geode.connectors.jdbc.internal.InternalJdbcConnectorService;
+import org.apache.geode.distributed.DistributedMember;
+import org.apache.geode.distributed.DistributedSystem;
+import org.apache.geode.internal.cache.InternalCache;
+import org.apache.geode.management.internal.cli.functions.CliFunctionResult;
+import org.apache.geode.test.junit.categories.UnitTest;
+
+@Category(UnitTest.class)
+public class DescribeConnectionFunctionTest {
+
+  private static final String EXISTING_CONFIG = "existingConfig";
+
+  private DescribeConnectionFunction function;
+  private InternalJdbcConnectorService service;
+  private FunctionContext<String> context;
+  private ConnectionConfiguration connectionConfig;
+  private ResultSender<Object> resultSender;
+
+  @Before
+  public void setUp() {
+    function = new DescribeConnectionFunction();
+
+    InternalCache cache = mock(InternalCache.class);
+    context = mock(FunctionContext.class);
+    service = mock(InternalJdbcConnectorService.class);
+    connectionConfig = mock(ConnectionConfiguration.class);
+    resultSender = mock(ResultSender.class);
+
+    DistributedMember member = mock(DistributedMember.class);
+    DistributedSystem system = mock(DistributedSystem.class);
+
+    when(context.getResultSender()).thenReturn(resultSender);
+    when(context.getCache()).thenReturn(cache);
+    
when(cache.getService(eq(InternalJdbcConnectorService.class))).thenReturn(service);
+    
when(service.getConnectionConfig(EXISTING_CONFIG)).thenReturn(connectionConfig);
+    when(cache.getDistributedSystem()).thenReturn(system);
+    when(system.getDistributedMember()).thenReturn(member);
+  }
+
+  @Test
+  public void isHAReturnsFalse() {
+    assertThat(function.isHA()).isFalse();
+  }
+
+  @Test
+  public void getIdReturnsNameOfClass() {
+    assertThat(function.getId()).isEqualTo(function.getClass().getName());
+  }
+
+  @Test
+  public void serializes() {
+    Serializable original = function;
+    Object copy = SerializationUtils.clone(original);
+    
assertThat(copy).isNotSameAs(original).isInstanceOf(DescribeConnectionFunction.class);
+  }
+
+  @Test
+  public void returnsResultWithCorrectConfig() {
+    when(context.getArguments()).thenReturn(EXISTING_CONFIG);
+
+    function.execute(context);
+
+    ArgumentCaptor<Object> argument = ArgumentCaptor.forClass(Object.class);
+    verify(resultSender, times(1)).lastResult(argument.capture());
+    assertThat(argument.getValue()).isSameAs(connectionConfig);
+  }
+
+  @Test
+  public void returnNullWithNonExistingConfig() {
+    when(context.getArguments()).thenReturn("non existing");
+
+    function.execute(context);
+
+    ArgumentCaptor<Object> argument = ArgumentCaptor.forClass(Object.class);
+    verify(resultSender, times(1)).lastResult(argument.capture());
+    assertThat(argument.getValue()).isNull();
+
+  }
+
+  @Test
+  public void executeReturnsResultForExceptionWithoutMessage() {
+    when(service.getConnectionConfig(any())).thenThrow(new 
NullPointerException());
+
+    function.execute(context);
+
+    ArgumentCaptor<CliFunctionResult> argument = 
ArgumentCaptor.forClass(CliFunctionResult.class);
+    verify(resultSender, times(1)).lastResult(argument.capture());
+    
assertThat(argument.getValue().getMessage()).contains(NullPointerException.class.getName());
+  }
+
+  @Test
+  public void executeReturnsResultForExceptionWithMessage() {
+    when(service.getConnectionConfig(any()))
+        .thenThrow(new IllegalArgumentException("some message"));
+
+    function.execute(context);
+
+    ArgumentCaptor<CliFunctionResult> argument = 
ArgumentCaptor.forClass(CliFunctionResult.class);
+    verify(resultSender, times(1)).lastResult(argument.capture());
+    assertThat(argument.getValue().getMessage()).contains("some message")
+        .doesNotContain(IllegalArgumentException.class.getName());
+  }
+}

-- 
To stop receiving notification emails like this one, please contact
['"[email protected]" <[email protected]>'].

Reply via email to