Skip to content

Commit

Permalink
chore: refactor test to use context injection
Browse files Browse the repository at this point in the history
  • Loading branch information
Rafael Magalhaes committed Feb 7, 2024
1 parent 5dda6b9 commit 45ae2fa
Show file tree
Hide file tree
Showing 13 changed files with 31 additions and 98 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import org.eclipse.edc.jsonld.spi.JsonLd;
import org.eclipse.edc.junit.extensions.DependencyInjectionExtension;
import org.eclipse.edc.spi.system.ServiceExtensionContext;
import org.eclipse.edc.spi.system.injection.ObjectFactory;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
Expand All @@ -40,18 +39,15 @@
@ExtendWith(DependencyInjectionExtension.class)
public class JsonLdExtensionTest {

JsonLdExtension extension;

JsonLd jsonLdService = mock(JsonLd.class);

@BeforeEach
void setup(ObjectFactory factory, ServiceExtensionContext context) {
void setup(ServiceExtensionContext context) {
context.registerService(JsonLd.class, jsonLdService);
extension = factory.constructInstance(JsonLdExtension.class);
}

@Test
void initialize(ServiceExtensionContext context) {
void initialize(ServiceExtensionContext context, JsonLdExtension extension) {
extension.initialize(context);
jsonLdService.registerCachedDocument(eq(CREDENTIALS_V_1), any(URI.class));
jsonLdService.registerCachedDocument(eq(CREDENTIALS_SUMMARY_V_1), any(URI.class));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@
import org.eclipse.edc.spi.monitor.Monitor;
import org.eclipse.edc.spi.security.Vault;
import org.eclipse.edc.spi.system.ServiceExtensionContext;
import org.eclipse.edc.spi.system.injection.ObjectFactory;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.params.ParameterizedTest;
Expand All @@ -41,27 +39,23 @@

@ExtendWith(DependencyInjectionExtension.class)
class VaultSeedExtensionTest {
private VaultSeedExtension extension;
private ServiceExtensionContext context;
private Monitor monitor;

@BeforeEach
void setup(ServiceExtensionContext context, ObjectFactory factory) {
this.context = context;
monitor = mock(Monitor.class);
context.registerService(Monitor.class, monitor);
context.registerService(Vault.class, new InMemoryVault(monitor));
extension = factory.constructInstance(VaultSeedExtension.class);
}

@Test
void name() {
void name(VaultSeedExtension extension) {
assertThat(extension.name()).isEqualTo("Vault Seed Extension");
}

@ParameterizedTest
@ValueSource(strings = { "key1:", "key1:value1", "key1:value1;", ";key1:value1", ";sdf;key1:value1" })
void createInMemVault_validString(String secret) {
@ValueSource(strings = {"key1:", "key1:value1", "key1:value1;", ";key1:value1", ";sdf;key1:value1"})
void createInMemVault_validString(String secret, ServiceExtensionContext context, VaultSeedExtension extension) {
when(context.getSetting(eq(VaultSeedExtension.VAULT_MEMORY_SECRETS_PROPERTY), eq(null))).thenReturn(secret);
extension.createInMemVault(context);
verify(monitor, times(1)).debug(anyString());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import org.eclipse.edc.junit.extensions.DependencyInjectionExtension;
import org.eclipse.edc.spi.monitor.Monitor;
import org.eclipse.edc.spi.system.ServiceExtensionContext;
import org.eclipse.edc.spi.system.injection.ObjectFactory;
import org.eclipse.tractusx.edc.data.encryption.aes.AesEncryptor;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand All @@ -47,41 +46,37 @@
class TxEncryptorExtensionTest {

private final Monitor monitor = mock();
private TxEncryptorExtension extension;
private ServiceExtensionContext context;

@BeforeEach
void setup(ObjectFactory factory, ServiceExtensionContext c) {
c.registerService(Monitor.class, monitor);
context = c;
void setup(ServiceExtensionContext context) {
context.registerService(Monitor.class, monitor);
when(context.getSetting(ENCRYPTION_KEY_ALIAS, null)).thenReturn("test-key");
extension = factory.constructInstance(TxEncryptorExtension.class);
}

@Test
void createEncryptor_noConfig_createsDefault() {
void createEncryptor_noConfig_createsDefault(ServiceExtensionContext context, TxEncryptorExtension extension) {
var encryptor = extension.createEncryptor(context);
assertThat(encryptor).isInstanceOf(AesEncryptor.class);
}

@Test
void createEncryptor_otherAlgorithm_createsNoop() {
void createEncryptor_otherAlgorithm_createsNoop(ServiceExtensionContext context, TxEncryptorExtension extension) {
when(context.getSetting(eq(ENCRYPTION_ALGORITHM), any())).thenReturn("some-algorithm");
var encryptor = extension.createEncryptor(context);
assertThat(encryptor).isInstanceOf(NoopDataEncrypter.class);
verify(monitor).warning(eq("Algorithm some-algorithm is not known, will use a NOOP encryptor!"));
}

@Test
void createEncryptor_withPropertyEqualsAes() {
void createEncryptor_withPropertyEqualsAes(ServiceExtensionContext context, TxEncryptorExtension extension) {
when(context.getSetting(eq(ENCRYPTION_ALGORITHM), any())).thenReturn("AES");
var encryptor = extension.createEncryptor(context);
assertThat(encryptor).isInstanceOf(AesEncryptor.class);
}

@ParameterizedTest
@ValueSource(strings = { CACHING_ENABLED, CACHING_SECONDS })
void verifyDeprecationWarnings(String deprecatedSetting) {
void verifyDeprecationWarnings(String deprecatedSetting, ServiceExtensionContext context, TxEncryptorExtension extension) {
when(context.getSetting(eq(deprecatedSetting), any())).thenReturn("doesn't matter");
extension.createEncryptor(context);
verify(monitor).warning(startsWith("Caching the secret keys was deprecated because it is unsafe"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,18 +44,16 @@ public class EdrApiExtensionTest {
private final ManagementApiTypeTransformerRegistry transformerRegistry = mock();
private final WebService webService = mock(WebService.class);
private final ManagementApiConfiguration configuration = mock(ManagementApiConfiguration.class);
private EdrApiExtension extension;

@BeforeEach
void setUp(ObjectFactory factory, ServiceExtensionContext context) {
context.registerService(WebService.class, webService);
context.registerService(ManagementApiTypeTransformerRegistry.class, transformerRegistry);
context.registerService(ManagementApiConfiguration.class, configuration);
extension = factory.constructInstance(EdrApiExtension.class);
}

@Test
void initialize_ShouldConfigureTheController(ServiceExtensionContext context) {
void initialize_ShouldConfigureTheController(ServiceExtensionContext context, EdrApiExtension extension) {
var alias = "context";

when(configuration.getContextAlias()).thenReturn(alias);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@

import org.eclipse.edc.junit.extensions.DependencyInjectionExtension;
import org.eclipse.edc.spi.system.ServiceExtensionContext;
import org.eclipse.edc.spi.system.injection.ObjectFactory;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;

Expand All @@ -31,16 +29,8 @@
@ExtendWith(DependencyInjectionExtension.class)
public class InProcessCallbackRegistryExtensionTest {

InProcessCallbackRegistryExtension extension;


@BeforeEach
void setUp(ObjectFactory factory, ServiceExtensionContext context) {
extension = factory.constructInstance(InProcessCallbackRegistryExtension.class);
}

@Test
void shouldInitializeTheExtension(ServiceExtensionContext context) {
void shouldInitializeTheExtension(ServiceExtensionContext context, InProcessCallbackRegistryExtension extension) {
assertThat(extension.callbackRegistry()).isInstanceOf(InProcessCallbackRegistryImpl.class);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import org.eclipse.edc.junit.extensions.DependencyInjectionExtension;
import org.eclipse.edc.spi.message.RemoteMessageDispatcherRegistry;
import org.eclipse.edc.spi.system.ServiceExtensionContext;
import org.eclipse.edc.spi.system.injection.ObjectFactory;
import org.eclipse.tractusx.edc.spi.callback.InProcessCallback;
import org.eclipse.tractusx.edc.spi.callback.InProcessCallbackRegistry;
import org.junit.jupiter.api.BeforeEach;
Expand All @@ -41,25 +40,22 @@
@ExtendWith(DependencyInjectionExtension.class)
public class LocalCallbackExtensionTest {

LocalCallbackExtension extension;

RemoteMessageDispatcherRegistry dispatcherRegistry = mock(RemoteMessageDispatcherRegistry.class);

CallbackProtocolResolverRegistry resolverRegistry = mock(CallbackProtocolResolverRegistry.class);

InProcessCallbackRegistry inProcessCallbackRegistry = mock(InProcessCallbackRegistry.class);

@BeforeEach
void setUp(ObjectFactory factory, ServiceExtensionContext context) {
void setUp(ServiceExtensionContext context) {

context.registerService(RemoteMessageDispatcherRegistry.class, dispatcherRegistry);
context.registerService(CallbackProtocolResolverRegistry.class, resolverRegistry);
context.registerService(InProcessCallbackRegistry.class, inProcessCallbackRegistry);
extension = factory.constructInstance(LocalCallbackExtension.class);
}

@Test
void shouldInitializeTheExtension(ServiceExtensionContext context) {
void shouldInitializeTheExtension(ServiceExtensionContext context, LocalCallbackExtension extension) {
extension.initialize(context);

var captor = ArgumentCaptor.forClass(CallbackProtocolResolver.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import org.eclipse.edc.spi.iam.IdentityService;
import org.eclipse.edc.spi.system.ServiceExtensionContext;
import org.eclipse.edc.spi.system.configuration.Config;
import org.eclipse.edc.spi.system.injection.ObjectFactory;
import org.eclipse.tractusx.edc.iam.ssi.spi.SsiCredentialClient;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand All @@ -40,17 +39,13 @@ public class SsiIdentityServiceExtensionTest {

SsiIdentityServiceExtension extension;

ServiceExtensionContext context;

@BeforeEach
void setup(ObjectFactory factory, ServiceExtensionContext context) {
this.context = context;
void setup(ServiceExtensionContext context) {
context.registerService(SsiCredentialClient.class, mock(SsiCredentialClient.class));
extension = factory.constructInstance(SsiIdentityServiceExtension.class);
}

@Test
void initialize() {
void initialize(ServiceExtensionContext context, SsiIdentityServiceExtension extension) {
var cfg = mock(Config.class);
when(context.getConfig()).thenReturn(cfg);
when(cfg.getString(ENDPOINT_AUDIENCE)).thenReturn("test");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import org.eclipse.edc.junit.extensions.DependencyInjectionExtension;
import org.eclipse.edc.spi.agent.ParticipantAgentService;
import org.eclipse.edc.spi.system.ServiceExtensionContext;
import org.eclipse.edc.spi.system.injection.ObjectFactory;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
Expand All @@ -34,18 +33,15 @@
@ExtendWith(DependencyInjectionExtension.class)
public class SsiIdentityExtractorExtensionTest {

SsiIdentityExtractorExtension extension;

ParticipantAgentService participantAgentService = mock(ParticipantAgentService.class);

@BeforeEach
void setup(ObjectFactory factory, ServiceExtensionContext context) {
void setup(ServiceExtensionContext context) {
context.registerService(ParticipantAgentService.class, participantAgentService);
extension = factory.constructInstance(SsiIdentityExtractorExtension.class);
}

@Test
void initialize(ServiceExtensionContext context) {
void initialize(ServiceExtensionContext context, SsiIdentityExtractorExtension extension) {
extension.initialize(context);
verify(participantAgentService).register(isA(CredentialIdentityExtractor.class));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@

import org.eclipse.edc.junit.extensions.DependencyInjectionExtension;
import org.eclipse.edc.spi.system.ServiceExtensionContext;
import org.eclipse.edc.spi.system.injection.ObjectFactory;
import org.eclipse.edc.spi.types.TypeManager;
import org.eclipse.tractusx.edc.iam.ssi.miw.api.MiwApiClient;
import org.eclipse.tractusx.edc.iam.ssi.miw.api.MiwApiClientImpl;
Expand All @@ -39,18 +38,16 @@
public class SsiMiwApiClientExtensionTest {

private final SsiMiwConfiguration cfg = mock(SsiMiwConfiguration.class);
private SsiMiwApiClientExtension extension;

@BeforeEach
void setup(ObjectFactory factory, ServiceExtensionContext context) {
void setup(ServiceExtensionContext context) {
context.registerService(SsiMiwConfiguration.class, cfg);
context.registerService(MiwApiClient.class, mock(MiwApiClient.class));
context.registerService(TypeManager.class, new TypeManager());
extension = factory.constructInstance(SsiMiwApiClientExtension.class);
}

@Test
void initialize(ServiceExtensionContext context) {
void initialize(ServiceExtensionContext context, SsiMiwApiClientExtension extension) {
when(cfg.getUrl()).thenReturn("http://localhost");
when(cfg.getAuthorityId()).thenReturn("id");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@
import org.eclipse.edc.junit.extensions.DependencyInjectionExtension;
import org.eclipse.edc.spi.system.ServiceExtensionContext;
import org.eclipse.edc.spi.system.configuration.Config;
import org.eclipse.edc.spi.system.injection.ObjectFactory;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;

Expand All @@ -42,18 +40,8 @@
@ExtendWith(DependencyInjectionExtension.class)
public class SsiMiwConfigurationExtensionTest {

private SsiMiwConfigurationExtension extension;

private ServiceExtensionContext context;

@BeforeEach
void setup(ObjectFactory factory, ServiceExtensionContext context) {
this.context = context;
extension = factory.constructInstance(SsiMiwConfigurationExtension.class);
}

@Test
void initialize() {
void initialize(ServiceExtensionContext context, SsiMiwConfigurationExtension extension) {
var url = "http://localhost:8080";
var authorityId = "id";
var authorityIssuer = "issuer";
Expand All @@ -78,7 +66,7 @@ void initialize() {
}

@Test
void initialize_withDefaultIssuer() {
void initialize_withDefaultIssuer(ServiceExtensionContext context, SsiMiwConfigurationExtension extension) {
var url = "http://localhost:8080";
var authorityId = "id";

Expand All @@ -102,7 +90,7 @@ void initialize_withDefaultIssuer() {
}

@Test
void initialize_withTrailingUrl() {
void initialize_withTrailingUrl(ServiceExtensionContext context, SsiMiwConfigurationExtension extension) {
var url = "http://localhost:8080/";
var authorityId = "id";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@

import org.eclipse.edc.junit.extensions.DependencyInjectionExtension;
import org.eclipse.edc.spi.system.ServiceExtensionContext;
import org.eclipse.edc.spi.system.injection.ObjectFactory;
import org.eclipse.tractusx.edc.iam.ssi.miw.api.MiwApiClient;
import org.eclipse.tractusx.edc.iam.ssi.miw.credentials.SsiMiwCredentialClient;
import org.junit.jupiter.api.BeforeEach;
Expand All @@ -37,13 +36,12 @@ public class SsiMiwCredentialClientExtensionTest {
SsiMiwCredentialClientExtension extension;

@BeforeEach
void setup(ObjectFactory factory, ServiceExtensionContext context) {
void setup(ServiceExtensionContext context) {
context.registerService(MiwApiClient.class, mock(MiwApiClient.class));
extension = factory.constructInstance(SsiMiwCredentialClientExtension.class);
}

@Test
void initialize() {
void initialize(SsiMiwCredentialClientExtension extension) {
assertThat(extension.credentialVerifier()).isInstanceOf(SsiMiwCredentialClient.class);
}

Expand Down
Loading

0 comments on commit 45ae2fa

Please sign in to comment.