Skip to content

Commit 72bdf6c

Browse files
committed
Changes HTTP request handling methods, adds negative test cases
1 parent fa33f0b commit 72bdf6c

9 files changed

Lines changed: 101 additions & 27 deletions

File tree

‎apache-cxf/cxf-jaxrs-implementation/src/main/java/com/baeldung/cxf/jaxrs/implementation/Baeldung.java‎

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,15 +44,16 @@ public Course getCourse(@PathParam("courseOrder") int courseOrder) {
4444

4545
@PUT
4646
@Path("courses/{courseOrder}")
47-
public Response putCourse(@PathParam("courseOrder") int courseOrder, Course course) {
48-
Course existingCourse = courses.get(courseOrder);
49-
50-
if (existingCourse == null || existingCourse.getId() != course.getId() || !(existingCourse.getName().equals(course.getName()))) {
51-
courses.put(courseOrder, course);
52-
return Response.ok().build();
47+
public Response updateCourse(@PathParam("courseOrder") int courseOrder, Course course) {
48+
Course existingCourse = courses.get(courseOrder);
49+
if (existingCourse == null) {
50+
return Response.status(Response.Status.NOT_FOUND).build();
5351
}
54-
55-
return Response.notModified().build();
52+
if (existingCourse.equals(course)) {
53+
return Response.notModified().build();
54+
}
55+
courses.put(courseOrder, course);
56+
return Response.ok().build();
5657
}
5758

5859
@Path("courses/{courseOrder}/students")

‎apache-cxf/cxf-jaxrs-implementation/src/main/java/com/baeldung/cxf/jaxrs/implementation/Course.java‎

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import javax.ws.rs.*;
44
import javax.ws.rs.core.Response;
55
import javax.xml.bind.annotation.XmlRootElement;
6+
67
import java.util.ArrayList;
78
import java.util.List;
89

@@ -35,31 +36,42 @@ public List<Student> getStudents() {
3536
public void setStudents(List<Student> students) {
3637
this.students = students;
3738
}
38-
39+
3940
@GET
4041
@Path("{studentOrder}")
41-
public Student getStudent(@PathParam("studentOrder")int studentOrder) {
42+
public Student getStudent(@PathParam("studentOrder") int studentOrder) {
4243
return students.get(studentOrder);
4344
}
44-
45+
4546
@POST
46-
public Response postStudent(Student student) {
47+
public Response createStudent(Student student) {
4748
if (students == null) {
4849
students = new ArrayList<>();
4950
}
51+
if (students.contains(student)) {
52+
return Response.status(Response.Status.CONFLICT).build();
53+
}
5054
students.add(student);
5155
return Response.ok(student).build();
5256
}
53-
57+
5458
@DELETE
5559
@Path("{studentOrder}")
5660
public Response deleteStudent(@PathParam("studentOrder") int studentOrder) {
57-
Student student = students.get(studentOrder);
58-
if (student != null) {
59-
students.remove(studentOrder);
60-
return Response.ok().build();
61-
} else {
62-
return Response.notModified().build();
61+
if (students == null || studentOrder >= students.size()) {
62+
return Response.status(Response.Status.NOT_FOUND).build();
6363
}
64+
students.remove(studentOrder);
65+
return Response.ok().build();
66+
}
67+
68+
@Override
69+
public int hashCode() {
70+
return id + name.hashCode();
71+
}
72+
73+
@Override
74+
public boolean equals(Object obj) {
75+
return (obj instanceof Course) && (id == ((Course) obj).getId()) && (name.equals(((Course) obj).getName()));
6476
}
6577
}

‎apache-cxf/cxf-jaxrs-implementation/src/main/java/com/baeldung/cxf/jaxrs/implementation/Student.java‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,14 @@ public String getName() {
2222
public void setName(String name) {
2323
this.name = name;
2424
}
25+
26+
@Override
27+
public int hashCode() {
28+
return id + name.hashCode();
29+
}
30+
31+
@Override
32+
public boolean equals(Object obj) {
33+
return (obj instanceof Student) && (id == ((Student) obj).getId()) && (name.equals(((Student) obj).getName()));
34+
}
2535
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<Course>
2+
<id>2</id>
3+
<name>Apache CXF Support for RESTful</name>
4+
</Course>
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<Student>
2+
<id>1</id>
3+
<name>Student A</name>
4+
</Student>

apache-cxf/cxf-jaxrs-implementation/src/main/resources/student.xml renamed to apache-cxf/cxf-jaxrs-implementation/src/main/resources/created_student.xml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
<Student>
32
<id>3</id>
43
<name>Student C</name>

apache-cxf/cxf-jaxrs-implementation/src/main/resources/course.xml renamed to apache-cxf/cxf-jaxrs-implementation/src/main/resources/non_existent_course.xml

File renamed without changes.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<Course>
2+
<id>1</id>
3+
<name>REST with Spring</name>
4+
</Course>

‎apache-cxf/cxf-jaxrs-implementation/src/test/java/com/baeldung/cxf/jaxrs/implementation/ServiceTest.java‎

Lines changed: 47 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,24 +34,57 @@ public static void closeClient() throws IOException {
3434
}
3535

3636
@Test
37-
public void whenPutCourse_thenCorrect() throws IOException {
37+
public void whenUpdateNonExistentCourse_thenReceiveNotFoundResponse() throws IOException {
3838
HttpPut httpPut = new HttpPut(BASE_URL + "3");
39-
InputStream resourceStream = this.getClass().getClassLoader().getResourceAsStream("course.xml");
39+
InputStream resourceStream = this.getClass().getClassLoader().getResourceAsStream("non_existent_course.xml");
40+
httpPut.setEntity(new InputStreamEntity(resourceStream));
41+
httpPut.setHeader("Content-Type", "text/xml");
42+
43+
HttpResponse response = client.execute(httpPut);
44+
assertEquals(404, response.getStatusLine().getStatusCode());
45+
}
46+
47+
@Test
48+
public void whenUpdateUnchangedCourse_thenReceiveNotModifiedResponse() throws IOException {
49+
HttpPut httpPut = new HttpPut(BASE_URL + "1");
50+
InputStream resourceStream = this.getClass().getClassLoader().getResourceAsStream("unchanged_course.xml");
51+
httpPut.setEntity(new InputStreamEntity(resourceStream));
52+
httpPut.setHeader("Content-Type", "text/xml");
53+
54+
HttpResponse response = client.execute(httpPut);
55+
assertEquals(304, response.getStatusLine().getStatusCode());
56+
}
57+
58+
@Test
59+
public void whenUpdateValidCourse_thenReceiveOKResponse() throws IOException {
60+
HttpPut httpPut = new HttpPut(BASE_URL + "2");
61+
InputStream resourceStream = this.getClass().getClassLoader().getResourceAsStream("changed_course.xml");
4062
httpPut.setEntity(new InputStreamEntity(resourceStream));
4163
httpPut.setHeader("Content-Type", "text/xml");
4264

4365
HttpResponse response = client.execute(httpPut);
4466
assertEquals(200, response.getStatusLine().getStatusCode());
4567

46-
Course course = getCourse(3);
47-
assertEquals(3, course.getId());
68+
Course course = getCourse(2);
69+
assertEquals(2, course.getId());
4870
assertEquals("Apache CXF Support for RESTful", course.getName());
4971
}
72+
73+
@Test
74+
public void whenCreateConflictStudent_thenReceiveConflictResponse() throws IOException {
75+
HttpPost httpPost = new HttpPost(BASE_URL + "1/students");
76+
InputStream resourceStream = this.getClass().getClassLoader().getResourceAsStream("conflict_student.xml");
77+
httpPost.setEntity(new InputStreamEntity(resourceStream));
78+
httpPost.setHeader("Content-Type", "text/xml");
79+
80+
HttpResponse response = client.execute(httpPost);
81+
assertEquals(409, response.getStatusLine().getStatusCode());
82+
}
5083

5184
@Test
52-
public void whenPostStudent_thenCorrect() throws IOException {
85+
public void whenCreateValidStudent_thenReceiveOKResponse() throws IOException {
5386
HttpPost httpPost = new HttpPost(BASE_URL + "2/students");
54-
InputStream resourceStream = this.getClass().getClassLoader().getResourceAsStream("student.xml");
87+
InputStream resourceStream = this.getClass().getClassLoader().getResourceAsStream("created_student.xml");
5588
httpPost.setEntity(new InputStreamEntity(resourceStream));
5689
httpPost.setHeader("Content-Type", "text/xml");
5790

@@ -64,7 +97,14 @@ public void whenPostStudent_thenCorrect() throws IOException {
6497
}
6598

6699
@Test
67-
public void whenDeleteStudent_thenCorrect() throws IOException {
100+
public void whenDeleteInvalidStudent_thenReceiveNotFoundResponse() throws IOException {
101+
HttpDelete httpDelete = new HttpDelete(BASE_URL + "1/students/2");
102+
HttpResponse response = client.execute(httpDelete);
103+
assertEquals(404, response.getStatusLine().getStatusCode());
104+
}
105+
106+
@Test
107+
public void whenDeleteValidStudent_thenReceiveOKResponse() throws IOException {
68108
HttpDelete httpDelete = new HttpDelete(BASE_URL + "1/students/0");
69109
HttpResponse response = client.execute(httpDelete);
70110
assertEquals(200, response.getStatusLine().getStatusCode());

0 commit comments

Comments
 (0)