Daniel Bohry 1 рік тому
батько
коміт
6a8f90307b

+ 2 - 1
src/main/resources/application.yml

@@ -9,4 +9,5 @@ clients:
 spring:
   data:
     mongodb:
-      uri: ${mongo:}
+      uri: ${mongo:}
+      database: ${database:stocks-test}

+ 106 - 0
src/test/java/service/PortfolioServiceTest.java

@@ -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());
+    }
+
+}

+ 13 - 0
src/test/resources/application.yml

@@ -0,0 +1,13 @@
+server:
+  port: ${port:8080}
+
+clients:
+  stock:
+    url: ${provider:http://mock.com}
+    key: ${key:fake-key}
+
+spring:
+  data:
+    mongodb:
+      uri: ${mongo:}
+      database: ${database:stocks-test}