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.greglturnquist.springdatarest.HoppingRightAlong.java

public HoppingRightAlong() throws URISyntaxException {
    traverson = new Traverson(new URI("http://localhost:8080/api/"), MediaTypes.HAL_JSON);
}

From source file:org.springsource.restbucks.engine.web.EngineControllerIntegrationTests.java

@Test
public void customControllerReturnsDefaultMediaType() throws Exception {

    MockHttpServletResponse response = mvc.perform(get("/")).//
            andDo(MockMvcResultHandlers.print()).//
            andExpect(linkWithRelIsPresent(ENGINE_REL)). //
            andReturn().getResponse();//from  ww w.  ja v  a2 s  .  co m

    LinkDiscoverer discoverer = links.getLinkDiscovererFor(response.getContentType());
    Link link = discoverer.findLinkWithRel(ENGINE_REL, response.getContentAsString());

    mvc.perform(get(link.getHref())). //
            andExpect(content().contentTypeCompatibleWith(MediaTypes.HAL_JSON));
}

From source file:io.github.howiefh.jeews.modules.sys.controller.LoginControllerTest.java

@Test
public void testLogin() throws Exception {
    String user = "{\"username\":\"root\",\"password\":\"u12345\"}";
    MvcResult result = mockMvc//from   w w  w.j a  va2  s.c o  m
            .perform(post("/login").contentType(MediaType.APPLICATION_JSON).content(user)
                    .accept(MediaTypes.HAL_JSON))
            .andExpect(status().isOk()) // 200
            .andExpect(jsonPath("$.access_token").exists()).andExpect(jsonPath("$.user.id").exists())
            .andReturn();
    String content = result.getResponse().getContentAsString();
    JSONObject json = new JSONObject(content);
    String accessToken = json.get("access_token").toString();

    mockMvc.perform(
            get("/users/1").header("Authorization", "Bearer " + accessToken).accept(MediaTypes.HAL_JSON))
            .andExpect(status().isOk()) // 200
            .andExpect(content().contentType(MediaTypes.HAL_JSON)) // ??contentType
            .andReturn();
    mockMvc.perform(get("/users/1")).andExpect(status().isUnauthorized()) // 401
            .andReturn();
    String requestBody = "{\"username\":\"fh" + UUID.randomUUID() + "\",\"password\":\"123456\",\"email\":\""
            + UUID.randomUUID() + "@qq.om\",\"mobile\":" + "\"" + new Random().nextInt()
            + "\",\"roles\":[1,2],\"organizations\":[1],\"locked\":true}";
    mockMvc.perform(post("/users").header("Authorization", "Bearer " + accessToken)
            .contentType(MediaType.APPLICATION_JSON).content(requestBody).accept(MediaTypes.HAL_JSON)) // 
            .andExpect(status().isCreated()) // 201
            .andExpect(jsonPath("$.id").exists()) // Json path?JSON
            .andExpect(content().contentType(MediaTypes.HAL_JSON)).andReturn();
}

From source file:example.customers.integration.StoreIntegration.java

private void discoverByLocationLink() {

    try {//from ww w. ja  v a  2 s  . co  m
        URI storesUri = URI.create(env.getProperty("integration.stores.uri"));
        log.info("Trying to access the stores system at {}", storesUri);

        Traverson traverson = new Traverson(storesUri, MediaTypes.HAL_JSON);
        this.storesByLocationLink = traverson.follow("stores", "search", "by-location").asLink();

        log.info("Found stores-by-location link pointing to {}.", storesByLocationLink.getHref());

    } catch (RuntimeException o_O) {
        this.storesByLocationLink = null;
        log.info("Stores system unavailable. Got: ", o_O.getMessage());
    }
}

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

@Test
public void getAdminAccount_ShouldReturnCurrentAdminAccount() throws Exception {
    UserAccount adminUser = getTestLoggedInUserWithAdminRole();

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

    mockMvc.perform(get(API_ROOT + URL_ADMIN)).andExpect(status().isOk())
            .andExpect(content().contentType(MediaTypes.HAL_JSON))
            .andExpect(jsonPath("$._links.self.href", endsWith(URL_ADMIN)))
            .andExpect(jsonPath("$._links.users.templated", is(true)))
            .andExpect(jsonPath("$._links.users.href", endsWith(URL_ADMIN_USERS + "{?page,size,sort}")))
            .andExpect(jsonPath("$._links.comments.templated", is(true)))
            .andExpect(jsonPath("$._links.comments.href", endsWith(URL_ADMIN_COMMENTS + "{?page,size,sort}")))
            .andExpect(jsonPath("$._links.blogs.templated", is(true)))
            .andExpect(jsonPath("$._links.blogs.href", endsWith(URL_ADMIN_BLOGS + "{?page,size,sort}")));
}

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

