/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements. See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You 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
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * 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 org.apache.logging.log4j.core.filter;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.core.Appender;
import org.apache.logging.log4j.core.Filter;
import org.apache.logging.log4j.core.LoggerContext;
import org.apache.logging.log4j.core.config.Configuration;
import org.apache.logging.log4j.core.config.Configurator;
import org.apache.logging.log4j.core.util.KeyValuePair;
import org.apache.logging.log4j.message.MapMessage;
import org.apache.logging.log4j.status.StatusLogger;
import org.apache.logging.log4j.test.appender.ListAppender;
import org.junit.After;
import org.junit.Test;

import static org.junit.Assert.*;

/**
 *
 */
public class MapFilterTest {

    @After
    public void cleanup() {
        final LoggerContext ctx = LoggerContext.getContext(false);
        ctx.reconfigure();
        StatusLogger.getLogger().reset();
    }

    @Test
    public void testFilter() {
        final KeyValuePair[] pairs = new KeyValuePair[] { new KeyValuePair("FromAccount", "211000"),
                                                    new KeyValuePair("ToAccount", "123456")};
        MapFilter filter = MapFilter.createFilter(pairs, "and", null, null);
        filter.start();
        MapMessage msg = new MapMessage();
        msg.put("ToAccount", "123456");
        msg.put("FromAccount", "211000");
        msg.put("Amount", "1000.00");
        assertTrue(filter.isStarted());
        assertSame(Filter.Result.NEUTRAL, filter.filter(null, Level.DEBUG, null, msg, null));
        msg.put("ToAccount", "111111");
        assertSame(Filter.Result.DENY, filter.filter(null, Level.ERROR, null, msg, null));
        filter = MapFilter.createFilter(pairs, "or", null, null);
        filter.start();
        msg = new MapMessage();
        msg.put("ToAccount", "123456");
        msg.put("FromAccount", "211000");
        msg.put("Amount", "1000.00");
        assertTrue(filter.isStarted());
        assertSame(Filter.Result.NEUTRAL, filter.filter(null, Level.DEBUG, null, msg, null));
        msg.put("ToAccount", "111111");
        assertSame(Filter.Result.NEUTRAL, filter.filter(null, Level.ERROR, null, msg, null));
    }

    @Test
    public void testConfig() {
        final LoggerContext ctx = Configurator.initialize("Test1", "target/test-classes/log4j2-mapfilter.xml");
        final Configuration config = ctx.getConfiguration();
        final Filter filter = config.getFilter();
        assertNotNull("No MapFilter", filter);
        assertTrue("Not a MapFilter", filter instanceof  MapFilter);
        final MapFilter mapFilter = (MapFilter) filter;
        assertFalse("Should not be And filter", mapFilter.isAnd());
        final Map<String, List<String>> map = mapFilter.getMap();
        assertNotNull("No Map", map);
        assertFalse("No elements in Map", map.isEmpty());
        assertEquals("Incorrect number of elements in Map", 1, map.size());
        assertTrue("Map does not contain key eventId", map.containsKey("eventId"));
        assertEquals("List does not contain 2 elements", 2, map.get("eventId").size());
        final Logger logger = LogManager.getLogger(MapFilterTest.class);
        final Map<String, String> eventMap = new HashMap<>();
        eventMap.put("eventId", "Login");
        logger.debug(new MapMessage(eventMap));
        final Appender app = config.getAppender("LIST");
        assertNotNull("No List appender", app);
        final List<String> msgs = ((ListAppender) app).getMessages();
        assertNotNull("No messages", msgs);
        assertFalse("No messages", msgs.isEmpty());


    }
}

@@ -1077,113 +1077,14 @@
 ore.
-Appender;%0Aimport org.apache.logging.log4j.core.Filter;%0Aimport org.apache.logging.log4j.core.LoggerContext
+Filter
 ;%0Aim
@@ -1181,26 +1181,24 @@
 ore.
-config.Configurato
+util.KeyValuePai
 r;%0Ai
@@ -1228,38 +1228,39 @@
 g.log4j.
-core.util.KeyValuePair
+junit.LoggerContextRule
 ;%0Aimport
@@ -1309,61 +1309,8 @@
 ge;%0A
-import org.apache.logging.log4j.status.StatusLogger;%0A
 impo
@@ -1382,21 +1382,25 @@
 g.junit.
-After
+ClassRule
 ;%0Aimport
@@ -1502,13 +1502,17 @@
    @
-After
+ClassRule
 %0A   
@@ -1523,166 +1523,89 @@
 lic 
-void cleanup() %7B%0A        final LoggerContext ctx = LoggerContext.getContext(false);%0A        ctx.reconfigure();%0A        StatusLogger.getLogger().reset();%0A    %7D
+static LoggerContextRule context = new LoggerContextRule(%22log4j2-mapfilter.xml%22);
 %0A%0A  
@@ -2929,120 +2929,8 @@
 ) %7B%0A
-        final LoggerContext ctx = Configurator.initialize(%22Test1%22, %22target/test-classes/log4j2-mapfilter.xml%22);%0A
     
@@ -2967,10 +2967,14 @@
  = c
+ontex
 t
-x
 .get
@@ -3929,16 +3929,20 @@
   final 
+List
 Appender
@@ -3951,22 +3951,27 @@
 pp = con
-fig.ge
+text.getLis
 tAppende
@@ -3985,56 +3985,8 @@
 %22);%0A
-        assertNotNull(%22No List appender%22, app);%0A
     
@@ -4018,29 +4018,12 @@
 gs =
- ((ListAppender)
  app
-)
 .get

Simplify MapFilterTest using LoggerContextRule.