@Test
 public void testSpringAuthenticationProvider() throws Exception {
   MockEnvironment env = new MockEnvironment();
   env.setProperty("shell.auth", "spring");
   this.context = new AnnotationConfigWebApplicationContext();
   this.context.setEnvironment(env);
   this.context.setServletContext(new MockServletContext());
   this.context.register(SecurityConfiguration.class);
   this.context.register(CrshAutoConfiguration.class);
   this.context.refresh();
   PluginLifeCycle lifeCycle = this.context.getBean(PluginLifeCycle.class);
   AuthenticationPlugin<String> authenticationPlugin = null;
   String authentication = lifeCycle.getConfig().getProperty("crash.auth");
   assertThat(authentication).isNotNull();
   for (AuthenticationPlugin plugin :
       lifeCycle.getContext().getPlugins(AuthenticationPlugin.class)) {
     if (authentication.equals(plugin.getName())) {
       authenticationPlugin = plugin;
       break;
     }
   }
   assertThat(
           authenticationPlugin.authenticate(
               SecurityConfiguration.USERNAME, SecurityConfiguration.PASSWORD))
       .isTrue();
   assertThat(
           authenticationPlugin.authenticate(
               UUID.randomUUID().toString(), SecurityConfiguration.PASSWORD))
       .isFalse();
 }
  @Test
  public void testEndpointMBeanExporterWithProperties()
      throws IntrospectionException, InstanceNotFoundException, MalformedObjectNameException,
          ReflectionException {
    MockEnvironment environment = new MockEnvironment();
    environment.setProperty("endpoints.jmx.domain", "test-domain");
    environment.setProperty("endpoints.jmx.unique_names", "true");
    environment.setProperty("endpoints.jmx.static_names", "key1=value1, key2=value2");
    this.context = new AnnotationConfigApplicationContext();
    this.context.setEnvironment(environment);
    this.context.register(
        JmxAutoConfiguration.class,
        EndpointAutoConfiguration.class,
        EndpointMBeanExportAutoConfiguration.class);
    this.context.refresh();
    this.context.getBean(EndpointMBeanExporter.class);

    MBeanExporter mbeanExporter = this.context.getBean(EndpointMBeanExporter.class);

    assertNotNull(
        mbeanExporter
            .getServer()
            .getMBeanInfo(
                ObjectNameManager.getInstance(
                    getObjectName("test-domain", "healthEndpoint", this.context).toString()
                        + ",key1=value1,key2=value2")));
  }
  @Test
  public void testSimpleAuthenticationProvider() throws Exception {
    MockEnvironment env = new MockEnvironment();
    env.setProperty("shell.auth", "simple");
    env.setProperty("shell.auth.simple.username", "user");
    env.setProperty("shell.auth.simple.password", "password");
    this.context = new AnnotationConfigWebApplicationContext();
    this.context.setEnvironment(env);
    this.context.setServletContext(new MockServletContext());
    this.context.register(SecurityConfiguration.class);
    this.context.register(CrshAutoConfiguration.class);
    this.context.refresh();

    PluginLifeCycle lifeCycle = this.context.getBean(PluginLifeCycle.class);
    assertEquals(lifeCycle.getConfig().get("crash.auth"), "simple");

    AuthenticationPlugin<String> authenticationPlugin = null;
    String authentication = lifeCycle.getConfig().getProperty("crash.auth");
    assertNotNull(authentication);
    for (AuthenticationPlugin plugin :
        lifeCycle.getContext().getPlugins(AuthenticationPlugin.class)) {
      if (authentication.equals(plugin.getName())) {
        authenticationPlugin = plugin;
        break;
      }
    }
    assertNotNull(authenticationPlugin);
    assertTrue(authenticationPlugin.authenticate("user", "password"));
    assertFalse(authenticationPlugin.authenticate(UUID.randomUUID().toString(), "password"));
  }
 @Test
 public void testSshConfigurationWithKeyPath() {
   MockEnvironment env = new MockEnvironment();
   env.setProperty("shell.ssh.enabled", "true");
   env.setProperty("shell.ssh.key_path", "~/.ssh/id.pem");
   load(env);
   PluginLifeCycle lifeCycle = this.context.getBean(PluginLifeCycle.class);
   assertThat(lifeCycle.getConfig().getProperty("crash.ssh.keypath")).isEqualTo("~/.ssh/id.pem");
 }
 @Test
 public void testInitializersSeeBoundProperties() {
   MockEnvironment env = new MockEnvironment();
   env.setProperty("bar", "foo");
   this.context = new AnnotationConfigApplicationContext();
   this.context.setEnvironment(env);
   this.context.register(TestConfigurationWithInitializer.class);
   this.context.refresh();
 }
 @Test
 public void testSuccessfulValidationWithJSR303() {
   MockEnvironment env = new MockEnvironment();
   env.setProperty("test.foo", "123456");
   env.setProperty("test.bar", "654321");
   this.context = new AnnotationConfigApplicationContext();
   this.context.setEnvironment(env);
   this.context.register(TestConfigurationWithJSR303.class);
   this.context.refresh();
 }
 @Test
 public void testSshConfigurationCustomTimeouts() {
   MockEnvironment env = new MockEnvironment();
   env.setProperty("shell.ssh.enabled", "true");
   env.setProperty("shell.ssh.auth-timeout", "300000");
   env.setProperty("shell.ssh.idle-timeout", "400000");
   load(env);
   PluginLifeCycle lifeCycle = this.context.getBean(PluginLifeCycle.class);
   assertThat(lifeCycle.getConfig().getProperty("crash.ssh.auth_timeout")).isEqualTo("300000");
   assertThat(lifeCycle.getConfig().getProperty("crash.ssh.idle_timeout")).isEqualTo("400000");
 }
  @Test
  public void testSshConfigurationWithKeyPath() {
    MockEnvironment env = new MockEnvironment();
    env.setProperty("shell.ssh.enabled", "true");
    env.setProperty("shell.ssh.key_path", "~/.ssh/id.pem");
    this.context = new AnnotationConfigWebApplicationContext();
    this.context.setEnvironment(env);
    this.context.register(CrshAutoConfiguration.class);
    this.context.refresh();

    PluginLifeCycle lifeCycle = this.context.getBean(PluginLifeCycle.class);

    assertEquals(lifeCycle.getConfig().getProperty("crash.ssh.keypath"), "~/.ssh/id.pem");
  }
 @Test(expected = NoSuchBeanDefinitionException.class)
 public void testEndpointMBeanExporterIsNotInstalled() {
   MockEnvironment environment = new MockEnvironment();
   environment.setProperty("endpoints.jmx.enabled", "false");
   this.context = new AnnotationConfigApplicationContext();
   this.context.setEnvironment(environment);
   this.context.register(
       JmxAutoConfiguration.class,
       EndpointAutoConfiguration.class,
       EndpointMBeanExportAutoConfiguration.class);
   this.context.refresh();
   this.context.getBean(EndpointMBeanExporter.class);
   fail();
 }
 @Test
 public void testKeyAuthenticationProvider() {
   MockEnvironment env = new MockEnvironment();
   env.setProperty("shell.auth", "key");
   env.setProperty("shell.auth.key.path", "~/test.pem");
   this.context = new AnnotationConfigWebApplicationContext();
   this.context.setEnvironment(env);
   this.context.setServletContext(new MockServletContext());
   this.context.register(SecurityConfiguration.class);
   this.context.register(CrshAutoConfiguration.class);
   this.context.refresh();
   PluginLifeCycle lifeCycle = this.context.getBean(PluginLifeCycle.class);
   assertThat(lifeCycle.getConfig().get("crash.auth")).isEqualTo("key");
   assertThat(lifeCycle.getConfig().get("crash.auth.key.path")).isEqualTo("~/test.pem");
 }
  @Test
  public void testJaasAuthenticationProvider() {
    MockEnvironment env = new MockEnvironment();
    env.setProperty("shell.auth", "jaas");
    env.setProperty("shell.auth.jaas.domain", "my-test-domain");
    this.context = new AnnotationConfigWebApplicationContext();
    this.context.setEnvironment(env);
    this.context.setServletContext(new MockServletContext());
    this.context.register(SecurityConfiguration.class);
    this.context.register(CrshAutoConfiguration.class);
    this.context.refresh();

    PluginLifeCycle lifeCycle = this.context.getBean(PluginLifeCycle.class);
    assertEquals(lifeCycle.getConfig().get("crash.auth"), "jaas");
    assertEquals(lifeCycle.getConfig().get("crash.auth.jaas.domain"), "my-test-domain");
  }
  @Test
  public void testDisabledPlugins() throws Exception {
    MockEnvironment env = new MockEnvironment();
    env.setProperty(
        "shell.disabled_plugins", "GroovyREPL, termIOHandler, org.crsh.auth.AuthenticationPlugin");
    this.context = new AnnotationConfigWebApplicationContext();
    this.context.setEnvironment(env);
    this.context.register(CrshAutoConfiguration.class);
    this.context.refresh();

    PluginLifeCycle lifeCycle = this.context.getBean(PluginLifeCycle.class);
    assertNotNull(lifeCycle);

    assertNull(lifeCycle.getContext().getPlugin(GroovyREPL.class));
    assertNull(lifeCycle.getContext().getPlugin(ProcessorIOHandler.class));
    assertNull(lifeCycle.getContext().getPlugin(JaasAuthenticationPlugin.class));
  }
 @Test
 public void testValidationWithCustomValidatorNotSupported() {
   MockEnvironment env = new MockEnvironment();
   env.setProperty("test.foo", "bar");
   this.context = new AnnotationConfigApplicationContext();
   this.context.setEnvironment(env);
   this.context.register(
       TestConfigurationWithCustomValidator.class, PropertyWithValidatingSetter.class);
   try {
     // PropertyWithValidatingSetter should not use validator
     this.context.refresh();
     fail("Expected exception");
   } catch (BeanCreationException ex) {
     BindException bex = (BindException) ex.getRootCause();
     assertEquals(1, bex.getErrorCount());
   }
 }
 @Test
 public void testDisabledPlugins() throws Exception {
   MockEnvironment env = new MockEnvironment();
   env.setProperty(
       "shell.disabled_plugins",
       "termIOHandler, org.crsh.auth.AuthenticationPlugin, javaLanguage");
   load(env);
   PluginLifeCycle lifeCycle = this.context.getBean(PluginLifeCycle.class);
   assertThat(lifeCycle).isNotNull();
   assertThat(lifeCycle.getContext().getPlugins(TermIOHandler.class))
       .filteredOn(Matched.<TermIOHandler>when(isA(ProcessorIOHandler.class)))
       .isEmpty();
   assertThat(lifeCycle.getContext().getPlugins(AuthenticationPlugin.class))
       .filteredOn(Matched.<AuthenticationPlugin>when(isA(JaasAuthenticationPlugin.class)))
       .isEmpty();
   assertThat(lifeCycle.getContext().getPlugins(Language.class))
       .filteredOn(Matched.<Language>when(isA(JavaLanguage.class)))
       .isEmpty();
 }
  @Test
  public void testConfiguration() throws Exception {
    env.setProperty("cloud.aws.cloudwatch.namespace", "test");

    context.register(CloudWatchMetricAutoConfiguration.class);
    context.refresh();

    CloudWatchMetricWriter cloudWatchMetricWriter = context.getBean(CloudWatchMetricWriter.class);
    assertNotNull(cloudWatchMetricWriter);

    BufferingCloudWatchMetricSender cloudWatchMetricSender =
        context.getBean(BufferingCloudWatchMetricSender.class);
    assertNotNull(cloudWatchMetricSender);

    CloudWatchMetricProperties cloudWatchMetricProperties =
        context.getBean(CloudWatchMetricProperties.class);
    assertNotNull(cloudWatchMetricProperties);

    assertEquals(cloudWatchMetricSender.getNamespace(), cloudWatchMetricProperties.getNamespace());
    assertEquals(cloudWatchMetricSender.getMaxBuffer(), cloudWatchMetricProperties.getMaxBuffer());
    assertEquals(
        cloudWatchMetricSender.getNextRunDelayMillis(),
        cloudWatchMetricProperties.getNextRunDelayMillis());
  }