Search in sources :

Example 11 with MockHttpRequest

use of org.jboss.resteasy.mock.MockHttpRequest in project scm-review-plugin by scm-manager.

the class CommentRootResourceTest method shouldNotThrowConflictExceptionIfOnlyOneExpectationInUrl.

@Test
@SubjectAware(username = "slarti", password = "secret")
public void shouldNotThrowConflictExceptionIfOnlyOneExpectationInUrl() throws URISyntaxException, UnsupportedEncodingException {
    when(pullRequestService.get(any(), any(), any())).thenReturn(PULL_REQUEST);
    when(branchRevisionResolver.getRevisions(any(), eq(PULL_REQUEST))).thenReturn(new BranchRevisionResolver.RevisionResult("source", "target"));
    byte[] commentJson = "{\"comment\" : \"this is my comment\"}".getBytes();
    MockHttpRequest request = MockHttpRequest.post("/" + PullRequestRootResource.PULL_REQUESTS_PATH_V2 + "/space/name/1/comments?sourceRevision=source").content(commentJson).contentType(MediaType.APPLICATION_JSON);
    dispatcher.invoke(request, response);
    assertEquals(HttpServletResponse.SC_CREATED, response.getStatus());
}
Also used : MockHttpRequest(org.jboss.resteasy.mock.MockHttpRequest) BranchRevisionResolver(com.cloudogu.scm.review.pullrequest.dto.BranchRevisionResolver) Test(org.junit.Test) SubjectAware(com.github.sdorra.shiro.SubjectAware)

Example 12 with MockHttpRequest

use of org.jboss.resteasy.mock.MockHttpRequest in project scm-review-plugin by scm-manager.

the class CommentRootResourceTest method shouldGetAllLinks.

@Test
@SubjectAware(username = "slarti", password = "secret")
public void shouldGetAllLinks() throws URISyntaxException, IOException {
    mockExistingComments();
    MockHttpRequest request = MockHttpRequest.get("/" + PullRequestRootResource.PULL_REQUESTS_PATH_V2 + "/space/name/1/comments").contentType(MediaType.APPLICATION_JSON);
    dispatcher.invoke(request, response);
    assertEquals(HttpServletResponse.SC_OK, response.getStatus());
    ObjectMapper mapper = new ObjectMapper();
    JsonNode jsonNode = mapper.readValue(response.getContentAsString(), JsonNode.class);
    JsonNode prNode = jsonNode.get("_embedded").get("pullRequestComments");
    JsonNode comment_1 = prNode.path(0);
    JsonNode comment_2 = prNode.path(1);
    assertThat(comment_1.get("_links").get("self").get("href").asText()).isEqualTo("/v2/pull-requests/space/name/1/comments/1");
    assertThat(comment_2.get("_links").get("self").get("href").asText()).isEqualTo("/v2/pull-requests/space/name/1/comments/2");
    assertThat(comment_1.get("_links").get("update").get("href").asText()).isEqualTo("/v2/pull-requests/space/name/1/comments/1");
    assertThat(comment_2.get("_links").get("update").get("href").asText()).isEqualTo("/v2/pull-requests/space/name/1/comments/2");
    assertThat(comment_1.get("_links").get("delete").get("href").asText()).isEqualTo("/v2/pull-requests/space/name/1/comments/1");
    // must not delete comment with responses
    assertThat(comment_2.get("_links").get("delete")).isNull();
    // root comments have reply links
    assertThat(comment_1.get("_links").get("reply").get("href").asText()).isEqualTo("/v2/pull-requests/space/name/1/comments/1/reply");
    assertThat(comment_2.get("_links").get("reply").get("href").asText()).isEqualTo("/v2/pull-requests/space/name/1/comments/2/reply");
    JsonNode replies = comment_2.get("_embedded").get("replies");
    JsonNode reply_1 = replies.path(0);
    JsonNode reply_2 = replies.path(1);
    assertThat(reply_1.get("_links").get("self").get("href").asText()).isEqualTo("/v2/pull-requests/space/name/1/comments/2/replies/2_1");
    assertThat(reply_2.get("_links").get("self").get("href").asText()).isEqualTo("/v2/pull-requests/space/name/1/comments/2/replies/2_2");
    assertThat(reply_1.get("_links").get("update").get("href").asText()).isEqualTo("/v2/pull-requests/space/name/1/comments/2/replies/2_1");
    assertThat(reply_2.get("_links").get("update").get("href").asText()).isEqualTo("/v2/pull-requests/space/name/1/comments/2/replies/2_2");
    assertThat(reply_1.get("_links").get("delete").get("href").asText()).isEqualTo("/v2/pull-requests/space/name/1/comments/2/replies/2_1");
    assertThat(reply_2.get("_links").get("delete").get("href").asText()).isEqualTo("/v2/pull-requests/space/name/1/comments/2/replies/2_2");
    // replies no longer have a reply link
    assertThat(reply_1.get("_links").get("reply")).isNull();
    assertThat(reply_2.get("_links").get("reply")).isNull();
}
Also used : MockHttpRequest(org.jboss.resteasy.mock.MockHttpRequest) JsonNode(com.fasterxml.jackson.databind.JsonNode) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test) SubjectAware(com.github.sdorra.shiro.SubjectAware)

