Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add more projects to parent pom #218

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/maven.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ jobs:
strategy:
fail-fast: false
matrix:
versions: [8, 11, 17]
versions: [8, 11]
runs-on: ubuntu-latest
name: Build Java ${{ matrix.versions }} projects
steps:
Expand Down
13 changes: 0 additions & 13 deletions buildGradleJdk8Projects.sh

This file was deleted.

17 changes: 0 additions & 17 deletions buildJdk17Projects.sh

This file was deleted.

20 changes: 7 additions & 13 deletions launchdarkly-java-development-testing-hints/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,17 @@
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>de.rieckpil.blog</groupId>
<parent>
<groupId>de.rieckpil.blog</groupId>
<artifactId>blog-parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

<artifactId>launchdarkly-java-development-testing-hints</artifactId>
<version>1.0-SNAPSHOT</version>

<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<testcontainers.version>1.17.1</testcontainers.version>
<junit-jupiter.version>5.7.2</junit-jupiter.version>
<mockito.version>4.5.1</mockito.version>
<awaitility.version>4.2.0</awaitility.version>
Expand All @@ -29,13 +30,6 @@
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>testcontainers-bom</artifactId>
<version>${testcontainers.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@
public interface FeatureFlagClient {
String getCurrentValue(String featureFlagKey, String username);

void registerChangeListener(String featureFlagKey, String username, FeatureFlagValueChangeHandler changeHandler);
void registerChangeListener(
String featureFlagKey, String username, FeatureFlagValueChangeHandler changeHandler);
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
package de.rieckpil.blog;

import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

import com.launchdarkly.sdk.server.Components;
import com.launchdarkly.sdk.server.LDClient;
import com.launchdarkly.sdk.server.LDConfig;
import com.launchdarkly.sdk.server.integrations.FileData;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

public class FeatureFlagClientFactory {

Expand All @@ -16,26 +15,30 @@ public static FeatureFlagClient buildOnlineClient(String accessKey) {
return new LaunchDarklyFeatureFlagClient(onlineClient);
}

public static FeatureFlagClient buildOfflineFileBasedClient(String inMemoryFileLocation) {
Path localFeatureFlagStateFilePath = Paths.get(inMemoryFileLocation);

if (Files.exists(localFeatureFlagStateFilePath)) {
LDClient fileBaseClient = new LDClient(
"invalid-ignored-access-key",
new LDConfig.Builder()
.dataSource(
FileData.dataSource().filePaths(localFeatureFlagStateFilePath).autoUpdate(true))
.events(Components.noEvents())
.build());

return new LaunchDarklyFeatureFlagClient(fileBaseClient);
} else {
return buildOfflineClient();
public static FeatureFlagClient buildOfflineFileBasedClient(String inMemoryFileLocation) {
Path localFeatureFlagStateFilePath = Paths.get(inMemoryFileLocation);

if (Files.exists(localFeatureFlagStateFilePath)) {
LDClient fileBaseClient =
new LDClient(
"invalid-ignored-access-key",
new LDConfig.Builder()
.dataSource(
FileData.dataSource()
.filePaths(localFeatureFlagStateFilePath)
.autoUpdate(true))
.events(Components.noEvents())
.build());

return new LaunchDarklyFeatureFlagClient(fileBaseClient);
} else {
return buildOfflineClient();
}
}
}

public static FeatureFlagClient buildOfflineClient() {
LDClient offlineClient = new LDClient("ignored-access-key", new LDConfig.Builder().offline(true).build());
return new LaunchDarklyFeatureFlagClient(offlineClient);
}
public static FeatureFlagClient buildOfflineClient() {
LDClient offlineClient =
new LDClient("ignored-access-key", new LDConfig.Builder().offline(true).build());
return new LaunchDarklyFeatureFlagClient(offlineClient);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,21 @@ public LaunchDarklyFeatureFlagClient(LDClient ldClient) {

@Override
public String getCurrentValue(String featureFlagKey, String username) {
return ldClient.stringVariation(featureFlagKey, new LDUser.Builder(username).build(), "unknown");
return ldClient.stringVariation(
featureFlagKey, new LDUser.Builder(username).build(), "unknown");
}

@Override
public void registerChangeListener(String featureFlagKey, String username, FeatureFlagValueChangeHandler changeHandler) {
public void registerChangeListener(
String featureFlagKey, String username, FeatureFlagValueChangeHandler changeHandler) {
ldClient
.getFlagTracker()
.addFlagValueChangeListener(featureFlagKey, new LDUser.Builder(username).build(), (changeEvent) -> changeHandler.handle(changeEvent.getOldValue().stringValue(), changeEvent.getNewValue().stringValue()));
.getFlagTracker()
.addFlagValueChangeListener(
featureFlagKey,
new LDUser.Builder(username).build(),
(changeEvent) ->
changeHandler.handle(
changeEvent.getOldValue().stringValue(),
changeEvent.getNewValue().stringValue()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ public OrderService(FeatureFlagClient featureFlagClient) {
}

public void processOrder(String orderId) {
if("plane".equals(featureFlagClient.getCurrentValue("primary-shipment-method", "duke"))) {
if ("plane".equals(featureFlagClient.getCurrentValue("primary-shipment-method", "duke"))) {
// distribute via plane
}else {
} else {
// different implementation
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,12 @@ public void registerListener() {
LOG.info("Going to register root log level updater");

featureFlagClient.registerChangeListener(
"root-log-level",
"duke",
(oldValue, newValue) -> {
"root-log-level",
"duke",
(oldValue, newValue) -> {
LOG.info("Going to change the root log level from '{}' to '{}'", oldValue, newValue);

LOG.info("Going to change the root log level from '{}' to '{}'", oldValue, newValue);

Configurator.setRootLevel(Level.valueOf(newValue));
});
Configurator.setRootLevel(Level.valueOf(newValue));
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;

import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -26,8 +25,8 @@ public void initializeEmptyState() {
try {
JSONObject featureFlagState = new JSONObject().put("flagValues", new JSONObject());
Files.write(
featureFlagStateFile.toPath(),
featureFlagState.toString().getBytes(StandardCharsets.UTF_8));
featureFlagStateFile.toPath(),
featureFlagState.toString().getBytes(StandardCharsets.UTF_8));
} catch (Exception exception) {
LOG.error("Failed to initialize empty in-memory feature flag state", exception);
}
Expand All @@ -36,12 +35,12 @@ public void initializeEmptyState() {
public void updateFeature(String featureFlagKey, Object featureFlagValue) {
try {
JSONObject featureFlagState =
new JSONObject()
.put("flagValues", new JSONObject().put(featureFlagKey, featureFlagValue));
new JSONObject()
.put("flagValues", new JSONObject().put(featureFlagKey, featureFlagValue));

Files.write(
featureFlagStateFile.toPath(),
featureFlagState.toString().getBytes(StandardCharsets.UTF_8));
featureFlagStateFile.toPath(),
featureFlagState.toString().getBytes(StandardCharsets.UTF_8));
} catch (Exception exception) {
LOG.error("Failed to update in-memory feature flag state", exception);
}
Expand All @@ -51,4 +50,3 @@ public Path getPath() {
return featureFlagStateFile.toPath();
}
}

Original file line number Diff line number Diff line change
@@ -1,32 +1,25 @@
package de.rieckpil.blog;

import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.ArgumentMatchers.*;
import static org.mockito.Mockito.when;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;

import static org.junit.jupiter.api.Assertions.*;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.ArgumentMatchers.*;
import static org.mockito.Mockito.when;
import static org.mockito.Mockito.verify;

@ExtendWith(MockitoExtension.class)
class OrderServiceTest {

@Mock
private FeatureFlagClient featureFlagClient;
@Mock private FeatureFlagClient featureFlagClient;

@InjectMocks
private OrderService orderService;
@InjectMocks private OrderService orderService;

@Test
void shouldDeliverViaPlaneWhenConfigured() {
when(featureFlagClient.getCurrentValue(anyString(), anyString()))
.thenReturn("plane");
when(featureFlagClient.getCurrentValue(anyString(), anyString())).thenReturn("plane");

orderService.processOrder("42");

Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
package de.rieckpil.blog;

import java.util.concurrent.TimeUnit;
import static org.awaitility.Awaitility.await;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals;

import java.util.concurrent.TimeUnit;
import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.LogManager;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import static org.awaitility.Awaitility.await;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals;

class RootLogLevelUpdaterTest {

private TestDataFeatureFlagClient testDataFeatureFlagClient;
Expand All @@ -31,7 +30,7 @@ void shouldUpdateRootLogLevel() {
testDataFeatureFlagClient.updateFeatureFlag("root-log-level", "TRACE");

await()
.atMost(2, TimeUnit.SECONDS)
.untilAsserted(() -> assertEquals(Level.TRACE, LogManager.getRootLogger().getLevel()));
.atMost(2, TimeUnit.SECONDS)
.untilAsserted(() -> assertEquals(Level.TRACE, LogManager.getRootLogger().getLevel()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,30 @@ public class TestDataFeatureFlagClient implements FeatureFlagClient {

public TestDataFeatureFlagClient() {
this.testData = TestData.dataSource();
this.ldClient = new LDClient(
"ignored-access-key",
new LDConfig.Builder().dataSource(testData).events(Components.noEvents()).build());
this.ldClient =
new LDClient(
"ignored-access-key",
new LDConfig.Builder().dataSource(testData).events(Components.noEvents()).build());
}

@Override
public String getCurrentValue(String featureFlagKey, String username) {
return ldClient.stringVariation(featureFlagKey, new LDUser.Builder(username).build(), "unknown");
return ldClient.stringVariation(
featureFlagKey, new LDUser.Builder(username).build(), "unknown");
}

@Override
public void registerChangeListener(String featureFlagKey, String username, FeatureFlagValueChangeHandler changeHandler) {
public void registerChangeListener(
String featureFlagKey, String username, FeatureFlagValueChangeHandler changeHandler) {
ldClient
.getFlagTracker()
.addFlagValueChangeListener(featureFlagKey, new LDUser.Builder(username).build(), (changeEvent) -> changeHandler.handle(changeEvent.getOldValue().stringValue(), changeEvent.getNewValue().stringValue()));
.getFlagTracker()
.addFlagValueChangeListener(
featureFlagKey,
new LDUser.Builder(username).build(),
(changeEvent) ->
changeHandler.handle(
changeEvent.getOldValue().stringValue(),
changeEvent.getNewValue().stringValue()));
}

public void updateFeatureFlag(String featureFlag, String newValue) {
Expand Down
Loading
Loading