Example usage for org.springframework.http MediaType APPLICATION_JSON_UTF8

List of usage examples for org.springframework.http MediaType APPLICATION_JSON_UTF8

Introduction

In this page you can find the example usage for org.springframework.http MediaType APPLICATION_JSON_UTF8.

Prototype

MediaType APPLICATION_JSON_UTF8

To view the source code for org.springframework.http MediaType APPLICATION_JSON_UTF8.

Click Source Link

Document

Public constant media type for application/json;charset=UTF-8 .

Usage

From source file:com.hp.autonomy.frontend.find.core.savedsearches.query.AbstractSavedQueryIT.java

@Test
public void createAndFetch() throws Exception {
    // Query text containing U+1F435
    final String queryText = "monkey face character \uD83D\uDC35";

    final byte[] requestBytes = IOUtils.toByteArray(saveQueryRequestResource.getInputStream());

    final MockHttpServletRequestBuilder requestBuilder = post(SavedQueryController.PATH + '/')
            .content(requestBytes).contentType(MediaType.APPLICATION_JSON).with(authentication(biAuth()));

    final MvcResult createResult = mockMvc.perform(requestBuilder).andReturn();

    final JsonNode responseTree = mapper.readTree(createResult.getResponse().getContentAsString());
    final int id = responseTree.get("id").asInt();

    listSavedQueries().andExpect(status().isOk())
            .andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8))
            .andExpect(jsonPath("$[0].id", is(id)))
            .andExpect(jsonPath("$[0].title", is("\u30e2\u30f3\u30ad\u30fc")))
            .andExpect(jsonPath("$[0].queryText", is(queryText)))
            .andExpect(jsonPath("$[0].minDate", is(1400000000)))
            .andExpect(jsonPath("$[0].maxDate", is(1500000000)))
            .andExpect(jsonPath("$[0].conceptClusterPhrases", hasSize(3)))
            .andExpect(jsonPath("$[0].conceptClusterPhrases[*].phrase",
                    containsInAnyOrder("characters", "faces", "animals")))
            .andExpect(//from   w w w.  ja  va 2s.co m
                    jsonPath("$[0].conceptClusterPhrases[?(@.phrase=='characters')].primary", contains(true)))
            .andExpect(jsonPath("$[0].conceptClusterPhrases[?(@.phrase=='characters')].clusterId", contains(1)))
            .andExpect(jsonPath("$[0].conceptClusterPhrases[?(@.phrase=='faces')].primary", contains(false)))
            .andExpect(jsonPath("$[0].conceptClusterPhrases[?(@.phrase=='faces')].clusterId", contains(1)))
            .andExpect(jsonPath("$[0].conceptClusterPhrases[?(@.phrase=='animals')].primary", contains(true)))
            .andExpect(jsonPath("$[0].conceptClusterPhrases[?(@.phrase=='animals')].clusterId", contains(2)))
            .andExpect(jsonPath("$[0].parametricValues", hasSize(1)))
            .andExpect(jsonPath("$[0].parametricValues[0].field", is("CATEGORY")))
            .andExpect(jsonPath("$[0].parametricValues[0].value", is("COMPUTING")))
            .andExpect(jsonPath("$[0].parametricRanges", hasSize(1)))
            .andExpect(jsonPath("$[0].parametricRanges[0].field", is("SOME_DATE")))
            .andExpect(jsonPath("$[0].parametricRanges[0].min", is(123456789d)))
            .andExpect(jsonPath("$[0].parametricRanges[0].max", is(123456791d)))
            .andExpect(jsonPath("$[0].parametricRanges[0].type", is("Date")))
            .andExpect(jsonPath("$[0].indexes", hasSize(2)))
            .andExpect(jsonPath("$[0].indexes[*].name",
                    containsInAnyOrder("English Wikipedia", "\u65e5\u672c\u8a9e Wikipedia")))
            .andExpect(jsonPath("$[0].indexes[?(@.name=='English Wikipedia')].domain", contains("MY_DOMAIN")))
            .andExpect(jsonPath("$[0].indexes[?(@.name=='\u65e5\u672c\u8a9e Wikipedia')].domain",
                    contains("MY_DOMAIN")));
}

From source file:com.toptal.controller.EntryControllerTest.java

/**
 * Any user can delete an entry.// w  ww  .  j  a v  a2s.c om
 * @throws Exception If smth goes wrong.
 */
