GroupsDelegator extends BaseDelegator implements IGroupsDelegator { @Override public List<IUserGroup> getUserGroups(final String userOrGroupName, final GetUserGroupsOptions opts) throws P4JavaException { List<Map<String, Object>> resultMaps = execMapCmdList(GROUPS, Parameters.processParameters(opts, null, userOrGroupName, server), null); Map<String, UserGroup> userGroupMap = new HashMap<>(); if (nonNull(resultMaps)) { for (Map<String, Object> map : resultMaps) { ResultMapParser.handleErrorStr(map); UserGroup ugImpl; String groupName = parseString(map, MapKeys.GROUP_LC_KEY); if (userGroupMap.containsKey(groupName)) { ugImpl = userGroupMap.get(groupName); } else { ugImpl = new UserGroup(); ugImpl.setName(groupName); userGroupMap.put(groupName, ugImpl); } try { String userName = parseString(map, MapKeys.USER_LC_KEY); String maxScanRows = parseString(map, MapKeys.MAXSCANROWS_LC_KEY); String maxLockTime = parseString(map, MapKeys.MAXLOCKTIME_LC_KEY); String timeout = parseString(map, MapKeys.TIMEOUT_LC_KEY); String passwordTimeout = parseString(map, MapKeys.PASSWORD_TIMEOUT_LC_KEY); String maxResults = parseString(map, MapKeys.MAXRESULTS_LC_KEY); String isOwner = parseString(map, MapKeys.ISOWNER_LC_KEY); String isSubGroup = parseString(map, MapKeys.ISSUBGROUP_LC_KEY); if ("1".equals(isOwner)) { ugImpl.addOwner(userName); } if ("1".equals(isSubGroup)) { ugImpl.addSubgroup(userName); } else { ugImpl.addUser(userName); } if (isNotBlank(maxScanRows)) { ugImpl.setMaxScanRows(Integer.parseInt(maxScanRows)); } if (isNotBlank(maxLockTime)) { ugImpl.setMaxLockTime(Integer.parseInt(maxLockTime)); } if (isNotBlank(timeout)) { ugImpl.setTimeout(Integer.parseInt(timeout)); } if (isNotBlank(maxResults)) { ugImpl.setMaxResults(Integer.parseInt(maxResults)); } if (isNotBlank(passwordTimeout)) { ugImpl.setPasswordTimeout(Integer.parseInt(passwordTimeout)); } } catch (Throwable thr) { Log.warn("Unexpected exception in ServerImpl.getUserGroups: %s", thr.getMessage()); Log.exception(thr); } } } return new ArrayList<IUserGroup>(userGroupMap.values()); } GroupsDelegator(IOptionsServer server); @Override List<IUserGroup> getUserGroups(final String userOrGroupName,
            final GetUserGroupsOptions opts); List<IUserGroup> getUserGroups(final String userOrGroupName, final boolean indirect,
            final boolean displayValues, final int maxGroups);  }
