diff --git a/src/main/java/de/thm/arsnova/config/ExtraConfig.java b/src/main/java/de/thm/arsnova/config/ExtraConfig.java index 3295ed67e44a1ab788c6f61946fc34f769017367..fc073741d2aa8705da1a04017f31c620d61fe27d 100644 --- a/src/main/java/de/thm/arsnova/config/ExtraConfig.java +++ b/src/main/java/de/thm/arsnova/config/ExtraConfig.java @@ -1,7 +1,5 @@ package de.thm.arsnova.config; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; @@ -20,8 +18,6 @@ import de.thm.arsnova.socket.ARSnovaSocketIOServer; @Configuration public class ExtraConfig { - private final Logger LOGGER = LoggerFactory.getLogger(ExtraConfig.class); - @Autowired private Environment env; @@ -33,7 +29,7 @@ public class ExtraConfig { @Bean public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() { - PropertySourcesPlaceholderConfigurer configurer = new PropertySourcesPlaceholderConfigurer(); + final PropertySourcesPlaceholderConfigurer configurer = new PropertySourcesPlaceholderConfigurer(); configurer.setLocations(new Resource[] { new ClassPathResource("arsnova.properties.example"), new FileSystemResource("file:///etc/arsnova/arsnova.properties"), @@ -49,7 +45,7 @@ public class ExtraConfig { return null; } - ConnectorClientImpl connectorClient = new ConnectorClientImpl(); + final ConnectorClientImpl connectorClient = new ConnectorClientImpl(); connectorClient.setServiceLocation(env.getProperty("connector.uri")); connectorClient.setUsername(env.getProperty("connector.username")); connectorClient.setPassword(env.getProperty("connector.password")); @@ -59,7 +55,7 @@ public class ExtraConfig { @Profile("!test") @Bean(name = "socketServer", initMethod = "startServer", destroyMethod = "stopServer") public ARSnovaSocketIOServer socketServer() { - ARSnovaSocketIOServer socketServer = new ARSnovaSocketIOServer(); + final ARSnovaSocketIOServer socketServer = new ARSnovaSocketIOServer(); socketServer.setHostIp(socketIp); socketServer.setPortNumber(socketPort); socketServer.setUseSSL(socketUseSll); @@ -71,7 +67,7 @@ public class ExtraConfig { @Profile("test") @Bean(name = "socketServer", initMethod = "startServer", destroyMethod = "stopServer") public ARSnovaSocketIOServer socketTestServer() { - ARSnovaSocketIOServer socketServer = new ARSnovaSocketIOServer(); + final ARSnovaSocketIOServer socketServer = new ARSnovaSocketIOServer(); socketServer.setHostIp(socketIp); socketServer.setPortNumber(socketPort + 1234); socketServer.setUseSSL(socketUseSll); diff --git a/src/main/java/de/thm/arsnova/controller/LecturerQuestionController.java b/src/main/java/de/thm/arsnova/controller/LecturerQuestionController.java index ec70bfd4e681f92ef4b893f03402648756b9adc9..9ce717e5dc2a33148b3b4c192ce5fe7161786943 100644 --- a/src/main/java/de/thm/arsnova/controller/LecturerQuestionController.java +++ b/src/main/java/de/thm/arsnova/controller/LecturerQuestionController.java @@ -55,7 +55,7 @@ public class LecturerQuestionController extends AbstractController { @RequestMapping(value = "/{questionId}", method = RequestMethod.GET) public final Question getQuestion(@PathVariable final String questionId) { - Question question = questionService.getQuestion(questionId); + final Question question = questionService.getQuestion(questionId); if (question != null) { return question; } @@ -78,9 +78,10 @@ public class LecturerQuestionController extends AbstractController { @RequestBody final Question question ) { try { - return this.questionService.update(question); - } catch (Exception e) {} - throw new BadRequestException(); + return questionService.update(question); + } catch (final Exception e) { + throw new BadRequestException(); + } } @RequestMapping(value = "/{questionId}/publish", method = RequestMethod.POST) @@ -92,7 +93,7 @@ public class LecturerQuestionController extends AbstractController { if (publish != null) { question.setActive(publish); } - this.questionService.update(question); + questionService.update(question); } @RequestMapping(value = "/publish", method = RequestMethod.POST) @@ -104,7 +105,7 @@ public class LecturerQuestionController extends AbstractController { if (publish != null) { p = publish; } - this.questionService.publishAll(sessionkey, p); + questionService.publishAll(sessionkey, p); } @RequestMapping(value = "/{questionId}/publishstatistics", method = RequestMethod.POST) @@ -116,7 +117,7 @@ public class LecturerQuestionController extends AbstractController { if (showStatistics != null) { question.setShowStatistic(showStatistics); } - this.questionService.update(question); + questionService.update(question); } @RequestMapping(value = "/{questionId}/publishcorrectanswer", method = RequestMethod.POST) @@ -128,7 +129,7 @@ public class LecturerQuestionController extends AbstractController { if (showCorrectAnswer != null) { question.setShowAnswer(showCorrectAnswer); } - this.questionService.update(question); + questionService.update(question); } @RequestMapping(value = "/", method = RequestMethod.GET) @@ -165,13 +166,13 @@ public class LecturerQuestionController extends AbstractController { final HttpServletResponse response ) { if (lectureQuestionsOnly) { - this.questionService.deleteLectureQuestions(sessionkey); + questionService.deleteLectureQuestions(sessionkey); } else if (flashcardsOnly) { - this.questionService.deleteFlashcards(sessionkey); + questionService.deleteFlashcards(sessionkey); } else if (preparationQuestionsOnly) { - this.questionService.deletePreparationQuestions(sessionkey); + questionService.deletePreparationQuestions(sessionkey); } else { - this.questionService.deleteAllQuestions(sessionkey); + questionService.deleteAllQuestions(sessionkey); } } @@ -244,7 +245,7 @@ public class LecturerQuestionController extends AbstractController { @PathVariable final String questionId, final HttpServletResponse response ) { - Answer answer = questionService.getMyAnswer(questionId); + final Answer answer = questionService.getMyAnswer(questionId); if (answer == null) { response.setStatus(HttpStatus.NO_CONTENT.value()); return null; diff --git a/src/main/java/de/thm/arsnova/controller/LoginController.java b/src/main/java/de/thm/arsnova/controller/LoginController.java index a1fee3a1fda43f452836a96f9e3bffc093c3cfda..9e4fe2404efa1d74a7aaa1b783d12fb92d7dc9d4 100644 --- a/src/main/java/de/thm/arsnova/controller/LoginController.java +++ b/src/main/java/de/thm/arsnova/controller/LoginController.java @@ -90,7 +90,7 @@ public class LoginController extends AbstractController { @RequestParam(value = "referer", required = false) final String forcedReferer, @RequestParam(value = "successurl", required = false) final String successUrl, @RequestParam(value = "failureurl", required = false) final String failureUrl, - @RequestParam(value = "role", required = false) UserSessionService.Role role, + @RequestParam(value = "role", required = false) final UserSessionService.Role role, final HttpServletRequest request, final HttpServletResponse response ) throws IOException, ServletException { @@ -118,40 +118,48 @@ public class LoginController extends AbstractController { if ("cas".equals(type)) { casEntryPoint.commence(request, response, null); } else if ("twitter".equals(type)) { - String authUrl = twitterProvider.getAuthorizationUrl(new HttpUserSession(request)); + final String authUrl = twitterProvider.getAuthorizationUrl(new HttpUserSession(request)); result = new RedirectView(authUrl); } else if ("facebook".equals(type)) { - String authUrl = facebookProvider.getAuthorizationUrl(new HttpUserSession(request)); + final String authUrl = facebookProvider.getAuthorizationUrl(new HttpUserSession(request)); result = new RedirectView(authUrl); } else if ("google".equals(type)) { - String authUrl = googleProvider.getAuthorizationUrl(new HttpUserSession(request)); + final String authUrl = googleProvider.getAuthorizationUrl(new HttpUserSession(request)); result = new RedirectView(authUrl); } else if ("guest".equals(type)) { - List<GrantedAuthority> authorities = new ArrayList<GrantedAuthority>(); - authorities.add(new SimpleGrantedAuthority("ROLE_GUEST")); - String username = ""; - if (guestName != null && guestName.startsWith("Guest") && guestName.length() == MAX_USERNAME_LENGTH) { - username = guestName; - } else { - username = "Guest" + Sha512DigestUtils.shaHex( - request.getSession().getId() - ).substring(0, MAX_GUESTHASH_LENGTH); - } - org.springframework.security.core.userdetails.User user = - new org.springframework.security.core.userdetails.User( - username, "", true, true, true, true, authorities - ); - Authentication token = new UsernamePasswordAuthenticationToken(user, null, authorities); - - SecurityContextHolder.getContext().setAuthentication(token); - request.getSession(true).setAttribute(HttpSessionSecurityContextRepository.SPRING_SECURITY_CONTEXT_KEY, - SecurityContextHolder.getContext()); - result = new RedirectView(null == successUrl ? referer + "#auth/checkLogin" : successUrl); + result = handleGuestLogin(guestName, successUrl, request, referer); } return result; } + private View handleGuestLogin(final String guestName, + final String successUrl, final HttpServletRequest request, + final String referer) { + View result; + final List<GrantedAuthority> authorities = new ArrayList<GrantedAuthority>(); + authorities.add(new SimpleGrantedAuthority("ROLE_GUEST")); + String username = ""; + if (guestName != null && guestName.startsWith("Guest") && guestName.length() == MAX_USERNAME_LENGTH) { + username = guestName; + } else { + username = "Guest" + Sha512DigestUtils.shaHex( + request.getSession().getId() + ).substring(0, MAX_GUESTHASH_LENGTH); + } + final org.springframework.security.core.userdetails.User user = + new org.springframework.security.core.userdetails.User( + username, "", true, true, true, true, authorities + ); + final Authentication token = new UsernamePasswordAuthenticationToken(user, null, authorities); + + SecurityContextHolder.getContext().setAuthentication(token); + request.getSession(true).setAttribute(HttpSessionSecurityContextRepository.SPRING_SECURITY_CONTEXT_KEY, + SecurityContextHolder.getContext()); + result = new RedirectView(null == successUrl ? referer + "#auth/checkLogin" : successUrl); + return result; + } + @RequestMapping(value = { "/auth/", "/whoami" }, method = RequestMethod.GET) @ResponseBody public final User whoami() { @@ -161,7 +169,7 @@ public class LoginController extends AbstractController { @RequestMapping(value = { "/auth/logout", "/logout" }, method = RequestMethod.GET) public final View doLogout(final HttpServletRequest request) { - Authentication auth = SecurityContextHolder.getContext().getAuthentication(); + final Authentication auth = SecurityContextHolder.getContext().getAuthentication(); userService.removeUserFromMaps(userService.getCurrentUser()); request.getSession().invalidate(); SecurityContextHolder.clearContext(); @@ -174,7 +182,7 @@ public class LoginController extends AbstractController { @RequestMapping(value = { "/test/me" }, method = RequestMethod.GET) @ResponseBody public final User me() { - User me = userSessionService.getUser(); + final User me = userSessionService.getUser(); if (me == null) { throw new UnauthorizedException(); } @@ -184,7 +192,7 @@ public class LoginController extends AbstractController { @RequestMapping(value = { "/test/mysession" }, method = RequestMethod.GET) @ResponseBody public final Session mysession() { - Session mysession = userSessionService.getSession(); + final Session mysession = userSessionService.getSession(); if (mysession == null) { throw new UnauthorizedException(); } @@ -194,7 +202,7 @@ public class LoginController extends AbstractController { @RequestMapping(value = { "/test/myrole" }, method = RequestMethod.GET) @ResponseBody public final UserSessionService.Role myrole() { - UserSessionService.Role myrole = userSessionService.getRole(); + final UserSessionService.Role myrole = userSessionService.getRole(); if (myrole == null) { throw new UnauthorizedException(); } diff --git a/src/main/java/de/thm/arsnova/controller/StatisticsController.java b/src/main/java/de/thm/arsnova/controller/StatisticsController.java index 9a584b06978b4ad0fca5ced8c702e1d6bbd94603..d14769c5dac5531224a258c2ed15069d45ae5a32 100644 --- a/src/main/java/de/thm/arsnova/controller/StatisticsController.java +++ b/src/main/java/de/thm/arsnova/controller/StatisticsController.java @@ -1,7 +1,5 @@ package de.thm.arsnova.controller; -import java.util.concurrent.Callable; - import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; @@ -24,17 +22,6 @@ public class StatisticsController extends AbstractController { return statisticsService.getStatistics(); } - @RequestMapping(method = RequestMethod.GET, value = "/astatistics") - public final Callable<Statistics> getAsyncStatistics() { - return new Callable<Statistics> () { - - @Override - public Statistics call() throws Exception { - return statisticsService.getStatistics(); - } - }; - } - @DeprecatedApi @RequestMapping(method = RequestMethod.GET, value = "/statistics/activeusercount", produces = "text/plain") public final String countActiveUsers() { diff --git a/src/main/java/de/thm/arsnova/socket/ARSnovaSocketIOServer.java b/src/main/java/de/thm/arsnova/socket/ARSnovaSocketIOServer.java index 7fbb17ae61e3f4ec1a63cd14f63cbfb53d866bed..f67b8fe07b2e8ef16e26f1f86b48520da4517a49 100644 --- a/src/main/java/de/thm/arsnova/socket/ARSnovaSocketIOServer.java +++ b/src/main/java/de/thm/arsnova/socket/ARSnovaSocketIOServer.java @@ -65,12 +65,12 @@ public class ARSnovaSocketIOServer { @PreDestroy public void closeAllSessions() { LOGGER.info("Close all websockets due to @PreDestroy"); - for (SocketIOClient c : server.getAllClients()) { + for (final SocketIOClient c : server.getAllClients()) { c.disconnect(); } int clientCount = 0; - for (SocketIOClient c : server.getAllClients()) { + for (final SocketIOClient c : server.getAllClients()) { c.send(new Packet(PacketType.DISCONNECT)); clientCount++; } @@ -88,10 +88,10 @@ public class ARSnovaSocketIOServer { config.setHostname(hostIp); if (useSSL) { try { - InputStream stream = new FileInputStream(keystore); + final InputStream stream = new FileInputStream(keystore); config.setKeyStore(stream); config.setKeyStorePassword(storepass); - } catch (FileNotFoundException e) { + } catch (final FileNotFoundException e) { LOGGER.error("Keystore {} not found on filesystem", keystore); } } @@ -99,12 +99,12 @@ public class ARSnovaSocketIOServer { server.addEventListener("setFeedback", Feedback.class, new DataListener<Feedback>() { @Override - public void onData(SocketIOClient client, Feedback data, AckRequest ackSender) { + public void onData(final SocketIOClient client, final Feedback data, final AckRequest ackSender) { /** * do a check if user is in the session, for which he would give * a feedback */ - User u = userService.getUser2SocketId(client.getSessionId()); + final User u = userService.getUser2SocketId(client.getSessionId()); if (u == null || !userService.isUserInSession(u, data.getSessionkey())) { return; } @@ -114,7 +114,7 @@ public class ARSnovaSocketIOServer { server.addEventListener("setSession", Session.class, new DataListener<Session>() { @Override - public void onData(SocketIOClient client, Session session, AckRequest ackSender) { + public void onData(final SocketIOClient client, final Session session, final AckRequest ackSender) { sessionService.joinSession(session.getKeyword(), client.getSessionId()); /* active user count has to be sent to the client since the broadcast is * not always sent as long as the polling solution is active simultaneously */ @@ -125,22 +125,21 @@ public class ARSnovaSocketIOServer { server.addConnectListener(new ConnectListener() { @Override - public void onConnect(SocketIOClient client) { } + public void onConnect(final SocketIOClient client) { } }); server.addDisconnectListener(new DisconnectListener() { @Override - public void onDisconnect(SocketIOClient client) { + public void onDisconnect(final SocketIOClient client) { if ( userService == null || client.getSessionId() == null || userService.getUser2SocketId(client.getSessionId()) == null ) { - LOGGER.warn("NullPointer in ARSnovaSocketIOServer DisconnectListener"); return; } - String username = userService.getUser2SocketId(client.getSessionId()).getUsername(); - String sessionKey = userService.getSessionForUser(username); + final String username = userService.getUser2SocketId(client.getSessionId()).getUsername(); + final String sessionKey = userService.getSessionForUser(username); userService.removeUserFromSessionBySocketId(client.getSessionId()); userService.removeUser2SocketId(client.getSessionId()); if (null != sessionKey) { @@ -156,10 +155,10 @@ public class ARSnovaSocketIOServer { public void stopServer() throws Exception { LOGGER.trace("In stopServer method of class: {}", getClass().getName()); try { - for (SocketIOClient client : server.getAllClients()) { + for (final SocketIOClient client : server.getAllClients()) { client.disconnect(); } - } catch (Exception e) { + } catch (final Exception e) { /* If exceptions are not caught they could prevent the Socket.IO server from shutting down. */ LOGGER.error("Exception caught on Socket.IO shutdown: {}", e.getStackTrace()); } @@ -172,7 +171,7 @@ public class ARSnovaSocketIOServer { } @Required - public void setPortNumber(int portNumber) { + public void setPortNumber(final int portNumber) { this.portNumber = portNumber; } @@ -180,7 +179,7 @@ public class ARSnovaSocketIOServer { return hostIp; } - public void setHostIp(String hostIp) { + public void setHostIp(final String hostIp) { this.hostIp = hostIp; } @@ -189,7 +188,7 @@ public class ARSnovaSocketIOServer { } @Required - public void setStorepass(String storepass) { + public void setStorepass(final String storepass) { this.storepass = storepass; } @@ -198,7 +197,7 @@ public class ARSnovaSocketIOServer { } @Required - public void setKeystore(String keystore) { + public void setKeystore(final String keystore) { this.keystore = keystore; } @@ -207,17 +206,17 @@ public class ARSnovaSocketIOServer { } @Required - public void setUseSSL(boolean useSSL) { + public void setUseSSL(final boolean useSSL) { this.useSSL = useSSL; } - public void reportDeletedFeedback(String username, Set<String> arsSessions) { - List<UUID> connectionIds = findConnectionIdForUser(username); + public void reportDeletedFeedback(final String username, final Set<String> arsSessions) { + final List<UUID> connectionIds = findConnectionIdForUser(username); if (connectionIds.isEmpty()) { return; } - for (SocketIOClient client : server.getAllClients()) { + for (final SocketIOClient client : server.getAllClients()) { // Find the client whose feedback has been deleted and send a // message. if (connectionIds.contains(client.getSessionId())) { @@ -226,9 +225,9 @@ public class ARSnovaSocketIOServer { } } - private List<UUID> findConnectionIdForUser(String username) { - List<UUID> result = new ArrayList<UUID>(); - for (Entry<UUID, User> e : userService.socketId2User()) { + private List<UUID> findConnectionIdForUser(final String username) { + final List<UUID> result = new ArrayList<UUID>(); + for (final Entry<UUID, User> e : userService.socketId2User()) { if (e.getValue().getUsername().equals(username)) { result.add(e.getKey()); } @@ -243,37 +242,37 @@ public class ARSnovaSocketIOServer { * @param sessionKey * @param client */ - public void reportSessionDataToClient(String sessionKey, SocketIOClient client) { + public void reportSessionDataToClient(final String sessionKey, final SocketIOClient client) { client.sendEvent("activeUserCountData", userService.getUsersInSessionCount(sessionKey)); - de.thm.arsnova.entities.Feedback fb = feedbackService.getFeedback(sessionKey); + final de.thm.arsnova.entities.Feedback fb = feedbackService.getFeedback(sessionKey); client.sendEvent("feedbackData", fb.getValues()); } - public void reportUpdatedFeedbackForSession(String sessionKey) { - de.thm.arsnova.entities.Feedback fb = feedbackService.getFeedback(sessionKey); + public void reportUpdatedFeedbackForSession(final String sessionKey) { + final de.thm.arsnova.entities.Feedback fb = feedbackService.getFeedback(sessionKey); broadcastInSession(sessionKey, "feedbackData", fb.getValues()); try { - long averageFeedback = feedbackService.getAverageFeedbackRounded(sessionKey); + final long averageFeedback = feedbackService.getAverageFeedbackRounded(sessionKey); broadcastInSession(sessionKey, "feedbackDataRoundedAverage", averageFeedback); - } catch (NoContentException e) { + } catch (final NoContentException e) { broadcastInSession(sessionKey, "feedbackDataRoundedAverage", null); } } - public void reportFeedbackForUserInSession(de.thm.arsnova.entities.Session session, User user) { - de.thm.arsnova.entities.Feedback fb = feedbackService.getFeedback(session.getKeyword()); + public void reportFeedbackForUserInSession(final de.thm.arsnova.entities.Session session, final User user) { + final de.thm.arsnova.entities.Feedback fb = feedbackService.getFeedback(session.getKeyword()); Long averageFeedback; try { averageFeedback = feedbackService.getAverageFeedbackRounded(session.getKeyword()); - } catch (NoContentException e) { + } catch (final NoContentException e) { averageFeedback = null; } - List<UUID> connectionIds = findConnectionIdForUser(user.getUsername()); + final List<UUID> connectionIds = findConnectionIdForUser(user.getUsername()); if (connectionIds.isEmpty()) { return; } - for (SocketIOClient client : server.getAllClients()) { + for (final SocketIOClient client : server.getAllClients()) { if (connectionIds.contains(client.getSessionId())) { client.sendEvent("feedbackData", fb.getValues()); client.sendEvent("feedbackDataRoundedAverage", averageFeedback); @@ -281,9 +280,9 @@ public class ARSnovaSocketIOServer { } } - public void reportActiveUserCountForSession(String sessionKey) { + public void reportActiveUserCountForSession(final String sessionKey) { /* This check is needed as long as the HTTP polling solution is active simultaneously. */ - int count = userService.getUsersInSessionCount(sessionKey); + final int count = userService.getUsersInSessionCount(sessionKey); if (count == lastActiveUserCount) { return; } @@ -292,16 +291,16 @@ public class ARSnovaSocketIOServer { broadcastInSession(sessionKey, "activeUserCountData", count); } - public void reportAnswersToLecturerQuestionAvailable(String sessionKey, String lecturerQuestionId) { + public void reportAnswersToLecturerQuestionAvailable(final String sessionKey, final String lecturerQuestionId) { broadcastInSession(sessionKey, "answersToLecQuestionAvail", lecturerQuestionId); } - public void reportAudienceQuestionAvailable(String sessionKey, String audienceQuestionId) { + public void reportAudienceQuestionAvailable(final String sessionKey, final String audienceQuestionId) { /* TODO role handling implementation, send this only to users with role lecturer */ broadcastInSession(sessionKey, "audQuestionAvail", audienceQuestionId); } - public void reportLecturerQuestionAvailable(String sessionKey, String lecturerQuestionId) { + public void reportLecturerQuestionAvailable(final String sessionKey, final String lecturerQuestionId) { /* TODO role handling implementation, send this only to users with role audience */ broadcastInSession(sessionKey, "lecQuestionAvail", lecturerQuestionId); } @@ -312,24 +311,24 @@ public class ARSnovaSocketIOServer { * @param event The event to be send to client * TODO This method is unimplemented! */ - public void sendToClient(UUID sessionId, ARSnovaEvent event) { - for (SocketIOClient c : server.getAllClients()) { + public void sendToClient(final UUID sessionId, final ARSnovaEvent event) { + for (final SocketIOClient c : server.getAllClients()) { if (c.getSessionId().equals(sessionId)) { break; } } } - public void broadcastInSession(String sessionKey, String eventName, Object data) { + public void broadcastInSession(final String sessionKey, final String eventName, final Object data) { /** * collect a list of users which are in the current session iterate over * all connected clients and if send feedback, if user is in current * session */ - Set<User> users = userService.getUsersInSession(sessionKey); + final Set<User> users = userService.getUsersInSession(sessionKey); - for (SocketIOClient c : server.getAllClients()) { - User u = userService.getUser2SocketId(c.getSessionId()); + for (final SocketIOClient c : server.getAllClients()) { + final User u = userService.getUser2SocketId(c.getSessionId()); if (u != null && users.contains(u)) { c.sendEvent(eventName, data); } diff --git a/src/test/java/de/thm/arsnova/dao/NovaViewTest.java b/src/test/java/de/thm/arsnova/dao/NovaViewTest.java index 2ef0f87694f6e8e24807ac5dcd757f608a727bd6..4b6b28c9fd593709e8b13c35c734f82cf01e3726 100644 --- a/src/test/java/de/thm/arsnova/dao/NovaViewTest.java +++ b/src/test/java/de/thm/arsnova/dao/NovaViewTest.java @@ -18,7 +18,8 @@ */ package de.thm.arsnova.dao; -import static org.junit.Assert.*; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.fail; import java.io.UnsupportedEncodingException; import java.net.URLEncoder; @@ -29,88 +30,88 @@ public class NovaViewTest { @Test public void setKeyShouldAcceptSingleArgument() { - NovaView v = new NovaView(null); + final NovaView v = new NovaView(null); v.setKey("foo"); assertEncodedEquals("key", "\"foo\"", v.getQueryString()); } @Test public void setKeyShouldAcceptMultipleArgument() { - NovaView v = new NovaView(null); + final NovaView v = new NovaView(null); v.setKey("foo", "bar", "baz"); assertEncodedEquals("key", "[\"foo\",\"bar\",\"baz\"]", v.getQueryString()); } @Test public void setStartKeyShouldAcceptSingleArgument() { - NovaView v = new NovaView(null); + final NovaView v = new NovaView(null); v.setStartKey("foo"); assertEncodedEquals("startkey", "\"foo\"", v.getQueryString()); } @Test public void setStartKeyShouldAcceptSingleArgumentArray() { - NovaView v = new NovaView(null); + final NovaView v = new NovaView(null); v.setStartKeyArray("foo"); assertEncodedEquals("startkey", "[\"foo\"]", v.getQueryString()); } @Test public void setEndKeyShouldAcceptSingleArgumentArray() { - NovaView v = new NovaView(null); + final NovaView v = new NovaView(null); v.setEndKeyArray("foo"); assertEncodedEquals("endkey", "[\"foo\"]", v.getQueryString()); } @Test public void setEndKeyShouldAcceptSingleArgument() { - NovaView v = new NovaView(null); + final NovaView v = new NovaView(null); v.setEndKey("foo"); assertEncodedEquals("endkey", "\"foo\"", v.getQueryString()); } @Test public void setStartKeyShouldAcceptMultipleArgument() { - NovaView v = new NovaView(null); + final NovaView v = new NovaView(null); v.setStartKey("foo", "bar", "baz"); assertEncodedEquals("startkey", "[\"foo\",\"bar\",\"baz\"]", v.getQueryString()); } @Test public void setEndKeyShouldAcceptMultipleArgument() { - NovaView v = new NovaView(null); + final NovaView v = new NovaView(null); v.setEndKey("foo", "bar", "baz"); assertEncodedEquals("endkey", "[\"foo\",\"bar\",\"baz\"]", v.getQueryString()); } @Test public void keysShouldSupportEmptyObject() { - NovaView v = new NovaView(null); + final NovaView v = new NovaView(null); v.setKey("foo", "bar", "{}"); assertEncodedEquals("key", "[\"foo\",\"bar\",{}]", v.getQueryString()); } @Test public void arrayKeysShouldNotEnquoteNumbers() { - NovaView v = new NovaView(null); + final NovaView v = new NovaView(null); v.setKey("foo", "bar", "2"); assertEncodedEquals("key", "[\"foo\",\"bar\",2]", v.getQueryString()); } @Test public void singleArrayKeysShouldNotEnquoteNumbers() { - NovaView v1 = new NovaView(null); - NovaView v2 = new NovaView(null); + final NovaView v1 = new NovaView(null); + final NovaView v2 = new NovaView(null); v1.setStartKeyArray("2"); v2.setEndKeyArray("2"); assertEncodedEquals("startkey", "[2]", v1.getQueryString()); assertEncodedEquals("endkey", "[2]", v2.getQueryString()); } - private void assertEncodedEquals(String key, String expected, String actual) { + private void assertEncodedEquals(final String key, final String expected, final String actual) { try { assertEquals(key + "=" + URLEncoder.encode(expected, "UTF-8"), actual); - } catch (UnsupportedEncodingException e) { + } catch (final UnsupportedEncodingException e) { fail(e.getLocalizedMessage()); } }