From 03c4dcff3895e7a795006c7b30cd0325c4368be3 Mon Sep 17 00:00:00 2001 From: Arun Venmany Date: Wed, 27 Nov 2024 12:19:12 +0530 Subject: [PATCH 1/4] adding new lcls constructor Signed-off-by: Arun Venmany --- .../plugins/config/ServerConfigDocument.java | 17 +++- .../plugins/util/LibertyPropFilesUtility.java | 54 +++++++++++++ .../util/LibertyPropFilesUtilityTest.java | 78 +++++++++++++++++++ 3 files changed, 145 insertions(+), 4 deletions(-) create mode 100644 src/main/java/io/openliberty/tools/common/plugins/util/LibertyPropFilesUtility.java create mode 100644 src/test/java/io/openliberty/tools/common/plugins/util/LibertyPropFilesUtilityTest.java 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..8ed59fd4 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,18 @@ public ServerConfigDocument(CommonLoggerI log, File originalServerXMLFile, Map getLibertyDirectoryPropertyFiles(File installDir, File userDir, File serverDir) throws IOException { + Map libertyDirectoryPropertyToFile = new HashMap<>(); + + if (serverDir.exists()) { + 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; + } + + +} 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..ea3771a6 --- /dev/null +++ b/src/test/java/io/openliberty/tools/common/plugins/util/LibertyPropFilesUtilityTest.java @@ -0,0 +1,78 @@ +/** + * (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 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(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(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()); + } +} From 3bf1737d7bdd1bb51da68120e4cd4a5af3813480 Mon Sep 17 00:00:00 2001 From: Arun Venmany Date: Thu, 28 Nov 2024 09:50:10 +0530 Subject: [PATCH 2/4] updating lcls constructor, adding tests for that Signed-off-by: Arun Venmany --- .../common/plugins/config/ServerConfigDocument.java | 5 +++-- .../common/config/ServerConfigDocumentOverridesTest.java | 9 +++++++++ .../servers/springBootApplicationTest/server.xml | 2 +- 3 files changed, 13 insertions(+), 3 deletions(-) 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 8ed59fd4..ce360144 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 @@ -164,14 +164,15 @@ public ServerConfigDocument(CommonLoggerI log, File originalServerXMLFile, Map 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/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" /> From 80d5d396e7c7b7f3253af720dcf648ff6aea1606 Mon Sep 17 00:00:00 2001 From: Arun Venmany Date: Fri, 29 Nov 2024 10:45:08 +0530 Subject: [PATCH 3/4] updating libertyPropsFileUtility Signed-off-by: Arun Venmany --- .../plugins/config/ServerConfigDocument.java | 2 +- .../plugins/util/LibertyPropFilesUtility.java | 50 ++++++++++++------- .../util/LibertyPropFilesUtilityTest.java | 11 +++- 3 files changed, 41 insertions(+), 22 deletions(-) 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 ce360144..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 @@ -172,7 +172,7 @@ 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. @@ -15,39 +15,51 @@ */ 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(File installDir, File userDir, File serverDir) throws IOException { + public static Map getLibertyDirectoryPropertyFiles(CommonLoggerI log, File installDir, File userDir, File serverDir) throws IOException { Map libertyDirectoryPropertyToFile = new HashMap<>(); if (serverDir.exists()) { - libertyDirectoryPropertyToFile.put(ServerFeatureUtil.SERVER_CONFIG_DIR, serverDir.getCanonicalFile()); + try { + libertyDirectoryPropertyToFile.put(ServerFeatureUtil.SERVER_CONFIG_DIR, serverDir.getCanonicalFile()); + + libertyDirectoryPropertyToFile.put(ServerFeatureUtil.WLP_INSTALL_DIR, installDir.getCanonicalFile()); - libertyDirectoryPropertyToFile.put(ServerFeatureUtil.WLP_INSTALL_DIR, installDir.getCanonicalFile()); + libertyDirectoryPropertyToFile.put(ServerFeatureUtil.WLP_USER_DIR, userDir.getCanonicalFile()); - libertyDirectoryPropertyToFile.put(ServerFeatureUtil.WLP_USER_DIR, userDir.getCanonicalFile()); + File userExtDir = new File(userDir, "extension"); + libertyDirectoryPropertyToFile.put(ServerFeatureUtil.USR_EXTENSION_DIR, userExtDir.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"); - 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()); - 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 libertyDirectoryPropertyToFile; + return Collections.emptyMap(); } 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 index ea3771a6..95084db9 100644 --- a/src/test/java/io/openliberty/tools/common/plugins/util/LibertyPropFilesUtilityTest.java +++ b/src/test/java/io/openliberty/tools/common/plugins/util/LibertyPropFilesUtilityTest.java @@ -15,6 +15,7 @@ */ package io.openliberty.tools.common.plugins.util; +import io.openliberty.tools.common.TestLogger; import org.junit.Before; import org.junit.Test; @@ -52,7 +53,7 @@ public void setUp() throws IOException { @Test public void testGetLibertyDirectoryPropertyFiles() throws Exception { - Map libProperties = LibertyPropFilesUtility.getLibertyDirectoryPropertyFiles(installDir, userDir, serverDir); + 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()); @@ -69,10 +70,16 @@ public void testGetLibertyDirectoryPropertyFiles() throws Exception { @Test public void testGetLibertyDirectoryPropertyFilesEmptyResult() throws Exception { - Map libProperties = LibertyPropFilesUtility.getLibertyDirectoryPropertyFiles(installDir, userDir, + 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()); } } From b4d416c223de2b811aa4c7d0e29f176aa6712638 Mon Sep 17 00:00:00 2001 From: Arun Venmany Date: Fri, 29 Nov 2024 10:48:48 +0530 Subject: [PATCH 4/4] updating libertyPropsFileUtility Signed-off-by: Arun Venmany --- .../tools/common/plugins/util/LibertyPropFilesUtility.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/io/openliberty/tools/common/plugins/util/LibertyPropFilesUtility.java b/src/main/java/io/openliberty/tools/common/plugins/util/LibertyPropFilesUtility.java index a8a821a3..93e08ec2 100644 --- a/src/main/java/io/openliberty/tools/common/plugins/util/LibertyPropFilesUtility.java +++ b/src/main/java/io/openliberty/tools/common/plugins/util/LibertyPropFilesUtility.java @@ -26,7 +26,7 @@ public class LibertyPropFilesUtility { - public static Map getLibertyDirectoryPropertyFiles(CommonLoggerI log, File installDir, File userDir, File serverDir) throws IOException { + public static Map getLibertyDirectoryPropertyFiles(CommonLoggerI log, File installDir, File userDir, File serverDir) { Map libertyDirectoryPropertyToFile = new HashMap<>(); if (serverDir.exists()) {