@Test public void statusRanges() throws Exception { for (HttpStatus status : HttpStatus.values()) { MockHttpServletResponse response = new MockHttpServletResponse(); response.setStatus(status.value()); MvcResult mvcResult = new StubMvcResult(request, null, null, null, null, null, response); switch (status.series().value()) { case 1: this.matchers.is1xxInformational().match(mvcResult); break; case 2: this.matchers.is2xxSuccessful().match(mvcResult); break; case 3: this.matchers.is3xxRedirection().match(mvcResult); break; case 4: this.matchers.is4xxClientError().match(mvcResult); break; case 5: this.matchers.is5xxServerError().match(mvcResult); break; default: fail("Unexpected range for status code value " + status); } } }
.session(session) .with(cookieCsrf())) .andExpect(status().is2xxSuccessful()) .andExpect(view().name("mfa/enter_code")); .session(session) .with(cookieCsrf())) .andExpect(status().is2xxSuccessful()) .andExpect(view().name("mfa/enter_code"));
authRequest.param(OAuth2Utils.SCOPE, scope); result = mockMvc.perform(authRequest).andExpect(status().is2xxSuccessful()).andReturn(); OAuthToken oauthToken = JsonUtils.readValue(result.getResponse().getContentAsString(), OAuthToken.class);
.param("username", developer.getUserName()) .param("password", SECRET)) .andExpect(status().is2xxSuccessful()); .param("username", developer.getUserName()) .param("password", SECRET)) .andExpect(status().is2xxSuccessful());
.param("code", code) .param(OAuth2Utils.REDIRECT_URI, "http://localhost/test"); result = mockMvc.perform(authRequest).andDo(print()).andExpect(status().is2xxSuccessful()).andReturn(); OAuthToken oauthToken = JsonUtils.readValue(result.getResponse().getContentAsString(), OAuthToken.class);
@Test public void whenPostOptionsRetentionTimeDoesNotExist() throws Exception { final EventType defaultEventType = buildDefaultEventType(); postEventType(defaultEventType).andExpect(status().is2xxSuccessful()); final ArgumentCaptor<EventTypeBase> eventTypeCaptor = ArgumentCaptor.forClass(EventTypeBase.class); verify(eventTypeRepository, times(1)).saveEventType(eventTypeCaptor.capture()); assertEquals(TOPIC_RETENTION_TIME_MS, eventTypeCaptor.getValue().getOptions().getRetentionTime().longValue()); }
@Test public void whenPostOptionsRetentionTimeExist() throws Exception { final EventType defaultEventType = buildDefaultEventType(); defaultEventType.getOptions().setRetentionTime(TOPIC_RETENTION_TIME_MS); postEventType(defaultEventType).andExpect(status().is2xxSuccessful()); final ArgumentCaptor<EventTypeBase> eventTypeCaptor = ArgumentCaptor.forClass(EventTypeBase.class); verify(eventTypeRepository, times(1)).saveEventType(eventTypeCaptor.capture()); assertEquals(TOPIC_RETENTION_TIME_MS, eventTypeCaptor.getValue().getOptions().getRetentionTime().longValue()); }
@Test public void testBoundsGetGoodSrs() throws Exception { this.mockMvc .perform(get("/rest/bounds/topp:states/EPSG:900913/java")) .andExpect(status().is2xxSuccessful()); }
@Test public void testConfirmationIsValid() throws Exception { testImpl.postRegister(new RegisterRequest().setEmail(TEST_EMAIL)) .expectStatus(status().is2xxSuccessful()) .fetchConfirmationEmailTokenFromDB(TEST_EMAIL) .getRegister() .expectStatus(status().is2xxSuccessful()); }
@Test public void whenGetOptionsRetentionTimeExist() throws Exception { final EventType defaultEventType = buildDefaultEventType(); defaultEventType.getOptions().setRetentionTime(TOPIC_RETENTION_TIME_MS); final String eventTypeName = defaultEventType.getName(); doReturn(defaultEventType).when(eventTypeRepository).findByName(eventTypeName); getEventType(eventTypeName) .andExpect(status().is2xxSuccessful()) .andExpect(content().string(new StringContains("\"options\":{\"retention_time\":172800000}"))); }
@Test public void shouldReturnHelloWorld() throws Exception { mockMvc.perform(get("/hello")) .andExpect(content().string("Hello World!")) .andExpect(status().is2xxSuccessful()); }
@Test public void testReloadConfiguration() throws Exception { String content = "reload_configuration=1"; this.mockMvc .perform( post("/rest/reload") .contentType(MediaType.APPLICATION_FORM_URLENCODED_VALUE) .content(content) .contextPath("")) .andExpect(status().is2xxSuccessful()); } }
@Test public void shouldReturnCurrentWeather() throws Exception { WeatherResponse weatherResponse = new WeatherResponse("Hamburg, 8°C raining"); given(weatherClient.fetchWeather()).willReturn(Optional.of(weatherResponse)); mockMvc.perform(get("/weather")) .andExpect(status().is2xxSuccessful()) .andExpect(content().string("Hamburg, 8°C raining")); } }
@Test public void shouldReturnFullName() throws Exception { Person peter = new Person("Peter", "Pan"); given(personRepository.findByLastName("Pan")).willReturn(Optional.of(peter)); mockMvc.perform(get("/hello/Pan")) .andExpect(content().string("Hello Peter Pan!")) .andExpect(status().is2xxSuccessful()); }
@Test public void testResourceFoundCSS() throws Exception { mockMvc.perform(get("/rest/web/test.css")) .andExpect(status().is2xxSuccessful()) .andExpect(header().string("Content-Type", startsWith("text/css"))); }
@Test public void testResourceFoundPNG() throws Exception { mockMvc.perform(get("/rest/web/test.png")) .andExpect(status().is2xxSuccessful()) .andExpect(header().string("Content-Type", startsWith("image/png"))); }
@Test public void testResourceFoundJS() throws Exception { mockMvc.perform(get("/rest/web/test.js")) .andExpect(status().is2xxSuccessful()) .andExpect(header().string("Content-Type", startsWith("text/javascript"))); }
@Test public void testConfirmationIsInvalidValid() throws Exception { testImpl.postRegister(new RegisterRequest().setEmail(TEST_EMAIL)) .expectStatus(status().is2xxSuccessful()) .setConfirmationEmailToken(INVALID_CONFIRMATION_EMAIL_TOKEN) .getRegister() .expectStatus(status().isUnauthorized()); }
@Test public void springHealth() throws Exception { mockMvc.perform(get("/spring-health")) .andDo(print()) .andExpect(status().is2xxSuccessful()) .andExpect(content().contentType(MediaType.APPLICATION_JSON)) .andExpect(content().json("{\"status\":\"Spring MVC: Up and Running!\"}")); } }
@Test @Ignore // failing because of strict validation public void returnsAllPagesStrict() throws Exception { String json = readJSONFromFile("all-customers.json"); mockMvc.perform(get("/customer")) .andDo(print()) .andExpect(status().is2xxSuccessful()) .andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON)) .andExpect(content().json(json, true)); }