content: package io.reflectoring.coderadar.rest.useradministration.teams;
  
  import static io.reflectoring.coderadar.rest.JsonHelper.fromJson;
  import static org.springframework.restdocs.payload.PayloadDocumentation.requestFields;
  import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
  import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
  
  import io.reflectoring.coderadar.graph.useradministration.domain.TeamEntity;
  import io.reflectoring.coderadar.graph.useradministration.domain.UserEntity;
  import io.reflectoring.coderadar.graph.useradministration.repository.TeamRepository;
  import io.reflectoring.coderadar.graph.useradministration.repository.UserRepository;
  import io.reflectoring.coderadar.rest.ControllerTestTemplate;
  import io.reflectoring.coderadar.rest.JsonListWrapper;
  import io.reflectoring.coderadar.rest.domain.ErrorMessageResponse;
  import java.util.Collections;
  import java.util.List;
  import org.junit.jupiter.api.Assertions;
  import org.junit.jupiter.api.BeforeEach;
  import org.junit.jupiter.api.Test;
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.http.MediaType;
  import org.springframework.test.web.servlet.MvcResult;
  
  class AddUsersToTeamControllerIntegrationTest extends ControllerTestTemplate {
  
    @Autowired private TeamRepository teamRepository;
  
    @Autowired private UserRepository userRepository;
  
    private TeamEntity teamEntity;
    private UserEntity testUser;
  
    @BeforeEach
    void setUp() {
      testUser = new UserEntity();
      testUser.setUsername("username");
      testUser.setPassword("password1");
      userRepository.save(testUser);
  
      teamEntity = new TeamEntity();
      teamEntity.setName("testTeam");
      teamRepository.save(teamEntity, 1);
    }
  
    @Test
    void addUsersToTeamSuccessfully() throws Exception {
  
      ConstrainedFields<JsonListWrapper> fields = fields(JsonListWrapper.class);
  
      mvc()
          .perform(
              post("/api/teams/" + teamEntity.getId() + "/users")
                  .content(toJson(new JsonListWrapper<>(Collections.singletonList(testUser.getId()))))
                  .contentType(MediaType.APPLICATION_JSON))
          .andExpect(status().isOk())
          .andDo(
              document(
                  "teams/add/user",
                  requestFields(
                      fields.withPath("elements").description("A list containing user IDs"))))
          .andReturn();
  
      List<TeamEntity> teams = teamRepository.findAllWithMembers();
      Assertions.assertEquals(1L, teams.size());
      Assertions.assertEquals("testTeam", teams.get(0).getName());
      Assertions.assertEquals("username", teams.get(0).getMembers().get(0).getUsername());
    }
  
    @Test
    void throwsExceptionWhenTeamDoesNotExist() throws Exception {
      MvcResult result =
          mvc()
              .perform(
                  post("/api/teams/1000/users")
                      .content(
                          toJson(new JsonListWrapper<>(Collections.singletonList(testUser.getId()))))
                      .contentType(MediaType.APPLICATION_JSON))
              .andExpect(status().isNotFound())
              .andReturn();
  
      String errorMessage =
          fromJson(result.getResponse().getContentAsString(), ErrorMessageResponse.class)
              .getErrorMessage();
      Assertions.assertEquals("Team with id 1000 not found.", errorMessage);
    }
  
    @Test
    void throwsExceptionWhenUserDoesNotExist() throws Exception {
      MvcResult result =
          mvc()
              .perform(
                  post("/api/teams/" + teamEntity.getId() + "/users")
                      .content(toJson(new JsonListWrapper<>(Collections.singletonList(1000))))
                      .contentType(MediaType.APPLICATION_JSON))
              .andExpect(status().isNotFound())
              .andReturn();
  
      String errorMessage =
          fromJson(result.getResponse().getContentAsString(), ErrorMessageResponse.class)
              .getErrorMessage();
      Assertions.assertEquals("User with id 1000 not found.", errorMessage);
    }
  }
  