[GitHub] [tomee] rzo1 opened a new pull request #765: TOMEE-2976 - Provide two examples using Arquillian & JUnit 5

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

[GitHub] [tomee] rzo1 opened a new pull request #765: TOMEE-2976 - Provide two examples using Arquillian & JUnit 5

GitBox

rzo1 opened a new pull request #765:
URL: https://github.com/apache/tomee/pull/765


   # What does this MR do?
   
   - Adds two examples of testing with Arquillian and JUnit 5
   
   # References
   
   - http://mail-archives.apache.org/mod_mbox/tomee-dev/202103.mbox/%3C1615057870806-0.post%40n4.nabble.com%3E
   - https://issues.apache.org/jira/browse/TOMEE-2976


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
[hidden email]


Reply | Threaded
Open this post in threaded view
|

[GitHub] [tomee] cesarhernandezgt commented on a change in pull request #765: TOMEE-2976 - Provide two examples using Arquillian & JUnit 5

GitBox

cesarhernandezgt commented on a change in pull request #765:
URL: https://github.com/apache/tomee/pull/765#discussion_r589886825



##########
File path: examples/junit5-arquillian-simple-websockets/src/test/java/resources/arquillian.xml
##########
@@ -0,0 +1,33 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+
+    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.
+-->
+<arquillian xmlns="http://jboss.org/schema/arquillian"
+            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+            xsi:schemaLocation="
+              http://jboss.org/schema/arquillian
+              http://jboss.org/schema/arquillian/arquillian_1_0.xsd">
+    <container qualifier="tomee" default="true">
+        <configuration>
+            <property name="serverXml">src/main/conf/server.xml</property>
+            <property name="httpPort">8081</property>

Review comment:
       I recall fixing port numbers can lead to concurrency issues during test executions.
   The port definition in arquillian.xml can be dynamic.
   
   Example
   ```      <property name="httpPort">-1</property>
         <property name="stopPort">-1</property>
   ```
   https://github.com/apache/tomee/blob/master/examples/mp-custom-healthcheck/src/test/resources/arquillian.xml#L22-L23 
   
   

