Search in sources :

Example 36 with SubjectAware

use of com.github.sdorra.shiro.SubjectAware in project scm-review-plugin by scm-manager.

the class PullRequestRootResourceTest method shouldCreateNewValidPullRequest.

@Test
@SubjectAware(username = "slarti")
public void shouldCreateNewValidPullRequest() throws URISyntaxException, IOException {
    mockPrincipal();
    mockChangesets("sourceBranch", "targetBranch", new Changeset());
    byte[] pullRequestJson = loadJson("com/cloudogu/scm/review/pullRequest.json");
    MockHttpRequest request = MockHttpRequest.post("/" + PullRequestRootResource.PULL_REQUESTS_PATH_V2 + "/space/name").content(pullRequestJson).contentType(PullRequestMediaType.PULL_REQUEST);
    dispatcher.invoke(request, response);
    assertEquals(HttpServletResponse.SC_CREATED, response.getStatus());
    assertThat(response.getOutputHeaders().getFirst("Location")).hasToString("/v2/pull-requests/space/name/1");
    PullRequest pullRequest = pullRequestStoreCaptor.getValue();
    assertThat(pullRequest.getAuthor()).isEqualTo("user1");
}
Also used : MockHttpRequest(org.jboss.resteasy.mock.MockHttpRequest) PullRequest(com.cloudogu.scm.review.pullrequest.service.PullRequest) TestData.createPullRequest(com.cloudogu.scm.review.TestData.createPullRequest) Changeset(sonia.scm.repository.Changeset) Test(org.junit.Test) SubjectAware(com.github.sdorra.shiro.SubjectAware)

Example 37 with SubjectAware

use of com.github.sdorra.shiro.SubjectAware in project scm-review-plugin by scm-manager.

the class PullRequestRootResourceTest method shouldSortPullRequestsByLastModified.

@Test
@SubjectAware(username = "rr")
public void shouldSortPullRequestsByLastModified() throws URISyntaxException, IOException {
    when(repositoryResolver.resolve(new NamespaceAndName(REPOSITORY_NAMESPACE, REPOSITORY_NAME))).thenReturn(repository);
    String firstPR = "first_PR";
    String toDayUpdatedPR = "to_day_updated_PR";
    String lastPR = "last_PR";
    Instant firstCreation = Instant.MIN;
    Instant firstCreationPlusOneDay = firstCreation.plus(Duration.ofDays(1));
    Instant lastCreation = Instant.MAX;
    PullRequest firstPullRequest = createPullRequest(firstPR);
    firstPullRequest.setCreationDate(firstCreationPlusOneDay);
    firstPullRequest.setLastModified(firstCreationPlusOneDay);
    PullRequest toDayUpdatedPullRequest = createPullRequest(toDayUpdatedPR);
    toDayUpdatedPullRequest.setCreationDate(firstCreation);
    toDayUpdatedPullRequest.setLastModified(Instant.now());
    PullRequest lastPullRequest = createPullRequest(lastPR);
    lastPullRequest.setCreationDate(lastCreation);
    lastPullRequest.setLastModified(lastCreation);
    when(store.getAll()).thenReturn(Lists.newArrayList(lastPullRequest, firstPullRequest, toDayUpdatedPullRequest));
    MockHttpRequest request = MockHttpRequest.get("/" + PullRequestRootResource.PULL_REQUESTS_PATH_V2 + "/" + REPOSITORY_NAMESPACE + "/" + REPOSITORY_NAME + "");
    dispatcher.invoke(request, response);
    assertThat(response.getStatus()).isEqualTo(200);
    ObjectMapper mapper = new ObjectMapper();
    JsonNode jsonNode = mapper.readValue(response.getContentAsString(), JsonNode.class);
    JsonNode prNode = jsonNode.get("_embedded").get("pullRequests");
    JsonNode pr_1 = prNode.path(0);
    JsonNode pr_2 = prNode.path(1);
    JsonNode pr_3 = prNode.path(2);
    assertThat(pr_1.get("id").asText()).isEqualTo(lastPR);
    assertThat(pr_2.get("id").asText()).isEqualTo(toDayUpdatedPR);
    assertThat(pr_3.get("id").asText()).isEqualTo(firstPR);
}
Also used : MockHttpRequest(org.jboss.resteasy.mock.MockHttpRequest) NamespaceAndName(sonia.scm.repository.NamespaceAndName) Instant(java.time.Instant) PullRequest(com.cloudogu.scm.review.pullrequest.service.PullRequest) TestData.createPullRequest(com.cloudogu.scm.review.TestData.createPullRequest) JsonNode(com.fasterxml.jackson.databind.JsonNode) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test) SubjectAware(com.github.sdorra.shiro.SubjectAware)

Example 38 with SubjectAware

use of com.github.sdorra.shiro.SubjectAware in project scm-review-plugin by scm-manager.

the class PullRequestRootResourceTest method shouldFailOnUpdatingNonExistingPullRequest.

