/*
 * Copyright 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.boot.actuate.health;

import java.io.File;

import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;

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

/**
 * Tests for {@link DiskSpaceHealthIndicator}.
 *
 * @author Mattias Severson
 */
@RunWith(MockitoJUnitRunner.class)
public class DiskSpaceHealthIndicatorTests {

	static final long THRESHOLD_BYTES = 1024;

	@Rule
	public ExpectedException exception = ExpectedException.none();

	@Mock
	private File fileMock;

	private HealthIndicator healthIndicator;

	@Before
	public void setUp() throws Exception {
		given(this.fileMock.exists()).willReturn(true);
		given(this.fileMock.canRead()).willReturn(true);
		this.healthIndicator = new DiskSpaceHealthIndicator(createProperties(
				this.fileMock, THRESHOLD_BYTES));
	}

	@Test
	public void diskSpaceIsUp() throws Exception {
		given(this.fileMock.getFreeSpace()).willReturn(THRESHOLD_BYTES + 10);
		Health health = this.healthIndicator.health();
		assertEquals(Status.UP, health.getStatus());
		assertEquals(THRESHOLD_BYTES, health.getDetails().get("threshold"));
		assertEquals(THRESHOLD_BYTES + 10, health.getDetails().get("free"));
	}

	@Test
	public void diskSpaceIsDown() throws Exception {
		given(this.fileMock.getFreeSpace()).willReturn(THRESHOLD_BYTES - 10);
		Health health = this.healthIndicator.health();
		assertEquals(Status.DOWN, health.getStatus());
		assertEquals(THRESHOLD_BYTES, health.getDetails().get("threshold"));
		assertEquals(THRESHOLD_BYTES - 10, health.getDetails().get("free"));
	}

	private DiskSpaceHealthIndicatorProperties createProperties(File path, long threshold) {
		DiskSpaceHealthIndicatorProperties properties = new DiskSpaceHealthIndicatorProperties();
		properties.setPath(path);
		properties.setThreshold(threshold);
		return properties;
	}

}

@@ -1736,24 +1736,97 @@
 YTES + 10);%0A
+%09%09given(this.fileMock.getTotalSpace()).willReturn(THRESHOLD_BYTES * 10);%0A
 %09%09Health hea
@@ -1854,32 +1854,32 @@
 cator.health();%0A
-
 %09%09assertEquals(S
@@ -2047,24 +2047,96 @@
 t(%22free%22));%0A
+%09%09assertEquals(THRESHOLD_BYTES * 10, health.getDetails().get(%22total%22));%0A
 %09%7D%0A%0A%09@Test%0A%09
@@ -2252,24 +2252,97 @@
 YTES - 10);%0A
+%09%09given(this.fileMock.getTotalSpace()).willReturn(THRESHOLD_BYTES * 10);%0A
 %09%09Health hea
@@ -2490,32 +2490,32 @@
 (%22threshold%22));%0A
-
 %09%09assertEquals(T
@@ -2565,24 +2565,96 @@
 t(%22free%22));%0A
+%09%09assertEquals(THRESHOLD_BYTES * 10, health.getDetails().get(%22total%22));%0A
 %09%7D%0A%0A%09private

Test that total space is included in disk health information