|
| 1 | +/* |
| 2 | + * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved. |
| 3 | + * |
| 4 | + * Licensed under the Universal Permissive License v 1.0 as shown at http://oss.oracle.com/licenses/upl. |
| 5 | + */ |
| 6 | + |
| 7 | +package weblogic.logging.exporter.config; |
| 8 | + |
| 9 | +import static org.junit.jupiter.api.Assertions.*; |
| 10 | + |
| 11 | +import java.io.ByteArrayOutputStream; |
| 12 | +import java.io.File; |
| 13 | +import java.io.PrintStream; |
| 14 | +import java.util.ArrayList; |
| 15 | +import org.junit.jupiter.api.AfterEach; |
| 16 | +import org.junit.jupiter.api.BeforeEach; |
| 17 | +import org.junit.jupiter.api.DisplayName; |
| 18 | +import org.junit.jupiter.api.Test; |
| 19 | + |
| 20 | +@DisplayName("Test the Config class") |
| 21 | +public class ConfigTest { |
| 22 | + |
| 23 | + private final ByteArrayOutputStream outContent = new ByteArrayOutputStream(); |
| 24 | + private final ByteArrayOutputStream errContent = new ByteArrayOutputStream(); |
| 25 | + private final PrintStream originalOut = System.out; |
| 26 | + private final PrintStream originalErr = System.err; |
| 27 | + |
| 28 | + private static final String EXPECTED_STRING = |
| 29 | + "Config{weblogicLoggingIndexName='index1', publishHost='host1', " |
| 30 | + + "publishPort=1234, weblogicLoggingExporterSeverity='Warning', " |
| 31 | + + "weblogicLoggingExporterBulkSize='2', enabled=false, " |
| 32 | + + "weblogicLoggingExporterFilters=[FilterConfig{expression='MSGID != 'BEA-000449'', " |
| 33 | + + "servers=[]}], domainUID='domain1'}"; |
| 34 | + |
| 35 | + @BeforeEach |
| 36 | + public void setUpStreams() { |
| 37 | + System.setOut(new PrintStream(outContent)); |
| 38 | + System.setErr(new PrintStream(errContent)); |
| 39 | + } |
| 40 | + |
| 41 | + @AfterEach |
| 42 | + public void restoreStreams() { |
| 43 | + System.setOut(originalOut); |
| 44 | + System.setErr(originalErr); |
| 45 | + } |
| 46 | + |
| 47 | + @DisplayName("Create the default config from an empty file") |
| 48 | + @Test |
| 49 | + public void createDefaultConfigFromEmptyFile() { |
| 50 | + // create config by loading an empty file |
| 51 | + Config config = Config.loadConfig(new File("src/test/resources/emptyConfig.yaml")); |
| 52 | + |
| 53 | + // now check that the config contains the expected default values |
| 54 | + assertAll( |
| 55 | + "config", |
| 56 | + () -> assertEquals("localhost", config.getHost()), |
| 57 | + () -> assertEquals(9200, config.getPort()), |
| 58 | + () -> assertEquals(true, config.getEnabled()), |
| 59 | + () -> assertEquals("wls", config.getIndexName()), |
| 60 | + () -> assertEquals(null, config.getSeverity()), |
| 61 | + () -> assertTrue(config.getFilterConfigs() instanceof ArrayList), |
| 62 | + () -> assertEquals(0, config.getFilterConfigs().size()), |
| 63 | + () -> assertEquals(1, config.getBulkSize()), |
| 64 | + () -> assertEquals("unknown", config.getDomainUID())); |
| 65 | + } |
| 66 | + |
| 67 | + @DisplayName("Create config from file") |
| 68 | + @Test |
| 69 | + public void createConfigFromFile() { |
| 70 | + // create config by loading an empty file |
| 71 | + Config config = Config.loadConfig(new File("src/test/resources/config1.yaml")); |
| 72 | + |
| 73 | + // now check that the config contains the expected values |
| 74 | + assertAll( |
| 75 | + "config", |
| 76 | + () -> assertEquals("host1", config.getHost()), |
| 77 | + () -> assertEquals(1234, config.getPort()), |
| 78 | + () -> assertEquals(false, config.getEnabled()), |
| 79 | + () -> assertEquals("index1", config.getIndexName()), |
| 80 | + () -> assertEquals("Warning", config.getSeverity()), |
| 81 | + () -> assertTrue(config.getFilterConfigs() instanceof ArrayList), |
| 82 | + () -> assertEquals(1, config.getFilterConfigs().size()), |
| 83 | + () -> |
| 84 | + assertEquals( |
| 85 | + "FilterConfig{expression='MSGID != 'BEA-000449'', servers=[]}", |
| 86 | + config.getFilterConfigs().get(0).toString()), |
| 87 | + () -> assertEquals(2, config.getBulkSize()), |
| 88 | + () -> assertEquals("domain1", config.getDomainUID())); |
| 89 | + } |
| 90 | + |
| 91 | + @DisplayName("Config file does not exist") |
| 92 | + @Test |
| 93 | + public void configFileDoesNotExist() { |
| 94 | + Config config = Config.loadConfig(new File("src/test/resources/no-such-file.yaml")); |
| 95 | + assertTrue(outContent.toString().contains(("Not Found"))); |
| 96 | + assertTrue(outContent.toString().contains("Using default for all parameters")); |
| 97 | + } |
| 98 | + |
| 99 | + @DisplayName("Config file cannot be parsed") |
| 100 | + @Test |
| 101 | + public void configFileCannotBeParsed() { |
| 102 | + Config config = Config.loadConfig(new File("src/test/resources/bad.yaml")); |
| 103 | + assertTrue(outContent.toString().contains(("Error parsing configuration file"))); |
| 104 | + assertTrue(outContent.toString().contains("Using default for all parameters")); |
| 105 | + } |
| 106 | + |
| 107 | + @DisplayName("Should convert index name to lower case") |
| 108 | + @Test |
| 109 | + public void shouldConvertIndexNameToLowerCase() { |
| 110 | + // create config by loading an empty file |
| 111 | + Config config = Config.loadConfig(new File("src/test/resources/config2.yaml")); |
| 112 | + |
| 113 | + // now check that the config contains the expected values |
| 114 | + assertEquals("index2", config.getIndexName()); |
| 115 | + } |
| 116 | + |
| 117 | + @DisplayName("Check the toString() method works as expected") |
| 118 | + @Test |
| 119 | + public void checkToStringWorksAsExpected() { |
| 120 | + Config config = Config.loadConfig(new File("src/test/resources/config1.yaml")); |
| 121 | + assertEquals(EXPECTED_STRING, config.toString()); |
| 122 | + } |
| 123 | +} |
0 commit comments