Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 83 additions & 4 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -1,16 +1,95 @@
<?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>org.example</groupId>
<artifactId>praktikum</artifactId>
<artifactId>QA-java-diplom-1</artifactId>
<version>1.0-SNAPSHOT</version>

<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<junit.version>4.13.2</junit.version>
<mockito.version>4.6.1</mockito.version>
<jacoco.version>0.8.10</jacoco.version>
</properties>

</project>
<build>
<plugins>
<!-- Surefire Plugin для запуска тестов с JUnit 4 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M9</version>
<configuration>
<argLine>-javaagent:${settings.localRepository}/org/jacoco/org.jacoco.agent/${jacoco.version}/org.jacoco.agent-${jacoco.version}-runtime.jar=destfile=${project.build.directory}/jacoco.exec</argLine>
<includes>
<include>**/*Test.java</include>
</includes>
</configuration>
</plugin>

<!-- Jacoco Plugin -->
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.12</version>
<executions>
<execution>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>report</id>
<phase>test</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
<configuration>
<outputDirectory>${project.build.directory}/site/jacoco</outputDirectory>
<dataFile>${project.build.directory}/jacoco.exec</dataFile>
<formats>
<format>HTML</format>
</formats>

<includes>
<include>praktikum/Bun.class</include>
<include>praktikum/Burger.class</include>
<include>praktikum/Ingredient.class</include>
<include>praktikum/IngredientType.class</include>
</includes>
</configuration>
</plugin>
</plugins>
</build>

<dependencies>
<!-- JUnit 4 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>

<!-- Mockito -->
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>5.4.0</version>
<scope>test</scope>
</dependency>

<!-- JUnitParams -->
<dependency>
<groupId>pl.pragmatists</groupId>
<artifactId>JUnitParams</artifactId>
<version>1.1.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
37 changes: 19 additions & 18 deletions src/main/java/praktikum/Burger.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,8 @@

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

/**
* Модель бургера.
* Бургер состоит из булочек и ингредиентов (начинка или соус).
* Ингредиенты можно перемещать и удалять.
* Можно распечать чек с информацией о бургере.
*/
public class Burger {

public Bun bun;
Expand All @@ -26,32 +21,38 @@ public void removeIngredient(int index) {
ingredients.remove(index);
}

public void moveIngredient(int index, int newIndex) {
ingredients.add(newIndex, ingredients.remove(index));
public void moveIngredient(int fromIndex, int toIndex) {
Ingredient ingredient = ingredients.remove(fromIndex);
ingredients.add(toIndex, ingredient);
}

public float getPrice() {
float price = bun.getPrice() * 2;

float total = 0;
if (bun != null) {
total += bun.getPrice() * 2; // верхняя и нижняя булка
}
for (Ingredient ingredient : ingredients) {
price += ingredient.getPrice();
total += ingredient.getPrice();
}

return price;
return total;
}

public String getReceipt() {
StringBuilder receipt = new StringBuilder(String.format("(==== %s ====)%n", bun.getName()));
StringBuilder receipt = new StringBuilder();

receipt.append(String.format("(==== %s ====)%n", bun.getName()));

for (Ingredient ingredient : ingredients) {
receipt.append(String.format("= %s %s =%n", ingredient.getType().toString().toLowerCase(),
receipt.append(String.format("= %s %s =%n",
ingredient.getType().toString().toLowerCase(),
ingredient.getName()));
}

receipt.append(String.format("(==== %s ====)%n", bun.getName()));
receipt.append(String.format("%nPrice: %f%n", getPrice()));
receipt.append(String.format("(==== %s ====)%n%n", bun.getName()));

// Старательно форматируем цену: 2 знака после точки, локаль US
receipt.append(String.format(Locale.US, "Price: %.2f%n", getPrice()));

return receipt.toString();
}

}
213 changes: 213 additions & 0 deletions src/test/java/praktikum/BurgerTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,213 @@
package praktikum;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import junitparams.JUnitParamsRunner;
import junitparams.Parameters;

import java.util.Locale;

import static org.junit.Assert.*;
import static org.mockito.Mockito.*;

@RunWith(JUnitParamsRunner.class)
public class BurgerTest {

// ====== Константы ======
private static final float BUN_PRICE_SMALL = 100f;
private static final float BUN_PRICE_MEDIUM = 150f;
private static final float BUN_PRICE_LARGE = 200f;

private static final float SAUCE_PRICE = 50f;
private static final float FILLING_PRICE = 75f;

// ====== Моки ======
private Burger burger;
private Bun bunMock;
private Ingredient sauceIngredient;
private Ingredient fillingIngredient;

@Before
public void setUp() {
burger = new Burger();
bunMock = mock(Bun.class);
sauceIngredient = mock(Ingredient.class);
fillingIngredient = mock(Ingredient.class);

when(bunMock.getName()).thenReturn("Test Bun");
when(bunMock.getPrice()).thenReturn(BUN_PRICE_SMALL);

when(sauceIngredient.getName()).thenReturn("Tomato Sauce");
when(sauceIngredient.getPrice()).thenReturn(SAUCE_PRICE);
when(sauceIngredient.getType()).thenReturn(IngredientType.SAUCE);

when(fillingIngredient.getName()).thenReturn("Beef Patty");
when(fillingIngredient.getPrice()).thenReturn(FILLING_PRICE);
when(fillingIngredient.getType()).thenReturn(IngredientType.FILLING);
}

// ====== Тесты булки ======
@Test
public void testSetBun() {
burger.setBuns(bunMock);
assertEquals(bunMock, burger.bun);
}

// ====== Тесты добавления ингредиентов ======
@Test
public void testAddSauceIngredient() {
burger.setBuns(bunMock);
burger.addIngredient(sauceIngredient);
assertEquals(sauceIngredient, burger.ingredients.get(0));
}

@Test
public void testAddFillingIngredient() {
burger.setBuns(bunMock);
burger.addIngredient(fillingIngredient);
assertEquals(fillingIngredient, burger.ingredients.get(0));
}

// ====== Тесты удаления ингредиентов ======
@Test
public void testRemoveIngredientKeepsCorrectIngredient() {
burger.setBuns(bunMock);
burger.addIngredient(sauceIngredient);
burger.addIngredient(fillingIngredient);

burger.removeIngredient(0);
assertEquals(fillingIngredient, burger.ingredients.get(0));
}

@Test
public void testRemoveIngredientListSize() {
burger.setBuns(bunMock);
burger.addIngredient(sauceIngredient);
burger.addIngredient(fillingIngredient);

burger.removeIngredient(0);
assertEquals(1, burger.ingredients.size());
}

// ====== Тесты перемещения ингредиентов ======
@Test
public void testMoveIngredientFirstPosition() {
burger.setBuns(bunMock);
burger.addIngredient(sauceIngredient);
burger.addIngredient(fillingIngredient);

burger.moveIngredient(0, 1);
assertEquals(fillingIngredient, burger.ingredients.get(0));
}

@Test
public void testMoveIngredientSecondPosition() {
burger.setBuns(bunMock);
burger.addIngredient(sauceIngredient);
burger.addIngredient(fillingIngredient);

burger.moveIngredient(0, 1);
assertEquals(sauceIngredient, burger.ingredients.get(1));
}

// ====== Параметризированные тесты цены ======
private Object[] parametersForBurgerPriceTest() {
Ingredient sauce = mock(Ingredient.class);
when(sauce.getPrice()).thenReturn(SAUCE_PRICE);

Ingredient filling = mock(Ingredient.class);
when(filling.getPrice()).thenReturn(FILLING_PRICE);

return new Object[]{
new Object[]{BUN_PRICE_SMALL, new Ingredient[]{sauce, filling}, 325f},
new Object[]{BUN_PRICE_LARGE, new Ingredient[]{sauce}, 450f},
new Object[]{BUN_PRICE_MEDIUM, new Ingredient[]{}, 300f}
};
}

@Test
@Parameters(method = "parametersForBurgerPriceTest")
public void testGetPriceParameterized(float bunPrice, Ingredient[] ingredients, float expectedPrice) {
Bun bun = mock(Bun.class);
when(bun.getPrice()).thenReturn(bunPrice);
when(bun.getName()).thenReturn("Test Bun");

Burger burger = new Burger();
burger.setBuns(bun);

for (Ingredient ingredient : ingredients) {
burger.addIngredient(ingredient);
}

assertEquals(expectedPrice, burger.getPrice(), 0.01);
}

// ====== Параметризированные тесты добавления ингредиентов ======
private Object[] parametersForAddIngredientTest() {
return new Object[]{
new Object[]{sauceIngredient},
new Object[]{fillingIngredient}
};
}

@Test
@Parameters(method = "parametersForAddIngredientTest")
public void testAddIngredientParameterized(Ingredient ingredient) {
burger.setBuns(bunMock);
burger.addIngredient(ingredient);
assertTrue(burger.ingredients.contains(ingredient));
}

// ====== Тесты рецепта ======
@Test
public void testGetReceiptFull() {
burger.setBuns(bunMock);
burger.addIngredient(sauceIngredient);
burger.addIngredient(fillingIngredient);

String expectedReceipt = String.format(Locale.US,
"(==== %s ====)%n" +
"= %s %s =%n" +
"= %s %s =%n" +
"(==== %s ====)%n" +
"%nPrice: %.2f%n",
bunMock.getName(),
sauceIngredient.getType().toString().toLowerCase(), sauceIngredient.getName(),
fillingIngredient.getType().toString().toLowerCase(), fillingIngredient.getName(),
bunMock.getName(),
burger.getPrice()
);

assertEquals(expectedReceipt, burger.getReceipt());
}

@Test
public void testGetReceiptEmptyIngredients() {
burger.setBuns(bunMock);
String expectedReceipt = String.format(Locale.US,
"(==== %s ====)%n" +
"(==== %s ====)%n%n" +
"Price: %.2f%n",
bunMock.getName(),
bunMock.getName(),
burger.getPrice());
assertEquals(expectedReceipt, burger.getReceipt());
}

@Test
public void testGetReceiptWithoutBun() {
burger.addIngredient(sauceIngredient);
burger.addIngredient(fillingIngredient);

try {
burger.getReceipt();
fail("Expected NullPointerException when bun is null");
} catch (NullPointerException ignored) {}
}

@Test
public void testGetPriceWithoutBun() {
assertEquals(0f, burger.getPrice(), 0.01);
}
}
Loading