Merge pull request #758 from nguyennamthai/master
Apache CXF Support for RESTful Web Services
This commit is contained in:
commit
1657916676
|
@ -44,15 +44,16 @@ public class Baeldung {
|
|||
|
||||
@PUT
|
||||
@Path("courses/{courseOrder}")
|
||||
public Response putCourse(@PathParam("courseOrder") int courseOrder, Course course) {
|
||||
Course existingCourse = courses.get(courseOrder);
|
||||
|
||||
if (existingCourse == null || existingCourse.getId() != course.getId() || !(existingCourse.getName().equals(course.getName()))) {
|
||||
courses.put(courseOrder, course);
|
||||
return Response.ok().build();
|
||||
public Response updateCourse(@PathParam("courseOrder") int courseOrder, Course course) {
|
||||
Course existingCourse = courses.get(courseOrder);
|
||||
if (existingCourse == null) {
|
||||
return Response.status(Response.Status.NOT_FOUND).build();
|
||||
}
|
||||
|
||||
return Response.notModified().build();
|
||||
if (existingCourse.equals(course)) {
|
||||
return Response.notModified().build();
|
||||
}
|
||||
courses.put(courseOrder, course);
|
||||
return Response.ok().build();
|
||||
}
|
||||
|
||||
@Path("courses/{courseOrder}/students")
|
||||
|
|
|
@ -3,6 +3,7 @@ package com.baeldung.cxf.jaxrs.implementation;
|
|||
import javax.ws.rs.*;
|
||||
import javax.ws.rs.core.Response;
|
||||
import javax.xml.bind.annotation.XmlRootElement;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
|
@ -35,31 +36,42 @@ public class Course {
|
|||
public void setStudents(List<Student> students) {
|
||||
this.students = students;
|
||||
}
|
||||
|
||||
|
||||
@GET
|
||||
@Path("{studentOrder}")
|
||||
public Student getStudent(@PathParam("studentOrder")int studentOrder) {
|
||||
public Student getStudent(@PathParam("studentOrder") int studentOrder) {
|
||||
return students.get(studentOrder);
|
||||
}
|
||||
|
||||
|
||||
@POST
|
||||
public Response postStudent(Student student) {
|
||||
public Response createStudent(Student student) {
|
||||
if (students == null) {
|
||||
students = new ArrayList<>();
|
||||
}
|
||||
if (students.contains(student)) {
|
||||
return Response.status(Response.Status.CONFLICT).build();
|
||||
}
|
||||
students.add(student);
|
||||
return Response.ok(student).build();
|
||||
}
|
||||
|
||||
|
||||
@DELETE
|
||||
@Path("{studentOrder}")
|
||||
public Response deleteStudent(@PathParam("studentOrder") int studentOrder) {
|
||||
Student student = students.get(studentOrder);
|
||||
if (student != null) {
|
||||
students.remove(studentOrder);
|
||||
return Response.ok().build();
|
||||
} else {
|
||||
return Response.notModified().build();
|
||||
if (students == null || studentOrder >= students.size()) {
|
||||
return Response.status(Response.Status.NOT_FOUND).build();
|
||||
}
|
||||
students.remove(studentOrder);
|
||||
return Response.ok().build();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return id + name.hashCode();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
return (obj instanceof Course) && (id == ((Course) obj).getId()) && (name.equals(((Course) obj).getName()));
|
||||
}
|
||||
}
|
|
@ -22,4 +22,14 @@ public class Student {
|
|||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return id + name.hashCode();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
return (obj instanceof Student) && (id == ((Student) obj).getId()) && (name.equals(((Student) obj).getName()));
|
||||
}
|
||||
}
|
|
@ -0,0 +1,4 @@
|
|||
<Course>
|
||||
<id>2</id>
|
||||
<name>Apache CXF Support for RESTful</name>
|
||||
</Course>
|
|
@ -0,0 +1,4 @@
|
|||
<Student>
|
||||
<id>1</id>
|
||||
<name>Student A</name>
|
||||
</Student>
|
|
@ -1,4 +1,3 @@
|
|||
|
||||
<Student>
|
||||
<id>3</id>
|
||||
<name>Student C</name>
|
|
@ -0,0 +1,4 @@
|
|||
<Course>
|
||||
<id>1</id>
|
||||
<name>REST with Spring</name>
|
||||
</Course>
|
|
@ -34,24 +34,57 @@ public class ServiceTest {
|
|||
}
|
||||
|
||||
@Test
|
||||
public void whenPutCourse_thenCorrect() throws IOException {
|
||||
public void whenUpdateNonExistentCourse_thenReceiveNotFoundResponse() throws IOException {
|
||||
HttpPut httpPut = new HttpPut(BASE_URL + "3");
|
||||
InputStream resourceStream = this.getClass().getClassLoader().getResourceAsStream("course.xml");
|
||||
InputStream resourceStream = this.getClass().getClassLoader().getResourceAsStream("non_existent_course.xml");
|
||||
httpPut.setEntity(new InputStreamEntity(resourceStream));
|
||||
httpPut.setHeader("Content-Type", "text/xml");
|
||||
|
||||
HttpResponse response = client.execute(httpPut);
|
||||
assertEquals(404, response.getStatusLine().getStatusCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenUpdateUnchangedCourse_thenReceiveNotModifiedResponse() throws IOException {
|
||||
HttpPut httpPut = new HttpPut(BASE_URL + "1");
|
||||
InputStream resourceStream = this.getClass().getClassLoader().getResourceAsStream("unchanged_course.xml");
|
||||
httpPut.setEntity(new InputStreamEntity(resourceStream));
|
||||
httpPut.setHeader("Content-Type", "text/xml");
|
||||
|
||||
HttpResponse response = client.execute(httpPut);
|
||||
assertEquals(304, response.getStatusLine().getStatusCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenUpdateValidCourse_thenReceiveOKResponse() throws IOException {
|
||||
HttpPut httpPut = new HttpPut(BASE_URL + "2");
|
||||
InputStream resourceStream = this.getClass().getClassLoader().getResourceAsStream("changed_course.xml");
|
||||
httpPut.setEntity(new InputStreamEntity(resourceStream));
|
||||
httpPut.setHeader("Content-Type", "text/xml");
|
||||
|
||||
HttpResponse response = client.execute(httpPut);
|
||||
assertEquals(200, response.getStatusLine().getStatusCode());
|
||||
|
||||
Course course = getCourse(3);
|
||||
assertEquals(3, course.getId());
|
||||
Course course = getCourse(2);
|
||||
assertEquals(2, course.getId());
|
||||
assertEquals("Apache CXF Support for RESTful", course.getName());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCreateConflictStudent_thenReceiveConflictResponse() throws IOException {
|
||||
HttpPost httpPost = new HttpPost(BASE_URL + "1/students");
|
||||
InputStream resourceStream = this.getClass().getClassLoader().getResourceAsStream("conflict_student.xml");
|
||||
httpPost.setEntity(new InputStreamEntity(resourceStream));
|
||||
httpPost.setHeader("Content-Type", "text/xml");
|
||||
|
||||
HttpResponse response = client.execute(httpPost);
|
||||
assertEquals(409, response.getStatusLine().getStatusCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenPostStudent_thenCorrect() throws IOException {
|
||||
public void whenCreateValidStudent_thenReceiveOKResponse() throws IOException {
|
||||
HttpPost httpPost = new HttpPost(BASE_URL + "2/students");
|
||||
InputStream resourceStream = this.getClass().getClassLoader().getResourceAsStream("student.xml");
|
||||
InputStream resourceStream = this.getClass().getClassLoader().getResourceAsStream("created_student.xml");
|
||||
httpPost.setEntity(new InputStreamEntity(resourceStream));
|
||||
httpPost.setHeader("Content-Type", "text/xml");
|
||||
|
||||
|
@ -64,7 +97,14 @@ public class ServiceTest {
|
|||
}
|
||||
|
||||
@Test
|
||||
public void whenDeleteStudent_thenCorrect() throws IOException {
|
||||
public void whenDeleteInvalidStudent_thenReceiveNotFoundResponse() throws IOException {
|
||||
HttpDelete httpDelete = new HttpDelete(BASE_URL + "1/students/2");
|
||||
HttpResponse response = client.execute(httpDelete);
|
||||
assertEquals(404, response.getStatusLine().getStatusCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenDeleteValidStudent_thenReceiveOKResponse() throws IOException {
|
||||
HttpDelete httpDelete = new HttpDelete(BASE_URL + "1/students/0");
|
||||
HttpResponse response = client.execute(httpDelete);
|
||||
assertEquals(200, response.getStatusLine().getStatusCode());
|
||||
|
|
Loading…
Reference in New Issue