Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Var processing lcls #465

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -159,10 +160,19 @@ public ServerConfigDocument(CommonLoggerI log, File originalServerXMLFile, Map<S
initializeAppsLocation();
}

// LCLS constructor
// TODO: populate libertyDirectoryPropertyToFile with workspace information
public ServerConfigDocument(CommonLoggerI log) throws PluginExecutionException {
this(log, null, null);

/**
* Constructor for LCLS usage
* @param log logger instance
* @param originalServerXMLFile original xml file
* @param installDirectory install directory file
* @param userDirectory user directory file
* @param serverDirectory server directory file
* @throws PluginExecutionException
* @throws IOException
*/
public ServerConfigDocument(CommonLoggerI log, File originalServerXMLFile, File installDirectory, File userDirectory, File serverDirectory) throws PluginExecutionException, IOException {
this(log, originalServerXMLFile, LibertyPropFilesUtility.getLibertyDirectoryPropertyFiles(log,installDirectory, userDirectory, serverDirectory));
}

// test constructor that takes in initial properties to be called modularly
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/**
* (C) Copyright IBM Corporation 2024
* <p>
* 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
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* 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<String, File> getLibertyDirectoryPropertyFiles(CommonLoggerI log, File installDir, File userDir, File serverDir) {
Map<String, File> 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();
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/**
* (C) Copyright IBM Corporation 2024.
* <p>
* 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
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* 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<String, File> 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<String, File> 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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@
location="guide-spring-boot-0.1.0.jar"
name="guide-spring-boot" />
<springBootApplication id="guide-spring-boot"
location="guide-spring-boot-0.1.0.jar"
location="guide-spring-boot-0.2.0.jar"
name="guide-spring-boot" />
</server>