diff --git a/openbas-api/src/main/java/io/openbas/rest/team/TeamApi.java b/openbas-api/src/main/java/io/openbas/rest/team/TeamApi.java index ce49073360..c647acc0cf 100644 --- a/openbas-api/src/main/java/io/openbas/rest/team/TeamApi.java +++ b/openbas-api/src/main/java/io/openbas/rest/team/TeamApi.java @@ -48,6 +48,8 @@ @RequiredArgsConstructor public class TeamApi extends RestBehavior { + public static final String TEAM_URI = "/api/teams"; + private final ExerciseRepository exerciseRepository; private final ScenarioRepository scenarioRepository; private final TeamRepository teamRepository; @@ -57,7 +59,7 @@ public class TeamApi extends RestBehavior { private final TeamService teamService; @LogExecutionTime - @GetMapping("/api/teams") + @GetMapping(TEAM_URI) @PreAuthorize("isObserver()") @Tracing(name = "Get teams", layer = "api", operation = "GET") public Iterable getTeams() { @@ -115,7 +117,7 @@ public Iterable getTeamPlayers(@PathVariable String teamId) { return teamRepository.findById(teamId).orElseThrow(ElementNotFoundException::new).getUsers(); } - @PostMapping("/api/teams") + @PostMapping(TEAM_URI) @PreAuthorize("isPlanner()") @Transactional(rollbackFor = Exception.class) public Team createTeam(@Valid @RequestBody TeamCreateInput input) { diff --git a/openbas-api/src/test/java/io/openbas/rest/ScenarioTeamApiTest.java b/openbas-api/src/test/java/io/openbas/rest/ScenarioTeamApiTest.java new file mode 100644 index 0000000000..c43c667fba --- /dev/null +++ b/openbas-api/src/test/java/io/openbas/rest/ScenarioTeamApiTest.java @@ -0,0 +1,238 @@ +package io.openbas.rest; + +import static io.openbas.database.specification.TeamSpecification.fromScenario; +import static io.openbas.helper.StreamHelper.fromIterable; +import static io.openbas.rest.scenario.ScenarioApi.SCENARIO_URI; +import static io.openbas.utils.JsonUtils.asJsonString; +import static io.openbas.utils.fixtures.ScenarioFixture.getScenario; +import static io.openbas.utils.fixtures.TeamFixture.TEAM_NAME; +import static org.junit.jupiter.api.Assertions.*; +import static org.junit.jupiter.api.TestInstance.Lifecycle.PER_CLASS; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + +import com.jayway.jsonpath.JsonPath; +import io.openbas.IntegrationTest; +import io.openbas.database.model.Scenario; +import io.openbas.database.model.ScenarioTeamUser; +import io.openbas.database.model.Team; +import io.openbas.database.model.User; +import io.openbas.database.repository.ScenarioRepository; +import io.openbas.database.repository.ScenarioTeamUserRepository; +import io.openbas.database.repository.TeamRepository; +import io.openbas.database.repository.UserRepository; +import io.openbas.rest.exercise.form.ExerciseTeamPlayersEnableInput; +import io.openbas.rest.exercise.form.ScenarioTeamPlayersEnableInput; +import io.openbas.rest.scenario.form.ScenarioUpdateTeamsInput; +import io.openbas.utils.fixtures.UserFixture; +import io.openbas.utils.mockUser.WithMockAdminUser; +import java.util.List; +import java.util.Optional; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.TestInstance; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.MediaType; +import org.springframework.test.web.servlet.MockMvc; +import org.springframework.transaction.annotation.Transactional; + +@TestInstance(PER_CLASS) +@Transactional +class ScenarioTeamApiTest extends IntegrationTest { + + @Autowired private MockMvc mvc; + + @Autowired private ScenarioRepository scenarioRepository; + @Autowired private ScenarioTeamUserRepository scenarioTeamUserRepository; + @Autowired private TeamRepository teamRepository; + @Autowired private UserRepository userRepository; + + @DisplayName("Given a valid scenario and team input, should add team to scenario successfully") + @Test + @WithMockAdminUser + void given_validScenarioAndTeamInput_should_addTeamToScenarioSuccessfully() throws Exception { + // -- PREPARE -- + Scenario scenario = getScenario(); + Scenario scenarioCreated = this.scenarioRepository.save(scenario); + Team team = new Team(); + team.setName(TEAM_NAME); + Team teamCreated = this.teamRepository.save(team); + ScenarioUpdateTeamsInput input = new ScenarioUpdateTeamsInput(); + input.setTeamIds(List.of(teamCreated.getId())); + + // -- EXECUTE -- + this.mvc + .perform( + put(SCENARIO_URI + "/" + scenarioCreated.getId() + "/teams/add") + .content(asJsonString(input)) + .contentType(MediaType.APPLICATION_JSON) + .accept(MediaType.APPLICATION_JSON)) + .andExpect(status().is2xxSuccessful()) + .andReturn() + .getResponse() + .getContentAsString(); + + // -- ASSERT -- + Optional scenarioSaved = this.scenarioRepository.findById(scenarioCreated.getId()); + assertTrue(scenarioSaved.isPresent()); + assertEquals(TEAM_NAME, scenarioSaved.get().getTeams().getFirst().getName()); + } + + @DisplayName("Given a valid scenario with teams, should retrieve teams successfully") + @Test + @WithMockAdminUser + void given_validScenarioWithTeams_should_retrieveTeamsSuccessfully() throws Exception { + // -- PREPARE -- + Team team = new Team(); + team.setName(TEAM_NAME); + Team teamCreated = this.teamRepository.save(team); + + Scenario scenario = getScenario(); + scenario.setTeams(List.of(teamCreated)); + Scenario scenarioCreated = this.scenarioRepository.save(scenario); + + // -- EXECUTE -- + String response = + this.mvc + .perform( + get(SCENARIO_URI + "/" + scenarioCreated.getId() + "/teams") + .accept(MediaType.APPLICATION_JSON)) + .andExpect(status().is2xxSuccessful()) + .andReturn() + .getResponse() + .getContentAsString(); + + // -- ASSERT -- + assertNotNull(response); + assertEquals(teamCreated.getId(), JsonPath.read(response, "$[0].team_id")); + } + + @DisplayName("Given a valid scenario and team, should add player to team successfully") + @Test + @WithMockAdminUser + void given_validScenarioAndTeam_should_addPlayerToTeamSuccessfully() throws Exception { + // -- PREPARE -- + Team team = new Team(); + team.setName(TEAM_NAME); + Team teamCreated = this.teamRepository.save(team); + + Scenario scenario = getScenario(); + scenario.setTeams(List.of(teamCreated)); + Scenario scenarioCreated = this.scenarioRepository.save(scenario); + + User user = UserFixture.getUser(); + User userCreated = this.userRepository.save(user); + + ScenarioTeamPlayersEnableInput input = new ScenarioTeamPlayersEnableInput(); + input.setPlayersIds(List.of(userCreated.getId())); + + // -- EXECUTE -- + this.mvc + .perform( + put(SCENARIO_URI + + "/" + + scenarioCreated.getId() + + "/teams/" + + teamCreated.getId() + + "/players/add") + .content(asJsonString(input)) + .contentType(MediaType.APPLICATION_JSON) + .accept(MediaType.APPLICATION_JSON)) + .andExpect(status().is2xxSuccessful()) + .andReturn() + .getResponse() + .getContentAsString(); + + // -- ASSERT -- + List scenarioTeamUsers = + fromIterable(this.scenarioTeamUserRepository.findAll()); + assertTrue(!scenarioTeamUsers.isEmpty()); + assertTrue( + scenarioTeamUsers.stream().anyMatch(s -> userCreated.getId().equals(s.getUser().getId()))); + } + + @DisplayName( + "Given a valid scenario and team with a player, should remove player from team successfully") + @Test + @WithMockAdminUser + void given_validScenarioAndTeamWithPlayer_should_removePlayerFromTeamSuccessfully() + throws Exception { + // -- PREPARE -- + Team team = new Team(); + team.setName(TEAM_NAME); + Team teamCreated = this.teamRepository.save(team); + + Scenario scenario = getScenario(); + scenario.setTeams(List.of(teamCreated)); + Scenario scenarioCreated = this.scenarioRepository.save(scenario); + + User user = UserFixture.getUser(); + User userCreated = this.userRepository.save(user); + + ScenarioTeamUser scenarioTeamUser = new ScenarioTeamUser(); + scenarioTeamUser.setScenario(scenarioCreated); + scenarioTeamUser.setTeam(teamCreated); + scenarioTeamUser.setUser(userCreated); + this.scenarioTeamUserRepository.save(scenarioTeamUser); + + ExerciseTeamPlayersEnableInput input = new ExerciseTeamPlayersEnableInput(); + input.setPlayersIds(List.of(userCreated.getId())); + + // -- EXECUTE -- + this.mvc + .perform( + put(SCENARIO_URI + + "/" + + scenarioCreated.getId() + + "/teams/" + + teamCreated.getId() + + "/players/remove") + .content(asJsonString(input)) + .contentType(MediaType.APPLICATION_JSON) + .accept(MediaType.APPLICATION_JSON)) + .andExpect(status().is2xxSuccessful()) + .andReturn() + .getResponse() + .getContentAsString(); + + // -- ASSERT -- + List scenarioTeamUsers = + fromIterable(this.scenarioTeamUserRepository.findAll()); + assertTrue(!scenarioTeamUsers.isEmpty()); + assertFalse(scenarioTeamUsers.stream().anyMatch(s -> s.getScenario() == null)); + } + + @DisplayName("Given a valid scenario with a team, should remove team from scenario successfully") + @Test + @WithMockAdminUser + void given_validScenarioWithTeam_should_removeTeamFromScenarioSuccessfully() throws Exception { + // -- PREPARE -- + Team team = new Team(); + team.setName(TEAM_NAME); + Team teamCreated = this.teamRepository.save(team); + + Scenario scenario = getScenario(); + scenario.setTeams(List.of(teamCreated)); + Scenario scenarioCreated = this.scenarioRepository.save(scenario); + + ScenarioUpdateTeamsInput input = new ScenarioUpdateTeamsInput(); + input.setTeamIds(List.of(teamCreated.getId())); + + // -- EXECUTE -- + this.mvc + .perform( + put(SCENARIO_URI + "/" + scenarioCreated.getId() + "/teams/remove") + .content(asJsonString(input)) + .contentType(MediaType.APPLICATION_JSON) + .accept(MediaType.APPLICATION_JSON)) + .andExpect(status().is2xxSuccessful()) + .andReturn() + .getResponse() + .getContentAsString(); + + // -- ASSERT -- + List teams = this.teamRepository.findAll(fromScenario(scenarioCreated.getId())); + assertTrue(teams.isEmpty()); + } +} diff --git a/openbas-api/src/test/java/io/openbas/rest/TeamApiTest.java b/openbas-api/src/test/java/io/openbas/rest/TeamApiTest.java index c20d526ef4..358cb6ad1e 100644 --- a/openbas-api/src/test/java/io/openbas/rest/TeamApiTest.java +++ b/openbas-api/src/test/java/io/openbas/rest/TeamApiTest.java @@ -1,86 +1,54 @@ package io.openbas.rest; -import static io.openbas.rest.scenario.ScenarioApi.SCENARIO_URI; +import static io.openbas.rest.team.TeamApi.TEAM_URI; import static io.openbas.utils.JsonUtils.asJsonString; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertNotNull; +import static io.openbas.utils.fixtures.TeamFixture.*; +import static org.junit.jupiter.api.Assertions.*; import static org.junit.jupiter.api.TestInstance.Lifecycle.PER_CLASS; -import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; import com.jayway.jsonpath.JsonPath; -import io.openbas.database.model.Scenario; +import io.openbas.IntegrationTest; +import io.openbas.database.model.Exercise; import io.openbas.database.model.Team; -import io.openbas.database.model.User; -import io.openbas.database.repository.ScenarioRepository; import io.openbas.database.repository.TeamRepository; -import io.openbas.database.repository.UserRepository; -import io.openbas.rest.exercise.form.ScenarioTeamPlayersEnableInput; -import io.openbas.rest.scenario.form.ScenarioUpdateTeamsInput; -import io.openbas.service.ScenarioService; -import io.openbas.utils.mockUser.WithMockObserverUser; -import io.openbas.utils.mockUser.WithMockPlannerUser; -import java.util.ArrayList; +import io.openbas.rest.exercise.service.ExerciseService; +import io.openbas.rest.team.form.TeamCreateInput; +import io.openbas.utils.fixtures.ExerciseFixture; +import io.openbas.utils.mockUser.WithMockAdminUser; +import jakarta.servlet.ServletException; import java.util.List; -import org.junit.jupiter.api.*; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.TestInstance; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; -import org.springframework.boot.test.context.SpringBootTest; import org.springframework.http.MediaType; import org.springframework.test.web.servlet.MockMvc; +import org.springframework.transaction.annotation.Transactional; -@SpringBootTest -@AutoConfigureMockMvc -@TestMethodOrder(MethodOrderer.OrderAnnotation.class) @TestInstance(PER_CLASS) -public class TeamApiTest { +@Transactional +class TeamApiTest extends IntegrationTest { @Autowired private MockMvc mvc; - @Autowired private ScenarioService scenarioService; - @Autowired private ScenarioRepository scenarioRepository; + @Autowired private ExerciseService exerciseService; @Autowired private TeamRepository teamRepository; - @Autowired private UserRepository userRepository; - static String SCENARIO_ID; - static String TEAM_ID; - static String USER_ID; - - @AfterAll - void afterAll() { - this.scenarioRepository.deleteById(SCENARIO_ID); - this.teamRepository.deleteById(TEAM_ID); - this.userRepository.deleteById(USER_ID); - } - - // -- SCENARIOS -- - - @DisplayName("Add a team on a scenario") + @DisplayName("Given valid team input, should create a team successfully") @Test - @Order(1) - @WithMockPlannerUser - void addTeamOnScenarioTest() throws Exception { - // -- PREPARE -- - Scenario scenario = new Scenario(); - scenario.setName("Scenario name"); - Scenario scenarioCreated = this.scenarioService.createScenario(scenario); - SCENARIO_ID = scenarioCreated.getId(); - - Team team = new Team(); - team.setName("My team"); - team = this.teamRepository.save(team); - TEAM_ID = team.getId(); - - ScenarioUpdateTeamsInput input = new ScenarioUpdateTeamsInput(); - input.setTeamIds(List.of(TEAM_ID)); + @WithMockAdminUser + void given_validTeamInput_should_createTeamSuccessfully() throws Exception { + // --PREPARE-- + TeamCreateInput teamInput = createTeam(); - // -- EXECUTE -- + // --EXECUTE-- String response = - this.mvc - .perform( - put(SCENARIO_URI + "/" + SCENARIO_ID + "/teams/add") - .content(asJsonString(input)) + mvc.perform( + post(TEAM_URI) + .content(asJsonString(teamInput)) .contentType(MediaType.APPLICATION_JSON) .accept(MediaType.APPLICATION_JSON)) .andExpect(status().is2xxSuccessful()) @@ -88,57 +56,55 @@ void addTeamOnScenarioTest() throws Exception { .getResponse() .getContentAsString(); - // -- ASSERT -- - assertNotNull(response); - response = - this.mvc - .perform(get(SCENARIO_URI + "/" + SCENARIO_ID).accept(MediaType.APPLICATION_JSON)) - .andExpect(status().is2xxSuccessful()) - .andReturn() - .getResponse() - .getContentAsString(); - assertEquals(TEAM_ID, JsonPath.read(response, "$.scenario_teams[0]")); + // --ASSERT-- + assertEquals(teamInput.getName(), JsonPath.read(response, "$.team_name")); + assertEquals(teamInput.getDescription(), JsonPath.read(response, "$.team_description")); } - @DisplayName("Retrieve teams on a scenario") + @DisplayName("Given existing team name input, should throw an exception") @Test - @Order(2) - @WithMockObserverUser - void retrieveTeamsOnScenarioTest() throws Exception { - // -- EXECUTE -- + @WithMockAdminUser + void given_existingTeamNameInput_should_throwAnException() throws Exception { + // --PREPARE-- + Team team = new Team(); + team.setName(TEAM_NAME); + this.teamRepository.save(team); + + TeamCreateInput teamInput = createTeam(); + + // --EXECUTE-- String response = - this.mvc - .perform( - get(SCENARIO_URI + "/" + SCENARIO_ID + "/teams").accept(MediaType.APPLICATION_JSON)) - .andExpect(status().is2xxSuccessful()) + mvc.perform( + post(TEAM_URI) + .content(asJsonString(teamInput)) + .contentType(MediaType.APPLICATION_JSON) + .accept(MediaType.APPLICATION_JSON)) + .andExpect(status().is4xxClientError()) .andReturn() .getResponse() .getContentAsString(); - // -- ASSERT -- - assertNotNull(response); - assertEquals(TEAM_ID, JsonPath.read(response, "$[0].team_id")); + // --ASSERT-- + assertEquals( + "Global teams (non contextual) cannot have the same name (already exists)", + JsonPath.read(response, "$.message")); } - @DisplayName("Add a player to a team on a scenario") + @DisplayName("Given valid contextual team input, should create a contextual team successfully") @Test - @Order(3) - @WithMockPlannerUser - void addPlayerOnTeamOnScenarioTest() throws Exception { + @WithMockAdminUser + void given_validContextualTeamInput_should_createContextualTeamSuccessfully() throws Exception { // -- PREPARE -- - User user = new User(); - user.setEmail("testfiligran@gmail.com"); - user = this.userRepository.save(user); - USER_ID = user.getId(); - ScenarioTeamPlayersEnableInput input = new ScenarioTeamPlayersEnableInput(); - input.setPlayersIds(List.of(USER_ID)); - - // -- EXECUTE -- + Exercise exercise = ExerciseFixture.getExercise(); + exercise = this.exerciseService.createExercise(exercise); + + TeamCreateInput teamInput = createContextualExerciseTeam(List.of(exercise.getId())); + + // --EXECUTE-- String response = - this.mvc - .perform( - put(SCENARIO_URI + "/" + SCENARIO_ID + "/teams/" + TEAM_ID + "/players/add") - .content(asJsonString(input)) + mvc.perform( + post(TEAM_URI) + .content(asJsonString(teamInput)) .contentType(MediaType.APPLICATION_JSON) .accept(MediaType.APPLICATION_JSON)) .andExpect(status().is2xxSuccessful()) @@ -146,72 +112,90 @@ void addPlayerOnTeamOnScenarioTest() throws Exception { .getResponse() .getContentAsString(); - // -- ASSERT -- - assertNotNull(response); - response = - this.mvc - .perform(get(SCENARIO_URI + "/" + SCENARIO_ID).accept(MediaType.APPLICATION_JSON)) - .andExpect(status().is2xxSuccessful()) - .andReturn() - .getResponse() - .getContentAsString(); - assertEquals(USER_ID, JsonPath.read(response, "$.scenario_teams_users[0].user_id")); + // --ASSERT-- + assertEquals(CONTEXTUAL_TEAM_NAME, JsonPath.read(response, "$.team_name")); } - @DisplayName("Remove a player to a team on a scenario") + @DisplayName("Given existing contextual team name input, should throw an exception") @Test - @Order(4) - @WithMockPlannerUser - void removePlayerOnTeamOnScenarioTest() throws Exception { + @WithMockAdminUser + void given_existingContextualTeamNameInput_should_throwAnException() throws Exception { // -- PREPARE -- - ScenarioTeamPlayersEnableInput input = new ScenarioTeamPlayersEnableInput(); - input.setPlayersIds(List.of(USER_ID)); + Exercise exercise = ExerciseFixture.getExercise(); + exercise = this.exerciseService.createExercise(exercise); + Team team = new Team(); + team.setName(CONTEXTUAL_TEAM_NAME); + team.setContextual(true); + team.setExercises(List.of(exercise)); + this.teamRepository.save(team); + + TeamCreateInput teamInput = createContextualExerciseTeam(List.of(exercise.getId())); - // -- EXECUTE -- + // --EXECUTE-- String response = - this.mvc - .perform( - put(SCENARIO_URI + "/" + SCENARIO_ID + "/teams/" + TEAM_ID + "/players/remove") - .content(asJsonString(input)) + mvc.perform( + post(TEAM_URI) + .content(asJsonString(teamInput)) .contentType(MediaType.APPLICATION_JSON) .accept(MediaType.APPLICATION_JSON)) - .andExpect(status().is2xxSuccessful()) + .andExpect(status().is4xxClientError()) .andReturn() .getResponse() .getContentAsString(); - // -- ASSERT -- - assertNotNull(response); - response = - this.mvc - .perform(get(SCENARIO_URI + "/" + SCENARIO_ID).accept(MediaType.APPLICATION_JSON)) + // --ASSERT-- + assertEquals( + "A contextual team with the same name already exists on this simulation", + JsonPath.read(response, "$.message")); + } + + @DisplayName("Given valid team ID and input, should update team successfully") + @Test + @WithMockAdminUser + void given_validTeamIdAndInput_should_updateTeamSuccessfully() throws Exception { + // --PREPARE-- + TeamCreateInput teamInput = createTeam(); + + Team team = new Team(); + team.setUpdateAttributes(teamInput); + team = teamRepository.save(team); + String newName = "updatedName"; + teamInput.setName(newName); + + // --EXECUTE-- + String response = + mvc.perform( + put(TEAM_URI + "/" + team.getId()) + .content(asJsonString(teamInput)) + .contentType(MediaType.APPLICATION_JSON) + .accept(MediaType.APPLICATION_JSON)) .andExpect(status().is2xxSuccessful()) .andReturn() .getResponse() .getContentAsString(); - assertEquals(new ArrayList<>(), JsonPath.read(response, "$.scenario_teams_users")); + + // --ASSERT-- + assertEquals(newName, JsonPath.read(response, "$.team_name")); } - @DisplayName("Remove a team on a scenario") + @DisplayName("Given valid team ID and input, should upsert team successfully") @Test - @Order(5) - @WithMockPlannerUser - void removeTeamOnScenarioTest() throws Exception { - // -- PREPARE -- - ScenarioUpdateTeamsInput input = new ScenarioUpdateTeamsInput(); - input.setTeamIds( - new ArrayList<>() { - { - add(TEAM_ID); - } - }); - - // -- EXECUTE -- + @WithMockAdminUser + void given_validTeamIdAndInput_should_upsertTeamSuccessfully() throws Exception { + // --PREPARE-- + TeamCreateInput teamInput = createTeam(); + + Team team = new Team(); + team.setUpdateAttributes(teamInput); + teamRepository.save(team); + String newName = "updatedName"; + teamInput.setName(newName); + + // --EXECUTE-- String response = - this.mvc - .perform( - put(SCENARIO_URI + "/" + SCENARIO_ID + "/teams/remove") - .content(asJsonString(input)) + mvc.perform( + post(TEAM_URI + "/upsert") + .content(asJsonString(teamInput)) .contentType(MediaType.APPLICATION_JSON) .accept(MediaType.APPLICATION_JSON)) .andExpect(status().is2xxSuccessful()) @@ -219,15 +203,65 @@ void removeTeamOnScenarioTest() throws Exception { .getResponse() .getContentAsString(); - // -- ASSERT -- - assertNotNull(response); - response = - this.mvc - .perform(get(SCENARIO_URI + "/" + SCENARIO_ID).accept(MediaType.APPLICATION_JSON)) + // --ASSERT-- + assertEquals(newName, JsonPath.read(response, "$.team_name")); + // --THEN-- + teamRepository.deleteById(JsonPath.read(response, "$.team_id")); + } + + @DisplayName("Given non existing and team input, should upsert team successfully") + @Test + @WithMockAdminUser + void given_nonExistingTeamInput_should_upsertTeamSuccessfully() throws Exception { + // --PREPARE-- + TeamCreateInput teamInput = createTeam(); + + // --EXECUTE-- + String response = + mvc.perform( + post(TEAM_URI + "/upsert") + .content(asJsonString(teamInput)) + .contentType(MediaType.APPLICATION_JSON) + .accept(MediaType.APPLICATION_JSON)) .andExpect(status().is2xxSuccessful()) .andReturn() .getResponse() .getContentAsString(); - assertEquals(new ArrayList<>(), JsonPath.read(response, "$.scenario_teams")); + + // --ASSERT-- + assertEquals(TEAM_NAME, JsonPath.read(response, "$.team_name")); + } + + @DisplayName("Given contextual team input with multiple exercise, should throw an exception") + @Test + @WithMockAdminUser + void given_contextualTeamWithMultipleExercise_should_throwAnException() { + // -- PREPARE -- + Exercise exercise1 = ExerciseFixture.getExercise(); + exercise1.setName("exercise 1"); + exercise1 = this.exerciseService.createExercise(exercise1); + Exercise exercise2 = ExerciseFixture.getExercise(); + exercise2.setName("exercise 2"); + exercise2 = this.exerciseService.createExercise(exercise2); + + TeamCreateInput teamInput = + createContextualExerciseTeam(List.of(exercise1.getId(), exercise2.getId())); + + // --EXECUTE-- + Exception exception = + assertThrows( + ServletException.class, + () -> + mvc.perform( + post(TEAM_URI + "/upsert") + .content(asJsonString(teamInput)) + .contentType(MediaType.APPLICATION_JSON) + .accept(MediaType.APPLICATION_JSON))); + + String expectedMessage = "Contextual team can only be associated to one exercise"; + String actualMessage = exception.getMessage(); + + // --ASSERT-- + assertTrue(actualMessage.contains(expectedMessage)); } } diff --git a/openbas-api/src/test/java/io/openbas/utils/fixtures/TeamFixture.java b/openbas-api/src/test/java/io/openbas/utils/fixtures/TeamFixture.java index 826f86c171..4bdfd7799f 100644 --- a/openbas-api/src/test/java/io/openbas/utils/fixtures/TeamFixture.java +++ b/openbas-api/src/test/java/io/openbas/utils/fixtures/TeamFixture.java @@ -2,11 +2,39 @@ import io.openbas.database.model.Team; import io.openbas.database.model.User; +import io.openbas.rest.team.form.TeamCreateInput; import java.util.ArrayList; +import java.util.List; public class TeamFixture { public static final String TEAM_NAME = "My team"; + public static final String CONTEXTUAL_TEAM_NAME = "My contextual team"; + + public static TeamCreateInput createTeam() { + TeamCreateInput teamCreateInput = new TeamCreateInput(); + teamCreateInput.setName(TEAM_NAME); + teamCreateInput.setDescription("Team description"); + return teamCreateInput; + } + + public static TeamCreateInput createContextualExerciseTeam(List exerciseIds) { + TeamCreateInput teamCreateInput = new TeamCreateInput(); + teamCreateInput.setName(CONTEXTUAL_TEAM_NAME); + teamCreateInput.setDescription("Team description"); + teamCreateInput.setContextual(true); + teamCreateInput.setExerciseIds(exerciseIds); + return teamCreateInput; + } + + public static TeamCreateInput createContextualScenarioTeam(List scenarioIds) { + TeamCreateInput teamCreateInput = new TeamCreateInput(); + teamCreateInput.setName("Scenario team"); + teamCreateInput.setDescription("Team description"); + teamCreateInput.setContextual(true); + teamCreateInput.setScenarioIds(scenarioIds); + return teamCreateInput; + } public static Team getTeam(final User user) { return getTeam(user, TEAM_NAME, false); // Call the other method with default value