Skip to content

Commit

Permalink
chore: add premiumFeatures.enable token to build info (#19846)
Browse files Browse the repository at this point in the history
* chore: add premiumFeatures.enable token to build info

* dropped _TOKEN from constant name

* formatting

* Update flow-plugins/flow-plugin-base/src/main/java/com/vaadin/flow/plugin/base/BuildFrontendUtil.java

Co-authored-by: Mikhail Shabarov <[email protected]>

* Update flow-plugins/flow-plugin-base/src/main/java/com/vaadin/flow/plugin/base/BuildFrontendUtil.java

---------

Co-authored-by: Mikhail Shabarov <[email protected]>
  • Loading branch information
tltv and mshabarov authored Sep 2, 2024
1 parent 9ee97e8 commit 0d27e33
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 85 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -755,9 +755,16 @@ public static void updateBuildFile(PluginAdapterBuild adapter,
buildInfo.put(SERVLET_PARAMETER_PRODUCTION_MODE, true);
buildInfo.put(APPLICATION_IDENTIFIER,
adapter.applicationIdentifier());
if (licenseRequired && LocalSubscriptionKey.get() != null) {
adapter.logInfo("Daily Active User tracking enabled");
buildInfo.put(DAU_TOKEN, true);
if (licenseRequired) {
if (LocalSubscriptionKey.get() != null) {
adapter.logInfo("Daily Active User tracking enabled");
buildInfo.put(DAU_TOKEN, true);
}
if (LicenseChecker.isValidLicense("vaadin-commercial-cc-client",
null, BuildType.PRODUCTION)) {
adapter.logInfo("Premium Features are enabled");
buildInfo.put(Constants.PREMIUM_FEATURES, true);
}
}

FileUtils.write(tokenFile, JsonUtil.stringify(buildInfo, 2) + "\n",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.mockito.ArgumentMatchers;
import org.mockito.InOrder;
import org.mockito.MockedConstruction;
import org.mockito.MockedStatic;
Expand All @@ -50,6 +49,7 @@
import com.vaadin.flow.server.frontend.scanner.ClassFinder;
import com.vaadin.flow.server.frontend.scanner.FrontendDependenciesScanner;
import com.vaadin.flow.utils.LookupImpl;
import com.vaadin.pro.licensechecker.LicenseChecker;
import com.vaadin.pro.licensechecker.Product;

import elemental.json.Json;
Expand Down Expand Up @@ -219,24 +219,13 @@ public void detectsUsedCommercialComponents() {
@Test
public void propagateBuildInfo_tokenFileNotExisting_createTokenFile()
throws Exception {
fillAdapter();

BuildFrontendUtil.propagateBuildInfo(adapter);
File tokenFile = new File(resourceOutput, TOKEN_FILE);
Assert.assertTrue("Token file should have been created",
tokenFile.exists());
prepareAndAssertTokenFile();
}

@Test
public void propagateBuildInfo_existingTokenFileWithDifferentContent_overwritesTokenFile()
throws Exception {
fillAdapter();

BuildFrontendUtil.propagateBuildInfo(adapter);

File tokenFile = new File(resourceOutput, TOKEN_FILE);
Assert.assertTrue("Token file should have been created",
tokenFile.exists());
File tokenFile = prepareAndAssertTokenFile();
long lastModified = tokenFile.lastModified();

Thread.sleep(100);
Expand All @@ -250,13 +239,7 @@ public void propagateBuildInfo_existingTokenFileWithDifferentContent_overwritesT
@Test
public void propagateBuildInfo_existingTokenFileWithSameContent_doesNotWriteTokenFile()
throws Exception {
fillAdapter();

BuildFrontendUtil.propagateBuildInfo(adapter);

File tokenFile = new File(resourceOutput, TOKEN_FILE);
Assert.assertTrue("Token file should have been created",
tokenFile.exists());
File tokenFile = prepareAndAssertTokenFile();
long lastModified = tokenFile.lastModified();

Thread.sleep(100);
Expand Down Expand Up @@ -321,13 +304,7 @@ public void updateBuildFile_tokenFileNotExisting_doNothing()
@Test
public void updateBuildFile_tokenExisting_developmentEntriesRemoved()
throws Exception {
fillAdapter();

BuildFrontendUtil.propagateBuildInfo(adapter);

File tokenFile = new File(resourceOutput, TOKEN_FILE);
Assert.assertTrue("Token file should have been created",
tokenFile.exists());
File tokenFile = prepareAndAssertTokenFile();
JsonObject buildInfoJsonDev = Json
.parse(Files.readString(tokenFile.toPath()));

Expand All @@ -347,13 +324,7 @@ public void updateBuildFile_tokenExisting_developmentEntriesRemoved()
@Test
public void updateBuildFile_tokenExisting_applicationIdentifierAdded()
throws Exception {
fillAdapter();

BuildFrontendUtil.propagateBuildInfo(adapter);

File tokenFile = new File(resourceOutput, TOKEN_FILE);
Assert.assertTrue("Token file should have been created",
tokenFile.exists());
File tokenFile = prepareAndAssertTokenFile();

BuildFrontendUtil.updateBuildFile(adapter, false);
Assert.assertTrue("Token file should still exist", tokenFile.exists());
Expand All @@ -367,42 +338,34 @@ public void updateBuildFile_tokenExisting_applicationIdentifierAdded()
@Test
public void updateBuildFile_tokenExisting_licenseRequiredAndSubscriptionKey_dauFlagAdded()
throws Exception {
fillAdapter();

BuildFrontendUtil.propagateBuildInfo(adapter);

File tokenFile = new File(resourceOutput, TOKEN_FILE);
Assert.assertTrue("Token file should have been created",
tokenFile.exists());

String subscriptionKey = System.getProperty("vaadin.subscriptionKey");
System.setProperty("vaadin.subscriptionKey", "sub-123");
try {
BuildFrontendUtil.updateBuildFile(adapter, true);
} finally {
if (subscriptionKey != null) {
System.setProperty("vaadin.subscriptionKey", subscriptionKey);
} else {
System.clearProperty("vaadin.subscriptionKey");
File tokenFile = prepareAndAssertTokenFile();
withMockedLicenseChecker(false, () -> {
String subscriptionKey = System
.getProperty("vaadin.subscriptionKey");
System.setProperty("vaadin.subscriptionKey", "sub-123");
try {
BuildFrontendUtil.updateBuildFile(adapter, true);
} finally {
if (subscriptionKey != null) {
System.setProperty("vaadin.subscriptionKey",
subscriptionKey);
} else {
System.clearProperty("vaadin.subscriptionKey");
}
}
}
Assert.assertTrue("Token file should still exist", tokenFile.exists());
JsonObject buildInfoJsonProd = Json
.parse(Files.readString(tokenFile.toPath()));
Assert.assertTrue("DAU flag should be active in token file",
buildInfoJsonProd.getBoolean(Constants.DAU_TOKEN));
Assert.assertTrue("Token file should still exist",
tokenFile.exists());
JsonObject buildInfoJsonProd = Json
.parse(Files.readString(tokenFile.toPath()));
Assert.assertTrue("DAU flag should be active in token file",
buildInfoJsonProd.getBoolean(Constants.DAU_TOKEN));
});
}

@Test
public void updateBuildFile_tokenExisting_licenseNotRequiredAndSubscriptionKey_dauFlagNotAdded()
throws Exception {
fillAdapter();

BuildFrontendUtil.propagateBuildInfo(adapter);

File tokenFile = new File(resourceOutput, TOKEN_FILE);
Assert.assertTrue("Token file should have been created",
tokenFile.exists());
File tokenFile = prepareAndAssertTokenFile();

String subscriptionKey = System.getProperty("vaadin.subscriptionKey");
System.setProperty("vaadin.subscriptionKey", "sub-123");
Expand All @@ -425,30 +388,94 @@ public void updateBuildFile_tokenExisting_licenseNotRequiredAndSubscriptionKey_d
@Test
public void updateBuildFile_tokenExisting_licenseRequiredNoSubscriptionKey_dauFlagNotAdded()
throws Exception {
File tokenFile = prepareAndAssertTokenFile();
withMockedLicenseChecker(false, () -> {
String subscriptionKey = System
.getProperty("vaadin.subscriptionKey");
System.clearProperty("vaadin.subscriptionKey");
try {
BuildFrontendUtil.updateBuildFile(adapter, true);
} finally {
if (subscriptionKey != null) {
System.setProperty("vaadin.subscriptionKey",
subscriptionKey);
} else {
System.clearProperty("vaadin.subscriptionKey");
}
}
Assert.assertTrue("Token file should still exist",
tokenFile.exists());
JsonObject buildInfoJsonProd = Json
.parse(Files.readString(tokenFile.toPath()));
Assert.assertFalse("DAU flag should not be present in token file",
buildInfoJsonProd.hasKey(Constants.DAU_TOKEN));
});
}

@Test
public void updateBuildFile_tokenExisting_licenseRequiredAndIsPremiumLike_premiumFeaturesFlagAdded()
throws Exception {
File tokenFile = prepareAndAssertTokenFile();

withMockedLicenseChecker(true, () -> {
BuildFrontendUtil.updateBuildFile(adapter, true);
Assert.assertTrue("Token file should still exist",
tokenFile.exists());
JsonObject buildInfoJsonProd = Json
.parse(Files.readString(tokenFile.toPath()));
Assert.assertTrue(
Constants.PREMIUM_FEATURES
+ " flag should be active in token file",
buildInfoJsonProd.getBoolean(Constants.PREMIUM_FEATURES));
});
}

@Test
public void updateBuildFile_tokenExisting_licenseRequiredAndIsNotPremiumLike_premiumFeaturesFlagNotAdded()
throws Exception {
File tokenFile = prepareAndAssertTokenFile();

withMockedLicenseChecker(false, () -> {
BuildFrontendUtil.updateBuildFile(adapter, true);
Assert.assertTrue("Token file should still exist",
tokenFile.exists());
JsonObject buildInfoJsonProd = Json
.parse(Files.readString(tokenFile.toPath()));
Assert.assertFalse(
Constants.PREMIUM_FEATURES
+ " flag should not be active in token file",
buildInfoJsonProd.hasKey(Constants.PREMIUM_FEATURES));
});
}

private void withMockedLicenseChecker(boolean isValidLicense,
ThrowingRunnable test) throws IOException {
try (MockedStatic<LicenseChecker> licenseChecker = Mockito
.mockStatic(LicenseChecker.class)) {
licenseChecker
.when(() -> LicenseChecker.isValidLicense(Mockito.any(),
Mockito.any(), Mockito.any()))
.thenReturn(isValidLicense);
licenseChecker.when(LicenseChecker::getLogger)
.thenReturn(Mockito.mock(org.slf4j.Logger.class));
test.run();
}
}

@FunctionalInterface
private interface ThrowingRunnable {
void run() throws IOException;
}

private File prepareAndAssertTokenFile() throws URISyntaxException {
fillAdapter();

BuildFrontendUtil.propagateBuildInfo(adapter);

File tokenFile = new File(resourceOutput, TOKEN_FILE);
Assert.assertTrue("Token file should have been created",
tokenFile.exists());

String subscriptionKey = System.getProperty("vaadin.subscriptionKey");
System.clearProperty("vaadin.subscriptionKey");
try {
BuildFrontendUtil.updateBuildFile(adapter, true);
} finally {
if (subscriptionKey != null) {
System.setProperty("vaadin.subscriptionKey", subscriptionKey);
} else {
System.clearProperty("vaadin.subscriptionKey");
}
}
Assert.assertTrue("Token file should still exist", tokenFile.exists());
JsonObject buildInfoJsonProd = Json
.parse(Files.readString(tokenFile.toPath()));
Assert.assertFalse("DAU flag should not be present in token file",
buildInfoJsonProd.hasKey(Constants.DAU_TOKEN));
return tokenFile;
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public final class Constants implements Serializable {
public static final String EXTERNAL_STATS_FILE_TOKEN = "externalStatsFile";
public static final String EXTERNAL_STATS_URL_TOKEN = "externalStatsUrl";
public static final String DAU_TOKEN = "dau.enable";
public static final String PREMIUM_FEATURES = "premiumFeatures.enable";

public static final String POLYFILLS_DEFAULT_VALUE = "";

Expand Down

0 comments on commit 0d27e33

Please sign in to comment.