/*
 * Copyright 2020 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
 *
 *     https://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.geode.boot.autoconfigure.caching;

import static org.assertj.core.api.Assertions.assertThat;

import javax.annotation.Resource;

import org.junit.FixMethodOrder;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.MethodSorters;

import org.apache.geode.cache.Region;
import org.apache.geode.cache.client.ClientRegionShortcut;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.gemfire.config.annotation.EnableCachingDefinedRegions;
import org.springframework.data.gemfire.tests.integration.IntegrationTestsSupport;
import org.springframework.data.gemfire.util.RegionUtils;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.junit4.SpringRunner;

import org.assertj.core.api.Assertions;

import example.app.books.NonBeanType;
import example.app.books.model.Book;
import example.app.books.service.support.CachingBookService;

/**
 * Integration Tests testing the auto-configuration of Spring's Cache Abstraction with Apache Geode
 * as the caching provider.
 *
 * @author John Blum
 * @see org.junit.Test
 * @see org.apache.geode.cache.Region
 * @see org.springframework.boot.autoconfigure.SpringBootApplication
 * @see org.springframework.boot.test.context.SpringBootTest
 * @see org.springframework.data.gemfire.config.annotation.EnableCachingDefinedRegions
 * @see org.springframework.data.gemfire.tests.integration.IntegrationTestsSupport
 * @see org.springframework.geode.boot.autoconfigure.CachingProviderAutoConfiguration
 * @see org.springframework.test.annotation.DirtiesContext
 * @see org.springframework.test.context.junit4.SpringRunner
 * @since 1.0.0
 */
@DirtiesContext
@RunWith(SpringRunner.class)
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.NONE)
@SuppressWarnings("unused")
public class AutoConfiguredCachingIntegrationTests extends IntegrationTestsSupport {

	@Autowired
	private CachingBookService bookService;

	@Resource(name = "CachedBooks")
	private Region<String, Book> cachedBooks;

	private void assertBook(Book book, String title) {

		Assertions.assertThat(book).isNotNull();
		Assertions.assertThat(book.isNew()).isFalse();
		Assertions.assertThat(book.getTitle()).isEqualTo(title);
	}

	@Test
	public void bookServiceWasConfiguredCorrectly() {

		Assertions.assertThat(this.bookService).isNotNull();
		Assertions.assertThat(this.bookService.isCacheMiss()).isFalse();
	}

	@Test
	public void cachedBooksRegionWasConfiguredCorrectly() {

		Assertions.assertThat(this.cachedBooks).isNotNull();
		assertThat(this.cachedBooks.getName()).isEqualTo("CachedBooks");
		assertThat(this.cachedBooks.getFullPath()).isEqualTo(RegionUtils.toRegionPath("CachedBooks"));
		Assertions.assertThat(this.cachedBooks).isEmpty();
	}

	@Test
	public void geodeAsTheCachingProviderWasAutoConfiguredCorrectly() {

		Assertions.assertThat(this.cachedBooks).isEmpty();

		Book bookOne = this.bookService.findByTitle("Star Wars 3 - Revenge of the Sith");

		assertBook(bookOne, "Star Wars 3 - Revenge of the Sith");
		Assertions.assertThat(this.bookService.isCacheMiss()).isTrue();
		Assertions.assertThat(this.cachedBooks).hasSize(1);
		Assertions.assertThat(this.cachedBooks.get(bookOne.getTitle())).isEqualTo(bookOne);

		Book bookOneAgain = this.bookService.findByTitle(bookOne.getTitle());

		Assertions.assertThat(bookOneAgain).isEqualTo(bookOne);
		Assertions.assertThat(this.bookService.isCacheMiss()).isFalse();
		Assertions.assertThat(this.cachedBooks).hasSize(1);
		Assertions.assertThat(this.cachedBooks.get(bookOne.getTitle())).isEqualTo(bookOne);

		Book bookTwo = this.bookService.findByTitle("Star Wars 6 - Return of the Jedi");

		assertBook(bookTwo, "Star Wars 6 - Return of the Jedi");
		Assertions.assertThat(this.bookService.isCacheMiss()).isTrue();
		Assertions.assertThat(this.cachedBooks).hasSize(2);
		Assertions.assertThat(this.cachedBooks.get(bookOne.getTitle())).isEqualTo(bookOne);
		Assertions.assertThat(this.cachedBooks.get(bookTwo.getTitle())).isEqualTo(bookTwo);
	}

	@SpringBootApplication(scanBasePackageClasses = NonBeanType.class)
	@EnableCachingDefinedRegions(clientRegionShortcut = ClientRegionShortcut.LOCAL)
	static class TestConfiguration {  }

}
