Merge branch 'ImprovedServerTests' into 'main'

Fixed PortChecker Server Test

See merge request cse1105/2025-2026/teams/csep-team-76!81
This commit is contained in:
Mei Chang van der Werff 2026-01-22 15:34:58 +01:00
commit 274a48c5c2
2 changed files with 66 additions and 35 deletions

View file

@ -31,31 +31,37 @@ class PortCheckerTest {
} }
} }
@Test @Test
void invalidPort(){ void findNotDefaultFreePort() throws IOException {
PortChecker checker = new PortChecker();
assertThrows(IllegalArgumentException.class, ()-> {
checker.isPortAvailable(-1);
}
);
assertThrows(IllegalArgumentException.class, ()-> {
checker.isPortAvailable(65536);
}
);
}
@Test
void findFreePort() throws IOException {
PortChecker checker = new PortChecker(); PortChecker checker = new PortChecker();
int port = checker.findFreePort(); int port = checker.findFreePort();
int defaultPort = 8080; int lowestPossiblePort = 0;
int lastPort = 8090; int highestPossiblePort = 65535;
boolean greaterOrEqual = port >= defaultPort; assertTrue(port > lowestPossiblePort);
boolean lessOrEqual = port <= lastPort; assertTrue(port <= highestPossiblePort);
boolean inRange = greaterOrEqual && lessOrEqual; assertTrue(checker.isPortAvailable(port));
boolean isItFree = checker.isPortAvailable(port); }
@Test
void findDefaultFreePort() throws IOException {
PortChecker checker = new PortChecker();
assertTrue(inRange); boolean free = checker.isPortAvailable(8080);
assertTrue(free);
assertEquals(checker.findFreePort(),8080);
}
@Test
void invalidFreePort(){
PortChecker checker = new PortChecker();
assertThrows(IllegalArgumentException.class, () ->
checker.isPortAvailable(-1)
);
assertThrows(IllegalArgumentException.class, () ->
checker.isPortAvailable(65536)
);
} }
} }

View file