@Test
@SubjectAware(username = "slarti")
public void shouldFailOnUpdatingNonExistingPullRequest() throws URISyntaxException, IOException {
    initRepoWithPRs("ns", "repo");
    when(store.get("opened_1")).thenThrow(new NotFoundException("x", "y"));
    MockHttpRequest request = MockHttpRequest.put("/" + PullRequestRootResource.PULL_REQUESTS_PATH_V2 + "/ns/repo/opened_1").content("{\"title\": \"new Title\", \"description\": \"new description\"}".getBytes()).contentType(PullRequestMediaType.PULL_REQUEST);
    dispatcher.invoke(request, response);
    assertThat(response.getContentAsString()).containsPattern("could not find.*");
    verify(store, never()).update(any());
}
Also used : MockHttpRequest(org.jboss.resteasy.mock.MockHttpRequest) NotFoundException(sonia.scm.NotFoundException) Test(org.junit.Test) SubjectAware(com.github.sdorra.shiro.SubjectAware)

Example 39 with SubjectAware

use of com.github.sdorra.shiro.SubjectAware in project scm-review-plugin by scm-manager.

the class PullRequestRootResourceTest method shouldGetAlreadyExistsExceptionOnCreatePR.

@Test
@SubjectAware(username = "slarti")
public void shouldGetAlreadyExistsExceptionOnCreatePR() throws URISyntaxException, IOException {
    PullRequest pr = new PullRequest();
    pr.setStatus(PullRequestStatus.OPEN);
    pr.setSource("source");
    pr.setTarget("target");
    pr.setTitle("title");
    pr.setId("id");
    when(store.getAll()).thenReturn(Lists.newArrayList(pr));
    byte[] pullRequestJson = "{\"source\": \"source\", \"target\": \"target\", \"title\": \"pull request\"}".getBytes();
    when(repositoryResolver.resolve(new NamespaceAndName(REPOSITORY_NAMESPACE, REPOSITORY_NAME))).thenReturn(repository);
    MockHttpRequest request = MockHttpRequest.post("/" + PullRequestRootResource.PULL_REQUESTS_PATH_V2 + "/" + REPOSITORY_NAMESPACE + "/" + REPOSITORY_NAME).content(pullRequestJson).contentType(PullRequestMediaType.PULL_REQUEST);
    dispatcher.invoke(request, response);
    assertThat(response.getContentAsString()).containsPattern("pull request with id id in repository with id .* already exists");
}
Also used : MockHttpRequest(org.jboss.resteasy.mock.MockHttpRequest) NamespaceAndName(sonia.scm.repository.NamespaceAndName) PullRequest(com.cloudogu.scm.review.pullrequest.service.PullRequest) TestData.createPullRequest(com.cloudogu.scm.review.TestData.createPullRequest) Test(org.junit.Test) SubjectAware(com.github.sdorra.shiro.SubjectAware)

Example 40 with SubjectAware

use of com.github.sdorra.shiro.SubjectAware in project scm-review-plugin by scm-manager.

the class PullRequestRootResourceTest method shouldGetSelfLink.

@Test
@SubjectAware(username = "rr")
public void shouldGetSelfLink() throws URISyntaxException, IOException {
    initRepoWithPRs("ns", "repo");
    MockHttpRequest request = MockHttpRequest.get("/" + PullRequestRootResource.PULL_REQUESTS_PATH_V2 + "/ns/repo");
    dispatcher.invoke(request, response);
    assertThat(response.getStatus()).isEqualTo(200);
    ObjectMapper mapper = new ObjectMapper();
    JsonNode jsonNode = mapper.readValue(response.getContentAsString(), JsonNode.class);
    JsonNode prNode = jsonNode.get("_embedded").get("pullRequests");
    prNode.elements().forEachRemaining(node -> {
        String actual = node.path("_links").path("self").path("href").asText();
        assertThat(actual).isEqualTo("/" + PullRequestRootResource.PULL_REQUESTS_PATH_V2 + "/ns/repo/" + node.get("id").asText());
    });
}
Also used : MockHttpRequest(org.jboss.resteasy.mock.MockHttpRequest) JsonNode(com.fasterxml.jackson.databind.JsonNode) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test) SubjectAware(com.github.sdorra.shiro.SubjectAware)

Aggregations

SubjectAware (com.github.sdorra.shiro.SubjectAware)91 Test (org.junit.Test)91 MockHttpRequest (org.jboss.resteasy.mock.MockHttpRequest)45 Comment.createComment (com.cloudogu.scm.review.comment.service.Comment.createComment)27 JsonNode (com.fasterxml.jackson.databind.JsonNode)18 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)16 PullRequest (com.cloudogu.scm.review.pullrequest.service.PullRequest)14 Reply.createReply (com.cloudogu.scm.review.comment.service.Reply.createReply)12 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)12 TestData.createPullRequest (com.cloudogu.scm.review.TestData.createPullRequest)11 Repository (sonia.scm.repository.Repository)9 BasicComment (com.cloudogu.scm.review.comment.service.BasicComment)6 BranchRevisionResolver (com.cloudogu.scm.review.pullrequest.dto.BranchRevisionResolver)6 HalEnricherContext (sonia.scm.api.v2.resources.HalEnricherContext)6 NamespaceAndName (sonia.scm.repository.NamespaceAndName)6 DisplayUser (sonia.scm.user.DisplayUser)6 User (sonia.scm.user.User)6 Changeset (sonia.scm.repository.Changeset)5 Comment (com.cloudogu.scm.review.comment.service.Comment)4 ShiroRule (com.github.sdorra.shiro.ShiroRule)3