fix: the 26 checkstyle violations in tests (Thank you checkstyle)

This commit is contained in:
Natalia Cholewa 2025-12-05 14:47:30 +01:00
commit 6d5f7af225

View file

@ -26,6 +26,20 @@ public class IngredientControllerTest {
private final IngredientRepository ingredientRepository;
private IngredientController controller;
private static final double PROTEIN_BASE = 1.0;
private static final double FAT_BASE = 2.0;
private static final double CARBS_BASE = 2.0;
private static final double PROTEIN_ALT = 0.5;
private static final double FAT_ALT = 1.0;
private static final double CARBS_ALT = 1.5;
private static final int OK_STATUS = 200;
private static final int BAD_REQUEST_STATUS = 400;
private static final int NOT_FOUND_STATUS = 404;
private static final Long INVALID_ID = 2137L;
@Autowired
public IngredientControllerTest(IngredientRepository ingredientRepository,
SimpMessagingTemplate template) {
@ -38,7 +52,7 @@ public class IngredientControllerTest {
Stream.of("Salt", "Sugar", "Flour", "Eggs", "Milk")
.map(name -> {
return new Ingredient(name, 1.0, 2.0, 3.0);
return new Ingredient(name, PROTEIN_BASE, FAT_BASE, CARBS_BASE);
})
.forEach(ingredientRepository::save);
}
@ -55,16 +69,19 @@ public class IngredientControllerTest {
List<Ingredient> ingredients = controller.getIngredients(Optional.empty(), Optional.empty()).getBody();
assertNotNull(ingredients);
assertEquals(5, ingredients.size());
final int expectedCount = 5;
assertEquals(expectedCount, ingredients.size());
assertEquals("Eggs", ingredients.getFirst().name);
}
@Test
public void testGetPaginatedIngredients() {
List<Ingredient> ingredients = controller.getIngredients(Optional.of(0), Optional.of(2)).getBody();
final int limit = 2;
List<Ingredient> ingredients = controller.getIngredients(Optional.of(0), Optional.of(limit)).getBody();
assertNotNull(ingredients);
assertEquals(2, ingredients.size());
final int expectedCount = 2;
assertEquals(expectedCount, ingredients.size());
assertEquals("Eggs", ingredients.get(0).name);
assertEquals("Flour", ingredients.get(1).name);
}
@ -82,31 +99,31 @@ public class IngredientControllerTest {
@Test
public void testGetIngredientByInvalidId() {
Long invalidId = 2137L;
var response = controller.getIngredientById(INVALID_ID);
var response = controller.getIngredientById(invalidId);
assertEquals(HttpStatusCode.valueOf(404), response.getStatusCode());
assertEquals(HttpStatusCode.valueOf(NOT_FOUND_STATUS), response.getStatusCode());
}
@Test
public void testCreateIngredient() {
Ingredient newIngredient = new Ingredient("Butter", 0.5, 1.0, 1.5);
Ingredient newIngredient = new Ingredient("Butter", PROTEIN_ALT, FAT_ALT, CARBS_ALT);
Ingredient createdIngredient = controller.createIngredient(newIngredient).getBody();
final int expectedCount = 6;
assertNotNull(createdIngredient);
assertEquals("Butter", createdIngredient.name);
assertEquals(6, ingredientRepository.count());
assertEquals(expectedCount, ingredientRepository.count());
}
@Test
public void testCreateIngredientMissingName() {
Ingredient newIngredient = new Ingredient(null, 0.5, 1.0, 1.5);
Ingredient newIngredient = new Ingredient(null, PROTEIN_ALT, FAT_ALT, CARBS_ALT);
var response = controller.createIngredient(newIngredient);
assertEquals(HttpStatusCode.valueOf(400), response.getStatusCode());
assertEquals(HttpStatusCode.valueOf(BAD_REQUEST_STATUS), response.getStatusCode());
}
@Test
@ -119,24 +136,22 @@ public class IngredientControllerTest {
Long id = ingredient.id;
Ingredient updatedData = new Ingredient("Sea Salt", 1.5, 2.5, 3.5);
Ingredient updatedData = new Ingredient("Sea Salt", PROTEIN_ALT, FAT_ALT, CARBS_ALT);
Ingredient updatedIngredient = controller.updateIngredient(id, updatedData).getBody();
assertNotNull(updatedIngredient);
assertEquals("Sea Salt", updatedIngredient.name);
assertEquals(1.5, updatedIngredient.proteinPer100g);
assertEquals(PROTEIN_ALT, updatedIngredient.proteinPer100g);
}
@Test
public void testUpdateMissingIngredient() {
Long invalidId = 2137L;
Ingredient updatedData = new Ingredient("Sea Salt", PROTEIN_ALT, FAT_ALT, CARBS_ALT);
Ingredient updatedData = new Ingredient("Sea Salt", 1.5, 2.5, 3.5);
var response = controller.updateIngredient(INVALID_ID, updatedData);
var response = controller.updateIngredient(invalidId, updatedData);
assertEquals(HttpStatusCode.valueOf(404), response.getStatusCode());
assertEquals(HttpStatusCode.valueOf(NOT_FOUND_STATUS), response.getStatusCode());
}
@Test
@ -151,16 +166,15 @@ public class IngredientControllerTest {
var response = controller.deleteIngredient(id);
assertEquals(HttpStatusCode.valueOf(200), response.getStatusCode());
assertEquals(4, ingredientRepository.count());
final int expectedCount = 4;
assertEquals(HttpStatusCode.valueOf(OK_STATUS), response.getStatusCode());
assertEquals(expectedCount, ingredientRepository.count());
}
@Test
public void testDeleteMissingIngredient() {
Long invalidId = 2137L;
var response = controller.deleteIngredient(INVALID_ID);
var response = controller.deleteIngredient(invalidId);
assertEquals(HttpStatusCode.valueOf(404), response.getStatusCode());
assertEquals(HttpStatusCode.valueOf(NOT_FOUND_STATUS), response.getStatusCode());
}
}