/*
 * Copyright 2013-2014 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.config;

import org.junit.After;
import org.junit.Test;
import org.mockito.Mockito;
import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration;
import org.springframework.boot.test.EnvironmentTestUtils;
import org.springframework.cloud.config.client.ConfigClientProperties;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import com.netflix.appinfo.InstanceInfo;
import com.netflix.discovery.DiscoveryClient;

import static org.junit.Assert.assertEquals;
import static org.mockito.BDDMockito.given;

/**
 * @author Dave Syer
 */
public class DiscoveryClientConfigServiceBootstrapConfigurationTests {

	private AnnotationConfigApplicationContext context;

	private DiscoveryClient client = Mockito.mock(DiscoveryClient.class);

	private InstanceInfo info = InstanceInfo.Builder.newBuilder().setAppName("app")
			.setHostName("foo").setHomePageUrl("/", null).build();

	@After
	public void close() {
		if (this.context != null) {
			this.context.close();
		}
	}

	@Test
	public void offByDefault() throws Exception {
		this.context = new AnnotationConfigApplicationContext(
				DiscoveryClientConfigServiceBootstrapConfiguration.class);
		assertEquals(0, this.context.getBeanNamesForType(DiscoveryClient.class).length);
		assertEquals(
				0,
				this.context
						.getBeanNamesForType(DiscoveryClientConfigServiceBootstrapConfiguration.class).length);
	}

	@Test
	public void onWhenRequested() throws Exception {
		given(this.client.getNextServerFromEureka("CONFIGSERVER", false)).willReturn(
				this.info);
		setup("spring.cloud.config.discovery.enabled=true");
		assertEquals(
				1,
				this.context
						.getBeanNamesForType(DiscoveryClientConfigServiceBootstrapConfiguration.class).length);
		Mockito.verify(this.client).getNextServerFromEureka("CONFIGSERVER", false);
		ConfigClientProperties locator = this.context
				.getBean(ConfigClientProperties.class);
		assertEquals("http://foo:7001/", locator.getUri());
	}

	@Test
	public void setsPasssword() throws Exception {
		this.info.getMetadata().put("password", "bar");
		given(this.client.getNextServerFromEureka("CONFIGSERVER", false)).willReturn(
				this.info);
		setup("spring.cloud.config.discovery.enabled=true");
		ConfigClientProperties locator = this.context
				.getBean(ConfigClientProperties.class);
		assertEquals("http://foo:7001/", locator.getUri());
		assertEquals("bar", locator.getPassword());
		assertEquals("user", locator.getUsername());
	}

	@Test
	public void setsPath() throws Exception {
		this.info.getMetadata().put("configPath", "/bar");
		given(this.client.getNextServerFromEureka("CONFIGSERVER", false)).willReturn(
				this.info);
		setup("spring.cloud.config.discovery.enabled=true");
		ConfigClientProperties locator = this.context
				.getBean(ConfigClientProperties.class);
		assertEquals("http://foo:7001/bar", locator.getUri());
	}

	private void setup(String... env) {
		this.context = new AnnotationConfigApplicationContext();
		EnvironmentTestUtils.addEnvironment(this.context, env);
		this.context.getDefaultListableBeanFactory().registerSingleton(
				"mockDiscoveryClient", this.client);
		this.context.register(PropertyPlaceholderAutoConfiguration.class,
				DiscoveryClientConfigServiceBootstrapConfiguration.class,
				ConfigClientProperties.class);
		this.context.refresh();
	}

}

@@ -2622,32 +2622,35 @@
 1/%22, locator.get
+Raw
 Uri());%0A%09%7D%0A%0A%09@Te
@@ -3031,32 +3031,35 @@
 1/%22, locator.get
+Raw
 Uri());%0A%09%09assert
@@ -3542,16 +3542,19 @@
 ator.get
+Raw
 Uri());%0A

Align with API changes from config client