### Question:
MavenSettingsServerCredentials implements InferredAuthProvider { @Override public Optional<AuthProperty> inferAuth(String registry) throws InferredAuthException { Server server = getServerFromMavenSettings(registry); if (server == null) { return Optional.empty(); } SettingsDecryptionRequest request = new DefaultSettingsDecryptionRequest(server); SettingsDecryptionResult result = decrypter.decrypt(request); for (SettingsProblem problem : result.getProblems()) { if (problem.getSeverity() == SettingsProblem.Severity.ERROR || problem.getSeverity() == SettingsProblem.Severity.FATAL) { throw new InferredAuthException( "Unable to decrypt server(" + registry + ") info from settings.xml: " + problem); } } Server resultServer = result.getServer(); String username = resultServer.getUsername(); String password = resultServer.getPassword(); return Optional.of( new AuthProperty() { @Override public String getUsername() { return username; } @Override public String getPassword() { return password; } @Override public String getAuthDescriptor() { return CREDENTIAL_SOURCE; } @Override public String getUsernameDescriptor() { return CREDENTIAL_SOURCE; } @Override public String getPasswordDescriptor() { return CREDENTIAL_SOURCE; } }); } MavenSettingsServerCredentials(Settings settings, SettingsDecrypter decrypter); @Override Optional<AuthProperty> inferAuth(String registry);  }### Answer:
@Test public void testInferredAuth_decrypterFailure() { try { mavenSettingsServerCredentials.inferAuth("badServer"); Assert.fail(); } catch (InferredAuthException ex) { MatcherAssert.assertThat( ex.getMessage(), CoreMatchers.startsWith("Unable to decrypt server(badServer) info from settings.xml:")); } }
@Test public void testInferredAuth_successEncrypted() throws InferredAuthException { Optional<AuthProperty> auth = mavenSettingsServerCredentials.inferAuth("encryptedServer"); Assert.assertTrue(auth.isPresent()); Assert.assertEquals("encryptedUser", auth.get().getUsername()); Assert.assertEquals("password1", auth.get().getPassword()); }
@Test public void testInferredAuth_successUnencrypted() throws InferredAuthException { Optional<AuthProperty> auth = mavenSettingsServerCredentials.inferAuth("simpleServer"); Assert.assertTrue(auth.isPresent()); Assert.assertEquals("simpleUser", auth.get().getUsername()); Assert.assertEquals("password2", auth.get().getPassword()); }
@Test public void testInferredAuth_successNoPasswordDoesNotBlowUp() throws InferredAuthException { Optional<AuthProperty> auth = mavenSettingsServerCredentialsNoMasterPassword.inferAuth("simpleServer"); Assert.assertTrue(auth.isPresent()); Assert.assertEquals("simpleUser", auth.get().getUsername()); Assert.assertEquals("password2", auth.get().getPassword()); }
@Test public void testInferredAuth_registryWithHostAndPort() throws InferredAuthException { Optional<AuthProperty> auth = mavenSettingsServerCredentialsNoMasterPassword.inferAuth("docker.example.com:8080"); Assert.assertTrue(auth.isPresent()); Assert.assertEquals("registryUser", auth.get().getUsername()); Assert.assertEquals("registryPassword", auth.get().getPassword()); }
@Test public void testInferredAuth_registryWithHostWithoutPort() throws InferredAuthException { Optional<AuthProperty> auth = mavenSettingsServerCredentialsNoMasterPassword.inferAuth("docker.example.com"); Assert.assertTrue(auth.isPresent()); Assert.assertEquals("registryUser", auth.get().getUsername()); Assert.assertEquals("registryPassword", auth.get().getPassword()); }
@Test public void testInferredAuth_registrySettingsWithPort() throws InferredAuthException { Optional<AuthProperty> auth = mavenSettingsServerCredentialsNoMasterPassword.inferAuth("docker.example.com:5432"); Assert.assertTrue(auth.isPresent()); Assert.assertEquals("registryUser", auth.get().getUsername()); Assert.assertEquals("registryPassword", auth.get().getPassword()); }
@Test public void testInferredAuth_notFound() throws InferredAuthException { Assert.assertFalse(mavenSettingsServerCredentials.inferAuth("serverUnknown").isPresent()); }