|
|
@@ -0,0 +1,106 @@
|
|
|
+package service;
|
|
|
+
|
|
|
+import com.danielbohry.stocks.App;
|
|
|
+import com.danielbohry.stocks.domain.Portfolio;
|
|
|
+import com.danielbohry.stocks.domain.Stock;
|
|
|
+import com.danielbohry.stocks.repository.PortfolioRepository;
|
|
|
+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.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.boot.test.context.SpringBootTest;
|
|
|
+import org.springframework.test.context.ContextConfiguration;
|
|
|
+
|
|
|
+import java.util.List;
|
|
|
+import java.util.Optional;
|
|
|
+
|
|
|
+import static org.junit.jupiter.api.Assertions.*;
|
|
|
+
|
|
|
+@SpringBootTest
|
|
|
+@ContextConfiguration(classes = {App.class})
|
|
|
+public class PortfolioServiceTest {
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private PortfolioRepository repository;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private StockService stockService;
|
|
|
+
|
|
|
+ private PortfolioService service;
|
|
|
+
|
|
|
+ @BeforeEach
|
|
|
+ public void setup() {
|
|
|
+ service = new PortfolioService(repository, stockService);
|
|
|
+ }
|
|
|
+
|
|
|
+ @AfterEach
|
|
|
+ public void teardown() {
|
|
|
+ repository.deleteAll();
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void shouldCreateNewPortfolio() {
|
|
|
+ //when
|
|
|
+ Portfolio response = service.create();
|
|
|
+
|
|
|
+ //then
|
|
|
+ assertNotNull(response.getId());
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void shouldGetAPortfolio() {
|
|
|
+ //given
|
|
|
+ Portfolio portfolio = service.create();
|
|
|
+
|
|
|
+ //when
|
|
|
+ Portfolio response = service.get(portfolio.getId());
|
|
|
+
|
|
|
+ //then
|
|
|
+ assertNotNull(response.getId());
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void shouldGetAllPortfolios() {
|
|
|
+ //given
|
|
|
+ Portfolio portfolio = service.create();
|
|
|
+ Portfolio portfolio2 = service.create();
|
|
|
+
|
|
|
+ //when
|
|
|
+ List<Portfolio> response = service.getAll();
|
|
|
+
|
|
|
+ //then
|
|
|
+ assertEquals(2, response.size());
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void shouldUpdateAPortfolio() {
|
|
|
+ //given
|
|
|
+ Portfolio portfolio = service.create();
|
|
|
+ Stock newStock = Stock.builder()
|
|
|
+ .code("code")
|
|
|
+ .quantity(3)
|
|
|
+ .build();
|
|
|
+
|
|
|
+ //when
|
|
|
+ Portfolio response = service.update(portfolio.getId(), List.of(newStock));
|
|
|
+
|
|
|
+ //then
|
|
|
+ assertNotNull(response.getId());
|
|
|
+ assertFalse(response.getStocks().isEmpty());
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void shouldDeleteAPortfolio() {
|
|
|
+ //given
|
|
|
+ Portfolio portfolio = service.create();
|
|
|
+
|
|
|
+ //when
|
|
|
+ service.delete(portfolio.getId());
|
|
|
+
|
|
|
+ //then
|
|
|
+ assertEquals(repository.findById(portfolio.getId()), Optional.empty());
|
|
|
+ }
|
|
|
+
|
|
|
+}
|