听到许多好评后,我想我会尝试一下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.crt
PS请注意,正如许多专家(通过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