Skip to content

Commit

Permalink
fix test
Browse files Browse the repository at this point in the history
  • Loading branch information
paullatzelsperger committed Jul 11, 2024
1 parent a0a8c41 commit dc842d4
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,15 @@
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;

@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();
Expand All @@ -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
Expand All @@ -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);
}
Expand All @@ -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);
}
Expand All @@ -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);
}
Expand All @@ -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);
Expand All @@ -131,21 +139,22 @@ 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);
}

private ParticipantContext.Builder superUserContext() {
return ParticipantContext.Builder.newInstance()
.participantId("super-user")
.participantId(SUPER_USER)
.apiTokenAlias("super-user-apikey");

}
Expand Down

0 comments on commit dc842d4

Please sign in to comment.