Example usage for org.springframework.hateoas MediaTypes HAL_JSON

List of usage examples for org.springframework.hateoas MediaTypes HAL_JSON

Introduction

In this page you can find the example usage for org.springframework.hateoas MediaTypes HAL_JSON.

Prototype

MediaType HAL_JSON

To view the source code for org.springframework.hateoas MediaTypes HAL_JSON.

Click Source Link

Document

Public constant media type for application/hal+json .

Usage

From source file:com.jiwhiz.rest.admin.UserRestControllerTest.java

@Test
public void getUserAccountById_ShouldReturnUserAccount() throws Exception {
    UserAccount user = getTestLoggedInUser();
    when(userAccountRepositoryMock.findOne(USER_ID)).thenReturn(user);

    mockMvc.perform(get(API_ROOT + URL_ADMIN_USERS_USER, USER_USERNAME)).andExpect(status().isOk())
            .andExpect(content().contentType(MediaTypes.HAL_JSON)).andExpect(jsonPath("$.id", is(USER_ID)))
            .andExpect(jsonPath("$.displayName", is(USER_DISPLAY_NAME)))
            .andExpect(jsonPath("$._links.self.href", endsWith(URL_ADMIN_USERS + "/" + USER_USERNAME)))
            .andExpect(jsonPath("$._links.profile.href", endsWith("public/profiles/" + USER_USERNAME)))
            .andExpect(jsonPath("$._links.socialConnections.href",
                    endsWith("admin/users/" + USER_USERNAME + "/socialConnections")));
}

From source file:com.jiwhiz.rest.author.AuthorBlogCommentRestControllerTest.java

@Test
public void getCommentPostsByBlogPostId_ShouldReturnAllCommentPostsForBlog() throws Exception {
    UserAccount user = getTestLoggedInUserWithAuthorRole();
    when(userAccountServiceMock.getCurrentUser()).thenReturn(user);

    BlogPost blog = new BlogPost(user, "My Test Blog", "Hello!", "TEST");
    blog.setId(BLOG_ID);//w  ww  .  j av a  2s  . c  om
    when(blogPostRepositoryMock.findOne(BLOG_ID)).thenReturn(blog);

    when(commentPostRepositoryMock.findByBlogPostOrderByCreatedTimeDesc(any(BlogPost.class),
            any(Pageable.class))).thenReturn(
                    new PageImpl<CommentPost>(getTestUserCommentPostList(), new PageRequest(0, 10), 1));

    mockMvc.perform(get(API_ROOT + URL_AUTHOR_BLOGS_BLOG_COMMENTS, BLOG_ID)).andExpect(status().isOk())
            .andExpect(content().contentType(MediaTypes.HAL_JSON))
            .andExpect(jsonPath("$._embedded.commentPostList", hasSize(2)))
            .andExpect(jsonPath("$._embedded.commentPostList[0].id", is(COMMENTS_1_ID)))
            .andExpect(jsonPath("$._embedded.commentPostList[0].content", is(COMMENTS_1_CONTENT)))
            .andExpect(jsonPath("$._embedded.commentPostList[1].id", is(COMMENTS_2_ID)))
            .andExpect(jsonPath("$._embedded.commentPostList[1].content", is(COMMENTS_2_CONTENT)))
            .andExpect(jsonPath("$._links.self.templated", is(true)))
            .andExpect(jsonPath("$._links.self.href", endsWith(BLOG_ID + "/comments{?page,size,sort}")));
}

From source file:com.example.notes.GettingStartedDocumentation.java

@Test
public void index() throws Exception {
    this.mockMvc.perform(get("/").accept(MediaTypes.HAL_JSON)).andExpect(status().isOk())
            .andExpect(jsonPath("_links.notes", is(notNullValue())))
            .andExpect(jsonPath("_links.tags", is(notNullValue())));
}

From source file:com.jiwhiz.rest.site.WebsiteRestControllerTest.java