@Test
public final void userDeletes() throws Exception {
    final String name = "nameE";
    final String password = "passwordE";
    final User user = this.users
            .save(User.builder().name(name).password(password).role(Role.ROLE_USER).build());
    final Entry entry = this.entries.save(new Entry(null, new Date(), 1002L, 102L, user));
    this.getMvc()
            .perform(MockMvcRequestBuilders.delete(this.subPath(entry.getId()))
                    .contentType(MediaType.APPLICATION_JSON_UTF8)
                    .headers(this.addAuth(new HttpHeaders(), name, password)))
            .andDo(MockMvcResultHandlers.print()).andExpect(MockMvcResultMatchers.status().isOk());
}

From source file:com.snv.user.UserControllerTest.java

@Test
public void should_return_user_on_get() throws Exception {
    when(this.userService.get(Matchers.anyLong())).thenReturn(this.user);

    MvcResult result = mockMvc//w ww .  j ava 2s  . com
            .perform(MockMvcRequestBuilders.get(CONTROLLER_URL + "/1").accept(MediaType.APPLICATION_JSON_UTF8))
            .andExpect(MockMvcResultMatchers.status().is2xxSuccessful()).andReturn();

    verify(this.userService, times(1)).get(Matchers.anyLong());

    assertFalse(result.getResponse().getContentAsString().isEmpty());
    User actual = objectMapper.readValue(result.getResponse().getContentAsString(), User.class);
    assertNotNull("user created can not be null !", actual);
    assertEquals("users not same !", actual, this.user);
    assertEquals("user's first name is not same", actual.getFirstName(), this.user.getFirstName());
    assertEquals("user's last name is not same", actual.getLastName(), this.user.getLastName());
    assertEquals("user's email name is not same", actual.getEmail(), this.user.getEmail());
    assertEquals("user's login name is not same", actual.getLogin(), this.user.getLogin());
    assertEquals("user's password name is not same", actual.getPassword(), this.user.getPassword());
}

From source file:com.crossover.trial.weather.endpoint.RestWeatherCollectorEndpointTest.java

@Test
@DatabaseSetup("classpath:dbtest/airport_small.xml")
public void testDeleteAirportWithMeasurements() throws Exception {
    mockMvc.perform(post("/collect/weather/bos/cloud_cover").contentType(MediaType.APPLICATION_JSON_UTF8)
            .content("{ " + "\"mean\": 0.0," + "\"first\": 10," + "\"second\": 20," + "\"third\": 30,"
                    + "\"count\": 40" + "}"))
            .andExpect(status().isOk());

    mockMvc.perform(delete("/collect/airport/bos")).andExpect(status().isNoContent());

    mockMvc.perform(delete("/collect/airport/bos")).andExpect(status().isNotFound());
}

From source file:com.hp.autonomy.frontend.find.idol.comparison.ComparisonServiceIT.java

@Test
public void getResults() throws Exception {
    final String[] stateMatchIds = { sixDocStateToken };
    final String[] stateDontMatchIds = { twoDocStateToken };

    final MockHttpServletRequestBuilder requestBuilder = get(
            ComparisonController.BASE_PATH + '/' + ComparisonController.RESULTS_PATH + '/')
                    .param(ComparisonController.STATE_MATCH_PARAM, stateMatchIds)
                    .param(ComparisonController.STATE_DONT_MATCH_PARAM, stateDontMatchIds)
                    .param(ComparisonController.RESULTS_START_PARAM, "1")
                    .param(ComparisonController.MAX_RESULTS_PARAM, "6")
                    .param(ComparisonController.SUMMARY_PARAM, "context")
                    .param(ComparisonController.SORT_PARAM, "relevance")
                    .param(ComparisonController.HIGHLIGHT_PARAM, "false")
                    .contentType(MediaType.APPLICATION_JSON).with(authentication(biAuth()));

    mockMvc.perform(requestBuilder).andExpect(status().isOk())
            .andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8))
            .andExpect(jsonPath("$.documents", hasSize(4)));
}

From source file:com.toptal.controller.UserControllerTest.java

/**
 * Anonymous cannot list all users.//ww w .  j a va 2 s. co  m
 * @throws Exception If smth goes wrong.
 */
