|
| 1 | +package org.openmrs.module.reporting.evaluation; |
| 2 | + |
| 3 | +import java.util.List; |
| 4 | + |
| 5 | +import org.apache.logging.log4j.Level; |
| 6 | +import org.apache.logging.log4j.LogManager; |
| 7 | +import org.apache.logging.log4j.core.LoggerContext; |
| 8 | +import org.apache.logging.log4j.core.config.AppenderRef; |
| 9 | +import org.apache.logging.log4j.core.config.Configuration; |
| 10 | +import org.apache.logging.log4j.core.config.LoggerConfig; |
| 11 | +import org.apache.logging.log4j.core.layout.PatternLayout; |
| 12 | +import org.junit.AfterClass; |
| 13 | +import org.junit.Assert; |
| 14 | +import org.junit.Before; |
| 15 | +import org.junit.BeforeClass; |
| 16 | +import org.junit.Test; |
| 17 | +import org.openmrs.api.context.Context; |
| 18 | +import org.openmrs.logging.MemoryAppender; |
| 19 | +import org.openmrs.logging.OpenmrsLoggingUtil; |
| 20 | +import org.openmrs.module.reporting.cohort.definition.GenderCohortDefinition; |
| 21 | +import org.openmrs.module.reporting.indicator.CohortIndicator; |
| 22 | +import org.openmrs.module.reporting.indicator.service.IndicatorService; |
| 23 | +import org.openmrs.module.reporting.test.OpenmrsVersionTestListener; |
| 24 | +import org.openmrs.module.reporting.test.RequiresVersion; |
| 25 | +import org.openmrs.test.BaseModuleContextSensitiveTest; |
| 26 | +import org.openmrs.util.OpenmrsUtil; |
| 27 | +import org.springframework.test.context.TestExecutionListeners; |
| 28 | + |
| 29 | +/** |
| 30 | + * Tests for {@link EvaluationProfiler} |
| 31 | + */ |
| 32 | +@TestExecutionListeners(OpenmrsVersionTestListener.class) |
| 33 | +@RequiresVersion("2.4.* - 2.*") |
| 34 | +public class EvaluationProfilerTest24On extends BaseModuleContextSensitiveTest { |
| 35 | + |
| 36 | + protected EvaluationProfiler profiler1, profiler2; |
| 37 | + |
| 38 | + private static MemoryAppender appender; |
| 39 | + |
| 40 | + @BeforeClass |
| 41 | + public static void beforeClass() { |
| 42 | + appender = OpenmrsLoggingUtil.getMemoryAppender(); |
| 43 | + |
| 44 | + if (appender == null) { |
| 45 | + LoggerContext context = (LoggerContext) LogManager.getContext(false); |
| 46 | + Configuration config = context.getConfiguration(); |
| 47 | + |
| 48 | + appender = MemoryAppender.newBuilder().setName("MEMORY_APPENDER").setLayout(PatternLayout.createDefaultLayout()) |
| 49 | + .setConfiguration(config).build(); |
| 50 | + appender.start(); |
| 51 | + |
| 52 | + config.addAppender(appender); |
| 53 | + |
| 54 | + AppenderRef appenderRef = AppenderRef.createAppenderRef("MEMORY_APPENDER", Level.ALL, null); |
| 55 | + LoggerConfig loggerConfig = LoggerConfig.createLogger(false, Level.ALL, EvaluationProfiler.class.getName(), null, |
| 56 | + new AppenderRef[] { appenderRef }, null, config, null); |
| 57 | + loggerConfig.addAppender(appender, Level.ALL, null); |
| 58 | + config.addLogger(EvaluationProfiler.class.getName(), loggerConfig); |
| 59 | + context.updateLoggers(); |
| 60 | + } |
| 61 | + |
| 62 | + } |
| 63 | + |
| 64 | + @AfterClass |
| 65 | + public static void afterClass() { |
| 66 | + ((LoggerContext) LogManager.getContext()).updateLoggers(); |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * Setup each test by configuring AOP on the relevant services and logging for the profiler class |
| 71 | + */ |
| 72 | + @Before |
| 73 | + public void setup() { |
| 74 | + profiler1 = new EvaluationProfiler(new EvaluationContext()); |
| 75 | + profiler2 = new EvaluationProfiler(new EvaluationContext()); |
| 76 | + } |
| 77 | + |
| 78 | + @Test |
| 79 | + public void integration() throws EvaluationException { |
| 80 | + GenderCohortDefinition males = new GenderCohortDefinition(); |
| 81 | + males.setName("males"); |
| 82 | + males.setMaleIncluded(true); |
| 83 | + |
| 84 | + CohortIndicator count = new CohortIndicator(); // No name, log message should use "?" |
| 85 | + count.setCohortDefinition(males, ""); |
| 86 | + |
| 87 | + Context.getService(IndicatorService.class).evaluate(count, null); |
| 88 | + |
| 89 | + List<String> split = appender.getLogLines(); |
| 90 | + Assert.assertEquals(6, split.size()); |
| 91 | + Assert.assertTrue(split.get(0).contains("EVALUATION_STARTED")); |
| 92 | + Assert.assertTrue(split.get(1).contains(">")); |
| 93 | + Assert.assertTrue(split.get(1).contains("CohortIndicator")); |
| 94 | + Assert.assertTrue(split.get(2).contains(">>")); |
| 95 | + Assert.assertTrue(split.get(2).contains("GenderCohortDefinition[males]")); |
| 96 | + Assert.assertTrue(split.get(5).contains("EVALUATION_COMPLETED")); |
| 97 | + } |
| 98 | +} |
0 commit comments