Skip to content

Commit

Permalink
PortalTestConfiguration: Removing fireEvent and put operations, intro…
Browse files Browse the repository at this point in the history
…ducing update
  • Loading branch information
cuioss committed Aug 28, 2024
1 parent 305370f commit 693442c
Show file tree
Hide file tree
Showing 10 changed files with 40 additions and 96 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -68,15 +68,15 @@ void shouldLogoutAuthenticatedAsDefault() {

@Test
void shouldNotAuthenticateAsDefaultAsConfigured() {
configuration.fireEvent(MockAuthenticationFacade.CONFIGURATION_KEY_AUTHENTICATED, "false");
configuration.update(MockAuthenticationFacade.CONFIGURATION_KEY_AUTHENTICATED, "false");
final var userInfo = underTest.retrieveCurrentAuthenticationContext(servletRequest);
assertNotNull(userInfo);
assertFalse(userInfo.isAuthenticated());
}

@Test
void shouldProvideConfiguredRoles() {
configuration.fireEvent(MockAuthenticationFacade.CONFIGURATION_KEY_ROLES, "role, role2");
configuration.update(MockAuthenticationFacade.CONFIGURATION_KEY_ROLES, "role, role2");
final var userInfo = underTest.retrieveCurrentAuthenticationContext(servletRequest);
assertEquals(2, userInfo.getRoles().size());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,12 +101,11 @@ public void assertLogoutURL(String actualUrl, String... parts) {

public void configure(PortalTestConfiguration configuration, MockWebServer mockWebServer) {
currentPort = String.valueOf(mockWebServer.getPort());
configuration.put(OAuthConfigKeys.OPEN_ID_SERVER_BASE_URL, "http://localhost:" + mockWebServer.getPort());
configuration.put(OAuthConfigKeys.OPEN_ID_DISCOVER_PATH, OIDC_DISCOVERY_PATH);
configuration.put(OAuthConfigKeys.OPEN_ID_CLIENT_ID, CLIENT_ID);
configuration.put(OAuthConfigKeys.OPEN_ID_CLIENT_SECRET, CLIENT_SECRET);
configuration.put(OAuthConfigKeys.EXTERNAL_HOSTNAME, EXTERNAL_HOSTNAME);
configuration.fireEvent();
configuration.update(OAuthConfigKeys.OPEN_ID_SERVER_BASE_URL, "http://localhost:" + mockWebServer.getPort());
configuration.update(OAuthConfigKeys.OPEN_ID_DISCOVER_PATH, OIDC_DISCOVERY_PATH);
configuration.update(OAuthConfigKeys.OPEN_ID_CLIENT_ID, CLIENT_ID);
configuration.update(OAuthConfigKeys.OPEN_ID_CLIENT_SECRET, CLIENT_SECRET);
configuration.update(OAuthConfigKeys.EXTERNAL_HOSTNAME, EXTERNAL_HOSTNAME);
}

public List<RecordedRequest> nonWellKnownRequests(MockWebServer mockWebServer) throws InterruptedException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ class Oauth2DiscoveryConfigurationProducerTest

@BeforeEach
void beforeEach() {
configuration.fireEvent(OAuthConfigKeys.CONFIG_VALIDATION_ENABLED, "false");
configuration.update(OAuthConfigKeys.CONFIG_VALIDATION_ENABLED, "false");
}

@Test
Expand All @@ -78,7 +78,7 @@ void shouldOverwriteInternalUrls() {
dispatcher.configure(configuration, mockWebServer);
var url1 = new URLGenerator().next().toString();
var url2 = new URLGenerator().next().toString();
configuration.fireEvent(
configuration.update(
OAuthConfigKeys.OPEN_ID_SERVER_TOKEN_URL, url1,
OAuthConfigKeys.OPEN_ID_SERVER_USER_INFO_URL, url2);

Expand All @@ -90,7 +90,7 @@ void shouldOverwriteInternalUrls() {

@Test
void invalidOpenIdConfig() {
configuration.put(OAuthConfigKeys.CONFIG_VALIDATION_ENABLED, "true");
configuration.update(OAuthConfigKeys.CONFIG_VALIDATION_ENABLED, "true");
dispatcher.setSimulateInvalidOidcConfig(true);
dispatcher.configure(configuration, mockWebServer);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,7 @@ void testDeprecatedCreateAuthenticatedUserInfo() throws InterruptedException {
@Test
void testCreateAuthenticatedUserInfo() throws InterruptedException {

configuration.put(OAuthConfigKeys.OPEN_ID_ROLE_MAPPER_CLAIM, "ehealth-suite-roles");
configuration.fireEvent();
configuration.update(OAuthConfigKeys.OPEN_ID_ROLE_MAPPER_CLAIM, "ehealth-suite-roles");

var code = new BigInteger(260, new Random()).toString(32);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ void testServiceNotAvailable() {

@Test
void testServiceAvailable() {
configuration.fireEvent("abc.url", mockWebServer.url("success").toString());
configuration.update("abc.url", mockWebServer.url("success").toString());
assertNotNull(underTestProvider.get());
assertTrue(underTestProvider.get().isServiceAvailable());
assertEquals("test", underTestProvider.get().get().test());
Expand Down
12 changes: 5 additions & 7 deletions modules/test/portal-core-unit-testing/README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class ConfigurationEnabledTest {
@​Test
void shouldAdjustConfiguration() {
configuration.fireEvent("someKey", "someValue");
configuration.update("someKey", "someValue");
}
----

Expand Down Expand Up @@ -262,7 +262,7 @@ Dispatcher for a jwks endpoint
[source,java]
----
/**
* Handles the Resolving of JWKS Files from the Mocked oauth-Server. In essence it returns the file
* Handles the Resolving of JWKS Files from the Mocked oauth-Server. In essence, it returns the file
* "src/test/resources/token/test-public-key.jwks"
*/
public class JwksResolveDispatcher implements ModuleDispatcherElement {
Expand Down Expand Up @@ -323,10 +323,8 @@ class TokenParserProducerTest implements ShouldBeNotNull<TokenParserProducer>, M
mockserverPort = mockWebServer.getPort();
configuration.put(VERIFY_SIGNATURE_JWKS_URL,
"http://localhost:" + mockserverPort + jwksResolveDispatcher.getBaseUrl());
configuration.put(VERIFY_SIGNATURE_ISSUER, TestTokenProducer.ISSUER);
configuration.put(VERIFY_SIGNATURE_REFRESH_INTERVAL, "60");
configuration.fireEvent();
configuration.update(VERIFY_SIGNATURE_ISSUER, TestTokenProducer.ISSUER);
configuration.update(VERIFY_SIGNATURE_REFRESH_INTERVAL, "60");
jwksResolveDispatcher.setCallCounter(0);
}
Expand All @@ -340,7 +338,7 @@ class TokenParserProducerTest implements ShouldBeNotNull<TokenParserProducer>, M
JsonWebToken jsonWebToken = assertDoesNotThrow(() -> ParsedToken.jsonWebTokenFrom(token, parser, LOGGER));
assertValidJsonWebToken(jsonWebToken, token);
}
// For some reason there are always at least 2 calls, instead of expected one call. No
// For some reason, there are always at least 2 calls, instead of expected one call. No
// problem because as shown within this test, the number stays at 2
assertTrue(jwksResolveDispatcher.getCallCounter() < 3);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public void beforeEach(ExtensionContext context) {
var splitted = Splitter.on(':').splitToList(element);
checkArgument(2 <= splitted.size(), "Expected element in the form key:value, but was " + element);
log.debug(() -> "Adding configuration entry: " + element);
configuration.put(splitted.get(0), element.substring(element.indexOf(':') + 1));
configuration.update(splitted.get(0), element.substring(element.indexOf(':') + 1));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
import java.util.Optional;
import java.util.Set;

import static de.cuioss.tools.base.Preconditions.checkArgument;
import static de.cuioss.tools.collect.CollectionLiterals.immutableMap;
import static java.util.Collections.synchronizedMap;

Expand All @@ -58,18 +57,17 @@
*
* <pre>
* <code>
* configuration.put("key1", "value1");
* configuration.put("key2", "value2");
* configuration.fireEvent();
* configuration.update("key1", "value1");
* configuration.update("key2", "value2");
* </code>
* </pre>
* <p>
* or like
*
* <pre>
* <code>
* configuration.fireEvent("key1", "value1");
* configuration.fireEvent("key1", "value1", "key2", "value2");
* configuration.update("key1", "value1");
* configuration.update("key1", "value1", "key2", "value2");
* </code>
* </pre>
* <p>
Expand All @@ -91,71 +89,35 @@ public class PortalTestConfiguration implements ConfigSource {

private static final Map<String, String> properties = synchronizedMap(new HashMap<>());

/**
* Contains the delta config since the last fireEvent
*/
private static final Map<String, String> delta = synchronizedMap(new HashMap<>());

/**
* Fires the event
*/
public void fireEvent() {
if (!delta.isEmpty()) {
delta.clear();
}
}

/**
* Configures the given map
*/
public void fireEvent(final Map<String, String> deltaMap) {
deltaMap.forEach(this::put);
fireEvent();
public void update(final Map<String, String> deltaMap) {
properties.putAll(deltaMap);
}

/**
* Shorthand for calling {@link #fireEvent(Map)} without the need for creating a
* Shorthand for calling {@link #update(Map)} without the need for creating a
* map
*
* @param key of the entry
* @param value of the entry
*/
public void fireEvent(final String key, final String value) {
fireEvent(immutableMap(key, value));
public void update(final String key, final String value) {
update(immutableMap(key, value));
}

/**
* Shorthand for calling {@link #fireEvent(Map)} without the need for creating a
* Shorthand for calling {@link #update(Map)} without the need for creating a
* map
*
* @param key1 of the entry1
* @param value1 of the entry1
* @param key2 of the entry2
* @param value2 of the entry2
*/
public void fireEvent(final String key1, final String value1, final String key2, final String value2) {
fireEvent(immutableMap(key1, value1, key2, value2));
}

/**
* Adds a key / value pair
*
* @param key
* @param value
*/
public void put(final String key, final String value) {
properties.put(key, value);
delta.put(key, value);
}

/**
* Similar to {@link #fireEvent(Map)} but without firing an event.
*
* @param map to be added to this config source
*/
public void putAll(final Map<String, String> map) {
checkArgument(null != map, "map must not be null");
map.forEach(this::put);
public void update(final String key1, final String value1, final String key2, final String value2) {
update(immutableMap(key1, value1, key2, value2));
}

/**
Expand All @@ -165,38 +127,32 @@ public void putAll(final Map<String, String> map) {
* @see #removeAll()
*/
public void clear() {
delta.clear();
properties.clear();
System.clearProperty(PortalConfigurationKeys.PORTAL_CONFIG_DIR);
}

/**
* If the key exists in the local storage: Marks it as removed in the delta map
* and removes it from the local storage.
*
* @param key
* @param key to be removed
*/
public void remove(final String key) {
if (properties.containsKey(key)) {
delta.put(key, "");
properties.remove(key);
}
properties.remove(key);
}

/**
* Marks all current properties as removed in the delta map. Removes all
* properties from the local storage.
*/
public void removeAll() {
properties.forEach((k, v) -> delta.put(k, ""));
properties.clear();
}

/**
* @param projectStage
* @param projectStage to be set
*/
public void setPortalProjectStage(final de.cuioss.portal.common.stage.ProjectStage projectStage) {
fireEvent(PortalConfigurationKeys.PORTAL_STAGE, projectStage.name().toLowerCase());
update(PortalConfigurationKeys.PORTAL_STAGE, projectStage.name().toLowerCase());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ void shouldHandleMicroProfile() {
assertNotNull(attributeMpOptional);
assertTrue(attributeMpOptional.get().isPresent());

configuration.fireEvent(PORTAL_CUSTOMIZATION_ENABLED, "false");
configuration.update(PORTAL_CUSTOMIZATION_ENABLED, "false");
assertTrue(attributeMpProvider.get());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,10 @@ void shouldAddAndRemoveConfig() {
assertNull(underTest.getValue(key));
assertConfigNotPresent(key);

underTest.put(key, value);
underTest.fireEvent();
underTest.update(key, value);
assertConfigPresent(key, value);

underTest.remove(key);
underTest.fireEvent();
assertConfigNotPresent(key);
}

Expand All @@ -71,11 +69,10 @@ void shouldHandleFireEventDirectly() {
assertNull(underTest.getValue(key));
assertConfigNotPresent(key);

underTest.fireEvent(key, value);
underTest.update(key, value);
assertConfigPresent(key, value);

underTest.removeAll();
underTest.fireEvent();
assertConfigNotPresent(key);
underTest.removeAll();
}
Expand All @@ -90,12 +87,11 @@ void shouldHandleFireMultipleEventDirectly() {
assertNull(underTest.getValue(key));
assertConfigNotPresent(key);

underTest.fireEvent(key, value, key2, value2);
underTest.update(key, value, key2, value2);
assertConfigPresent(key, value);
assertConfigPresent(key2, value2);

underTest.removeAll();
underTest.fireEvent();
assertConfigNotPresent(key);
assertConfigNotPresent(key2);
}
Expand All @@ -110,21 +106,17 @@ void shouldHandleFireMultiplePut() {
assertNull(underTest.getValue(key));
assertConfigNotPresent(key);

underTest.putAll(immutableMap(key, value, key2, value2));
underTest.fireEvent();
underTest.update(immutableMap(key, value, key2, value2));
assertConfigPresent(key, value);
assertConfigPresent(key2, value2);

underTest.removeAll();
underTest.fireEvent();
assertConfigNotPresent(key);
assertConfigNotPresent(key2);

underTest.fireEvent(immutableMap(key, value, key2, value2));
underTest.update(immutableMap(key, value, key2, value2));
assertConfigPresent(key, value);
assertConfigPresent(key2, value2);

assertThrows(IllegalArgumentException.class, () -> underTest.putAll(null));
}

void assertConfigPresent(String key, String value) {
Expand Down

0 comments on commit 693442c

Please sign in to comment.