@Test
public final void anonymousDoesNotAccessList() throws Exception {
    final User user = User.builder().name("aaaaaa").password("bbbbb").role(Role.ROLE_USER).build();
    final User saved = this.dao.save(user);
    Assert.assertNotNull(this.dao.findOne(saved.getId()));
    this.getMvc()
            .perform(MockMvcRequestBuilders.get(UserController.PATH)
                    .contentType(MediaType.APPLICATION_JSON_UTF8))
            .andDo(MockMvcResultHandlers.print()).andExpect(MockMvcResultMatchers.status().isUnauthorized());
}

From source file:com.snv.bank.account.AccountControllerTest.java

@Test
public void should_return_list_of_account_on_getall() throws Exception {
    List<Account> accounts = Arrays.asList(this.account);
    when(this.accountService.getAll()).thenReturn(accounts);

    MvcResult result = mockMvc//ww  w .  j  a v a  2s  . c  om
            .perform(MockMvcRequestBuilders.get(CONTROLLER_URL).accept(MediaType.APPLICATION_JSON_UTF8))
            .andExpect(MockMvcResultMatchers.status().is2xxSuccessful()).andReturn();

    verify(this.accountService, times(1)).getAll();

    assertFalse(result.getResponse().getContentAsString().isEmpty());
    List<Account> actual = objectMapper.readValue(result.getResponse().getContentAsString(), List.class);

    assertNotNull("List of accounts can not be null !", actual);
    assertFalse("List of accounts can not be empty", actual.isEmpty());
}

From source file:com.toptal.controller.EntryControllerTest.java

/**
 * Any user can filter entries.//ww w .j  a  v a 2s.  c  o m
 * @throws Exception If smth goes wrong.
 */
@Test
public final void userFilters() throws Exception {
    final SimpleDateFormat sdf = new SimpleDateFormat(Format.DATE_FORMAT, Locale.getDefault());
    final String name = "nameF";
    final String password = "passwordF";
    final User user = this.users
            .save(User.builder().name(name).password(password).role(Role.ROLE_USER).build());
    final Date date = new Date();
    this.entries.save(new Entry(null, date, 1003L, 103L, user));
    this.getMvc().perform(MockMvcRequestBuilders
            .get(String.format("%s/%s/%s", EntryController.PATH, sdf.format(new Date(date.getTime() - 1)),
                    sdf.format(new Date(date.getTime() + 1))))
            .contentType(MediaType.APPLICATION_JSON_UTF8)
            .headers(this.addAuth(new HttpHeaders(), name, password))).andDo(MockMvcResultHandlers.print())
            .andExpect(MockMvcResultMatchers.status().isOk());
}

From source file:com.toptal.controller.UserControllerTest.java

/**
 * User cannot list all users.//from w  w  w  .  j ava 2s. c  o m
 * @throws Exception If smth goes wrong.
 */
@Test
public final void userDoesNotAccessList() throws Exception {
    final String name = "newUserToTestList";
    final String password = "asdoqissfdfa";
    final User user = User.builder().name(name).password(password).role(Role.ROLE_USER).build();
    final User saved = this.dao.save(user);
    Assert.assertNotNull(this.dao.findOne(saved.getId()));
    this.getMvc()
            .perform(
                    MockMvcRequestBuilders.get(UserController.PATH).contentType(MediaType.APPLICATION_JSON_UTF8)
                            .headers(this.addAuth(new HttpHeaders(), name, password)))
            .andDo(MockMvcResultHandlers.print()).andExpect(MockMvcResultMatchers.status().isForbidden());
}

From source file:com.snv.user.UserControllerTest.java

@Test
public void should_return_boolean_true_on_delete_success() throws Exception {
    when(this.userService.delete(Matchers.any(User.class))).thenReturn(Boolean.TRUE);

    MvcResult result = mockMvc//from  w w w .j a  v a  2s .  co  m
            .perform(MockMvcRequestBuilders.delete(CONTROLLER_URL).content(this.mapper.writeValueAsString(user))
                    .contentType(MediaType.APPLICATION_JSON_UTF8).accept(MediaType.APPLICATION_JSON_UTF8))
            .andExpect(MockMvcResultMatchers.status().is2xxSuccessful()).andReturn();

    verify(this.userService, times(1)).delete(Matchers.any(User.class));

    Boolean actual = objectMapper.readValue(result.getResponse().getContentAsString(), Boolean.class);
    assertNotNull("result on delete can not be null !", actual);
    assertTrue("result on delete is not true !", actual);
}