JAMES-1896 Tests should wait for authentication response

Project: http://git-wip-us.apache.org/repos/asf/james-project/repo
Commit: http://git-wip-us.apache.org/repos/asf/james-project/commit/9bc0442e
Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/9bc0442e
Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/9bc0442e

Branch: refs/heads/master
Commit: 9bc0442eaf4d509a96d2111ed4423f6d0167173e
Parents: db703cd
Author: Antoine Duprat <[email protected]>
Authored: Wed Dec 21 16:16:10 2016 +0100
Committer: Antoine Duprat <[email protected]>
Committed: Fri Dec 23 11:10:04 2016 +0100

----------------------------------------------------------------------
 server/pom.xml                                  |  4 +--
 .../james/jmap/HttpJmapAuthentication.java      | 29 ++++++++++++++++----
 2 files changed, 26 insertions(+), 7 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/james-project/blob/9bc0442e/server/pom.xml
----------------------------------------------------------------------
diff --git a/server/pom.xml b/server/pom.xml
index 16d3952..8065656 100644
--- a/server/pom.xml
+++ b/server/pom.xml
@@ -949,7 +949,7 @@
                 <artifactId>commons-collections4</artifactId>
                 <version>4.0</version>
             </dependency>
-           <dependency>
+            <dependency>
                 <groupId>org.hamcrest</groupId>
                 <artifactId>java-hamcrest</artifactId>
                 <version>2.0.0.0</version>
@@ -1508,7 +1508,7 @@
             <dependency>
                 <groupId>com.jayway.awaitility</groupId>
                 <artifactId>awaitility</artifactId>
-                <version>1.6.3</version>
+                <version>1.6.5</version>
             </dependency>
             <dependency>
                 <groupId>com.jayway.jsonpath</groupId>

http://git-wip-us.apache.org/repos/asf/james-project/blob/9bc0442e/server/protocols/jmap-integration-testing/jmap-integration-testing-common/src/test/java/org/apache/james/jmap/HttpJmapAuthentication.java
----------------------------------------------------------------------
diff --git 
a/server/protocols/jmap-integration-testing/jmap-integration-testing-common/src/test/java/org/apache/james/jmap/HttpJmapAuthentication.java
 
b/server/protocols/jmap-integration-testing/jmap-integration-testing-common/src/test/java/org/apache/james/jmap/HttpJmapAuthentication.java
index 07ad607..06bc960 100644
--- 
a/server/protocols/jmap-integration-testing/jmap-integration-testing-common/src/test/java/org/apache/james/jmap/HttpJmapAuthentication.java
+++ 
b/server/protocols/jmap-integration-testing/jmap-integration-testing-common/src/test/java/org/apache/james/jmap/HttpJmapAuthentication.java
@@ -21,6 +21,7 @@ package org.apache.james.jmap;
 
 import java.io.IOException;
 import java.net.URISyntaxException;
+import java.util.concurrent.TimeUnit;
 
 import org.apache.http.client.ClientProtocolException;
 import org.apache.http.client.fluent.Request;
@@ -28,23 +29,40 @@ import org.apache.http.client.fluent.Response;
 import org.apache.http.client.utils.URIBuilder;
 import org.apache.http.entity.ContentType;
 import org.apache.james.jmap.api.access.AccessToken;
+import org.hamcrest.core.IsAnything;
 
+import com.jayway.awaitility.Awaitility;
+import com.jayway.awaitility.Duration;
+import com.jayway.awaitility.core.ConditionFactory;
 import com.jayway.jsonpath.JsonPath;
 
 public class HttpJmapAuthentication {
 
+    private static final ConditionFactory CALMLY_AWAIT = Awaitility.with()
+            .pollInterval(Duration.FIVE_HUNDRED_MILLISECONDS)
+            .and().with()
+            .pollDelay(Duration.ONE_HUNDRED_MILLISECONDS)
+            .await();
+
     public static AccessToken authenticateJamesUser(URIBuilder uriBuilder, 
String username, String password) throws ClientProtocolException, IOException, 
URISyntaxException {
         String continuationToken = getContinuationToken(uriBuilder, username);
 
-        Response response = 
Request.Post(uriBuilder.setPath("/authentication").build())
+        Response response = CALMLY_AWAIT
+                .atMost(30, TimeUnit.SECONDS)
+                .ignoreExceptions()
+                .until(() -> postAuthenticate(uriBuilder, password, 
continuationToken), IsAnything.anything());
+
+        return AccessToken.fromString(
+                    JsonPath.parse(response.returnContent().asString())
+                    .read("accessToken"));
+    }
+
+    private static Response postAuthenticate(URIBuilder uriBuilder, String 
password, String continuationToken) throws ClientProtocolException, 
IOException, URISyntaxException {
+        return Request.Post(uriBuilder.setPath("/authentication").build())
                 .bodyString("{\"token\": \"" + continuationToken + "\", 
\"method\": \"password\", \"password\": \"" + password + "\"}", 
                         ContentType.APPLICATION_JSON)
                 .setHeader("Accept", 
ContentType.APPLICATION_JSON.getMimeType())
                 .execute();
-        
-        return AccessToken.fromString(
-                    JsonPath.parse(response.returnContent().asString())
-                    .read("accessToken"));
     }
 
     private static String getContinuationToken(URIBuilder uriBuilder, String 
username) throws ClientProtocolException, IOException, URISyntaxException {
@@ -56,4 +74,5 @@ public class HttpJmapAuthentication {
         return JsonPath.parse(response.returnContent().asString())
             .read("continuationToken");
     }
+
 }


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to