Update README.md

This commit is contained in:
Jonathan Cook
2019-10-13 23:40:45 +02:00
committed by GitHub
parent db85c8f275
commit d17e102e39
20392 changed files with 1639367 additions and 0 deletions
@@ -0,0 +1,22 @@
package com.baeldung.jeekotlin.entity
import com.fasterxml.jackson.annotation.JsonProperty
import javax.persistence.*
@Entity
data class Student constructor (
@SequenceGenerator(name = "student_id_seq", sequenceName = "student_id_seq", allocationSize = 1)
@GeneratedValue(generator = "student_id_seq", strategy = GenerationType.SEQUENCE)
@Id
var id: Long?,
var firstName: String,
var lastName: String
) {
constructor() : this(null, "", "")
constructor(firstName: String, lastName: String) : this(null, firstName, lastName)
}
@@ -0,0 +1,9 @@
package com.baeldung.jeekotlin.rest
import javax.ws.rs.ApplicationPath
import javax.ws.rs.core.Application
@ApplicationPath("/")
class ApplicationConfig : Application() {
override fun getClasses() = setOf(StudentResource::class.java)
}
@@ -0,0 +1,42 @@
package com.baeldung.jeekotlin.rest
import com.baeldung.jeekotlin.entity.Student
import com.baeldung.jeekotlin.service.StudentService
import javax.inject.Inject
import javax.ws.rs.*
import javax.ws.rs.core.MediaType
import javax.ws.rs.core.Response
@Path("/student")
open class StudentResource {
@Inject
private lateinit var service: StudentService
@POST
open fun create(student: Student): Response {
service.create(student)
return Response.ok().build()
}
@GET
@Path("/{id}")
open fun read(@PathParam("id") id: Long): Response {
val student = service.read(id)
return Response.ok(student, MediaType.APPLICATION_JSON_TYPE).build()
}
@PUT
open fun update(student: Student): Response {
service.update(student)
return Response.ok(student, MediaType.APPLICATION_JSON_TYPE).build()
}
@DELETE
@Path("/{id}")
open fun delete(@PathParam("id") id: Long): Response {
service.delete(id)
return Response.noContent().build()
}
}
@@ -0,0 +1,21 @@
package com.baeldung.jeekotlin.service
import com.baeldung.jeekotlin.entity.Student
import javax.ejb.Stateless
import javax.persistence.EntityManager
import javax.persistence.PersistenceContext
@Stateless
open class StudentService {
@PersistenceContext
private lateinit var entityManager: EntityManager
open fun create(student: Student) = entityManager.persist(student)
open fun read(id: Long): Student? = entityManager.find(Student::class.java, id)
open fun update(student: Student) = entityManager.merge(student)
open fun delete(id: Long) = entityManager.remove(read(id))
}
@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="2.0">
<persistence-unit name="pu" transaction-type="JTA">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<jta-data-source>java:jboss/datasources/ExampleDS</jta-data-source>
<class>com.enpy.entity.Student</class>
<properties>
<property name="hibernate.hbm2ddl.auto" value="create"/>
</properties>
</persistence-unit>
</persistence>
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/beans_1_1.xsd"
bean-discovery-mode="all">
</beans>
@@ -0,0 +1,108 @@
package com.baeldung.jeekotlin;
import com.baeldung.jeekotlin.entity.Student;
import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.arquillian.container.test.api.RunAsClient;
import org.jboss.arquillian.test.api.ArquillianResource;
import org.jboss.arquillian.junit.Arquillian;
import org.jboss.shrinkwrap.api.Filters;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.asset.EmptyAsset;
import org.jboss.shrinkwrap.api.spec.JavaArchive;
import org.jboss.shrinkwrap.api.spec.WebArchive;
import org.jboss.shrinkwrap.resolver.api.maven.Maven;
import org.junit.Test;
import org.junit.runner.RunWith;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import javax.ws.rs.client.Entity;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import java.net.URISyntaxException;
import java.net.URL;
import static org.junit.Assert.assertEquals;
@RunWith(Arquillian.class)
public class StudentResourceIntegrationTest {
@Deployment
public static WebArchive createDeployment() {
JavaArchive[] kotlinRuntime = Maven.configureResolver()
.workOffline()
.withMavenCentralRepo(true)
.withClassPathResolution(true)
.loadPomFromFile("pom.xml")
.resolve("org.jetbrains.kotlin:kotlin-stdlib")
.withTransitivity()
.as(JavaArchive.class);
return ShrinkWrap.create(WebArchive.class, "kotlin.war")
.addPackages(true, Filters.exclude(".*Test*"),
"com.baeldung.jeekotlin"
)
.addAsLibraries(kotlinRuntime)
.addAsResource("META-INF/persistence.xml")
.addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml");
}
@Test
@RunAsClient
public void when_post__then_return_ok(@ArquillianResource URL url) throws URISyntaxException, JsonProcessingException {
String student = new ObjectMapper().writeValueAsString(new Student("firstName", "lastName"));
WebTarget webTarget = ClientBuilder.newClient().target(url.toURI());
Response response = webTarget
.path("/student")
.request(MediaType.APPLICATION_JSON)
.post(Entity.json(student));
assertEquals(200, response.getStatus());
}
@Test
@RunAsClient
public void when_get__then_return_ok(@ArquillianResource URL url) throws URISyntaxException, JsonProcessingException {
WebTarget webTarget = ClientBuilder.newClient().target(url.toURI());
Response response = webTarget
.path("/student/1")
.request(MediaType.APPLICATION_JSON)
.get();
assertEquals(200, response.getStatus());
}
@Test
@RunAsClient
public void when_put__then_return_ok(@ArquillianResource URL url) throws URISyntaxException, JsonProcessingException {
Student student = new Student("firstName", "lastName");
student.setId(1L);
String studentJson = new ObjectMapper().writeValueAsString(student);
WebTarget webTarget = ClientBuilder.newClient().target(url.toURI());
Response response = webTarget
.path("/student")
.request(MediaType.APPLICATION_JSON)
.put(Entity.json(studentJson));
assertEquals(200, response.getStatus());
}
@Test
@RunAsClient
public void when_delete__then_return_ok(@ArquillianResource URL url) throws URISyntaxException, JsonProcessingException {
WebTarget webTarget = ClientBuilder.newClient().target(url.toURI());
Response response = webTarget
.path("/student/1")
.request()
.delete();
assertEquals(204, response.getStatus());
}
}
@@ -0,0 +1,22 @@
<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="wildfly-managed">
<configuration>
<property name="jbossHome">target/wildfly-8.2.1.Final</property>
<property name="serverConfig">standalone.xml</property>
<property name="outputToConsole">true</property>
<property name="managementPort">9990</property>
</configuration>
</container>
<container qualifier="wildfly-remote" default="true">
<configuration>
<property name="managementAddress">127.0.0.1</property>
<property name="managementPort">9990</property>
<property name="username">admin</property>
<property name="password">pass</property>
<property name="allowConnectingToRunningServer">true</property>
</configuration>
</container>
</arquillian>