Skip to content

Commit 4b12d00

Browse files
committed
OCP-DEMO Create Thorntail-v4 account microservice
1 parent 9946143 commit 4b12d00

File tree

12 files changed

+385
-0
lines changed

12 files changed

+385
-0
lines changed
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>org.hibernate.demos.messageboard</groupId>
8+
<artifactId>account-micro</artifactId>
9+
<version>1.0.0-SNAPSHOT</version>
10+
<packaging>jar</packaging>
11+
12+
<name>Account MicroService - Thorntail v4</name>
13+
<description>Preview of Account MicroService - using Thorntail v4</description>
14+
15+
<properties>
16+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
17+
18+
<maven.compiler.target>1.8</maven.compiler.target>
19+
<maven.compiler.source>1.8</maven.compiler.source>
20+
21+
<!-- TODO: looking forward to 4.0.0.Final -->
22+
<version.thorntail>4.0.0-SNAPSHOT</version.thorntail>
23+
24+
<version.log4j>2.11.1</version.log4j>
25+
</properties>
26+
27+
<dependencyManagement>
28+
<dependencies>
29+
<dependency>
30+
<groupId>io.thorntail</groupId>
31+
<artifactId>thorntail-bom</artifactId>
32+
<version>${version.thorntail}</version>
33+
<type>pom</type>
34+
<scope>import</scope>
35+
</dependency>
36+
</dependencies>
37+
</dependencyManagement>
38+
39+
<dependencies>
40+
<dependency>
41+
<groupId>io.thorntail</groupId>
42+
<artifactId>thorntail-datasources</artifactId>
43+
</dependency>
44+
45+
<dependency>
46+
<groupId>io.thorntail</groupId>
47+
<artifactId>thorntail-jpa</artifactId>
48+
</dependency>
49+
50+
<dependency>
51+
<groupId>io.thorntail</groupId>
52+
<artifactId>thorntail-jaxrs</artifactId>
53+
</dependency>
54+
55+
<dependency>
56+
<groupId>io.thorntail</groupId>
57+
<artifactId>thorntail-bean-validation</artifactId>
58+
</dependency>
59+
60+
<dependency>
61+
<groupId>io.thorntail</groupId>
62+
<artifactId>thorntail-health</artifactId>
63+
</dependency>
64+
65+
<!-- Use a BRING YOUR OWN log -->
66+
<dependency>
67+
<groupId>org.apache.logging.log4j</groupId>
68+
<artifactId>log4j-slf4j-impl</artifactId>
69+
<version>${version.log4j}</version>
70+
</dependency>
71+
72+
<dependency>
73+
<groupId>com.h2database</groupId>
74+
<artifactId>h2</artifactId>
75+
<version>1.4.197</version>
76+
<scope>test</scope>
77+
</dependency>
78+
79+
<dependency>
80+
<groupId>io.thorntail</groupId>
81+
<artifactId>thorntail-testing</artifactId>
82+
<scope>test</scope>
83+
</dependency>
84+
</dependencies>
85+
86+
</project>
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package org.hibernate.demo.account;
2+
3+
import java.util.Objects;
4+
import javax.persistence.Entity;
5+
import javax.persistence.GeneratedValue;
6+
import javax.persistence.GenerationType;
7+
import javax.persistence.Id;
8+
9+
import org.hibernate.annotations.NaturalId;
10+
11+
@Entity
12+
public class User {
13+
14+
@Id
15+
@GeneratedValue(strategy = GenerationType.SEQUENCE)
16+
private Long id;
17+
18+
@NaturalId
19+
private String userName;
20+
21+
private User() {
22+
}
23+
24+
public User(String userName) {
25+
this.userName = userName;
26+
}
27+
28+
public Long getId() {
29+
return id;
30+
}
31+
32+
public String getUserName() {
33+
return userName;
34+
}
35+
36+
@Override
37+
public boolean equals(Object o) {
38+
if ( this == o ) {
39+
return true;
40+
}
41+
if ( o == null || getClass() != o.getClass() ) {
42+
return false;
43+
}
44+
User user = (User) o;
45+
return Objects.equals( id, user.id ) && Objects.equals( userName, user.userName );
46+
}
47+
48+
@Override
49+
public int hashCode() {
50+
return Objects.hash( id, userName );
51+
}
52+
53+
@Override
54+
public String toString() {
55+
final StringBuilder sb = new StringBuilder( "User{" );
56+
sb.append( "id=" ).append( id );
57+
sb.append( ", userName='" ).append( userName ).append( '\'' );
58+
sb.append( '}' );
59+
return sb.toString();
60+
}
61+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package org.hibernate.demo.account;
2+
3+
import javax.ws.rs.ApplicationPath;
4+
import javax.ws.rs.core.Application;
5+
6+
import io.thorntail.Main;
7+
8+
@ApplicationPath( "/" )
9+
public class UserApp extends Application {
10+
public static void main(String... args) throws Exception {
11+
Main.main(args);
12+
}
13+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package org.hibernate.demo.account;
2+
3+
import java.util.List;
4+
import javax.enterprise.context.ApplicationScoped;
5+
import javax.persistence.EntityManager;
6+
import javax.persistence.PersistenceContext;
7+
import javax.validation.Valid;
8+
9+
import org.hibernate.Session;
10+
11+
@ApplicationScoped
12+
public class UserRepo {
13+
14+
@PersistenceContext
15+
private EntityManager em;
16+
17+
public UserRepo() {
18+
}
19+
20+
public UserRepo(EntityManager em) {
21+
this.em = em;
22+
}
23+
24+
public void add(@Valid User user) {
25+
em.persist( user );
26+
}
27+
28+
public User findByUserName(String userName) {
29+
return em.unwrap( Session.class )
30+
.byNaturalId( User.class ).using( "userName", userName )
31+
.load();
32+
}
33+
34+
public void delete(String userName) {
35+
User user = findByUserName( userName );
36+
if (user != null) {
37+
em.remove( user );
38+
}
39+
}
40+
41+
public List<User> findAll() {
42+
return em.createQuery( "from User u order by u.id" ).getResultList();
43+
}
44+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package org.hibernate.demo.account;
2+
3+
import java.util.List;
4+
import javax.enterprise.context.RequestScoped;
5+
import javax.inject.Inject;
6+
import javax.transaction.Transactional;
7+
import javax.ws.rs.Consumes;
8+
import javax.ws.rs.DELETE;
9+
import javax.ws.rs.GET;
10+
import javax.ws.rs.POST;
11+
import javax.ws.rs.Path;
12+
import javax.ws.rs.PathParam;
13+
import javax.ws.rs.Produces;
14+
import javax.ws.rs.QueryParam;
15+
import javax.ws.rs.core.MediaType;
16+
17+
@Path("/user")
18+
@RequestScoped
19+
@Transactional
20+
@Produces(MediaType.APPLICATION_JSON)
21+
@Consumes(MediaType.APPLICATION_JSON)
22+
public class UserService {
23+
24+
@Inject
25+
private UserRepo repo;
26+
27+
@GET
28+
@Path("all")
29+
public List<User> findAllUsers() {
30+
return repo.findAll();
31+
}
32+
33+
@GET
34+
public User findByUsername(@QueryParam("username") String username) {
35+
return repo.findByUserName( username );
36+
}
37+
38+
@POST
39+
public User createNewUser(User user) {
40+
repo.add( user );
41+
return user;
42+
}
43+
44+
@DELETE
45+
@Path("username/{username}")
46+
public void delete(@PathParam("username") String username) {
47+
repo.delete( username );
48+
}
49+
50+
}

openshift/message-board/account-micro/src/main/resources/META-INF/beans.xml

Whitespace-only changes.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
hibernate.dialect=org.hibernate.dialect.MySQLDialect
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation=" http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
4+
<persistence-unit name="primary">
5+
<jta-data-source>java:jboss/datasources/MyDS</jta-data-source>
6+
<properties>
7+
<property name="hibernate.hbm2ddl.auto" value="update"/>
8+
</properties>
9+
</persistence-unit>
10+
</persistence>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<Configuration status="WARN">
3+
<Appenders>
4+
<Console name="Console" target="SYSTEM_OUT">
5+
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
6+
</Console>
7+
</Appenders>
8+
<Loggers>
9+
<Root level="info">
10+
<AppenderRef ref="Console"/>
11+
</Root>
12+
</Loggers>
13+
</Configuration>
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
package org.hibernate.demo.account;
2+
3+
import static io.restassured.RestAssured.given;
4+
import static io.restassured.RestAssured.when;
5+
import static org.assertj.core.api.Assertions.assertThat;
6+
import static org.hamcrest.Matchers.notNullValue;
7+
8+
import java.util.List;
9+
import javax.inject.Inject;
10+
import javax.transaction.UserTransaction;
11+
12+
import org.junit.Test;
13+
import org.junit.runner.RunWith;
14+
15+
import io.restassured.http.ContentType;
16+
import io.thorntail.test.ThorntailTestRunner;
17+
18+
/**
19+
* @author Fabio Massimo Ercoli
20+
*/
21+
@RunWith(ThorntailTestRunner.class)
22+
public class UserAppTest {
23+
24+
@Inject
25+
private UserRepo repo;
26+
27+
@Inject
28+
private UserTransaction utx;
29+
30+
@Test
31+
public void testCreateAndDelete_OnRepo() throws Exception {
32+
User hibernate = new User( "hibernate" );
33+
34+
utx.begin();
35+
repo.add( hibernate );
36+
utx.commit();
37+
38+
assertThat( hibernate.getId() ).isNotNull();
39+
40+
utx.begin();
41+
List<User> allUsers = repo.findAll();
42+
utx.commit();
43+
44+
assertThat( allUsers ).containsExactly( hibernate );
45+
46+
utx.begin();
47+
User reloaded = repo.findByUserName( "hibernate" );
48+
utx.commit();
49+
50+
assertThat( reloaded ).isEqualTo( hibernate );
51+
52+
utx.begin();
53+
repo.delete( "hibernate" );
54+
utx.commit();
55+
56+
utx.begin();
57+
allUsers = repo.findAll();
58+
utx.commit();
59+
60+
assertThat( allUsers ).isEmpty();
61+
}
62+
63+
@Test
64+
public void testCreateAndDelete_onService() throws Exception {
65+
User hibernate = new User( "hibernate" );
66+
67+
given().body( hibernate ).contentType( ContentType.JSON )
68+
.when().post( "/user" )
69+
.then().statusCode( 200 )
70+
.body( "id", notNullValue() );
71+
72+
User[] users = given().accept( ContentType.JSON )
73+
.when().get( "/user/all" )
74+
.as( User[].class );
75+
76+
assertThat( users ).hasSize( 1 );
77+
hibernate = users[0];
78+
79+
User reloaded = given().accept( ContentType.JSON )
80+
.queryParam( "username", "hibernate" )
81+
.when().get( "/user" )
82+
.as( User.class );
83+
84+
assertThat( reloaded ).isEqualTo( hibernate );
85+
86+
when().delete( "/user/username/hibernate" )
87+
.then().statusCode( 204 );
88+
89+
users = given().accept( ContentType.JSON )
90+
.when().get( "/user/all" )
91+
.as( User[].class );
92+
93+
assertThat( users ).isEmpty();
94+
}
95+
96+
}

0 commit comments

Comments
 (0)