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
void invalidPort(){
PortChecker checker = new PortChecker();
assertThrows(IllegalArgumentException.class, ()-> {
checker.isPortAvailable(-1);
}
);
assertThrows(IllegalArgumentException.class, ()-> {
checker.isPortAvailable(65536);
}
);
}
@Test
void findFreePort() throws IOException {
void findNotDefaultFreePort() throws IOException {
PortChecker checker = new PortChecker();
int port = checker.findFreePort();
int defaultPort = 8080;
int lastPort = 8090;
int lowestPossiblePort = 0;
int highestPossiblePort = 65535;
boolean greaterOrEqual = port >= defaultPort;
boolean lessOrEqual = port <= lastPort;
boolean inRange = greaterOrEqual && lessOrEqual;
boolean isItFree = checker.isPortAvailable(port);
assertTrue(port > lowestPossiblePort);
assertTrue(port <= highestPossiblePort);
assertTrue(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(
null,
"Recipe " + x,
"en", List.of(), List.of()))
"en",
List.of(),
List.of()))
.toList();
controller = new RecipeController(
recipeService,
@ -80,8 +82,11 @@ public class RecipeControllerTest {
if (tags.contains("test-from-init-data")) {
ids = LongStream
.range(0, NUM_RECIPES)
.map(idx -> recipeRepository.save(recipes.get((int) idx)).getId())
.boxed().toList();
.map(idx -> recipeRepository
.save(recipes.get((int) idx))
.getId())
.boxed()
.toList();
}
// Some tests need to know the stored IDs of objects
@ -117,7 +122,9 @@ public class RecipeControllerTest {
controller.getRecipes(
Optional.empty(),
Optional.empty(),
Optional.of(List.of("en", "nl"))).getBody().size());;
Optional.of(List.of("en", "nl")))
.getBody()
.size());
}
@Test
@ -129,7 +136,9 @@ public class RecipeControllerTest {
controller.getRecipes(
Optional.empty(),
Optional.of(LIMIT),
Optional.of(List.of("en"))).getBody().size());
Optional.of(List.of("en")))
.getBody()
.size());
}
@Test
@ -140,7 +149,9 @@ public class RecipeControllerTest {
controller.getRecipes(
Optional.empty(),
Optional.empty(),
Optional.of(List.of("en", "nl"))).getBody().size());
Optional.of(List.of("en", "nl")))
.getBody()
.size());
}
@Test
@ -151,7 +162,9 @@ public class RecipeControllerTest {
controller.getRecipes(
Optional.empty(),
Optional.empty(),
Optional.of(List.of("nl"))).getBody().size());
Optional.of(List.of("nl")))
.getBody()
.size());
}
@Test
@Tag("test-from-init-data")
@ -161,7 +174,9 @@ public class RecipeControllerTest {
controller.getRecipes(
Optional.empty(),
Optional.of(LIMIT),
Optional.of(List.of("en", "nl"))).getBody().size());
Optional.of(List.of("en", "nl")))
.getBody()
.size());
}
@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
assertEquals(
recipes.get(CHECK_INDEX),
controller.getRecipe(recipeIds.get(CHECK_INDEX)).getBody());
controller.getRecipe(recipeIds.get(CHECK_INDEX))
.getBody());
}
@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.
assertEquals(
HttpStatus.NOT_FOUND,
controller.getRecipe((long) CHECK_INDEX).getStatusCode());
controller.getRecipe((long) CHECK_INDEX)
.getStatusCode());
}
@Test
@ -192,7 +209,8 @@ public class RecipeControllerTest {
// The object has been successfully deleted
assertEquals(HttpStatus.OK,
controller.deleteRecipe(recipeIds.get(DELETE_INDEX)).getStatusCode());
controller.deleteRecipe(recipeIds.get(DELETE_INDEX))
.getStatusCode());
}
@Test
@ -211,7 +229,8 @@ public class RecipeControllerTest {
public void deleteOneRecipeFail() {
final Long DELETE_INDEX = 5L;
assertEquals(HttpStatus.BAD_REQUEST,
controller.deleteRecipe(DELETE_INDEX).getStatusCode());
controller.deleteRecipe(DELETE_INDEX)
.getStatusCode());
}
@Test
@ -219,10 +238,16 @@ public class RecipeControllerTest {
@Tag("need-ids")
public void updateOneRecipeHasNewData() {
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");
controller.updateRecipe(newRecipe.getId(), newRecipe);
assertEquals("New recipe",
recipeRepository.getReferenceById(recipeIds.get(UPDATE_INDEX)).getName());
recipeRepository.getReferenceById(recipeIds
.get(UPDATE_INDEX))
.getName());
}
}