@Test public void testConnectionException() throws Exception { when(server.execMapCmdList(eq(CmdSpec.GROUPS.toString()), argThat(new CommandLineArgumentMatcher(new String[] { "-i", "-v", groupName })), eq(null))).thenThrow(new ConnectionException("Read timed out.")); assertThrows(ConnectionException.class, () -> groupsDelegator.getUserGroups(groupName, true, true, -1)); }
@Test public void testAccessException() throws P4JavaException { when(server.execMapCmdList(eq(CmdSpec.GROUPS.toString()), argThat(new CommandLineArgumentMatcher(new String[] { "-i", "-v", groupName })), eq(null))).thenThrow(AccessException.AccessExceptionForTests.class); assertThrows(AccessException.class, () -> groupsDelegator.getUserGroups(groupName, true, true, -1)); }
@Test public void testRequestException() throws P4JavaException { when(server.execMapCmdList(eq(CmdSpec.GROUPS.toString()), argThat(new CommandLineArgumentMatcher(new String[] { "-i", "-v", groupName })), eq(null))).thenThrow(new RequestException("Invalid")); assertThrows(RequestException.class, () -> groupsDelegator.getUserGroups(groupName, true, true, -1)); }
@Test public void testGetMany() throws P4JavaException { List<Map<String, Object>> populatedResultMaps = new ArrayList<>(4); HashMap<String, Object> resultMap1 = createMockResultMap("grpName1", "Sean", "3", "10", "5", "6", "100", "1", "0"); resultMaps.add(resultMap1); HashMap<String, Object> resultMap2 = createMockResultMap("grpName1", "subGrp1", "3", "10", "5", "6", "100", "0", "1"); populatedResultMaps.add(resultMap2); HashMap<String, Object> resultMap3 = createMockResultMap("grpName1", "subGrp2", "15", "10", "5", "6", "100", "0", "1"); populatedResultMaps.add(resultMap3); HashMap<String, Object> resultMap4 = createMockResultMap("grpName2", "Tim", "3", "10", "5", "6", "100", "1", "0"); populatedResultMaps.add(resultMap4); when(server.execMapCmdList(eq(CmdSpec.GROUPS.toString()), argThat(new CommandLineArgumentMatcher(new String[] {})), eq(null))) .thenReturn(populatedResultMaps); when(server.getErrorStr(any())).thenReturn(dummyServerErrorMessage(EMPTY)); List<IUserGroup> userGroups = groupsDelegator.getUserGroups("", new GetUserGroupsOptions()); assertEquals(2, userGroups.size()); for (IUserGroup userGroup : userGroups) { if ("grpName1".equals(userGroup.getName())) { assertEquals(2, userGroup.getSubgroups().size()); } else { assertEquals("grpName2", userGroup.getName()); assertTrue(userGroup.getOwners().contains("Tim")); } } }
@Test public void testUnlimitedTimeout() throws P4JavaException { List<Map<String, Object>> populatedResultMaps = new ArrayList<>(4); HashMap<String, Object> resultMap1 = createMockResultMap("grpName1", "Sean", "3", "10", "unlimited", "6", "100", "1", "0"); populatedResultMaps.add(resultMap1); when(server.execMapCmdList(eq(CmdSpec.GROUPS.toString()), argThat(new CommandLineArgumentMatcher(new String[] {})), eq(null))) .thenReturn(populatedResultMaps); when(firstResultMap.get(MapKeys.GROUP_LC_KEY)).thenReturn(groupName); when(server.getErrorStr(any())).thenReturn(dummyServerErrorMessage(EMPTY)); List<IUserGroup> userGroups = groupsDelegator.getUserGroups(null, mock(GetUserGroupsOptions.class)); assertEquals(1, userGroups.size()); }
@Test public void testGetGroups() throws P4JavaException { when(server.execMapCmdList(eq(CmdSpec.GROUPS.toString()), argThat(new CommandLineArgumentMatcher(new String[] { "-i", "-v", groupName })), eq((Map<String, Object>) null))).thenReturn(resultMaps); when(firstResultMap.get(MapKeys.GROUP_LC_KEY)).thenReturn(groupName); List<IUserGroup> groups = groupsDelegator.getUserGroups(groupName, true, true, -1); verify(server).execMapCmdList(eq(CmdSpec.GROUPS.toString()), argThat(new CommandLineArgumentMatcher(new String[] { "-i", "-v", groupName })), eq((Map<String, Object>) null)); assertEquals(1, groups.size()); assertEquals(groups.get(0).getName(), groupName); }
@Test(expected = ConnectionException.class) public void testConnectionException() throws Exception { when(server.execMapCmdList(eq(CmdSpec.GROUPS.toString()), argThat(new CommandLineArgumentMatcher(new String[] { "-i", "-v", groupName })), eq((Map<String, Object>) null))) .thenThrow(new ConnectionException("Read timed out.")); groupsDelegator.getUserGroups(groupName, true, true, -1); }
@Test(expected = AccessException.class) public void testAccessException() throws P4JavaException { when(server.execMapCmdList(eq(CmdSpec.GROUPS.toString()), argThat(new CommandLineArgumentMatcher(new String[] { "-i", "-v", groupName })), eq((Map<String, Object>) null))).thenThrow(new AccessException("Denied.")); groupsDelegator.getUserGroups(groupName, true, true, -1); }
@Test(expected = RequestException.class) public void testRequestException() throws P4JavaException { when(server.execMapCmdList(eq(CmdSpec.GROUPS.toString()), argThat(new CommandLineArgumentMatcher(new String[] { "-i", "-v", groupName })), eq((Map<String, Object>) null))).thenThrow(new RequestException("Invalid")); groupsDelegator.getUserGroups(groupName, true, true, -1); }
@Test public void testNullResponse() throws P4JavaException { when(server.execMapCmdList(eq(CmdSpec.GROUPS.toString()), argThat(new CommandLineArgumentMatcher(new String[] { "-i", "-v", groupName })), eq((Map<String, Object>) null))).thenReturn(null); List<IUserGroup> userGroups = groupsDelegator.getUserGroups(groupName, new GetUserGroupsOptions()); assertEquals(0, userGroups.size()); }
@Test public void testEmptyResponse() throws P4JavaException { when(server.execMapCmdList(eq(CmdSpec.GROUPS.toString()), argThat(new CommandLineArgumentMatcher(new String[] { "-i", "-v", groupName })), eq((Map<String, Object>) null))).thenReturn(new ArrayList<Map<String, Object>>()); List<IUserGroup> userGroups = groupsDelegator.getUserGroups(groupName, new GetUserGroupsOptions()); assertEquals(0, userGroups.size()); }
@SuppressWarnings({ "unchecked", "deprecation" }) @Test public void testGetMany() throws P4JavaException { List<Map<String, Object>> populatedResultMaps = new ArrayList<>(4); HashMap<String, Object> resultMap1 = createMockResultMap("grpName1", "Sean", "3", "10", "5", "6", "100", "1", "0"); resultMaps.add(resultMap1); HashMap<String, Object> resultMap2 = createMockResultMap("grpName1", "subGrp1", "3", "10", "5", "6", "100", "0", "1"); populatedResultMaps.add(resultMap2); HashMap<String, Object> resultMap3 = createMockResultMap("grpName1", "subGrp2", "15", "10", "5", "6", "100", "0", "1"); populatedResultMaps.add(resultMap3); HashMap<String, Object> resultMap4 = createMockResultMap("grpName2", "Tim", "3", "10", "5", "6", "100", "1", "0"); populatedResultMaps.add(resultMap4); when(server.execMapCmdList(eq(CmdSpec.GROUPS.toString()), argThat(new CommandLineArgumentMatcher(new String[] {})), eq((Map<String, Object>) null))).thenReturn(populatedResultMaps); when(server.getErrorStr((Map<String, Object>) any())).thenReturn(EMPTY); List<IUserGroup> userGroups = groupsDelegator.getUserGroups("", new GetUserGroupsOptions()); assertEquals(2, userGroups.size()); for (IUserGroup userGroup : userGroups) { if ("grpName1".equals(userGroup.getName())) { assertEquals(2, userGroup.getSubgroups().size()); } else { assertEquals("grpName2", userGroup.getName()); assertTrue(userGroup.getOwners().contains("Tim")); } } }
@SuppressWarnings({ "unchecked", "deprecation" }) @Test public void testUnlimitedTimeout() throws P4JavaException { List<Map<String, Object>> populatedResultMaps = new ArrayList<>(4); HashMap<String, Object> resultMap1 = createMockResultMap("grpName1", "Sean", "3", "10", "unlimited", "6", "100", "1", "0"); populatedResultMaps.add(resultMap1); when(server.execMapCmdList(eq(CmdSpec.GROUPS.toString()), argThat(new CommandLineArgumentMatcher(new String[] {})), eq((Map<String, Object>) null))).thenReturn(populatedResultMaps); when(firstResultMap.get(MapKeys.GROUP_LC_KEY)).thenReturn(groupName); when(server.getErrorStr((Map<String, Object>) any())).thenReturn(EMPTY); List<IUserGroup> userGroups = groupsDelegator.getUserGroups(null, mock(GetUserGroupsOptions.class)); assertEquals(1, userGroups.size()); }
@Test public void testGetGroups() throws P4JavaException { when(server.execMapCmdList(eq(CmdSpec.GROUPS.toString()), argThat(new CommandLineArgumentMatcher(new String[] { "-i", "-v", groupName })), eq(null))).thenReturn(resultMaps); when(firstResultMap.get(MapKeys.GROUP_LC_KEY)).thenReturn(groupName); List<IUserGroup> groups = groupsDelegator.getUserGroups(groupName, true, true, -1); verify(server).execMapCmdList(eq(CmdSpec.GROUPS.toString()), argThat(new CommandLineArgumentMatcher(new String[] { "-i", "-v", groupName })), eq(null)); assertEquals(1, groups.size()); assertEquals(groups.get(0).getName(), groupName); }
@Test public void testConnectionException() throws Exception { when(server.execMapCmdList(eq(CmdSpec.GROUPS.toString()), argThat(new CommandLineArgumentMatcher(new String[] { "-i", "-v", groupName })), eq(null))).thenThrow(new ConnectionException("Read timed out.")); expectThrows(ConnectionException.class, () -> groupsDelegator.getUserGroups(groupName, true, true, -1)); }
@Test public void testAccessException() throws P4JavaException { when(server.execMapCmdList(eq(CmdSpec.GROUPS.toString()), argThat(new CommandLineArgumentMatcher(new String[] { "-i", "-v", groupName })), eq(null))).thenThrow(new AccessException("Denied.")); expectThrows(AccessException.class, () -> groupsDelegator.getUserGroups(groupName, true, true, -1)); }
@Test public void testRequestException() throws P4JavaException { when(server.execMapCmdList(eq(CmdSpec.GROUPS.toString()), argThat(new CommandLineArgumentMatcher(new String[] { "-i", "-v", groupName })), eq(null))).thenThrow(new RequestException("Invalid")); expectThrows(RequestException.class, () -> groupsDelegator.getUserGroups(groupName, true, true, -1)); }
@Test public void testNullResponse() throws P4JavaException { when(server.execMapCmdList(eq(CmdSpec.GROUPS.toString()), argThat(new CommandLineArgumentMatcher(new String[] { "-i", "-v", groupName })), eq(null))).thenReturn(null); List<IUserGroup> userGroups = groupsDelegator.getUserGroups(groupName, new GetUserGroupsOptions()); assertEquals(0, userGroups.size()); }
@Test public void testEmptyResponse() throws P4JavaException { when(server.execMapCmdList(eq(CmdSpec.GROUPS.toString()), argThat(new CommandLineArgumentMatcher(new String[] { "-i", "-v", groupName })), eq(null))).thenReturn(new ArrayList<Map<String, Object>>()); List<IUserGroup> userGroups = groupsDelegator.getUserGroups(groupName, new GetUserGroupsOptions()); assertEquals(0, userGroups.size()); }
@Test public void testGetMany() throws P4JavaException { List<Map<String, Object>> populatedResultMaps = new ArrayList<>(4); HashMap<String, Object> resultMap1 = createMockResultMap("grpName1", "Sean", "3", "10", "5", "6", "100", "1", "0"); resultMaps.add(resultMap1); HashMap<String, Object> resultMap2 = createMockResultMap("grpName1", "subGrp1", "3", "10", "5", "6", "100", "0", "1"); populatedResultMaps.add(resultMap2); HashMap<String, Object> resultMap3 = createMockResultMap("grpName1", "subGrp2", "15", "10", "5", "6", "100", "0", "1"); populatedResultMaps.add(resultMap3); HashMap<String, Object> resultMap4 = createMockResultMap("grpName2", "Tim", "3", "10", "5", "6", "100", "1", "0"); populatedResultMaps.add(resultMap4); when(server.execMapCmdList(eq(CmdSpec.GROUPS.toString()), argThat(new CommandLineArgumentMatcher(new String[] {})), eq(null))) .thenReturn(populatedResultMaps); when(server.getErrorStr(any())).thenReturn(EMPTY); List<IUserGroup> userGroups = groupsDelegator.getUserGroups("", new GetUserGroupsOptions()); assertEquals(2, userGroups.size()); for (IUserGroup userGroup : userGroups) { if ("grpName1".equals(userGroup.getName())) { assertEquals(2, userGroup.getSubgroups().size()); } else { assertEquals("grpName2", userGroup.getName()); assertTrue(userGroup.getOwners().contains("Tim")); } } }
@Test public void testUnlimitedTimeout() throws P4JavaException { List<Map<String, Object>> populatedResultMaps = new ArrayList<>(4); HashMap<String, Object> resultMap1 = createMockResultMap("grpName1", "Sean", "3", "10", "unlimited", "6", "100", "1", "0"); populatedResultMaps.add(resultMap1); when(server.execMapCmdList(eq(CmdSpec.GROUPS.toString()), argThat(new CommandLineArgumentMatcher(new String[] {})), eq(null))) .thenReturn(populatedResultMaps); when(firstResultMap.get(MapKeys.GROUP_LC_KEY)).thenReturn(groupName); when(server.getErrorStr(any())).thenReturn(EMPTY); List<IUserGroup> userGroups = groupsDelegator.getUserGroups(null, mock(GetUserGroupsOptions.class)); assertEquals(1, userGroups.size()); }