使用Arquillian(远程)测试OpenLiberty
聽到許多好評后,我想我會嘗試一下Open Liberty 。
在這篇文章中,我將討論以下內容:
- 開放自由的設置
- 設置JDBC連接
- 設置Arquillian
- 測試REST端點
安裝開放自由
在撰寫本文時,我正在使用Open Liberty 18.0.0.1,并且正在使用Java SE 1.8.0_172(PS Keen繼續使用Java 9和Java 10,但我認為最好等待LTS Java 11)。
安裝非常容易。 假設我們要創建一個正在運行的服務器名稱test 。
首先,解壓縮您的Open Liberty下載文件。 它將創建目錄結構wlp 。
導航到bin目錄并運行以下命令:
./server create test現在,服務器名稱test已創建。 開始:
./server start test參數test是服務器的名稱。
導航到http://localhost:9080/test以查看上下文根。
停止,
./server stop test配置server.xml
啟動test服務器后,將在/usr/servers/test下創建一個目錄,該目錄內有一個名為server.xml文件。 讓我們來看看。
<?xml version="1.0" encoding="UTF-8"?> <server description="new server"><!-- Enable features --><featureManager><feature>jsp-2.3</feature></featureManager><!-- To access this server from a remote client add a host attribute to the following element, e.g. host="*" --><httpEndpoint id="defaultHttpEndpoint"httpPort="9080"httpsPort="9443" /><!-- Automatically expand WAR files and EAR files --><applicationManager autoExpand="true"/> </server>在本文中,我們使用Java EE 7 Web Profile來實現這一點,這樣做非常簡單(甚至不必重新啟動服務器)。 只需更改featureManager 。
<?xml version="1.0" encoding="UTF-8"?> <server description="new server"><!-- Enable features --><featureManager><feature>webProfile-7.0</feature></featureManager><!-- the rest of the configuration omitted -->您可以通過查看console.log來檢查正在動態加載的功能。
配置JDBC數據源
在server.xml中配置數據源
對于本練習,我正在使用MySQL 8.0。 設置MySQL及其配置不在本文的討論范圍之內。
假設我們已經創建了一個新數據庫,也稱為test 。
要設置您的數據源,請對server.xml進行以下修改,然后重新啟動(或者不,對此不太確定,但是重新啟動沒有任何危害)。
評論交錯。
<?xml version="3.0" encoding="UTF-8"?> <server description="new server"><!-- Enable features --><featureManager><feature>webProfile-7.0</feature></featureManager><!-- Declare the jar files for MySQL access through JDBC. --><dataSource id="testDS" jndiName="jdbc/testDS"><jdbcDriver libraryRef="MySQLLib"/><properties databaseName="test"serverName="localhost" portNumber="3306"user="root" password="P4sswordGoesH3r3"/></dataSource><library id="MySQLLib"><file name="/home/dwuysan/dev/appservers/wlp/usr/shared/resources/mysql/mysql-connector-java-8.0.11.jar"/></library><!-- Automatically expand WAR files and EAR files --><applicationManager autoExpand="true"/> </server>persistence.xml
OpenLiberty附帶有作為JPA Provider捆綁的EclipseLink。 在此示例中,我沒有配置任何EclipseLink的屬性。
<?xml version="1.0" encoding="UTF-8"?> <persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd"version="2.1"><persistence-unit name="testPU"><jta-data-source>jdbc/testDS</jta-data-source> <properties></properties></persistence-unit> </persistence>然后,您可以通過以下方式在Java EE應用程序中調用它:
@Stateless @LocalBean public class LogService {@PersistenceContextprivate EntityManager em;public Collection<Log> getLogs() {return this.em.createNamedQuery(Log.FIND_ALL, Log.class).getResultList();} }設置arquillian
在本文中,我們將針對正在運行的OpenLiberty服務器實施Arquillian遠程測試。
首先,將arquillian添加到pom.xml 。
配置pom.xml
這是已修改的pom.xml:
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>id.co.lucyana</groupId><artifactId>test</artifactId><version>1.0-SNAPSHOT</version><packaging>war</packaging><name>test</name><properties><endorsed.dir>${project.build.directory}/endorsed</endorsed.dir><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding></properties><dependencyManagement><dependencies><dependency><groupId>org.jboss.arquillian</groupId><artifactId>arquillian-bom</artifactId><version>1.4.0.Final</version><scope>import</scope><type>pom</type></dependency></dependencies></dependencyManagement><dependencies> <dependency><groupId>org.jboss.arquillian.graphene</groupId><artifactId>graphene-webdriver</artifactId><version>2.3.2</version><type>pom</type><scope>test</scope></dependency><dependency><groupId>org.seleniumhq.selenium</groupId><artifactId>selenium-java</artifactId><scope>test</scope><version>3.12.0</version></dependency><dependency><groupId>org.jboss.arquillian.junit</groupId><artifactId>arquillian-junit-container</artifactId><scope>test</scope></dependency> <dependency><!-- Arquillian WebSphere Liberty Profile support --><groupId>io.openliberty.arquillian</groupId><artifactId>arquillian-liberty-remote</artifactId><version>1.0.0</version><scope>test</scope></dependency><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.12</version><scope>test</scope></dependency><dependency><groupId>javax</groupId><artifactId>javaee-web-api</artifactId><version>7.0</version><scope>provided</scope></dependency></dependencies><build><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><version>3.7.0</version><configuration><source>1.8</source><target>1.8</target><compilerArguments><endorseddirs>${endorsed.dir}</endorseddirs></compilerArguments></configuration></plugin><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-war-plugin</artifactId><version>3.2.1</version><configuration><failOnMissingWebXml>false</failOnMissingWebXml></configuration></plugin><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-dependency-plugin</artifactId><version>3.1.0</version><executions><execution><phase>validate</phase><goals><goal>copy</goal></goals><configuration><outputDirectory>${endorsed.dir}</outputDirectory><silent>true</silent><artifactItems><artifactItem><groupId>javax</groupId><artifactId>javaee-endorsed-api</artifactId><version>7.0</version><type>jar</type></artifactItem></artifactItems></configuration></execution></executions></plugin></plugins></build> </project>修改server.xml
這里提供的文檔是不言自明的。 請查閱此文檔以獲取有關如何啟用遠程測試的更多最新信息。
<?xml version="1.0" encoding="UTF-8"?> <server description="new server"><!-- Enable features --><featureManager><feature>webProfile-7.0</feature><feature>restConnector-2.0</feature></featureManager><!-- Declare the jar files for MySQL access through JDBC. --><dataSource id="testDS" jndiName="jdbc/testDS"><jdbcDriver libraryRef="MySQLLib"/><properties databaseName="test" serverName="localhost" portNumber="3306" user="root" password="P4sswordGoesH3r3"/></dataSource><library id="MySQLLib"><file name="/home/dwuysan/dev/appservers/wlp/usr/shared/resources/mysql/mysql-connector-java-8.0.11.jar"/></library><httpEndpoint httpPort="9080" httpsPort="9443" id="defaultHttpEndpoint" host="*" /><!-- userName and password should also be set in arquillian.xml to these values --><quickStartSecurity userName="admin" userPassword="admin" /><!-- Enable the keystore --><keyStore id="defaultKeyStore" password="password" /><applicationMonitor updateTrigger="mbean" /><logging consoleLogLevel="INFO" /><!-- This section is needed to allow upload of files to the dropins directory, the remote container adapter relies on this configuration --><remoteFileAccess><writeDir>${server.config.dir}/dropins</writeDir></remoteFileAccess><!-- Automatically expand WAR files and EAR files --><applicationManager autoExpand="true"/> </server>信任服務器(即證書)
您還需要讓客戶端信任這些密鑰,否則您將看到SSL證書信任錯誤,并且需要授予容器適配器寫入dropins目錄的權限” (Liberty-Arquillian 2018)
完成上述所有必要的更改后,(并重新啟動服務器),請注意,在<location of your OpenLiberty server>/usr/servers/test/resources/security下創建了一個新目錄,其中test為名稱我們最初創建的服務器的數量。
請注意,正在創建兩個文件, keys.jks和ltpa.keys 。 現在,我們對keys.jks感興趣。
為了能夠從Netbeans(Maven)運行測試,JDK必須信任正在運行的OpenLiberty。
檢查證書
keytool -list -v -keystore key.jks Enter keystore password:此處的password基本上是我們在server.xml中創建的password ,尤其是以下行:
<!-- Enable the keystore --> <keyStore id="defaultKeyStore" password="password" />因此,輸入密碼,輸出應如下所示:
***************** WARNING WARNING WARNING ***************** * The integrity of the information stored in your keystore * * has NOT been verified! In order to verify its integrity, * * you must provide your keystore password. * ***************** WARNING WARNING WARNING *****************Keystore type: jks Keystore provider: SUNYour keystore contains 1 entryAlias name: default Creation date: May 26, 2018 Entry type: PrivateKeyEntry Certificate chain length: 1 Certificate[1]: Owner: CN=localhost, OU=test, O=ibm, C=us Issuer: CN=localhost, OU=test, O=ibm, C=us Serial number: 2a6c5b27 Valid from: Sat May 26 12:24:30 WITA 2018 until: Sun May 26 12:24:30 WITA 2019 Certificate fingerprints:MD5: 63:92:B2:4A:25:E3:BB:3B:96:37:11:C1:A7:25:38:B5SHA1: B6:38:95:88:FC:50:EC:A0:8E:41:4E:DE:B5:D4:8B:85:2E:61:A2:5FSHA256: 9C:7B:6A:FA:46:8C:50:F2:7D:7B:C4:24:4B:15:78:5A:34:25:C8:43:D1:AB:4D:EE:C7:00:4C:AF:30:F5:5C:92 Signature algorithm name: SHA256withRSA Subject Public Key Algorithm: 2048-bit RSA key Version: 3Extensions: #1: ObjectId: 2.5.29.14 Criticality=false SubjectKeyIdentifier [ KeyIdentifier [ 0000: 88 F2 C2 32 73 73 B6 66 8F FA 42 85 1F 43 A5 AF ...2ss.f..B..C.. 0010: 84 33 62 D5 .3b. ] ]******************************************* *******************************************接下來,導出證書
現在,我們需要創建一個.cer 。 使用以下命令:
keytool -export -alias default -file testwlp.crt -keystore key.jks Enter keystore password:基本上,我們將alias證書導出到名為testwlp.crt的文件中。 現在,應該創建一個名為testwlp.crt的文件。
最后,通過將證書導入JDK cacert來信任該證書
keytool -import -trustcacerts -keystore $JAVA_HOME/jre/lib/security/cacerts -storepass changeit -alias testwlp -import -file testwlp.crtPS請注意,正如許多專家(通過Twitter)指出的那樣,顯然有很多“信任”服務器的方法。 我相信有更好的方法,并且我盡可能不接觸任何JDK的文件。
創建arquillian.xml
現在完成了所有這些管道工作,讓我們相應地添加arquillian.xml 。
<?xml version="1.0" encoding="UTF-8"?> <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"><engine><property name="deploymentExportPath">target</property></engine><container qualifier="liberty-remote" default="true"><configuration><property name="hostName">localhost</property><property name="serverName">test</property><!-- check the 'quickStartSecurity' on 'server.xml' --><property name="username">admin</property><property name="password">admin</property><!-- check the 'server.xml' --><property name="httpPort">9080</property><property name="httpsPort">9443</property> </configuration></container> <extension qualifier="webdriver"><!--<property name="browser">firefox</property>--><property name="remoteReusable">false</property></extension> </arquillian>編寫REST測試用例
完成所有這些設置后,您現在可以編寫Arquillian Test用例。 以下是針對JAX-RS端點的測試用例的示例(請原諒測試用例的簡單性,這一點是為了說明我們如何使用Arquillian-remote針對OpenLiberty進行測試) :
package id.co.lucyana.test.resource;import id.co.lucyana.test.entity.Log; import id.co.lucyana.test.services.LogService; import id.co.lucyana.test.util.ApplicationConfig; import java.net.URL; import org.jboss.arquillian.container.test.api.Deployment; import org.jboss.arquillian.container.test.api.RunAsClient; import org.jboss.arquillian.drone.api.annotation.Drone; import org.jboss.arquillian.junit.Arquillian; import org.jboss.arquillian.test.api.ArquillianResource; import org.jboss.shrinkwrap.api.ArchivePaths; import org.jboss.shrinkwrap.api.ShrinkWrap; import org.jboss.shrinkwrap.api.spec.JavaArchive; import org.junit.Assert; import org.junit.Test; import org.junit.runner.RunWith; import org.openqa.selenium.WebDriver;@RunWith(Arquillian.class) public class LogResourceTest {@Droneprivate WebDriver webDriver;@Deploymentpublic static JavaArchive createTestArchive() {return ShrinkWrap.create(JavaArchive.class).addClass(Log.class).addClass(LogResource.class).addClass(LogService.class).addClass(ApplicationConfig.class).addAsManifestResource("test-persistence.xml",ArchivePaths.create("persistence.xml"));}@Test@RunAsClientpublic void testLogResource(@ArquillianResource URL url) { this.webDriver.get(url.toString() + "resources/log");String pageSource = this.webDriver.getPageSource();System.out.println("RESULT: " + pageSource);Assert.assertTrue(true);} }參考文獻
DigiCert,2018, '如何將受信任的根安裝到Java cacerts Keystore中' ,DigiCert,于2018年6月20日訪問
Liberty-Arquillian,2018年, ``Arquillian Liberty遠程文檔'' ,GitHub。 Inc,于2018年6月20日訪問
SSLShopper,2008年, “最常見的Java Keytool密鑰庫命令” ,SSLShopper,于2018年6月20日訪問
翻譯自: https://www.javacodegeeks.com/2018/06/testing-openliberty-arquillian-remote.html
總結
以上是生活随笔為你收集整理的使用Arquillian(远程)测试OpenLiberty的全部內容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: 苹果手机屏幕失灵怎么办
- 下一篇: 女儿国遇难50字概括 女儿国遇难50字概