##########
File path: examples/junit5-arquillian-simple-websockets/src/test/java/org/superbiz/websockets/WebSocketResourceTest.java
##########
@@ -0,0 +1,95 @@
+/*
+ * 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.
+ */
+
+package org.superbiz.websockets;
+
+import org.jboss.arquillian.container.test.api.Deployment;
+import org.jboss.arquillian.container.test.api.RunAsClient;
+import org.jboss.arquillian.junit5.ArquillianExtension;
+import org.jboss.arquillian.test.api.ArquillianResource;
+import org.jboss.shrinkwrap.api.ShrinkWrap;
+import org.jboss.shrinkwrap.api.spec.WebArchive;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+
+import javax.websocket.*;
+import java.io.File;
+import java.io.IOException;
+import java.net.URI;
+import java.net.URISyntaxException;
+import java.net.URL;
+import java.util.concurrent.TimeUnit;
+
+import static org.junit.jupiter.api.Assertions.*;
+
+@RunAsClient
+@ExtendWith(ArquillianExtension.class)
+public class WebSocketResourceTest {
+
+    private static final int PORT = 8081;
+
+    @ArquillianResource()
+    private URL base;
+
+    @Deployment(testable = false)
+    public static final WebArchive app() {
+        return ShrinkWrap.create(WebArchive.class, "demo.war")
+                .addClasses(WebSocketResource.class)
+                .addAsWebInfResource(new File("src/main/webapp/WEB-INF/web.xml"), "web.xml")
+                .addAsWebInfResource(new File("src/main/resources/META-INF/beans.xml"), "beans.xml");
+    }
+
+    @Test
+    public void testConnectAndReceiveMessage() throws Exception {
+        Session session = connectToServer(MyWebSocketClientObject.class);
+        assertNotNull(session);
+
+        assertTrue(MyWebSocketClientObject.latch.await(2, TimeUnit.SECONDS));
+        assertEquals("Successfully opened session", MyWebSocketClientObject.response);
+
+        session.close();
+    }
+
+    @Test
+    public void testConnectAndSendPayload() throws Exception {
+
+        String payload = "I am the payload sent to this resource.";
+        Session session = connectToServer(MyWebSocketClientObject.class);
+        assertNotNull(session);
+
+        assertTrue(MyWebSocketClientObject.latch.await(2, TimeUnit.SECONDS));
+        assertEquals("Successfully opened session", MyWebSocketClientObject.response);
+
+        session.getBasicRemote().sendText(payload);
+
+        assertTrue(MyWebSocketClientObject.payloadLatch.await(2, TimeUnit.SECONDS));
+        assertEquals("Received: " + payload, MyWebSocketClientObject.response);
+
+        session.close();
+    }
+
+    /**
+     * Method used to supply connection to the server by passing the naming of
+     * the websocket endpoint
+     */
+    public Session connectToServer(Class<?> endpoint) throws DeploymentException, IOException, URISyntaxException {
+        WebSocketContainer container = ContainerProvider.getWebSocketContainer();
+        assertNotNull(container);
+        return container.connectToServer(endpoint, new URI("ws", base.getUserInfo(), "localhost", PORT,"/api/socket",null, null));

Review comment:
       Once arquillian dinamicaly allocate an available port (check my other comment) you can obtain the webTarget with `base.toExternalForm()`
   Excample
   https://github.com/apache/tomee/blob/master/examples/mp-custom-healthcheck/src/test/java/org/superbiz/test/WeatherServiceTest.java#L68




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
[hidden email]


Reply | Threaded
Open this post in threaded view
|

[GitHub] [tomee] rzo1 commented on a change in pull request #765: TOMEE-2976 - Provide two examples using Arquillian & JUnit 5

GitBox
In reply to this post by GitBox

rzo1 commented on a change in pull request #765:
URL: https://github.com/apache/tomee/pull/765#discussion_r590059284



##########
File path: examples/junit5-arquillian-simple-websockets/src/test/java/resources/arquillian.xml
##########
@@ -0,0 +1,33 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+
+    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.
+-->
+<arquillian xmlns="http://jboss.org/schema/arquillian"
+            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+            xsi:schemaLocation="
+              http://jboss.org/schema/arquillian
+              http://jboss.org/schema/arquillian/arquillian_1_0.xsd">
+    <container qualifier="tomee" default="true">
+        <configuration>
+            <property name="serverXml">src/main/conf/server.xml</property>
+            <property name="httpPort">8081</property>

Review comment:
       You are right. If CI executes multiple jobs in parallel, it might clash with ports. We normally work with Gitlab CI and isolated containers. Nice catch! I fixed this.

##########
File path: examples/junit5-arquillian-simple-websockets/src/test/java/org/superbiz/websockets/WebSocketResourceTest.java
##########
@@ -0,0 +1,95 @@
+/*
+ * 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.
+ */
+
+package org.superbiz.websockets;
+
+import org.jboss.arquillian.container.test.api.Deployment;
+import org.jboss.arquillian.container.test.api.RunAsClient;
+import org.jboss.arquillian.junit5.ArquillianExtension;
+import org.jboss.arquillian.test.api.ArquillianResource;
+import org.jboss.shrinkwrap.api.ShrinkWrap;
+import org.jboss.shrinkwrap.api.spec.WebArchive;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+
+import javax.websocket.*;
+import java.io.File;
+import java.io.IOException;
+import java.net.URI;
+import java.net.URISyntaxException;
+import java.net.URL;
+import java.util.concurrent.TimeUnit;
+
+import static org.junit.jupiter.api.Assertions.*;
+
+@RunAsClient
+@ExtendWith(ArquillianExtension.class)
+public class WebSocketResourceTest {
+
+    private static final int PORT = 8081;
+
+    @ArquillianResource()
+    private URL base;
+
+    @Deployment(testable = false)
+    public static final WebArchive app() {
+        return ShrinkWrap.create(WebArchive.class, "demo.war")
+                .addClasses(WebSocketResource.class)
+                .addAsWebInfResource(new File("src/main/webapp/WEB-INF/web.xml"), "web.xml")
+                .addAsWebInfResource(new File("src/main/resources/META-INF/beans.xml"), "beans.xml");
+    }
+
+    @Test
+    public void testConnectAndReceiveMessage() throws Exception {
+        Session session = connectToServer(MyWebSocketClientObject.class);
+        assertNotNull(session);
+
+        assertTrue(MyWebSocketClientObject.latch.await(2, TimeUnit.SECONDS));
+        assertEquals("Successfully opened session", MyWebSocketClientObject.response);
+
+        session.close();
+    }
+
+    @Test
+    public void testConnectAndSendPayload() throws Exception {
+
+        String payload = "I am the payload sent to this resource.";
+        Session session = connectToServer(MyWebSocketClientObject.class);
+        assertNotNull(session);
+
+        assertTrue(MyWebSocketClientObject.latch.await(2, TimeUnit.SECONDS));
+        assertEquals("Successfully opened session", MyWebSocketClientObject.response);
+
+        session.getBasicRemote().sendText(payload);
+
+        assertTrue(MyWebSocketClientObject.payloadLatch.await(2, TimeUnit.SECONDS));
+        assertEquals("Received: " + payload, MyWebSocketClientObject.response);
+
+        session.close();
+    }
+
+    /**
+     * Method used to supply connection to the server by passing the naming of
+     * the websocket endpoint
+     */
+    public Session connectToServer(Class<?> endpoint) throws DeploymentException, IOException, URISyntaxException {
+        WebSocketContainer container = ContainerProvider.getWebSocketContainer();
+        assertNotNull(container);
+        return container.connectToServer(endpoint, new URI("ws", base.getUserInfo(), "localhost", PORT,"/api/socket",null, null));

Review comment:
       Fixed.




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
[hidden email]


Reply | Threaded
Open this post in threaded view
|

[GitHub] [tomee] rzo1 commented on pull request #765: TOMEE-2976 - Provide two examples using Arquillian & JUnit 5

GitBox
In reply to this post by GitBox

rzo1 commented on pull request #765:
URL: https://github.com/apache/tomee/pull/765#issuecomment-805784199


   @cesarhernandezgt The failing tests in https://ci-builds.apache.org/job/Tomee/job/pull-request/132/ seem unrelated to me. Is this a Jenkins issue? wdyt?


--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
[hidden email]


Reply | Threaded
Open this post in threaded view
|

[GitHub] [tomee] rzo1 commented on pull request #765: TOMEE-2976 - Provide two examples using Arquillian & JUnit 5

GitBox
In reply to this post by GitBox

rzo1 commented on pull request #765:
URL: https://github.com/apache/tomee/pull/765#issuecomment-806406721


   Ok. It seems, that the PR builder is now **green** again. I will merge this two JUnit 5 related examples. If we need to adjust anything afterwards, I will happily doing it :)


--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
[hidden email]


Reply | Threaded
Open this post in threaded view
|

[GitHub] [tomee] rzo1 merged pull request #765: TOMEE-2976 - Provide two examples using Arquillian & JUnit 5

GitBox
In reply to this post by GitBox

rzo1 merged pull request #765:
URL: https://github.com/apache/tomee/pull/765


   


--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
[hidden email]