[MCHANGES-379] [REGRESSION] Authentication does not work after Upgrade

Using built-in SettingsDecrypter component to decrypt the password in the settings.

git-svn-id: https://svn.apache.org/repos/asf/maven/plugins/trunk@1784412 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/pom.xml b/pom.xml
index e38b94c..6211a27 100644
--- a/pom.xml
+++ b/pom.xml
@@ -382,6 +382,12 @@
       <scope>test</scope>
     </dependency>
     <dependency>
+      <groupId>org.mockito</groupId>
+      <artifactId>mockito-core</artifactId>
+      <version>1.9.5</version>
+      <scope>test</scope>
+    </dependency>
+    <dependency>
       <groupId>org.apache.maven.plugin-testing</groupId>
       <artifactId>maven-plugin-testing-harness</artifactId>
       <version>2.1</version>
diff --git a/src/main/java/org/apache/maven/plugins/announcement/AnnouncementMojo.java b/src/main/java/org/apache/maven/plugins/announcement/AnnouncementMojo.java
index 141405a..ef7b4c5 100644
--- a/src/main/java/org/apache/maven/plugins/announcement/AnnouncementMojo.java
+++ b/src/main/java/org/apache/maven/plugins/announcement/AnnouncementMojo.java
@@ -49,6 +49,7 @@
 import org.apache.maven.plugins.trac.TracIssueManagmentSystem;
 import org.apache.maven.project.MavenProject;
 import org.apache.maven.settings.Settings;
+import org.apache.maven.settings.crypto.SettingsDecrypter;
 import org.apache.velocity.Template;
 import org.apache.velocity.app.VelocityEngine;
 import org.apache.velocity.context.Context;
@@ -235,6 +236,12 @@
     private VelocityComponent velocity;
 
     /**
+     * Component used to decrypt server information.
+     */
+    @Component
+    private SettingsDecrypter settingsDecrypter;
+
+    /**
      * Version of the artifact.
      */
     @Parameter( property = "changes.version", defaultValue = "${project.version}", required = true )
@@ -845,7 +852,7 @@
             GitHubDownloader issueDownloader =
                 new GitHubDownloader( project, githubAPIScheme, githubAPIPort, includeOpenIssues, true );
 
-            issueDownloader.configureAuthentication( githubAPIServerId, settings, getLog() );
+            issueDownloader.configureAuthentication( settingsDecrypter, githubAPIServerId, settings, getLog() );
 
             return getReleases( issueDownloader.getIssueList(), new GitHubIssueManagementSystem() );
         }
diff --git a/src/main/java/org/apache/maven/plugins/github/GitHubDownloader.java b/src/main/java/org/apache/maven/plugins/github/GitHubDownloader.java
index a6d0dd4..9f7c57b 100644
--- a/src/main/java/org/apache/maven/plugins/github/GitHubDownloader.java
+++ b/src/main/java/org/apache/maven/plugins/github/GitHubDownloader.java
@@ -24,7 +24,10 @@
 import org.apache.maven.project.MavenProject;
 import org.apache.maven.settings.Server;
 import org.apache.maven.settings.Settings;
-
+import org.apache.maven.settings.building.SettingsProblem;
+import org.apache.maven.settings.crypto.DefaultSettingsDecryptionRequest;
+import org.apache.maven.settings.crypto.SettingsDecrypter;
+import org.apache.maven.settings.crypto.SettingsDecryptionResult;
 import org.eclipse.egit.github.core.Label;
 import org.eclipse.egit.github.core.client.GitHubClient;
 import org.eclipse.egit.github.core.service.IssueService;
@@ -212,7 +215,8 @@
         return issueList;
     }
 
