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