@Test
public void getPublicWebsiteResource_ShouldReturnWebsiteResource() throws Exception {
    UserAccount user = getTestLoggedInUserWithAdminRole();

    when(userAccountServiceMock.getCurrentUser()).thenReturn(user);

    mockMvc.perform(get(API_ROOT + URL_SITE)).andExpect(status().isOk())
            .andExpect(content().contentType(MediaTypes.HAL_JSON))
            //check links
            .andExpect(jsonPath("$._links.self.href", endsWith(URL_SITE)))
            .andExpect(jsonPath("$._links.blogs.templated", is(true)))
            .andExpect(jsonPath("$._links.blogs.href", endsWith(URL_SITE_BLOGS + "{?page,size,sort}")))
            .andExpect(jsonPath("$._links.blog.templated", is(true)))
            .andExpect(jsonPath("$._links.blog.href", endsWith(URL_SITE_BLOGS_BLOG)))
            .andExpect(jsonPath("$._links.profile.templated", is(true)))
            .andExpect(jsonPath("$._links.profile.href", endsWith(URL_SITE_PROFILES_USER)))
            .andExpect(jsonPath("$._links.latestBlog.href", endsWith(URL_SITE_LATEST_BLOG)))
            .andExpect(jsonPath("$._links.recentBlogs.href", endsWith(URL_SITE_RECENT_BLOGS)))
            .andExpect(jsonPath("$._links.recentComments.href", endsWith(URL_SITE_RECENT_COMMENTS)))
            .andExpect(jsonPath("$._links.tagCloud.href", endsWith(URL_SITE_TAG_CLOUDS)));
}

From source file:com.sra.biotech.submittool.persistence.client.RestClientConfiguration.java

public MappingJackson2HttpMessageConverter halConverter() {
    RelProvider defaultRelProvider = defaultRelProvider();
    RelProvider annotationRelProvider = annotationRelProvider();

    OrderAwarePluginRegistry<RelProvider, Class<?>> relProviderPluginRegistry = OrderAwarePluginRegistry
            .create(Arrays.asList(defaultRelProvider, annotationRelProvider));

    DelegatingRelProvider delegatingRelProvider = new DelegatingRelProvider(relProviderPluginRegistry);

    ObjectMapper halObjectMapper = new ObjectMapper();
    halObjectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    halObjectMapper.registerModule(new Jackson2HalModule());
    halObjectMapper//from w  ww .  j a va 2s .  c  om
            .setHandlerInstantiator(new Jackson2HalModule.HalHandlerInstantiator(delegatingRelProvider, null));

    MappingJackson2HttpMessageConverter halConverter = new MappingJackson2HttpMessageConverter();
    halConverter.setSupportedMediaTypes(Arrays.asList(MediaTypes.HAL_JSON));
    halConverter.setObjectMapper(halObjectMapper);
    return halConverter;
}

From source file:com.jiwhiz.rest.admin.CommentRestControllerTest.java

@Test
public void getCommentPostById_ShouldReturnCommentPost() throws Exception {
    UserAccount user = getTestLoggedInUserWithAdminRole();
    when(userAccountServiceMock.getCurrentUser()).thenReturn(user);

    CommentPost comment = getTestApprovedCommentPost();
    when(commentPostRepositoryMock.findOne(COMMENT_ID)).thenReturn(comment);

    mockMvc.perform(get(API_ROOT + URL_ADMIN_COMMENTS_COMMENT, COMMENT_ID)).andExpect(status().isOk())
            .andExpect(content().contentType(MediaTypes.HAL_JSON)).andExpect(jsonPath("$.id", is(COMMENT_ID)))
            .andExpect(jsonPath("$.content", is(COMMENT_CONTENT)))
            .andExpect(jsonPath("$._links.self.href", endsWith(URL_ADMIN_COMMENTS + "/" + COMMENT_ID)));
}

From source file:com.jiwhiz.rest.user.UserCommentRestControllerTest.java

