/*
 * Copyright 2017-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.cloud.gcp.autoconfigure.firestore;

import com.google.api.gax.grpc.InstantiatingGrpcChannelProvider;
import com.google.cloud.firestore.FirestoreOptions;
import org.junit.Test;

import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import org.springframework.cloud.gcp.autoconfigure.core.GcpContextAutoConfiguration;
import org.springframework.cloud.gcp.data.firestore.FirestoreTemplate;

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

/**
 * Tests for Firestore Emulator autoconfiguration.
 *
 * @author Daniel Zou
 */
public class GcpFirestoreEmulatorAutoConfigurationTests {

	ApplicationContextRunner contextRunner =
			new ApplicationContextRunner()
					.withConfiguration(AutoConfigurations.of(
							GcpFirestoreEmulatorAutoConfiguration.class,
							GcpContextAutoConfiguration.class,
							GcpFirestoreAutoConfiguration.class));

	@Test
	public void testAutoConfigurationEnabled() {
		contextRunner
				.withPropertyValues(
						"spring.cloud.gcp.firestore.emulator.enabled=true",
						"spring.cloud.gcp.firestore.host-port=localhost:9000")
				.run(context -> {
					FirestoreOptions firestoreOptions = context.getBean(FirestoreOptions.class);
					String endpoint =
							((InstantiatingGrpcChannelProvider)
									firestoreOptions.getTransportChannelProvider()).getEndpoint();
					assertThat(endpoint).isEqualTo("localhost:9000");

					FirestoreTemplate firestoreTemplate = context.getBean(FirestoreTemplate.class);
					assertThat(firestoreTemplate.isUsingStreamTokens()).isFalse();
				});
	}

	@Test
	public void testAutoConfigurationDisabled() {
		contextRunner
				.run(context -> {
					FirestoreOptions firestoreOptions = context.getBean(FirestoreOptions.class);
					String endpoint =
							((InstantiatingGrpcChannelProvider)
									firestoreOptions.getTransportChannelProvider()).getEndpoint();
					assertThat(endpoint).isEqualTo("firestore.googleapis.com:443");

					FirestoreTemplate firestoreTemplate = context.getBean(FirestoreTemplate.class);
					assertThat(firestoreTemplate.isUsingStreamTokens()).isTrue();
				});
	}
}

@@ -793,24 +793,54 @@
 oreOptions;%0A
+import org.junit.BeforeClass;%0A
 import org.j
@@ -850,16 +850,16 @@
 t.Test;%0A
-
 %0Aimport 
@@ -1209,16 +1209,99 @@
 ertThat;
+%0Aimport static org.hamcrest.Matchers.is;%0Aimport static org.junit.Assume.assumeThat;
 %0A%0A/**%0A *
@@ -1646,32 +1646,32 @@
 guration.class,%0A
-
 %09%09%09%09%09%09%09GcpFirest
@@ -1701,16 +1701,250 @@
 ass));%0A%0A
+%09@BeforeClass%0A%09public static void checkToRun() %7B%0A%09%09assumeThat(%22Firestore emulator integration tests are disabled. %22%0A%09%09%09%09%09%09+ %22Please use '-Dit.firestore=true' to enable them. %22,%0A%09%09%09%09System.getProperty(%22it.firestore%22), is(%22true%22));%0A%09%7D%0A%0A
 %09@Test%0A%09

Add guard for firestore emulator integration tests. (#2343)