I'm testing my Rest APIs on Postman and URL in browser, but I don't know how to test an API with a POST method that takes an object as a parameter. I know there is nothing wrong with my code because it is part of Hyperskill projects and it passed all the tests. It's really just I dont know how to test it and I can't find an answer anywhere.
Here is what I should test and what results I should get:
Request body:
{
"row": 3,
"column": 4
}
Response body:
{
"row": 3,
"column": 4,
}
When I try the same Request body again:
{
"row": 3,
"column": 4
}
Response body should give me an error message:
{
"error": "The ticket has been already purchased!"
}
Instead when I test following URL http://localhost:28852/purchase?row=3&column=4 in browser I get:
Whitelabel Error Page This application has no explicit mapping for /error, so you are seeing this as a fallback. Mon Jan 23 08:53:33 CET 2023 There was an unexpected error (type=Method Not Allowed, status=405).
When I try to test my code in Postman I get:
And here's my full code in Java:
@SpringBootApplication
public class Main {
public static void main(String[] args) {
SpringApplication.run(Main.class, args);
}
}
@RestController
public class SeatController {
public SeatController() {}
private ScreenRoom screenRoom = new ScreenRoom(9, 9);
@GetMapping("/seats")
public ScreenRoom getSeat(){
return screenRoom;
}
@PostMapping("/purchase")
public synchronized ResponseEntity<?> postSeat(@RequestBody Seat seat){
if (seat.getRow() < 1 || seat.getRow() > 9 || seat.getColumn() < 1 || seat.getColumn() > 9) {
return new ResponseEntity<>(Map.of("error", "The number of a row or a column is out of bounds!"), HttpStatus.BAD_REQUEST);
}
if (screenRoom.getAvailable_seats().contains(seat)) {
screenRoom.removeFromAvailableSeats(seat);
return new ResponseEntity<>(seat, HttpStatus.OK);
} else {
return new ResponseEntity<>(Map.of("error", "The ticket has been already purchased!"), HttpStatus.BAD_REQUEST);
}
}
}
public class ScreenRoom {
private int total_rows;
private int total_columns;
private List<Seat> available_seats = new ArrayList<>();
public ScreenRoom(int total_rows, int total_columns) {
this.total_rows = total_rows;
this.total_columns = total_columns;
for (int i = 1; i <= total_rows; i++) {
for (int j = 1; j <= total_columns; j++) {
available_seats.add(new Seat(i, j));
}
}
}
public int getTotal_rows() {
return total_rows;
}
public void setTotal_rows(int total_rows) {
this.total_rows = total_rows;
}
public int getTotal_columns() {
return total_columns;
}
public void setTotal_columns(int total_columns) {
this.total_columns = total_columns;
}
public List<Seat> getAvailable_seats() {
return available_seats;
}
public void setAvailable_seats(List<Seat> available_seats) {
this.available_seats = available_seats;
}
public void removeFromAvailableSeats(Seat seat) {
this.available_seats.remove(seat);
}
public void getSeat(Seat seat) {
this.available_seats.get(this.available_seats.indexOf(seat));
}
}
public class Seat {
private int row;
private int column;
public Seat(int row, int column) {
this.row = row;
this.column = column;
}
public int getRow() {
return row;
}
public void setRow(int row) {
this.row = row;
}
public int getColumn() {
return column;
}
public void setColumn(int column) {
this.column = column;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Seat seat = (Seat) o;
if (row != seat.row) return false;
return column == seat.column;
}
}
What exactly I'm doing wrong regarding testing? Thank you for your time and answers.