Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
Jookkii authored Dec 9, 2024
1 parent d1fdfb5 commit c497dae
Show file tree
Hide file tree
Showing 17 changed files with 381 additions and 0 deletions.
46 changes: 46 additions & 0 deletions Mocks/Mocks.iml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?xml version="1.0" encoding="UTF-8"?>
<module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_11">
<output url="file://$MODULE_DIR$/target/classes" />
<output-test url="file://$MODULE_DIR$/target/test-classes" />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/main/resources" type="java-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/test/java" isTestSource="true" />
<excludeFolder url="file://$MODULE_DIR$/target" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="module-library">
<library>
<CLASSES>
<root url="file://$MODULE_DIR$/lib" />
</CLASSES>
<JAVADOC />
<SOURCES />
<jarDirectory url="file://$MODULE_DIR$/lib" recursive="false" />
</library>
</orderEntry>
<orderEntry type="library" scope="TEST" name="Maven: org.mockito:mockito-core:3.6.28" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: net.bytebuddy:byte-buddy:1.10.18" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: net.bytebuddy:byte-buddy-agent:1.10.18" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.objenesis:objenesis:3.1" level="project" />
<orderEntry type="module-library">
<library name="Maven: put.io.students.fancylibrary:put.io.students.fancylibrary:1.0">
<CLASSES>
<root url="jar://$MODULE_DIR$/lib/FancyLibrary.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES />
</library>
</orderEntry>
<orderEntry type="library" scope="TEST" name="Maven: org.junit.jupiter:junit-jupiter:5.7.0" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.junit.jupiter:junit-jupiter-api:5.7.0" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.apiguardian:apiguardian-api:1.1.0" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.opentest4j:opentest4j:1.2.0" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.junit.platform:junit-platform-commons:1.7.0" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.junit.jupiter:junit-jupiter-params:5.7.0" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.junit.jupiter:junit-jupiter-engine:5.7.0" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.junit.platform:junit-platform-engine:1.7.0" level="project" />
</component>
</module>
Binary file added Mocks/lib/FancyLibrary.jar
Binary file not shown.
48 changes: 48 additions & 0 deletions Mocks/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>io-mocks</groupId>
<artifactId>Mocks</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>4.9.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>put.io.students.fancylibrary</groupId>
<artifactId>put.io.students.fancylibrary</artifactId>
<version>1.0</version>
<scope>system</scope>
<systemPath>${project.basedir}/lib/FancyLibrary.jar</systemPath>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.7.0</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<release>11</release>
</configuration>
</plugin>
</plugins>
</build>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
</project>
31 changes: 31 additions & 0 deletions Mocks/src/main/java/put/io/testing/mocks/Expense.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
65 changes: 65 additions & 0 deletions Mocks/src/main/java/put/io/testing/mocks/ExpenseManager.java
Original file line number Diff line number Diff line change
@@ -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<Expense> expenses = expenseRepository.getExpenses();

return calculateTotal(expenses);
}

public long calculateTotalForCategory(String category) {
List<Expense> expenses = expenseRepository.getExpensesByCategory(category);

return calculateTotal(expenses);
}

private long calculateTotal(List<Expense> 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;
}

}
75 changes: 75 additions & 0 deletions Mocks/src/main/java/put/io/testing/mocks/ExpenseRepository.java
Original file line number Diff line number Diff line change
@@ -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<Expense> expenses;
private IFancyDatabase fancyDatabase;

public ExpenseRepository(IFancyDatabase database) {
this.fancyDatabase = database;
expenses = new ArrayList<Expense>();
}

public ExpenseRepository() {
this.fancyDatabase = new FancyDatabase();
expenses = new ArrayList<Expense>();
}

@Override
public List<Expense> getExpenses() {
return Collections.unmodifiableList(expenses);
}

@Override
public List<Expense> getExpensesByCategory(String category) {
List<Expense> filteredList = new ArrayList<Expense>();

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<Expense>(fancyDatabase.<Expense>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();
}
}
17 changes: 17 additions & 0 deletions Mocks/src/main/java/put/io/testing/mocks/IExpenseRepository.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package put.io.testing.mocks;

import java.util.List;

public interface IExpenseRepository {
List<Expense> getExpenses();

List<Expense> getExpensesByCategory(String category);

void addExpense(Expense expense);

void deleteExpense(Expense expense);

void loadExpenses();

void saveExpenses();
}
17 changes: 17 additions & 0 deletions Mocks/src/test/java/put/io/testing/mocks/ExpenseManagerTest.java
Original file line number Diff line number Diff line change
@@ -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 {

}
Original file line number Diff line number Diff line change
@@ -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));
}
}
29 changes: 29 additions & 0 deletions Mocks/src/test/java/put/io/testing/mocks/MyDatabase.java
Original file line number Diff line number Diff line change
@@ -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 <T> void persist(T t) {

}

@Override
public <T> List<T> queryAll() {
return Collections.emptyList();
}

@Override
public void close() {

}

}
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.

0 comments on commit c497dae

Please sign in to comment.