diff --git a/src/main/java/io/openliberty/tools/common/plugins/config/ServerConfigDocument.java b/src/main/java/io/openliberty/tools/common/plugins/config/ServerConfigDocument.java index 054b352a..386c0e3e 100644 --- a/src/main/java/io/openliberty/tools/common/plugins/config/ServerConfigDocument.java +++ b/src/main/java/io/openliberty/tools/common/plugins/config/ServerConfigDocument.java @@ -44,6 +44,7 @@ import javax.xml.xpath.XPathExpressionException; import javax.xml.xpath.XPathFactory; +import io.openliberty.tools.common.plugins.util.LibertyPropFilesUtility; import io.openliberty.tools.common.plugins.util.PluginExecutionException; import org.apache.commons.io.comparator.NameFileComparator; import org.w3c.dom.Document; @@ -159,10 +160,19 @@ public ServerConfigDocument(CommonLoggerI log, File originalServerXMLFile, Map + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * http://www.apache.org/licenses/LICENSE-2.0 + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package io.openliberty.tools.common.plugins.util; + +import io.openliberty.tools.common.CommonLoggerI; + +import java.io.File; +import java.io.IOException; +import java.util.Collections; +import java.util.HashMap; +import java.util.Map; + +public class LibertyPropFilesUtility { + + + public static Map getLibertyDirectoryPropertyFiles(CommonLoggerI log, File installDir, File userDir, File serverDir) { + Map libertyDirectoryPropertyToFile = new HashMap<>(); + + if (serverDir.exists()) { + try { + libertyDirectoryPropertyToFile.put(ServerFeatureUtil.SERVER_CONFIG_DIR, serverDir.getCanonicalFile()); + + libertyDirectoryPropertyToFile.put(ServerFeatureUtil.WLP_INSTALL_DIR, installDir.getCanonicalFile()); + + libertyDirectoryPropertyToFile.put(ServerFeatureUtil.WLP_USER_DIR, userDir.getCanonicalFile()); + + File userExtDir = new File(userDir, "extension"); + libertyDirectoryPropertyToFile.put(ServerFeatureUtil.USR_EXTENSION_DIR, userExtDir.getCanonicalFile()); + + File userSharedDir = new File(userDir, "shared"); + File userSharedAppDir = new File(userSharedDir, "app"); + File userSharedConfigDir = new File(userSharedDir, "config"); + File userSharedResourcesDir = new File(userSharedDir, "resources"); + File userSharedStackGroupsDir = new File(userSharedDir, "stackGroups"); + + libertyDirectoryPropertyToFile.put(ServerFeatureUtil.SHARED_APP_DIR, userSharedAppDir.getCanonicalFile()); + libertyDirectoryPropertyToFile.put(ServerFeatureUtil.SHARED_CONFIG_DIR, userSharedConfigDir.getCanonicalFile()); + libertyDirectoryPropertyToFile.put(ServerFeatureUtil.SHARED_RESOURCES_DIR, userSharedResourcesDir.getCanonicalFile()); + libertyDirectoryPropertyToFile.put(ServerFeatureUtil.SHARED_STACKGROUP_DIR, userSharedStackGroupsDir.getCanonicalFile()); + + return libertyDirectoryPropertyToFile; + } catch (Exception e) { + log.warn("The properties for directories could not be initialized because an error occurred when accessing the directories."); + log.debug("Exception received: " + e.getMessage(), e); + } + } else { + log.warn("The " + serverDir + " directory cannot be accessed. The properties for directories could not be initialized."); + } + return Collections.emptyMap(); + } + + +} diff --git a/src/test/java/io/openliberty/tools/common/config/ServerConfigDocumentOverridesTest.java b/src/test/java/io/openliberty/tools/common/config/ServerConfigDocumentOverridesTest.java index 126c3980..d9a3888c 100644 --- a/src/test/java/io/openliberty/tools/common/config/ServerConfigDocumentOverridesTest.java +++ b/src/test/java/io/openliberty/tools/common/config/ServerConfigDocumentOverridesTest.java @@ -268,4 +268,13 @@ public void testProcessServerXmlWithMultipleSpringBootNodes() throws IOException assertThrows("Found multiple springBootApplication elements specified in the server configuration. Only one springBootApplication can be configured per Liberty server.", PluginExecutionException.class, () -> new ServerConfigDocument(new TestLogger(), null, libertyDirPropMap)); } + + @Test + public void testServerConfigDocumentForLCLS() throws IOException, PluginExecutionException { + ServerConfigDocument configDocument = new ServerConfigDocument(new TestLogger(), null, + WLP_DIR.toFile(),WLP_USER_DIR.toFile(),SERVER_CONFIG_DIR.toFile()); + assertEquals("ServerConfigDocument Liberty Property file map is not created properly ", 8, configDocument.getLibertyDirPropertyFiles().size()); + assertEquals("ServerConfigDocument http.port variable is not assigned properly ", "1111", configDocument.getProperties().getProperty("http.port")); + assertEquals("ServerConfigDocument includeLocation default property is not assigned properly ", "includes", configDocument.getDefaultProperties().getProperty("includeLocation")); + } } \ No newline at end of file diff --git a/src/test/java/io/openliberty/tools/common/plugins/util/LibertyPropFilesUtilityTest.java b/src/test/java/io/openliberty/tools/common/plugins/util/LibertyPropFilesUtilityTest.java new file mode 100644 index 00000000..95084db9 --- /dev/null +++ b/src/test/java/io/openliberty/tools/common/plugins/util/LibertyPropFilesUtilityTest.java @@ -0,0 +1,85 @@ +/** + * (C) Copyright IBM Corporation 2024. + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * http://www.apache.org/licenses/LICENSE-2.0 + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package io.openliberty.tools.common.plugins.util; + +import io.openliberty.tools.common.TestLogger; +import org.junit.Before; +import org.junit.Test; + +import java.io.File; +import java.io.IOException; +import java.util.Map; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +public class LibertyPropFilesUtilityTest { + + File serverDir; + File installDir; + File userDir; + File userExtensionDir; + File sharedResourceDir; + File sharedStackGroupDir; + File sharedAppDir; + File sharedConfigDir; + + @Before + public void setUp() throws IOException { + serverDir = new File("src/test/resources/serverConfig/liberty/wlp/usr/servers/defaultServer"); + installDir = new File("src/test/resources/serverConfig/liberty/wlp"); + userDir = new File("src/test/resources/serverConfig/liberty/wlp/usr"); + userExtensionDir = new File("src/test/resources/serverConfig/liberty/wlp/usr/extension"); + sharedResourceDir = new File("src/test/resources/serverConfig/liberty/wlp/usr/shared/resources"); + sharedStackGroupDir = new File("src/test/resources/serverConfig/liberty/wlp/usr/shared/stackGroups"); + sharedAppDir = new File("src/test/resources/serverConfig/liberty/wlp/usr/shared/app"); + sharedConfigDir = new File("src/test/resources/serverConfig/liberty/wlp/usr/shared/config"); + } + + @Test + public void testGetLibertyDirectoryPropertyFiles() throws Exception { + + Map libProperties = LibertyPropFilesUtility.getLibertyDirectoryPropertyFiles(new TestLogger(), installDir, userDir, serverDir); + // verify the libPropFiles + assertFalse("Liberty Directory Property files map should not be empty", libProperties.isEmpty()); + assertEquals(libProperties.get(ServerFeatureUtil.WLP_INSTALL_DIR).getCanonicalPath(), installDir.getCanonicalPath()); + assertEquals(libProperties.get(ServerFeatureUtil.WLP_USER_DIR).getCanonicalPath(), userDir.getCanonicalPath()); + assertEquals(libProperties.get(ServerFeatureUtil.SERVER_CONFIG_DIR).getCanonicalPath(), serverDir.getCanonicalPath()); + assertEquals(libProperties.get(ServerFeatureUtil.USR_EXTENSION_DIR).getCanonicalPath(), userExtensionDir.getCanonicalPath()); + assertEquals(libProperties.get(ServerFeatureUtil.SHARED_CONFIG_DIR).getCanonicalPath(), sharedConfigDir.getCanonicalPath()); + assertEquals(libProperties.get(ServerFeatureUtil.SHARED_APP_DIR).getCanonicalPath(), sharedAppDir.getCanonicalPath()); + assertEquals(libProperties.get(ServerFeatureUtil.SHARED_RESOURCES_DIR).getCanonicalPath(), sharedResourceDir.getCanonicalPath()); + assertEquals(libProperties.get(ServerFeatureUtil.SHARED_STACKGROUP_DIR).getCanonicalPath(), sharedStackGroupDir.getCanonicalPath()); + + } + + @Test + public void testGetLibertyDirectoryPropertyFilesEmptyResult() throws Exception { + + Map libProperties = LibertyPropFilesUtility.getLibertyDirectoryPropertyFiles(new TestLogger(), installDir, userDir, + new File("src/test/resources/invalidPath")); + // should be empty because serverDir does not exist + assertTrue("Liberty Directory Property files map should be empty since invalid serverDirectory is specified", + libProperties.isEmpty()); + + libProperties = LibertyPropFilesUtility.getLibertyDirectoryPropertyFiles(new TestLogger(), installDir, new File("src/test/resources/invalidPath\u0000"), serverDir); + + // verify the libPropFiles + assertTrue("Liberty Directory Property files map should be empty since invalid userDirectory is specified", + libProperties.isEmpty()); + } +} diff --git a/src/test/resources/servers/springBootApplicationTest/server.xml b/src/test/resources/servers/springBootApplicationTest/server.xml index 12f0287c..2e53c904 100644 --- a/src/test/resources/servers/springBootApplicationTest/server.xml +++ b/src/test/resources/servers/springBootApplicationTest/server.xml @@ -14,6 +14,6 @@ location="guide-spring-boot-0.1.0.jar" name="guide-spring-boot" />