/*
 * Copyright 2015 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.data.rest.core.mapping;

import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;
import static org.springframework.data.rest.core.mapping.RepositoryDetectionStrategy.RepositoryDetectionStrategies.*;

import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

import org.junit.Test;
import org.springframework.data.repository.Repository;
import org.springframework.data.repository.core.support.DefaultRepositoryMetadata;
import org.springframework.data.rest.core.annotation.RepositoryRestResource;
import org.springframework.data.rest.core.mapping.RepositoryDetectionStrategy.RepositoryDetectionStrategies;

/**
 * Unit tests for {@link RepositoryDetectionStrategies}.
 * 
 * @author Oliver Gierke
 * @soundtrack Katinka - Ausverkauf
 */
@SuppressWarnings("serial")
public class RepositoryDetectionStrategiesUnitTests {

	/**
	 * @see DATAREST-473
	 */
	@Test
	public void allExposesAllRepositories() {

		assertExposures(ALL, new HashMap<Class<?>, Boolean>() {
			{
				put(AnnotatedRepository.class, true);
				put(HiddenRepository.class, true);
				put(PublicRepository.class, true);
				put(PackageProtectedRepository.class, true);
			}
		});
	}

	/**
	 * @see DATAREST-473
	 */
	@Test
	public void defaultHonorsVisibilityAndAnnotations() {

		assertExposures(DEFAULT, new HashMap<Class<?>, Boolean>() {
			{
				put(AnnotatedRepository.class, true);
				put(HiddenRepository.class, false);
				put(PublicRepository.class, true);
				put(PackageProtectedRepository.class, false);
			}
		});
	}

	/**
	 * @see DATAREST-473
	 */
	@Test
	public void visibilityHonorsTypeVisibilityOnly() {

		assertExposures(VISIBILITY, new HashMap<Class<?>, Boolean>() {
			{
				put(AnnotatedRepository.class, false);
				put(HiddenRepository.class, true);
				put(PublicRepository.class, true);
				put(PackageProtectedRepository.class, false);
			}
		});
	}

	/**
	 * @see DATAREST-473
	 */

	@Test
	public void annotatedHonorsAnnotationsOnly() {

		assertExposures(ANNOTATED, new HashMap<Class<?>, Boolean>() {
			{
				put(AnnotatedRepository.class, true);
				put(HiddenRepository.class, false);
				put(PublicRepository.class, false);
				put(PackageProtectedRepository.class, false);
			}
		});
	}

	private static void assertExposures(RepositoryDetectionStrategy strategy, Map<Class<?>, Boolean> expected) {

		for (Entry<Class<?>, Boolean> entry : expected.entrySet()) {
			assertThat(strategy.isExported(new DefaultRepositoryMetadata(entry.getKey())), is(entry.getValue()));
		}
	}

	interface PackageProtectedRepository extends Repository<Object, Long> {}

	public interface PublicRepository extends Repository<Object, Long> {}

	@RepositoryRestResource
	interface AnnotatedRepository extends Repository<Object, Long> {}

	@RepositoryRestResource(exported = false)
	public interface HiddenRepository extends Repository<Object, Long> {}
}
