Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,23 @@
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;
import org.springframework.web.bind.annotation.RequestMapping;
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;
Expand All @@ -42,6 +47,21 @@
@ControllerAdvice
@Slf4j
public class ErrorController {

@ExceptionHandler(value = {RoomNotFoundRestException.class})
public ResponseEntity<RestErrorMessage> handleRoomNotFoundRestException() {
RestErrorMessage rem = new RestErrorMessage(404, new Date(), "Room not found.");

return new ResponseEntity<RestErrorMessage>(rem, HttpStatus.NOT_FOUND);
}

@ExceptionHandler(value = {InvalidPinRestException.class})
public ResponseEntity<RestErrorMessage> handleRoomPinException() {
RestErrorMessage rem = new RestErrorMessage(400, new Date(), "Room Pin not valid or not submitted.");

return new ResponseEntity<RestErrorMessage>(rem, HttpStatus.BAD_REQUEST);
}

@ExceptionHandler({RoomController.RoomNotFoundException.class})
@ResponseStatus(value = HttpStatus.NOT_FOUND)
// @ResponseStatus(value = HttpStatus.NOT_FOUND, reason = "Room not found")
Expand Down
Original file line number Diff line number Diff line change
@@ -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 {
}
Original file line number Diff line number Diff line change
@@ -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;
}

}
Original file line number Diff line number Diff line change
@@ -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 <https://www.gnu.org/licenses/>.
*/

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();
}

}

}
Original file line number Diff line number Diff line change
@@ -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 {
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down