diff --git a/src/main/java/de/hs_mannheim/informatik/ct/controller/ErrorController.java b/src/main/java/de/hs_mannheim/informatik/ct/controller/ErrorController.java index 7582a44e..2b42e7d8 100644 --- a/src/main/java/de/hs_mannheim/informatik/ct/controller/ErrorController.java +++ b/src/main/java/de/hs_mannheim/informatik/ct/controller/ErrorController.java @@ -21,11 +21,13 @@ import java.io.PrintWriter; import java.io.StringWriter; import java.io.UnsupportedEncodingException; +import java.util.Date; import javax.servlet.RequestDispatcher; import javax.servlet.http.HttpServletRequest; import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.ControllerAdvice; import org.springframework.web.bind.annotation.ExceptionHandler; @@ -33,6 +35,9 @@ import org.springframework.web.bind.annotation.ResponseStatus; import de.hs_mannheim.informatik.ct.controller.exception.InvalidRoomPinException; +import de.hs_mannheim.informatik.ct.controller.rest.InvalidPinRestException; +import de.hs_mannheim.informatik.ct.controller.rest.RestErrorMessage; +import de.hs_mannheim.informatik.ct.controller.rest.RoomNotFoundRestException; import de.hs_mannheim.informatik.ct.persistence.EventNotFoundException; import de.hs_mannheim.informatik.ct.persistence.InvalidEmailException; import de.hs_mannheim.informatik.ct.persistence.InvalidExternalUserdataException; @@ -42,6 +47,21 @@ @ControllerAdvice @Slf4j public class ErrorController { + + @ExceptionHandler(value = {RoomNotFoundRestException.class}) + public ResponseEntity handleRoomNotFoundRestException() { + RestErrorMessage rem = new RestErrorMessage(404, new Date(), "Room not found."); + + return new ResponseEntity(rem, HttpStatus.NOT_FOUND); + } + + @ExceptionHandler(value = {InvalidPinRestException.class}) + public ResponseEntity handleRoomPinException() { + RestErrorMessage rem = new RestErrorMessage(400, new Date(), "Room Pin not valid or not submitted."); + + return new ResponseEntity(rem, HttpStatus.BAD_REQUEST); + } + @ExceptionHandler({RoomController.RoomNotFoundException.class}) @ResponseStatus(value = HttpStatus.NOT_FOUND) // @ResponseStatus(value = HttpStatus.NOT_FOUND, reason = "Room not found") diff --git a/src/main/java/de/hs_mannheim/informatik/ct/controller/rest/InvalidPinRestException.java b/src/main/java/de/hs_mannheim/informatik/ct/controller/rest/InvalidPinRestException.java new file mode 100644 index 00000000..8ff18dcd --- /dev/null +++ b/src/main/java/de/hs_mannheim/informatik/ct/controller/rest/InvalidPinRestException.java @@ -0,0 +1,8 @@ +package de.hs_mannheim.informatik.ct.controller.rest; + +import org.springframework.http.HttpStatus; +import org.springframework.web.bind.annotation.ResponseStatus; + +@ResponseStatus(code = HttpStatus.NOT_FOUND, reason = "Room not found") +public class InvalidPinRestException extends RuntimeException { +} diff --git a/src/main/java/de/hs_mannheim/informatik/ct/controller/rest/RestErrorMessage.java b/src/main/java/de/hs_mannheim/informatik/ct/controller/rest/RestErrorMessage.java new file mode 100644 index 00000000..65d5e53c --- /dev/null +++ b/src/main/java/de/hs_mannheim/informatik/ct/controller/rest/RestErrorMessage.java @@ -0,0 +1,29 @@ +package de.hs_mannheim.informatik.ct.controller.rest; + +import java.util.Date; + +public class RestErrorMessage { + private int statusCode; + private Date timestamp; + private String message; + + public RestErrorMessage(int statusCode, Date timestamp, String message) { + super(); + this.statusCode = statusCode; + this.timestamp = timestamp; + this.message = message; + } + + public int getStatusCode() { + return statusCode; + } + + public Date getTimestamp() { + return timestamp; + } + + public String getMessage() { + return message; + } + +} diff --git a/src/main/java/de/hs_mannheim/informatik/ct/controller/rest/RestRoomController.java b/src/main/java/de/hs_mannheim/informatik/ct/controller/rest/RestRoomController.java new file mode 100644 index 00000000..31ad4e77 --- /dev/null +++ b/src/main/java/de/hs_mannheim/informatik/ct/controller/rest/RestRoomController.java @@ -0,0 +1,61 @@ +/* + * Corona Tracking Tool der Hochschule Mannheim + * Copyright (c) 2021 Hochschule Mannheim + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +package de.hs_mannheim.informatik.ct.controller.rest; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; + +import de.hs_mannheim.informatik.ct.controller.RoomController.RoomNotFoundException; +import de.hs_mannheim.informatik.ct.controller.exception.InvalidRoomPinException; +import de.hs_mannheim.informatik.ct.persistence.services.RoomService; +import de.hs_mannheim.informatik.ct.persistence.services.RoomVisitService; +import lombok.val; + +@RestController +@RequestMapping("/api") +public class RestRoomController { // couldn't resist using this name ;-) + @Autowired + private RoomVisitService rvs; + + @Autowired + private RoomService roomService; + + @RequestMapping(value="/rooms/{roomId}/visitors", method=RequestMethod.GET) + public String getNumberOfCheckedInVisitors(@PathVariable(value = "roomId") String id, + @RequestParam(value = "pin", required=false) String pin) + throws InvalidRoomPinException, RoomNotFoundRestException { + + try { + val room = roomService.getRoomOrThrow(id); + + if (pin == null || !room.getRoomPin().equals(pin)) + throw new InvalidPinRestException(); + + return "" + rvs.getVisitorCount(room); + } catch(RoomNotFoundException rnfe) { + throw new RoomNotFoundRestException(); + } + + } + +} \ No newline at end of file diff --git a/src/main/java/de/hs_mannheim/informatik/ct/controller/rest/RoomNotFoundRestException.java b/src/main/java/de/hs_mannheim/informatik/ct/controller/rest/RoomNotFoundRestException.java new file mode 100644 index 00000000..ba062989 --- /dev/null +++ b/src/main/java/de/hs_mannheim/informatik/ct/controller/rest/RoomNotFoundRestException.java @@ -0,0 +1,8 @@ +package de.hs_mannheim.informatik.ct.controller.rest; + +import org.springframework.http.HttpStatus; +import org.springframework.web.bind.annotation.ResponseStatus; + +@ResponseStatus(code = HttpStatus.NOT_FOUND, reason = "Room not found") +public class RoomNotFoundRestException extends RuntimeException { +} \ No newline at end of file diff --git a/src/test/java/de/hs_mannheim/informatik/ct/end_to_end/RoomControllerTest.java b/src/test/java/de/hs_mannheim/informatik/ct/end_to_end/RoomControllerTest.java index b3ec2a3a..48cdcaf0 100644 --- a/src/test/java/de/hs_mannheim/informatik/ct/end_to_end/RoomControllerTest.java +++ b/src/test/java/de/hs_mannheim/informatik/ct/end_to_end/RoomControllerTest.java @@ -19,6 +19,7 @@ package de.hs_mannheim.informatik.ct.end_to_end; import static org.hamcrest.Matchers.containsString; +import static org.hamcrest.Matchers.is; import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.csrf; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; @@ -293,6 +294,44 @@ public void roomNotFoundException() throws Exception { .andExpect(status().is(404)) // checking for response status code 404 .andExpect(content().string(containsString("Raum nicht gefunden")));// checking if error message is displayed for user } + + @Test + public void testRoomVisitorsViaRest() throws Exception { + Room testRoom = roomService.findByName(TEST_ROOM_NAME).get(); + + // /{roomId} + this.mockMvc.perform( + get("/api/rooms/" + TEST_ROOM_NAME + "/visitors?pin=" + TEST_ROOM_PIN).with(csrf())) + .andExpect(status().isOk()) + .andExpect(content().string(is("0")) + ); + + + fillRoom(testRoom, 2); + this.mockMvc.perform( + get("/api/rooms/" + TEST_ROOM_NAME + "/visitors?pin=" + TEST_ROOM_PIN).with(csrf())) + .andExpect(status().isOk()) + .andExpect(content().string(is("2")) + ); + + fillRoom(testRoom, 5); + this.mockMvc.perform( + get("/api/rooms/" + TEST_ROOM_NAME + "/visitors?pin=" + TEST_ROOM_PIN).with(csrf())) + .andExpect(status().isOk()) + .andExpect(content().string(is("7")) + ); + + + this.mockMvc.perform( + get("/api/rooms/" + TEST_ROOM_NAME + "/visitors").with(csrf())) + .andExpect(status().isBadRequest() + ); + + this.mockMvc.perform( + get("/api/rooms/notExistingName/visitors").with(csrf())) + .andExpect(status().isNotFound() + ); + } /** * Helper method that creates users to fill room. diff --git a/src/test/java/de/hs_mannheim/informatik/ct/persistence/services/RoomServiceTest.java b/src/test/java/de/hs_mannheim/informatik/ct/persistence/services/RoomServiceTest.java index a7770c82..013de7e3 100644 --- a/src/test/java/de/hs_mannheim/informatik/ct/persistence/services/RoomServiceTest.java +++ b/src/test/java/de/hs_mannheim/informatik/ct/persistence/services/RoomServiceTest.java @@ -2,7 +2,6 @@ import static org.junit.jupiter.api.Assertions.assertAll; import static org.junit.jupiter.api.Assertions.assertThrows; -import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; import java.util.Collections;