From dc842d44879b928e40e4d4057a3065d0b6498a31 Mon Sep 17 00:00:00 2001 From: Paul Latzelsperger Date: Wed, 10 Jul 2024 16:16:14 +0200 Subject: [PATCH] fix test --- .../seed/ParticipantContextSeedExtension.java | 1 - .../ParticipantContextSeedExtensionTest.java | 23 +++++++++++++------ 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/extensions/superuser-seed/src/main/java/org/eclipse/edc/identityhub/seed/ParticipantContextSeedExtension.java b/extensions/superuser-seed/src/main/java/org/eclipse/edc/identityhub/seed/ParticipantContextSeedExtension.java index ca922f309..448e03197 100644 --- a/extensions/superuser-seed/src/main/java/org/eclipse/edc/identityhub/seed/ParticipantContextSeedExtension.java +++ b/extensions/superuser-seed/src/main/java/org/eclipse/edc/identityhub/seed/ParticipantContextSeedExtension.java @@ -22,7 +22,6 @@ import org.eclipse.edc.runtime.metamodel.annotation.Setting; import org.eclipse.edc.spi.EdcException; import org.eclipse.edc.spi.monitor.Monitor; -import org.eclipse.edc.spi.result.ServiceFailure; import org.eclipse.edc.spi.security.Vault; import org.eclipse.edc.spi.system.ServiceExtension; import org.eclipse.edc.spi.system.ServiceExtensionContext; diff --git a/extensions/superuser-seed/src/test/java/org/eclipse/edc/identityhub/seed/ParticipantContextSeedExtensionTest.java b/extensions/superuser-seed/src/test/java/org/eclipse/edc/identityhub/seed/ParticipantContextSeedExtensionTest.java index b578c10a1..6635bacd0 100644 --- a/extensions/superuser-seed/src/test/java/org/eclipse/edc/identityhub/seed/ParticipantContextSeedExtensionTest.java +++ b/extensions/superuser-seed/src/test/java/org/eclipse/edc/identityhub/seed/ParticipantContextSeedExtensionTest.java @@ -32,6 +32,7 @@ import static org.mockito.ArgumentMatchers.contains; import static org.mockito.ArgumentMatchers.eq; import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verifyNoMoreInteractions; import static org.mockito.Mockito.when; @@ -39,6 +40,7 @@ @ExtendWith(DependencyInjectionExtension.class) class ParticipantContextSeedExtensionTest { + public static final String SUPER_USER = "super-user"; private final ParticipantContextService participantContextService = mock(); private final Vault vault = mock(); private final Monitor monitor = mock(); @@ -48,6 +50,7 @@ void setup(ServiceExtensionContext context) { context.registerService(ParticipantContextService.class, participantContextService); context.registerService(Vault.class, vault); context.registerService(Monitor.class, monitor); + when(participantContextService.getParticipantContext(eq(SUPER_USER))).thenReturn(ServiceResult.notFound("foobar")); } @Test @@ -58,6 +61,7 @@ void start_verifySuperUser(ParticipantContextSeedExtension ext, ext.initialize(context); ext.start(); + verify(participantContextService).getParticipantContext(eq(SUPER_USER)); verify(participantContextService).createParticipantContext(any()); verifyNoMoreInteractions(participantContextService); } @@ -69,6 +73,8 @@ void start_failsToCreate(ParticipantContextSeedExtension ext, ServiceExtensionCo .thenReturn(ServiceResult.badRequest("test-message")); ext.initialize(context); assertThatThrownBy(ext::start).isInstanceOf(EdcException.class); + + verify(participantContextService).getParticipantContext(eq(SUPER_USER)); verify(participantContextService).createParticipantContext(any()); verifyNoMoreInteractions(participantContextService); } @@ -86,13 +92,14 @@ void start_withApiKeyOverride(ParticipantContextSeedExtension ext, when(participantContextService.createParticipantContext(any())) .thenReturn(ServiceResult.success("generated-api-key")); - when(participantContextService.getParticipantContext(eq("super-user"))) + when(participantContextService.getParticipantContext(eq(SUPER_USER))) + .thenReturn(ServiceResult.notFound("foobar")) .thenReturn(ServiceResult.success(superUserContext().build())); ext.initialize(context); ext.start(); + verify(participantContextService, times(2)).getParticipantContext(eq(SUPER_USER)); verify(participantContextService).createParticipantContext(any()); - verify(participantContextService).getParticipantContext(eq("super-user")); verify(vault).storeSecret(eq("super-user-apikey"), eq(apiKeyOverride)); verifyNoMoreInteractions(participantContextService, vault); } @@ -108,13 +115,14 @@ void start_withInvalidKeyOverride(ParticipantContextSeedExtension ext, when(participantContextService.createParticipantContext(any())) .thenReturn(ServiceResult.success("generated-api-key")); - when(participantContextService.getParticipantContext(eq("super-user"))) + when(participantContextService.getParticipantContext(eq(SUPER_USER))) + .thenReturn(ServiceResult.notFound("foobar")) .thenReturn(ServiceResult.success(superUserContext().build())); ext.initialize(context); ext.start(); verify(participantContextService).createParticipantContext(any()); - verify(participantContextService).getParticipantContext(eq("super-user")); + verify(participantContextService, times(2)).getParticipantContext(eq(SUPER_USER)); verify(vault).storeSecret(eq("super-user-apikey"), eq(apiKeyOverride)); verify(monitor).warning(contains("this key appears to have an invalid format")); verifyNoMoreInteractions(participantContextService, vault); @@ -131,13 +139,14 @@ void start_whenVaultReturnsFailure(ParticipantContextSeedExtension ext, when(participantContextService.createParticipantContext(any())) .thenReturn(ServiceResult.success("generated-api-key")); - when(participantContextService.getParticipantContext(eq("super-user"))) + when(participantContextService.getParticipantContext(eq(SUPER_USER))) + .thenReturn(ServiceResult.notFound("foobar")) .thenReturn(ServiceResult.success(superUserContext().build())); ext.initialize(context); ext.start(); + verify(participantContextService, times(2)).getParticipantContext(eq(SUPER_USER)); verify(participantContextService).createParticipantContext(any()); - verify(participantContextService).getParticipantContext(eq("super-user")); verify(vault).storeSecret(eq("super-user-apikey"), eq(apiKeyOverride)); verify(monitor).warning(eq("Error storing API key in vault: test-failure")); verifyNoMoreInteractions(participantContextService, vault); @@ -145,7 +154,7 @@ void start_whenVaultReturnsFailure(ParticipantContextSeedExtension ext, private ParticipantContext.Builder superUserContext() { return ParticipantContext.Builder.newInstance() - .participantId("super-user") + .participantId(SUPER_USER) .apiTokenAlias("super-user-apikey"); }