EnvironmentFactory { public Map<String, Object> create(final JmxParameters jmxParameters) { final Optional<Credentials> credentialsOptional = jmxParameters.getCredentials(); if (credentialsOptional.isPresent()) { final Credentials credentials = credentialsOptional.get(); final String username = credentials.getUsername(); final String password = credentials.getPassword(); final Map<String, Object> environment = new HashMap<>(); environment.put(CREDENTIALS, new String[] { username, password }); return environment; } return EMPTY_MAP; }  Map<String, Object> create(final JmxParameters jmxParameters);  }
@Test public void shouldCreateEnvironmentWithCredentialsIfPresent() throws Exception { final String username = "Fred"; final String password = "Password123"; final JmxParameters jmxParameters = jmxParameters() .withHost("host") .withPort(928134) .withUsername(username) .withPassword(password) .build(); final Map<String, Object> environment = environmentFactory.create(jmxParameters); assertThat(environment.size(), is(1)); final String[] credentialsArray = (String[]) environment.get(CREDENTIALS); assertThat(credentialsArray.length, is(2)); assertThat(credentialsArray[0], is(username)); assertThat(credentialsArray[1], is(password)); }
@Test public void shouldCreateEmptyEnvironmentIfCredentialsNotPresent() throws Exception { final JmxParameters jmxParameters = jmxParameters() .withHost("host") .withPort(928134) .build(); assertThat(jmxParameters.getCredentials().isPresent(), is(false)); final Map<String, Object> environment = environmentFactory.create(jmxParameters); assertThat(environment.isEmpty(), is(true)); }