### Question:
DefaultSurveillanceService implements SurveillanceService, InitializingBean { protected String getUsername() { SecurityContext context = SecurityContextHolder.getContext(); Assert.state(context != null, "No security context found when calling SecurityContextHolder.getContext()"); org.springframework.security.core.Authentication auth = context.getAuthentication(); Assert.state(auth != null, "No Authentication object found when calling getAuthentication on our SecurityContext object"); Object obj = auth.getPrincipal(); Assert.state(obj != null, "No principal object found when calling getPrincipal on our Authentication object"); if (obj instanceof UserDetails) { return ((UserDetails)obj).getUsername(); } else { throw new IllegalStateException("principal should always be instanceof UserDetails"); } }  SurveillanceData getSurveillanceData(); Alarm[] getAlarmsForSet(SurveillanceSet set); String[] getNodeNames(SurveillanceSet set); String[][] getResources(SurveillanceSet set); String[][] getChildResources(String id); String[][] getPrefabGraphs(String id); Notification[] getNotificationsForSet(SurveillanceSet set); NodeRtc[] getRtcForSet(SurveillanceSet set); @Override void afterPropertiesSet(); void setNodeDao(NodeDao nodeDao); void setNotificationDao(NotificationDao notifDao); void setResourceDao(ResourceDao resourceDao); void setGraphDao(GraphDao graphDao); org.opennms.web.svclayer.SurveillanceService getWebSurveillanceService(); void setWebSurveillanceService(org.opennms.web.svclayer.SurveillanceService webSurveillanceService); SurveillanceViewConfigDao getSurveillanceViewConfigDao(); void setSurveillanceViewConfigDao(SurveillanceViewConfigDao surveillanceViewConfigDao); CategoryDao getCategoryDao(); void setCategoryDao(CategoryDao categoryDao); AlarmDao getAlarmDao(); void setAlarmDao(AlarmDao alarmDao); RtcService getRtcService(); void setRtcService(RtcService rtcService); GroupDao getGroupDao(); void setGroupDao(GroupDao groupDao); OutageDao getOutageDao(); void setOutageDao(OutageDao outageDao);  }### Answer:
@Test public void testGetUsernameWithUserDetails() { UserDetails details = populateSecurityContext(); String user = m_service.getUsername(); assertNotNull("user should not be null", user); assertEquals("user name", details.getUsername(), user); }
@Test(expected=IllegalStateException.class) public void testGetUsernameWithStringPrincipal() { org.springframework.security.core.Authentication auth = new UsernamePasswordAuthenticationToken("user", null, new ArrayList<GrantedAuthority>()); SecurityContextHolder.getContext().setAuthentication(auth); String user = m_service.getUsername(); }
@Test public void testGetUsernameNoAuthenticationObject() { ThrowableAnticipator ta = new ThrowableAnticipator(); ta.anticipate(new IllegalStateException("No Authentication object found when calling getAuthentication on our SecurityContext object")); try { m_service.getUsername(); } catch (Throwable t) { ta.throwableReceived(t); } ta.verifyAnticipated(); }
@Test public void testGetUsernameNoPrincipalObject() { org.springframework.security.core.Authentication auth = new UsernamePasswordAuthenticationToken(null, null, new ArrayList<GrantedAuthority>()); SecurityContextHolder.getContext().setAuthentication(auth); ThrowableAnticipator ta = new ThrowableAnticipator(); ta.anticipate(new IllegalStateException("No principal object found when calling getPrincipal on our Authentication object")); try { m_service.getUsername(); } catch (Throwable t) { ta.throwableReceived(t); } ta.verifyAnticipated(); }