diff --git a/dotCMS/src/main/java/com/dotcms/ai/app/ConfigService.java b/dotCMS/src/main/java/com/dotcms/ai/app/ConfigService.java index ddcb27bb4188..115439388cc2 100644 --- a/dotCMS/src/main/java/com/dotcms/ai/app/ConfigService.java +++ b/dotCMS/src/main/java/com/dotcms/ai/app/ConfigService.java @@ -2,11 +2,11 @@ import com.dotcms.api.web.HttpServletRequestThreadLocal; import com.dotcms.security.apps.AppSecrets; +import com.dotcms.util.LicenseValiditySupplier; import com.dotmarketing.beans.Host; import com.dotmarketing.business.APILocator; import com.dotmarketing.business.web.WebAPILocator; -import com.dotmarketing.exception.DotDataException; -import com.dotmarketing.exception.DotSecurityException; +import com.dotmarketing.util.Logger; import com.liferay.portal.model.User; import io.vavr.control.Try; @@ -20,7 +20,10 @@ public class ConfigService { public static final ConfigService INSTANCE = new ConfigService(); + private final LicenseValiditySupplier licenseValiditySupplier; + private ConfigService() { + licenseValiditySupplier = new LicenseValiditySupplier() {}; } /** @@ -28,8 +31,14 @@ private ConfigService() { * by dotCMS. */ public AppConfig config(final Host host) { - final User systemUser = APILocator.systemUser(); final Host resolved = resolveHost(host); + + if (!licenseValiditySupplier.hasValidLicense()) { + Logger.debug(this, "No valid license found, returning empty configuration"); + return new AppConfig(resolved.getHostname(), Map.of()); + } + + final User systemUser = APILocator.systemUser(); Optional appSecrets = Try .of(() -> APILocator.getAppsAPI().getSecrets(AppKeys.APP_KEY, false, resolved, systemUser)) .get(); @@ -43,7 +52,6 @@ public AppConfig config(final Host host) { realHost = resolved; } - return new AppConfig(realHost.getHostname(), appSecrets.map(AppSecrets::getSecrets).orElse(Map.of())); } diff --git a/dotcms-integration/src/test/java/com/dotcms/ai/viewtool/AIViewToolTest.java b/dotcms-integration/src/test/java/com/dotcms/ai/viewtool/AIViewToolTest.java index b6c7dd2d7933..314079da2a93 100644 --- a/dotcms-integration/src/test/java/com/dotcms/ai/viewtool/AIViewToolTest.java +++ b/dotcms-integration/src/test/java/com/dotcms/ai/viewtool/AIViewToolTest.java @@ -1,5 +1,6 @@ package com.dotcms.ai.viewtool; +import com.dotcms.IntegrationTestBase; import com.dotcms.ai.AiTest; import com.dotcms.ai.app.AppConfig; import com.dotcms.ai.app.ConfigService; @@ -23,6 +24,7 @@ import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; import static org.mockito.Mockito.mock; /** @@ -33,7 +35,7 @@ * * @author vico */ -public class AIViewToolTest { +public class AIViewToolTest extends IntegrationTestBase { private static AppConfig config; private static WireMockServer wireMockServer; @@ -129,6 +131,22 @@ public void test_generateImage_fromMapPrompt() { assertImageResponse(response, prompt.get("prompt").toString(), "dalailama"); } + /** + * Scenario: No license + * When initializing the AIView tool + * Then should NOT throw exception + */ + @Test + public void test_noLicense_initShouldNotFail() throws Exception { + runNoLicense(()-> { + try { + aiViewTool.init(mock(ViewContext.class)); + } catch (Exception e) { + fail("Should not throw exception"); + } + }); + } + private void assertTextResponse(final JSONObject response, final String containedText) { assertNotNull(response); assertTrue(response.containsKey("choices"));