Skip to content

Commit

Permalink
fix: use per-class runtimes in E2E tests (#371)
Browse files Browse the repository at this point in the history
* fix(test): use new test runtime model

* move to per-class runtimes

* DEPENDENCIES
  • Loading branch information
paullatzelsperger authored Jun 7, 2024
1 parent 3b7f774 commit 27ade02
Show file tree
Hide file tree
Showing 7 changed files with 123 additions and 67 deletions.
1 change: 1 addition & 0 deletions DEPENDENCIES
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,7 @@ maven/mavencentral/org.codehaus.plexus/plexus-utils/3.3.0, , approved, CQ21066
maven/mavencentral/org.eclipse.angus/angus-activation/1.0.0, EPL-2.0 OR GPL-2.0-only with Classpath-exception-2.0, approved, ee4j.angus
maven/mavencentral/org.eclipse.edc/api-observability/0.7.1-SNAPSHOT, Apache-2.0, approved, technology.edc
maven/mavencentral/org.eclipse.edc/asset-spi/0.7.1-SNAPSHOT, Apache-2.0, approved, technology.edc
maven/mavencentral/org.eclipse.edc/auth-spi/0.7.1-SNAPSHOT, Apache-2.0, approved, technology.edc
maven/mavencentral/org.eclipse.edc/autodoc-processor/0.7.1-SNAPSHOT, Apache-2.0, approved, technology.edc
maven/mavencentral/org.eclipse.edc/boot-lib/0.7.1-SNAPSHOT, Apache-2.0, approved, technology.edc
maven/mavencentral/org.eclipse.edc/boot-spi/0.7.1-SNAPSHOT, Apache-2.0, approved, technology.edc
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -300,8 +300,8 @@ private void deleted(ParticipantContextDeleted event) {
//unpublish and delete all DIDs associated with that participant
var errors = findByParticipantId(participantId)
.stream()
.map(didResource -> unpublish(didResource.getDid())
.compose(u -> deleteById(didResource.getDid())))
.map(didResource -> unpublish(didResource.getDid()).compose(u -> deleteById(didResource.getDid())))
.filter(AbstractResult::failed)
.map(AbstractResult::getFailureDetail)
.collect(Collectors.joining(", "));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,14 @@
import org.eclipse.edc.iam.did.spi.document.DidDocument;
import org.eclipse.edc.identithub.spi.did.events.DidDocumentPublished;
import org.eclipse.edc.identithub.spi.did.events.DidDocumentUnpublished;
import org.eclipse.edc.identityhub.spi.participantcontext.ParticipantContextService;
import org.eclipse.edc.identityhub.spi.participantcontext.model.ParticipantContext;
import org.eclipse.edc.junit.annotations.EndToEndTest;
import org.eclipse.edc.spi.event.EventRouter;
import org.eclipse.edc.spi.event.EventSubscriber;
import org.eclipse.edc.spi.query.QuerySpec;
import org.hamcrest.Matchers;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;

import java.util.Arrays;
Expand All @@ -39,6 +42,15 @@
@EndToEndTest
public class DidManagementApiEndToEndTest extends IdentityApiEndToEndTest {


@AfterEach
void tearDown() {
// purge all users
var store = RUNTIME.getService(ParticipantContextService.class);
store.query(QuerySpec.max()).getContent()
.forEach(pc -> store.deleteParticipantContext(pc.getParticipantId()));
}

@Test
void publishDid_notOwner_expect403() {
var subscriber = mock(EventSubscriber.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@
import org.eclipse.edc.identityhub.spi.store.KeyPairResourceStore;
import org.eclipse.edc.identityhub.spi.store.ParticipantContextStore;
import org.eclipse.edc.identityhub.tests.fixtures.IdentityHubRuntimeConfiguration;
import org.eclipse.edc.junit.extensions.EdcRuntimeExtension;
import org.eclipse.edc.junit.extensions.EmbeddedRuntime;
import org.eclipse.edc.junit.extensions.RuntimeExtension;
import org.eclipse.edc.junit.extensions.RuntimePerClassExtension;
import org.eclipse.edc.spi.EdcException;
import org.eclipse.edc.spi.query.Criterion;
import org.eclipse.edc.spi.query.QuerySpec;
Expand All @@ -50,34 +52,51 @@ public abstract class IdentityApiEndToEndTest {
.id("identity-hub")
.build();
@RegisterExtension
protected static final EdcRuntimeExtension RUNTIME = new EdcRuntimeExtension(":launcher", "identity-hub", RUNTIME_CONFIGURATION.controlPlaneConfiguration());
protected static final RuntimeExtension RUNTIME = new RuntimePerClassExtension(new EmbeddedRuntime("identity-hub", RUNTIME_CONFIGURATION.controlPlaneConfiguration(), ":launcher"));

protected static ParticipantManifest.Builder createNewParticipant() {
protected static String createSuperUser() {
return createParticipant(SUPER_USER, List.of(ServicePrincipal.ROLE_ADMIN));
}

private static String createParticipant(String participantId, List<String> roles) {
var manifest = ParticipantManifest.Builder.newInstance()
.participantId("another-participant")
.active(false)
.did("did:web:another:participant")
.serviceEndpoint(new Service("test-service", "test-service-type", "https://test.com"))
.key(createKeyDescriptor().build());
return manifest;
.participantId(participantId)
.active(true)
.roles(roles)
.serviceEndpoint(new Service("test-service-id", "test-type", "http://foo.bar.com"))
.did("did:web:" + participantId)
.key(KeyDescriptor.Builder.newInstance()
.privateKeyAlias(participantId + "-alias")
.resourceId(participantId + "-resource")
.keyId(participantId + "-key")
.keyGeneratorParams(Map.of("algorithm", "EC", "curve", "secp256r1"))
.build())
.build();
var srv = RUNTIME.getService(ParticipantContextService.class);
return srv.createParticipantContext(manifest).orElseThrow(f -> new EdcException(f.getFailureDetail()));
}

@NotNull
public static KeyDescriptor.Builder createKeyDescriptor() {
public KeyDescriptor.Builder createKeyDescriptor() {
return KeyDescriptor.Builder.newInstance()
.privateKeyAlias("another-alias")
.keyGeneratorParams(Map.of("algorithm", "EdDSA", "curve", "Ed25519"))
.keyId("another-keyid");
}

protected String createSuperUser() {
return createParticipant(SUPER_USER, List.of(ServicePrincipal.ROLE_ADMIN));
protected ParticipantManifest.Builder createNewParticipant() {
return ParticipantManifest.Builder.newInstance()
.participantId("another-participant")
.active(false)
.did("did:web:another:participant")
.serviceEndpoint(new Service("test-service", "test-service-type", "https://test.com"))
.key(createKeyDescriptor().build());
}

protected String storeParticipant(ParticipantContext pc) {
var store = RUNTIME.getContext().getService(ParticipantContextStore.class);
var store = RUNTIME.getService(ParticipantContextStore.class);

var vault = RUNTIME.getContext().getService(Vault.class);
var vault = RUNTIME.getService(Vault.class);
var token = createTokenFor(pc.getParticipantId());
vault.storeSecret(pc.getApiTokenAlias(), token);
store.create(pc).orElseThrow(f -> new RuntimeException(f.getFailureDetail()));
Expand All @@ -93,7 +112,7 @@ protected String createTokenFor(String userId) {
}

protected <T> T getService(Class<T> type) {
return RUNTIME.getContext().getService(type);
return RUNTIME.getService(type);
}

protected Collection<KeyPairResource> getKeyPairsForParticipant(String participantId) {
Expand All @@ -112,22 +131,4 @@ protected ParticipantContext getParticipant(String participantId) {
.getParticipantContext(participantId)
.orElseThrow(f -> new EdcException(f.getFailureDetail()));
}

private String createParticipant(String participantId, List<String> roles) {
var manifest = ParticipantManifest.Builder.newInstance()
.participantId(participantId)
.active(true)
.roles(roles)
.serviceEndpoint(new Service("test-service-id", "test-type", "http://foo.bar.com"))
.did("did:web:" + participantId)
.key(KeyDescriptor.Builder.newInstance()
.privateKeyAlias(participantId + "-alias")
.resourceId(participantId + "-resource")
.keyId(participantId + "-key")
.keyGeneratorParams(Map.of("algorithm", "EC", "curve", "secp256r1"))
.build())
.build();
var srv = RUNTIME.getContext().getService(ParticipantContextService.class);
return srv.createParticipantContext(manifest).orElseThrow(f -> new EdcException(f.getFailureDetail()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,21 @@
import org.eclipse.edc.identityhub.spi.keypair.events.KeyPairRotated;
import org.eclipse.edc.identityhub.spi.keypair.model.KeyPairResource;
import org.eclipse.edc.identityhub.spi.keypair.model.KeyPairState;
import org.eclipse.edc.identityhub.spi.participantcontext.ParticipantContextService;
import org.eclipse.edc.identityhub.spi.participantcontext.model.KeyDescriptor;
import org.eclipse.edc.identityhub.spi.participantcontext.model.ParticipantContext;
import org.eclipse.edc.junit.annotations.EndToEndTest;
import org.eclipse.edc.spi.EdcException;
import org.eclipse.edc.spi.event.EventRouter;
import org.eclipse.edc.spi.event.EventSubscriber;
import org.eclipse.edc.spi.query.QuerySpec;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;

import java.util.Arrays;
import java.util.Base64;
import java.util.Map;
import java.util.UUID;
import java.util.stream.IntStream;

import static io.restassured.http.ContentType.JSON;
import static java.util.stream.IntStream.range;
Expand All @@ -48,13 +50,12 @@
@EndToEndTest
public class KeyPairResourceApiEndToEndTest extends IdentityApiEndToEndTest {

private static KeyDescriptor.Builder createKeyDescriptor(String participantId) {
var keyId = UUID.randomUUID().toString();
return KeyDescriptor.Builder.newInstance()
.keyId(keyId)
.resourceId(UUID.randomUUID().toString())
.keyGeneratorParams(Map.of("algorithm", "EC", "curve", Curve.P_384.getStdName()))
.privateKeyAlias("%s-%s-alias".formatted(participantId, keyId));
@AfterEach
void tearDown() {
// purge all users
var pcService = RUNTIME.getService(ParticipantContextService.class);
pcService.query(QuerySpec.max()).getContent()
.forEach(pc -> pcService.deleteParticipantContext(pc.getParticipantId()).getContent());
}

@Test
Expand Down Expand Up @@ -384,7 +385,7 @@ void getAll_withPaging() {
@Test
void getAll_withDefaultPaging() {
var superUserKey = createSuperUser();
IntStream.range(0, 70)
range(0, 70)
.forEach(i -> {
var participantId = "user" + i;
createParticipant(participantId); // implicitly creates a keypair
Expand Down Expand Up @@ -490,11 +491,20 @@ void activate_illegalState() {
.body(notNullValue());
}

private KeyDescriptor.Builder createKeyDescriptor(String participantId) {
var keyId = UUID.randomUUID().toString();
return KeyDescriptor.Builder.newInstance()
.keyId(keyId)
.resourceId(UUID.randomUUID().toString())
.keyGeneratorParams(Map.of("algorithm", "EC", "curve", Curve.P_384.getStdName()))
.privateKeyAlias("%s-%s-alias".formatted(participantId, keyId));
}

private String createKeyPair(String participantId) {

var descriptor = createKeyDescriptor(participantId).build();

var service = RUNTIME.getContext().getService(KeyPairService.class);
var service = RUNTIME.getService(KeyPairService.class);
service.addKeyPair(participantId, descriptor, true)
.orElseThrow(f -> new EdcException(f.getFailureDetail()));
return descriptor.getResourceId();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
import org.eclipse.edc.spi.EdcException;
import org.eclipse.edc.spi.event.EventRouter;
import org.eclipse.edc.spi.event.EventSubscriber;
import org.eclipse.edc.spi.query.QuerySpec;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
Expand All @@ -50,6 +52,14 @@
@EndToEndTest
public class ParticipantContextApiEndToEndTest extends IdentityApiEndToEndTest {

@AfterEach
void tearDown() {
// purge all users
var pcService = RUNTIME.getService(ParticipantContextService.class);
pcService.query(QuerySpec.max()).getContent()
.forEach(pc -> pcService.deleteParticipantContext(pc.getParticipantId()).getContent());
}

@Test
void getUserById() {
var apikey = createSuperUser();
Expand Down Expand Up @@ -222,7 +232,7 @@ void activateParticipant_principalIsSuperser() {
.log().ifError()
.statusCode(204);

var updatedParticipant = RUNTIME.getContext().getService(ParticipantContextService.class).getParticipantContext(participantId).orElseThrow(f -> new EdcException(f.getFailureDetail()));
var updatedParticipant = RUNTIME.getService(ParticipantContextService.class).getParticipantContext(participantId).orElseThrow(f -> new EdcException(f.getFailureDetail()));
assertThat(updatedParticipant.getState()).isEqualTo(ParticipantContextState.ACTIVATED.ordinal());
// verify the correct event was emitted
verify(subscriber).on(argThat(env -> {
Expand Down
Loading

0 comments on commit 27ade02

Please sign in to comment.