diff --git a/Mocks/Mocks.iml b/Mocks/Mocks.iml new file mode 100644 index 0000000..790af0e --- /dev/null +++ b/Mocks/Mocks.iml @@ -0,0 +1,46 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/Mocks/lib/FancyLibrary.jar b/Mocks/lib/FancyLibrary.jar new file mode 100644 index 0000000..c6d1537 Binary files /dev/null and b/Mocks/lib/FancyLibrary.jar differ diff --git a/Mocks/pom.xml b/Mocks/pom.xml new file mode 100644 index 0000000..5f1ba4d --- /dev/null +++ b/Mocks/pom.xml @@ -0,0 +1,48 @@ + + + 4.0.0 + + io-mocks + Mocks + 1.0-SNAPSHOT + + + org.mockito + mockito-core + 4.9.0 + test + + + put.io.students.fancylibrary + put.io.students.fancylibrary + 1.0 + system + ${project.basedir}/lib/FancyLibrary.jar + + + org.junit.jupiter + junit-jupiter + 5.7.0 + test + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.8.0 + + 11 + + + + + + + UTF-8 + + diff --git a/Mocks/src/main/java/put/io/testing/mocks/Expense.java b/Mocks/src/main/java/put/io/testing/mocks/Expense.java new file mode 100644 index 0000000..93300a4 --- /dev/null +++ b/Mocks/src/main/java/put/io/testing/mocks/Expense.java @@ -0,0 +1,31 @@ +package put.io.testing.mocks; + +public class Expense { + private String title; + private String category; + private long amount; + + public String getTitle() { + return title; + } + + public void setTitle(String title) { + this.title = title; + } + + public String getCategory() { + return category; + } + + public void setCategory(String category) { + this.category = category; + } + + public long getAmount() { + return amount; + } + + public void setAmount(long amount) { + this.amount = amount; + } +} diff --git a/Mocks/src/main/java/put/io/testing/mocks/ExpenseManager.java b/Mocks/src/main/java/put/io/testing/mocks/ExpenseManager.java new file mode 100644 index 0000000..a9c92cc --- /dev/null +++ b/Mocks/src/main/java/put/io/testing/mocks/ExpenseManager.java @@ -0,0 +1,65 @@ +package put.io.testing.mocks; + +import java.net.ConnectException; +import java.util.List; + +import put.io.students.fancylibrary.service.FancyService; + +public class ExpenseManager { + + private ExpenseRepository expenseRepository; + private FancyService fancyService; + + public ExpenseManager() { + + this.expenseRepository = new ExpenseRepository(); + this.fancyService = new FancyService(); + } + + public long calculateTotal() { + List expenses = expenseRepository.getExpenses(); + + return calculateTotal(expenses); + } + + public long calculateTotalForCategory(String category) { + List expenses = expenseRepository.getExpensesByCategory(category); + + return calculateTotal(expenses); + } + + private long calculateTotal(List expenses) { + long total = 0; + + for (Expense expense : expenses) { + total += expense.getAmount(); + } + + return total; + } + + public long calculateTotalInDollars() { + long total = calculateTotal(); + + double convertedTotal = -1; + try { + convertedTotal = fancyService.convert(total, "PLN", "USD"); + } catch (ConnectException e) { + + } + + return (int)convertedTotal; + } + + public long convertExpenseToDollars(Expense expense) { + double convertedAmount = 1; + try { + convertedAmount = fancyService.convert(expense.getAmount(), "PLN", "USD"); + } catch (ConnectException e) { + + } + + return (int)convertedAmount; + } + +} diff --git a/Mocks/src/main/java/put/io/testing/mocks/ExpenseRepository.java b/Mocks/src/main/java/put/io/testing/mocks/ExpenseRepository.java new file mode 100644 index 0000000..d5bf400 --- /dev/null +++ b/Mocks/src/main/java/put/io/testing/mocks/ExpenseRepository.java @@ -0,0 +1,75 @@ +package put.io.testing.mocks; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +import put.io.students.fancylibrary.database.FancyDatabase; +import put.io.students.fancylibrary.database.IFancyDatabase; + +public class ExpenseRepository implements IExpenseRepository { + + private List expenses; + private IFancyDatabase fancyDatabase; + + public ExpenseRepository(IFancyDatabase database) { + this.fancyDatabase = database; + expenses = new ArrayList(); + } + + public ExpenseRepository() { + this.fancyDatabase = new FancyDatabase(); + expenses = new ArrayList(); + } + + @Override + public List getExpenses() { + return Collections.unmodifiableList(expenses); + } + + @Override + public List getExpensesByCategory(String category) { + List filteredList = new ArrayList(); + + for (Expense expense : expenses) { + if (expense.getCategory().equals(category)) { + filteredList.add(expense); + } + } + + return filteredList; + } + + @Override + public void addExpense(Expense expense) { + expenses.add(expense); + } + + @Override + public void deleteExpense(Expense expense) { + expenses.remove(expense); + } + + @Override + public void loadExpenses() { + + fancyDatabase.connect(); + + expenses = new ArrayList(fancyDatabase.queryAll()); + fancyDatabase.close(); + } + + @Override + public void saveExpenses() { + fancyDatabase.connect(); + + int i = 1; + for (Expense expense : expenses) { + fancyDatabase.persist(expense); + if (i++ % 2 == 0) + fancyDatabase.persist(expense); + } + + fancyDatabase.close(); + } +} diff --git a/Mocks/src/main/java/put/io/testing/mocks/IExpenseRepository.java b/Mocks/src/main/java/put/io/testing/mocks/IExpenseRepository.java new file mode 100644 index 0000000..04d3f5e --- /dev/null +++ b/Mocks/src/main/java/put/io/testing/mocks/IExpenseRepository.java @@ -0,0 +1,17 @@ +package put.io.testing.mocks; + +import java.util.List; + +public interface IExpenseRepository { + List getExpenses(); + + List getExpensesByCategory(String category); + + void addExpense(Expense expense); + + void deleteExpense(Expense expense); + + void loadExpenses(); + + void saveExpenses(); +} diff --git a/Mocks/src/test/java/put/io/testing/mocks/ExpenseManagerTest.java b/Mocks/src/test/java/put/io/testing/mocks/ExpenseManagerTest.java new file mode 100644 index 0000000..349f3e3 --- /dev/null +++ b/Mocks/src/test/java/put/io/testing/mocks/ExpenseManagerTest.java @@ -0,0 +1,17 @@ +package put.io.testing.mocks; + +import static org.junit.jupiter.api.Assertions.*; +import static org.mockito.Mockito.*; + +import java.net.ConnectException; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +import org.junit.jupiter.api.Test; +import org.mockito.invocation.InvocationOnMock; +import org.mockito.stubbing.Answer; + +public class ExpenseManagerTest { + +} diff --git a/Mocks/src/test/java/put/io/testing/mocks/ExpenseRepositoryTest.java b/Mocks/src/test/java/put/io/testing/mocks/ExpenseRepositoryTest.java new file mode 100644 index 0000000..38adbc1 --- /dev/null +++ b/Mocks/src/test/java/put/io/testing/mocks/ExpenseRepositoryTest.java @@ -0,0 +1,53 @@ +package put.io.testing.mocks; + +import org.junit.jupiter.api.Test; +import org.mockito.InOrder; +import put.io.students.fancylibrary.database.IFancyDatabase; + +import java.util.Collections; + +import static org.mockito.Mockito.*; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; + +public class ExpenseRepositoryTest { + + @Test + void loadExpenses() { + IFancyDatabase mock = mock(IFancyDatabase.class); + InOrder inOrder = inOrder(mock); + //given + ExpenseRepository rep = new ExpenseRepository(mock); + //when + when(mock.queryAll()).thenReturn(Collections.emptyList()); + rep.loadExpenses(); + inOrder.verify(mock).connect(); + inOrder.verify(mock).queryAll(); + inOrder.verify(mock).close(); + + //then + assertArrayEquals(rep.getExpenses().toArray(), new Expense[]{}); + } + + @Test + void saveExpenses() { + IFancyDatabase mock = mock(IFancyDatabase.class); + InOrder inOrder = inOrder(mock); + //given + ExpenseRepository rep = new ExpenseRepository(mock); + //when + when(mock.queryAll()).thenReturn(Collections.emptyList()); + Expense expense = new Expense(); + rep.loadExpenses(); + rep.addExpense(expense); + for (int i = 0; i < 5; i++) { + rep.saveExpenses(); + } + inOrder.verify(mock).connect(); + inOrder.verify(mock).queryAll(); + inOrder.verify(mock).close(); + + //then + verify(mock, times(5)).persist(any(Expense.class)); + } +} diff --git a/Mocks/src/test/java/put/io/testing/mocks/MyDatabase.java b/Mocks/src/test/java/put/io/testing/mocks/MyDatabase.java new file mode 100644 index 0000000..7961c3b --- /dev/null +++ b/Mocks/src/test/java/put/io/testing/mocks/MyDatabase.java @@ -0,0 +1,29 @@ +package put.io.testing.mocks; + +import put.io.students.fancylibrary.database.IFancyDatabase; + +import java.util.Collections; +import java.util.List; + +public class MyDatabase implements IFancyDatabase { + @Override + public void connect() { + + } + + @Override + public void persist(T t) { + + } + + @Override + public List queryAll() { + return Collections.emptyList(); + } + + @Override + public void close() { + + } + +} diff --git a/Mocks/target/classes/put/io/testing/mocks/Expense.class b/Mocks/target/classes/put/io/testing/mocks/Expense.class new file mode 100644 index 0000000..6c3eac9 Binary files /dev/null and b/Mocks/target/classes/put/io/testing/mocks/Expense.class differ diff --git a/Mocks/target/classes/put/io/testing/mocks/ExpenseManager.class b/Mocks/target/classes/put/io/testing/mocks/ExpenseManager.class new file mode 100644 index 0000000..1e37339 Binary files /dev/null and b/Mocks/target/classes/put/io/testing/mocks/ExpenseManager.class differ diff --git a/Mocks/target/classes/put/io/testing/mocks/ExpenseRepository.class b/Mocks/target/classes/put/io/testing/mocks/ExpenseRepository.class new file mode 100644 index 0000000..43c3d2b Binary files /dev/null and b/Mocks/target/classes/put/io/testing/mocks/ExpenseRepository.class differ diff --git a/Mocks/target/classes/put/io/testing/mocks/IExpenseRepository.class b/Mocks/target/classes/put/io/testing/mocks/IExpenseRepository.class new file mode 100644 index 0000000..19a37d3 Binary files /dev/null and b/Mocks/target/classes/put/io/testing/mocks/IExpenseRepository.class differ diff --git a/Mocks/target/test-classes/put/io/testing/mocks/ExpenseManagerTest.class b/Mocks/target/test-classes/put/io/testing/mocks/ExpenseManagerTest.class new file mode 100644 index 0000000..ec504b7 Binary files /dev/null and b/Mocks/target/test-classes/put/io/testing/mocks/ExpenseManagerTest.class differ diff --git a/Mocks/target/test-classes/put/io/testing/mocks/ExpenseRepositoryTest.class b/Mocks/target/test-classes/put/io/testing/mocks/ExpenseRepositoryTest.class new file mode 100644 index 0000000..e29ca26 Binary files /dev/null and b/Mocks/target/test-classes/put/io/testing/mocks/ExpenseRepositoryTest.class differ diff --git a/Mocks/target/test-classes/put/io/testing/mocks/MyDatabase.class b/Mocks/target/test-classes/put/io/testing/mocks/MyDatabase.class new file mode 100644 index 0000000..a63b72e Binary files /dev/null and b/Mocks/target/test-classes/put/io/testing/mocks/MyDatabase.class differ