@Test
public void getCommentPostById_ShouldReturnCommentPost() throws Exception {
    UserAccount user = getTestLoggedInUserWithAuthorRole();
    CommentPost comment = getTestApprovedCommentPost();

    when(userAccountServiceMock.getCurrentUser()).thenReturn(user);
    when(commentPostRepositoryMock.findOne(COMMENT_ID)).thenReturn(comment);

    mockMvc.perform(get(API_ROOT + URL_USER_COMMENTS_COMMENT, COMMENT_ID)).andExpect(status().isOk())
            .andExpect(content().contentType(MediaTypes.HAL_JSON)).andExpect(jsonPath("$.id", is(COMMENT_ID)))
            .andExpect(jsonPath("$.content", is(COMMENT_CONTENT)))
            .andExpect(jsonPath("$._links.self.href", endsWith(URL_USER_COMMENTS + "/" + COMMENT_ID)))
            .andExpect(jsonPath("$._links.user.href", endsWith(URL_USER)))
            .andExpect(jsonPath("$._links.blog.href", endsWith(URL_SITE_BLOGS + "/" + BLOG_ID)));
}

From source file:com.jiwhiz.rest.author.AuthorBlogRestControllerTest.java

@Test
public void getBlogPostById_ShouldReturnBlogPost() throws Exception {
    UserAccount user = getTestLoggedInUserWithAuthorRole();
    when(userAccountServiceMock.getCurrentUser()).thenReturn(user);

    BlogPost blog = getTestSinglePublishedBlogPost();
    when(blogPostRepositoryMock.findOne(BLOG_ID)).thenReturn(blog);

    mockMvc.perform(get(API_ROOT + URL_AUTHOR_BLOGS_BLOG, BLOG_ID)).andExpect(status().isOk())
            .andExpect(content().contentType(MediaTypes.HAL_JSON)).andExpect(jsonPath("$.id", is(BLOG_ID)))
            .andExpect(jsonPath("$.title", is(BLOG_TITLE)))
            .andExpect(jsonPath("$._links.self.href", endsWith(BLOG_ID.toString())))
            .andExpect(jsonPath("$._links.comments.templated", is(true)))
            .andExpect(jsonPath("$._links.comments.href",
                    endsWith("/author/blogs/" + BLOG_ID + "/comments{?page,size,sort}")));
}

From source file:example.company.UrlLevelSecurityTests.java

@Test
public void allowsPostRequestForAdmin() throws Exception {

    HttpHeaders headers = new HttpHeaders();
    headers.set(HttpHeaders.ACCEPT, "application/hal+json");
    headers.set(HttpHeaders.AUTHORIZATION, "Basic " + new String(Base64.encode(("ollie:gierke").getBytes())));

    mvc.perform(get("/employees").//
            headers(headers)).//
            andExpect(content().contentType(MediaTypes.HAL_JSON)).//
            andExpect(status().isOk()).//
            andDo(print());//from   w w w. j  a  v a  2s .c  om

    headers.set(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE);

    String location = mvc.perform(post("/employees").//
            content(PAYLOAD).//
            headers(headers)).//
            andExpect(status().isCreated()).//
            andDo(print()).//
            andReturn().getResponse().getHeader(HttpHeaders.LOCATION);

    ObjectMapper mapper = new ObjectMapper();

    String content = mvc.perform(get(location)).//
            andReturn().getResponse().getContentAsString();
    Employee employee = mapper.readValue(content, Employee.class);

    assertThat(employee.getFirstName(), is("Saruman"));
    assertThat(employee.getLastName(), is("the White"));
    assertThat(employee.getTitle(), is("Wizard"));
}

From source file:com.example.notes.GettingStartedDocumentation.java

String createNote() throws Exception {
    Map<String, String> note = new HashMap<String, String>();
    note.put("title", "Note creation with cURL");
    note.put("body", "An example of how to create a note using cURL");

    String noteLocation = this.mockMvc
            .perform(post("/notes").contentType(MediaTypes.HAL_JSON)
                    .content(objectMapper.writeValueAsString(note)))
            .andExpect(status().isCreated()).andExpect(header().string("Location", notNullValue())).andReturn()
            .getResponse().getHeader("Location");
    return noteLocation;
}