Skip to content

Commit

Permalink
Refactor 'createGroupAndIgnoreConflict'
Browse files Browse the repository at this point in the history
  • Loading branch information
adrianhoelzl-sap committed Sep 5, 2024
1 parent bdec670 commit 7fc64b2
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import org.cloudfoundry.identity.uaa.oauth.jwt.Jwt;
import org.cloudfoundry.identity.uaa.provider.LdapIdentityProviderDefinition;
import org.cloudfoundry.identity.uaa.oauth.token.ClaimConstants;
import org.cloudfoundry.identity.uaa.scim.ScimGroup;
import org.cloudfoundry.identity.uaa.scim.ScimUser;
import org.cloudfoundry.identity.uaa.test.UaaTestAccounts;
import org.cloudfoundry.identity.uaa.util.JsonUtils;
Expand Down Expand Up @@ -57,8 +56,7 @@ public class LdapIntegrationTests {
public void setup() {
String token = IntegrationTestUtils.getClientCredentialsToken(serverRunning.getBaseUrl(), "admin", "adminsecret");

ScimGroup group = new ScimGroup(null, "zones.testzone1.admin", null);
IntegrationTestUtils.createGroupAndIgnoreConflict(token, "", serverRunning.getBaseUrl(), group);
IntegrationTestUtils.ensureGroupExists(token, "", serverRunning.getBaseUrl(), "zones.testzone1.admin");
}

@After
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,20 +158,11 @@ public static void checkZoneDNSSupport() {
public void setup() {
String token = IntegrationTestUtils.getClientCredentialsToken(baseUrl, "admin", "adminsecret");

ScimGroup group = new ScimGroup(null, "zones.uaa.admin", null);
IntegrationTestUtils.createGroupAndIgnoreConflict(token, "", baseUrl, group);

group = new ScimGroup(null, "zones.testzone1.admin", null);
IntegrationTestUtils.createGroupAndIgnoreConflict(token, "", baseUrl, group);

group = new ScimGroup(null, "zones.testzone2.admin", null);
IntegrationTestUtils.createGroupAndIgnoreConflict(token, "", baseUrl, group);

group = new ScimGroup(null, "zones.testzone3.admin", null);
IntegrationTestUtils.createGroupAndIgnoreConflict(token, "", baseUrl, group);

group = new ScimGroup(null, "zones.testzone4.admin", null);
IntegrationTestUtils.createGroupAndIgnoreConflict(token, "", baseUrl, group);
IntegrationTestUtils.ensureGroupExists(token, null, baseUrl, "zones.uaa.admin");
IntegrationTestUtils.ensureGroupExists(token, null, baseUrl, "zones.testzone1.admin");
IntegrationTestUtils.ensureGroupExists(token, null, baseUrl, "zones.testzone2.admin");
IntegrationTestUtils.ensureGroupExists(token, null, baseUrl, "zones.testzone3.admin");
IntegrationTestUtils.ensureGroupExists(token, null, baseUrl, "zones.testzone4.admin");
}

@After
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -531,6 +531,23 @@ public static String findGroupId(RestTemplate client,
return null;
}

public static ScimGroup ensureGroupExists(
final String token,
final String zoneId,
final String url,
final String displayName
) {
final ScimGroup existingGroup = getGroup(token, zoneId, url, displayName);
if (existingGroup != null) {
return existingGroup;
}
final ScimGroup group = new ScimGroup(null, displayName, zoneId);
return createGroup(token, zoneId, url, group);
}

/**
* @return the group or {@code null} if it does not exist
*/
public static ScimGroup getGroup(String token,
String zoneId,
String url,
Expand Down Expand Up @@ -559,30 +576,11 @@ public static ScimGroup getGroup(String token,
}
}

public static ScimGroup createGroupAndIgnoreConflict(
final String token,
final String zoneId,
final String url,
final ScimGroup group
) {
return createGroup(token, zoneId, url, group, HttpStatus.CREATED, HttpStatus.CONFLICT);
}

public static ScimGroup createGroup(
final String token,
final String zoneId,
final String url,
final ScimGroup group
) {
return createGroup(token, zoneId, url, group, HttpStatus.CREATED);
}

private static ScimGroup createGroup(
final String token,
final String zoneId,
final String url,
final ScimGroup group,
final HttpStatus ...expectedStatusCodes
) {
final RestTemplate template = new RestTemplate();
template.setErrorHandler(fiveHundredErrorHandler);
Expand All @@ -599,7 +597,7 @@ private static ScimGroup createGroup(
new HttpEntity<>(JsonUtils.writeValueAsBytes(group), headers),
ScimGroup.class
);
assertStatusCode(response, expectedStatusCodes);
assertStatusCode(response, HttpStatus.CREATED);
final ScimGroup responseBody = response.getBody();
assertNotNull(responseBody);
return responseBody;
Expand Down Expand Up @@ -1012,8 +1010,7 @@ public static String getZoneAdminToken(String baseUrl, ServerRunning serverRunni
ScimUser user = IntegrationTestUtils.createUser(adminClient, baseUrl, email, "firstname", "lastname", email, true);

String groupName = "zones." + zoneId + ".admin";
ScimGroup group = new ScimGroup(null, groupName, null);
createGroupAndIgnoreConflict(getClientCredentialsToken(baseUrl, "admin", "adminsecret"), "", baseUrl, group);
ensureGroupExists(getClientCredentialsToken(baseUrl, "admin", "adminsecret"), "", baseUrl, groupName);
String groupId = IntegrationTestUtils.findGroupId(adminClient, baseUrl, groupName);
assertThat("Couldn't find group : " + groupId, groupId, is(CoreMatchers.notNullValue()));
IntegrationTestUtils.addMemberToGroup(adminClient, baseUrl, user.getId(), groupId);
Expand Down

0 comments on commit 7fc64b2

Please sign in to comment.