Skip to content

Commit

Permalink
Added CDIManagedClientsTest.
Browse files Browse the repository at this point in the history
  • Loading branch information
merlante committed Aug 6, 2024
1 parent fe82e97 commit 93117cf
Show file tree
Hide file tree
Showing 2 changed files with 188 additions and 0 deletions.
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,12 @@
<version>5.10.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-inline</artifactId>
<version>5.2.0</version>
<scope>test</scope>
</dependency>
<!-- Use Weld as a CDI container for CDI testing. -->
<dependency>
<groupId>org.jboss.weld</groupId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
package org.project_kessel.relations.client;

import org.junit.jupiter.api.Test;
import org.mockito.MockedStatic;
import org.mockito.Mockito;

import java.util.Optional;

import static org.mockito.Mockito.*;

class CDIManagedClientsTest {
@Test
void testInsecureNoAuthnMakesCorrectManagerCall() {
Config config = makeDummyConfig(false, makeDummyAuthenticationConfig(false));
CDIManagedClients cdiManagedClients = new CDIManagedClients();

try (MockedStatic<RelationsGrpcClientsManager> dummyManager = Mockito.mockStatic(RelationsGrpcClientsManager.class)) {
cdiManagedClients.getManager(config);
dummyManager.verify(
() -> RelationsGrpcClientsManager.forInsecureClients(anyString()),
times(1)
);
dummyManager.verify(
() -> RelationsGrpcClientsManager.forInsecureClients(anyString(), any()),
times(0)
);
dummyManager.verify(
() -> RelationsGrpcClientsManager.forSecureClients(anyString()),
times(0)
);
dummyManager.verify(
() -> RelationsGrpcClientsManager.forSecureClients(anyString(), any()),
times(0)
);
}
}

@Test
void testInsecureWithAuthnMakesCorrectManagerCall() {
Config config = makeDummyConfig(false, makeDummyAuthenticationConfig(true));
CDIManagedClients cdiManagedClients = new CDIManagedClients();

try (MockedStatic<RelationsGrpcClientsManager> dummyManager = Mockito.mockStatic(RelationsGrpcClientsManager.class)) {
cdiManagedClients.getManager(config);
dummyManager.verify(
() -> RelationsGrpcClientsManager.forInsecureClients(anyString()),
times(0)
);
dummyManager.verify(
() -> RelationsGrpcClientsManager.forInsecureClients(anyString(), any()),
times(1)
);
dummyManager.verify(
() -> RelationsGrpcClientsManager.forSecureClients(anyString()),
times(0)
);
dummyManager.verify(
() -> RelationsGrpcClientsManager.forSecureClients(anyString(), any()),
times(0)
);
}
}

@Test
void testSecureNoAuthnMakesCorrectManagerCall() {
Config config = makeDummyConfig(true, makeDummyAuthenticationConfig(false));
CDIManagedClients cdiManagedClients = new CDIManagedClients();

try (MockedStatic<RelationsGrpcClientsManager> dummyManager = Mockito.mockStatic(RelationsGrpcClientsManager.class)) {
cdiManagedClients.getManager(config);
dummyManager.verify(
() -> RelationsGrpcClientsManager.forInsecureClients(anyString()),
times(0)
);
dummyManager.verify(
() -> RelationsGrpcClientsManager.forInsecureClients(anyString(), any()),
times(0)
);
dummyManager.verify(
() -> RelationsGrpcClientsManager.forSecureClients(anyString()),
times(1)
);
dummyManager.verify(
() -> RelationsGrpcClientsManager.forSecureClients(anyString(), any()),
times(0)
);
}
}

@Test
void testSecureWithAuthnMakesCorrectManagerCall() {
Config config = makeDummyConfig(true, makeDummyAuthenticationConfig(true));
CDIManagedClients cdiManagedClients = new CDIManagedClients();

try (MockedStatic<RelationsGrpcClientsManager> dummyManager = Mockito.mockStatic(RelationsGrpcClientsManager.class)) {
cdiManagedClients.getManager(config);
dummyManager.verify(
() -> RelationsGrpcClientsManager.forInsecureClients(anyString()),
times(0)
);
dummyManager.verify(
() -> RelationsGrpcClientsManager.forInsecureClients(anyString(), any()),
times(0)
);
dummyManager.verify(
() -> RelationsGrpcClientsManager.forSecureClients(anyString()),
times(0)
);
dummyManager.verify(
() -> RelationsGrpcClientsManager.forSecureClients(anyString(), any()),
times(1)
);
}
}

static Config makeDummyConfig(boolean secure, Config.AuthenticationConfig authnConfig) {
return new Config() {
@Override
public boolean isSecureClients() {
return secure;
}

@Override
public String targetUrl() {
return "localhost";
}

@Override
public Optional<AuthenticationConfig> authenticationConfig() {
return Optional.of(authnConfig);
}
};
}

static Config.AuthenticationConfig makeDummyAuthenticationConfig(boolean authnEnabled) {
return new Config.AuthenticationConfig() {
@Override
public Config.AuthMode mode() {
if(!authnEnabled) {
return Config.AuthMode.DISABLED;
}
// pick some arbitrary non disabled mode
return Config.AuthMode.OIDC_CLIENT_CREDENTIALS;
}

@Override
public Optional<Config.OIDCClientCredentialsConfig> clientCredentialsConfig() {
if(!authnEnabled) {
return Optional.empty();
}

// provide dummy config matching mode, above.
return Optional.of(new Config.OIDCClientCredentialsConfig() {
@Override
public String issuer() {
return "";
}

@Override
public String clientId() {
return "";
}

@Override
public String clientSecret() {
return "";
}

@Override
public Optional<String[]> scope() {
return Optional.empty();
}

@Override
public Optional<String> oidcClientCredentialsMinterImplementation() {
return Optional.empty();
}
});
}
};
}
}

0 comments on commit 93117cf

Please sign in to comment.