Example 13 with MockHttpRequest

use of org.jboss.resteasy.mock.MockHttpRequest in project scm-review-plugin by scm-manager.

the class CommentRootResourceTest method shouldThrowConflictExceptionIfTargetRevisionNotEqualPullRequestTargetRevision.

@Test
@SubjectAware(username = "slarti", password = "secret")
public void shouldThrowConflictExceptionIfTargetRevisionNotEqualPullRequestTargetRevision() throws URISyntaxException, UnsupportedEncodingException {
    when(pullRequestService.get(any(), any(), any())).thenReturn(PULL_REQUEST);
    when(branchRevisionResolver.getRevisions(any(), eq(PULL_REQUEST))).thenReturn(new BranchRevisionResolver.RevisionResult("source", "target"));
    byte[] commentJson = "{\"comment\" : \"this is my comment\"}".getBytes();
    MockHttpRequest request = MockHttpRequest.post("/" + PullRequestRootResource.PULL_REQUESTS_PATH_V2 + "/space/name/1/comments?sourceRevision=source&targetRevision=other").content(commentJson).contentType(MediaType.APPLICATION_JSON);
    dispatcher.invoke(request, response);
    assertEquals(HttpServletResponse.SC_CONFLICT, response.getStatus());
    assertThat(response.getContentAsString()).contains("modified concurrently");
}
Also used : MockHttpRequest(org.jboss.resteasy.mock.MockHttpRequest) BranchRevisionResolver(com.cloudogu.scm.review.pullrequest.dto.BranchRevisionResolver) Test(org.junit.Test) SubjectAware(com.github.sdorra.shiro.SubjectAware)

Example 14 with MockHttpRequest

use of org.jboss.resteasy.mock.MockHttpRequest in project scm-review-plugin by scm-manager.

the class CommentRootResourceTest method shouldCreateNewComment.

