Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import com.salesforce.multicloudj.iam.driver.AbstractIam;
import com.salesforce.multicloudj.iam.model.PolicyDocument;
import com.salesforce.multicloudj.iam.model.Statement;
import com.salesforce.multicloudj.iam.model.CreateOptions;
import com.salesforce.multicloudj.iam.model.TrustConfiguration;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assertions;
Expand All @@ -13,6 +15,7 @@
import org.junit.jupiter.api.TestInstance;

import java.util.List;
import java.util.Optional;

@TestInstance(TestInstance.Lifecycle.PER_CLASS)
public abstract class AbstractIamIT {
Expand All @@ -33,6 +36,10 @@ public interface Harness extends AutoCloseable {

String getIamEndpoint();

String getTrustedPrincipal();

String getTestIdentityName();

default String getPolicyVersion() {
return "";
}
Expand All @@ -41,12 +48,14 @@ default String getPolicyVersion() {

List<String> getTestPolicyActions();

String getTestPolicyName();
}
String getTestPolicyName();
}

protected abstract Harness createHarness();

private Harness harness;
private AbstractIam iam;
private IamClient iamClient;

/**
* Initializes the WireMock server before all tests.
Expand All @@ -73,27 +82,29 @@ public void shutdownWireMockServer() throws Exception {
@BeforeEach
public void setupTestEnvironment() {
TestsUtil.startWireMockRecording(harness.getIamEndpoint());
iam = harness.createIamDriver(true);
iamClient = new IamClient(iam);
}

/**
* Cleans up the test environment after each test.
*/
@AfterEach
public void cleanupTestEnvironment() {
public void cleanupTestEnvironment() throws Exception {
TestsUtil.stopWireMockRecording();
if (iamClient != null) {
iamClient.close(); // closes underlying AbstractIam
}
}

@Test
public void testAttachInlinePolicy() {
AbstractIam iam = harness.createIamDriver(true);
IamClient iamClient = new IamClient(iam);

Statement.StatementBuilder statementBuilder = Statement.builder()
Statement.StatementBuilder statementBuilder = Statement.builder()
.effect(harness.getTestPolicyEffect());
for (String action : harness.getTestPolicyActions()) {
statementBuilder.action(action);
}

PolicyDocument policyDocument = PolicyDocument.builder()
.version(harness.getPolicyVersion())
.statement(statementBuilder.build())
Expand All @@ -109,10 +120,7 @@ public void testAttachInlinePolicy() {

@Test
public void testGetInlinePolicyDetails() {
AbstractIam iam = harness.createIamDriver(true);
IamClient iamClient = new IamClient(iam);

PolicyDocument policyDocument = PolicyDocument.builder()
PolicyDocument policyDocument = PolicyDocument.builder()
.version(harness.getPolicyVersion())
.statement(Statement.builder()
.effect(harness.getTestPolicyEffect())
Expand All @@ -139,15 +147,12 @@ public void testGetInlinePolicyDetails() {

@Test
public void testGetAttachedPolicies() {
AbstractIam iam = harness.createIamDriver(true);
IamClient iamClient = new IamClient(iam);

Statement.StatementBuilder statementBuilder = Statement.builder()
Statement.StatementBuilder statementBuilder = Statement.builder()
.effect(harness.getTestPolicyEffect());
for (String action : harness.getTestPolicyActions()) {
statementBuilder.action(action);
}

PolicyDocument policyDocument = PolicyDocument.builder()
.version(harness.getPolicyVersion())
.statement(statementBuilder.build())
Expand All @@ -171,10 +176,7 @@ public void testGetAttachedPolicies() {

@Test
public void testRemovePolicy() {
AbstractIam iam = harness.createIamDriver(true);
IamClient iamClient = new IamClient(iam);

PolicyDocument policyDocument = PolicyDocument.builder()
PolicyDocument policyDocument = PolicyDocument.builder()
.version(harness.getPolicyVersion())
.statement(Statement.builder()
.effect(harness.getTestPolicyEffect())
Expand All @@ -189,11 +191,244 @@ public void testRemovePolicy() {
harness.getIdentityName()
);

iamClient.removePolicy(
harness.getIdentityName(),
harness.getTestPolicyName(),
harness.getTenantId(),
harness.getRegion()
);
}
iamClient.removePolicy(
harness.getIdentityName(),
harness.getTestPolicyName(),
harness.getTenantId(),
harness.getRegion()
);
}

private void cleanUpIdentity(String identity) {
try {
iamClient.deleteIdentity(
identity,
harness.getTenantId(),
harness.getRegion()
);
} catch (Exception e) {
// Ignore
}
}

/**
* Tests creating an identity without trust configuration.
*/
@Test
public void testCreateIdentityWithoutTrustConfig() {
String identityName = harness.getTestIdentityName();
String identityId = iamClient.createIdentity(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After the Identity creation, do we have any clean up step for these test created resources?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch. CleanUp is not implemented. I'll address it in follow up commits.

identityName,
"Test identity for MultiCloudJ integration tests",
harness.getTenantId(),
harness.getRegion(),
Optional.empty(),
Optional.empty()
);

Assertions.assertNotNull(identityId, "Identity ID should not be null");
Assertions.assertFalse(identityId.isEmpty(), "Identity ID should not be empty");

cleanUpIdentity(identityName);
}

/**
* Tests creating an identity with trust configuration.
*/
@Test
public void testCreateIdentityWithTrustConfig() {
String identityName = harness.getTestIdentityName() + "Trusted";
TrustConfiguration trustConfig = TrustConfiguration.builder()
.addTrustedPrincipal(harness.getTrustedPrincipal())
.build();

String identityId = iamClient.createIdentity(
identityName,
"Test identity with trust configuration",
harness.getTenantId(),
harness.getRegion(),
Optional.of(trustConfig),
Optional.empty()
);

Assertions.assertNotNull(identityId, "Identity ID should not be null");
Assertions.assertFalse(identityId.isEmpty(), "Identity ID should not be empty");

cleanUpIdentity(identityName);
}

/**
* Tests creating an identity with CreateOptions.
*/
@Test
public void testCreateIdentityWithOptions() {
String identityName = harness.getTestIdentityName() + "Options";
CreateOptions options = CreateOptions.builder().build();

String identityId = iamClient.createIdentity(
identityName,
"Test identity with options",
harness.getTenantId(),
harness.getRegion(),
Optional.empty(),
Optional.of(options)
);

Assertions.assertNotNull(identityId, "Identity ID should not be null");
Assertions.assertFalse(identityId.isEmpty(), "Identity ID should not be empty");

cleanUpIdentity(identityName);
}

/**
* Tests creating an identity with null description.
*/
@Test
public void testCreateIdentityWithNullDescription() {
String identityName = harness.getTestIdentityName() + "NoDesc";

String identityId = iamClient.createIdentity(
identityName,
null,
harness.getTenantId(),
harness.getRegion(),
Optional.empty(),
Optional.empty()
);

Assertions.assertNotNull(identityId, "Identity ID should not be null");
Assertions.assertFalse(identityId.isEmpty(), "Identity ID should not be empty");

cleanUpIdentity(identityName);
}

/**
* Tests getting an identity by name.
*/
@Test
public void testGetIdentity() throws InterruptedException {
String identityName = harness.getTestIdentityName() + "Get";
// First create an identity
String identityId = iamClient.createIdentity(
identityName,
"Test identity for get operation",
harness.getTenantId(),
harness.getRegion(),
Optional.empty(),
Optional.empty()
);

// sleep for 500ms
Thread.sleep(500);

// Then retrieve it
String retrievedIdentity = iamClient.getIdentity(
harness.getTestIdentityName() + "Get",
harness.getTenantId(),
harness.getRegion()
);

Assertions.assertNotNull(retrievedIdentity, "Retrieved identity should not be null");
Assertions.assertFalse(retrievedIdentity.isEmpty(), "Retrieved identity should not be empty");

cleanUpIdentity(identityName);
}

/**
* Tests that the provider ID is correctly set.
*/
@Test
public void testProviderId() {
Assertions.assertNotNull(iam.getProviderId(), "Provider ID should not be null");
Assertions.assertEquals(harness.getProviderId(), iam.getProviderId(),
"Provider ID should match the expected value");
}

/**
* Tests exception mapping for provider-specific exceptions.
*/
@Test
public void testExceptionMapping() {
// Test with a generic exception
Throwable genericException = new RuntimeException("Generic error");
Class<? extends com.salesforce.multicloudj.common.exceptions.SubstrateSdkException> exceptionClass =
iam.getException(genericException);

Assertions.assertNotNull(exceptionClass, "Exception class should not be null");
Assertions.assertEquals(
com.salesforce.multicloudj.common.exceptions.UnknownException.class,
exceptionClass,
"Generic exceptions should map to UnknownException"
);
}

/**
* Tests deleting an identity.
*/
@Test
public void testDeleteIdentity() {
// First create an identity
String identityId = iamClient.createIdentity(
harness.getTestIdentityName() + "Delete",
"Test identity for delete operation",
harness.getTenantId(),
harness.getRegion(),
Optional.empty(),
Optional.empty()
);

Assertions.assertNotNull(identityId, "Identity ID should not be null");

// Then delete it - should not throw any exception
Assertions.assertDoesNotThrow(() ->
iamClient.deleteIdentity(
harness.getTestIdentityName() + "Delete",
harness.getTenantId(),
harness.getRegion()
)
);
}

/**
* Tests the complete lifecycle: create, get, and delete an identity.
*/
@Test
public void testIdentityLifecycle() throws InterruptedException {
String testIdentityName = harness.getTestIdentityName() + "LifeCycle";

// Step 1: Create an identity
String identityId = iamClient.createIdentity(
testIdentityName,
"Test identity for lifecycle test",
harness.getTenantId(),
harness.getRegion(),
Optional.empty(),
Optional.empty()
);

Assertions.assertNotNull(identityId, "Identity ID should not be null after creation");
Assertions.assertFalse(identityId.isEmpty(), "Identity ID should not be empty after creation");

// Step 2: Get the identity to verify it exists
Thread.sleep(500);

String retrievedIdentity = iamClient.getIdentity(
testIdentityName,
harness.getTenantId(),
harness.getRegion()
);

Assertions.assertNotNull(retrievedIdentity, "Retrieved identity should not be null");
Assertions.assertFalse(retrievedIdentity.isEmpty(), "Retrieved identity should not be empty");

// Step 3: Delete the identity
Assertions.assertDoesNotThrow(() ->
iamClient.deleteIdentity(
testIdentityName,
harness.getTenantId(),
harness.getRegion()
),
"Deleting identity should not throw an exception"
);
}
}
6 changes: 6 additions & 0 deletions iam/iam-gcp/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,12 @@
<version>3.12.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.wiremock</groupId>
<artifactId>wiremock-grpc-extension</artifactId>
<version>0.11.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.salesforce.multicloudj</groupId>
<artifactId>iam-client</artifactId>
Expand Down
Loading
Loading