Skip to content

Commit 039e584

Browse files
committed
Added CDI @asynchronous sample and fixed Swarm Arquillian deps
1 parent a0abc87 commit 039e584

File tree

14 files changed

+280
-35
lines changed

14 files changed

+280
-35
lines changed

config/README.md

Lines changed: 7 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,14 @@
1-
# Eclipse MicroProfile 1.2 Samples - JWT Auth 1.0
1+
# Eclipse MicroProfile 1.2 Samples - Config 1.1
22

3-
- [Wiki project page](https://wiki.eclipse.org/MicroProfile/JWT_Auth)
4-
- [Spec, API, TCK GitHub repo](https://github.com/eclipse/microprofile-jwt-auth)
3+
- [Wiki project page](https://wiki.eclipse.org/MicroProfile/Config)
4+
- [Spec, API, TCK GitHub repo](https://github.com/eclipse/microprofile-config)
55

66
## Samples ##
77

8-
- **basic-authentication** The test sends a very basic signed JWT token to a protected servlet. The MP-JWT Auth implementation
9-
checks if the token is valid and sets the authenticated identity from the `upn` field and `groups` field
8+
- **basic-injection** Shows the injection of some simple values from the `META-INF/microprofile-config.properties` file
9+
- **config-source** Shows how an alternative programmatic source for configuration values can be used
10+
- **converter** Shows how a custom converter is globally set so values from a config source can be converted to the type of the injection point.
11+
- **default-conversion** Shows that a number of default conversions can be applied to injected values
1012

1113

1214

13-
## Implementation config ##
14-
15-
There's at least 2 items that need to be configured in an implementation specific way: the *issuer*, which represents the party that vended the token, and the *public key* which is used to verify the signature of a JWT token being sent to the server.
16-
17-
- **Payara (tech preview)**
18-
- *issuer* - defined in "payara-mp-jwt.properties" which is placed in the classpath root of the application archive
19-
- *public key* - the file "publicKey.pem" which is also placed in the classpath root of the application archive
20-
- **Liberty**
21-
- *issuer* - defined in the "mpJwt" tag in "server.xml" which in the placed inside the installed Liberty: `[install dir]/wlp/usr/servers/defaultServer/server.xml`
22-
- *public key* - the file "key.jks" which in the placed inside the installed Liberty: `[install dir]/wlp/usr/servers/defaultServer/resources/security/key.jks`
23-
- **WildFly (Swarm)**
24-
- *issuer* - defined in "project-default.yml" which is placed in the classpath root of the application archive. This also configures the security system (security domain in JBoss terms) such that all artifacts to support MP-Auth JWT are installed
25-
- *public key* - the file "MP-JWT-SIGNER" which is placed in the classpath's META-INF of the application archive.
26-
27-
28-
## TCK ##
29-
30-
The public/private keys are taken from the MP-Auth TCK. The Liberty key is in a special format and has been taken from the Liberty TCK version. Config files for the various servers are inspired by those in the TCK and TCK extensions.
31-
32-
See the following URLs:
33-
34-
- [MP-Auth TCK](https://github.com/eclipse/microprofile-jwt-auth/tree/master/tck)
35-
- [Payara TCK Ext](https://github.com/payara/Payara/tree/Payara-5/appserver/payara-appserver-modules/microprofile/jwt-auth-tck)
36-
- [Liberty TCK](https://github.com/OpenLiberty/open-liberty/tree/master/dev/com.ibm.ws.security.mp.jwt_fat_tck)
37-
- [WildFly TCK Ext](https://github.com/MicroProfileJWT/wfswarm-jwt-auth-tck)
38-

config/basic-injection/src/test/java/org/eclipse/microprofile12/config/basicinjection/BasicInjectionTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public class BasicInjectionTest {
2929
@Deployment(testable = false)
3030
public static WebArchive createDeployment() {
3131
WebArchive archive =
32-
create(WebArchive.class)
32+
create(WebArchive.class, "BasicInjectionTest.war")
3333
.addClasses(
3434
ApplicationInit.class,
3535
Servlet.class

config/config-source/src/test/java/org/eclipse/microprofile12/config/configsource/ConfigSourceNotPresentTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public class ConfigSourceNotPresentTest {
3030
@Deployment(testable = false)
3131
public static WebArchive createDeployment() {
3232
WebArchive archive =
33-
create(WebArchive.class)
33+
create(WebArchive.class, "ConfigSourceNotPresentTest.war")
3434
.addClasses(
3535
ApplicationInit.class,
3636
Servlet.class

config/config-source/src/test/java/org/eclipse/microprofile12/config/configsource/ConfigSourcePresentTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public class ConfigSourcePresentTest {
2929
@Deployment(testable = false)
3030
public static WebArchive createDeployment() {
3131
WebArchive archive =
32-
create(WebArchive.class)
32+
create(WebArchive.class, "ConfigSourcePresentTest.war")
3333
.addClasses(
3434
ApplicationInit.class,
3535
EchoConfigSource.class,

fault-tolerance/README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Eclipse MicroProfile 1.2 Samples - Fault Tolerance
2+
3+
- [Wiki project page](https://wiki.eclipse.org/MicroProfile/Fault_Tolerance)
4+
- [Spec, API, TCK GitHub repo](https://github.com/eclipse/microprofile-fault-tolerance)
5+
6+
## Samples ##
7+
8+
- **asynchronous** Demonstrates a CDI version of the EJB @Asynchronous annotation
9+
10+
11+
12+
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<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>
3+
4+
<parent>
5+
<groupId>org.eclipse.microprofile.samples12</groupId>
6+
<artifactId>fault-tolerance</artifactId>
7+
<version>1.0-SNAPSHOT</version>
8+
</parent>
9+
10+
<artifactId>asynchronous</artifactId>
11+
<packaging>war</packaging>
12+
<name>MicroProfile 1.2: Fault Tolerance - Asynchronous</name>
13+
14+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package org.eclipse.microprofile12.faulttolerance.asynchronous;
2+
3+
import static java.lang.Thread.sleep;
4+
import static java.util.concurrent.CompletableFuture.completedFuture;
5+
6+
import java.util.concurrent.Future;
7+
import java.util.logging.Level;
8+
import java.util.logging.Logger;
9+
10+
import javax.enterprise.context.ApplicationScoped;
11+
12+
import org.eclipse.microprofile.faulttolerance.Asynchronous;
13+
14+
/**
15+
* @author Arun Gupta
16+
* @author Arjan Tijms
17+
*/
18+
@Asynchronous
19+
@ApplicationScoped
20+
public class MyAsyncBeanClassLevel {
21+
22+
public static final long AWAIT = 3000;
23+
24+
public Future<Integer> addNumbers(int n1, int n2) {
25+
try {
26+
// simulating a long running query
27+
sleep(AWAIT);
28+
} catch (InterruptedException ex) {
29+
Logger.getLogger(MyAsyncBeanClassLevel.class.getName()).log(Level.SEVERE, null, ex);
30+
}
31+
32+
return completedFuture(n1 + n2);
33+
}
34+
35+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package org.eclipse.microprofile12.faulttolerance.asynchronous;
2+
3+
import static java.lang.Thread.sleep;
4+
import static java.util.concurrent.CompletableFuture.completedFuture;
5+
import static java.util.logging.Level.SEVERE;
6+
7+
import java.util.concurrent.Future;
8+
import java.util.logging.Logger;
9+
10+
import javax.enterprise.context.ApplicationScoped;
11+
12+
import org.eclipse.microprofile.faulttolerance.Asynchronous;
13+
14+
/**
15+
* @author Arun Gupta
16+
* @author Arjan Tijms
17+
*/
18+
@ApplicationScoped
19+
public class MyAsyncBeanMethodLevel {
20+
21+
public static final long AWAIT = 3000;
22+
23+
@Asynchronous
24+
public Future<Integer> addNumbers(int n1, int n2) {
25+
try {
26+
// Simulating a long running query
27+
sleep(AWAIT);
28+
} catch (InterruptedException ex) {
29+
Logger.getLogger(MyAsyncBeanMethodLevel.class.getName()).log(SEVERE, null, ex);
30+
}
31+
32+
return completedFuture(n1 + n2);
33+
}
34+
35+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package org.eclipse.microprofile12.faulttolerance.asynchronous;
2+
3+
import static com.jayway.awaitility.Awaitility.await;
4+
import static java.lang.System.currentTimeMillis;
5+
import static org.eclipse.microprofile12.Libraries.awaitability;
6+
import static org.hamcrest.MatcherAssert.assertThat;
7+
import static org.hamcrest.Matchers.equalTo;
8+
import static org.hamcrest.Matchers.is;
9+
import static org.hamcrest.Matchers.lessThan;
10+
import static org.jboss.shrinkwrap.api.ShrinkWrap.create;
11+
12+
import java.util.concurrent.ExecutionException;
13+
import java.util.concurrent.Future;
14+
15+
import javax.inject.Inject;
16+
17+
import org.jboss.arquillian.container.test.api.Deployment;
18+
import org.jboss.arquillian.junit.Arquillian;
19+
import org.jboss.shrinkwrap.api.asset.EmptyAsset;
20+
import org.jboss.shrinkwrap.api.spec.WebArchive;
21+
import org.junit.Test;
22+
import org.junit.runner.RunWith;
23+
24+
/**
25+
* @author Jakub Marchwicki
26+
* @author Arjan Tijms
27+
*/
28+
@RunWith(Arquillian.class)
29+
public class AsyncClassBeanTest {
30+
31+
@Inject
32+
private MyAsyncBeanClassLevel bean;
33+
34+
@Deployment
35+
public static WebArchive createDeployment() {
36+
return create(WebArchive.class)
37+
.addAsLibraries(awaitability())
38+
.addClasses(MyAsyncBeanClassLevel.class)
39+
.addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml");
40+
}
41+
42+
@Test
43+
public void shouldReturnAsyncSum() throws ExecutionException, InterruptedException {
44+
Integer numberOne = 5;
45+
Integer numberTwo = 10;
46+
47+
long start = currentTimeMillis();
48+
Future<Integer> resultFuture = bean.addNumbers(numberOne, numberTwo);
49+
50+
assertThat(resultFuture.isDone(), is(equalTo(false)));
51+
assertThat(currentTimeMillis() - start, is(lessThan(MyAsyncBeanMethodLevel.AWAIT)));
52+
53+
await().until(() -> resultFuture.isDone());
54+
55+
assertThat(resultFuture.get(), is(equalTo(numberOne + numberTwo)));
56+
}
57+
58+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package org.eclipse.microprofile12.faulttolerance.asynchronous;
2+
3+
import static com.jayway.awaitility.Awaitility.await;
4+
import static java.lang.System.currentTimeMillis;
5+
import static org.eclipse.microprofile12.Libraries.awaitability;
6+
import static org.hamcrest.MatcherAssert.assertThat;
7+
import static org.hamcrest.Matchers.equalTo;
8+
import static org.hamcrest.Matchers.is;
9+
import static org.hamcrest.Matchers.lessThan;
10+
import static org.jboss.shrinkwrap.api.ShrinkWrap.create;
11+
12+
import java.util.concurrent.ExecutionException;
13+
import java.util.concurrent.Future;
14+
15+
import javax.inject.Inject;
16+
17+
import org.jboss.arquillian.container.test.api.Deployment;
18+
import org.jboss.arquillian.junit.Arquillian;
19+
import org.jboss.shrinkwrap.api.asset.EmptyAsset;
20+
import org.jboss.shrinkwrap.api.spec.WebArchive;
21+
import org.junit.Test;
22+
import org.junit.runner.RunWith;
23+
24+
/**
25+
* @author Jakub Marchwicki
26+
* @author Arjan Tijms
27+
*/
28+
@RunWith(Arquillian.class)
29+
public class AsyncMethodBeanTest {
30+
31+
@Inject
32+
private MyAsyncBeanMethodLevel bean;
33+
34+
@Deployment
35+
public static WebArchive createDeployment() {
36+
return create(WebArchive.class)
37+
.addAsLibraries(awaitability())
38+
.addClasses(MyAsyncBeanMethodLevel.class)
39+
.addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml");
40+
}
41+
42+
@Test // Runs on Server
43+
public void shouldReturnAsyncSum() throws ExecutionException, InterruptedException {
44+
Integer numberOne = 5;
45+
Integer numberTwo = 10;
46+
47+
long start = currentTimeMillis();
48+
Future<Integer> resultFuture = bean.addNumbers(numberOne, numberTwo);
49+
50+
assertThat(resultFuture.isDone(), is(equalTo(false)));
51+
assertThat(currentTimeMillis() - start, is(lessThan(MyAsyncBeanMethodLevel.AWAIT)));
52+
53+
await().until(() -> resultFuture.isDone());
54+
55+
assertThat(resultFuture.get(), is(equalTo(numberOne + numberTwo)));
56+
}
57+
58+
}

0 commit comments

Comments
 (0)