@Test public void noParamGreetingShouldReturnDefaultMessage() throws Exception { this.mockMvc.perform(get("/greeting")).andDo(print()).andExpect(status().isOk()) .andExpect(jsonPath("$.content").value("Hello, World!")); }
@Test public void paramGreetingShouldReturnTailoredMessage() throws Exception { this.mockMvc.perform(get("/greeting").param("name", "Spring Community")) .andDo(print()).andExpect(status().isOk()) .andExpect(jsonPath("$.content").value("Hello, Spring Community!")); }
@Test public void hamcrestMatcher() throws Exception { this.mockMvc.perform(get("/music/people")) .andExpect(jsonPath("$.composers[0].name", startsWith("Johann"))) .andExpect(jsonPath("$.performers[0].name", endsWith("Ashkenazy"))) .andExpect(jsonPath("$.performers[1].name", containsString("di Me"))) .andExpect(jsonPath("$.composers[1].name", isIn(Arrays.asList("Johann Sebastian Bach", "Johannes Brahms")))); }
@Test public void exists() throws Exception { String composerByName = "$.composers[?(@.name == '%s')]"; String performerByName = "$.performers[?(@.name == '%s')]"; this.mockMvc.perform(get("/music/people")) .andExpect(jsonPath(composerByName, "Johann Sebastian Bach").exists()) .andExpect(jsonPath(composerByName, "Johannes Brahms").exists()) .andExpect(jsonPath(composerByName, "Edvard Grieg").exists()) .andExpect(jsonPath(composerByName, "Robert Schumann").exists()) .andExpect(jsonPath(performerByName, "Vladimir Ashkenazy").exists()) .andExpect(jsonPath(performerByName, "Yehudi Menuhin").exists()) .andExpect(jsonPath("$.composers[0]").exists()) .andExpect(jsonPath("$.composers[1]").exists()) .andExpect(jsonPath("$.composers[2]").exists()) .andExpect(jsonPath("$.composers[3]").exists()); }
@Test public void equality() throws Exception { this.mockMvc.perform(get("/music/people")) .andExpect(jsonPath("$.composers[0].name").value("Johann Sebastian Bach")) .andExpect(jsonPath("$.performers[1].name").value("Yehudi Menuhin")); // Hamcrest matchers... this.mockMvc.perform(get("/music/people")) .andExpect(jsonPath("$.composers[0].name").value(equalTo("Johann Sebastian Bach"))) .andExpect(jsonPath("$.performers[1].name").value(equalTo("Yehudi Menuhin"))); }
@Test public void doesNotExist() throws Exception { this.mockMvc.perform(get("/music/people")) .andExpect(jsonPath("$.composers[?(@.name == 'Edvard Grieeeeeeg')]").doesNotExist()) .andExpect(jsonPath("$.composers[?(@.name == 'Robert Schuuuuuuman')]").doesNotExist()) .andExpect(jsonPath("$.composers[4]").doesNotExist()); }
@Test public void hamcrestMatcherWithParameterizedJsonPath() throws Exception { String composerName = "$.composers[%s].name"; String performerName = "$.performers[%s].name"; this.mockMvc.perform(get("/music/people")) .andExpect(jsonPath(composerName, 0).value(startsWith("Johann"))) .andExpect(jsonPath(performerName, 0).value(endsWith("Ashkenazy"))) .andExpect(jsonPath(performerName, 1).value(containsString("di Me"))) .andExpect(jsonPath(composerName, 1).value(isIn(Arrays.asList("Johann Sebastian Bach", "Johannes Brahms")))); }
@Test void getPerson42() throws Exception { this.mockMvc.perform(get("/person/42").accept(MediaType.APPLICATION_JSON)) .andExpect(jsonPath("$.name", is("Dilbert"))); }
@Test void getPerson99() throws Exception { this.mockMvc.perform(get("/person/99").accept(MediaType.APPLICATION_JSON)) .andExpect(jsonPath("$.name", is("Wally"))); }
@Test void springMvcTest(WebApplicationContext wac) throws Exception { webAppContextSetup(wac).build() .perform(get("/person/42").accept(MediaType.APPLICATION_JSON)) .andExpect(status().isOk()) .andExpect(jsonPath("$.name", is("Dilbert"))); }
@Test void check_token_delete() throws Exception { check_token(MockMvcRequestBuilders.delete("/check_token"),status().isMethodNotAllowed()) .andExpect(jsonPath("$.error").value("method_not_allowed")) .andExpect(jsonPath("$.error_description").value(HtmlUtils.htmlEscape("Request method 'DELETE' not supported", "ISO-8859-1"))); }
@Test void assertion_missing() throws Exception { IdentityZone defaultZone = IdentityZone.getUaa(); createProvider(defaultZone, getTokenVerificationKey(originZone.getIdentityZone())); perform_grant_in_zone(defaultZone, null) .andExpect(status().isUnauthorized()) .andExpect(jsonPath("$.error").isNotEmpty()) .andExpect(jsonPath("$.error_description").isNotEmpty()) .andExpect(jsonPath("$.error_description").value("Assertion is missing")); }
@Test void test_Create_User_Too_Long_Password() throws Exception { String email = "joe@" + generator.generate().toLowerCase() + ".com"; ScimUser user = getScimUser(); user.setUserName(email); user.setPassword(new RandomValueStringGenerator(300).generate()); ResultActions result = createUserAndReturnResult(user, scimReadWriteToken, null, null); result.andExpect(status().isBadRequest()) .andExpect(jsonPath("$.error").value("invalid_password")) .andExpect(jsonPath("$.message").value("Password must be no more than 255 characters in length.")) .andExpect(jsonPath("$.error_description").value("Password must be no more than 255 characters in length.")); }
@Test public void queryParameter() throws Exception { standaloneSetup(new PersonController()).build() .perform(get("/search?name=George").accept(MediaType.APPLICATION_JSON)) .andExpect(status().isOk()) .andExpect(content().contentType("application/json;charset=UTF-8")) .andExpect(jsonPath("$.name").value("George")); }
@Test public void json() throws Exception { standaloneSetup(new PersonController()).build() .perform(get("/person/Lee").accept(MediaType.APPLICATION_JSON)) .andExpect(status().isOk()) .andExpect(content().contentType("application/json;charset=UTF-8")) .andExpect(jsonPath("$.name").value("Lee")); }
private void resetPassword(String defaultPassword) throws Exception { String code = getExpiringCode(null, null); MockHttpServletRequestBuilder post = post("/password_change") .header("Authorization", "Bearer " + loginToken) .contentType(APPLICATION_JSON) .content("{\"code\":\"" + code + "\",\"new_password\":\"" + defaultPassword + "\"}") .accept(APPLICATION_JSON); getMockMvc().perform(post) .andExpect(status().isOk()) .andExpect(jsonPath("$.user_id").exists()) .andExpect(jsonPath("$.username").value(user.getUserName())); }
@Test public void testJsonOnly() throws Exception { standaloneSetup(new PersonController()).setSingleView(new MappingJackson2JsonView()).build() .perform(get("/person/Corea")) .andExpect(status().isOk()) .andExpect(content().contentType(MediaType.APPLICATION_JSON)) .andExpect(jsonPath("$.person.name").value("Corea")); }
@Test void default_zone_jwt_grant() throws Exception { IdentityZone defaultZone = IdentityZone.getUaa(); createProvider(defaultZone, getTokenVerificationKey(originZone.getIdentityZone())); perform_grant_in_zone(defaultZone, getUaaIdToken(originZone.getIdentityZone(), originClient, originUser)) .andExpect(status().isOk()) .andExpect(jsonPath("$.access_token").isNotEmpty()); }
@Test void defaultZoneJwtGrantWithInternalIdp() throws Exception { BaseClientDetails defaultZoneClient = setUpClients(generator.generate(), "", "openid", "password", true); defaultZoneClient.setClientSecret(SECRET); IdentityZone defaultZone = IdentityZone.getUaa(); ScimUser defaultZoneUser = createUser(defaultZone); perform_grant_in_zone(defaultZone, getUaaIdToken(defaultZone, defaultZoneClient, defaultZoneUser)) .andExpect(status().isOk()) .andExpect(jsonPath("$.access_token").isNotEmpty()); }
.andExpect(status().isOk()) .andExpect(content().contentType(MediaType.APPLICATION_JSON)) .andExpect(jsonPath("$.person.name").value("Corea"));