content: package io.reflectoring.coderadar.rest.branches;
  
  import static io.reflectoring.coderadar.rest.JsonHelper.fromJson;
  import static io.reflectoring.coderadar.rest.ResultMatchers.containsResource;
  import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
  import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
  
  import io.reflectoring.coderadar.projectadministration.port.driver.project.create.CreateProjectCommand;
  import io.reflectoring.coderadar.projectadministration.service.project.CreateProjectService;
  import io.reflectoring.coderadar.rest.ControllerTestTemplate;
  import io.reflectoring.coderadar.rest.domain.GetBranchResponse;
  import java.net.URL;
  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.test.web.servlet.result.MockMvcResultMatchers;
  
  class ListBranchesControllerTest extends ControllerTestTemplate {
  
    @Autowired private CreateProjectService createProjectService;
  
    private Long projectId;
  
    @BeforeEach
    void setUp() {
      URL testRepoURL = this.getClass().getClassLoader().getResource("test-repository");
      projectId =
          createProjectService.createProject(
              new CreateProjectCommand(
                  "testProject", null, null, testRepoURL.toString(), false, null, "master"));
    }
  
    @Test
    void listAllBranchesOfProjectWithId() throws Exception {
      // Test
      mvc()
          .perform(get("/api/projects/" + projectId + "/branches"))
          .andExpect(status().isOk())
          .andExpect(containsResource(GetBranchResponse[].class))
          .andExpect(
              result -> {
                GetBranchResponse[] branchResponses =
                    fromJson(result.getResponse().getContentAsString(), GetBranchResponse[].class);
                Assertions.assertEquals(3, branchResponses.length);
                Assertions.assertEquals("master", branchResponses[0].getName());
                Assertions.assertEquals("testBranch1", branchResponses[1].getName());
                Assertions.assertEquals("testBranch2", branchResponses[2].getName());
              })
          .andDo(document("branches/list"));
    }
  
    @Test
    void listAllBranchesOfProjectReturnsErrorWhenProjectNotFound() throws Exception {
      mvc()
          .perform(get("/api/projects/100/branches"))
          .andExpect(status().isNotFound())
          .andExpect(
              MockMvcResultMatchers.jsonPath("errorMessage").value("Project with id 100 not found."));
    }
  }
  