SessionCache { public boolean addSession(final Session session) { if (isViolatingMaxNumberOfSessionPerIpPerUser(session)) { return false; } this.cache.put(session.getSessionId(), session); return true; } @Inject  SessionCache(final Props props); Optional<Long> getMaxNumberOfSessionsPerIpPerUser(); Session getSession(final String sessionId); long getSessionCount(); boolean addSession(final Session session); void removeSession(final String id); Set<Session> findSessionsByIP(final String ip);  }
@Test public void SessionCacheRejectDDoS() { final SessionCache sessionCache = createSessionCache(this.longTTL, 2L); final Session session1 = new Session("TEST_SESSION_ID1", new User("TEST_USER_HIT"), "123.12.12.123"); final Session session2 = new Session("TEST_SESSION_ID2", new User("TEST_USER_HIT"), "123.12.12.123"); final Session session3 = new Session("TEST_SESSION_ID3", new User("TEST_USER_HIT"), "123.12.12.123"); assertThat(sessionCache.addSession(session1)).isTrue(); assertThat(sessionCache.addSession(session2)).isTrue(); assertThat(sessionCache.addSession(session3)).isFalse(); }