Skip to content

Commit

Permalink
feat: add/removeActiveProfile to/from NeonBeeOptions.Mutable
Browse files Browse the repository at this point in the history
  • Loading branch information
kristian committed Oct 4, 2021
1 parent cbd53b5 commit 6977163
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 30 deletions.
5 changes: 3 additions & 2 deletions src/main/java/io/neonbee/NeonBee.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import java.io.IOException;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -366,7 +367,7 @@ private void registerCodec(String className, String codecClassName) {
* @return a composite future about the result of the deployment
*/
private Future<Void> deployVerticles() {
List<NeonBeeProfile> activeProfiles = options.getActiveProfiles();
Set<NeonBeeProfile> activeProfiles = options.getActiveProfiles();
if (logger.isInfoEnabled()) {
logger.info("Deploying verticle with active profiles: {}",
activeProfiles.stream().map(NeonBeeProfile::name).collect(Collectors.joining(",")));
Expand Down Expand Up @@ -465,7 +466,7 @@ private Future<Void> deployClassPathVerticles() {

@VisibleForTesting
static boolean filterByAutoDeployAndProfiles(Class<? extends Verticle> verticleClass,
List<NeonBeeProfile> activeProfiles) {
Collection<NeonBeeProfile> activeProfiles) {
NeonBeeDeployable annotation = verticleClass.getAnnotation(NeonBeeDeployable.class);
return annotation.autoDeploy() && annotation.profile().isActive(activeProfiles);
}
Expand Down
59 changes: 47 additions & 12 deletions src/main/java/io/neonbee/NeonBeeOptions.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
package io.neonbee;

import static io.neonbee.NeonBeeProfile.ALL;
import static io.neonbee.internal.helper.StringHelper.EMPTY;
import static java.util.Objects.requireNonNull;

import java.nio.file.Path;
import java.util.List;
import java.util.Collection;
import java.util.Objects;
import java.util.Set;
import java.util.UUID;

import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Sets;
import com.hazelcast.config.ClasspathXmlConfig;
import com.hazelcast.config.Config;

Expand Down Expand Up @@ -140,7 +144,7 @@ default Path getLogDirectory() {
*
* @return the currently active profiles.
*/
List<NeonBeeProfile> getActiveProfiles();
Set<NeonBeeProfile> getActiveProfiles();

/**
* Create a mutable NeonBeeOptions similar to VertxOptions, but as NeonBeeOptions are exposed only the interface
Expand Down Expand Up @@ -175,7 +179,7 @@ class Mutable implements NeonBeeOptions {

private Integer serverPort;

private List<NeonBeeProfile> activeProfiles = List.of(NeonBeeProfile.ALL);
private Set<NeonBeeProfile> activeProfiles = Set.of(ALL);

/**
* Instantiates a mutable {@link NeonBeeOptions} instance.
Expand Down Expand Up @@ -392,32 +396,63 @@ public Integer getServerPort() {
}

@Override
public List<NeonBeeProfile> getActiveProfiles() {
public Set<NeonBeeProfile> getActiveProfiles() {
return this.activeProfiles;
}

/**
* Set active profiles.
* Set the active profiles.
*
* @param activeProfiles the active profiles
* @param profiles the active profiles
* @return this instance for chaining
*/
public Mutable setActiveProfiles(List<NeonBeeProfile> activeProfiles) {
this.activeProfiles = activeProfiles;
public Mutable setActiveProfiles(Collection<NeonBeeProfile> profiles) {
this.activeProfiles = ImmutableSet.copyOf(requireNonNull(profiles));
return this;
}

/**
* Set the active profile values.
* Add an active profile.
*
* @param profile the active profile to add
* @return this instance for chaining
*/
public Mutable addActiveProfile(NeonBeeProfile profile) {
this.activeProfiles = Sets.union(this.activeProfiles, Set.of(profile)).immutableCopy();
return this;
}

/**
* Remove an active profile.
*
* @param profile the active profile to remove
* @return this instance for chaining
*/
public Mutable removeActiveProfile(NeonBeeProfile profile) {
this.activeProfiles = Sets.difference(this.activeProfiles, Set.of(profile)).immutableCopy();
return this;
}

/**
* Remove all active profiles. Equivalent of setting an empty set.
*
* @param profileValues the profile values
* @return this instance for chaining
*/
public Mutable setActiveProfileValues(String profileValues) {
this.activeProfiles = NeonBeeProfile.parseProfiles(profileValues);
public Mutable clearActiveProfiles() {
this.activeProfiles = Set.of();
return this;
}

/**
* Set the active profile values.
*
* @param values the profile values
* @return this instance for chaining
*/
public Mutable setActiveProfileValues(String values) {
return this.setActiveProfiles(NeonBeeProfile.parseProfiles(values));
}

private String generateName() {
return String.format("%s-%s", NeonBee.class.getSimpleName(), UUID.randomUUID().toString());
}
Expand Down
12 changes: 6 additions & 6 deletions src/main/java/io/neonbee/NeonBeeProfile.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.function.Predicate;
import java.util.stream.Collectors;

Expand All @@ -23,8 +23,8 @@ public enum NeonBeeProfile {
* @param activeProfiles The active profiles
* @return true if this profile is active
*/
public boolean isActive(List<NeonBeeProfile> activeProfiles) {
if (activeProfiles.contains(NO_WEB) && this == WEB) {
public boolean isActive(Collection<NeonBeeProfile> activeProfiles) {
if (activeProfiles.isEmpty() || (activeProfiles.contains(NO_WEB) && this == WEB)) {
return false;
}

Expand All @@ -37,15 +37,15 @@ public boolean isActive(List<NeonBeeProfile> activeProfiles) {
* @param values string with profile values
* @return a List with the parsed {@link NeonBeeProfile}s
*/
public static List<NeonBeeProfile> parseProfiles(String values) {
public static Set<NeonBeeProfile> parseProfiles(String values) {
return Optional.ofNullable(values).map(Strings::emptyToNull)
.map(nonEmptyValues -> Arrays.stream(nonEmptyValues.split(",")).map(value -> {
try {
return NeonBeeProfile.valueOf(value);
} catch (Exception e) {
return null;
}
}).filter(Objects::nonNull).collect(Collectors.toList())).filter(Predicate.not(Collection::isEmpty))
.orElse(List.of(ALL));
}).filter(Objects::nonNull).collect(Collectors.toSet())).filter(Predicate.not(Collection::isEmpty))
.orElse(Set.of(ALL));
}
}
14 changes: 14 additions & 0 deletions src/test/java/io/neonbee/NeonBeeOptionsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import java.io.IOException;
import java.nio.file.Path;
import java.util.List;
import java.util.Set;

import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -118,6 +119,19 @@ void checkProfiles() {
assertThat(opts.getActiveProfiles()).containsExactly(CORE, WEB);
opts = new NeonBeeOptions.Mutable().setActiveProfileValues("anything");
assertThat(opts.getActiveProfiles()).containsExactly(ALL);

opts = new NeonBeeOptions.Mutable().setActiveProfiles(List.of(CORE, WEB, WEB, CORE));
assertThat(opts.getActiveProfiles()).containsExactly(CORE, WEB);
Set<NeonBeeProfile> profiles = opts.getActiveProfiles();
assertThrows(UnsupportedOperationException.class, () -> profiles.add(WEB));

opts.addActiveProfile(ALL).addActiveProfile(ALL);
assertThat(opts.getActiveProfiles()).containsExactly(CORE, WEB, ALL);
opts.removeActiveProfile(CORE).removeActiveProfile(CORE).removeActiveProfile(WEB);
assertThat(opts.getActiveProfiles()).containsExactly(ALL);

opts.clearActiveProfiles();
assertThat(opts.getActiveProfiles()).isEmpty();
}

@Test
Expand Down
7 changes: 6 additions & 1 deletion src/test/java/io/neonbee/NeonBeeProfileTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import static io.neonbee.NeonBeeProfile.parseProfiles;

import java.util.List;
import java.util.Set;

import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -37,11 +38,15 @@ void isActive() {
assertThat(CORE.isActive(List.<NeonBeeProfile>of(STABLE, INCUBATOR))).isFalse();

assertThat(ALL.isActive(List.<NeonBeeProfile>of(STABLE))).isTrue();

assertThat(ALL.isActive(Set.of())).isFalse();
assertThat(CORE.isActive(Set.of())).isFalse();
assertThat(NO_WEB.isActive(Set.of())).isFalse();
}

@Test
void parseActiveProfile() {
List<NeonBeeProfile> profiles = parseProfiles("");
Set<NeonBeeProfile> profiles = parseProfiles("");
assertThat(profiles).contains(ALL);
assertThat(profiles).hasSize(1);
profiles = parseProfiles("CORE");
Expand Down
4 changes: 1 addition & 3 deletions src/test/java/io/neonbee/job/JobVerticleTest.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package io.neonbee.job;

import static com.google.common.truth.Truth.assertThat;
import static io.neonbee.NeonBeeProfile.ALL;
import static io.neonbee.NeonBeeProfile.NO_WEB;
import static io.neonbee.test.helper.OptionsHelper.defaultOptions;
import static org.mockito.ArgumentMatchers.any;
Expand All @@ -14,7 +13,6 @@
import java.time.Duration;
import java.time.Instant;
import java.time.temporal.ChronoUnit;
import java.util.List;
import java.util.concurrent.TimeUnit;

import org.junit.jupiter.api.DisplayName;
Expand Down Expand Up @@ -100,7 +98,7 @@ public String deploymentID() {

@Override
protected void adaptOptions(TestInfo testInfo, NeonBeeOptions.Mutable options) {
options.setActiveProfiles(List.of(ALL, NO_WEB));
options.addActiveProfile(NO_WEB);
options.setDisableJobScheduling(false);
}

Expand Down
10 changes: 4 additions & 6 deletions src/test/java/io/neonbee/test/base/NeonBeeTestBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.common.collect.ImmutableList;
import com.google.common.io.Resources;

import io.neonbee.NeonBee;
Expand Down Expand Up @@ -119,8 +118,7 @@ public void setUp(Vertx vertx, VertxTestContext testContext, TestInfo testInfo)

// add the NO_WEB profile to the active profiles list, this way we won't have to undeploy the ServerVerticle
// again later on and can deploy our dummy ServerVerticle right away
options.setActiveProfiles(new ImmutableList.Builder<NeonBeeProfile>().add(NO_WEB)
.addAll(options.getActiveProfiles()).build());
options.addActiveProfile(NO_WEB);
customUserPrincipal.set(true);
}

Expand Down Expand Up @@ -204,9 +202,9 @@ void tearDown(Vertx vertx, VertxTestContext testContext) throws IOException {
* scanning, watching files and job scheduling. It does however apply all profiles by default. Some examples how to
* use this method:
*
* - In case your test is not requiring any HTTP connectivity via the {@link ServerVerticle}, set the
* {@link NeonBeeProfile#NO_WEB} profile: {@code options.setActiveProfiles(List.of(ALL, NO_WEB));} to improve
* performance of your tests.
* - In case your test is not requiring any HTTP connectivity via the {@link ServerVerticle}, add the
* {@link NeonBeeProfile#NO_WEB} profile to improve performance of your tests:
* {@code options.addActiveProfile(NO_WEB);}.
*
* - In case your test is requiring job scheduling / you want to test {@link JobVerticle}, use
* {@link NeonBeeOptions.Mutable#setDisableJobScheduling(boolean)} and change the default to {@code true} instead.
Expand Down

0 comments on commit 6977163

Please sign in to comment.