Removing isPaid, allFeaturesEnabled, and isActive methods from enums.

Original commit: elastic/x-pack-elasticsearch@8b8c7792c7
This commit is contained in:
Chris Earle 2016-03-26 17:34:55 -04:00
parent 5e81beabf9
commit 55b9569f7b
15 changed files with 246 additions and 296 deletions

View File

@ -102,30 +102,6 @@ public class License implements ToXContent {
throw new IllegalArgumentException("unknown type [" + type + "]"); throw new IllegalArgumentException("unknown type [" + type + "]");
} }
} }
/**
* Determine if the operation mode should be treated like a paid license.
* <p>
* This should be used for licenses that do not tier features, but that require a paid license.
* <p>
* Note: This does not mean that the license state is enabled!
*
* @return {@code true} if the license is <em>not</em> {@link #BASIC}.
*/
public boolean isPaid() {
return this != BASIC;
}
/**
* Determine if the current operation mode unlocks all features.
* <p>
* Note: This does not mean that the license state is enabled!
*
* @return {@code true} if the license is either {@link #TRIAL} or {@link #PLATINUM}.
*/
public boolean allFeaturesEnabled() {
return this == TRIAL || this == PLATINUM;
}
} }
private License(int version, String uid, String issuer, String issuedTo, long issueDate, String type, private License(int version, String uid, String issuer, String issuedTo, long issueDate, String type,

View File

@ -16,29 +16,6 @@ import static org.hamcrest.Matchers.equalTo;
* If you change the behavior of these tests, then it means that licensing changes across the products! * If you change the behavior of these tests, then it means that licensing changes across the products!
*/ */
public class LicenseOperationModeTests extends ESTestCase { public class LicenseOperationModeTests extends ESTestCase {
public void testIsPaid() {
assertThat(OperationMode.BASIC.isPaid(), equalTo(false));
// EVERY other operation mode is expected to be equivalent to paid; loop will catch any new mode
for (OperationMode mode : OperationMode.values()) {
if (mode != OperationMode.BASIC) {
assertThat(mode.isPaid(), equalTo(true));
}
}
}
public void testAllFeaturesEnabled() {
assertThat(OperationMode.TRIAL.allFeaturesEnabled(), equalTo(true));
assertThat(OperationMode.PLATINUM.allFeaturesEnabled(), equalTo(true));
// EVERY other operation mode is expected to NOT enable everything; loop will catch any new mode
for (OperationMode mode : OperationMode.values()) {
if (mode != OperationMode.TRIAL && mode != OperationMode.PLATINUM) {
assertThat(mode.allFeaturesEnabled(), equalTo(false));
}
}
}
public void testResolveTrial() { public void testResolveTrial() {
// assert 1.x BWC // assert 1.x BWC
assertResolve(OperationMode.TRIAL, "nONE", "DEv", "deveLopment"); assertResolve(OperationMode.TRIAL, "nONE", "DEv", "deveLopment");
@ -53,14 +30,14 @@ public class LicenseOperationModeTests extends ESTestCase {
public void testResolveGold() { public void testResolveGold() {
// assert expected (2.x+) variant (note: no different 1.x variant of GOLD) // assert expected (2.x+) variant (note: no different 1.x variant of GOLD)
assertResolve(OperationMode.BASIC, "SiLvEr", "gOlD", "silver", "gold"); assertResolve(OperationMode.GOLD, "SiLvEr", "gOlD", "silver", "gold");
} }
public void testResolvePlatinum() { public void testResolvePlatinum() {
// assert 1.x BWC // assert 1.x BWC
assertResolve(OperationMode.TRIAL, "iNtErNaL"); assertResolve(OperationMode.PLATINUM, "iNtErNaL");
// assert expected (2.x+) variant // assert expected (2.x+) variant
assertResolve(OperationMode.TRIAL, "PlAtINum", "platinum"); assertResolve(OperationMode.PLATINUM, "PlAtINum", "platinum");
} }
public void testResolveUnknown() { public void testResolveUnknown() {

View File

@ -38,14 +38,5 @@ public enum LicenseState {
* changes to {@link #ENABLED}, otherwise * changes to {@link #ENABLED}, otherwise
* remains unchanged * remains unchanged
*/ */
DISABLED; DISABLED
/**
* Determine if the license should be treated as active.
*
* @return {@code true} if it is not {@link #DISABLED}.
*/
public boolean isActive() {
return this != DISABLED;
}
} }

View File

@ -67,10 +67,6 @@ public interface Licensee {
* <p> * <p>
* Note: Knowing the mode does not indicate whether the {@link #getLicenseState() state} is disabled. If that matters (e.g., * Note: Knowing the mode does not indicate whether the {@link #getLicenseState() state} is disabled. If that matters (e.g.,
* disabling services when a license becomes disabled), then you should check it as well! * disabling services when a license becomes disabled), then you should check it as well!
*
* @see OperationMode#allFeaturesEnabled()
* @see OperationMode#isPaid()
* @see LicenseState#isActive()
*/ */
public OperationMode getMode() { public OperationMode getMode() {
return mode; return mode;
@ -82,8 +78,6 @@ public interface Licensee {
* the state changes to {@link LicenseState#GRACE_PERIOD} * the state changes to {@link LicenseState#GRACE_PERIOD}
* and after the grace period has ended the state changes * and after the grace period has ended the state changes
* to {@link LicenseState#DISABLED} * to {@link LicenseState#DISABLED}
*
* @see LicenseState#isActive()
*/ */
public LicenseState getLicenseState() { public LicenseState getLicenseState() {
return licenseState; return licenseState;

View File

@ -1,28 +0,0 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/
package org.elasticsearch.license.plugin.core;
import org.elasticsearch.test.ESTestCase;
import static org.hamcrest.Matchers.equalTo;
/**
* Tests {@link LicenseState} correctness.
* <p>
* If you change the behavior of these tests, then it means that licensing changes across the products!
*/
public class LicenseStateTests extends ESTestCase {
public void testIsActive() {
assertThat(LicenseState.DISABLED.isActive(), equalTo(false));
// all other states are considered active; loop will catch any new state
for (LicenseState state : LicenseState.values()) {
if (state != LicenseState.DISABLED) {
assertThat(state.isActive(), equalTo(true));
}
}
}
}

View File

@ -9,7 +9,9 @@ import org.elasticsearch.common.Strings;
import org.elasticsearch.common.inject.Inject; import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.license.core.License; import org.elasticsearch.license.core.License;
import org.elasticsearch.license.core.License.OperationMode;
import org.elasticsearch.license.plugin.core.AbstractLicenseeComponent; import org.elasticsearch.license.plugin.core.AbstractLicenseeComponent;
import org.elasticsearch.license.plugin.core.LicenseState;
import org.elasticsearch.license.plugin.core.LicenseeRegistry; import org.elasticsearch.license.plugin.core.LicenseeRegistry;
import org.elasticsearch.graph.Graph; import org.elasticsearch.graph.Graph;
@ -31,17 +33,39 @@ public class GraphLicensee extends AbstractLicenseeComponent<GraphLicensee> {
@Override @Override
public String[] acknowledgmentMessages(License currentLicense, License newLicense) { public String[] acknowledgmentMessages(License currentLicense, License newLicense) {
if (newLicense.operationMode().allFeaturesEnabled() == false) { switch (newLicense.operationMode()) {
if (currentLicense != null && currentLicense.operationMode().allFeaturesEnabled()) { case BASIC:
case GOLD:
if (currentLicense != null) {
switch (currentLicense.operationMode()) {
case TRIAL:
case PLATINUM:
return new String[] { "Graph will be disabled" }; return new String[] { "Graph will be disabled" };
} }
} }
break;
}
return Strings.EMPTY_ARRAY; return Strings.EMPTY_ARRAY;
} }
/**
* Determine if Graph Exploration should be enabled.
* <p>
* Exploration is only disabled when the license has expired or if the mode is not:
* <ul>
* <li>{@link OperationMode#PLATINUM}</li>
* <li>{@link OperationMode#TRIAL}</li>
* </ul>
*
* @return {@code true} as long as the license is valid. Otherwise {@code false}.
*/
public boolean isGraphExploreEnabled() { public boolean isGraphExploreEnabled() {
// status is volatile // status is volatile
Status localStatus = status; Status localStatus = status;
return localStatus.getLicenseState().isActive() && localStatus.getMode().allFeaturesEnabled(); OperationMode operationMode = localStatus.getMode();
boolean licensed = operationMode == OperationMode.TRIAL || operationMode == OperationMode.PLATINUM;
return licensed && localStatus.getLicenseState() != LicenseState.DISABLED;
} }
} }

View File

@ -5,15 +5,9 @@
*/ */
package org.elasticsearch.graph.license; package org.elasticsearch.graph.license;
import org.elasticsearch.common.component.AbstractComponent;
import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.license.core.License; import org.elasticsearch.license.core.License.OperationMode;
import org.elasticsearch.license.plugin.core.AbstractLicenseeTestCase; import org.elasticsearch.license.plugin.core.AbstractLicenseeTestCase;
import org.elasticsearch.license.plugin.core.Licensee;
import org.elasticsearch.license.plugin.core.LicenseeRegistry;
import java.util.ArrayList;
import java.util.List;
import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.is;
@ -22,15 +16,15 @@ public class LicenseTests extends AbstractLicenseeTestCase {
private SimpleLicenseeRegistry licenseeRegistry = new SimpleLicenseeRegistry(); private SimpleLicenseeRegistry licenseeRegistry = new SimpleLicenseeRegistry();
public void testPlatinumTrialLicenseCanDoEverything() throws Exception { public void testPlatinumTrialLicenseCanDoEverything() throws Exception {
licenseeRegistry.setOperationMode(randomAllFeaturesMode()); licenseeRegistry.setOperationMode(randomTrialOrPlatinumMode());
GraphLicensee graphLicensee = new GraphLicensee(Settings.EMPTY, licenseeRegistry); GraphLicensee graphLicensee = new GraphLicensee(Settings.EMPTY, licenseeRegistry);
licenseeRegistry.register(graphLicensee); licenseeRegistry.register(graphLicensee);
assertLicensePlatinumTrialBehaviour(graphLicensee); assertLicensePlatinumTrialBehaviour(graphLicensee);
} }
public void testFreeLicenseIsDisabled() throws Exception { public void testBasicLicenseIsDisabled() throws Exception {
licenseeRegistry.setOperationMode(randomFreeMode()); licenseeRegistry.setOperationMode(OperationMode.BASIC);
GraphLicensee graphLicensee = new GraphLicensee(Settings.EMPTY, licenseeRegistry); GraphLicensee graphLicensee = new GraphLicensee(Settings.EMPTY, licenseeRegistry);
licenseeRegistry.register(graphLicensee); licenseeRegistry.register(graphLicensee);
@ -38,7 +32,7 @@ public class LicenseTests extends AbstractLicenseeTestCase {
} }
public void testNoLicenseDoesNotWork() { public void testNoLicenseDoesNotWork() {
licenseeRegistry.setOperationMode(randomFreeMode()); licenseeRegistry.setOperationMode(OperationMode.BASIC);
GraphLicensee graphLicensee = new GraphLicensee(Settings.EMPTY, licenseeRegistry); GraphLicensee graphLicensee = new GraphLicensee(Settings.EMPTY, licenseeRegistry);
licenseeRegistry.register(graphLicensee); licenseeRegistry.register(graphLicensee);
licenseeRegistry.disable(); licenseeRegistry.disable();
@ -47,7 +41,7 @@ public class LicenseTests extends AbstractLicenseeTestCase {
} }
public void testExpiredPlatinumTrialLicenseIsRestricted() throws Exception { public void testExpiredPlatinumTrialLicenseIsRestricted() throws Exception {
licenseeRegistry.setOperationMode(randomAllFeaturesMode()); licenseeRegistry.setOperationMode(randomTrialOrPlatinumMode());
GraphLicensee graphLicensee = new GraphLicensee(Settings.EMPTY, licenseeRegistry); GraphLicensee graphLicensee = new GraphLicensee(Settings.EMPTY, licenseeRegistry);
licenseeRegistry.register(graphLicensee); licenseeRegistry.register(graphLicensee);
licenseeRegistry.disable(); licenseeRegistry.disable();
@ -55,48 +49,48 @@ public class LicenseTests extends AbstractLicenseeTestCase {
assertLicenseBasicOrGoldOrNoneOrExpiredBehaviour(graphLicensee); assertLicenseBasicOrGoldOrNoneOrExpiredBehaviour(graphLicensee);
} }
public void testUpgradingFromFreeLicenseWorks() { public void testUpgradingFromBasicLicenseWorks() {
licenseeRegistry.setOperationMode(randomFreeMode()); licenseeRegistry.setOperationMode(OperationMode.BASIC);
GraphLicensee graphLicensee = new GraphLicensee(Settings.EMPTY, licenseeRegistry); GraphLicensee graphLicensee = new GraphLicensee(Settings.EMPTY, licenseeRegistry);
licenseeRegistry.register(graphLicensee); licenseeRegistry.register(graphLicensee);
assertLicenseBasicOrGoldOrNoneOrExpiredBehaviour(graphLicensee); assertLicenseBasicOrGoldOrNoneOrExpiredBehaviour(graphLicensee);
licenseeRegistry.setOperationMode(randomAllFeaturesMode()); licenseeRegistry.setOperationMode(randomTrialOrPlatinumMode());
assertLicensePlatinumTrialBehaviour(graphLicensee); assertLicensePlatinumTrialBehaviour(graphLicensee);
} }
public void testDowngradingToFreeLicenseWorks() { public void testDowngradingToBasicLicenseWorks() {
licenseeRegistry.setOperationMode(randomAllFeaturesMode()); licenseeRegistry.setOperationMode(randomTrialOrPlatinumMode());
GraphLicensee graphLicensee = new GraphLicensee(Settings.EMPTY, licenseeRegistry); GraphLicensee graphLicensee = new GraphLicensee(Settings.EMPTY, licenseeRegistry);
licenseeRegistry.register(graphLicensee); licenseeRegistry.register(graphLicensee);
assertLicensePlatinumTrialBehaviour(graphLicensee); assertLicensePlatinumTrialBehaviour(graphLicensee);
licenseeRegistry.setOperationMode(randomFreeMode()); licenseeRegistry.setOperationMode(OperationMode.BASIC);
assertLicenseBasicOrGoldOrNoneOrExpiredBehaviour(graphLicensee); assertLicenseBasicOrGoldOrNoneOrExpiredBehaviour(graphLicensee);
} }
public void testDowngradingToGoldLicenseWorks() { public void testDowngradingToGoldLicenseWorks() {
licenseeRegistry.setOperationMode(randomAllFeaturesMode()); licenseeRegistry.setOperationMode(randomTrialOrPlatinumMode());
GraphLicensee graphLicensee = new GraphLicensee(Settings.EMPTY, licenseeRegistry); GraphLicensee graphLicensee = new GraphLicensee(Settings.EMPTY, licenseeRegistry);
licenseeRegistry.register(graphLicensee); licenseeRegistry.register(graphLicensee);
assertLicensePlatinumTrialBehaviour(graphLicensee); assertLicensePlatinumTrialBehaviour(graphLicensee);
licenseeRegistry.setOperationMode(License.OperationMode.GOLD); licenseeRegistry.setOperationMode(OperationMode.GOLD);
assertLicenseBasicOrGoldOrNoneOrExpiredBehaviour(graphLicensee); assertLicenseBasicOrGoldOrNoneOrExpiredBehaviour(graphLicensee);
} }
public void testUpgradingExpiredLicenseWorks() { public void testUpgradingExpiredLicenseWorks() {
licenseeRegistry.setOperationMode(randomAllFeaturesMode()); licenseeRegistry.setOperationMode(randomTrialOrPlatinumMode());
GraphLicensee graphLicensee = new GraphLicensee(Settings.EMPTY, licenseeRegistry); GraphLicensee graphLicensee = new GraphLicensee(Settings.EMPTY, licenseeRegistry);
licenseeRegistry.register(graphLicensee); licenseeRegistry.register(graphLicensee);
licenseeRegistry.disable(); licenseeRegistry.disable();
assertLicenseBasicOrGoldOrNoneOrExpiredBehaviour(graphLicensee); assertLicenseBasicOrGoldOrNoneOrExpiredBehaviour(graphLicensee);
licenseeRegistry.setOperationMode(randomAllFeaturesMode()); licenseeRegistry.setOperationMode(randomTrialOrPlatinumMode());
assertLicensePlatinumTrialBehaviour(graphLicensee); assertLicensePlatinumTrialBehaviour(graphLicensee);
} }
@ -107,36 +101,4 @@ public class LicenseTests extends AbstractLicenseeTestCase {
private void assertLicenseBasicOrGoldOrNoneOrExpiredBehaviour(GraphLicensee graphLicensee) { private void assertLicenseBasicOrGoldOrNoneOrExpiredBehaviour(GraphLicensee graphLicensee) {
assertThat("Expected graph exploration not to be allowed", graphLicensee.isGraphExploreEnabled(), is(false)); assertThat("Expected graph exploration not to be allowed", graphLicensee.isGraphExploreEnabled(), is(false));
} }
public static class SimpleLicenseeRegistry extends AbstractComponent implements LicenseeRegistry {
private final List<Licensee> licensees = new ArrayList<>();
private License.OperationMode operationMode;
public SimpleLicenseeRegistry() {
super(Settings.EMPTY);
}
@Override
public void register(Licensee licensee) {
licensees.add(licensee);
enable();
}
public void enable() {
for (Licensee licensee : licensees) {
licensee.onChange(new Licensee.Status(operationMode, randomActiveState()));
}
}
public void disable() {
for (Licensee licensee : licensees) {
licensee.onChange(new Licensee.Status(operationMode, randomInactiveState()));
}
}
public void setOperationMode(License.OperationMode operationMode) {
this.operationMode = operationMode;
enable();
}
}
} }

View File

@ -5,10 +5,15 @@
*/ */
package org.elasticsearch.license.plugin.core; package org.elasticsearch.license.plugin.core;
import org.elasticsearch.common.component.AbstractComponent;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.license.core.License; import org.elasticsearch.license.core.License;
import org.elasticsearch.license.core.License.OperationMode;
import org.elasticsearch.test.ESTestCase; import org.elasticsearch.test.ESTestCase;
import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.List;
import java.util.function.Predicate; import java.util.function.Predicate;
import java.util.function.Supplier; import java.util.function.Supplier;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@ -32,7 +37,7 @@ public abstract class AbstractLicenseeTestCase extends ESTestCase {
* @param toMode New license * @param toMode New license
* @param licensee The licensee to test * @param licensee The licensee to test
*/ */
public static void assertEmptyAck(License.OperationMode fromMode, License.OperationMode toMode, Licensee licensee) { public static void assertEmptyAck(OperationMode fromMode, OperationMode toMode, Licensee licensee) {
License fromLicense = mock(License.class); License fromLicense = mock(License.class);
when(fromLicense.operationMode()).thenReturn(fromMode); when(fromLicense.operationMode()).thenReturn(fromMode);
License toLicense = mock(License.class); License toLicense = mock(License.class);
@ -57,7 +62,7 @@ public abstract class AbstractLicenseeTestCase extends ESTestCase {
* @param toMode New license * @param toMode New license
* @param licenseeSupplier Supplies the licensee to test * @param licenseeSupplier Supplies the licensee to test
*/ */
public static void assertEmptyAck(License.OperationMode fromMode, License.OperationMode toMode, Supplier<Licensee> licenseeSupplier) { public static void assertEmptyAck(OperationMode fromMode, OperationMode toMode, Supplier<Licensee> licenseeSupplier) {
assertEmptyAck(fromMode, toMode, licenseeSupplier.get()); assertEmptyAck(fromMode, toMode, licenseeSupplier.get());
} }
@ -70,7 +75,7 @@ public abstract class AbstractLicenseeTestCase extends ESTestCase {
* @param toMode New license * @param toMode New license
* @param licensee The licensee to test * @param licensee The licensee to test
*/ */
public static String[] ackLicenseChange(License.OperationMode fromMode, License.OperationMode toMode, Licensee licensee) { public static String[] ackLicenseChange(OperationMode fromMode, OperationMode toMode, Licensee licensee) {
License fromLicense = mock(License.class); License fromLicense = mock(License.class);
when(fromLicense.operationMode()).thenReturn(fromMode); when(fromLicense.operationMode()).thenReturn(fromMode);
License toLicense = mock(License.class); License toLicense = mock(License.class);
@ -88,67 +93,54 @@ public abstract class AbstractLicenseeTestCase extends ESTestCase {
* @param toMode New license * @param toMode New license
* @param licenseeSupplier Supplies the licensee to test * @param licenseeSupplier Supplies the licensee to test
*/ */
public static String[] ackLicenseChange(License.OperationMode fromMode, License.OperationMode toMode, Supplier<Licensee> licenseeSupplier) { public static String[] ackLicenseChange(OperationMode fromMode, OperationMode toMode, Supplier<Licensee> licenseeSupplier) {
return ackLicenseChange(fromMode, toMode, licenseeSupplier.get()); return ackLicenseChange(fromMode, toMode, licenseeSupplier.get());
} }
/** /**
* Get any {@link License.OperationMode} that have {@link License.OperationMode#allFeaturesEnabled()} all features enabled}. * Randomly get {@link OperationMode#TRIAL} or {@link OperationMode#PLATINUM}.
* *
* @return Never {@code null}. * @return Never {@code null}.
*/ */
public static License.OperationMode randomAllFeaturesMode() { public static OperationMode randomTrialOrPlatinumMode() {
return randomFrom(License.OperationMode.values(), License.OperationMode::allFeaturesEnabled); return randomFrom(OperationMode.TRIAL, OperationMode.PLATINUM);
} }
/** /**
* Get any {@link License.OperationMode} that {@link License.OperationMode#isPaid() is paid}. * Randomly get {@link OperationMode#TRIAL}, {@link OperationMode#GOLD}, or {@link OperationMode#PLATINUM}.
* *
* @return Never {@code null}. * @return Never {@code null}.
*/ */
public static License.OperationMode randomPaidMode() { public static OperationMode randomTrialGoldOrPlatinumMode() {
return randomFrom(License.OperationMode.values(), License.OperationMode::isPaid); return randomFrom(OperationMode.TRIAL, OperationMode.GOLD, OperationMode.PLATINUM);
} }
/** /**
* Get any {@link License.OperationMode} that {@link License.OperationMode#isPaid() is not paid}. * Randomly get any {@link OperationMode}.
* *
* @return Never {@code null}. * @return Never {@code null}.
*/ */
public static License.OperationMode randomFreeMode() { public static OperationMode randomMode() {
Predicate<License.OperationMode> isPaid = License.OperationMode::isPaid; return randomFrom(OperationMode.values());
return randomFrom(License.OperationMode.values(), isPaid.negate());
} }
/** /**
* Get any {@link #randomPaidMode() paid mode}, except the selected {@code mode}. * Get any {@link #randomMode() mode}, except the selected {@code mode}.
* *
* @param mode The mode to exclude. * @param mode The mode to exclude.
* @return Never {@code null}. * @return Never {@code null}.
*/ */
public static License.OperationMode randomFromPaidExcept(License.OperationMode mode) { public static OperationMode randomModeExcept(OperationMode mode) {
return randomValueOtherThan(mode, AbstractLicenseeTestCase::randomPaidMode); return randomValueOtherThan(mode, AbstractLicenseeTestCase::randomMode);
} }
/** /**
* Get any {@link LicenseState} that {@link LicenseState#isActive() is active}. * Randomly get {@link LicenseState#ENABLED} or {@link LicenseState#GRACE_PERIOD}.
* *
* @return Never {@code null}. * @return Never {@code null}.
*/ */
public static LicenseState randomActiveState() { public static LicenseState randomEnabledOrGracePeriodState() {
return randomFrom(LicenseState.values(), LicenseState::isActive); return randomFrom(LicenseState.ENABLED, LicenseState.GRACE_PERIOD);
}
/**
* Get any {@link LicenseState} that {@link LicenseState#isActive() is not active}.
*
* @return Never {@code null}.
*/
public static LicenseState randomInactiveState() {
Predicate<LicenseState> isActive = LicenseState::isActive;
return randomFrom(LicenseState.values(), isActive.negate());
} }
/** /**
@ -163,4 +155,36 @@ public abstract class AbstractLicenseeTestCase extends ESTestCase {
public static <T> T randomFrom(T[] values, Predicate<T> filter) { public static <T> T randomFrom(T[] values, Predicate<T> filter) {
return randomFrom(Arrays.stream(values).filter(filter).collect(Collectors.toList())); return randomFrom(Arrays.stream(values).filter(filter).collect(Collectors.toList()));
} }
public static class SimpleLicenseeRegistry extends AbstractComponent implements LicenseeRegistry {
private final List<Licensee> licensees = new ArrayList<>();
private OperationMode operationMode;
public SimpleLicenseeRegistry() {
super(Settings.EMPTY);
}
@Override
public void register(Licensee licensee) {
licensees.add(licensee);
enable();
}
public void enable() {
for (Licensee licensee : licensees) {
licensee.onChange(new Licensee.Status(operationMode, randomEnabledOrGracePeriodState()));
}
}
public void disable() {
for (Licensee licensee : licensees) {
licensee.onChange(new Licensee.Status(operationMode, LicenseState.DISABLED));
}
}
public void setOperationMode(License.OperationMode operationMode) {
this.operationMode = operationMode;
enable();
}
}
} }

View File

@ -10,7 +10,9 @@ import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.logging.LoggerMessageFormat; import org.elasticsearch.common.logging.LoggerMessageFormat;
import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.license.core.License; import org.elasticsearch.license.core.License;
import org.elasticsearch.license.core.License.OperationMode;
import org.elasticsearch.license.plugin.core.AbstractLicenseeComponent; import org.elasticsearch.license.plugin.core.AbstractLicenseeComponent;
import org.elasticsearch.license.plugin.core.LicenseState;
import org.elasticsearch.license.plugin.core.Licensee; import org.elasticsearch.license.plugin.core.Licensee;
import org.elasticsearch.license.plugin.core.LicenseeRegistry; import org.elasticsearch.license.plugin.core.LicenseeRegistry;
import org.elasticsearch.marvel.Marvel; import org.elasticsearch.marvel.Marvel;
@ -49,59 +51,67 @@ public class MarvelLicensee extends AbstractLicenseeComponent<MarvelLicensee> im
public String[] acknowledgmentMessages(License currentLicense, License newLicense) { public String[] acknowledgmentMessages(License currentLicense, License newLicense) {
switch (newLicense.operationMode()) { switch (newLicense.operationMode()) {
case BASIC: case BASIC:
if (currentLicense != null && currentLicense.operationMode().isPaid()) { if (currentLicense != null) {
switch (currentLicense.operationMode()) {
case TRIAL:
case GOLD:
case PLATINUM:
return new String[] { return new String[] {
LoggerMessageFormat.format( LoggerMessageFormat.format(
"Multi-cluster support is disabled for clusters with [{}] license. If you are\n" + "Multi-cluster support is disabled for clusters with [{}] license. If you are\n" +
"running multiple clusters, users won't be able to access the clusters with\n" + "running multiple clusters, users won't be able to access the clusters with\n" +
"[{}] licenses from within a single x-pack kibana instance. You will have to deploy a\n" + "[{}] licenses from within a single X-Pack Kibana instance. You will have to deploy a\n" +
"separate and dedicated x-pack kibana instance for each [{}] cluster you wish to monitor.", "separate and dedicated X-pack Kibana instance for each [{}] cluster you wish to monitor.",
newLicense.type(), newLicense.type(), newLicense.type()), newLicense.type(), newLicense.type(), newLicense.type()),
LoggerMessageFormat.format( LoggerMessageFormat.format(
"Automatic index cleanup is disabled for clusters with [{}] license.", newLicense.type()) "Automatic index cleanup is disabled for clusters with [{}] license.", newLicense.type())
}; };
} }
} }
break;
}
return Strings.EMPTY_ARRAY; return Strings.EMPTY_ARRAY;
} }
/** /**
* Determine if the index cleaning service is enabled. * Determine if the index cleaning service is enabled.
* <p> * <p>
* Collection is only disabled <em>automatically</em> when the license expires. * Collection is only disabled <em>automatically</em> when the license expires. All modes are valid for collection.
* <p> * <p>
*
* Collection <em>can</em> be disabled explicitly by the user, although that's generally a temporary solution to unrelated issues * Collection <em>can</em> be disabled explicitly by the user, although that's generally a temporary solution to unrelated issues
* (e.g., initial setup when the monitoring cluster doesn't actually exist). * (e.g., initial setup when the monitoring cluster doesn't actually exist).
* *
* @return {@code true} as long as the license is valid. Otherwise {@code false}. * @return {@code true} as long as the license is valid. Otherwise {@code false}.
*/ */
public boolean collectionEnabled() { public boolean collectionEnabled() {
return status.getLicenseState().isActive(); return status.getLicenseState() != LicenseState.DISABLED;
} }
/** /**
* Determine if the index cleaning service is enabled. * Determine if the index cleaning service is enabled.
* <p> * <p>
* Index cleaning is only disabled when the license expires. * Index cleaning is only disabled when the license expires. All modes are valid for cleaning.
* *
* @return {@code true} as long as the license is valid. Otherwise {@code false}. * @return {@code true} as long as the license is valid. Otherwise {@code false}.
*/ */
public boolean cleaningEnabled() { public boolean cleaningEnabled() {
return status.getLicenseState().isActive(); return status.getLicenseState() != LicenseState.DISABLED;
} }
/** /**
* Determine if the current license allows the retention of indices to be modified. * Determine if the current license allows the retention of indices to be modified.
* <p> * <p>
* Only users with a paid license type (or equivalent in the case of trial licenses) can update the retention period. * Only users with the following license types can update the retention period:
* <p> * <ul>
* <li>{@link OperationMode#PLATINUM}</li>
* <li>{@link OperationMode#GOLD}</li>
* <li>{@link OperationMode#TRIAL}</li>
* </ul>
* Note: This does not consider the <em>state</em> of the license so that any change is remembered for when they fix their license. * Note: This does not consider the <em>state</em> of the license so that any change is remembered for when they fix their license.
* *
* @return {@code true} if the user is allowed to modify the retention. Otherwise {@code false}. * @return {@code true} if the user is allowed to modify the retention. Otherwise {@code false}.
*/ */
public boolean allowUpdateRetention() { public boolean allowUpdateRetention() {
return status.getMode().isPaid(); return status.getMode() != OperationMode.BASIC;
} }
} }

View File

@ -31,42 +31,42 @@ public class MarvelLicenseeTests extends AbstractLicenseeTestCase {
private final MarvelLicensee licensee = new MarvelLicensee(Settings.EMPTY, registry); private final MarvelLicensee licensee = new MarvelLicensee(Settings.EMPTY, registry);
public void testAcknowledgementMessagesToAnyFromFreeIsNoOp() { public void testAcknowledgementMessagesToAnyFromFreeIsNoOp() {
assertEmptyAck(randomFreeMode(), randomFrom(OperationMode.values()), licensee); assertEmptyAck(OperationMode.BASIC, randomMode(), licensee);
} }
public void testAcknowledgementMessagesToPaidFromAnyIsNoOp() { public void testAcknowledgementMessagesToTrialGoldOrPlatinumFromAnyIsNoOp() {
assertEmptyAck(randomFrom(OperationMode.values()), randomPaidMode(), licensee); assertEmptyAck(randomMode(), randomTrialGoldOrPlatinumMode(), licensee);
} }
public void testAcknowledgementMessagesToFreeFromPaidNotesLimits() { public void testAcknowledgementMessagesToBasicFromNotBasicNotesLimits() {
String[] messages = ackLicenseChange(randomPaidMode(), randomFreeMode(), licensee); String[] messages = ackLicenseChange(randomModeExcept(OperationMode.BASIC), OperationMode.BASIC, licensee);
// leaving messages up to inspection // leaving messages up to inspection
assertThat(messages.length, equalTo(2)); assertThat(messages.length, equalTo(2));
} }
public void testCollectionEnabledIsTrueForActiveState() { public void testCollectionEnabledIsTrueForActiveState() {
assertEnabled(randomActiveState(), MarvelLicensee::collectionEnabled, true); assertEnabled(randomEnabledOrGracePeriodState(), MarvelLicensee::collectionEnabled, true);
} }
public void testCollectionEnabledIsFalseForInactiveState() { public void testCollectionEnabledIsFalseForInactiveState() {
assertEnabled(randomInactiveState(), MarvelLicensee::collectionEnabled, false); assertEnabled(LicenseState.DISABLED, MarvelLicensee::collectionEnabled, false);
} }
public void testCleaningEnabledIsTrueForActiveState() { public void testCleaningEnabledIsTrueForActiveState() {
assertEnabled(randomActiveState(), MarvelLicensee::cleaningEnabled, true); assertEnabled(randomEnabledOrGracePeriodState(), MarvelLicensee::cleaningEnabled, true);
} }
public void testCleaningEnabledIsFalseForInactiveState() { public void testCleaningEnabledIsFalseForInactiveState() {
assertEnabled(randomInactiveState(), MarvelLicensee::cleaningEnabled, false); assertEnabled(LicenseState.DISABLED, MarvelLicensee::cleaningEnabled, false);
} }
public void testAllowUpdateRetentionIsTrueForPaid() { public void testAllowUpdateRetentionIsTrueForNotBasic() {
assertEnabled(randomPaidMode(), MarvelLicensee::allowUpdateRetention, true); assertEnabled(randomModeExcept(OperationMode.BASIC), MarvelLicensee::allowUpdateRetention, true);
} }
public void testAllowUpdateRetentionIsFalseForFree() { public void testAllowUpdateRetentionIsFalseForBasic() {
assertEnabled(randomFreeMode(), MarvelLicensee::allowUpdateRetention, false); assertEnabled(OperationMode.BASIC, MarvelLicensee::allowUpdateRetention, false);
} }
/** /**
@ -95,7 +95,7 @@ public class MarvelLicenseeTests extends AbstractLicenseeTestCase {
* @param predicate The method to invoke (expected to be an instance method). * @param predicate The method to invoke (expected to be an instance method).
* @param expected The expected outcome given the {@code mode} and {@code predicate}. * @param expected The expected outcome given the {@code mode} and {@code predicate}.
*/ */
private void assertEnabled(License.OperationMode mode, Predicate<MarvelLicensee> predicate, boolean expected) { private void assertEnabled(OperationMode mode, Predicate<MarvelLicensee> predicate, boolean expected) {
Status status = mock(Status.class); Status status = mock(Status.class);
when(status.getMode()).thenReturn(mode); when(status.getMode()).thenReturn(mode);

View File

@ -5,6 +5,7 @@
*/ */
package org.elasticsearch.shield.license; package org.elasticsearch.shield.license;
import org.elasticsearch.license.core.License.OperationMode;
import org.elasticsearch.license.plugin.core.LicenseState; import org.elasticsearch.license.plugin.core.LicenseState;
import org.elasticsearch.license.plugin.core.Licensee.Status; import org.elasticsearch.license.plugin.core.Licensee.Status;
@ -23,31 +24,51 @@ public class ShieldLicenseState {
* @return true if the license allows for security features to be enabled (authc, authz, ip filter, audit, etc) * @return true if the license allows for security features to be enabled (authc, authz, ip filter, audit, etc)
*/ */
public boolean securityEnabled() { public boolean securityEnabled() {
return status.getMode().isPaid(); return status.getMode() != OperationMode.BASIC;
} }
/** /**
* Indicates whether the stats and health API calls should be allowed. If a license is expired and past the grace * Indicates whether the stats and health API calls should be allowed. If a license is expired and past the grace
* period then we deny these calls. * period then we deny these calls.
* *
* @return true if the license allows for the stats and health apis to be used. * @return true if the license allows for the stats and health APIs to be used.
*/ */
public boolean statsAndHealthEnabled() { public boolean statsAndHealthEnabled() {
return status.getLicenseState() != LicenseState.DISABLED; return status.getLicenseState() != LicenseState.DISABLED;
} }
/** /**
* @return true if the license enables DLS and FLS * Determine if Document Level Security (DLS) and Field Level Security (FLS) should be enabled.
* <p>
* DLS and FLS are only disabled when the mode is not:
* <ul>
* <li>{@link OperationMode#PLATINUM}</li>
* <li>{@link OperationMode#TRIAL}</li>
* </ul>
* Note: This does not consider the <em>state</em> of the license so that Security does not suddenly leak information!
*
* @return {@code true} to enable DLS and FLS. Otherwise {@code false}.
*/ */
public boolean documentAndFieldLevelSecurityEnabled() { public boolean documentAndFieldLevelSecurityEnabled() {
return status.getMode().allFeaturesEnabled(); Status status = this.status;
return status.getMode() == OperationMode.TRIAL || status.getMode() == OperationMode.PLATINUM;
} }
/** /**
* @return true if the license enables the use of custom authentication realms * Determine if Custom Realms should be enabled.
* <p>
* Custom Realms are only disabled when the mode is not:
* <ul>
* <li>{@link OperationMode#PLATINUM}</li>
* <li>{@link OperationMode#TRIAL}</li>
* </ul>
* Note: This does not consider the <em>state</em> of the license so that Security does not suddenly block requests!
*
* @return {@code true} to enable Custom Realms. Otherwise {@code false}.
*/ */
public boolean customRealmsEnabled() { public boolean customRealmsEnabled() {
return status.getMode().allFeaturesEnabled(); Status status = this.status;
return status.getMode() == OperationMode.TRIAL || status.getMode() == OperationMode.PLATINUM;
} }
void updateStatus(Status status) { void updateStatus(Status status) {

View File

@ -48,20 +48,32 @@ public class ShieldLicensee extends AbstractLicenseeComponent<ShieldLicensee> im
public String[] acknowledgmentMessages(License currentLicense, License newLicense) { public String[] acknowledgmentMessages(License currentLicense, License newLicense) {
switch (newLicense.operationMode()) { switch (newLicense.operationMode()) {
case BASIC: case BASIC:
if (currentLicense != null && currentLicense.operationMode().isPaid()) { if (currentLicense != null) {
return new String[]{"The following Shield functionality will be disabled: authentication, authorization, ip " + switch (currentLicense.operationMode()) {
"filtering, auditing, SSL will be disabled on node restart. Please restart your node after applying " + case TRIAL:
"the license."}; case GOLD:
case PLATINUM:
return new String[] {
"The following Shield functionality will be disabled: authentication, authorization, ip filtering, " +
"auditing, SSL will be disabled on node restart. Please restart your node after applying the license.",
"Field and document level access control will be disabled",
"Custom realms will be ignored"
};
}
} }
break; break;
case GOLD: case GOLD:
// Note: If we're upgrading from a non-paid license, then we do not need to inform them of anything if (currentLicense != null) {
if (currentLicense != null && currentLicense.operationMode().isPaid()) { switch (currentLicense.operationMode()) {
case BASIC: // though technically it was already disabled, it's not bad to remind them
case TRIAL:
case PLATINUM:
return new String[] { return new String[] {
"Field and document level access control will be disabled", "Field and document level access control will be disabled",
"Custom realms will be ignored" "Custom realms will be ignored"
}; };
} }
}
break; break;
} }
return Strings.EMPTY_ARRAY; return Strings.EMPTY_ARRAY;

View File

@ -58,23 +58,23 @@ public class ShieldLicenseeTests extends AbstractLicenseeTestCase {
verifyNoMoreInteractions(registry, shieldState); verifyNoMoreInteractions(registry, shieldState);
} }
public void testAcknowledgementMessagesFromFreeToAnyIsNoOp() { public void testAcknowledgementMessagesFromBasicToAnyIsNoOp() {
assertEmptyAck(randomFreeMode(), randomFrom(OperationMode.values()), this::buildLicensee); assertEmptyAck(OperationMode.BASIC, randomMode(), this::buildLicensee);
} }
public void testAcknowledgementMessagesFromAnyToNonGoldPaidIsNoOp() { public void testAcknowledgementMessagesFromAnyToTrialOrPlatinumIsNoOp() {
assertEmptyAck(randomFrom(OperationMode.values()), randomFromPaidExcept(OperationMode.GOLD), this::buildLicensee); assertEmptyAck(randomMode(), randomTrialOrPlatinumMode(), this::buildLicensee);
} }
public void testAcknowledgementMessagesFromPaidToFreeNotesLimits() { public void testAcknowledgementMessagesFromTrialGoldOrPlatinumToBasicNotesLimits() {
String[] messages = ackLicenseChange(randomPaidMode(), randomFreeMode(), this::buildLicensee); String[] messages = ackLicenseChange(randomTrialGoldOrPlatinumMode(), OperationMode.BASIC, this::buildLicensee);
// leaving messages up to inspection // leaving messages up to inspection
assertThat(messages.length, equalTo(1)); assertThat(messages.length, equalTo(3));
} }
public void testAcknowledgementMessagesFromNonGoldPaidToGoldNotesLimits() { public void testAcknowledgementMessagesFromTrialOrPlatinumToGoldNotesLimits() {
String[] messages = ackLicenseChange(randomFromPaidExcept(OperationMode.GOLD), OperationMode.GOLD, this::buildLicensee); String[] messages = ackLicenseChange(randomTrialOrPlatinumMode(), OperationMode.GOLD, this::buildLicensee);
// leaving messages up to inspection // leaving messages up to inspection
assertThat(messages.length, equalTo(2)); assertThat(messages.length, equalTo(2));

View File

@ -9,11 +9,16 @@ import org.elasticsearch.common.Strings;
import org.elasticsearch.common.inject.Inject; import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.license.core.License; import org.elasticsearch.license.core.License;
import org.elasticsearch.license.core.License.OperationMode;
import org.elasticsearch.license.plugin.core.AbstractLicenseeComponent; import org.elasticsearch.license.plugin.core.AbstractLicenseeComponent;
import org.elasticsearch.license.plugin.core.LicenseState; import org.elasticsearch.license.plugin.core.LicenseState;
import org.elasticsearch.license.plugin.core.LicenseeRegistry; import org.elasticsearch.license.plugin.core.LicenseeRegistry;
import org.elasticsearch.watcher.Watcher; import org.elasticsearch.watcher.Watcher;
import static org.elasticsearch.license.core.License.OperationMode.TRIAL;
import static org.elasticsearch.license.core.License.OperationMode.GOLD;
import static org.elasticsearch.license.core.License.OperationMode.PLATINUM;
public class WatcherLicensee extends AbstractLicenseeComponent<WatcherLicensee> { public class WatcherLicensee extends AbstractLicenseeComponent<WatcherLicensee> {
public static final String ID = Watcher.NAME; public static final String ID = Watcher.NAME;
@ -36,9 +41,14 @@ public class WatcherLicensee extends AbstractLicenseeComponent<WatcherLicensee>
public String[] acknowledgmentMessages(License currentLicense, License newLicense) { public String[] acknowledgmentMessages(License currentLicense, License newLicense) {
switch (newLicense.operationMode()) { switch (newLicense.operationMode()) {
case BASIC: case BASIC:
if (currentLicense != null && currentLicense.operationMode().isPaid()) { if (currentLicense != null) {
switch (currentLicense.operationMode()) {
case TRIAL:
case GOLD:
case PLATINUM:
return new String[] { "Watcher will be disabled" }; return new String[] { "Watcher will be disabled" };
} }
}
break; break;
} }
return Strings.EMPTY_ARRAY; return Strings.EMPTY_ARRAY;
@ -56,10 +66,25 @@ public class WatcherLicensee extends AbstractLicenseeComponent<WatcherLicensee>
return isWatcherTransportActionAllowed(); return isWatcherTransportActionAllowed();
} }
/**
* Determine if Watcher should be enabled.
* <p>
* Watcher is only disabled when the license has expired or if the mode is not:
* <ul>
* <li>{@link OperationMode#PLATINUM}</li>
* <li>{@link OperationMode#GOLD}</li>
* <li>{@link OperationMode#TRIAL}</li>
* </ul>
*
* @return {@code true} as long as the license is valid. Otherwise {@code false}.
*/
public boolean isWatcherTransportActionAllowed() { public boolean isWatcherTransportActionAllowed() {
// status is volatile, so a local variable is used for a consistent view // status is volatile, so a local variable is used for a consistent view
Status localStatus = status; Status localStatus = status;
return localStatus.getLicenseState().isActive() && localStatus.getMode().isPaid(); OperationMode operationMode = localStatus.getMode();
boolean licensed = operationMode == TRIAL || operationMode == GOLD || operationMode == PLATINUM;
return licensed && localStatus.getLicenseState() != LicenseState.DISABLED;
} }
} }

View File

@ -5,15 +5,9 @@
*/ */
package org.elasticsearch.watcher.license; package org.elasticsearch.watcher.license;
import org.elasticsearch.common.component.AbstractComponent;
import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.license.core.License; import org.elasticsearch.license.core.License.OperationMode;
import org.elasticsearch.license.plugin.core.AbstractLicenseeTestCase; import org.elasticsearch.license.plugin.core.AbstractLicenseeTestCase;
import org.elasticsearch.license.plugin.core.Licensee;
import org.elasticsearch.license.plugin.core.LicenseeRegistry;
import java.util.ArrayList;
import java.util.List;
import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.is;
@ -22,7 +16,7 @@ public class LicenseTests extends AbstractLicenseeTestCase {
private SimpleLicenseeRegistry licenseeRegistry = new SimpleLicenseeRegistry(); private SimpleLicenseeRegistry licenseeRegistry = new SimpleLicenseeRegistry();
public void testPlatinumGoldTrialLicenseCanDoEverything() throws Exception { public void testPlatinumGoldTrialLicenseCanDoEverything() throws Exception {
licenseeRegistry.setOperationMode(randomPaidMode()); licenseeRegistry.setOperationMode(randomTrialGoldOrPlatinumMode());
WatcherLicensee watcherLicensee = new WatcherLicensee(Settings.EMPTY, licenseeRegistry); WatcherLicensee watcherLicensee = new WatcherLicensee(Settings.EMPTY, licenseeRegistry);
licenseeRegistry.register(watcherLicensee); licenseeRegistry.register(watcherLicensee);
@ -30,7 +24,7 @@ public class LicenseTests extends AbstractLicenseeTestCase {
} }
public void testBasicLicenseIsDisabled() throws Exception { public void testBasicLicenseIsDisabled() throws Exception {
licenseeRegistry.setOperationMode(randomFreeMode()); licenseeRegistry.setOperationMode(OperationMode.BASIC);
WatcherLicensee watcherLicensee = new WatcherLicensee(Settings.EMPTY, licenseeRegistry); WatcherLicensee watcherLicensee = new WatcherLicensee(Settings.EMPTY, licenseeRegistry);
licenseeRegistry.register(watcherLicensee); licenseeRegistry.register(watcherLicensee);
@ -38,7 +32,7 @@ public class LicenseTests extends AbstractLicenseeTestCase {
} }
public void testNoLicenseDoesNotWork() { public void testNoLicenseDoesNotWork() {
licenseeRegistry.setOperationMode(randomFreeMode()); licenseeRegistry.setOperationMode(OperationMode.BASIC);
WatcherLicensee watcherLicensee = new WatcherLicensee(Settings.EMPTY, licenseeRegistry); WatcherLicensee watcherLicensee = new WatcherLicensee(Settings.EMPTY, licenseeRegistry);
licenseeRegistry.register(watcherLicensee); licenseeRegistry.register(watcherLicensee);
licenseeRegistry.disable(); licenseeRegistry.disable();
@ -47,7 +41,7 @@ public class LicenseTests extends AbstractLicenseeTestCase {
} }
public void testExpiredPlatinumGoldTrialLicenseIsRestricted() throws Exception { public void testExpiredPlatinumGoldTrialLicenseIsRestricted() throws Exception {
licenseeRegistry.setOperationMode(randomPaidMode()); licenseeRegistry.setOperationMode(randomTrialGoldOrPlatinumMode());
WatcherLicensee watcherLicensee = new WatcherLicensee(Settings.EMPTY, licenseeRegistry); WatcherLicensee watcherLicensee = new WatcherLicensee(Settings.EMPTY, licenseeRegistry);
licenseeRegistry.register(watcherLicensee); licenseeRegistry.register(watcherLicensee);
licenseeRegistry.disable(); licenseeRegistry.disable();
@ -56,36 +50,36 @@ public class LicenseTests extends AbstractLicenseeTestCase {
} }
public void testUpgradingFromBasicLicenseWorks() { public void testUpgradingFromBasicLicenseWorks() {
licenseeRegistry.setOperationMode(randomFreeMode()); licenseeRegistry.setOperationMode(OperationMode.BASIC);
WatcherLicensee watcherLicensee = new WatcherLicensee(Settings.EMPTY, licenseeRegistry); WatcherLicensee watcherLicensee = new WatcherLicensee(Settings.EMPTY, licenseeRegistry);
licenseeRegistry.register(watcherLicensee); licenseeRegistry.register(watcherLicensee);
assertLicenseBasicOrNoneOrExpiredBehaviour(watcherLicensee); assertLicenseBasicOrNoneOrExpiredBehaviour(watcherLicensee);
licenseeRegistry.setOperationMode(randomPaidMode()); licenseeRegistry.setOperationMode(randomTrialGoldOrPlatinumMode());
assertLicenseGoldPlatinumTrialBehaviour(watcherLicensee); assertLicenseGoldPlatinumTrialBehaviour(watcherLicensee);
} }
public void testDowngradingToBasicLicenseWorks() { public void testDowngradingToBasicLicenseWorks() {
licenseeRegistry.setOperationMode(randomPaidMode()); licenseeRegistry.setOperationMode(randomTrialGoldOrPlatinumMode());
WatcherLicensee watcherLicensee = new WatcherLicensee(Settings.EMPTY, licenseeRegistry); WatcherLicensee watcherLicensee = new WatcherLicensee(Settings.EMPTY, licenseeRegistry);
licenseeRegistry.register(watcherLicensee); licenseeRegistry.register(watcherLicensee);
assertLicenseGoldPlatinumTrialBehaviour(watcherLicensee); assertLicenseGoldPlatinumTrialBehaviour(watcherLicensee);
licenseeRegistry.setOperationMode(randomFreeMode()); licenseeRegistry.setOperationMode(OperationMode.BASIC);
assertLicenseBasicOrNoneOrExpiredBehaviour(watcherLicensee); assertLicenseBasicOrNoneOrExpiredBehaviour(watcherLicensee);
} }
public void testUpgradingExpiredLicenseWorks() { public void testUpgradingExpiredLicenseWorks() {
licenseeRegistry.setOperationMode(randomPaidMode()); licenseeRegistry.setOperationMode(randomTrialGoldOrPlatinumMode());
WatcherLicensee watcherLicensee = new WatcherLicensee(Settings.EMPTY, licenseeRegistry); WatcherLicensee watcherLicensee = new WatcherLicensee(Settings.EMPTY, licenseeRegistry);
licenseeRegistry.register(watcherLicensee); licenseeRegistry.register(watcherLicensee);
licenseeRegistry.disable(); licenseeRegistry.disable();
assertLicenseBasicOrNoneOrExpiredBehaviour(watcherLicensee); assertLicenseBasicOrNoneOrExpiredBehaviour(watcherLicensee);
licenseeRegistry.setOperationMode(randomPaidMode()); licenseeRegistry.setOperationMode(randomTrialGoldOrPlatinumMode());
assertLicenseGoldPlatinumTrialBehaviour(watcherLicensee); assertLicenseGoldPlatinumTrialBehaviour(watcherLicensee);
} }
@ -102,36 +96,4 @@ public class LicenseTests extends AbstractLicenseeTestCase {
assertThat("Expected watcher transport actions not to be allowed", watcherLicensee.isWatcherTransportActionAllowed(), is(false)); assertThat("Expected watcher transport actions not to be allowed", watcherLicensee.isWatcherTransportActionAllowed(), is(false));
assertThat("Expected actions of a watch not to be executed", watcherLicensee.isExecutingActionsAllowed(), is(false)); assertThat("Expected actions of a watch not to be executed", watcherLicensee.isExecutingActionsAllowed(), is(false));
} }
public static class SimpleLicenseeRegistry extends AbstractComponent implements LicenseeRegistry {
private final List<Licensee> licensees = new ArrayList<>();
private License.OperationMode operationMode;
public SimpleLicenseeRegistry() {
super(Settings.EMPTY);
}
@Override
public void register(Licensee licensee) {
licensees.add(licensee);
enable();
}
public void enable() {
for (Licensee licensee : licensees) {
licensee.onChange(new Licensee.Status(operationMode, randomActiveState()));
}
}
public void disable() {
for (Licensee licensee : licensees) {
licensee.onChange(new Licensee.Status(operationMode, randomInactiveState()));
}
}
public void setOperationMode(License.OperationMode operationMode) {
this.operationMode = operationMode;
enable();
}
}
} }