CreateDataSourceCommand extends SingleGfshCommand { @Override public boolean updateConfigForGroup(String group, CacheConfig config, Object element) { config.getJndiBindings().add((JndiBindingsType.JndiBinding) element); return true; }  @CliCommand(value = CREATE_DATA_SOURCE, help = CREATE_DATA_SOURCE__HELP) @CliMetaData(relatedTopic = CliStrings.DEFAULT_TOPIC_GEODE, interceptor = "org.apache.geode.connectors.jdbc.internal.cli.CreateDataSourceInterceptor") @ResourceOperation(resource = ResourcePermission.Resource.CLUSTER, operation = ResourcePermission.Operation.MANAGE) ResultModel createDataSource(
      @CliOption(key = POOLED_DATA_SOURCE_FACTORY_CLASS,
          help = POOLED_DATA_SOURCE_FACTORY_CLASS__HELP) String pooledDataSourceFactoryClass,
      @CliOption(key = URL, mandatory = true,
          help = URL__HELP) String url,
      @CliOption(key = NAME, mandatory = true, help = NAME__HELP) String name,
      @CliOption(key = USERNAME, help = USERNAME__HELP) String username,
      @CliOption(key = PASSWORD, help = PASSWORD__HELP) String password,
      @CliOption(key = CliStrings.IFNOTEXISTS, help = IFNOTEXISTS__HELP,
          specifiedDefaultValue = "true", unspecifiedDefaultValue = "false") boolean ifNotExists,
      @CliOption(key = POOLED, help = POOLED__HELP,
          specifiedDefaultValue = "true", unspecifiedDefaultValue = "true") boolean pooled,
      @CliOption(key = JDBC_DRIVER_CLASS,
          help = JDBC_DRIVER_CLASS__HELP) String jdbcDriver,
      @CliOption(key = POOL_PROPERTIES, optionContext = "splittingRegex=,(?![^{]*\\})",
          help = POOL_PROPERTIES_HELP) PoolProperty[] poolProperties); @Override boolean updateConfigForGroup(String group, CacheConfig config, Object element); @CliAvailabilityIndicator({CREATE_DATA_SOURCE}) boolean commandAvailable();  }
@Test public void whenNoMembersFoundAndClusterConfigRunningThenUpdateClusterConfig() { InternalConfigurationPersistenceService clusterConfigService = mock(InternalConfigurationPersistenceService.class); CacheConfig cacheConfig = mock(CacheConfig.class); doReturn(Collections.emptySet()).when(command).findMembers(any(), any()); doReturn(clusterConfigService).when(command).getConfigurationPersistenceService(); doReturn(cacheConfig).when(clusterConfigService).getCacheConfig(any()); doAnswer(invocation -> { UnaryOperator<CacheConfig> mutator = invocation.getArgument(1); mutator.apply(cacheConfig); return null; }).when(clusterConfigService).updateCacheConfig(any(), any()); gfsh.executeAndAssertThat(command, COMMAND + " --name=name --url=url") .statusIsSuccess() .containsOutput("No members found, data source saved to cluster configuration.") .containsOutput("Cluster configuration for group 'cluster' is updated"); verify(clusterConfigService).updateCacheConfig(any(), any()); verify(command).updateConfigForGroup(eq("cluster"), eq(cacheConfig), isNotNull()); }
@SuppressWarnings("deprecation") @Test public void whenMembersFoundAndClusterConfigRunningThenUpdateClusterConfigAndInvokeFunction() { Set<DistributedMember> members = new HashSet<>(); members.add(mock(DistributedMember.class)); CliFunctionResult result = new CliFunctionResult("server1", true, "Tried creating jndi binding \"name\" on \"server1\""); List<CliFunctionResult> results = new ArrayList<>(); results.add(result); InternalConfigurationPersistenceService clusterConfigService = mock(InternalConfigurationPersistenceService.class); CacheConfig cacheConfig = mock(CacheConfig.class); doReturn(members).when(command).findMembers(any(), any()); doReturn(clusterConfigService).when(command).getConfigurationPersistenceService(); doReturn(results).when(command).executeAndGetFunctionResult(any(), any(), any()); doReturn(cacheConfig).when(clusterConfigService).getCacheConfig(any()); doAnswer(invocation -> { UnaryOperator<CacheConfig> mutator = invocation.getArgument(1); mutator.apply(cacheConfig); return null; }).when(clusterConfigService).updateCacheConfig(any(), any()); gfsh.executeAndAssertThat(command, COMMAND + " --pooled --name=name --url=url --pool-properties={'name':'name1','value':'value1'}") .statusIsSuccess().tableHasColumnOnlyWithValues("Member", "server1") .tableHasColumnOnlyWithValues("Status", "OK").tableHasColumnOnlyWithValues("Message", "Tried creating jndi binding \"name\" on \"server1\""); verify(clusterConfigService).updateCacheConfig(any(), any()); verify(command).updateConfigForGroup(eq("cluster"), eq(cacheConfig), any()); ArgumentCaptor<CreateJndiBindingFunction> function = ArgumentCaptor.forClass(CreateJndiBindingFunction.class); ArgumentCaptor<Object[]> arguments = ArgumentCaptor.forClass(Object[].class); @SuppressWarnings("unchecked") ArgumentCaptor<Set<DistributedMember>> targetMembers = ArgumentCaptor.forClass(Set.class); verify(command, times(1)).executeAndGetFunctionResult(function.capture(), arguments.capture(), targetMembers.capture()); assertThat(function.getValue()).isInstanceOf(CreateJndiBindingFunction.class); assertThat(arguments.getValue()).isNotNull(); Object[] actualArguments = arguments.getValue(); JndiBinding jndiConfig = (JndiBinding) actualArguments[0]; boolean creatingDataSource = (Boolean) actualArguments[1]; assertThat(function.getValue()).isInstanceOf(CreateJndiBindingFunction.class); assertThat(creatingDataSource).isTrue(); assertThat(jndiConfig.getJndiName()).isEqualTo("name"); assertThat(jndiConfig.getConfigProperties().get(0).getName()).isEqualTo("name1"); assertThat(targetMembers.getValue()).isEqualTo(members); }