Skip to content

Commit

Permalink
Prevent ISE: Duplicate key when calling getGroups
Browse files Browse the repository at this point in the history
Filter out non user groups as those may have duplicate names.
Never fail for duplicates, but just use the first one then.

This closes #789
  • Loading branch information
kwin committed Feb 22, 2025
1 parent f0f1a75 commit b6658a3
Show file tree
Hide file tree
Showing 3 changed files with 148 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -334,12 +334,20 @@ Map<String, IMSGroup> getGroups(String token) throws IOException {
while (!isLastPage) {
GroupResponse response = getGroups(token, page++);
isLastPage = response.isLastPage;
groups.putAll(response.groups.stream().collect(Collectors.toMap(g -> g.getGroupName().toLowerCase(Locale.ROOT), Function.identity())));
groups.putAll(response.groups.stream()
.filter(g -> "USER_GROUP".equals(g.type))
.collect(Collectors.toMap(
g -> g.getGroupName().toLowerCase(Locale.ROOT),
Function.identity(),
(a,b) -> {
LOG.warn("Duplicate group name {} found, keeping first occurrence", a.getGroupName());
return a;
})));
}
return groups;
}

private GroupResponse getGroups(String token, int page) throws IOException {
GroupResponse getGroups(String token, int page) throws IOException {
ObjectMapper objectMapper = new ObjectMapper();
HttpGet httpGet;
try {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package biz.netcentric.cq.tools.actool.ims;

/*-
* #%L
* Access Control Tool Bundle
* %%
* Copyright (C) 2015 - 2024 Cognizant Netcentric
* %%
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
* #L%
*/

import static org.junit.jupiter.api.Assertions.assertEquals;

import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.osgi.services.HttpClientBuilderFactory;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.jupiter.MockitoExtension;

import com.fasterxml.jackson.databind.ObjectMapper;

import biz.netcentric.cq.tools.actool.ims.IMSUserManagement.Configuration;
import biz.netcentric.cq.tools.actool.ims.response.GroupResponse;
import biz.netcentric.cq.tools.actool.ims.response.IMSGroup;

@ExtendWith(MockitoExtension.class)
class IMSUserManagementTest {

private static String MOCK_TOKEN = "mockToken";
@Mock
private Configuration configuration;
@Mock
private HttpClientBuilderFactory httpClientBuilderFactory;
@Mock
private HttpClientBuilder httpClientBuilder;

@BeforeEach
void setUp() {
Mockito.when(httpClientBuilderFactory.newBuilder()).thenReturn(httpClientBuilder);
}

@Test
void testGetGroups() throws IOException {
IMSUserManagement imsUserManagement = new IMSUserManagement(configuration, httpClientBuilderFactory) {
@Override
GroupResponse getGroups(String token, int page) throws java.io.IOException {
try (InputStream inputStream = getClass().getResourceAsStream("groupResponse" + page +".json")) {
ObjectMapper objectMapper = new ObjectMapper();
return objectMapper.readValue(inputStream, GroupResponse.class);
}
}
};
Map<String, IMSGroup> groups = imsUserManagement.getGroups(MOCK_TOKEN);
assertEquals(2, groups.size());
// check keys only
Set<String> expectedKeys = new HashSet<>(Arrays.asList("document cloud 1", "document cloud 2"));
assertEquals(expectedKeys, groups.keySet());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
{
"lastPage": true,
"result": "success",
"groups": [
{
"type": "SYSADMIN_GROUP",
"groupName": "Administrators",
"memberCount": 11
},
{
"type": "USER_GROUP",
"groupName": "Document Cloud 1",
"memberCount": 26,
"adminGroupName": "_admin_Document Cloud 1",
"licenseQuota": "2"
},
{
"type": "USER_GROUP",
"groupName": "Document Cloud 2",
"memberCount": 26,
"adminGroupName": "_admin_Document Cloud 1",
"licenseQuota": "2"
},
{
"type": "PRODUCT_PROFILE",
"groupName": "Default Support Profile",
"memberCount": 0,
"productName": "All Apps plan - 100 GB",
"licenseQuota": "8"
},
{
"type": "PRODUCT_ADMIN_GROUP",
"groupName": "_product_admin_Adobe Document Cloud for business",
"memberCount": 2,
"productProfileName": "Adobe Document Cloud for business"
},
{
"type": "PRODUCT_ADMIN_GROUP",
"groupName": "_product_admin_Adobe Document Cloud for business",
"memberCount": 2,
"productProfileName": "Adobe Document Cloud for business"
},
{
"groupId": 561043099,
"groupName": "_product_admin_Adobe Experience Manager as a Cloud Service (ENTERPRISE_PRODUCT,Cloud Manager,DX - E123)",
"type": "PRODUCT_ADMIN_GROUP",
"memberCount": 7,
"productName": "Adobe Experience Manager as a Cloud Service (ENTERPRISE_PRODUCT,Cloud Manager,DX - E123)"
},
{
"groupId": 743706203,
"groupName": "_product_admin_Adobe Experience Manager as a Cloud Service (ENTERPRISE_PRODUCT,Cloud Manager,DX - E123)",
"type": "PRODUCT_ADMIN_GROUP",
"memberCount": 1,
"productName": "Adobe Experience Manager as a Cloud Service (ENTERPRISE_PRODUCT,Cloud Manager,DX - E123)"
},
{
"type": "DEVELOPER_GROUP",
"groupName": "_developer_Adobe Document Cloud for business",
"memberCount": 5,
"productProfileName": "Adobe Document Cloud for business"
}
]
}

0 comments on commit b6658a3

Please sign in to comment.