Skip to content

Commit

Permalink
Change to use passed-in property for applicationMonitor value
Browse files Browse the repository at this point in the history
  • Loading branch information
evie-lau committed Aug 7, 2023
1 parent efd5357 commit cc715a7
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> APP_MON_VALUE_LIST = Arrays.asList("polled", "mbean", "disabled");

String tool = null;

HashMap<String, String> 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();
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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("<applicationMonitor updateTrigger=\"${io.openliberty.tools.update.trigger}\"/>"));
assertTrue(content.contains("<applicationMonitor updateTrigger=\"mbean\"/>"));
}

@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());
}
}

0 comments on commit cc715a7

Please sign in to comment.