@Test
@SubjectAware(username = "slarti", password = "secret")
public void shouldCreateNewComment() throws URISyntaxException {
    when(pullRequestService.get(any(), any(), any())).thenReturn(PULL_REQUEST);
    when(service.add(eq(REPOSITORY_NAMESPACE), eq(REPOSITORY_NAME), eq("1"), argThat(t -> t.getComment().equals("this is my comment")))).thenReturn("1");
    byte[] commentJson = "{\"comment\" : \"this is my comment\"}".getBytes();
    MockHttpRequest request = MockHttpRequest.post("/" + PullRequestRootResource.PULL_REQUESTS_PATH_V2 + "/space/name/1/comments?sourceRevision=source&targetRevision=target").content(commentJson).contentType(MediaType.APPLICATION_JSON);
    dispatcher.invoke(request, response);
    assertEquals(HttpServletResponse.SC_CREATED, response.getStatus());
    assertThat(response.getOutputHeaders().getFirst("Location")).hasToString("/v2/pull-requests/space/name/1/comments/1");
}
Also used : ContextLine.copy(com.cloudogu.scm.review.comment.service.ContextLine.copy) Provider(javax.inject.Provider) ArgumentMatchers.argThat(org.mockito.ArgumentMatchers.argThat) ShiroRule(com.github.sdorra.shiro.ShiroRule) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) RestDispatcher(sonia.scm.web.RestDispatcher) URISyntaxException(java.net.URISyntaxException) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) BranchRevisionResolver(com.cloudogu.scm.review.pullrequest.dto.BranchRevisionResolver) Repository(sonia.scm.repository.Repository) PullRequest(com.cloudogu.scm.review.pullrequest.service.PullRequest) PullRequestRootResource(com.cloudogu.scm.review.pullrequest.api.PullRequestRootResource) PullRequestMapperImpl(com.cloudogu.scm.review.pullrequest.dto.PullRequestMapperImpl) MediaType(javax.ws.rs.core.MediaType) Comment(com.cloudogu.scm.review.comment.service.Comment) Arrays.asList(java.util.Arrays.asList) JsonNode(com.fasterxml.jackson.databind.JsonNode) UriBuilder(javax.ws.rs.core.UriBuilder) PullRequestService(com.cloudogu.scm.review.pullrequest.service.PullRequestService) RepositoryResolver(com.cloudogu.scm.review.RepositoryResolver) PullRequestResource(com.cloudogu.scm.review.pullrequest.api.PullRequestResource) ExecutedTransition(com.cloudogu.scm.review.comment.service.ExecutedTransition) ChannelRegistry(sonia.scm.sse.ChannelRegistry) MockHttpRequest(org.jboss.resteasy.mock.MockHttpRequest) CommentService(com.cloudogu.scm.review.comment.service.CommentService) UserDisplayManager(sonia.scm.user.UserDisplayManager) InlineContext(com.cloudogu.scm.review.comment.service.InlineContext) Providers(com.google.inject.util.Providers) UriInfo(javax.ws.rs.core.UriInfo) MockitoJUnitRunner(org.mockito.junit.MockitoJUnitRunner) UnsupportedEncodingException(java.io.UnsupportedEncodingException) Mockito.mock(org.mockito.Mockito.mock) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) MockHttpResponse(org.jboss.resteasy.mock.MockHttpResponse) Mock(org.mockito.Mock) RunWith(org.junit.runner.RunWith) ArrayList(java.util.ArrayList) Reply(com.cloudogu.scm.review.comment.service.Reply) Lists(com.google.common.collect.Lists) ImmutableList(com.google.common.collect.ImmutableList) Reply.createReply(com.cloudogu.scm.review.comment.service.Reply.createReply) RepositoryServiceFactory(sonia.scm.repository.api.RepositoryServiceFactory) MockedDiffLine(com.cloudogu.scm.review.comment.service.MockedDiffLine) Before(org.junit.Before) InjectMocks(org.mockito.InjectMocks) TestData(com.cloudogu.scm.review.TestData) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) HttpServletResponse(javax.servlet.http.HttpServletResponse) Test(org.junit.Test) IOException(java.io.IOException) Mockito.when(org.mockito.Mockito.when) CommentTransition(com.cloudogu.scm.review.comment.service.CommentTransition) SubjectAware(com.github.sdorra.shiro.SubjectAware) Rule(org.junit.Rule) Location(com.cloudogu.scm.review.comment.service.Location) Comment.createComment(com.cloudogu.scm.review.comment.service.Comment.createComment) Assert.assertEquals(org.junit.Assert.assertEquals) MockHttpRequest(org.jboss.resteasy.mock.MockHttpRequest) Test(org.junit.Test) SubjectAware(com.github.sdorra.shiro.SubjectAware)

Example 15 with MockHttpRequest

use of org.jboss.resteasy.mock.MockHttpRequest in project scm-review-plugin by scm-manager.

the class CommentResourceTest method shouldNotDeleteReplyIfPullRequestHasChanged.

@Test
public void shouldNotDeleteReplyIfPullRequestHasChanged() throws URISyntaxException, UnsupportedEncodingException {
    MockHttpRequest request = MockHttpRequest.delete("/" + PullRequestRootResource.PULL_REQUESTS_PATH_V2 + "/space/name/1/comments/1/replies/x?sourceRevision=wrong").contentType(MediaType.APPLICATION_JSON);
    dispatcher.invoke(request, response);
    verify(service, never()).delete(any(), any(), any(), any());
    Assertions.assertThat(response.getContentAsString()).contains("modified concurrently");
}
Also used : MockHttpRequest(org.jboss.resteasy.mock.MockHttpRequest) Test(org.junit.Test)

Aggregations

MockHttpRequest (org.jboss.resteasy.mock.MockHttpRequest)108 Test (org.junit.Test)64 SubjectAware (com.github.sdorra.shiro.SubjectAware)46 Test (org.junit.jupiter.api.Test)41 JsonNode (com.fasterxml.jackson.databind.JsonNode)29 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)24 PullRequest (com.cloudogu.scm.review.pullrequest.service.PullRequest)17 TestData.createPullRequest (com.cloudogu.scm.review.TestData.createPullRequest)16 DisplayUser (sonia.scm.user.DisplayUser)16 User (sonia.scm.user.User)16 MockHttpResponse (org.jboss.resteasy.mock.MockHttpResponse)14 NamespaceAndName (sonia.scm.repository.NamespaceAndName)13 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)9 Repository (sonia.scm.repository.Repository)9 Comment (com.cloudogu.scm.review.comment.service.Comment)8 BranchRevisionResolver (com.cloudogu.scm.review.pullrequest.dto.BranchRevisionResolver)8 UnsupportedEncodingException (java.io.UnsupportedEncodingException)8 URISyntaxException (java.net.URISyntaxException)8 ArgumentMatchers.any (org.mockito.ArgumentMatchers.any)8 ArgumentMatchers.argThat (org.mockito.ArgumentMatchers.argThat)8