CreateJndiBindingCommand extends SingleGfshCommand { @Override public boolean updateConfigForGroup(String group, CacheConfig config, Object element) { config.getJndiBindings().add((JndiBindingsType.JndiBinding) element); return true; }  @CliCommand(value = CREATE_JNDIBINDING, help = CREATE_JNDIBINDING__HELP) @CliMetaData(relatedTopic = CliStrings.TOPIC_GEODE_REGION, interceptor = "org.apache.geode.management.internal.cli.commands.UsernamePasswordInterceptor") @ResourceOperation(resource = ResourcePermission.Resource.CLUSTER, operation = ResourcePermission.Operation.MANAGE) ResultModel createJDNIBinding(
      @CliOption(key = BLOCKING_TIMEOUT_SECONDS,
          help = BLOCKING_TIMEOUT_SECONDS__HELP) Integer blockingTimeout,
      @CliOption(key = CONNECTION_POOLED_DATASOURCE_CLASS,
          help = CONNECTION_POOLED_DATASOURCE_CLASS__HELP) String connectionPooledDatasource,
      @CliOption(key = {URL, CONNECTION_URL}, mandatory = true,
          help = CONNECTION_URL__HELP) String connectionUrl,
      @CliOption(key = IDLE_TIMEOUT_SECONDS, help = IDLE_TIMEOUT_SECONDS__HELP) Integer idleTimeout,
      @CliOption(key = INIT_POOL_SIZE, help = INIT_POOL_SIZE__HELP) Integer initPoolSize,
      @CliOption(key = JDBC_DRIVER_CLASS,
          help = JDBC_DRIVER_CLASS__HELP) String jdbcDriver,
      @CliOption(key = JNDI_NAME, mandatory = true, help = JNDI_NAME__HELP) String jndiName,
      @CliOption(key = LOGIN_TIMEOUT_SECONDS,
          help = LOGIN_TIMEOUT_SECONDS__HELP) Integer loginTimeout,
      @CliOption(key = MANAGED_CONN_FACTORY_CLASS,
          help = MANAGED_CONN_FACTORY_CLASS__HELP) String managedConnFactory,
      @CliOption(key = MAX_POOL_SIZE, help = MAX_POOL_SIZE__HELP) Integer maxPoolSize,
      @CliOption(key = USERNAME, help = USERNAME__HELP) String username,
      @CliOption(key = PASSWORD, help = PASSWORD__HELP) String password,
      @CliOption(key = TRANSACTION_TYPE, help = TRANSACTION_TYPE__HELP) String transactionType,
      @CliOption(key = TYPE, unspecifiedDefaultValue = "SIMPLE",
          help = TYPE__HELP) DATASOURCE_TYPE type,
      @CliOption(key = XA_DATASOURCE_CLASS, help = XA_DATASOURCE_CLASS__HELP) String xaDataSource,
      @CliOption(key = CliStrings.IFNOTEXISTS, help = IFNOTEXISTS__HELP,
          specifiedDefaultValue = "true", unspecifiedDefaultValue = "false") boolean ifNotExists,
      @CliOption(key = DATASOURCE_CONFIG_PROPERTIES, optionContext = "splittingRegex=,(?![^{]*\\})",
          help = DATASOURCE_CONFIG_PROPERTIES_HELP) JndiBindingsType.JndiBinding.ConfigProperty[] dsConfigProperties); @Override boolean updateConfigForGroup(String group, CacheConfig config, Object element);  }
@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 + " --type=SIMPLE --name=name --jdbc-driver-class=driver --connection-url=url") .statusIsSuccess().containsOutput("No members found.") .containsOutput("Cluster configuration for group 'cluster' is updated"); verify(clusterConfigService).updateCacheConfig(any(), any()); verify(command).updateConfigForGroup(eq("cluster"), eq(cacheConfig), any()); }
@Test @SuppressWarnings("deprecation") 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 + " --type=SIMPLE --name=name --jdbc-driver-class=driver --connection-url=url --datasource-config-properties={'name':'name1','type':'type1','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()); Object[] actualArguments = arguments.getValue(); JndiBinding jndiConfig = (JndiBinding) actualArguments[0]; boolean creatingDataSource = (Boolean) actualArguments[1]; assertThat(function.getValue()).isInstanceOf(CreateJndiBindingFunction.class); assertThat(creatingDataSource).isFalse(); assertThat(jndiConfig.getJndiName()).isEqualTo("name"); assertThat(jndiConfig.getConfigProperties().get(0).getName()).isEqualTo("name1"); assertThat(targetMembers.getValue()).isEqualTo(members); }