@Test
public void getBlogPosts_ShouldReturnAllBlogPostsInSystem() throws Exception {
    Page<BlogPost> page = new PageImpl<BlogPost>(getTestPublishedBlogPostList(), new PageRequest(0, 10), 2);

    when(blogPostRepositoryMock.findAll(any(Pageable.class))).thenReturn(page);

    mockMvc.perform(get(API_ROOT + URL_ADMIN_BLOGS)).andExpect(status().isOk())
            .andExpect(content().contentType(MediaTypes.HAL_JSON))
            .andExpect(jsonPath("$._embedded.blogPostList", hasSize(2)))
            .andExpect(jsonPath("$._embedded.blogPostList[0].id", is(BLOGS_1_ID)))
            .andExpect(jsonPath("$._embedded.blogPostList[0].title", is(BLOGS_1_TITLE)))
            .andExpect(jsonPath("$._embedded.blogPostList[1].id", is(BLOGS_2_ID)))
            .andExpect(jsonPath("$._embedded.blogPostList[1].title", is(BLOGS_2_TITLE)))
            .andExpect(jsonPath("$._links.self.templated", is(true)))
            .andExpect(jsonPath("$._links.self.href", endsWith(URL_ADMIN_BLOGS + "{?page,size,sort}")));
}

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

@Test
public void getCurrentAuthor_ShouldReturnCurrentUserAndLinks() throws Exception {
    UserAccount user = getTestLoggedInUserWithAuthorRole();

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

    mockMvc.perform(get(API_ROOT + URL_AUTHOR)).andExpect(status().isOk())
            .andExpect(content().contentType(MediaTypes.HAL_JSON))
            .andExpect(jsonPath("$.displayName", is(USER_DISPLAY_NAME)))
            .andExpect(jsonPath("$.admin", is(false))).andExpect(jsonPath("$.author", is(true)))
            .andExpect(jsonPath("$._links.self.href", endsWith(URL_AUTHOR)))
            .andExpect(jsonPath("$._links.blogs.templated", is(true)))
            .andExpect(jsonPath("$._links.blogs.href", endsWith(URL_AUTHOR_BLOGS + "{?page,size,sort}")))
            .andExpect(jsonPath("$._links.blog.templated", is(true)))
            .andExpect(jsonPath("$._links.blog.href", endsWith(URL_AUTHOR_BLOGS_BLOG)));
}

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

@Test
public void getUserAccounts_ShouldReturnAllUserAccounts() throws Exception {
    Page<UserAccount> page = new PageImpl<UserAccount>(getTestUserAccountList(), new PageRequest(0, 10), 1);

    when(userAccountRepositoryMock.findAll(any(Pageable.class))).thenReturn(page);

    mockMvc.perform(get(API_ROOT + URL_ADMIN_USERS)).andExpect(status().isOk())
            .andExpect(content().contentType(MediaTypes.HAL_JSON))
            .andExpect(jsonPath("$._embedded.userAccountList", hasSize(2)))
            .andExpect(jsonPath("$._embedded.userAccountList[0].id", is(USERS_1_USER_ID)))
            .andExpect(jsonPath("$._embedded.userAccountList[1].id", is(USERS_2_USER_ID)))
            .andExpect(jsonPath("$._links.self.templated", is(true)))
            .andExpect(jsonPath("$._links.self.href", endsWith(URL_ADMIN_USERS + "{?page,size,sort}")));
}

From source file:example.company.UrlLevelSecurityTests.java

@Test
public void allowsAccessToRootResource() throws Exception {

    mvc.perform(get("/").//
            accept(MediaTypes.HAL_JSON)).//
            andExpect(header().string("Content-Type", MediaTypes.HAL_JSON.toString())).//
            andExpect(status().isOk()).//
            andDo(print());//from  w w  w  .  j a va 2  s . com
}