-    public void configureAuthentication( String githubAPIServerId, Settings settings, Log log )
+    public void configureAuthentication( SettingsDecrypter decrypter, String githubAPIServerId, Settings settings,
+                                         Log log )
     {
         boolean configured = false;
 
@@ -222,6 +226,12 @@
         {
             if ( server.getId().equals( githubAPIServerId ) )
             {
+                SettingsDecryptionResult result = decrypter.decrypt( new DefaultSettingsDecryptionRequest( server ) );
+                for ( SettingsProblem problem : result.getProblems() )
+                {
+                    log.error( problem.getMessage(), problem.getException() );
+                }
+                server = result.getServer();
                 String user = server.getUsername();
                 String password = server.getPassword();
                 this.client.setCredentials( user, password );
diff --git a/src/main/java/org/apache/maven/plugins/github/GitHubMojo.java b/src/main/java/org/apache/maven/plugins/github/GitHubMojo.java
index 7f4c89e..caa5cbf 100644
--- a/src/main/java/org/apache/maven/plugins/github/GitHubMojo.java
+++ b/src/main/java/org/apache/maven/plugins/github/GitHubMojo.java
@@ -26,6 +26,7 @@
 import java.util.Map;
 import java.util.ResourceBundle;
 
+import org.apache.maven.plugins.annotations.Component;
 import org.apache.maven.plugins.annotations.Mojo;
 import org.apache.maven.plugins.annotations.Parameter;
 import org.apache.maven.plugins.changes.AbstractChangesReport;
@@ -36,6 +37,7 @@
 import org.apache.maven.plugins.issues.IssuesReportHelper;
 import org.apache.maven.reporting.MavenReportException;
 import org.apache.maven.settings.Settings;
+import org.apache.maven.settings.crypto.SettingsDecrypter;
 
 /**
  * Goal which downloads issues from GitHub and generates a report.
@@ -67,6 +69,12 @@
     }
 
     /**
+     * Component used to decrypt server information.
+     */
+    @Component
+    private SettingsDecrypter settingsDecrypter;
+
+    /**
      * Sets the column names that you want to show in the report. The columns will appear in the report in the same
      * order as you specify them here. Multiple values can be separated by commas.
      * <p>
@@ -179,7 +187,7 @@
             GitHubDownloader issueDownloader =
                 new GitHubDownloader( project, githubAPIScheme, githubAPIPort, includeOpenIssues, onlyMilestoneIssues );
 
-            issueDownloader.configureAuthentication( githubAPIServerId, settings, getLog() );
+            issueDownloader.configureAuthentication( settingsDecrypter, githubAPIServerId, settings, getLog() );
 
             List<Issue> issueList = issueDownloader.getIssueList();
 
diff --git a/src/test/java/org/apache/maven/plugins/github/GitHubDownloaderTestCase.java b/src/test/java/org/apache/maven/plugins/github/GitHubDownloaderTestCase.java
index 42ce82d..38858b5 100644
--- a/src/test/java/org/apache/maven/plugins/github/GitHubDownloaderTestCase.java
+++ b/src/test/java/org/apache/maven/plugins/github/GitHubDownloaderTestCase.java
@@ -1,26 +1,52 @@
-/*
- * Licensed to Elasticsearch under one or more contributor license agreements. See the NOTICE file distributed with this
- * work for additional information regarding copyright ownership. Elasticsearch licenses this file to you under the
- * Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may
- * obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
- * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations under the License.
- */
-
 package org.apache.maven.plugins.github;
 
-import junit.framework.TestCase;
-import org.apache.maven.model.IssueManagement;
-import org.apache.maven.plugins.github.GitHubDownloader;
-import org.apache.maven.plugins.issues.Issue;
-import org.apache.maven.project.MavenProject;
-import org.eclipse.egit.github.core.User;
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.any;
 
 import java.io.IOException;
+import java.net.MalformedURLException;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.List;
+
+import org.apache.maven.model.IssueManagement;
+import org.apache.maven.plugin.logging.Log;
+import org.apache.maven.plugins.issues.Issue;
+import org.apache.maven.project.MavenProject;
+import org.apache.maven.settings.Server;
+import org.apache.maven.settings.Settings;
+import org.apache.maven.settings.building.DefaultSettingsProblem;
+import org.apache.maven.settings.building.SettingsProblem;
+import org.apache.maven.settings.building.SettingsProblem.Severity;
+import org.apache.maven.settings.crypto.DefaultSettingsDecryptionRequest;
+import org.apache.maven.settings.crypto.SettingsDecrypter;
+import org.apache.maven.settings.crypto.SettingsDecryptionRequest;
+import org.apache.maven.settings.crypto.SettingsDecryptionResult;
+import org.eclipse.egit.github.core.User;
+import org.mockito.ArgumentCaptor;
+
+import junit.framework.TestCase;
 
 public class GitHubDownloaderTestCase
     extends TestCase
@@ -29,14 +55,8 @@
     public void testCreateIssue()
         throws IOException
     {
-
-        MavenProject mavenProject = new MavenProject();
-        IssueManagement issueManagement = new IssueManagement();
-        issueManagement.setSystem( "GitHub" );
-        issueManagement.setUrl( "https://github.com/dadoonet/spring-elasticsearch/issues/" );
-        mavenProject.setIssueManagement( issueManagement );
-
-        GitHubDownloader gitHubDownloader = new GitHubDownloader( mavenProject, "https", 80, true, false );
+        IssueManagement issueManagement = newGitHubIssueManagement();
+        GitHubDownloader gitHubDownloader = newGitHubDownloader( issueManagement );
 
         org.eclipse.egit.github.core.Issue githubIssue = new org.eclipse.egit.github.core.Issue();
         githubIssue.setNumber( 1 );
@@ -53,4 +73,76 @@
         assertEquals( githubIssue.getTitle(), issue.getSummary() );
         assertEquals( issueManagement.getUrl() + githubIssue.getNumber(), issue.getLink() );
     }
+
+    public void testConfigureAuthenticationWithProblems()
+        throws Exception
+    {
+        IssueManagement issueManagement = newGitHubIssueManagement();
+        GitHubDownloader gitHubDownloader = newGitHubDownloader( issueManagement );
+        Settings settings = new Settings();
+        Server server = newServer( "github-server" );
+        settings.addServer( server );
+        SettingsDecrypter decrypter = mock( SettingsDecrypter.class );
+        SettingsDecryptionResult result = mock( SettingsDecryptionResult.class );
+        Log log = mock( Log.class );
+        when( result.getProblems() ).thenReturn( Arrays.<SettingsProblem>asList( new DefaultSettingsProblem( "Ups "
+            + server.getId(), Severity.ERROR, null, -1, -1, null ) ) );
+        when( result.getServer() ).thenReturn( server );
+        when( decrypter.decrypt( any( SettingsDecryptionRequest.class ) ) ).thenReturn( result );
+
+        gitHubDownloader.configureAuthentication( decrypter, "github-server", settings, log );
+
+        verify( log ).error( "Ups github-server", null );
+        ArgumentCaptor<SettingsDecryptionRequest> argument = ArgumentCaptor.forClass( SettingsDecryptionRequest.class );
+        verify( decrypter ).decrypt( argument.capture() );
+        List<Server> servers = ( (DefaultSettingsDecryptionRequest) argument.getValue() ).getServers();
+        assertEquals( 1, servers.size() );
+        assertSame( server, servers.get( 0 ) );
+    }
+
+    public void testConfigureAuthenticationWithNoServer()
+        throws Exception
+    {
+        IssueManagement issueManagement = newGitHubIssueManagement();
+        GitHubDownloader gitHubDownloader = newGitHubDownloader( issueManagement );
+        Settings settings = new Settings();
+        Server server = newServer( "not-the-right-one" );
+        settings.addServer( server );
+        SettingsDecrypter decrypter = mock( SettingsDecrypter.class );
+        SettingsDecryptionResult result = mock( SettingsDecryptionResult.class );
+        Log log = mock( Log.class );
+        when( result.getProblems() ).thenReturn( Collections.<SettingsProblem>emptyList() );
+        when( result.getServer() ).thenReturn( server );
+        when( decrypter.decrypt( new DefaultSettingsDecryptionRequest( server ) ) ).thenReturn( result );
+
+        gitHubDownloader.configureAuthentication( decrypter, "github-server", settings, log );
+
+        verify( log ).warn( "Can't find server id [github-server] configured in githubAPIServerId." );
+    }
+
+    private Server newServer( String id )
+    {
+        Server server = new Server();
+        server.setId( id );
+        server.setUsername( "some-user" );
+        server.setPassword( "Sup3rSecret" );
+        return server;
+    }
+
+    private GitHubDownloader newGitHubDownloader( IssueManagement issueManagement )
+        throws MalformedURLException
+    {
+        MavenProject mavenProject = new MavenProject();
+        mavenProject.setIssueManagement( issueManagement );
+        return new GitHubDownloader( mavenProject, "https", 80, true, false );
+    }
+
+    private IssueManagement newGitHubIssueManagement()
+    {
+        IssueManagement issueManagement = new IssueManagement();
+        issueManagement.setSystem( "GitHub" );
+        issueManagement.setUrl( "https://github.com/dadoonet/spring-elasticsearch/issues/" );
+        return issueManagement;
+    }
+
 }