|
|
@@ -1,14 +1,17 @@
|
|
|
package service;
|
|
|
|
|
|
import com.danielbohry.stocks.App;
|
|
|
+import com.danielbohry.stocks.client.StockClient;
|
|
|
import com.danielbohry.stocks.domain.Portfolio;
|
|
|
import com.danielbohry.stocks.domain.Stock;
|
|
|
import com.danielbohry.stocks.repository.PortfolioRepository;
|
|
|
+import com.danielbohry.stocks.repository.QuoteRepository;
|
|
|
import com.danielbohry.stocks.service.PortfolioService;
|
|
|
import com.danielbohry.stocks.service.StockService;
|
|
|
import org.junit.jupiter.api.AfterEach;
|
|
|
import org.junit.jupiter.api.BeforeEach;
|
|
|
import org.junit.jupiter.api.Test;
|
|
|
+import org.mockito.Mock;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.boot.test.context.SpringBootTest;
|
|
|
import org.springframework.test.context.ContextConfiguration;
|
|
|
@@ -17,33 +20,39 @@ import java.util.List;
|
|
|
import java.util.Optional;
|
|
|
|
|
|
import static org.junit.jupiter.api.Assertions.*;
|
|
|
+import static org.mockito.ArgumentMatchers.any;
|
|
|
+import static org.mockito.Mockito.when;
|
|
|
|
|
|
@SpringBootTest
|
|
|
@ContextConfiguration(classes = {App.class})
|
|
|
public class PortfolioServiceTest {
|
|
|
|
|
|
@Autowired
|
|
|
- private PortfolioRepository repository;
|
|
|
+ private PortfolioRepository portfolioRepository;
|
|
|
|
|
|
@Autowired
|
|
|
- private StockService stockService;
|
|
|
+ private QuoteRepository quoteRepository;
|
|
|
|
|
|
- private PortfolioService service;
|
|
|
+ @Mock
|
|
|
+ private StockClient client;
|
|
|
+
|
|
|
+ private PortfolioService portfolioService;
|
|
|
|
|
|
@BeforeEach
|
|
|
public void setup() {
|
|
|
- service = new PortfolioService(repository, stockService);
|
|
|
+ StockService stockService = new StockService(quoteRepository, client, "key");
|
|
|
+ portfolioService = new PortfolioService(portfolioRepository, stockService);
|
|
|
}
|
|
|
|
|
|
@AfterEach
|
|
|
public void teardown() {
|
|
|
- repository.deleteAll();
|
|
|
+ portfolioRepository.deleteAll();
|
|
|
}
|
|
|
|
|
|
@Test
|
|
|
public void shouldCreateNewPortfolio() {
|
|
|
//when
|
|
|
- Portfolio response = service.create();
|
|
|
+ Portfolio response = portfolioService.create();
|
|
|
|
|
|
//then
|
|
|
assertNotNull(response.getId());
|
|
|
@@ -52,10 +61,10 @@ public class PortfolioServiceTest {
|
|
|
@Test
|
|
|
public void shouldGetAPortfolio() {
|
|
|
//given
|
|
|
- Portfolio portfolio = service.create();
|
|
|
+ Portfolio portfolio = portfolioService.create();
|
|
|
|
|
|
//when
|
|
|
- Portfolio response = service.get(portfolio.getId());
|
|
|
+ Portfolio response = portfolioService.get(portfolio.getId());
|
|
|
|
|
|
//then
|
|
|
assertNotNull(response.getId());
|
|
|
@@ -64,11 +73,11 @@ public class PortfolioServiceTest {
|
|
|
@Test
|
|
|
public void shouldGetAllPortfolios() {
|
|
|
//given
|
|
|
- Portfolio portfolio = service.create();
|
|
|
- Portfolio portfolio2 = service.create();
|
|
|
+ Portfolio portfolio = portfolioService.create();
|
|
|
+ Portfolio portfolio2 = portfolioService.create();
|
|
|
|
|
|
//when
|
|
|
- List<Portfolio> response = service.getAll();
|
|
|
+ List<Portfolio> response = portfolioService.getAll();
|
|
|
|
|
|
//then
|
|
|
assertEquals(2, response.size());
|
|
|
@@ -77,14 +86,16 @@ public class PortfolioServiceTest {
|
|
|
@Test
|
|
|
public void shouldUpdateAPortfolio() {
|
|
|
//given
|
|
|
- Portfolio portfolio = service.create();
|
|
|
+ when(client.getStockInfo(any(), any())).thenReturn(new StockService.StockInfoResponse());
|
|
|
+
|
|
|
+ Portfolio portfolio = portfolioService.create();
|
|
|
Stock newStock = Stock.builder()
|
|
|
.code("code")
|
|
|
.quantity(3)
|
|
|
.build();
|
|
|
|
|
|
//when
|
|
|
- Portfolio response = service.update(portfolio.getId(), List.of(newStock));
|
|
|
+ Portfolio response = portfolioService.update(portfolio.getId(), List.of(newStock));
|
|
|
|
|
|
//then
|
|
|
assertNotNull(response.getId());
|
|
|
@@ -94,13 +105,13 @@ public class PortfolioServiceTest {
|
|
|
@Test
|
|
|
public void shouldDeleteAPortfolio() {
|
|
|
//given
|
|
|
- Portfolio portfolio = service.create();
|
|
|
+ Portfolio portfolio = portfolioService.create();
|
|
|
|
|
|
//when
|
|
|
- service.delete(portfolio.getId());
|
|
|
+ portfolioService.delete(portfolio.getId());
|
|
|
|
|
|
//then
|
|
|
- assertEquals(repository.findById(portfolio.getId()), Optional.empty());
|
|
|
+ assertEquals(portfolioRepository.findById(portfolio.getId()), Optional.empty());
|
|
|
}
|
|
|
|
|
|
}
|