From cc715a7370f8f31d06ab691b9ebf55990cb1ec6b Mon Sep 17 00:00:00 2001 From: Evie Lau Date: Mon, 7 Aug 2023 14:14:57 -0500 Subject: [PATCH] Change to use passed-in property for applicationMonitor value --- .../ApplicationMonitorConfigXmlDocument.java | 19 ++++++++++-- .../plugins/config/ServerConfigXmlTest.java | 29 +++++++++++++++++-- 2 files changed, 44 insertions(+), 4 deletions(-) diff --git a/src/main/java/io/openliberty/tools/common/plugins/config/ApplicationMonitorConfigXmlDocument.java b/src/main/java/io/openliberty/tools/common/plugins/config/ApplicationMonitorConfigXmlDocument.java index e8fa826e..a58394c4 100644 --- a/src/main/java/io/openliberty/tools/common/plugins/config/ApplicationMonitorConfigXmlDocument.java +++ b/src/main/java/io/openliberty/tools/common/plugins/config/ApplicationMonitorConfigXmlDocument.java @@ -17,23 +17,38 @@ import java.io.File; import java.io.IOException; +import java.util.Arrays; import java.util.HashMap; +import java.util.List; import javax.xml.parsers.ParserConfigurationException; import javax.xml.transform.TransformerException; +import io.openliberty.tools.common.plugins.util.PluginExecutionException; + public class ApplicationMonitorConfigXmlDocument extends XmlDocument { public static final String APPLICATION_CONFIG_XML_FILENAME = "liberty-plugin-app-monitor-config.xml"; + private static List APP_MON_VALUE_LIST = Arrays.asList("polled", "mbean", "disabled"); + String tool = null; HashMap attributes = new HashMap<>(); public ApplicationMonitorConfigXmlDocument(String tool) { this.tool = tool; - attributes.put("updateTrigger", "${io.openliberty.tools.update.trigger}"); } - public void writeAppMonitorConfigXmlDocument(File serverDirectory) throws IOException, TransformerException, ParserConfigurationException { + public void writeAppMonitorConfigXmlDocument(File serverDirectory, String applicationMonitorValue) throws IOException, TransformerException, ParserConfigurationException, PluginExecutionException { + // if applicationMonitor not set, return + if (applicationMonitorValue == null) { + return; + } + // continue with creating configDropins/override file + if (!APP_MON_VALUE_LIST.contains(applicationMonitorValue)) { + throw new PluginExecutionException("applicationMonitor value \"" + applicationMonitorValue + "\" is not supported. Must be one of: " + APP_MON_VALUE_LIST); + } + attributes.put("updateTrigger", applicationMonitorValue); + File appMonXml = getAppMonitorConfigXmlFile(serverDirectory); if (!appMonXml.getParentFile().exists()) { appMonXml.getParentFile().mkdirs(); diff --git a/src/test/java/io/openliberty/tools/common/plugins/config/ServerConfigXmlTest.java b/src/test/java/io/openliberty/tools/common/plugins/config/ServerConfigXmlTest.java index 1ca450b0..0811a0d7 100644 --- a/src/test/java/io/openliberty/tools/common/plugins/config/ServerConfigXmlTest.java +++ b/src/test/java/io/openliberty/tools/common/plugins/config/ServerConfigXmlTest.java @@ -1,5 +1,6 @@ package io.openliberty.tools.common.plugins.config; +import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; import java.io.File; @@ -14,11 +15,35 @@ public void createAppMonitorConfigTest() throws Exception { File serverDirectory = new File(serverConfigDir, "liberty/wlp/usr/servers/defaultServer"); ApplicationMonitorConfigXmlDocument appMonXml = new ApplicationMonitorConfigXmlDocument("test"); - appMonXml.writeAppMonitorConfigXmlDocument(serverDirectory); + appMonXml.writeAppMonitorConfigXmlDocument(serverDirectory, "mbean"); File newFile = appMonXml.getAppMonitorConfigXmlFile(serverDirectory); String content = Files.readString(newFile.toPath()); assertTrue(content.contains("Generated by liberty-test-plugin")); - assertTrue(content.contains("")); + assertTrue(content.contains("")); + } + + @Test + public void createAppMonitorConfigWithInvalidValueTest() throws Exception { + File serverDirectory = new File(serverConfigDir, "liberty/wlp/usr/servers/defaultServer"); + + ApplicationMonitorConfigXmlDocument appMonXml = new ApplicationMonitorConfigXmlDocument("test"); + try { + appMonXml.writeAppMonitorConfigXmlDocument(serverDirectory, "asdf"); + } catch (Exception e) { + assertTrue(e.getMessage().contains("applicationMonitor value")); + assertTrue(e.getMessage().contains("is not supported")); + } + } + + @Test + public void createAppMonitorConfigWithoutValueTest() throws Exception { + File serverDirectory = new File(serverConfigDir, "liberty/wlp/usr/servers/defaultServer"); + + ApplicationMonitorConfigXmlDocument appMonXml = new ApplicationMonitorConfigXmlDocument("test"); + appMonXml.writeAppMonitorConfigXmlDocument(serverDirectory, null); + + File newFile = appMonXml.getAppMonitorConfigXmlFile(serverDirectory); + assertFalse(newFile.exists()); } }