Skip to content

Commit

Permalink
feat: add missing tests for NeonBeeConfig
Browse files Browse the repository at this point in the history
  • Loading branch information
pk-work committed May 6, 2021
1 parent 9499d2f commit 40b66db
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 5 deletions.
72 changes: 68 additions & 4 deletions src/test/java/io/neonbee/config/NeonBeeConfigTest.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,62 @@
package io.neonbee.config;

import static com.google.common.truth.Truth.assertThat;
import static io.neonbee.config.NeonBeeConfig.DEFAULT_EVENT_BUS_TIMEOUT;
import static io.neonbee.config.NeonBeeConfig.DEFAULT_TIME_ZONE;
import static io.neonbee.config.NeonBeeConfig.DEFAULT_TRACKING_DATA_HANDLING_STRATEGY;

import java.lang.reflect.Method;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.function.Function;

import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInfo;

import io.neonbee.test.base.NeonBeeTestBase;
import io.neonbee.test.helper.WorkingDirectoryBuilder;
import io.vertx.core.Vertx;
import io.vertx.core.json.JsonArray;
import io.vertx.core.json.JsonObject;
import io.vertx.junit5.Timeout;
import io.vertx.junit5.VertxTestContext;

class NeonBeeConfigTest {
class NeonBeeConfigTest extends NeonBeeTestBase {
private static final int DUMMY_EVENT_BUS_TIMEOUT = 1337;

private static final String DUMMY_TRACKING_DATA_HANDLING_STRATEGY = "Hodor";

private static final String DUMMY_TIME_ZONE = "Hammer Time";

private static final Map<String, String> DUMMY_EVENT_BUS_CODECS = Map.of("Random", "Codec");

private static final List<String> DUMMY_PLATFORM_CLASSES = List.of("Hodor");

private static final NeonBeeConfig DUMMY_NEONBEE_CONFIG =
new NeonBeeConfig().setEventBusTimeout(DUMMY_EVENT_BUS_TIMEOUT)
.setTrackingDataHandlingStrategy(DUMMY_TRACKING_DATA_HANDLING_STRATEGY).setTimeZone(DUMMY_TIME_ZONE)
.setEventBusCodecs(DUMMY_EVENT_BUS_CODECS).setPlatformClasses(DUMMY_PLATFORM_CLASSES);

@Override
protected WorkingDirectoryBuilder provideWorkingDirectoryBuilder(TestInfo testInfo, VertxTestContext testContext) {
if ("testLoad".equals(testInfo.getTestMethod().map(Method::getName).orElse(null))) {
return WorkingDirectoryBuilder.standard().setNeonBeeConfig(DUMMY_NEONBEE_CONFIG);
} else {
return super.provideWorkingDirectoryBuilder(testInfo, testContext);
}
}

@Test
@Timeout(value = 2, timeUnit = TimeUnit.SECONDS)
@DisplayName("should load NeonBeeConfig correctly from working dir")
void testLoad(Vertx vertx, VertxTestContext testContext) {
NeonBeeConfig.load(vertx).onComplete(testContext.succeeding(nbc -> {
testContext.verify(() -> isEqualToDummyConfig(nbc));
testContext.completeNow();
}));
}

@Test
@DisplayName("should read the trackingDataHandlingStrategy correctly")
Expand All @@ -37,8 +82,27 @@ void testGetPlatformClasses() {
}

@Test
@DisplayName("should have the correct default timezone")
void testDefaultTimeZone() {
assertThat(new NeonBeeConfig().getTimeZone()).isEqualTo(NeonBeeConfig.DEFAULT_TIME_ZONE);
@DisplayName("should have the correct default values")
void testDefaultValues() {
NeonBeeConfig defaultConfig = new NeonBeeConfig();
assertThat(defaultConfig.getEventBusTimeout()).isEqualTo(DEFAULT_EVENT_BUS_TIMEOUT);
assertThat(defaultConfig.getTrackingDataHandlingStrategy()).isEqualTo(DEFAULT_TRACKING_DATA_HANDLING_STRATEGY);
assertThat(defaultConfig.getTimeZone()).isEqualTo(DEFAULT_TIME_ZONE);
assertThat(defaultConfig.getEventBusCodecs()).isEmpty();
assertThat(defaultConfig.getPlatformClasses()).isEmpty();
}

@Test
@DisplayName("setters should work as expected")
void testSetters() {
isEqualToDummyConfig(DUMMY_NEONBEE_CONFIG);
}

private void isEqualToDummyConfig(NeonBeeConfig nbc) {
assertThat(nbc.getEventBusTimeout()).isEqualTo(DUMMY_EVENT_BUS_TIMEOUT);
assertThat(nbc.getTrackingDataHandlingStrategy()).isEqualTo(DUMMY_TRACKING_DATA_HANDLING_STRATEGY);
assertThat(nbc.getTimeZone()).isEqualTo(DUMMY_TIME_ZONE);
assertThat(nbc.getEventBusCodecs()).isEqualTo(DUMMY_EVENT_BUS_CODECS);
assertThat(nbc.getPlatformClasses()).isEqualTo(DUMMY_PLATFORM_CLASSES);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@

import com.sap.cds.reflect.CdsModel;

import io.neonbee.NeonBee;
import io.neonbee.config.NeonBeeConfig;
import io.neonbee.entity.ModelDefinitionHelper;
import io.neonbee.internal.verticle.ServerVerticle;
import io.vertx.core.DeploymentOptions;
Expand Down Expand Up @@ -57,6 +59,8 @@ private enum DirType {

private Consumer<Path> customTask;

private NeonBeeConfig neonbeeConfig;

private WorkingDirectoryBuilder(DirType dirType, Path sourcePath) {
this.dirType = dirType;
this.sourcePath = sourcePath;
Expand Down Expand Up @@ -112,13 +116,24 @@ public WorkingDirectoryBuilder addModel(Path modelPath) {
* working directory.
*
* @param customTask {@link Path} to the model
* @return The WorkingDirectoryBuilder
* @return the WorkingDirectoryBuilder
*/
public WorkingDirectoryBuilder setCustomTask(Consumer<Path> customTask) {
this.customTask = customTask;
return this;
}

/**
* Adds the NeonBeeConfig to the working directory.
*
* @param neonbeeConfig the {@link NeonBeeConfig} to set.
* @return the WorkingDirectoryBuilder
*/
public WorkingDirectoryBuilder setNeonBeeConfig(NeonBeeConfig neonbeeConfig) {
this.neonbeeConfig = neonbeeConfig;
return this;
}

/**
* Builds the working directory of the related NeonBee instance, inside of the passed working directory root.
*
Expand Down Expand Up @@ -156,6 +171,11 @@ public void build(Path workingDirRoot) throws IOException {
copyModel(workingDirRoot.resolve(MODELS_DIR), modelToAdd);
}

if (neonbeeConfig != null) {
Path destination = workingDirRoot.resolve(CONFIG_DIR).resolve(NeonBee.class.getName() + ".json");
Files.writeString(destination, neonbeeConfig.toJson().encodePrettily());
}

Optional.ofNullable(customTask).ifPresent(ct -> ct.accept(workingDirRoot));
}

Expand Down

0 comments on commit 40b66db

Please sign in to comment.