/*
 * Copyright 2012-2016 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.boot.actuate.info;

import org.junit.Test;

import org.springframework.boot.test.util.EnvironmentTestUtils;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.StandardEnvironment;

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

/**
 * Tests for {@link EnvironmentInfoContributor}.
 *
 * @author Stephane Nicoll
 */
public class EnvironmentInfoContributorTests {

	private final StandardEnvironment environment = new StandardEnvironment();

	@Test
	public void extractOnlyInfoProperty() {
		EnvironmentTestUtils.addEnvironment(this.environment, "info.app=my app",
				"info.version=1.0.0", "foo=bar");
		Info actual = contributeFrom(this.environment);
		assertThat(actual.get("app", String.class)).isEqualTo("my app");
		assertThat(actual.get("version", String.class)).isEqualTo("1.0.0");
		assertThat(actual.getDetails().size()).isEqualTo(2);
	}

	@Test
	public void extractNoEntry() {
		EnvironmentTestUtils.addEnvironment(this.environment, "foo=bar");
		Info actual = contributeFrom(this.environment);
		assertThat(actual.getDetails().size()).isEqualTo(0);
	}

	private static Info contributeFrom(ConfigurableEnvironment environment) {
		EnvironmentInfoContributor contributor = new EnvironmentInfoContributor(
				environment);
		Info.Builder builder = new Info.Builder();
		contributor.contribute(builder);
		return builder.build();
	}

}

@@ -21,9 +21,9 @@
 -201
-6
+7
  the
@@ -663,16 +663,69 @@
 .info;%0A%0A
+import java.util.Collections;%0Aimport java.util.Map;%0A%0A
 import o
@@ -901,35 +901,164 @@
 ore.env.
-StandardEnvironment
+MutablePropertySources;%0Aimport org.springframework.core.env.StandardEnvironment;%0Aimport org.springframework.core.env.SystemEnvironmentPropertySource
 ;%0A%0Aimpor
@@ -1946,16 +1946,490 @@
 0);%0A%09%7D%0A%0A
+%09@Test%0A%09@SuppressWarnings(%22unchecked%22)%0A%09public void propertiesFromEnvironmentShouldBindCorrectly() throws Exception %7B%0A%09%09MutablePropertySources propertySources = this.environment.getPropertySources();%0A%09%09propertySources.addFirst(new SystemEnvironmentPropertySource(%22system%22,%0A%09%09%09%09Collections.singletonMap(%22INFO_ENVIRONMENT_FOO%22, %22green%22)));%0A%09%09Info actual = contributeFrom(this.environment);%0A%09%09assertThat(actual.get(%22environment%22, Map.class)).containsEntry(%22foo%22, %22green%22);%0A%09%7D%0A%0A
 %09private

Test correct binding in '/info' endpoint