/*
 * Copyright 2013-2017 the original author or authors.
 *
 * Licensed 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.springframework.cloud.netflix.zuul;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;

import org.junit.Test;
import org.springframework.test.util.ReflectionTestUtils;
import org.springframework.util.ReflectionUtils;

import com.netflix.zuul.FilterLoader;
import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.filters.FilterRegistry;
import com.netflix.zuul.monitoring.CounterFactory;
import com.netflix.zuul.monitoring.TracerFactory;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;

public class ZuulFilterInitializerTests {

	private Map<String, ZuulFilter> filters = getFilters();
	private CounterFactory counterFactory = mock(CounterFactory.class);
	private TracerFactory tracerFactory = mock(TracerFactory.class);
	private FilterLoader filterLoader = new FilterLoader();
	private FilterRegistry filterRegistry = getFilterRegistry();

	private final ZuulFilterInitializer initializer = new ZuulFilterInitializer(filters,
			counterFactory, tracerFactory, filterLoader, filterRegistry);

	@Test
	public void shouldSetupOnContextInitializedEvent() throws Exception {
		initializer.contextInitialized();

		assertEquals(tracerFactory, TracerFactory.instance());
		assertEquals(counterFactory, CounterFactory.instance());
		assertThat(filterRegistry.getAllFilters())
				.containsAll(filters.values());
	}

	@Test
	public void shouldCleanupOnContextDestroyed() throws Exception {
		initializer.contextDestroyed();

		assertEquals(null, ReflectionTestUtils.getField(TracerFactory.class, "INSTANCE"));
		assertEquals(null,
				ReflectionTestUtils.getField(CounterFactory.class, "INSTANCE"));
		assertTrue(FilterRegistry.instance().getAllFilters().isEmpty());
		assertTrue(getHashFiltersByType().isEmpty());
	}

	private Map getHashFiltersByType() {
		Field field = ReflectionUtils.findField(FilterLoader.class, "hashFiltersByType");
		ReflectionUtils.makeAccessible(field);
		return (Map) ReflectionUtils.getField(field, FilterLoader.getInstance());
	}

	private Map<String, ZuulFilter> getFilters() {
		Map<String, ZuulFilter> filters = new HashMap<>();
		filters.put("key1", mock(ZuulFilter.class));
		filters.put("key2", mock(ZuulFilter.class));
		return filters;
	}

	private FilterRegistry getFilterRegistry() {
		try {
			Constructor<FilterRegistry> constructor = FilterRegistry.class
					.getDeclaredConstructor(new Class[0]);
			constructor.setAccessible(true);
			return constructor.newInstance(new Object[0]);
		}
		catch (Exception e) {
			throw new RuntimeException(e);
		}
	}
}
@@ -783,16 +783,41 @@
 l.Map;%0A%0A
+import org.junit.Before;%0A
 import o
@@ -1437,23 +1437,8 @@
 ters
- = getFilters()
 ;%0A%09p
@@ -1477,39 +1477,125 @@
 tory
- = mock(CounterFactory.class)
+;%0A%09private TracerFactory tracerFactory;%0A%09private FilterLoader filterLoader;%0A%09private FilterRegistry filterRegistry
 ;%0A
+%0A
 %09pri
@@ -1599,34 +1599,116 @@
 private 
-TracerFactory trac
+ZuulFilterInitializer initializer;%0A%0A%09@Before%0A%09public void init() %7B%0A%09%09filters = getFilters();%0A%09%09count
 erFactor
@@ -1716,20 +1716,21 @@
  = mock(
-Trac
+Count
 erFactor
@@ -1740,37 +1740,62 @@
 lass);%0A%09
-private FilterLoader 
+%09tracerFactory = mock(TracerFactory.class);%0A%09%09
 filterLo
@@ -1822,39 +1822,17 @@
 der();%0A%09
-private FilterRegistry 
+%09
 filterRe
@@ -1865,46 +1865,10 @@
 ();%0A
-%0A%09private final ZuulFilterInitializer 
+%09%09
 init
@@ -1912,16 +1912,17 @@
 ilters,%0A
+%09
 %09%09%09count
@@ -1979,16 +1979,20 @@
 stry);%0A%0A
+%09%7D%0A%0A
 %09@Test%0A%09

Initialize fields for each test