/*
 * Copyright © 2015 by Frank W. Rahn. Alle Rechte vorbehalten. All rights reserved.
 */
package de.rahn.finances.server.web;

import static de.rahn.finances.domains.entities.SecurityType.loan;
import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.notNullValue;
import static org.hamcrest.Matchers.nullValue;
import static org.junit.Assert.assertThat;

import java.util.List;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import de.rahn.finances.domains.entities.Security;
import de.rahn.finances.services.SecuritiesService;

/**
 * Der Test zur Klasse {@link SecuritiesManagementApplication}.
 * @author Frank W. Rahn
 */
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = SecuritiesManagementApplication.class)
public class SecuritiesManagementApplicationTests {

	/** SYMBOL */
	private static final String SYMBOL = "301";

	/** NAME */
	private static final String NAME = "Test AG";

	/** WKN */
	private static final String WKN = "300001";

	/** ISIN */
	private static final String ISIN = "DE0003000018";

	@Autowired
	private SecuritiesService service;

	/**
	 * Alle Wertpapiere laden, alledings gibt es noch keine.
	 */
	@Test
	public void loadAllSecurities() {
		List<Security> securities = service.getSecurities();

		assertThat("Die Liste darf nicht leer sein", securities, hasSize(21));
	}

	/**
	 * Alle Wertpapiere laden, alledings gibt es noch keine.
	 */
	@Test
	public void createSecurity() {
		Security security = new Security();
		security.setIsin(ISIN);
		security.setWkn(WKN);
		security.setName(NAME);
		security.setSymbol(SYMBOL);
		security.setType(loan);

		assertThat(security.isNew(), is(true));
		assertThat(security.getId(), nullValue());

		security = service.save(security);

		assertThat(security, notNullValue());
		assertThat(security.isNew(), is(false));
		assertThat(security.getId(), notNullValue());

		security = service.getSecurity(security.getId());

		assertThat(security, notNullValue());
		assertThat(security.isNew(), is(false));
		assertThat(security.getId(), notNullValue());
		assertThat(security.getIsin(), is(ISIN));
		assertThat(security.getWkn(), is(WKN));
		assertThat(security.getName(), is(NAME));
		assertThat(security.getSymbol(), is(SYMBOL));
		assertThat(security.getType(), is(loan));
	}

}
@@ -189,16 +189,68 @@
 e.loan;%0A
+import static org.hamcrest.Matchers.containsString;%0A
 import s
@@ -286,16 +286,16 @@
 asSize;%0A
-
 import s
@@ -465,16 +465,70 @@
 ertThat;
+%0Aimport static org.springframework.http.HttpStatus.OK;
 %0A%0Aimport
@@ -664,16 +664,75 @@
 owired;%0A
+import org.springframework.beans.factory.annotation.Value;%0A
 import o
@@ -792,16 +792,176 @@
 ration;%0A
+import org.springframework.boot.test.TestRestTemplate;%0Aimport org.springframework.boot.test.WebIntegrationTest;%0Aimport org.springframework.http.ResponseEntity;%0A
 import o
@@ -1347,16 +1347,85 @@
 .class)%0A
+@WebIntegrationTest(%7B %22server.port=0%22, %22spring.jmx.enabled=false%22 %7D)%0A
 public c
@@ -1764,16 +1764,68 @@
 rvice;%0A%0A
+%09@Value(%22$%7Blocal.server.port%7D%22)%0A%09private int port;%0A%0A
 %09/**%0A%09 *
@@ -2953,24 +2953,24 @@
 s(SYMBOL));%0A
-
 %09%09assertThat
@@ -3005,9 +3005,361 @@
 ));%0A%09%7D%0A%0A
+%09/**%0A%09 * Teste die Health-Endpoints.%0A%09 */%0A%09@Test%0A%09public void testHealth() %7B%0A%09%09ResponseEntity%3CString%3E answer =%0A%09%09%09new TestRestTemplate().getForEntity(%22http://localhost:%22 + port + %22/manage/health%22, String.class);%0A%0A%09%09assertThat(answer.getStatusCode(), is(OK));%0A%09%09assertThat(answer.getBody(), containsString(%22%5C%22securitiesManagementApplication%5C%22 :%22));%0A%09%7D%0A%0A
 %7D

Test für den Health-Endpoint