diff --git a/apache-cxf/cxf-jaxrs-implementation/src/main/java/com/baeldung/cxf/jaxrs/implementation/Baeldung.java b/apache-cxf/cxf-jaxrs-implementation/src/main/java/com/baeldung/cxf/jaxrs/implementation/Baeldung.java index 592df4b7c3..39872f4e32 100644 --- a/apache-cxf/cxf-jaxrs-implementation/src/main/java/com/baeldung/cxf/jaxrs/implementation/Baeldung.java +++ b/apache-cxf/cxf-jaxrs-implementation/src/main/java/com/baeldung/cxf/jaxrs/implementation/Baeldung.java @@ -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") diff --git a/apache-cxf/cxf-jaxrs-implementation/src/main/java/com/baeldung/cxf/jaxrs/implementation/Course.java b/apache-cxf/cxf-jaxrs-implementation/src/main/java/com/baeldung/cxf/jaxrs/implementation/Course.java index 32689a332f..efb7191290 100644 --- a/apache-cxf/cxf-jaxrs-implementation/src/main/java/com/baeldung/cxf/jaxrs/implementation/Course.java +++ b/apache-cxf/cxf-jaxrs-implementation/src/main/java/com/baeldung/cxf/jaxrs/implementation/Course.java @@ -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 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())); } } \ No newline at end of file diff --git a/apache-cxf/cxf-jaxrs-implementation/src/main/java/com/baeldung/cxf/jaxrs/implementation/Student.java b/apache-cxf/cxf-jaxrs-implementation/src/main/java/com/baeldung/cxf/jaxrs/implementation/Student.java index de782e4edb..bd3dad0f5e 100644 --- a/apache-cxf/cxf-jaxrs-implementation/src/main/java/com/baeldung/cxf/jaxrs/implementation/Student.java +++ b/apache-cxf/cxf-jaxrs-implementation/src/main/java/com/baeldung/cxf/jaxrs/implementation/Student.java @@ -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())); + } } \ No newline at end of file diff --git a/apache-cxf/cxf-jaxrs-implementation/src/main/resources/changed_course.xml b/apache-cxf/cxf-jaxrs-implementation/src/main/resources/changed_course.xml new file mode 100644 index 0000000000..097cf2ce58 --- /dev/null +++ b/apache-cxf/cxf-jaxrs-implementation/src/main/resources/changed_course.xml @@ -0,0 +1,4 @@ + + 2 + Apache CXF Support for RESTful + \ No newline at end of file diff --git a/apache-cxf/cxf-jaxrs-implementation/src/main/resources/conflict_student.xml b/apache-cxf/cxf-jaxrs-implementation/src/main/resources/conflict_student.xml new file mode 100644 index 0000000000..ccf97edb2e --- /dev/null +++ b/apache-cxf/cxf-jaxrs-implementation/src/main/resources/conflict_student.xml @@ -0,0 +1,4 @@ + + 1 + Student A + \ No newline at end of file diff --git a/apache-cxf/cxf-jaxrs-implementation/src/main/resources/student.xml b/apache-cxf/cxf-jaxrs-implementation/src/main/resources/created_student.xml similarity index 82% rename from apache-cxf/cxf-jaxrs-implementation/src/main/resources/student.xml rename to apache-cxf/cxf-jaxrs-implementation/src/main/resources/created_student.xml index 7fb6189815..068c9dae4b 100644 --- a/apache-cxf/cxf-jaxrs-implementation/src/main/resources/student.xml +++ b/apache-cxf/cxf-jaxrs-implementation/src/main/resources/created_student.xml @@ -1,4 +1,3 @@ - 3 Student C diff --git a/apache-cxf/cxf-jaxrs-implementation/src/main/resources/course.xml b/apache-cxf/cxf-jaxrs-implementation/src/main/resources/non_existent_course.xml similarity index 100% rename from apache-cxf/cxf-jaxrs-implementation/src/main/resources/course.xml rename to apache-cxf/cxf-jaxrs-implementation/src/main/resources/non_existent_course.xml diff --git a/apache-cxf/cxf-jaxrs-implementation/src/main/resources/unchanged_course.xml b/apache-cxf/cxf-jaxrs-implementation/src/main/resources/unchanged_course.xml new file mode 100644 index 0000000000..5936fdc094 --- /dev/null +++ b/apache-cxf/cxf-jaxrs-implementation/src/main/resources/unchanged_course.xml @@ -0,0 +1,4 @@ + + 1 + REST with Spring + \ No newline at end of file diff --git a/apache-cxf/cxf-jaxrs-implementation/src/test/java/com/baeldung/cxf/jaxrs/implementation/ServiceTest.java b/apache-cxf/cxf-jaxrs-implementation/src/test/java/com/baeldung/cxf/jaxrs/implementation/ServiceTest.java index 8c606436c8..8d06518df2 100644 --- a/apache-cxf/cxf-jaxrs-implementation/src/test/java/com/baeldung/cxf/jaxrs/implementation/ServiceTest.java +++ b/apache-cxf/cxf-jaxrs-implementation/src/test/java/com/baeldung/cxf/jaxrs/implementation/ServiceTest.java @@ -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());