SessionManager { Collection<Session> getValidSessionsElseRemove(String httpid, Predicate<Session> predicat) { Collection<Session> wss = new ArrayList(); if(httpid_wss.containsKey(httpid)) { Collection<Session> befores = httpid_wss.get(httpid); for (Session before : befores) { if(predicat.test(before)) { wss.add(before); wsid_httpid.put(before.getId(), httpid); } else { wsid_httpid.remove(before.getId()); } } if(wss.isEmpty()) { httpid_wss.remove(httpid); } else { httpid_wss.put(httpid, wss); } } return wss; }  void linkWsToHttp(Session ws, String httpid); void unlinkWs(String wsid); Collection<Session> getUserSessions(String httpid);  }
@Test public void getValidSessionsElseRemoveNoHttpid() { System.out.println("getValidSessionsElseRemove"); String httpid = "HTTPID"; Predicate predicate = mock(Predicate.class); when(httpid_wss.containsKey(eq(httpid))).thenReturn(Boolean.FALSE); Collection<Session> result = instance.getValidSessionsElseRemove(httpid, predicate); assertThat(result).isEmpty(); }
@Test public void getValidSessionsElseRemoveResultEmpty() { System.out.println("getValidSessionsElseRemove"); String httpid = "HTTPID"; Predicate predicate = mock(Predicate.class); Collection<Session> befores = Collections.EMPTY_LIST; when(httpid_wss.containsKey(eq(httpid))).thenReturn(Boolean.TRUE); when(httpid_wss.get(httpid)).thenReturn(befores); Collection<Session> result = instance.getValidSessionsElseRemove(httpid, predicate); assertThat(result).isEmpty(); verify(httpid_wss).remove(eq(httpid)); }
@Test public void getValidSessionsElseRemove() { System.out.println("getValidSessionsElseRemove"); String httpid = "HTTPID"; Predicate predicate = mock(Predicate.class); Collection<Session> befores = Arrays.asList(getMockWs(), getMockWs(), getMockWs()); when(httpid_wss.containsKey(eq(httpid))).thenReturn(Boolean.TRUE); when(httpid_wss.get(httpid)).thenReturn(befores); when(predicate.test(any(Session.class))).thenReturn(Boolean.FALSE, Boolean.TRUE, Boolean.FALSE); Collection<Session> result = instance.getValidSessionsElseRemove(httpid, predicate); assertThat(result).hasSize(1); verify(httpid_wss).put(eq(httpid), anyList()); }