@ -67,7 +67,9 @@ public class RecipeControllerTest {
.mapToObj(x -> new Recipe( .mapToObj(x -> new Recipe(
null, null,
"Recipe " + x, "Recipe " + x,
"en", List.of(), List.of())) "en",
List.of(),
List.of()))
.toList(); .toList();
controller = new RecipeController( controller = new RecipeController(
recipeService, recipeService,
@ -80,8 +82,11 @@ public class RecipeControllerTest {
if (tags.contains("test-from-init-data")) { if (tags.contains("test-from-init-data")) {
ids = LongStream ids = LongStream
.range(0, NUM_RECIPES) .range(0, NUM_RECIPES)
.map(idx -> recipeRepository.save(recipes.get((int) idx)).getId()) .map(idx -> recipeRepository
.boxed().toList(); .save(recipes.get((int) idx))
.getId())
.boxed()
.toList();
} }
// Some tests need to know the stored IDs of objects // Some tests need to know the stored IDs of objects
@ -117,7 +122,9 @@ public class RecipeControllerTest {
controller.getRecipes( controller.getRecipes(
Optional.empty(), Optional.empty(),
Optional.empty(), Optional.empty(),
Optional.of(List.of("en", "nl"))).getBody().size());; Optional.of(List.of("en", "nl")))
.getBody()
.size());
} }
@Test @Test
@ -129,7 +136,9 @@ public class RecipeControllerTest {
controller.getRecipes( controller.getRecipes(
Optional.empty(), Optional.empty(),
Optional.of(LIMIT), Optional.of(LIMIT),
Optional.of(List.of("en"))).getBody().size()); Optional.of(List.of("en")))
.getBody()
.size());
} }
@Test @Test
@ -140,7 +149,9 @@ public class RecipeControllerTest {
controller.getRecipes( controller.getRecipes(
Optional.empty(), Optional.empty(),
Optional.empty(), Optional.empty(),
Optional.of(List.of("en", "nl"))).getBody().size()); Optional.of(List.of("en", "nl")))
.getBody()
.size());
} }
@Test @Test
@ -151,7 +162,9 @@ public class RecipeControllerTest {
controller.getRecipes( controller.getRecipes(
Optional.empty(), Optional.empty(),
Optional.empty(), Optional.empty(),
Optional.of(List.of("nl"))).getBody().size()); Optional.of(List.of("nl")))
.getBody()
.size());
} }
@Test @Test
@Tag("test-from-init-data") @Tag("test-from-init-data")
@ -161,7 +174,9 @@ public class RecipeControllerTest {
controller.getRecipes( controller.getRecipes(
Optional.empty(), Optional.empty(),
Optional.of(LIMIT), Optional.of(LIMIT),
Optional.of(List.of("en", "nl"))).getBody().size()); Optional.of(List.of("en", "nl")))
.getBody()
.size());
} }
@Test @Test
@ -172,7 +187,8 @@ public class RecipeControllerTest {
// The third item in the input list is the same as the third item retrieved from the database // The third item in the input list is the same as the third item retrieved from the database
assertEquals( assertEquals(
recipes.get(CHECK_INDEX), recipes.get(CHECK_INDEX),
controller.getRecipe(recipeIds.get(CHECK_INDEX)).getBody()); controller.getRecipe(recipeIds.get(CHECK_INDEX))
.getBody());
} }
@Test @Test
@ -181,7 +197,8 @@ public class RecipeControllerTest {
// There does not exist a recipe with ID=3 since there are no items in the repository. // There does not exist a recipe with ID=3 since there are no items in the repository.
assertEquals( assertEquals(
HttpStatus.NOT_FOUND, HttpStatus.NOT_FOUND,
controller.getRecipe((long) CHECK_INDEX).getStatusCode()); controller.getRecipe((long) CHECK_INDEX)
.getStatusCode());
} }
@Test @Test
@ -192,7 +209,8 @@ public class RecipeControllerTest {
// The object has been successfully deleted // The object has been successfully deleted
assertEquals(HttpStatus.OK, assertEquals(HttpStatus.OK,
controller.deleteRecipe(recipeIds.get(DELETE_INDEX)).getStatusCode()); controller.deleteRecipe(recipeIds.get(DELETE_INDEX))
.getStatusCode());
} }
@Test @Test
@ -211,7 +229,8 @@ public class RecipeControllerTest {
public void deleteOneRecipeFail() { public void deleteOneRecipeFail() {
final Long DELETE_INDEX = 5L; final Long DELETE_INDEX = 5L;
assertEquals(HttpStatus.BAD_REQUEST, assertEquals(HttpStatus.BAD_REQUEST,
controller.deleteRecipe(DELETE_INDEX).getStatusCode()); controller.deleteRecipe(DELETE_INDEX)
.getStatusCode());
} }
@Test @Test
@ -219,10 +238,16 @@ public class RecipeControllerTest {
@Tag("need-ids") @Tag("need-ids")
public void updateOneRecipeHasNewData() { public void updateOneRecipeHasNewData() {
final int UPDATE_INDEX = 5; final int UPDATE_INDEX = 5;
Recipe newRecipe = controller.getRecipe(recipeIds.get(UPDATE_INDEX)).getBody(); Recipe newRecipe = controller.getRecipe(recipeIds
.get(UPDATE_INDEX))
.getBody();
newRecipe.setName("New recipe"); newRecipe.setName("New recipe");
controller.updateRecipe(newRecipe.getId(), newRecipe); controller.updateRecipe(newRecipe.getId(), newRecipe);
assertEquals("New recipe", assertEquals("New recipe",
recipeRepository.getReferenceById(recipeIds.get(UPDATE_INDEX)).getName()); recipeRepository.getReferenceById(recipeIds
.get(UPDATE_INDEX))
.getName());
} }
} }