@Before public void setup() { service1 = mock(IUserRoleListService.class); service2 = mock(IUserRoleListService.class); mockTenant = mock(ITenant.class); String joe = "joe"; String ceo = "ceo"; when(service1.getAllRoles()).thenReturn(Arrays.asList("ceo", "admin")); when(service1.getAllRoles(mockTenant)).thenReturn(Arrays.asList("ceo", "admin")); when(service1.getAllUsers()).thenReturn(Arrays.asList("joe", "suzy")); when(service1.getAllUsers(mockTenant)).thenReturn(Arrays.asList("joe", "suzy")); when(service1.getRolesForUser(mockTenant, joe)).thenReturn(Arrays.asList("admin")); when(service1.getSystemRoles()).thenReturn(Arrays.asList("authenticated", "anonymous")); when(service1.getUsersInRole(mockTenant, ceo)).thenReturn(Arrays.asList("suzy")); when(service2.getAllRoles()).thenReturn(Arrays.asList("extraRole1", "admin")); when(service2.getAllRoles(mockTenant)).thenReturn(Arrays.asList("extraRole1", "admin")); when(service2.getAllUsers()).thenReturn(Arrays.asList("user1", "suzy")); when(service2.getAllUsers(mockTenant)).thenReturn(Arrays.asList("user1", "suzy")); when(service2.getRolesForUser(mockTenant, joe)).thenReturn(Arrays.asList("admin", "rockstar")); when(service2.getSystemRoles()).thenReturn(Arrays.asList("serveradmin", "anonymous")); when(service2.getUsersInRole(mockTenant, ceo)).thenReturn(Arrays.asList("ted", "suzy")); compositeService = new CompositeUserRoleListService(Arrays.asList(service1, service2)); }
@Test public void testGetSystemRoles() throws Exception { IUserRoleListService mockService = mock(IUserRoleListService.class); when(mockService.getSystemRoles()).thenReturn(Arrays.asList("foo", "bar")); CachingUserRoleListServiceDecorator decorator = new CachingUserRoleListServiceDecorator(mockService); List<String> allRoles = decorator.getSystemRoles(); assertArrayEquals("does not match", new String[] {"foo", "bar"}, allRoles.toArray()); // second call should be from cache allRoles = decorator.getSystemRoles(); assertArrayEquals("does not match", new String[] {"foo", "bar"}, allRoles.toArray()); verify(mockService, times(1)).getSystemRoles(); }
@Override public List<String> getSystemRoles() { return userRoleListService.getSystemRoles(); }