You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by or...@apache.org on 2022/05/31 12:04:33 UTC
[camel] branch main updated: (chores) camel-netty-http: fix test execution on Github actions
This is an automated email from the ASF dual-hosted git repository.
orpiske pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel.git
The following commit(s) were added to refs/heads/main by this push:
new 2c8366c4bd7 (chores) camel-netty-http: fix test execution on Github actions
2c8366c4bd7 is described below
commit 2c8366c4bd7a76c065a181b1e46ae36831fe0779
Author: Otavio Rodolfo Piske <an...@gmail.com>
AuthorDate: Tue May 31 09:59:12 2022 +0200
(chores) camel-netty-http: fix test execution on Github actions
---
components/camel-netty-http/pom.xml | 5 ++
...tyHttpAccessHttpRequestAndResponseBeanTest.java | 7 ++-
...ttyHttpSimpleBasicAuthConstraintMapperTest.java | 54 ++++++++++++++++------
.../netty/http/NettyHttpSimpleBasicAuthTest.java | 50 ++++++++++++--------
.../http/NettyHttpSimplePooledExchangeTest.java | 19 +++++++-
5 files changed, 99 insertions(+), 36 deletions(-)
diff --git a/components/camel-netty-http/pom.xml b/components/camel-netty-http/pom.xml
index f9b45dc2a6d..2740dac17a4 100644
--- a/components/camel-netty-http/pom.xml
+++ b/components/camel-netty-http/pom.xml
@@ -161,6 +161,11 @@
<artifactId>javax.annotation-api</artifactId>
<scope>test</scope>
</dependency>
+ <dependency>
+ <groupId>org.awaitility</groupId>
+ <artifactId>awaitility</artifactId>
+ <scope>test</scope>
+ </dependency>
<!-- logging -->
<dependency>
diff --git a/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/NettyHttpAccessHttpRequestAndResponseBeanTest.java b/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/NettyHttpAccessHttpRequestAndResponseBeanTest.java
index 09ab9a6a08b..b2fa586b9c1 100644
--- a/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/NettyHttpAccessHttpRequestAndResponseBeanTest.java
+++ b/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/NettyHttpAccessHttpRequestAndResponseBeanTest.java
@@ -17,6 +17,7 @@
package org.apache.camel.component.netty.http;
import java.nio.charset.Charset;
+import java.util.concurrent.TimeUnit;
import io.netty.handler.codec.http.DefaultFullHttpResponse;
import io.netty.handler.codec.http.FullHttpRequest;
@@ -28,6 +29,7 @@ import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.component.netty.NettyConverter;
import org.junit.jupiter.api.Test;
+import static org.awaitility.Awaitility.await;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class NettyHttpAccessHttpRequestAndResponseBeanTest extends BaseNettyTest {
@@ -37,10 +39,11 @@ public class NettyHttpAccessHttpRequestAndResponseBeanTest extends BaseNettyTest
getMockEndpoint("mock:input").expectedBodiesReceived("World", "Camel");
String out = template.requestBody("netty-http:http://localhost:{{port}}/foo", "World", String.class);
- assertEquals("Bye World", out);
+
+ await().atMost(3, TimeUnit.SECONDS).untilAsserted(() -> assertEquals("Bye World", out));
String out2 = template.requestBody("netty-http:http://localhost:{{port}}/foo", "Camel", String.class);
- assertEquals("Bye Camel", out2);
+ await().atMost(3, TimeUnit.SECONDS).untilAsserted(() -> assertEquals("Bye Camel", out2));
assertMockEndpointsSatisfied();
}
diff --git a/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/NettyHttpSimpleBasicAuthConstraintMapperTest.java b/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/NettyHttpSimpleBasicAuthConstraintMapperTest.java
index 69504cdec2b..3e504016f1d 100644
--- a/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/NettyHttpSimpleBasicAuthConstraintMapperTest.java
+++ b/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/NettyHttpSimpleBasicAuthConstraintMapperTest.java
@@ -16,17 +16,26 @@
*/
package org.apache.camel.component.netty.http;
+import java.util.concurrent.TimeUnit;
+
import org.apache.camel.BindToRegistry;
import org.apache.camel.CamelExecutionException;
import org.apache.camel.builder.RouteBuilder;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.DisplayName;
+import org.junit.jupiter.api.MethodOrderer;
+import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.TestMethodOrder;
+import static io.netty.handler.codec.http.HttpResponseStatus.UNAUTHORIZED;
import static org.apache.camel.test.junit5.TestSupport.assertIsInstanceOf;
+import static org.awaitility.Awaitility.await;
import static org.junit.jupiter.api.Assertions.assertEquals;
-import static org.junit.jupiter.api.Assertions.fail;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
public class NettyHttpSimpleBasicAuthConstraintMapperTest extends BaseNettyTest {
@Override
@@ -53,28 +62,45 @@ public class NettyHttpSimpleBasicAuthConstraintMapperTest extends BaseNettyTest
return matcher;
}
+ @DisplayName("Tests whether it can access the public page without auth")
@Test
- public void testBasicAuth() throws Exception {
+ @Order(1)
+ void testAccessToPublicPage() {
getMockEndpoint("mock:input").expectedBodiesReceived("Hello Public", "Hello World");
- // we dont need auth for the public page
+ // we do not need auth for the public page
String out = template.requestBody("netty-http:http://localhost:{{port}}/foo/public/hello.txt", "Hello Public",
String.class);
+
assertEquals("Bye World", out);
+ }
+
+ @DisplayName("Tests whether it returns unauthorized (HTTP 401) for unauthorized access")
+ @Test
+ @Order(2)
+ void testUnauthorized() {
+ CamelExecutionException exception = assertThrows(CamelExecutionException.class,
+ () -> template.requestBody("netty-http:http://localhost:{{port}}/foo", "Hello World", String.class),
+ "Should have thrown a CamelExecutionException");
- try {
- template.requestBody("netty-http:http://localhost:{{port}}/foo", "Hello World", String.class);
- fail("Should send back 401");
- } catch (CamelExecutionException e) {
- NettyHttpOperationFailedException cause = assertIsInstanceOf(NettyHttpOperationFailedException.class, e.getCause());
- assertEquals(401, cause.getStatusCode());
- }
+ NettyHttpOperationFailedException cause
+ = assertIsInstanceOf(NettyHttpOperationFailedException.class, exception.getCause());
+ assertEquals(UNAUTHORIZED.code(), cause.getStatusCode(), "Should have sent back HTTP status 401");
+ }
- // username:password is scott:secret
+ @DisplayName("Tests whether it authorized access to non-public resource succeeds")
+ @Test
+ @Order(3)
+ public void testBasicAuth() throws Exception {
String auth = "Basic c2NvdHQ6c2VjcmV0";
- out = template.requestBodyAndHeader("netty-http:http://localhost:{{port}}/foo", "Hello World", "Authorization", auth,
- String.class);
- assertEquals("Bye World", out);
+
+ await().atMost(2, TimeUnit.SECONDS).untilAsserted(
+ () -> {
+ String out = template.requestBodyAndHeader("netty-http:http://localhost:{{port}}/foo", "Hello World",
+ "Authorization", auth,
+ String.class);
+ assertEquals("Bye World", out);
+ });
assertMockEndpointsSatisfied();
}
diff --git a/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/NettyHttpSimpleBasicAuthTest.java b/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/NettyHttpSimpleBasicAuthTest.java
index 67c4f010976..722729b3e2c 100644
--- a/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/NettyHttpSimpleBasicAuthTest.java
+++ b/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/NettyHttpSimpleBasicAuthTest.java
@@ -20,12 +20,18 @@ import org.apache.camel.CamelExecutionException;
import org.apache.camel.builder.RouteBuilder;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.DisplayName;
+import org.junit.jupiter.api.MethodOrderer;
+import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.TestMethodOrder;
+import static io.netty.handler.codec.http.HttpResponseStatus.UNAUTHORIZED;
import static org.apache.camel.test.junit5.TestSupport.assertIsInstanceOf;
import static org.junit.jupiter.api.Assertions.assertEquals;
-import static org.junit.jupiter.api.Assertions.fail;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
public class NettyHttpSimpleBasicAuthTest extends BaseNettyTest {
@Override
@@ -42,17 +48,27 @@ public class NettyHttpSimpleBasicAuthTest extends BaseNettyTest {
super.tearDown();
}
- @Test
- public void testBasicAuth() throws Exception {
+ private void sendUnauthorizedRequest() {
+ CamelExecutionException exception = assertThrows(CamelExecutionException.class,
+ () -> template.requestBody("netty-http:http://localhost:{{port}}/foo", "Hello World", String.class),
+ "Should have thrown a CamelExecutionException");
+
+ NettyHttpOperationFailedException cause
+ = assertIsInstanceOf(NettyHttpOperationFailedException.class, exception.getCause());
+ assertEquals(UNAUTHORIZED.code(), cause.getStatusCode(), "Should have sent back HTTP status 401");
+ }
- try {
- template.requestBody("netty-http:http://localhost:{{port}}/foo", "Hello World", String.class);
- fail("Should send back 401");
- } catch (CamelExecutionException e) {
- NettyHttpOperationFailedException cause = assertIsInstanceOf(NettyHttpOperationFailedException.class, e.getCause());
- assertEquals(401, cause.getStatusCode());
- }
+ @Order(1)
+ @DisplayName("Tests whether it returns unauthorized (HTTP 401) for unauthorized access")
+ @Test
+ void testBasicAuth() {
+ sendUnauthorizedRequest();
+ }
+ @Order(2)
+ @DisplayName("Tests whether it authorized access succeeds")
+ @Test
+ void testWithAuth() throws InterruptedException {
getMockEndpoint("mock:input").expectedBodiesReceived("Hello World");
// username:password is scott:secret
@@ -62,15 +78,13 @@ public class NettyHttpSimpleBasicAuthTest extends BaseNettyTest {
assertEquals("Bye World", out);
assertMockEndpointsSatisfied();
+ }
- try {
- template.requestBody("netty-http:http://localhost:{{port}}/foo", "Hello World", String.class);
- fail("Should send back 401");
- } catch (CamelExecutionException e) {
- NettyHttpOperationFailedException cause = assertIsInstanceOf(NettyHttpOperationFailedException.class, e.getCause());
- assertEquals(401, cause.getStatusCode());
- }
-
+ @Order(3)
+ @DisplayName("Tests whether it returns unauthorized (HTTP 401) for unauthorized access after a successful authorization")
+ @Test
+ void testBasicAuthPostAuth() {
+ sendUnauthorizedRequest();
}
@Override
diff --git a/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/NettyHttpSimplePooledExchangeTest.java b/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/NettyHttpSimplePooledExchangeTest.java
index 6e2b79f4b08..f9428d18e28 100644
--- a/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/NettyHttpSimplePooledExchangeTest.java
+++ b/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/NettyHttpSimplePooledExchangeTest.java
@@ -16,14 +16,22 @@
*/
package org.apache.camel.component.netty.http;
+import java.util.concurrent.TimeUnit;
+
import org.apache.camel.CamelContext;
import org.apache.camel.ExtendedCamelContext;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.impl.engine.PooledExchangeFactory;
+import org.awaitility.Awaitility;
+import org.junit.jupiter.api.Assumptions;
+import org.junit.jupiter.api.MethodOrderer;
+import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.TestMethodOrder;
import static org.junit.jupiter.api.Assertions.assertEquals;
+@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
public class NettyHttpSimplePooledExchangeTest extends BaseNettyTest {
@Override
@@ -33,8 +41,11 @@ public class NettyHttpSimplePooledExchangeTest extends BaseNettyTest {
return camelContext;
}
+ @Order(1)
@Test
public void testOne() throws Exception {
+ Assumptions.assumeTrue(context.isStarted());
+
getMockEndpoint("mock:input").expectedBodiesReceived("World");
String out = template.requestBody("netty-http:http://localhost:{{port}}/pooled", "World", String.class);
@@ -43,6 +54,7 @@ public class NettyHttpSimplePooledExchangeTest extends BaseNettyTest {
assertMockEndpointsSatisfied();
}
+ @Order(2)
@Test
public void testThree() throws Exception {
getMockEndpoint("mock:input").expectedBodiesReceived("World", "Camel", "Earth");
@@ -53,8 +65,11 @@ public class NettyHttpSimplePooledExchangeTest extends BaseNettyTest {
out = template.requestBody("netty-http:http://localhost:{{port}}/pooled", "Camel", String.class);
assertEquals("Bye Camel", out);
- out = template.requestBody("netty-http:http://localhost:{{port}}/pooled", "Earth", String.class);
- assertEquals("Bye Earth", out);
+ Awaitility.await().atMost(2, TimeUnit.SECONDS).untilAsserted(
+ () -> {
+ String reqOut = template.requestBody("netty-http:http://localhost:{{port}}/pooled", "Earth", String.class);
+ assertEquals("Bye Earth", reqOut);
+ });
assertMockEndpointsSatisfied();
}